StreamState.cs 10 KB

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