StreamState.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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 bool EnableDlnaHeaders { get; set; }
  118. public void Dispose()
  119. {
  120. DisposeTranscodingThrottler();
  121. DisposeLiveStream();
  122. DisposeLogStream();
  123. DisposeIsoMount();
  124. TranscodingJob = null;
  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 async void DisposeLiveStream()
  157. {
  158. if (MediaSource.RequiresClosing && string.IsNullOrWhiteSpace(Request.LiveStreamId) && !string.IsNullOrWhiteSpace(MediaSource.LiveStreamId))
  159. {
  160. try
  161. {
  162. await _mediaSourceManager.CloseLiveStream(MediaSource.LiveStreamId).ConfigureAwait(false);
  163. }
  164. catch (Exception ex)
  165. {
  166. _logger.ErrorException("Error closing media source", ex);
  167. }
  168. }
  169. }
  170. public string OutputFilePath { get; set; }
  171. public string ActualOutputVideoCodec
  172. {
  173. get
  174. {
  175. var codec = OutputVideoCodec;
  176. if (string.Equals(codec, "copy", StringComparison.OrdinalIgnoreCase))
  177. {
  178. var stream = VideoStream;
  179. if (stream != null)
  180. {
  181. return stream.Codec;
  182. }
  183. return null;
  184. }
  185. return codec;
  186. }
  187. }
  188. public string ActualOutputAudioCodec
  189. {
  190. get
  191. {
  192. var codec = OutputAudioCodec;
  193. if (string.Equals(codec, "copy", StringComparison.OrdinalIgnoreCase))
  194. {
  195. var stream = AudioStream;
  196. if (stream != null)
  197. {
  198. return stream.Codec;
  199. }
  200. return null;
  201. }
  202. return codec;
  203. }
  204. }
  205. public DeviceProfile DeviceProfile { get; set; }
  206. public int? TotalOutputBitrate
  207. {
  208. get
  209. {
  210. return (OutputAudioBitrate ?? 0) + (OutputVideoBitrate ?? 0);
  211. }
  212. }
  213. public int? OutputWidth
  214. {
  215. get
  216. {
  217. if (VideoStream != null && VideoStream.Width.HasValue && VideoStream.Height.HasValue)
  218. {
  219. var size = new ImageSize
  220. {
  221. Width = VideoStream.Width.Value,
  222. Height = VideoStream.Height.Value
  223. };
  224. var newSize = DrawingUtils.Resize(size,
  225. VideoRequest.Width,
  226. VideoRequest.Height,
  227. VideoRequest.MaxWidth,
  228. VideoRequest.MaxHeight);
  229. return Convert.ToInt32(newSize.Width);
  230. }
  231. if (VideoRequest == null)
  232. {
  233. return null;
  234. }
  235. return VideoRequest.MaxWidth ?? VideoRequest.Width;
  236. }
  237. }
  238. public int? OutputHeight
  239. {
  240. get
  241. {
  242. if (VideoStream != null && VideoStream.Width.HasValue && VideoStream.Height.HasValue)
  243. {
  244. var size = new ImageSize
  245. {
  246. Width = VideoStream.Width.Value,
  247. Height = VideoStream.Height.Value
  248. };
  249. var newSize = DrawingUtils.Resize(size,
  250. VideoRequest.Width,
  251. VideoRequest.Height,
  252. VideoRequest.MaxWidth,
  253. VideoRequest.MaxHeight);
  254. return Convert.ToInt32(newSize.Height);
  255. }
  256. if (VideoRequest == null)
  257. {
  258. return null;
  259. }
  260. return VideoRequest.MaxHeight ?? VideoRequest.Height;
  261. }
  262. }
  263. /// <summary>
  264. /// Predicts the audio sample rate that will be in the output stream
  265. /// </summary>
  266. public int? TargetVideoBitDepth
  267. {
  268. get
  269. {
  270. var stream = VideoStream;
  271. return stream == null || !Request.Static ? null : stream.BitDepth;
  272. }
  273. }
  274. /// <summary>
  275. /// Gets the target reference frames.
  276. /// </summary>
  277. /// <value>The target reference frames.</value>
  278. public int? TargetRefFrames
  279. {
  280. get
  281. {
  282. var stream = VideoStream;
  283. return stream == null || !Request.Static ? null : stream.RefFrames;
  284. }
  285. }
  286. public int? TargetVideoStreamCount
  287. {
  288. get
  289. {
  290. if (Request.Static)
  291. {
  292. return GetMediaStreamCount(MediaStreamType.Video, int.MaxValue);
  293. }
  294. return GetMediaStreamCount(MediaStreamType.Video, 1);
  295. }
  296. }
  297. public int? TargetAudioStreamCount
  298. {
  299. get
  300. {
  301. if (Request.Static)
  302. {
  303. return GetMediaStreamCount(MediaStreamType.Audio, int.MaxValue);
  304. }
  305. return GetMediaStreamCount(MediaStreamType.Audio, 1);
  306. }
  307. }
  308. private int? GetMediaStreamCount(MediaStreamType type, int limit)
  309. {
  310. var count = MediaSource.GetStreamCount(type);
  311. if (count.HasValue)
  312. {
  313. count = Math.Min(count.Value, limit);
  314. }
  315. return count;
  316. }
  317. /// <summary>
  318. /// Predicts the audio sample rate that will be in the output stream
  319. /// </summary>
  320. public float? TargetFramerate
  321. {
  322. get
  323. {
  324. var stream = VideoStream;
  325. var requestedFramerate = VideoRequest.MaxFramerate ?? VideoRequest.Framerate;
  326. return requestedFramerate.HasValue && !Request.Static
  327. ? requestedFramerate
  328. : stream == null ? null : stream.AverageFrameRate ?? stream.RealFrameRate;
  329. }
  330. }
  331. public TransportStreamTimestamp TargetTimestamp
  332. {
  333. get
  334. {
  335. var defaultValue = string.Equals(OutputContainer, "m2ts", StringComparison.OrdinalIgnoreCase) ?
  336. TransportStreamTimestamp.Valid :
  337. TransportStreamTimestamp.None;
  338. return !Request.Static
  339. ? defaultValue
  340. : InputTimestamp;
  341. }
  342. }
  343. /// <summary>
  344. /// Predicts the audio sample rate that will be in the output stream
  345. /// </summary>
  346. public int? TargetPacketLength
  347. {
  348. get
  349. {
  350. var stream = VideoStream;
  351. return !Request.Static
  352. ? null
  353. : stream == null ? null : stream.PacketLength;
  354. }
  355. }
  356. /// <summary>
  357. /// Predicts the audio sample rate that will be in the output stream
  358. /// </summary>
  359. public string TargetVideoProfile
  360. {
  361. get
  362. {
  363. var stream = VideoStream;
  364. return !string.IsNullOrEmpty(VideoRequest.Profile) && !Request.Static
  365. ? VideoRequest.Profile
  366. : stream == null ? null : stream.Profile;
  367. }
  368. }
  369. public string TargetVideoCodecTag
  370. {
  371. get
  372. {
  373. var stream = VideoStream;
  374. return !Request.Static
  375. ? null
  376. : stream == null ? null : stream.CodecTag;
  377. }
  378. }
  379. public bool? IsTargetAnamorphic
  380. {
  381. get
  382. {
  383. if (Request.Static)
  384. {
  385. return VideoStream == null ? null : VideoStream.IsAnamorphic;
  386. }
  387. return false;
  388. }
  389. }
  390. public bool? IsTargetAVC
  391. {
  392. get
  393. {
  394. if (Request.Static)
  395. {
  396. return VideoStream == null ? null : VideoStream.IsAVC;
  397. }
  398. return true;
  399. }
  400. }
  401. public TranscodingJob TranscodingJob;
  402. public override void ReportTranscodingProgress(TimeSpan? transcodingPosition, float? framerate, double? percentComplete, long? bytesTranscoded, int? bitRate)
  403. {
  404. ApiEntryPoint.Instance.ReportTranscodingProgress(TranscodingJob, this, transcodingPosition, framerate, percentComplete, bytesTranscoded, bitRate);
  405. }
  406. }
  407. }