StreamState.cs 14 KB

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