StreamState.cs 9.9 KB

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