StreamState.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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 System;
  9. using System.Collections.Generic;
  10. using System.IO;
  11. using System.Threading;
  12. namespace MediaBrowser.Api.Playback
  13. {
  14. public class StreamState : IDisposable
  15. {
  16. private readonly ILogger _logger;
  17. private readonly ILiveTvManager _liveTvManager;
  18. public string RequestedUrl { get; set; }
  19. public StreamRequest Request { get; set; }
  20. public VideoStreamRequest VideoRequest
  21. {
  22. get { return Request as VideoStreamRequest; }
  23. }
  24. /// <summary>
  25. /// Gets or sets the log file stream.
  26. /// </summary>
  27. /// <value>The log file stream.</value>
  28. public Stream LogFileStream { get; set; }
  29. public MediaStream AudioStream { get; set; }
  30. public MediaStream VideoStream { get; set; }
  31. public MediaStream SubtitleStream { get; set; }
  32. /// <summary>
  33. /// Gets or sets the iso mount.
  34. /// </summary>
  35. /// <value>The iso mount.</value>
  36. public IIsoMount IsoMount { get; set; }
  37. public string MediaPath { get; set; }
  38. public bool IsRemote { get; set; }
  39. public bool IsInputVideo { get; set; }
  40. public VideoType VideoType { get; set; }
  41. public IsoType? IsoType { get; set; }
  42. public List<string> PlayableStreamFileNames { get; set; }
  43. public bool HasMediaStreams { get; set; }
  44. public string LiveTvStreamId { get; set; }
  45. public int SegmentLength = 10;
  46. public int HlsListSize;
  47. public long? RunTimeTicks;
  48. public string OutputAudioSync = "1";
  49. public string OutputVideoSync = "vfr";
  50. public List<string> SupportedAudioCodecs { get; set; }
  51. public StreamState(ILiveTvManager liveTvManager, ILogger logger)
  52. {
  53. _liveTvManager = liveTvManager;
  54. _logger = logger;
  55. SupportedAudioCodecs = new List<string>();
  56. }
  57. public string InputAudioSync { get; set; }
  58. public string InputVideoSync { get; set; }
  59. public bool DeInterlace { get; set; }
  60. public bool ReadInputAtNativeFramerate { get; set; }
  61. public string InputFormat { get; set; }
  62. public string InputVideoCodec { get; set; }
  63. public string InputAudioCodec { get; set; }
  64. public string MimeType { get; set; }
  65. public bool EstimateContentLength { get; set; }
  66. public bool EnableMpegtsM2TsMode { get; set; }
  67. public TranscodeSeekInfo TranscodeSeekInfo { get; set; }
  68. public string GetMimeType(string outputPath)
  69. {
  70. if (!string.IsNullOrEmpty(MimeType))
  71. {
  72. return MimeType;
  73. }
  74. return MimeTypes.GetMimeType(outputPath);
  75. }
  76. public void Dispose()
  77. {
  78. DisposeLiveStream();
  79. DisposeLogStream();
  80. DisposeIsoMount();
  81. }
  82. private void DisposeLogStream()
  83. {
  84. if (LogFileStream != null)
  85. {
  86. try
  87. {
  88. LogFileStream.Dispose();
  89. }
  90. catch (Exception ex)
  91. {
  92. _logger.ErrorException("Error disposing log stream", ex);
  93. }
  94. LogFileStream = null;
  95. }
  96. }
  97. private void DisposeIsoMount()
  98. {
  99. if (IsoMount != null)
  100. {
  101. try
  102. {
  103. IsoMount.Dispose();
  104. }
  105. catch (Exception ex)
  106. {
  107. _logger.ErrorException("Error disposing iso mount", ex);
  108. }
  109. IsoMount = null;
  110. }
  111. }
  112. private async void DisposeLiveStream()
  113. {
  114. if (!string.IsNullOrEmpty(LiveTvStreamId))
  115. {
  116. try
  117. {
  118. await _liveTvManager.CloseLiveStream(LiveTvStreamId, CancellationToken.None).ConfigureAwait(false);
  119. }
  120. catch (Exception ex)
  121. {
  122. _logger.ErrorException("Error closing live tv stream", ex);
  123. }
  124. }
  125. }
  126. public int? OutputAudioChannels;
  127. public int? OutputAudioSampleRate;
  128. public int? OutputAudioBitrate;
  129. public int? OutputVideoBitrate;
  130. public string OutputContainer { get; set; }
  131. public DeviceProfile DeviceProfile { get; set; }
  132. public int? TotalOutputBitrate
  133. {
  134. get
  135. {
  136. return (OutputAudioBitrate ?? 0) + (OutputVideoBitrate ?? 0);
  137. }
  138. }
  139. public int? OutputWidth
  140. {
  141. get
  142. {
  143. if (VideoStream != null && VideoStream.Width.HasValue && VideoStream.Height.HasValue)
  144. {
  145. var size = new ImageSize
  146. {
  147. Width = VideoStream.Width.Value,
  148. Height = VideoStream.Height.Value
  149. };
  150. var newSize = DrawingUtils.Resize(size,
  151. VideoRequest.Width,
  152. VideoRequest.Height,
  153. VideoRequest.MaxWidth,
  154. VideoRequest.MaxHeight);
  155. return Convert.ToInt32(newSize.Width);
  156. }
  157. return VideoRequest.MaxWidth ?? VideoRequest.Width;
  158. }
  159. }
  160. public int? OutputHeight
  161. {
  162. get
  163. {
  164. if (VideoStream != null && VideoStream.Width.HasValue && VideoStream.Height.HasValue)
  165. {
  166. var size = new ImageSize
  167. {
  168. Width = VideoStream.Width.Value,
  169. Height = VideoStream.Height.Value
  170. };
  171. var newSize = DrawingUtils.Resize(size,
  172. VideoRequest.Width,
  173. VideoRequest.Height,
  174. VideoRequest.MaxWidth,
  175. VideoRequest.MaxHeight);
  176. return Convert.ToInt32(newSize.Height);
  177. }
  178. return VideoRequest.MaxHeight ?? VideoRequest.Height;
  179. }
  180. }
  181. }
  182. }