StreamState.cs 12 KB

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