StreamState.cs 15 KB

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