StreamState.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 string UserAgent { get; set; }
  78. public StreamState(IMediaSourceManager mediaSourceManager, ILogger logger, TranscodingJobType transcodingType)
  79. : base(transcodingType)
  80. {
  81. _mediaSourceManager = mediaSourceManager;
  82. _logger = logger;
  83. }
  84. public bool EstimateContentLength { get; set; }
  85. public TranscodeSeekInfo TranscodeSeekInfo { get; set; }
  86. public bool EnableDlnaHeaders { get; set; }
  87. public override void Dispose()
  88. {
  89. DisposeTranscodingThrottler();
  90. DisposeLogStream();
  91. DisposeLiveStream();
  92. TranscodingJob = null;
  93. }
  94. private void DisposeTranscodingThrottler()
  95. {
  96. if (TranscodingThrottler != null)
  97. {
  98. try
  99. {
  100. TranscodingThrottler.Dispose();
  101. }
  102. catch (Exception ex)
  103. {
  104. _logger.LogError(ex, "Error disposing TranscodingThrottler");
  105. }
  106. TranscodingThrottler = null;
  107. }
  108. }
  109. private void DisposeLogStream()
  110. {
  111. if (LogFileStream != null)
  112. {
  113. try
  114. {
  115. LogFileStream.Dispose();
  116. }
  117. catch (Exception ex)
  118. {
  119. _logger.LogError(ex, "Error disposing log stream");
  120. }
  121. LogFileStream = null;
  122. }
  123. }
  124. private async void DisposeLiveStream()
  125. {
  126. if (MediaSource.RequiresClosing && string.IsNullOrWhiteSpace(Request.LiveStreamId) && !string.IsNullOrWhiteSpace(MediaSource.LiveStreamId))
  127. {
  128. try
  129. {
  130. await _mediaSourceManager.CloseLiveStream(MediaSource.LiveStreamId).ConfigureAwait(false);
  131. }
  132. catch (Exception ex)
  133. {
  134. _logger.LogError(ex, "Error closing media source");
  135. }
  136. }
  137. }
  138. public DeviceProfile DeviceProfile { get; set; }
  139. public TranscodingJob TranscodingJob;
  140. public override void ReportTranscodingProgress(TimeSpan? transcodingPosition, float framerate, double? percentComplete, long bytesTranscoded, int? bitRate)
  141. {
  142. ApiEntryPoint.Instance.ReportTranscodingProgress(TranscodingJob, this, transcodingPosition, framerate, percentComplete, bytesTranscoded, bitRate);
  143. }
  144. }
  145. }