From 088c77674b5eda627798bbba3bdb528c2fb0f170 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Fri, 18 Oct 2013 16:02:56 -0400 Subject: add locking around subtitle extraction --- .../MediaEncoder/MediaEncoder.cs | 67 ++++++++++++++++++++-- 1 file changed, 63 insertions(+), 4 deletions(-) (limited to 'MediaBrowser.Server.Implementations/MediaEncoder/MediaEncoder.cs') diff --git a/MediaBrowser.Server.Implementations/MediaEncoder/MediaEncoder.cs b/MediaBrowser.Server.Implementations/MediaEncoder/MediaEncoder.cs index a9690c665..fe2246656 100644 --- a/MediaBrowser.Server.Implementations/MediaEncoder/MediaEncoder.cs +++ b/MediaBrowser.Server.Implementations/MediaEncoder/MediaEncoder.cs @@ -1,4 +1,5 @@ -using MediaBrowser.Common.Configuration; +using System.Collections.Concurrent; +using MediaBrowser.Common.Configuration; using MediaBrowser.Common.IO; using MediaBrowser.Common.MediaInfo; using MediaBrowser.Model.Entities; @@ -84,6 +85,21 @@ namespace MediaBrowser.Server.Implementations.MediaEncoder get { return FFMpegPath; } } + /// + /// The _semaphoreLocks + /// + private readonly ConcurrentDictionary _semaphoreLocks = new ConcurrentDictionary(); + + /// + /// Gets the lock. + /// + /// The filename. + /// System.Object. + private SemaphoreSlim GetLock(string filename) + { + return _semaphoreLocks.GetOrAdd(filename, key => new SemaphoreSlim(1, 1)); + } + /// /// Gets the media info. /// @@ -359,6 +375,35 @@ namespace MediaBrowser.Server.Implementations.MediaEncoder ((Process)sender).Dispose(); } + /// + /// Converts the text subtitle to ass. + /// + /// The input path. + /// The output path. + /// The language. + /// The offset. + /// The cancellation token. + /// Task. + public async Task ConvertTextSubtitleToAss(string inputPath, string outputPath, string language, TimeSpan offset, + CancellationToken cancellationToken) + { + var semaphore = GetLock(outputPath); + + await semaphore.WaitAsync(cancellationToken).ConfigureAwait(false); + + try + { + if (!File.Exists(outputPath)) + { + await ConvertTextSubtitleToAssInternal(inputPath, outputPath, language, offset, cancellationToken).ConfigureAwait(false); + } + } + finally + { + semaphore.Release(); + } + } + /// /// Converts the text subtitle to ass. /// @@ -372,7 +417,7 @@ namespace MediaBrowser.Server.Implementations.MediaEncoder /// or /// outputPath /// - public async Task ConvertTextSubtitleToAss(string inputPath, string outputPath, string language, TimeSpan offset, + private async Task ConvertTextSubtitleToAssInternal(string inputPath, string outputPath, string language, TimeSpan offset, CancellationToken cancellationToken) { if (string.IsNullOrEmpty(inputPath)) @@ -562,9 +607,23 @@ namespace MediaBrowser.Server.Implementations.MediaEncoder /// The cancellation token. /// Task. /// Must use inputPath list overload - public Task ExtractTextSubtitle(string[] inputFiles, InputType type, int subtitleStreamIndex, TimeSpan offset, string outputPath, CancellationToken cancellationToken) + public async Task ExtractTextSubtitle(string[] inputFiles, InputType type, int subtitleStreamIndex, TimeSpan offset, string outputPath, CancellationToken cancellationToken) { - return ExtractTextSubtitleInternal(GetInputArgument(inputFiles, type), subtitleStreamIndex, offset, outputPath, cancellationToken); + var semaphore = GetLock(outputPath); + + await semaphore.WaitAsync(cancellationToken).ConfigureAwait(false); + + try + { + if (!File.Exists(outputPath)) + { + await ExtractTextSubtitleInternal(GetInputArgument(inputFiles, type), subtitleStreamIndex, offset, outputPath, cancellationToken).ConfigureAwait(false); + } + } + finally + { + semaphore.Release(); + } } /// -- cgit v1.2.3