2015-03-30 09:16:34 -07:00
|
|
|
|
using MediaBrowser.Common.Configuration;
|
|
|
|
|
using MediaBrowser.Model.Configuration;
|
|
|
|
|
using MediaBrowser.Model.Logging;
|
2015-02-27 10:43:06 -07:00
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.Api.Playback
|
|
|
|
|
{
|
|
|
|
|
public class TranscodingThrottler : IDisposable
|
|
|
|
|
{
|
|
|
|
|
private readonly TranscodingJob _job;
|
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
private Timer _timer;
|
2015-02-28 11:47:05 -07:00
|
|
|
|
private bool _isPaused;
|
2015-03-30 09:16:34 -07:00
|
|
|
|
private readonly IConfigurationManager _config;
|
2015-02-27 10:43:06 -07:00
|
|
|
|
|
2015-03-30 09:16:34 -07:00
|
|
|
|
public TranscodingThrottler(TranscodingJob job, ILogger logger, IConfigurationManager config)
|
2015-03-05 13:10:09 -07:00
|
|
|
|
{
|
|
|
|
|
_job = job;
|
|
|
|
|
_logger = logger;
|
2015-03-30 09:16:34 -07:00
|
|
|
|
_config = config;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private EncodingOptions GetOptions()
|
|
|
|
|
{
|
|
|
|
|
return _config.GetConfiguration<EncodingOptions>("encoding");
|
2015-03-05 13:10:09 -07:00
|
|
|
|
}
|
|
|
|
|
|
2015-02-27 10:43:06 -07:00
|
|
|
|
public void Start()
|
|
|
|
|
{
|
2015-03-13 11:51:11 -07:00
|
|
|
|
_timer = new Timer(TimerCallback, null, 5000, 5000);
|
2015-02-27 10:43:06 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void TimerCallback(object state)
|
|
|
|
|
{
|
2015-02-28 06:42:47 -07:00
|
|
|
|
if (_job.HasExited)
|
|
|
|
|
{
|
|
|
|
|
DisposeTimer();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-30 09:16:34 -07:00
|
|
|
|
var options = GetOptions();
|
|
|
|
|
|
2016-01-08 21:27:58 -07:00
|
|
|
|
if (options.EnableThrottling && IsThrottleAllowed(_job, options.ThrottleDelaySeconds))
|
2015-02-27 10:43:06 -07:00
|
|
|
|
{
|
|
|
|
|
PauseTranscoding();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
UnpauseTranscoding();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void PauseTranscoding()
|
|
|
|
|
{
|
2015-02-28 11:47:05 -07:00
|
|
|
|
if (!_isPaused)
|
|
|
|
|
{
|
|
|
|
|
_logger.Debug("Sending pause command to ffmpeg");
|
|
|
|
|
|
2015-03-16 11:24:42 -07:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
_job.Process.StandardInput.Write("c");
|
|
|
|
|
_isPaused = true;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.ErrorException("Error pausing transcoding", ex);
|
|
|
|
|
}
|
2015-02-28 11:47:05 -07:00
|
|
|
|
}
|
2015-02-27 10:43:06 -07:00
|
|
|
|
}
|
|
|
|
|
|
2015-04-10 15:16:41 -07:00
|
|
|
|
public void UnpauseTranscoding()
|
2015-02-27 10:43:06 -07:00
|
|
|
|
{
|
2015-02-28 11:47:05 -07:00
|
|
|
|
if (_isPaused)
|
|
|
|
|
{
|
|
|
|
|
_logger.Debug("Sending unpause command to ffmpeg");
|
|
|
|
|
|
2015-03-16 11:24:42 -07:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
_job.Process.StandardInput.WriteLine();
|
|
|
|
|
_isPaused = false;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.ErrorException("Error unpausing transcoding", ex);
|
|
|
|
|
}
|
2015-02-28 11:47:05 -07:00
|
|
|
|
}
|
2015-02-27 10:43:06 -07:00
|
|
|
|
}
|
|
|
|
|
|
2015-03-30 09:16:34 -07:00
|
|
|
|
private bool IsThrottleAllowed(TranscodingJob job, int thresholdSeconds)
|
2015-02-27 10:43:06 -07:00
|
|
|
|
{
|
|
|
|
|
var bytesDownloaded = job.BytesDownloaded ?? 0;
|
|
|
|
|
var transcodingPositionTicks = job.TranscodingPositionTicks ?? 0;
|
|
|
|
|
var downloadPositionTicks = job.DownloadPositionTicks ?? 0;
|
|
|
|
|
|
|
|
|
|
var path = job.Path;
|
2015-03-30 09:16:34 -07:00
|
|
|
|
var gapLengthInTicks = TimeSpan.FromSeconds(thresholdSeconds).Ticks;
|
2015-02-27 10:43:06 -07:00
|
|
|
|
|
|
|
|
|
if (downloadPositionTicks > 0 && transcodingPositionTicks > 0)
|
|
|
|
|
{
|
|
|
|
|
// HLS - time-based consideration
|
|
|
|
|
|
2015-03-30 09:16:34 -07:00
|
|
|
|
var targetGap = gapLengthInTicks;
|
2015-02-27 10:43:06 -07:00
|
|
|
|
var gap = transcodingPositionTicks - downloadPositionTicks;
|
|
|
|
|
|
|
|
|
|
if (gap < targetGap)
|
|
|
|
|
{
|
2015-02-28 06:42:47 -07:00
|
|
|
|
//_logger.Debug("Not throttling transcoder gap {0} target gap {1}", gap, targetGap);
|
2015-02-27 10:43:06 -07:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-28 06:42:47 -07:00
|
|
|
|
//_logger.Debug("Throttling transcoder gap {0} target gap {1}", gap, targetGap);
|
2015-02-27 10:43:06 -07:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (bytesDownloaded > 0 && transcodingPositionTicks > 0)
|
|
|
|
|
{
|
|
|
|
|
// Progressive Streaming - byte-based consideration
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var bytesTranscoded = job.BytesTranscoded ?? new FileInfo(path).Length;
|
|
|
|
|
|
|
|
|
|
// Estimate the bytes the transcoder should be ahead
|
2015-03-30 09:16:34 -07:00
|
|
|
|
double gapFactor = gapLengthInTicks;
|
2015-02-27 10:43:06 -07:00
|
|
|
|
gapFactor /= transcodingPositionTicks;
|
|
|
|
|
var targetGap = bytesTranscoded * gapFactor;
|
|
|
|
|
|
|
|
|
|
var gap = bytesTranscoded - bytesDownloaded;
|
|
|
|
|
|
|
|
|
|
if (gap < targetGap)
|
|
|
|
|
{
|
2015-02-28 06:42:47 -07:00
|
|
|
|
//_logger.Debug("Not throttling transcoder gap {0} target gap {1} bytes downloaded {2}", gap, targetGap, bytesDownloaded);
|
2015-02-27 10:43:06 -07:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-28 06:42:47 -07:00
|
|
|
|
//_logger.Debug("Throttling transcoder gap {0} target gap {1} bytes downloaded {2}", gap, targetGap, bytesDownloaded);
|
2015-02-27 10:43:06 -07:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
2015-02-28 06:42:47 -07:00
|
|
|
|
//_logger.Error("Error getting output size");
|
2015-02-28 11:47:05 -07:00
|
|
|
|
return false;
|
2015-02-27 10:43:06 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-28 11:47:05 -07:00
|
|
|
|
//_logger.Debug("No throttle data for " + path);
|
2015-02-27 10:43:06 -07:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-24 18:34:34 -07:00
|
|
|
|
public void Stop()
|
|
|
|
|
{
|
|
|
|
|
DisposeTimer();
|
|
|
|
|
UnpauseTranscoding();
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-27 10:43:06 -07:00
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
DisposeTimer();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void DisposeTimer()
|
|
|
|
|
{
|
|
|
|
|
if (_timer != null)
|
|
|
|
|
{
|
|
|
|
|
_timer.Dispose();
|
|
|
|
|
_timer = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|