StreamState.cs 14 KB

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