aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Api/Playback/Progressive/BaseProgressiveStreamingService.cs
diff options
context:
space:
mode:
authorLuke Pulverenti <luke.pulverenti@gmail.com>2014-09-02 22:30:24 -0400
committerLuke Pulverenti <luke.pulverenti@gmail.com>2014-09-02 22:30:24 -0400
commit60d1d5cdee642ee9d5be7e91be5caeb9a1a756df (patch)
tree67dcd2270977afafc596a1c4568cdaa4cf9d26dc /MediaBrowser.Api/Playback/Progressive/BaseProgressiveStreamingService.cs
parenta3d553a7fbe93fe416c940e80d5e511a2d753f25 (diff)
restore nuget targets for mono build
Diffstat (limited to 'MediaBrowser.Api/Playback/Progressive/BaseProgressiveStreamingService.cs')
-rw-r--r--MediaBrowser.Api/Playback/Progressive/BaseProgressiveStreamingService.cs95
1 files changed, 83 insertions, 12 deletions
diff --git a/MediaBrowser.Api/Playback/Progressive/BaseProgressiveStreamingService.cs b/MediaBrowser.Api/Playback/Progressive/BaseProgressiveStreamingService.cs
index f0ad6ce5c..50929e6f3 100644
--- a/MediaBrowser.Api/Playback/Progressive/BaseProgressiveStreamingService.cs
+++ b/MediaBrowser.Api/Playback/Progressive/BaseProgressiveStreamingService.cs
@@ -142,10 +142,9 @@ namespace MediaBrowser.Api.Playback.Progressive
var outputPath = state.OutputFilePath;
var outputPathExists = File.Exists(outputPath);
- var isStatic = request.Static ||
- (outputPathExists && !ApiEntryPoint.Instance.HasActiveTranscodingJob(outputPath, TranscodingJobType.Progressive));
+ var isTranscodeCached = outputPathExists && !ApiEntryPoint.Instance.HasActiveTranscodingJob(outputPath, TranscodingJobType.Progressive);
- AddDlnaHeaders(state, responseHeaders, isStatic);
+ AddDlnaHeaders(state, responseHeaders, request.Static || isTranscodeCached);
// Static stream
if (request.Static)
@@ -154,6 +153,10 @@ namespace MediaBrowser.Api.Playback.Progressive
using (state)
{
+ var job = string.IsNullOrEmpty(request.TranscodingJobId) ?
+ null :
+ ApiEntryPoint.Instance.GetTranscodingJob(request.TranscodingJobId);
+
var limits = new List<long>();
if (state.InputBitrate.HasValue)
{
@@ -172,7 +175,13 @@ namespace MediaBrowser.Api.Playback.Progressive
}
// Take the greater of the above to methods, just to be safe
- var throttleLimit = limits.Count > 0 ? limits.Max() : 0;
+ var throttleLimit = limits.Count > 0 ? limits.First() : 0;
+
+ // Pad to play it safe
+ var bytesPerSecond = Convert.ToInt64(1.05 * throttleLimit);
+
+ // Don't even start evaluating this until at least two minutes have content have been consumed
+ var targetGap = throttleLimit * 120;
return ResultFactory.GetStaticFileResult(Request, new StaticFileResultOptions
{
@@ -182,17 +191,17 @@ namespace MediaBrowser.Api.Playback.Progressive
Path = state.MediaPath,
Throttle = request.Throttle,
- // Pad by 20% to play it safe
- ThrottleLimit = Convert.ToInt64(1.2 * throttleLimit),
+ ThrottleLimit = bytesPerSecond,
- // 3.5 minutes
- MinThrottlePosition = throttleLimit * 210
+ MinThrottlePosition = targetGap,
+
+ ThrottleCallback = (l1, l2) => ThrottleCallack(l1, l2, bytesPerSecond, job)
});
}
}
// Not static but transcode cache file exists
- if (outputPathExists && !ApiEntryPoint.Instance.HasActiveTranscodingJob(outputPath, TranscodingJobType.Progressive))
+ if (isTranscodeCached)
{
var contentType = state.GetMimeType(outputPath);
@@ -225,6 +234,67 @@ namespace MediaBrowser.Api.Playback.Progressive
}
}
+ private readonly long _gapLengthInTicks = TimeSpan.FromMinutes(3).Ticks;
+
+ private long ThrottleCallack(long currentBytesPerSecond, long bytesWritten, long originalBytesPerSecond, TranscodingJob job)
+ {
+ var bytesDownloaded = job.BytesDownloaded ?? 0;
+ var transcodingPositionTicks = job.TranscodingPositionTicks ?? 0;
+ var downloadPositionTicks = job.DownloadPositionTicks ?? 0;
+
+ var path = job.Path;
+
+ 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
+ double gapFactor = _gapLengthInTicks;
+ gapFactor /= transcodingPositionTicks;
+ var targetGap = bytesTranscoded * gapFactor;
+
+ var gap = bytesTranscoded - bytesDownloaded;
+
+ if (gap < targetGap)
+ {
+ //Logger.Debug("Not throttling transcoder gap {0} target gap {1} bytes downloaded {2}", gap, targetGap, bytesDownloaded);
+ return 0;
+ }
+
+ //Logger.Debug("Throttling transcoder gap {0} target gap {1} bytes downloaded {2}", gap, targetGap, bytesDownloaded);
+ }
+ catch
+ {
+ //Logger.Error("Error getting output size");
+ }
+ }
+ else if (downloadPositionTicks > 0 && transcodingPositionTicks > 0)
+ {
+ // HLS - time-based consideration
+
+ var targetGap = _gapLengthInTicks;
+ var gap = transcodingPositionTicks - downloadPositionTicks;
+
+ if (gap < targetGap)
+ {
+ //Logger.Debug("Not throttling transcoder gap {0} target gap {1}", gap, targetGap);
+ return 0;
+ }
+
+ //Logger.Debug("Throttling transcoder gap {0} target gap {1}", gap, targetGap);
+ }
+ else
+ {
+ //Logger.Debug("No throttle data for " + path);
+ }
+
+ return originalBytesPerSecond;
+ }
+
/// <summary>
/// Gets the static remote stream result.
/// </summary>
@@ -325,17 +395,18 @@ namespace MediaBrowser.Api.Playback.Progressive
await ApiEntryPoint.Instance.TranscodingStartLock.WaitAsync(cancellationTokenSource.Token).ConfigureAwait(false);
try
{
+ TranscodingJob job;
+
if (!File.Exists(outputPath))
{
- await StartFfMpeg(state, outputPath, cancellationTokenSource).ConfigureAwait(false);
+ job = await StartFfMpeg(state, outputPath, cancellationTokenSource).ConfigureAwait(false);
}
else
{
- ApiEntryPoint.Instance.OnTranscodeBeginRequest(outputPath, TranscodingJobType.Progressive);
+ job = ApiEntryPoint.Instance.OnTranscodeBeginRequest(outputPath, TranscodingJobType.Progressive);
state.Dispose();
}
- var job = ApiEntryPoint.Instance.GetTranscodingJob(outputPath, TranscodingJobType.Progressive);
var result = new ProgressiveStreamWriter(outputPath, Logger, FileSystem, job);
result.Options["Content-Type"] = contentType;