StreamState.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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 VideoType VideoType { get; set; }
  47. public IsoType? IsoType { get; set; }
  48. public List<string> PlayableStreamFileNames { get; set; }
  49. public string LiveTvStreamId { get; set; }
  50. public int SegmentLength = 6;
  51. public int HlsListSize
  52. {
  53. get
  54. {
  55. return ReadInputAtNativeFramerate ? 1000 : 0;
  56. }
  57. }
  58. public long? RunTimeTicks;
  59. public long? InputBitrate { get; set; }
  60. public long? InputFileSize { get; set; }
  61. public string OutputAudioSync = "1";
  62. public string OutputVideoSync = "vfr";
  63. public List<string> SupportedAudioCodecs { get; set; }
  64. public StreamState(ILiveTvManager liveTvManager, ILogger logger)
  65. {
  66. _liveTvManager = liveTvManager;
  67. _logger = logger;
  68. SupportedAudioCodecs = new List<string>();
  69. PlayableStreamFileNames = new List<string>();
  70. RemoteHttpHeaders = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
  71. AllMediaStreams = new List<MediaStream>();
  72. }
  73. public string InputAudioSync { get; set; }
  74. public string InputVideoSync { get; set; }
  75. public bool DeInterlace { get; set; }
  76. public bool ReadInputAtNativeFramerate { get; set; }
  77. public TransportStreamTimestamp InputTimestamp { get; set; }
  78. public string MimeType { get; set; }
  79. public bool EstimateContentLength { get; set; }
  80. public bool EnableMpegtsM2TsMode { get; set; }
  81. public TranscodeSeekInfo TranscodeSeekInfo { get; set; }
  82. public long? EncodingDurationTicks { get; set; }
  83. public string ItemType { get; set; }
  84. public string GetMimeType(string outputPath)
  85. {
  86. if (!string.IsNullOrEmpty(MimeType))
  87. {
  88. return MimeType;
  89. }
  90. return MimeTypes.GetMimeType(outputPath);
  91. }
  92. public void Dispose()
  93. {
  94. DisposeTranscodingThrottler();
  95. DisposeLiveStream();
  96. DisposeLogStream();
  97. DisposeIsoMount();
  98. }
  99. private void DisposeLogStream()
  100. {
  101. if (LogFileStream != null)
  102. {
  103. try
  104. {
  105. LogFileStream.Dispose();
  106. }
  107. catch (Exception ex)
  108. {
  109. _logger.ErrorException("Error disposing log stream", ex);
  110. }
  111. LogFileStream = null;
  112. }
  113. }
  114. private void DisposeTranscodingThrottler()
  115. {
  116. if (TranscodingThrottler != null)
  117. {
  118. try
  119. {
  120. TranscodingThrottler.Dispose();
  121. }
  122. catch (Exception ex)
  123. {
  124. _logger.ErrorException("Error disposing TranscodingThrottler", ex);
  125. }
  126. TranscodingThrottler = null;
  127. }
  128. }
  129. private void DisposeIsoMount()
  130. {
  131. if (IsoMount != null)
  132. {
  133. try
  134. {
  135. IsoMount.Dispose();
  136. }
  137. catch (Exception ex)
  138. {
  139. _logger.ErrorException("Error disposing iso mount", ex);
  140. }
  141. IsoMount = null;
  142. }
  143. }
  144. private async void DisposeLiveStream()
  145. {
  146. if (!string.IsNullOrEmpty(LiveTvStreamId))
  147. {
  148. try
  149. {
  150. await _liveTvManager.CloseLiveStream(LiveTvStreamId, CancellationToken.None).ConfigureAwait(false);
  151. }
  152. catch (Exception ex)
  153. {
  154. _logger.ErrorException("Error closing live tv stream", ex);
  155. }
  156. }
  157. }
  158. public int InternalSubtitleStreamOffset { get; set; }
  159. public string OutputFilePath { get; set; }
  160. public string OutputVideoCodec { get; set; }
  161. public string OutputAudioCodec { get; set; }
  162. public int? OutputAudioChannels;
  163. public int? OutputAudioSampleRate;
  164. public int? OutputAudioBitrate;
  165. public int? OutputVideoBitrate;
  166. public string ActualOutputVideoCodec
  167. {
  168. get
  169. {
  170. var codec = OutputVideoCodec;
  171. if (string.Equals(codec, "copy", StringComparison.OrdinalIgnoreCase))
  172. {
  173. var stream = VideoStream;
  174. if (stream != null)
  175. {
  176. return stream.Codec;
  177. }
  178. return null;
  179. }
  180. return codec;
  181. }
  182. }
  183. public string ActualOutputAudioCodec
  184. {
  185. get
  186. {
  187. var codec = OutputAudioCodec;
  188. if (string.Equals(codec, "copy", StringComparison.OrdinalIgnoreCase))
  189. {
  190. var stream = AudioStream;
  191. if (stream != null)
  192. {
  193. return stream.Codec;
  194. }
  195. return null;
  196. }
  197. return codec;
  198. }
  199. }
  200. public string OutputContainer { get; set; }
  201. public DeviceProfile DeviceProfile { get; set; }
  202. public int? TotalOutputBitrate
  203. {
  204. get
  205. {
  206. return (OutputAudioBitrate ?? 0) + (OutputVideoBitrate ?? 0);
  207. }
  208. }
  209. public int? OutputWidth
  210. {
  211. get
  212. {
  213. if (VideoStream != null && VideoStream.Width.HasValue && VideoStream.Height.HasValue)
  214. {
  215. var size = new ImageSize
  216. {
  217. Width = VideoStream.Width.Value,
  218. Height = VideoStream.Height.Value
  219. };
  220. var newSize = DrawingUtils.Resize(size,
  221. VideoRequest.Width,
  222. VideoRequest.Height,
  223. VideoRequest.MaxWidth,
  224. VideoRequest.MaxHeight);
  225. return Convert.ToInt32(newSize.Width);
  226. }
  227. if (VideoRequest == null)
  228. {
  229. return null;
  230. }
  231. return VideoRequest.MaxWidth ?? VideoRequest.Width;
  232. }
  233. }
  234. public int? OutputHeight
  235. {
  236. get
  237. {
  238. if (VideoStream != null && VideoStream.Width.HasValue && VideoStream.Height.HasValue)
  239. {
  240. var size = new ImageSize
  241. {
  242. Width = VideoStream.Width.Value,
  243. Height = VideoStream.Height.Value
  244. };
  245. var newSize = DrawingUtils.Resize(size,
  246. VideoRequest.Width,
  247. VideoRequest.Height,
  248. VideoRequest.MaxWidth,
  249. VideoRequest.MaxHeight);
  250. return Convert.ToInt32(newSize.Height);
  251. }
  252. if (VideoRequest == null)
  253. {
  254. return null;
  255. }
  256. return VideoRequest.MaxHeight ?? VideoRequest.Height;
  257. }
  258. }
  259. /// <summary>
  260. /// Predicts the audio sample rate that will be in the output stream
  261. /// </summary>
  262. public int? TargetVideoBitDepth
  263. {
  264. get
  265. {
  266. var stream = VideoStream;
  267. return stream == null || !Request.Static ? null : stream.BitDepth;
  268. }
  269. }
  270. /// <summary>
  271. /// Gets the target reference frames.
  272. /// </summary>
  273. /// <value>The target reference frames.</value>
  274. public int? TargetRefFrames
  275. {
  276. get
  277. {
  278. var stream = VideoStream;
  279. return stream == null || !Request.Static ? null : stream.RefFrames;
  280. }
  281. }
  282. /// <summary>
  283. /// Predicts the audio sample rate that will be in the output stream
  284. /// </summary>
  285. public float? TargetFramerate
  286. {
  287. get
  288. {
  289. var stream = VideoStream;
  290. var requestedFramerate = VideoRequest.MaxFramerate ?? VideoRequest.Framerate;
  291. return requestedFramerate.HasValue && !Request.Static
  292. ? requestedFramerate
  293. : stream == null ? null : stream.AverageFrameRate ?? stream.RealFrameRate;
  294. }
  295. }
  296. /// <summary>
  297. /// Predicts the audio sample rate that will be in the output stream
  298. /// </summary>
  299. public double? TargetVideoLevel
  300. {
  301. get
  302. {
  303. var stream = VideoStream;
  304. return !string.IsNullOrEmpty(VideoRequest.Level) && !Request.Static
  305. ? double.Parse(VideoRequest.Level, CultureInfo.InvariantCulture)
  306. : stream == null ? null : stream.Level;
  307. }
  308. }
  309. public TransportStreamTimestamp TargetTimestamp
  310. {
  311. get
  312. {
  313. var defaultValue = string.Equals(OutputContainer, "m2ts", StringComparison.OrdinalIgnoreCase) ?
  314. TransportStreamTimestamp.Valid :
  315. TransportStreamTimestamp.None;
  316. return !Request.Static
  317. ? defaultValue
  318. : InputTimestamp;
  319. }
  320. }
  321. /// <summary>
  322. /// Predicts the audio sample rate that will be in the output stream
  323. /// </summary>
  324. public int? TargetPacketLength
  325. {
  326. get
  327. {
  328. var stream = VideoStream;
  329. return !Request.Static
  330. ? null
  331. : stream == null ? null : stream.PacketLength;
  332. }
  333. }
  334. /// <summary>
  335. /// Predicts the audio sample rate that will be in the output stream
  336. /// </summary>
  337. public string TargetVideoProfile
  338. {
  339. get
  340. {
  341. var stream = VideoStream;
  342. return !string.IsNullOrEmpty(VideoRequest.Profile) && !Request.Static
  343. ? VideoRequest.Profile
  344. : stream == null ? null : stream.Profile;
  345. }
  346. }
  347. public bool? IsTargetAnamorphic
  348. {
  349. get
  350. {
  351. if (Request.Static)
  352. {
  353. return VideoStream == null ? null : VideoStream.IsAnamorphic;
  354. }
  355. return false;
  356. }
  357. }
  358. public bool? IsTargetCabac
  359. {
  360. get
  361. {
  362. if (Request.Static)
  363. {
  364. return VideoStream == null ? null : VideoStream.IsCabac;
  365. }
  366. return true;
  367. }
  368. }
  369. }
  370. }