StreamState.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  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 = 3;
  57. public int HlsListSize
  58. {
  59. get
  60. {
  61. return ReadInputAtNativeFramerate ? 1000 : 0;
  62. }
  63. }
  64. public long? RunTimeTicks;
  65. public long? InputBitrate { get; set; }
  66. public long? InputFileSize { get; set; }
  67. public string OutputAudioSync = "1";
  68. public string OutputVideoSync = "vfr";
  69. public List<string> SupportedAudioCodecs { get; set; }
  70. public StreamState(IMediaSourceManager mediaSourceManager, ILogger logger)
  71. {
  72. _mediaSourceManager = mediaSourceManager;
  73. _logger = logger;
  74. SupportedAudioCodecs = new List<string>();
  75. PlayableStreamFileNames = new List<string>();
  76. RemoteHttpHeaders = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
  77. }
  78. public string InputAudioSync { get; set; }
  79. public string InputVideoSync { get; set; }
  80. public bool DeInterlace { get; set; }
  81. public bool ReadInputAtNativeFramerate { get; set; }
  82. public TransportStreamTimestamp InputTimestamp { get; set; }
  83. public string MimeType { get; set; }
  84. public bool EstimateContentLength { get; set; }
  85. public bool EnableMpegtsM2TsMode { get; set; }
  86. public TranscodeSeekInfo TranscodeSeekInfo { get; set; }
  87. public long? EncodingDurationTicks { 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 ((MediaSource.RequiresClosing) && string.IsNullOrWhiteSpace(Request.LiveStreamId))
  151. {
  152. try
  153. {
  154. await _mediaSourceManager.CloseLiveStream(MediaSource.LiveStreamId, CancellationToken.None).ConfigureAwait(false);
  155. }
  156. catch (Exception ex)
  157. {
  158. _logger.ErrorException("Error closing media source", 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. public int? TargetVideoStreamCount
  287. {
  288. get
  289. {
  290. if (Request.Static)
  291. {
  292. return GetMediaStreamCount(MediaStreamType.Video, int.MaxValue);
  293. }
  294. return GetMediaStreamCount(MediaStreamType.Video, 1);
  295. }
  296. }
  297. public int? TargetAudioStreamCount
  298. {
  299. get
  300. {
  301. if (Request.Static)
  302. {
  303. return GetMediaStreamCount(MediaStreamType.Audio, int.MaxValue);
  304. }
  305. return GetMediaStreamCount(MediaStreamType.Audio, 1);
  306. }
  307. }
  308. private int? GetMediaStreamCount(MediaStreamType type, int limit)
  309. {
  310. var count = MediaSource.GetStreamCount(type);
  311. if (count.HasValue)
  312. {
  313. count = Math.Min(count.Value, limit);
  314. }
  315. return count;
  316. }
  317. /// <summary>
  318. /// Predicts the audio sample rate that will be in the output stream
  319. /// </summary>
  320. public float? TargetFramerate
  321. {
  322. get
  323. {
  324. var stream = VideoStream;
  325. var requestedFramerate = VideoRequest.MaxFramerate ?? VideoRequest.Framerate;
  326. return requestedFramerate.HasValue && !Request.Static
  327. ? requestedFramerate
  328. : stream == null ? null : stream.AverageFrameRate ?? stream.RealFrameRate;
  329. }
  330. }
  331. /// <summary>
  332. /// Predicts the audio sample rate that will be in the output stream
  333. /// </summary>
  334. public double? TargetVideoLevel
  335. {
  336. get
  337. {
  338. var stream = VideoStream;
  339. return !string.IsNullOrEmpty(VideoRequest.Level) && !Request.Static
  340. ? double.Parse(VideoRequest.Level, CultureInfo.InvariantCulture)
  341. : stream == null ? null : stream.Level;
  342. }
  343. }
  344. public TransportStreamTimestamp TargetTimestamp
  345. {
  346. get
  347. {
  348. var defaultValue = string.Equals(OutputContainer, "m2ts", StringComparison.OrdinalIgnoreCase) ?
  349. TransportStreamTimestamp.Valid :
  350. TransportStreamTimestamp.None;
  351. return !Request.Static
  352. ? defaultValue
  353. : InputTimestamp;
  354. }
  355. }
  356. /// <summary>
  357. /// Predicts the audio sample rate that will be in the output stream
  358. /// </summary>
  359. public int? TargetPacketLength
  360. {
  361. get
  362. {
  363. var stream = VideoStream;
  364. return !Request.Static
  365. ? null
  366. : stream == null ? null : stream.PacketLength;
  367. }
  368. }
  369. /// <summary>
  370. /// Predicts the audio sample rate that will be in the output stream
  371. /// </summary>
  372. public string TargetVideoProfile
  373. {
  374. get
  375. {
  376. var stream = VideoStream;
  377. return !string.IsNullOrEmpty(VideoRequest.Profile) && !Request.Static
  378. ? VideoRequest.Profile
  379. : stream == null ? null : stream.Profile;
  380. }
  381. }
  382. public bool? IsTargetAnamorphic
  383. {
  384. get
  385. {
  386. if (Request.Static)
  387. {
  388. return VideoStream == null ? null : VideoStream.IsAnamorphic;
  389. }
  390. return false;
  391. }
  392. }
  393. public bool? IsTargetCabac
  394. {
  395. get
  396. {
  397. if (Request.Static)
  398. {
  399. return VideoStream == null ? null : VideoStream.IsCabac;
  400. }
  401. return true;
  402. }
  403. }
  404. }
  405. }