StreamState.cs 14 KB

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