Skip to content

Commit

Permalink
Merge pull request #7994 from nyanmisaka/fix-throttler
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuaboniface committed Jun 27, 2022
2 parents d342b79 + 506ed69 commit a7d45b5
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 26 deletions.
4 changes: 2 additions & 2 deletions Jellyfin.Api/Controllers/DynamicHlsController.cs
Expand Up @@ -285,7 +285,7 @@ public class DynamicHlsController : BaseJellyfinApiController
// Due to CTS.Token calling ThrowIfDisposed (https://github.com/dotnet/runtime/issues/29970) we have to "cache" the token
// since it gets disposed when ffmpeg exits
var cancellationToken = cancellationTokenSource.Token;
using var state = await StreamingHelpers.GetStreamingState(
var state = await StreamingHelpers.GetStreamingState(
streamingRequest,
Request,
_authContext,
Expand Down Expand Up @@ -1432,7 +1432,7 @@ private async Task<ActionResult> GetDynamicSegment(StreamingRequestDto streaming
var cancellationTokenSource = new CancellationTokenSource();
var cancellationToken = cancellationTokenSource.Token;

using var state = await StreamingHelpers.GetStreamingState(
var state = await StreamingHelpers.GetStreamingState(
streamingRequest,
Request,
_authContext,
Expand Down
2 changes: 1 addition & 1 deletion Jellyfin.Api/Controllers/VideosController.cs
Expand Up @@ -427,7 +427,7 @@ public async Task<ActionResult> MergeVersions([FromQuery, Required, ModelBinder(
StreamOptions = streamOptions
};

using var state = await StreamingHelpers.GetStreamingState(
var state = await StreamingHelpers.GetStreamingState(
streamingRequest,
Request,
_authContext,
Expand Down
21 changes: 7 additions & 14 deletions Jellyfin.Api/Helpers/TranscodingJobHelper.cs
Expand Up @@ -654,27 +654,20 @@ private void StartThrottler(StreamState state, TranscodingJobDto transcodingJob)
{
if (EnableThrottling(state))
{
transcodingJob.TranscodingThrottler = state.TranscodingThrottler = new TranscodingThrottler(transcodingJob, new Logger<TranscodingThrottler>(new LoggerFactory()), _serverConfigurationManager, _fileSystem);
state.TranscodingThrottler.Start();
transcodingJob.TranscodingThrottler = new TranscodingThrottler(transcodingJob, new Logger<TranscodingThrottler>(new LoggerFactory()), _serverConfigurationManager, _fileSystem);
transcodingJob.TranscodingThrottler.Start();
}
}

private bool EnableThrottling(StreamState state)
{
var encodingOptions = _serverConfigurationManager.GetEncodingOptions();

// enable throttling when NOT using hardware acceleration
if (string.IsNullOrEmpty(encodingOptions.HardwareAccelerationType))
{
return state.InputProtocol == MediaProtocol.File &&
state.RunTimeTicks.HasValue &&
state.RunTimeTicks.Value >= TimeSpan.FromMinutes(5).Ticks &&
state.IsInputVideo &&
state.VideoType == VideoType.VideoFile &&
!EncodingHelper.IsCopyCodec(state.OutputVideoCodec);
}

return false;
return state.InputProtocol == MediaProtocol.File &&
state.RunTimeTicks.HasValue &&
state.RunTimeTicks.Value >= TimeSpan.FromMinutes(5).Ticks &&
state.IsInputVideo &&
state.VideoType == VideoType.VideoFile;
}

/// <summary>
Expand Down
8 changes: 0 additions & 8 deletions Jellyfin.Api/Models/StreamingDtos/StreamState.cs
Expand Up @@ -47,11 +47,6 @@ public StreamingRequestDto Request
}
}

/// <summary>
/// Gets or sets the transcoding throttler.
/// </summary>
public TranscodingThrottler? TranscodingThrottler { get; set; }

/// <summary>
/// Gets the video request.
/// </summary>
Expand Down Expand Up @@ -191,11 +186,8 @@ protected virtual void Dispose(bool disposing)
{
_mediaSourceManager.CloseLiveStream(MediaSource.LiveStreamId).GetAwaiter().GetResult();
}

TranscodingThrottler?.Dispose();
}

TranscodingThrottler = null;
TranscodingJob = null;

_disposed = true;
Expand Down
2 changes: 1 addition & 1 deletion MediaBrowser.Controller/MediaEncoding/JobLogger.cs
Expand Up @@ -111,7 +111,7 @@ private void ParseLogLine(string line, EncodingJobInfo state)

percent = 100.0 * currentMs / totalMs;

transcodingPosition = val;
transcodingPosition = TimeSpan.FromMilliseconds(currentMs);
}
}
else if (part.StartsWith("size=", StringComparison.OrdinalIgnoreCase))
Expand Down

0 comments on commit a7d45b5

Please sign in to comment.