StreamState.cs 10 KB

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