2019-01-13 12:24:58 -07:00
|
|
|
|
using System;
|
|
|
|
|
using MediaBrowser.Common.Configuration;
|
2018-12-14 02:40:55 -07:00
|
|
|
|
using MediaBrowser.Model.Configuration;
|
|
|
|
|
using MediaBrowser.Model.IO;
|
|
|
|
|
using MediaBrowser.Model.Threading;
|
2018-12-14 16:48:06 -07:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
2018-12-14 02:40:55 -07:00
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.Api.Playback
|
|
|
|
|
{
|
|
|
|
|
public class TranscodingThrottler : IDisposable
|
|
|
|
|
{
|
|
|
|
|
private readonly TranscodingJob _job;
|
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
private ITimer _timer;
|
|
|
|
|
private bool _isPaused;
|
|
|
|
|
private readonly IConfigurationManager _config;
|
|
|
|
|
private readonly ITimerFactory _timerFactory;
|
|
|
|
|
private readonly IFileSystem _fileSystem;
|
|
|
|
|
|
|
|
|
|
public TranscodingThrottler(TranscodingJob job, ILogger logger, IConfigurationManager config, ITimerFactory timerFactory, IFileSystem fileSystem)
|
|
|
|
|
{
|
|
|
|
|
_job = job;
|
|
|
|
|
_logger = logger;
|
|
|
|
|
_config = config;
|
|
|
|
|
_timerFactory = timerFactory;
|
|
|
|
|
_fileSystem = fileSystem;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private EncodingOptions GetOptions()
|
|
|
|
|
{
|
|
|
|
|
return _config.GetConfiguration<EncodingOptions>("encoding");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Start()
|
|
|
|
|
{
|
|
|
|
|
_timer = _timerFactory.Create(TimerCallback, null, 5000, 5000);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void TimerCallback(object state)
|
|
|
|
|
{
|
|
|
|
|
if (_job.HasExited)
|
|
|
|
|
{
|
|
|
|
|
DisposeTimer();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var options = GetOptions();
|
|
|
|
|
|
|
|
|
|
if (options.EnableThrottling && IsThrottleAllowed(_job, options.ThrottleDelaySeconds))
|
|
|
|
|
{
|
|
|
|
|
PauseTranscoding();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
UnpauseTranscoding();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void PauseTranscoding()
|
|
|
|
|
{
|
|
|
|
|
if (!_isPaused)
|
|
|
|
|
{
|
2018-12-14 16:48:06 -07:00
|
|
|
|
_logger.LogDebug("Sending pause command to ffmpeg");
|
2018-12-14 02:40:55 -07:00
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
_job.Process.StandardInput.Write("c");
|
|
|
|
|
_isPaused = true;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2018-12-20 05:11:26 -07:00
|
|
|
|
_logger.LogError(ex, "Error pausing transcoding");
|
2018-12-14 02:40:55 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void UnpauseTranscoding()
|
|
|
|
|
{
|
|
|
|
|
if (_isPaused)
|
|
|
|
|
{
|
2018-12-20 05:11:26 -07:00
|
|
|
|
_logger.LogDebug("Sending resume command to ffmpeg");
|
2018-12-14 02:40:55 -07:00
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
_job.Process.StandardInput.WriteLine();
|
|
|
|
|
_isPaused = false;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2018-12-20 05:11:26 -07:00
|
|
|
|
_logger.LogError(ex, "Error resuming transcoding");
|
2018-12-14 02:40:55 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool IsThrottleAllowed(TranscodingJob job, int thresholdSeconds)
|
|
|
|
|
{
|
|
|
|
|
var bytesDownloaded = job.BytesDownloaded ?? 0;
|
|
|
|
|
var transcodingPositionTicks = job.TranscodingPositionTicks ?? 0;
|
|
|
|
|
var downloadPositionTicks = job.DownloadPositionTicks ?? 0;
|
|
|
|
|
|
|
|
|
|
var path = job.Path;
|
|
|
|
|
var gapLengthInTicks = TimeSpan.FromSeconds(thresholdSeconds).Ticks;
|
|
|
|
|
|
|
|
|
|
if (downloadPositionTicks > 0 && transcodingPositionTicks > 0)
|
|
|
|
|
{
|
|
|
|
|
// HLS - time-based consideration
|
|
|
|
|
|
|
|
|
|
var targetGap = gapLengthInTicks;
|
|
|
|
|
var gap = transcodingPositionTicks - downloadPositionTicks;
|
|
|
|
|
|
|
|
|
|
if (gap < targetGap)
|
|
|
|
|
{
|
2018-12-20 05:11:26 -07:00
|
|
|
|
_logger.LogDebug("Not throttling transcoder gap {0} target gap {1}", gap, targetGap);
|
2018-12-14 02:40:55 -07:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-20 05:11:26 -07:00
|
|
|
|
_logger.LogDebug("Throttling transcoder gap {0} target gap {1}", gap, targetGap);
|
2018-12-14 02:40:55 -07:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (bytesDownloaded > 0 && transcodingPositionTicks > 0)
|
|
|
|
|
{
|
|
|
|
|
// Progressive Streaming - byte-based consideration
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var bytesTranscoded = job.BytesTranscoded ?? _fileSystem.GetFileInfo(path).Length;
|
|
|
|
|
|
|
|
|
|
// Estimate the bytes the transcoder should be ahead
|
|
|
|
|
double gapFactor = gapLengthInTicks;
|
|
|
|
|
gapFactor /= transcodingPositionTicks;
|
|
|
|
|
var targetGap = bytesTranscoded * gapFactor;
|
|
|
|
|
|
|
|
|
|
var gap = bytesTranscoded - bytesDownloaded;
|
|
|
|
|
|
|
|
|
|
if (gap < targetGap)
|
|
|
|
|
{
|
2018-12-20 05:11:26 -07:00
|
|
|
|
_logger.LogDebug("Not throttling transcoder gap {0} target gap {1} bytes downloaded {2}", gap, targetGap, bytesDownloaded);
|
2018-12-14 02:40:55 -07:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-20 05:11:26 -07:00
|
|
|
|
_logger.LogDebug("Throttling transcoder gap {0} target gap {1} bytes downloaded {2}", gap, targetGap, bytesDownloaded);
|
2018-12-14 02:40:55 -07:00
|
|
|
|
return true;
|
|
|
|
|
}
|
2018-12-20 05:11:26 -07:00
|
|
|
|
catch (Exception ex)
|
2018-12-14 02:40:55 -07:00
|
|
|
|
{
|
2018-12-20 05:11:26 -07:00
|
|
|
|
_logger.LogError(ex, "Error getting output size");
|
2018-12-14 02:40:55 -07:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-20 05:11:26 -07:00
|
|
|
|
_logger.LogDebug("No throttle data for " + path);
|
2018-12-14 02:40:55 -07:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Stop()
|
|
|
|
|
{
|
|
|
|
|
DisposeTimer();
|
|
|
|
|
UnpauseTranscoding();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
DisposeTimer();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void DisposeTimer()
|
|
|
|
|
{
|
|
|
|
|
if (_timer != null)
|
|
|
|
|
{
|
|
|
|
|
_timer.Dispose();
|
|
|
|
|
_timer = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|