StreamState.cs 15 KB

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