StreamState.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  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 IsInputVideo { get; set; }
  48. public bool IsInputArchive { get; set; }
  49. public VideoType VideoType { get; set; }
  50. public IsoType? IsoType { get; set; }
  51. public List<string> PlayableStreamFileNames { 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(IMediaSourceManager mediaSourceManager, ILogger logger)
  68. {
  69. _mediaSourceManager = mediaSourceManager;
  70. _logger = logger;
  71. SupportedAudioCodecs = new List<string>();
  72. PlayableStreamFileNames = new List<string>();
  73. RemoteHttpHeaders = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
  74. }
  75. public string InputAudioSync { get; set; }
  76. public string InputVideoSync { get; set; }
  77. public bool DeInterlace { get; set; }
  78. public bool ReadInputAtNativeFramerate { get; set; }
  79. public TransportStreamTimestamp InputTimestamp { get; set; }
  80. public string MimeType { get; set; }
  81. public bool EstimateContentLength { get; set; }
  82. public bool EnableMpegtsM2TsMode { get; set; }
  83. public TranscodeSeekInfo TranscodeSeekInfo { get; set; }
  84. public long? EncodingDurationTicks { get; set; }
  85. public string GetMimeType(string outputPath)
  86. {
  87. if (!string.IsNullOrEmpty(MimeType))
  88. {
  89. return MimeType;
  90. }
  91. return MimeTypes.GetMimeType(outputPath);
  92. }
  93. public void Dispose()
  94. {
  95. DisposeTranscodingThrottler();
  96. DisposeLiveStream();
  97. DisposeLogStream();
  98. DisposeIsoMount();
  99. }
  100. private void DisposeLogStream()
  101. {
  102. if (LogFileStream != null)
  103. {
  104. try
  105. {
  106. LogFileStream.Dispose();
  107. }
  108. catch (Exception ex)
  109. {
  110. _logger.ErrorException("Error disposing log stream", ex);
  111. }
  112. LogFileStream = null;
  113. }
  114. }
  115. private void DisposeTranscodingThrottler()
  116. {
  117. if (TranscodingThrottler != null)
  118. {
  119. try
  120. {
  121. TranscodingThrottler.Dispose();
  122. }
  123. catch (Exception ex)
  124. {
  125. _logger.ErrorException("Error disposing TranscodingThrottler", ex);
  126. }
  127. TranscodingThrottler = null;
  128. }
  129. }
  130. private void DisposeIsoMount()
  131. {
  132. if (IsoMount != null)
  133. {
  134. try
  135. {
  136. IsoMount.Dispose();
  137. }
  138. catch (Exception ex)
  139. {
  140. _logger.ErrorException("Error disposing iso mount", ex);
  141. }
  142. IsoMount = null;
  143. }
  144. }
  145. private async void DisposeLiveStream()
  146. {
  147. if (MediaSource.RequiresClosing && string.IsNullOrWhiteSpace(Request.LiveStreamId))
  148. {
  149. try
  150. {
  151. await _mediaSourceManager.CloseLiveStream(MediaSource.LiveStreamId, CancellationToken.None).ConfigureAwait(false);
  152. }
  153. catch (Exception ex)
  154. {
  155. _logger.ErrorException("Error closing media source", ex);
  156. }
  157. }
  158. }
  159. public int InternalSubtitleStreamOffset { get; set; }
  160. public string OutputFilePath { get; set; }
  161. public string OutputVideoCodec { get; set; }
  162. public string OutputAudioCodec { get; set; }
  163. public int? OutputAudioChannels;
  164. public int? OutputAudioSampleRate;
  165. public int? OutputAudioBitrate;
  166. public int? OutputVideoBitrate;
  167. public string ActualOutputVideoCodec
  168. {
  169. get
  170. {
  171. var codec = OutputVideoCodec;
  172. if (string.Equals(codec, "copy", StringComparison.OrdinalIgnoreCase))
  173. {
  174. var stream = VideoStream;
  175. if (stream != null)
  176. {
  177. return stream.Codec;
  178. }
  179. return null;
  180. }
  181. return codec;
  182. }
  183. }
  184. public string ActualOutputAudioCodec
  185. {
  186. get
  187. {
  188. var codec = OutputAudioCodec;
  189. if (string.Equals(codec, "copy", StringComparison.OrdinalIgnoreCase))
  190. {
  191. var stream = AudioStream;
  192. if (stream != null)
  193. {
  194. return stream.Codec;
  195. }
  196. return null;
  197. }
  198. return codec;
  199. }
  200. }
  201. public string OutputContainer { get; set; }
  202. public DeviceProfile DeviceProfile { get; set; }
  203. public int? TotalOutputBitrate
  204. {
  205. get
  206. {
  207. return (OutputAudioBitrate ?? 0) + (OutputVideoBitrate ?? 0);
  208. }
  209. }
  210. public int? OutputWidth
  211. {
  212. get
  213. {
  214. if (VideoStream != null && VideoStream.Width.HasValue && VideoStream.Height.HasValue)
  215. {
  216. var size = new ImageSize
  217. {
  218. Width = VideoStream.Width.Value,
  219. Height = VideoStream.Height.Value
  220. };
  221. var newSize = DrawingUtils.Resize(size,
  222. VideoRequest.Width,
  223. VideoRequest.Height,
  224. VideoRequest.MaxWidth,
  225. VideoRequest.MaxHeight);
  226. return Convert.ToInt32(newSize.Width);
  227. }
  228. if (VideoRequest == null)
  229. {
  230. return null;
  231. }
  232. return VideoRequest.MaxWidth ?? VideoRequest.Width;
  233. }
  234. }
  235. public int? OutputHeight
  236. {
  237. get
  238. {
  239. if (VideoStream != null && VideoStream.Width.HasValue && VideoStream.Height.HasValue)
  240. {
  241. var size = new ImageSize
  242. {
  243. Width = VideoStream.Width.Value,
  244. Height = VideoStream.Height.Value
  245. };
  246. var newSize = DrawingUtils.Resize(size,
  247. VideoRequest.Width,
  248. VideoRequest.Height,
  249. VideoRequest.MaxWidth,
  250. VideoRequest.MaxHeight);
  251. return Convert.ToInt32(newSize.Height);
  252. }
  253. if (VideoRequest == null)
  254. {
  255. return null;
  256. }
  257. return VideoRequest.MaxHeight ?? VideoRequest.Height;
  258. }
  259. }
  260. /// <summary>
  261. /// Predicts the audio sample rate that will be in the output stream
  262. /// </summary>
  263. public int? TargetVideoBitDepth
  264. {
  265. get
  266. {
  267. var stream = VideoStream;
  268. return stream == null || !Request.Static ? null : stream.BitDepth;
  269. }
  270. }
  271. /// <summary>
  272. /// Gets the target reference frames.
  273. /// </summary>
  274. /// <value>The target reference frames.</value>
  275. public int? TargetRefFrames
  276. {
  277. get
  278. {
  279. var stream = VideoStream;
  280. return stream == null || !Request.Static ? null : stream.RefFrames;
  281. }
  282. }
  283. public int? TargetVideoStreamCount
  284. {
  285. get
  286. {
  287. if (Request.Static)
  288. {
  289. return GetMediaStreamCount(MediaStreamType.Video, int.MaxValue);
  290. }
  291. return GetMediaStreamCount(MediaStreamType.Video, 1);
  292. }
  293. }
  294. public int? TargetAudioStreamCount
  295. {
  296. get
  297. {
  298. if (Request.Static)
  299. {
  300. return GetMediaStreamCount(MediaStreamType.Audio, int.MaxValue);
  301. }
  302. return GetMediaStreamCount(MediaStreamType.Audio, 1);
  303. }
  304. }
  305. private int? GetMediaStreamCount(MediaStreamType type, int limit)
  306. {
  307. var count = MediaSource.GetStreamCount(type);
  308. if (count.HasValue)
  309. {
  310. count = Math.Min(count.Value, limit);
  311. }
  312. return count;
  313. }
  314. /// <summary>
  315. /// Predicts the audio sample rate that will be in the output stream
  316. /// </summary>
  317. public float? TargetFramerate
  318. {
  319. get
  320. {
  321. var stream = VideoStream;
  322. var requestedFramerate = VideoRequest.MaxFramerate ?? VideoRequest.Framerate;
  323. return requestedFramerate.HasValue && !Request.Static
  324. ? requestedFramerate
  325. : stream == null ? null : stream.AverageFrameRate ?? stream.RealFrameRate;
  326. }
  327. }
  328. /// <summary>
  329. /// Predicts the audio sample rate that will be in the output stream
  330. /// </summary>
  331. public double? TargetVideoLevel
  332. {
  333. get
  334. {
  335. var stream = VideoStream;
  336. return !string.IsNullOrEmpty(VideoRequest.Level) && !Request.Static
  337. ? double.Parse(VideoRequest.Level, CultureInfo.InvariantCulture)
  338. : stream == null ? null : stream.Level;
  339. }
  340. }
  341. public TransportStreamTimestamp TargetTimestamp
  342. {
  343. get
  344. {
  345. var defaultValue = string.Equals(OutputContainer, "m2ts", StringComparison.OrdinalIgnoreCase) ?
  346. TransportStreamTimestamp.Valid :
  347. TransportStreamTimestamp.None;
  348. return !Request.Static
  349. ? defaultValue
  350. : InputTimestamp;
  351. }
  352. }
  353. /// <summary>
  354. /// Predicts the audio sample rate that will be in the output stream
  355. /// </summary>
  356. public int? TargetPacketLength
  357. {
  358. get
  359. {
  360. var stream = VideoStream;
  361. return !Request.Static
  362. ? null
  363. : stream == null ? null : stream.PacketLength;
  364. }
  365. }
  366. /// <summary>
  367. /// Predicts the audio sample rate that will be in the output stream
  368. /// </summary>
  369. public string TargetVideoProfile
  370. {
  371. get
  372. {
  373. var stream = VideoStream;
  374. return !string.IsNullOrEmpty(VideoRequest.Profile) && !Request.Static
  375. ? VideoRequest.Profile
  376. : stream == null ? null : stream.Profile;
  377. }
  378. }
  379. public bool? IsTargetAnamorphic
  380. {
  381. get
  382. {
  383. if (Request.Static)
  384. {
  385. return VideoStream == null ? null : VideoStream.IsAnamorphic;
  386. }
  387. return false;
  388. }
  389. }
  390. public bool? IsTargetCabac
  391. {
  392. get
  393. {
  394. if (Request.Static)
  395. {
  396. return VideoStream == null ? null : VideoStream.IsCabac;
  397. }
  398. return true;
  399. }
  400. }
  401. }
  402. }