StreamState.cs 11 KB

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