StreamState.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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 StreamState()
  27. {
  28. PlayableStreamFileNames = new List<string>();
  29. }
  30. /// <summary>
  31. /// Gets or sets the log file stream.
  32. /// </summary>
  33. /// <value>The log file stream.</value>
  34. public Stream LogFileStream { get; set; }
  35. public string InputContainer { get; set; }
  36. public MediaStream AudioStream { get; set; }
  37. public MediaStream VideoStream { get; set; }
  38. public MediaStream SubtitleStream { get; set; }
  39. /// <summary>
  40. /// Gets or sets the iso mount.
  41. /// </summary>
  42. /// <value>The iso mount.</value>
  43. public IIsoMount IsoMount { get; set; }
  44. public string MediaPath { get; set; }
  45. public bool IsRemote { get; set; }
  46. public bool IsInputVideo { get; set; }
  47. public VideoType VideoType { get; set; }
  48. public IsoType? IsoType { get; set; }
  49. public List<string> PlayableStreamFileNames { get; set; }
  50. public string LiveTvStreamId { get; set; }
  51. public int SegmentLength = 10;
  52. public int HlsListSize;
  53. public long? RunTimeTicks;
  54. public string OutputAudioSync = "1";
  55. public string OutputVideoSync = "vfr";
  56. public List<string> SupportedAudioCodecs { get; set; }
  57. public StreamState(ILiveTvManager liveTvManager, ILogger logger)
  58. {
  59. _liveTvManager = liveTvManager;
  60. _logger = logger;
  61. SupportedAudioCodecs = new List<string>();
  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? OutputAudioChannels;
  134. public int? OutputAudioSampleRate;
  135. public int? OutputAudioBitrate;
  136. public int? OutputVideoBitrate;
  137. public string OutputContainer { get; set; }
  138. public DeviceProfile DeviceProfile { get; set; }
  139. public int? TotalOutputBitrate
  140. {
  141. get
  142. {
  143. return (OutputAudioBitrate ?? 0) + (OutputVideoBitrate ?? 0);
  144. }
  145. }
  146. public int? OutputWidth
  147. {
  148. get
  149. {
  150. if (VideoStream != null && VideoStream.Width.HasValue && VideoStream.Height.HasValue)
  151. {
  152. var size = new ImageSize
  153. {
  154. Width = VideoStream.Width.Value,
  155. Height = VideoStream.Height.Value
  156. };
  157. var newSize = DrawingUtils.Resize(size,
  158. VideoRequest.Width,
  159. VideoRequest.Height,
  160. VideoRequest.MaxWidth,
  161. VideoRequest.MaxHeight);
  162. return Convert.ToInt32(newSize.Width);
  163. }
  164. return VideoRequest.MaxWidth ?? VideoRequest.Width;
  165. }
  166. }
  167. public int? OutputHeight
  168. {
  169. get
  170. {
  171. if (VideoStream != null && VideoStream.Width.HasValue && VideoStream.Height.HasValue)
  172. {
  173. var size = new ImageSize
  174. {
  175. Width = VideoStream.Width.Value,
  176. Height = VideoStream.Height.Value
  177. };
  178. var newSize = DrawingUtils.Resize(size,
  179. VideoRequest.Width,
  180. VideoRequest.Height,
  181. VideoRequest.MaxWidth,
  182. VideoRequest.MaxHeight);
  183. return Convert.ToInt32(newSize.Height);
  184. }
  185. return VideoRequest.MaxHeight ?? VideoRequest.Height;
  186. }
  187. }
  188. /// <summary>
  189. /// Predicts the audio sample rate that will be in the output stream
  190. /// </summary>
  191. public int? TargetVideoBitDepth
  192. {
  193. get
  194. {
  195. var stream = VideoStream;
  196. return stream == null || !Request.Static ? null : stream.BitDepth;
  197. }
  198. }
  199. /// <summary>
  200. /// Predicts the audio sample rate that will be in the output stream
  201. /// </summary>
  202. public double? TargetFramerate
  203. {
  204. get
  205. {
  206. var stream = VideoStream;
  207. var requestedFramerate = VideoRequest.MaxFramerate ?? VideoRequest.Framerate;
  208. return requestedFramerate.HasValue && !Request.Static
  209. ? requestedFramerate
  210. : stream == null ? null : stream.AverageFrameRate ?? stream.RealFrameRate;
  211. }
  212. }
  213. /// <summary>
  214. /// Predicts the audio sample rate that will be in the output stream
  215. /// </summary>
  216. public double? TargetVideoLevel
  217. {
  218. get
  219. {
  220. var stream = VideoStream;
  221. return !string.IsNullOrEmpty(VideoRequest.Level) && !Request.Static
  222. ? double.Parse(VideoRequest.Level, CultureInfo.InvariantCulture)
  223. : stream == null ? null : stream.Level;
  224. }
  225. }
  226. public TransportStreamTimestamp TargetTimestamp
  227. {
  228. get
  229. {
  230. var defaultValue = string.Equals(OutputContainer, "m2ts", StringComparison.OrdinalIgnoreCase) ?
  231. TransportStreamTimestamp.Valid :
  232. TransportStreamTimestamp.None;
  233. return !Request.Static
  234. ? defaultValue
  235. : InputTimestamp;
  236. }
  237. }
  238. /// <summary>
  239. /// Predicts the audio sample rate that will be in the output stream
  240. /// </summary>
  241. public int? TargetPacketLength
  242. {
  243. get
  244. {
  245. var stream = VideoStream;
  246. return !Request.Static
  247. ? null
  248. : stream == null ? null : stream.PacketLength;
  249. }
  250. }
  251. /// <summary>
  252. /// Predicts the audio sample rate that will be in the output stream
  253. /// </summary>
  254. public string TargetVideoProfile
  255. {
  256. get
  257. {
  258. var stream = VideoStream;
  259. return !string.IsNullOrEmpty(VideoRequest.Profile) && !Request.Static
  260. ? VideoRequest.Profile
  261. : stream == null ? null : stream.Profile;
  262. }
  263. }
  264. }
  265. }