diff options
Diffstat (limited to 'MediaBrowser.MediaEncoding/Transcoding/TranscodeManager.cs')
| -rw-r--r-- | MediaBrowser.MediaEncoding/Transcoding/TranscodeManager.cs | 61 |
1 files changed, 30 insertions, 31 deletions
diff --git a/MediaBrowser.MediaEncoding/Transcoding/TranscodeManager.cs b/MediaBrowser.MediaEncoding/Transcoding/TranscodeManager.cs index d79e4441a..146b30643 100644 --- a/MediaBrowser.MediaEncoding/Transcoding/TranscodeManager.cs +++ b/MediaBrowser.MediaEncoding/Transcoding/TranscodeManager.cs @@ -4,10 +4,12 @@ using System.Diagnostics; using System.Globalization; using System.IO; using System.Linq; +using System.Runtime.CompilerServices; using System.Text; using System.Text.Json; using System.Threading; using System.Threading.Tasks; +using AsyncKeyedLock; using Jellyfin.Data.Enums; using Jellyfin.Extensions; using MediaBrowser.Common; @@ -43,7 +45,11 @@ public sealed class TranscodeManager : ITranscodeManager, IDisposable private readonly IAttachmentExtractor _attachmentExtractor; private readonly List<TranscodingJob> _activeTranscodingJobs = new(); - private readonly Dictionary<string, SemaphoreSlim> _transcodingLocks = new(); + private readonly AsyncKeyedLocker<string> _transcodingLocks = new(o => + { + o.PoolSize = 20; + o.PoolInitialFill = 1; + }); /// <summary> /// Initializes a new instance of the <see cref="TranscodeManager"/> class. @@ -224,11 +230,6 @@ public sealed class TranscodeManager : ITranscodeManager, IDisposable } } - lock (_transcodingLocks) - { - _transcodingLocks.Remove(job.Path!); - } - job.Stop(); if (delete(job.Path!)) @@ -404,7 +405,7 @@ public sealed class TranscodeManager : ITranscodeManager, IDisposable var user = userId.IsEmpty() ? null : _userManager.GetUserById(userId); if (user is not null && !user.HasPermission(PermissionKind.EnableVideoPlaybackTranscoding)) { - this.OnTranscodeFailedToStart(outputPath, transcodingJobType, state); + OnTranscodeFailedToStart(outputPath, transcodingJobType, state); throw new ArgumentException("User does not have access to video transcoding."); } @@ -416,7 +417,12 @@ public sealed class TranscodeManager : ITranscodeManager, IDisposable if (state.SubtitleStream is not null && state.SubtitleDeliveryMethod == SubtitleDeliveryMethod.Encode) { var attachmentPath = Path.Combine(_appPaths.CachePath, "attachments", state.MediaSource.Id); - if (state.VideoType != VideoType.Dvd) + if (state.MediaSource.VideoType == VideoType.Dvd || state.MediaSource.VideoType == VideoType.BluRay) + { + var concatPath = Path.Join(_serverConfigurationManager.GetTranscodePath(), state.MediaSource.Id + ".concat"); + await _attachmentExtractor.ExtractAllAttachments(concatPath, state.MediaSource, attachmentPath, cancellationTokenSource.Token).ConfigureAwait(false); + } + else { await _attachmentExtractor.ExtractAllAttachments(state.MediaPath, state.MediaSource, attachmentPath, cancellationTokenSource.Token).ConfigureAwait(false); } @@ -431,7 +437,7 @@ public sealed class TranscodeManager : ITranscodeManager, IDisposable } } - var process = new Process + using var process = new Process { StartInfo = new ProcessStartInfo { @@ -451,7 +457,7 @@ public sealed class TranscodeManager : ITranscodeManager, IDisposable EnableRaisingEvents = true }; - var transcodingJob = this.OnTranscodeBeginning( + var transcodingJob = OnTranscodeBeginning( outputPath, state.Request.PlaySessionId, state.MediaSource.LiveStreamId, @@ -506,7 +512,7 @@ public sealed class TranscodeManager : ITranscodeManager, IDisposable catch (Exception ex) { _logger.LogError(ex, "Error starting FFmpeg"); - this.OnTranscodeFailedToStart(outputPath, transcodingJobType, state); + OnTranscodeFailedToStart(outputPath, transcodingJobType, state); throw; } @@ -625,11 +631,6 @@ public sealed class TranscodeManager : ITranscodeManager, IDisposable } } - lock (_transcodingLocks) - { - _transcodingLocks.Remove(path); - } - if (!string.IsNullOrWhiteSpace(state.Request.DeviceId)) { _sessionManager.ClearTranscodingInfo(state.Request.DeviceId); @@ -705,21 +706,6 @@ public sealed class TranscodeManager : ITranscodeManager, IDisposable } } - /// <inheritdoc /> - public SemaphoreSlim GetTranscodingLock(string outputPath) - { - lock (_transcodingLocks) - { - if (!_transcodingLocks.TryGetValue(outputPath, out SemaphoreSlim? result)) - { - result = new SemaphoreSlim(1, 1); - _transcodingLocks[outputPath] = result; - } - - return result; - } - } - private void OnPlaybackProgress(object? sender, PlaybackProgressEventArgs e) { if (!string.IsNullOrWhiteSpace(e.PlaySessionId)) @@ -742,10 +728,23 @@ public sealed class TranscodeManager : ITranscodeManager, IDisposable } } + /// <summary> + /// Transcoding lock. + /// </summary> + /// <param name="outputPath">The output path of the transcoded file.</param> + /// <param name="cancellationToken">The cancellation token.</param> + /// <returns>An <see cref="IDisposable"/>.</returns> + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public ValueTask<IDisposable> LockAsync(string outputPath, CancellationToken cancellationToken) + { + return _transcodingLocks.LockAsync(outputPath, cancellationToken); + } + /// <inheritdoc /> public void Dispose() { _sessionManager.PlaybackProgress -= OnPlaybackProgress; _sessionManager.PlaybackStart -= OnPlaybackProgress; + _transcodingLocks.Dispose(); } } |
