StreamState.cs 12 KB

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