StreamState.cs 13 KB

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