StreamState.cs 14 KB

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