StreamState.cs 9.2 KB

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