StreamState.cs 11 KB

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