StreamState.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. using System;
  2. using System.IO;
  3. using MediaBrowser.Controller.Library;
  4. using MediaBrowser.Controller.MediaEncoding;
  5. using MediaBrowser.Model.Dlna;
  6. using MediaBrowser.Model.Net;
  7. using Microsoft.Extensions.Logging;
  8. namespace MediaBrowser.Api.Playback
  9. {
  10. public class StreamState : EncodingJobInfo, IDisposable
  11. {
  12. private readonly ILogger _logger;
  13. private readonly IMediaSourceManager _mediaSourceManager;
  14. public string RequestedUrl { get; set; }
  15. public StreamRequest Request
  16. {
  17. get => (StreamRequest)BaseRequest;
  18. set
  19. {
  20. BaseRequest = value;
  21. IsVideoRequest = VideoRequest != null;
  22. }
  23. }
  24. public TranscodingThrottler TranscodingThrottler { get; set; }
  25. public VideoStreamRequest VideoRequest => Request as VideoStreamRequest;
  26. /// <summary>
  27. /// Gets or sets the log file stream.
  28. /// </summary>
  29. /// <value>The log file stream.</value>
  30. public Stream LogFileStream { get; set; }
  31. public IDirectStreamProvider DirectStreamProvider { get; set; }
  32. public string WaitForPath { get; set; }
  33. public bool IsOutputVideo => Request is VideoStreamRequest;
  34. public int SegmentLength
  35. {
  36. get
  37. {
  38. if (Request.SegmentLength.HasValue)
  39. {
  40. return Request.SegmentLength.Value;
  41. }
  42. if (string.Equals(OutputVideoCodec, "copy", StringComparison.OrdinalIgnoreCase))
  43. {
  44. var userAgent = UserAgent ?? string.Empty;
  45. if (userAgent.IndexOf("AppleTV", StringComparison.OrdinalIgnoreCase) != -1 ||
  46. userAgent.IndexOf("cfnetwork", StringComparison.OrdinalIgnoreCase) != -1 ||
  47. userAgent.IndexOf("ipad", StringComparison.OrdinalIgnoreCase) != -1 ||
  48. userAgent.IndexOf("iphone", StringComparison.OrdinalIgnoreCase) != -1 ||
  49. userAgent.IndexOf("ipod", StringComparison.OrdinalIgnoreCase) != -1)
  50. {
  51. if (IsSegmentedLiveStream)
  52. {
  53. return 6;
  54. }
  55. return 6;
  56. }
  57. if (IsSegmentedLiveStream)
  58. {
  59. return 3;
  60. }
  61. return 6;
  62. }
  63. return 3;
  64. }
  65. }
  66. public int MinSegments
  67. {
  68. get
  69. {
  70. if (Request.MinSegments.HasValue)
  71. {
  72. return Request.MinSegments.Value;
  73. }
  74. return SegmentLength >= 10 ? 2 : 3;
  75. }
  76. }
  77. public int HlsListSize => 0;
  78. public string UserAgent { get; set; }
  79. public StreamState(IMediaSourceManager mediaSourceManager, ILogger logger, TranscodingJobType transcodingType)
  80. : base(logger, mediaSourceManager, transcodingType)
  81. {
  82. _mediaSourceManager = mediaSourceManager;
  83. _logger = logger;
  84. }
  85. public string MimeType { get; set; }
  86. public bool EstimateContentLength { get; set; }
  87. public TranscodeSeekInfo TranscodeSeekInfo { get; set; }
  88. public long? EncodingDurationTicks { get; set; }
  89. public string GetMimeType(string outputPath, bool enableStreamDefault = true)
  90. {
  91. if (!string.IsNullOrEmpty(MimeType))
  92. {
  93. return MimeType;
  94. }
  95. return MimeTypes.GetMimeType(outputPath, enableStreamDefault);
  96. }
  97. public bool EnableDlnaHeaders { get; set; }
  98. public void Dispose()
  99. {
  100. DisposeTranscodingThrottler();
  101. DisposeLiveStream();
  102. DisposeLogStream();
  103. TranscodingJob = null;
  104. }
  105. private void DisposeLogStream()
  106. {
  107. if (LogFileStream != null)
  108. {
  109. try
  110. {
  111. LogFileStream.Dispose();
  112. }
  113. catch (Exception ex)
  114. {
  115. _logger.LogError(ex, "Error disposing log stream");
  116. }
  117. LogFileStream = null;
  118. }
  119. }
  120. private void DisposeTranscodingThrottler()
  121. {
  122. if (TranscodingThrottler != null)
  123. {
  124. try
  125. {
  126. TranscodingThrottler.Dispose();
  127. }
  128. catch (Exception ex)
  129. {
  130. _logger.LogError(ex, "Error disposing TranscodingThrottler");
  131. }
  132. TranscodingThrottler = null;
  133. }
  134. }
  135. private async void DisposeLiveStream()
  136. {
  137. if (MediaSource.RequiresClosing && string.IsNullOrWhiteSpace(Request.LiveStreamId) && !string.IsNullOrWhiteSpace(MediaSource.LiveStreamId))
  138. {
  139. try
  140. {
  141. await _mediaSourceManager.CloseLiveStream(MediaSource.LiveStreamId).ConfigureAwait(false);
  142. }
  143. catch (Exception ex)
  144. {
  145. _logger.LogError(ex, "Error closing media source");
  146. }
  147. }
  148. }
  149. public string OutputFilePath { get; set; }
  150. public string ActualOutputVideoCodec
  151. {
  152. get
  153. {
  154. var codec = OutputVideoCodec;
  155. if (string.Equals(codec, "copy", StringComparison.OrdinalIgnoreCase))
  156. {
  157. var stream = VideoStream;
  158. if (stream != null)
  159. {
  160. return stream.Codec;
  161. }
  162. return null;
  163. }
  164. return codec;
  165. }
  166. }
  167. public string ActualOutputAudioCodec
  168. {
  169. get
  170. {
  171. var codec = OutputAudioCodec;
  172. if (string.Equals(codec, "copy", StringComparison.OrdinalIgnoreCase))
  173. {
  174. var stream = AudioStream;
  175. if (stream != null)
  176. {
  177. return stream.Codec;
  178. }
  179. return null;
  180. }
  181. return codec;
  182. }
  183. }
  184. public DeviceProfile DeviceProfile { get; set; }
  185. public TranscodingJob TranscodingJob;
  186. public override void ReportTranscodingProgress(TimeSpan? transcodingPosition, float framerate, double? percentComplete, long bytesTranscoded, int? bitRate)
  187. {
  188. ApiEntryPoint.Instance.ReportTranscodingProgress(TranscodingJob, this, transcodingPosition, 0, percentComplete, 0, bitRate);
  189. }
  190. }
  191. }