StreamState.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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. using MediaBrowser.Controller.MediaEncoding;
  17. namespace MediaBrowser.Api.Playback
  18. {
  19. public class StreamState : EncodingJobInfo, IDisposable
  20. {
  21. private readonly ILogger _logger;
  22. private readonly IMediaSourceManager _mediaSourceManager;
  23. public string RequestedUrl { get; set; }
  24. public StreamRequest Request
  25. {
  26. get { return (StreamRequest)BaseRequest; }
  27. set
  28. {
  29. BaseRequest = value;
  30. IsVideoRequest = VideoRequest != null;
  31. }
  32. }
  33. public TranscodingThrottler TranscodingThrottler { get; set; }
  34. public VideoStreamRequest VideoRequest
  35. {
  36. get { return Request as VideoStreamRequest; }
  37. }
  38. /// <summary>
  39. /// Gets or sets the log file stream.
  40. /// </summary>
  41. /// <value>The log file stream.</value>
  42. public Stream LogFileStream { get; set; }
  43. public IDirectStreamProvider DirectStreamProvider { get; set; }
  44. public string WaitForPath { get; set; }
  45. public bool IsOutputVideo
  46. {
  47. get { return Request is VideoStreamRequest; }
  48. }
  49. public int SegmentLength
  50. {
  51. get
  52. {
  53. if (string.Equals(OutputVideoCodec, "copy", StringComparison.OrdinalIgnoreCase))
  54. {
  55. var userAgent = UserAgent ?? string.Empty;
  56. if (userAgent.IndexOf("AppleTV", StringComparison.OrdinalIgnoreCase) != -1)
  57. {
  58. return 10;
  59. }
  60. if (userAgent.IndexOf("cfnetwork", StringComparison.OrdinalIgnoreCase) != -1 ||
  61. userAgent.IndexOf("ipad", StringComparison.OrdinalIgnoreCase) != -1 ||
  62. userAgent.IndexOf("iphone", StringComparison.OrdinalIgnoreCase) != -1 ||
  63. userAgent.IndexOf("ipod", StringComparison.OrdinalIgnoreCase) != -1)
  64. {
  65. return 10;
  66. }
  67. if (IsSegmentedLiveStream)
  68. {
  69. return 3;
  70. }
  71. return 6;
  72. }
  73. return 3;
  74. }
  75. }
  76. public bool IsSegmentedLiveStream
  77. {
  78. get
  79. {
  80. return TranscodingType != TranscodingJobType.Progressive && !RunTimeTicks.HasValue;
  81. }
  82. }
  83. public int HlsListSize
  84. {
  85. get
  86. {
  87. return 0;
  88. }
  89. }
  90. public List<string> SupportedSubtitleCodecs { get; set; }
  91. public string UserAgent { get; set; }
  92. public TranscodingJobType TranscodingType { get; set; }
  93. public StreamState(IMediaSourceManager mediaSourceManager, ILogger logger, TranscodingJobType transcodingType)
  94. : base(logger)
  95. {
  96. _mediaSourceManager = mediaSourceManager;
  97. _logger = logger;
  98. SupportedSubtitleCodecs = new List<string>();
  99. TranscodingType = transcodingType;
  100. }
  101. public string MimeType { get; set; }
  102. public bool EstimateContentLength { get; set; }
  103. public bool EnableMpegtsM2TsMode { get; set; }
  104. public TranscodeSeekInfo TranscodeSeekInfo { get; set; }
  105. public long? EncodingDurationTicks { get; set; }
  106. public string GetMimeType(string outputPath)
  107. {
  108. if (!string.IsNullOrEmpty(MimeType))
  109. {
  110. return MimeType;
  111. }
  112. return MimeTypes.GetMimeType(outputPath);
  113. }
  114. public void Dispose()
  115. {
  116. DisposeTranscodingThrottler();
  117. DisposeLiveStream();
  118. DisposeLogStream();
  119. DisposeIsoMount();
  120. }
  121. private void DisposeLogStream()
  122. {
  123. if (LogFileStream != null)
  124. {
  125. try
  126. {
  127. LogFileStream.Dispose();
  128. }
  129. catch (Exception ex)
  130. {
  131. _logger.ErrorException("Error disposing log stream", ex);
  132. }
  133. LogFileStream = null;
  134. }
  135. }
  136. private void DisposeTranscodingThrottler()
  137. {
  138. if (TranscodingThrottler != null)
  139. {
  140. try
  141. {
  142. TranscodingThrottler.Dispose();
  143. }
  144. catch (Exception ex)
  145. {
  146. _logger.ErrorException("Error disposing TranscodingThrottler", ex);
  147. }
  148. TranscodingThrottler = null;
  149. }
  150. }
  151. private async void DisposeLiveStream()
  152. {
  153. if (MediaSource.RequiresClosing && string.IsNullOrWhiteSpace(Request.LiveStreamId) && !string.IsNullOrWhiteSpace(MediaSource.LiveStreamId))
  154. {
  155. try
  156. {
  157. await _mediaSourceManager.CloseLiveStream(MediaSource.LiveStreamId).ConfigureAwait(false);
  158. }
  159. catch (Exception ex)
  160. {
  161. _logger.ErrorException("Error closing media source", ex);
  162. }
  163. }
  164. }
  165. public string OutputFilePath { get; set; }
  166. public int? OutputAudioBitrate;
  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 DeviceProfile DeviceProfile { get; set; }
  202. public int? TotalOutputBitrate
  203. {
  204. get
  205. {
  206. return (OutputAudioBitrate ?? 0) + (OutputVideoBitrate ?? 0);
  207. }
  208. }
  209. public int? OutputWidth
  210. {
  211. get
  212. {
  213. if (VideoStream != null && VideoStream.Width.HasValue && VideoStream.Height.HasValue)
  214. {
  215. var size = new ImageSize
  216. {
  217. Width = VideoStream.Width.Value,
  218. Height = VideoStream.Height.Value
  219. };
  220. var newSize = DrawingUtils.Resize(size,
  221. VideoRequest.Width,
  222. VideoRequest.Height,
  223. VideoRequest.MaxWidth,
  224. VideoRequest.MaxHeight);
  225. return Convert.ToInt32(newSize.Width);
  226. }
  227. if (VideoRequest == null)
  228. {
  229. return null;
  230. }
  231. return VideoRequest.MaxWidth ?? VideoRequest.Width;
  232. }
  233. }
  234. public int? OutputHeight
  235. {
  236. get
  237. {
  238. if (VideoStream != null && VideoStream.Width.HasValue && VideoStream.Height.HasValue)
  239. {
  240. var size = new ImageSize
  241. {
  242. Width = VideoStream.Width.Value,
  243. Height = VideoStream.Height.Value
  244. };
  245. var newSize = DrawingUtils.Resize(size,
  246. VideoRequest.Width,
  247. VideoRequest.Height,
  248. VideoRequest.MaxWidth,
  249. VideoRequest.MaxHeight);
  250. return Convert.ToInt32(newSize.Height);
  251. }
  252. if (VideoRequest == null)
  253. {
  254. return null;
  255. }
  256. return VideoRequest.MaxHeight ?? VideoRequest.Height;
  257. }
  258. }
  259. /// <summary>
  260. /// Predicts the audio sample rate that will be in the output stream
  261. /// </summary>
  262. public int? TargetVideoBitDepth
  263. {
  264. get
  265. {
  266. var stream = VideoStream;
  267. return stream == null || !Request.Static ? null : stream.BitDepth;
  268. }
  269. }
  270. /// <summary>
  271. /// Gets the target reference frames.
  272. /// </summary>
  273. /// <value>The target reference frames.</value>
  274. public int? TargetRefFrames
  275. {
  276. get
  277. {
  278. var stream = VideoStream;
  279. return stream == null || !Request.Static ? null : stream.RefFrames;
  280. }
  281. }
  282. public int? TargetVideoStreamCount
  283. {
  284. get
  285. {
  286. if (Request.Static)
  287. {
  288. return GetMediaStreamCount(MediaStreamType.Video, int.MaxValue);
  289. }
  290. return GetMediaStreamCount(MediaStreamType.Video, 1);
  291. }
  292. }
  293. public int? TargetAudioStreamCount
  294. {
  295. get
  296. {
  297. if (Request.Static)
  298. {
  299. return GetMediaStreamCount(MediaStreamType.Audio, int.MaxValue);
  300. }
  301. return GetMediaStreamCount(MediaStreamType.Audio, 1);
  302. }
  303. }
  304. private int? GetMediaStreamCount(MediaStreamType type, int limit)
  305. {
  306. var count = MediaSource.GetStreamCount(type);
  307. if (count.HasValue)
  308. {
  309. count = Math.Min(count.Value, limit);
  310. }
  311. return count;
  312. }
  313. /// <summary>
  314. /// Predicts the audio sample rate that will be in the output stream
  315. /// </summary>
  316. public float? TargetFramerate
  317. {
  318. get
  319. {
  320. var stream = VideoStream;
  321. var requestedFramerate = VideoRequest.MaxFramerate ?? VideoRequest.Framerate;
  322. return requestedFramerate.HasValue && !Request.Static
  323. ? requestedFramerate
  324. : stream == null ? null : stream.AverageFrameRate ?? stream.RealFrameRate;
  325. }
  326. }
  327. public TransportStreamTimestamp TargetTimestamp
  328. {
  329. get
  330. {
  331. var defaultValue = string.Equals(OutputContainer, "m2ts", StringComparison.OrdinalIgnoreCase) ?
  332. TransportStreamTimestamp.Valid :
  333. TransportStreamTimestamp.None;
  334. return !Request.Static
  335. ? defaultValue
  336. : InputTimestamp;
  337. }
  338. }
  339. /// <summary>
  340. /// Predicts the audio sample rate that will be in the output stream
  341. /// </summary>
  342. public int? TargetPacketLength
  343. {
  344. get
  345. {
  346. var stream = VideoStream;
  347. return !Request.Static
  348. ? null
  349. : stream == null ? null : stream.PacketLength;
  350. }
  351. }
  352. /// <summary>
  353. /// Predicts the audio sample rate that will be in the output stream
  354. /// </summary>
  355. public string TargetVideoProfile
  356. {
  357. get
  358. {
  359. var stream = VideoStream;
  360. return !string.IsNullOrEmpty(VideoRequest.Profile) && !Request.Static
  361. ? VideoRequest.Profile
  362. : stream == null ? null : stream.Profile;
  363. }
  364. }
  365. public string TargetVideoCodecTag
  366. {
  367. get
  368. {
  369. var stream = VideoStream;
  370. return !Request.Static
  371. ? null
  372. : stream == null ? null : stream.CodecTag;
  373. }
  374. }
  375. public bool? IsTargetAnamorphic
  376. {
  377. get
  378. {
  379. if (Request.Static)
  380. {
  381. return VideoStream == null ? null : VideoStream.IsAnamorphic;
  382. }
  383. return false;
  384. }
  385. }
  386. public bool? IsTargetAVC
  387. {
  388. get
  389. {
  390. if (Request.Static)
  391. {
  392. return VideoStream == null ? null : VideoStream.IsAVC;
  393. }
  394. return true;
  395. }
  396. }
  397. }
  398. }