StreamState.cs 12 KB

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