StreamState.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. using MediaBrowser.Controller.LiveTv;
  2. using MediaBrowser.Model.Dlna;
  3. using MediaBrowser.Model.Drawing;
  4. using MediaBrowser.Model.Entities;
  5. using MediaBrowser.Model.IO;
  6. using MediaBrowser.Model.Logging;
  7. using MediaBrowser.Model.MediaInfo;
  8. using MediaBrowser.Model.Net;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Globalization;
  12. using System.IO;
  13. using System.Threading;
  14. namespace MediaBrowser.Api.Playback
  15. {
  16. public class StreamState : IDisposable
  17. {
  18. private readonly ILogger _logger;
  19. private readonly ILiveTvManager _liveTvManager;
  20. public string RequestedUrl { get; set; }
  21. public StreamRequest Request { get; set; }
  22. public TranscodingThrottler TranscodingThrottler { get; set; }
  23. public VideoStreamRequest VideoRequest
  24. {
  25. get { return Request as VideoStreamRequest; }
  26. }
  27. public Dictionary<string, string> RemoteHttpHeaders { get; set; }
  28. /// <summary>
  29. /// Gets or sets the log file stream.
  30. /// </summary>
  31. /// <value>The log file stream.</value>
  32. public Stream LogFileStream { get; set; }
  33. public string InputContainer { get; set; }
  34. public List<MediaStream> AllMediaStreams { get; set; }
  35. public MediaStream AudioStream { get; set; }
  36. public MediaStream VideoStream { get; set; }
  37. public MediaStream SubtitleStream { get; set; }
  38. /// <summary>
  39. /// Gets or sets the iso mount.
  40. /// </summary>
  41. /// <value>The iso mount.</value>
  42. public IIsoMount IsoMount { get; set; }
  43. public string MediaPath { get; set; }
  44. public string WaitForPath { get; set; }
  45. public MediaProtocol InputProtocol { get; set; }
  46. public bool IsInputVideo { get; set; }
  47. public bool IsInputArchive { get; set; }
  48. public VideoType VideoType { get; set; }
  49. public IsoType? IsoType { get; set; }
  50. public List<string> PlayableStreamFileNames { get; set; }
  51. public string LiveTvStreamId { get; set; }
  52. public int SegmentLength = 3;
  53. public bool EnableGenericHlsSegmenter = false;
  54. public int HlsListSize
  55. {
  56. get
  57. {
  58. return ReadInputAtNativeFramerate ? 1000 : 0;
  59. }
  60. }
  61. public long? RunTimeTicks;
  62. public long? InputBitrate { get; set; }
  63. public long? InputFileSize { get; set; }
  64. public string OutputAudioSync = "1";
  65. public string OutputVideoSync = "vfr";
  66. public List<string> SupportedAudioCodecs { get; set; }
  67. public StreamState(ILiveTvManager liveTvManager, ILogger logger)
  68. {
  69. _liveTvManager = liveTvManager;
  70. _logger = logger;
  71. SupportedAudioCodecs = new List<string>();
  72. PlayableStreamFileNames = new List<string>();
  73. RemoteHttpHeaders = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
  74. AllMediaStreams = new List<MediaStream>();
  75. }
  76. public string InputAudioSync { get; set; }
  77. public string InputVideoSync { get; set; }
  78. public bool DeInterlace { get; set; }
  79. public bool ReadInputAtNativeFramerate { get; set; }
  80. public TransportStreamTimestamp InputTimestamp { get; set; }
  81. public string MimeType { get; set; }
  82. public bool EstimateContentLength { get; set; }
  83. public bool EnableMpegtsM2TsMode { get; set; }
  84. public TranscodeSeekInfo TranscodeSeekInfo { get; set; }
  85. public long? EncodingDurationTicks { get; set; }
  86. public string ItemType { get; set; }
  87. public string ItemId { get; set; }
  88. public string GetMimeType(string outputPath)
  89. {
  90. if (!string.IsNullOrEmpty(MimeType))
  91. {
  92. return MimeType;
  93. }
  94. return MimeTypes.GetMimeType(outputPath);
  95. }
  96. public void Dispose()
  97. {
  98. DisposeTranscodingThrottler();
  99. DisposeLiveStream();
  100. DisposeLogStream();
  101. DisposeIsoMount();
  102. }
  103. private void DisposeLogStream()
  104. {
  105. if (LogFileStream != null)
  106. {
  107. try
  108. {
  109. LogFileStream.Dispose();
  110. }
  111. catch (Exception ex)
  112. {
  113. _logger.ErrorException("Error disposing log stream", ex);
  114. }
  115. LogFileStream = null;
  116. }
  117. }
  118. private void DisposeTranscodingThrottler()
  119. {
  120. if (TranscodingThrottler != null)
  121. {
  122. try
  123. {
  124. TranscodingThrottler.Dispose();
  125. }
  126. catch (Exception ex)
  127. {
  128. _logger.ErrorException("Error disposing TranscodingThrottler", ex);
  129. }
  130. TranscodingThrottler = null;
  131. }
  132. }
  133. private void DisposeIsoMount()
  134. {
  135. if (IsoMount != null)
  136. {
  137. try
  138. {
  139. IsoMount.Dispose();
  140. }
  141. catch (Exception ex)
  142. {
  143. _logger.ErrorException("Error disposing iso mount", ex);
  144. }
  145. IsoMount = null;
  146. }
  147. }
  148. private async void DisposeLiveStream()
  149. {
  150. if (!string.IsNullOrEmpty(LiveTvStreamId))
  151. {
  152. try
  153. {
  154. await _liveTvManager.CloseLiveStream(LiveTvStreamId, CancellationToken.None).ConfigureAwait(false);
  155. }
  156. catch (Exception ex)
  157. {
  158. _logger.ErrorException("Error closing live tv stream", ex);
  159. }
  160. }
  161. }
  162. public int InternalSubtitleStreamOffset { get; set; }
  163. public string OutputFilePath { get; set; }
  164. public string OutputVideoCodec { get; set; }
  165. public string OutputAudioCodec { get; set; }
  166. public int? OutputAudioChannels;
  167. public int? OutputAudioSampleRate;
  168. public int? OutputAudioBitrate;
  169. public int? OutputVideoBitrate;
  170. public string ActualOutputVideoCodec
  171. {
  172. get
  173. {
  174. var codec = OutputVideoCodec;
  175. if (string.Equals(codec, "copy", StringComparison.OrdinalIgnoreCase))
  176. {
  177. var stream = VideoStream;
  178. if (stream != null)
  179. {
  180. return stream.Codec;
  181. }
  182. return null;
  183. }
  184. return codec;
  185. }
  186. }
  187. public string ActualOutputAudioCodec
  188. {
  189. get
  190. {
  191. var codec = OutputAudioCodec;
  192. if (string.Equals(codec, "copy", StringComparison.OrdinalIgnoreCase))
  193. {
  194. var stream = AudioStream;
  195. if (stream != null)
  196. {
  197. return stream.Codec;
  198. }
  199. return null;
  200. }
  201. return codec;
  202. }
  203. }
  204. public string OutputContainer { get; set; }
  205. public DeviceProfile DeviceProfile { get; set; }
  206. public int? TotalOutputBitrate
  207. {
  208. get
  209. {
  210. return (OutputAudioBitrate ?? 0) + (OutputVideoBitrate ?? 0);
  211. }
  212. }
  213. public int? OutputWidth
  214. {
  215. get
  216. {
  217. if (VideoStream != null && VideoStream.Width.HasValue && VideoStream.Height.HasValue)
  218. {
  219. var size = new ImageSize
  220. {
  221. Width = VideoStream.Width.Value,
  222. Height = VideoStream.Height.Value
  223. };
  224. var newSize = DrawingUtils.Resize(size,
  225. VideoRequest.Width,
  226. VideoRequest.Height,
  227. VideoRequest.MaxWidth,
  228. VideoRequest.MaxHeight);
  229. return Convert.ToInt32(newSize.Width);
  230. }
  231. if (VideoRequest == null)
  232. {
  233. return null;
  234. }
  235. return VideoRequest.MaxWidth ?? VideoRequest.Width;
  236. }
  237. }
  238. public int? OutputHeight
  239. {
  240. get
  241. {
  242. if (VideoStream != null && VideoStream.Width.HasValue && VideoStream.Height.HasValue)
  243. {
  244. var size = new ImageSize
  245. {
  246. Width = VideoStream.Width.Value,
  247. Height = VideoStream.Height.Value
  248. };
  249. var newSize = DrawingUtils.Resize(size,
  250. VideoRequest.Width,
  251. VideoRequest.Height,
  252. VideoRequest.MaxWidth,
  253. VideoRequest.MaxHeight);
  254. return Convert.ToInt32(newSize.Height);
  255. }
  256. if (VideoRequest == null)
  257. {
  258. return null;
  259. }
  260. return VideoRequest.MaxHeight ?? VideoRequest.Height;
  261. }
  262. }
  263. /// <summary>
  264. /// Predicts the audio sample rate that will be in the output stream
  265. /// </summary>
  266. public int? TargetVideoBitDepth
  267. {
  268. get
  269. {
  270. var stream = VideoStream;
  271. return stream == null || !Request.Static ? null : stream.BitDepth;
  272. }
  273. }
  274. /// <summary>
  275. /// Gets the target reference frames.
  276. /// </summary>
  277. /// <value>The target reference frames.</value>
  278. public int? TargetRefFrames
  279. {
  280. get
  281. {
  282. var stream = VideoStream;
  283. return stream == null || !Request.Static ? null : stream.RefFrames;
  284. }
  285. }
  286. /// <summary>
  287. /// Predicts the audio sample rate that will be in the output stream
  288. /// </summary>
  289. public float? TargetFramerate
  290. {
  291. get
  292. {
  293. var stream = VideoStream;
  294. var requestedFramerate = VideoRequest.MaxFramerate ?? VideoRequest.Framerate;
  295. return requestedFramerate.HasValue && !Request.Static
  296. ? requestedFramerate
  297. : stream == null ? null : stream.AverageFrameRate ?? stream.RealFrameRate;
  298. }
  299. }
  300. /// <summary>
  301. /// Predicts the audio sample rate that will be in the output stream
  302. /// </summary>
  303. public double? TargetVideoLevel
  304. {
  305. get
  306. {
  307. var stream = VideoStream;
  308. return !string.IsNullOrEmpty(VideoRequest.Level) && !Request.Static
  309. ? double.Parse(VideoRequest.Level, CultureInfo.InvariantCulture)
  310. : stream == null ? null : stream.Level;
  311. }
  312. }
  313. public TransportStreamTimestamp TargetTimestamp
  314. {
  315. get
  316. {
  317. var defaultValue = string.Equals(OutputContainer, "m2ts", StringComparison.OrdinalIgnoreCase) ?
  318. TransportStreamTimestamp.Valid :
  319. TransportStreamTimestamp.None;
  320. return !Request.Static
  321. ? defaultValue
  322. : InputTimestamp;
  323. }
  324. }
  325. /// <summary>
  326. /// Predicts the audio sample rate that will be in the output stream
  327. /// </summary>
  328. public int? TargetPacketLength
  329. {
  330. get
  331. {
  332. var stream = VideoStream;
  333. return !Request.Static
  334. ? null
  335. : stream == null ? null : stream.PacketLength;
  336. }
  337. }
  338. /// <summary>
  339. /// Predicts the audio sample rate that will be in the output stream
  340. /// </summary>
  341. public string TargetVideoProfile
  342. {
  343. get
  344. {
  345. var stream = VideoStream;
  346. return !string.IsNullOrEmpty(VideoRequest.Profile) && !Request.Static
  347. ? VideoRequest.Profile
  348. : stream == null ? null : stream.Profile;
  349. }
  350. }
  351. public bool? IsTargetAnamorphic
  352. {
  353. get
  354. {
  355. if (Request.Static)
  356. {
  357. return VideoStream == null ? null : VideoStream.IsAnamorphic;
  358. }
  359. return false;
  360. }
  361. }
  362. public bool? IsTargetCabac
  363. {
  364. get
  365. {
  366. if (Request.Static)
  367. {
  368. return VideoStream == null ? null : VideoStream.IsCabac;
  369. }
  370. return true;
  371. }
  372. }
  373. }
  374. }