|
@@ -1,7 +1,6 @@
|
|
#pragma warning disable CS1591
|
|
#pragma warning disable CS1591
|
|
|
|
|
|
using System;
|
|
using System;
|
|
-using System.Collections.Concurrent;
|
|
|
|
using System.Collections.Generic;
|
|
using System.Collections.Generic;
|
|
using System.Diagnostics;
|
|
using System.Diagnostics;
|
|
using System.Diagnostics.CodeAnalysis;
|
|
using System.Diagnostics.CodeAnalysis;
|
|
@@ -12,6 +11,7 @@ using System.Net.Http;
|
|
using System.Text;
|
|
using System.Text;
|
|
using System.Threading;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using System.Threading.Tasks;
|
|
|
|
+using AsyncKeyedLock;
|
|
using MediaBrowser.Common;
|
|
using MediaBrowser.Common;
|
|
using MediaBrowser.Common.Configuration;
|
|
using MediaBrowser.Common.Configuration;
|
|
using MediaBrowser.Common.Extensions;
|
|
using MediaBrowser.Common.Extensions;
|
|
@@ -28,7 +28,7 @@ using UtfUnknown;
|
|
|
|
|
|
namespace MediaBrowser.MediaEncoding.Subtitles
|
|
namespace MediaBrowser.MediaEncoding.Subtitles
|
|
{
|
|
{
|
|
- public sealed class SubtitleEncoder : ISubtitleEncoder
|
|
|
|
|
|
+ public sealed class SubtitleEncoder : ISubtitleEncoder, IDisposable
|
|
{
|
|
{
|
|
private readonly ILogger<SubtitleEncoder> _logger;
|
|
private readonly ILogger<SubtitleEncoder> _logger;
|
|
private readonly IApplicationPaths _appPaths;
|
|
private readonly IApplicationPaths _appPaths;
|
|
@@ -41,8 +41,11 @@ namespace MediaBrowser.MediaEncoding.Subtitles
|
|
/// <summary>
|
|
/// <summary>
|
|
/// The _semaphoreLocks.
|
|
/// The _semaphoreLocks.
|
|
/// </summary>
|
|
/// </summary>
|
|
- private readonly ConcurrentDictionary<string, SemaphoreSlim> _semaphoreLocks =
|
|
|
|
- new ConcurrentDictionary<string, SemaphoreSlim>();
|
|
|
|
|
|
+ private readonly AsyncKeyedLocker<string> _semaphoreLocks = new(o =>
|
|
|
|
+ {
|
|
|
|
+ o.PoolSize = 20;
|
|
|
|
+ o.PoolInitialFill = 1;
|
|
|
|
+ });
|
|
|
|
|
|
public SubtitleEncoder(
|
|
public SubtitleEncoder(
|
|
ILogger<SubtitleEncoder> logger,
|
|
ILogger<SubtitleEncoder> logger,
|
|
@@ -293,16 +296,6 @@ namespace MediaBrowser.MediaEncoding.Subtitles
|
|
throw new ArgumentException("Unsupported format: " + format);
|
|
throw new ArgumentException("Unsupported format: " + format);
|
|
}
|
|
}
|
|
|
|
|
|
- /// <summary>
|
|
|
|
- /// Gets the lock.
|
|
|
|
- /// </summary>
|
|
|
|
- /// <param name="filename">The filename.</param>
|
|
|
|
- /// <returns>System.Object.</returns>
|
|
|
|
- private SemaphoreSlim GetLock(string filename)
|
|
|
|
- {
|
|
|
|
- return _semaphoreLocks.GetOrAdd(filename, _ => new SemaphoreSlim(1, 1));
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
/// <summary>
|
|
/// <summary>
|
|
/// Converts the text subtitle to SRT.
|
|
/// Converts the text subtitle to SRT.
|
|
/// </summary>
|
|
/// </summary>
|
|
@@ -313,21 +306,13 @@ namespace MediaBrowser.MediaEncoding.Subtitles
|
|
/// <returns>Task.</returns>
|
|
/// <returns>Task.</returns>
|
|
private async Task ConvertTextSubtitleToSrt(MediaStream subtitleStream, MediaSourceInfo mediaSource, string outputPath, CancellationToken cancellationToken)
|
|
private async Task ConvertTextSubtitleToSrt(MediaStream subtitleStream, MediaSourceInfo mediaSource, string outputPath, CancellationToken cancellationToken)
|
|
{
|
|
{
|
|
- var semaphore = GetLock(outputPath);
|
|
|
|
-
|
|
|
|
- await semaphore.WaitAsync(cancellationToken).ConfigureAwait(false);
|
|
|
|
-
|
|
|
|
- try
|
|
|
|
|
|
+ using (await _semaphoreLocks.LockAsync(outputPath, cancellationToken).ConfigureAwait(false))
|
|
{
|
|
{
|
|
if (!File.Exists(outputPath))
|
|
if (!File.Exists(outputPath))
|
|
{
|
|
{
|
|
await ConvertTextSubtitleToSrtInternal(subtitleStream, mediaSource, outputPath, cancellationToken).ConfigureAwait(false);
|
|
await ConvertTextSubtitleToSrtInternal(subtitleStream, mediaSource, outputPath, cancellationToken).ConfigureAwait(false);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
- finally
|
|
|
|
- {
|
|
|
|
- semaphore.Release();
|
|
|
|
- }
|
|
|
|
}
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// <summary>
|
|
@@ -472,7 +457,7 @@ namespace MediaBrowser.MediaEncoding.Subtitles
|
|
/// <returns>Task.</returns>
|
|
/// <returns>Task.</returns>
|
|
private async Task ExtractAllTextSubtitles(MediaSourceInfo mediaSource, CancellationToken cancellationToken)
|
|
private async Task ExtractAllTextSubtitles(MediaSourceInfo mediaSource, CancellationToken cancellationToken)
|
|
{
|
|
{
|
|
- var semaphores = new List<SemaphoreSlim>();
|
|
|
|
|
|
+ var locks = new List<AsyncKeyedLockReleaser<string>>();
|
|
var extractableStreams = new List<MediaStream>();
|
|
var extractableStreams = new List<MediaStream>();
|
|
|
|
|
|
try
|
|
try
|
|
@@ -484,16 +469,16 @@ namespace MediaBrowser.MediaEncoding.Subtitles
|
|
{
|
|
{
|
|
var outputPath = GetSubtitleCachePath(mediaSource, subtitleStream.Index, "." + GetTextSubtitleFormat(subtitleStream));
|
|
var outputPath = GetSubtitleCachePath(mediaSource, subtitleStream.Index, "." + GetTextSubtitleFormat(subtitleStream));
|
|
|
|
|
|
- var semaphore = GetLock(outputPath);
|
|
|
|
- await semaphore.WaitAsync(cancellationToken).ConfigureAwait(false);
|
|
|
|
|
|
+ var @lock = _semaphoreLocks.GetOrAdd(outputPath);
|
|
|
|
+ await @lock.SemaphoreSlim.WaitAsync(cancellationToken).ConfigureAwait(false);
|
|
|
|
|
|
if (File.Exists(outputPath))
|
|
if (File.Exists(outputPath))
|
|
{
|
|
{
|
|
- semaphore.Release();
|
|
|
|
|
|
+ @lock.Dispose();
|
|
continue;
|
|
continue;
|
|
}
|
|
}
|
|
|
|
|
|
- semaphores.Add(semaphore);
|
|
|
|
|
|
+ locks.Add(@lock);
|
|
extractableStreams.Add(subtitleStream);
|
|
extractableStreams.Add(subtitleStream);
|
|
}
|
|
}
|
|
|
|
|
|
@@ -508,9 +493,9 @@ namespace MediaBrowser.MediaEncoding.Subtitles
|
|
}
|
|
}
|
|
finally
|
|
finally
|
|
{
|
|
{
|
|
- foreach (var semaphore in semaphores)
|
|
|
|
|
|
+ foreach (var @lock in locks)
|
|
{
|
|
{
|
|
- semaphore.Release();
|
|
|
|
|
|
+ @lock.Dispose();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -657,16 +642,12 @@ namespace MediaBrowser.MediaEncoding.Subtitles
|
|
string outputPath,
|
|
string outputPath,
|
|
CancellationToken cancellationToken)
|
|
CancellationToken cancellationToken)
|
|
{
|
|
{
|
|
- var semaphore = GetLock(outputPath);
|
|
|
|
-
|
|
|
|
- await semaphore.WaitAsync(cancellationToken).ConfigureAwait(false);
|
|
|
|
-
|
|
|
|
- var subtitleStreamIndex = EncodingHelper.FindIndex(mediaSource.MediaStreams, subtitleStream);
|
|
|
|
-
|
|
|
|
- try
|
|
|
|
|
|
+ using (await _semaphoreLocks.LockAsync(outputPath, cancellationToken).ConfigureAwait(false))
|
|
{
|
|
{
|
|
if (!File.Exists(outputPath))
|
|
if (!File.Exists(outputPath))
|
|
{
|
|
{
|
|
|
|
+ var subtitleStreamIndex = EncodingHelper.FindIndex(mediaSource.MediaStreams, subtitleStream);
|
|
|
|
+
|
|
var args = _mediaEncoder.GetInputArgument(mediaSource.Path, mediaSource);
|
|
var args = _mediaEncoder.GetInputArgument(mediaSource.Path, mediaSource);
|
|
|
|
|
|
if (subtitleStream.IsExternal)
|
|
if (subtitleStream.IsExternal)
|
|
@@ -682,10 +663,6 @@ namespace MediaBrowser.MediaEncoding.Subtitles
|
|
cancellationToken).ConfigureAwait(false);
|
|
cancellationToken).ConfigureAwait(false);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
- finally
|
|
|
|
- {
|
|
|
|
- semaphore.Release();
|
|
|
|
- }
|
|
|
|
}
|
|
}
|
|
|
|
|
|
private async Task ExtractTextSubtitleInternal(
|
|
private async Task ExtractTextSubtitleInternal(
|
|
@@ -901,6 +878,12 @@ namespace MediaBrowser.MediaEncoding.Subtitles
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /// <inheritdoc />
|
|
|
|
+ public void Dispose()
|
|
|
|
+ {
|
|
|
|
+ _semaphoreLocks.Dispose();
|
|
|
|
+ }
|
|
|
|
+
|
|
#pragma warning disable CA1034 // Nested types should not be visible
|
|
#pragma warning disable CA1034 // Nested types should not be visible
|
|
// Only public for the unit tests
|
|
// Only public for the unit tests
|
|
public readonly record struct SubtitleInfo
|
|
public readonly record struct SubtitleInfo
|