| | | 1 | | using System; |
| | | 2 | | using System.Threading; |
| | | 3 | | using System.Threading.Tasks; |
| | | 4 | | using MediaBrowser.Common.Configuration; |
| | | 5 | | using MediaBrowser.Model.Configuration; |
| | | 6 | | using MediaBrowser.Model.IO; |
| | | 7 | | using Microsoft.Extensions.Logging; |
| | | 8 | | |
| | | 9 | | namespace MediaBrowser.Controller.MediaEncoding; |
| | | 10 | | |
| | | 11 | | /// <summary> |
| | | 12 | | /// Transcoding throttler. |
| | | 13 | | /// </summary> |
| | | 14 | | public class TranscodingThrottler : IDisposable |
| | | 15 | | { |
| | | 16 | | private readonly TranscodingJob _job; |
| | | 17 | | private readonly ILogger<TranscodingThrottler> _logger; |
| | | 18 | | private readonly IConfigurationManager _config; |
| | | 19 | | private readonly IFileSystem _fileSystem; |
| | | 20 | | private readonly IMediaEncoder _mediaEncoder; |
| | | 21 | | private Timer? _timer; |
| | | 22 | | private bool _isPaused; |
| | | 23 | | |
| | | 24 | | /// <summary> |
| | | 25 | | /// Initializes a new instance of the <see cref="TranscodingThrottler"/> class. |
| | | 26 | | /// </summary> |
| | | 27 | | /// <param name="job">Transcoding job dto.</param> |
| | | 28 | | /// <param name="logger">Instance of the <see cref="ILogger{TranscodingThrottler}"/> interface.</param> |
| | | 29 | | /// <param name="config">Instance of the <see cref="IConfigurationManager"/> interface.</param> |
| | | 30 | | /// <param name="fileSystem">Instance of the <see cref="IFileSystem"/> interface.</param> |
| | | 31 | | /// <param name="mediaEncoder">Instance of the <see cref="IMediaEncoder"/> interface.</param> |
| | | 32 | | public TranscodingThrottler(TranscodingJob job, ILogger<TranscodingThrottler> logger, IConfigurationManager config, |
| | | 33 | | { |
| | 0 | 34 | | _job = job; |
| | 0 | 35 | | _logger = logger; |
| | 0 | 36 | | _config = config; |
| | 0 | 37 | | _fileSystem = fileSystem; |
| | 0 | 38 | | _mediaEncoder = mediaEncoder; |
| | 0 | 39 | | } |
| | | 40 | | |
| | | 41 | | /// <summary> |
| | | 42 | | /// Start timer. |
| | | 43 | | /// </summary> |
| | | 44 | | public void Start() |
| | | 45 | | { |
| | 0 | 46 | | _timer = new Timer(TimerCallback, null, 5000, 5000); |
| | 0 | 47 | | } |
| | | 48 | | |
| | | 49 | | /// <summary> |
| | | 50 | | /// Unpause transcoding. |
| | | 51 | | /// </summary> |
| | | 52 | | /// <returns>A <see cref="Task"/>.</returns> |
| | | 53 | | public async Task UnpauseTranscoding() |
| | | 54 | | { |
| | | 55 | | if (_isPaused) |
| | | 56 | | { |
| | | 57 | | _logger.LogDebug("Sending resume command to ffmpeg"); |
| | | 58 | | |
| | | 59 | | try |
| | | 60 | | { |
| | | 61 | | var resumeKey = _mediaEncoder.IsPkeyPauseSupported ? "u" : Environment.NewLine; |
| | | 62 | | await _job.Process!.StandardInput.WriteAsync(resumeKey).ConfigureAwait(false); |
| | | 63 | | _isPaused = false; |
| | | 64 | | } |
| | | 65 | | catch (Exception ex) |
| | | 66 | | { |
| | | 67 | | _logger.LogError(ex, "Error resuming transcoding"); |
| | | 68 | | } |
| | | 69 | | } |
| | | 70 | | } |
| | | 71 | | |
| | | 72 | | /// <summary> |
| | | 73 | | /// Stop throttler. |
| | | 74 | | /// </summary> |
| | | 75 | | /// <returns>A <see cref="Task"/>.</returns> |
| | | 76 | | public async Task Stop() |
| | | 77 | | { |
| | | 78 | | DisposeTimer(); |
| | | 79 | | await UnpauseTranscoding().ConfigureAwait(false); |
| | | 80 | | } |
| | | 81 | | |
| | | 82 | | /// <summary> |
| | | 83 | | /// Dispose throttler. |
| | | 84 | | /// </summary> |
| | | 85 | | public void Dispose() |
| | | 86 | | { |
| | 0 | 87 | | Dispose(true); |
| | 0 | 88 | | GC.SuppressFinalize(this); |
| | 0 | 89 | | } |
| | | 90 | | |
| | | 91 | | /// <summary> |
| | | 92 | | /// Dispose throttler. |
| | | 93 | | /// </summary> |
| | | 94 | | /// <param name="disposing">Disposing.</param> |
| | | 95 | | protected virtual void Dispose(bool disposing) |
| | | 96 | | { |
| | 0 | 97 | | if (disposing) |
| | | 98 | | { |
| | 0 | 99 | | DisposeTimer(); |
| | | 100 | | } |
| | 0 | 101 | | } |
| | | 102 | | |
| | | 103 | | private EncodingOptions GetOptions() |
| | | 104 | | { |
| | 0 | 105 | | return _config.GetEncodingOptions(); |
| | | 106 | | } |
| | | 107 | | |
| | | 108 | | private async void TimerCallback(object? state) |
| | | 109 | | { |
| | | 110 | | if (_job.HasExited) |
| | | 111 | | { |
| | | 112 | | DisposeTimer(); |
| | | 113 | | return; |
| | | 114 | | } |
| | | 115 | | |
| | | 116 | | var options = GetOptions(); |
| | | 117 | | |
| | | 118 | | if (options.EnableThrottling && IsThrottleAllowed(_job, Math.Max(options.ThrottleDelaySeconds, 60))) |
| | | 119 | | { |
| | | 120 | | await PauseTranscoding().ConfigureAwait(false); |
| | | 121 | | } |
| | | 122 | | else |
| | | 123 | | { |
| | | 124 | | await UnpauseTranscoding().ConfigureAwait(false); |
| | | 125 | | } |
| | | 126 | | } |
| | | 127 | | |
| | | 128 | | private async Task PauseTranscoding() |
| | | 129 | | { |
| | | 130 | | if (!_isPaused) |
| | | 131 | | { |
| | | 132 | | var pauseKey = _mediaEncoder.IsPkeyPauseSupported ? "p" : "c"; |
| | | 133 | | |
| | | 134 | | _logger.LogDebug("Sending pause command [{Key}] to ffmpeg", pauseKey); |
| | | 135 | | |
| | | 136 | | try |
| | | 137 | | { |
| | | 138 | | await _job.Process!.StandardInput.WriteAsync(pauseKey).ConfigureAwait(false); |
| | | 139 | | _isPaused = true; |
| | | 140 | | } |
| | | 141 | | catch (Exception ex) |
| | | 142 | | { |
| | | 143 | | _logger.LogError(ex, "Error pausing transcoding"); |
| | | 144 | | } |
| | | 145 | | } |
| | | 146 | | } |
| | | 147 | | |
| | | 148 | | private bool IsThrottleAllowed(TranscodingJob job, int thresholdSeconds) |
| | | 149 | | { |
| | 0 | 150 | | var bytesDownloaded = job.BytesDownloaded; |
| | 0 | 151 | | var transcodingPositionTicks = job.TranscodingPositionTicks ?? 0; |
| | 0 | 152 | | var downloadPositionTicks = job.DownloadPositionTicks ?? 0; |
| | | 153 | | |
| | 0 | 154 | | var path = job.Path ?? throw new ArgumentException("Path can't be null."); |
| | | 155 | | |
| | 0 | 156 | | var gapLengthInTicks = TimeSpan.FromSeconds(thresholdSeconds).Ticks; |
| | | 157 | | |
| | 0 | 158 | | if (downloadPositionTicks > 0 && transcodingPositionTicks > 0) |
| | | 159 | | { |
| | | 160 | | // HLS - time-based consideration |
| | | 161 | | |
| | 0 | 162 | | var targetGap = gapLengthInTicks; |
| | 0 | 163 | | var gap = transcodingPositionTicks - downloadPositionTicks; |
| | | 164 | | |
| | 0 | 165 | | if (gap < targetGap) |
| | | 166 | | { |
| | 0 | 167 | | _logger.LogDebug("Not throttling transcoder gap {0} target gap {1}", gap, targetGap); |
| | 0 | 168 | | return false; |
| | | 169 | | } |
| | | 170 | | |
| | 0 | 171 | | _logger.LogDebug("Throttling transcoder gap {0} target gap {1}", gap, targetGap); |
| | 0 | 172 | | return true; |
| | | 173 | | } |
| | | 174 | | |
| | 0 | 175 | | if (bytesDownloaded > 0 && transcodingPositionTicks > 0) |
| | | 176 | | { |
| | | 177 | | // Progressive Streaming - byte-based consideration |
| | | 178 | | |
| | | 179 | | try |
| | | 180 | | { |
| | 0 | 181 | | var bytesTranscoded = job.BytesTranscoded ?? _fileSystem.GetFileInfo(path).Length; |
| | | 182 | | |
| | | 183 | | // Estimate the bytes the transcoder should be ahead |
| | 0 | 184 | | double gapFactor = gapLengthInTicks; |
| | 0 | 185 | | gapFactor /= transcodingPositionTicks; |
| | 0 | 186 | | var targetGap = bytesTranscoded * gapFactor; |
| | | 187 | | |
| | 0 | 188 | | var gap = bytesTranscoded - bytesDownloaded; |
| | | 189 | | |
| | 0 | 190 | | if (gap < targetGap) |
| | | 191 | | { |
| | 0 | 192 | | _logger.LogDebug("Not throttling transcoder gap {0} target gap {1} bytes downloaded {2}", gap, targe |
| | 0 | 193 | | return false; |
| | | 194 | | } |
| | | 195 | | |
| | 0 | 196 | | _logger.LogDebug("Throttling transcoder gap {0} target gap {1} bytes downloaded {2}", gap, targetGap, by |
| | 0 | 197 | | return true; |
| | | 198 | | } |
| | 0 | 199 | | catch (Exception ex) |
| | | 200 | | { |
| | 0 | 201 | | _logger.LogError(ex, "Error getting output size"); |
| | 0 | 202 | | return false; |
| | | 203 | | } |
| | | 204 | | } |
| | | 205 | | |
| | 0 | 206 | | _logger.LogDebug("No throttle data for {Path}", path); |
| | 0 | 207 | | return false; |
| | 0 | 208 | | } |
| | | 209 | | |
| | | 210 | | private void DisposeTimer() |
| | | 211 | | { |
| | 0 | 212 | | if (_timer is not null) |
| | | 213 | | { |
| | 0 | 214 | | _timer.Dispose(); |
| | 0 | 215 | | _timer = null; |
| | | 216 | | } |
| | 0 | 217 | | } |
| | | 218 | | } |