StreamState.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. using MediaBrowser.Controller.LiveTv;
  2. using MediaBrowser.Model.Dlna;
  3. using MediaBrowser.Model.Drawing;
  4. using MediaBrowser.Model.Entities;
  5. using MediaBrowser.Model.IO;
  6. using MediaBrowser.Model.Logging;
  7. using MediaBrowser.Model.MediaInfo;
  8. using MediaBrowser.Model.Net;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Globalization;
  12. using System.IO;
  13. using System.Threading;
  14. namespace MediaBrowser.Api.Playback
  15. {
  16. public class StreamState : IDisposable
  17. {
  18. private readonly ILogger _logger;
  19. private readonly ILiveTvManager _liveTvManager;
  20. public string RequestedUrl { get; set; }
  21. public StreamRequest Request { get; set; }
  22. public TranscodingThrottler TranscodingThrottler { get; set; }
  23. public VideoStreamRequest VideoRequest
  24. {
  25. get { return Request as VideoStreamRequest; }
  26. }
  27. public Dictionary<string, string> RemoteHttpHeaders { get; set; }
  28. /// <summary>
  29. /// Gets or sets the log file stream.
  30. /// </summary>
  31. /// <value>The log file stream.</value>
  32. public Stream LogFileStream { get; set; }
  33. public string InputContainer { get; set; }
  34. public List<MediaStream> AllMediaStreams { get; set; }
  35. public MediaStream AudioStream { get; set; }
  36. public MediaStream VideoStream { get; set; }
  37. public MediaStream SubtitleStream { get; set; }
  38. /// <summary>
  39. /// Gets or sets the iso mount.
  40. /// </summary>
  41. /// <value>The iso mount.</value>
  42. public IIsoMount IsoMount { get; set; }
  43. public string MediaPath { get; set; }
  44. public MediaProtocol InputProtocol { get; set; }
  45. public bool IsInputVideo { get; set; }
  46. public bool IsInputArchive { get; set; }
  47. public VideoType VideoType { get; set; }
  48. public IsoType? IsoType { get; set; }
  49. public List<string> PlayableStreamFileNames { get; set; }
  50. public string LiveTvStreamId { get; set; }
  51. public int SegmentLength = 3;
  52. public bool EnableGenericHlsSegmenter;
  53. public int HlsListSize
  54. {
  55. get
  56. {
  57. return ReadInputAtNativeFramerate ? 1000 : 0;
  58. }
  59. }
  60. public long? RunTimeTicks;
  61. public long? InputBitrate { get; set; }
  62. public long? InputFileSize { get; set; }
  63. public string OutputAudioSync = "1";
  64. public string OutputVideoSync = "vfr";
  65. public List<string> SupportedAudioCodecs { get; set; }
  66. public StreamState(ILiveTvManager liveTvManager, ILogger logger)
  67. {
  68. _liveTvManager = liveTvManager;
  69. _logger = logger;
  70. SupportedAudioCodecs = new List<string>();
  71. PlayableStreamFileNames = new List<string>();
  72. RemoteHttpHeaders = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
  73. AllMediaStreams = new List<MediaStream>();
  74. }
  75. public string InputAudioSync { get; set; }
  76. public string InputVideoSync { get; set; }
  77. public bool DeInterlace { get; set; }
  78. public bool ReadInputAtNativeFramerate { get; set; }
  79. public TransportStreamTimestamp InputTimestamp { get; set; }
  80. public string MimeType { get; set; }
  81. public bool EstimateContentLength { get; set; }
  82. public bool EnableMpegtsM2TsMode { get; set; }
  83. public TranscodeSeekInfo TranscodeSeekInfo { get; set; }
  84. public long? EncodingDurationTicks { get; set; }
  85. public string ItemType { get; set; }
  86. public string ItemId { get; set; }
  87. public string GetMimeType(string outputPath)
  88. {
  89. if (!string.IsNullOrEmpty(MimeType))
  90. {
  91. return MimeType;
  92. }
  93. return MimeTypes.GetMimeType(outputPath);
  94. }
  95. public void Dispose()
  96. {
  97. DisposeTranscodingThrottler();
  98. DisposeLiveStream();
  99. DisposeLogStream();
  100. DisposeIsoMount();
  101. }
  102. private void DisposeLogStream()
  103. {
  104. if (LogFileStream != null)
  105. {
  106. try
  107. {
  108. LogFileStream.Dispose();
  109. }
  110. catch (Exception ex)
  111. {
  112. _logger.ErrorException("Error disposing log stream", ex);
  113. }
  114. LogFileStream = null;
  115. }
  116. }
  117. private void DisposeTranscodingThrottler()
  118. {
  119. if (TranscodingThrottler != null)
  120. {
  121. try
  122. {
  123. TranscodingThrottler.Dispose();
  124. }
  125. catch (Exception ex)
  126. {
  127. _logger.ErrorException("Error disposing TranscodingThrottler", ex);
  128. }
  129. TranscodingThrottler = null;
  130. }
  131. }
  132. private void DisposeIsoMount()
  133. {
  134. if (IsoMount != null)
  135. {
  136. try
  137. {
  138. IsoMount.Dispose();
  139. }
  140. catch (Exception ex)
  141. {
  142. _logger.ErrorException("Error disposing iso mount", ex);
  143. }
  144. IsoMount = null;
  145. }
  146. }
  147. private async void DisposeLiveStream()
  148. {
  149. if (!string.IsNullOrEmpty(LiveTvStreamId))
  150. {
  151. try
  152. {
  153. await _liveTvManager.CloseLiveStream(LiveTvStreamId, CancellationToken.None).ConfigureAwait(false);
  154. }
  155. catch (Exception ex)
  156. {
  157. _logger.ErrorException("Error closing live tv stream", ex);
  158. }
  159. }
  160. }
  161. public int InternalSubtitleStreamOffset { get; set; }
  162. public string OutputFilePath { get; set; }
  163. public string OutputVideoCodec { get; set; }
  164. public string OutputAudioCodec { get; set; }
  165. public int? OutputAudioChannels;
  166. public int? OutputAudioSampleRate;
  167. public int? OutputAudioBitrate;
  168. public int? OutputVideoBitrate;
  169. public string ActualOutputVideoCodec
  170. {
  171. get
  172. {
  173. var codec = OutputVideoCodec;
  174. if (string.Equals(codec, "copy", StringComparison.OrdinalIgnoreCase))
  175. {
  176. var stream = VideoStream;
  177. if (stream != null)
  178. {
  179. return stream.Codec;
  180. }
  181. return null;
  182. }
  183. return codec;
  184. }
  185. }
  186. public string ActualOutputAudioCodec
  187. {
  188. get
  189. {
  190. var codec = OutputAudioCodec;
  191. if (string.Equals(codec, "copy", StringComparison.OrdinalIgnoreCase))
  192. {
  193. var stream = AudioStream;
  194. if (stream != null)
  195. {
  196. return stream.Codec;
  197. }
  198. return null;
  199. }
  200. return codec;
  201. }
  202. }
  203. public string OutputContainer { get; set; }
  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. /// <summary>
  286. /// Predicts the audio sample rate that will be in the output stream
  287. /// </summary>
  288. public float? TargetFramerate
  289. {
  290. get
  291. {
  292. var stream = VideoStream;
  293. var requestedFramerate = VideoRequest.MaxFramerate ?? VideoRequest.Framerate;
  294. return requestedFramerate.HasValue && !Request.Static
  295. ? requestedFramerate
  296. : stream == null ? null : stream.AverageFrameRate ?? stream.RealFrameRate;
  297. }
  298. }
  299. /// <summary>
  300. /// Predicts the audio sample rate that will be in the output stream
  301. /// </summary>
  302. public double? TargetVideoLevel
  303. {
  304. get
  305. {
  306. var stream = VideoStream;
  307. return !string.IsNullOrEmpty(VideoRequest.Level) && !Request.Static
  308. ? double.Parse(VideoRequest.Level, CultureInfo.InvariantCulture)
  309. : stream == null ? null : stream.Level;
  310. }
  311. }
  312. public TransportStreamTimestamp TargetTimestamp
  313. {
  314. get
  315. {
  316. var defaultValue = string.Equals(OutputContainer, "m2ts", StringComparison.OrdinalIgnoreCase) ?
  317. TransportStreamTimestamp.Valid :
  318. TransportStreamTimestamp.None;
  319. return !Request.Static
  320. ? defaultValue
  321. : InputTimestamp;
  322. }
  323. }
  324. /// <summary>
  325. /// Predicts the audio sample rate that will be in the output stream
  326. /// </summary>
  327. public int? TargetPacketLength
  328. {
  329. get
  330. {
  331. var stream = VideoStream;
  332. return !Request.Static
  333. ? null
  334. : stream == null ? null : stream.PacketLength;
  335. }
  336. }
  337. /// <summary>
  338. /// Predicts the audio sample rate that will be in the output stream
  339. /// </summary>
  340. public string TargetVideoProfile
  341. {
  342. get
  343. {
  344. var stream = VideoStream;
  345. return !string.IsNullOrEmpty(VideoRequest.Profile) && !Request.Static
  346. ? VideoRequest.Profile
  347. : stream == null ? null : stream.Profile;
  348. }
  349. }
  350. public bool? IsTargetAnamorphic
  351. {
  352. get
  353. {
  354. if (Request.Static)
  355. {
  356. return VideoStream == null ? null : VideoStream.IsAnamorphic;
  357. }
  358. return false;
  359. }
  360. }
  361. public bool? IsTargetCabac
  362. {
  363. get
  364. {
  365. if (Request.Static)
  366. {
  367. return VideoStream == null ? null : VideoStream.IsCabac;
  368. }
  369. return true;
  370. }
  371. }
  372. }
  373. }