StreamState.cs 14 KB

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