Exemple #1
0
 /// <summary>
 /// Initializes a new instance of the <see cref="TranscodingThrottler"/> class.
 /// </summary>
 /// <param name="job">Transcoding job dto.</param>
 /// <param name="logger">Instance of the <see cref="ILogger{TranscodingThrottler}"/> interface.</param>
 /// <param name="config">Instance of the <see cref="IConfigurationManager"/> interface.</param>
 /// <param name="fileSystem">Instance of the <see cref="IFileSystem"/> interface.</param>
 public TranscodingThrottler(TranscodingJobDto job, ILogger <TranscodingThrottler> logger, IConfigurationManager config, IFileSystem fileSystem)
 {
     _job        = job;
     _logger     = logger;
     _config     = config;
     _fileSystem = fileSystem;
 }
        private bool IsThrottleAllowed(TranscodingJobDto 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)
                {
                    _logger.LogDebug("Not throttling transcoder gap {0} target gap {1}", gap, targetGap);
                    return(false);
                }

                _logger.LogDebug("Throttling transcoder gap {0} target gap {1}", gap, targetGap);
                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)
                    {
                        _logger.LogDebug("Not throttling transcoder gap {0} target gap {1} bytes downloaded {2}", gap, targetGap, bytesDownloaded);
                        return(false);
                    }

                    _logger.LogDebug("Throttling transcoder gap {0} target gap {1} bytes downloaded {2}", gap, targetGap, bytesDownloaded);
                    return(true);
                }
                catch (Exception ex)
                {
                    _logger.LogError(ex, "Error getting output size");
                    return(false);
                }
            }

            _logger.LogDebug("No throttle data for " + path);
            return(false);
        }