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