diff options
| author | Luke Pulverenti <luke.pulverenti@gmail.com> | 2013-10-18 16:02:56 -0400 |
|---|---|---|
| committer | Luke Pulverenti <luke.pulverenti@gmail.com> | 2013-10-18 16:02:56 -0400 |
| commit | 088c77674b5eda627798bbba3bdb528c2fb0f170 (patch) | |
| tree | 529d81c5c4d6aa6242a902eaef1c2b32d5dc0e65 /MediaBrowser.Server.Implementations/MediaEncoder/MediaEncoder.cs | |
| parent | e6829fcdf0db7e4ca0f42e414e077f31f621ad8b (diff) | |
add locking around subtitle extraction
Diffstat (limited to 'MediaBrowser.Server.Implementations/MediaEncoder/MediaEncoder.cs')
| -rw-r--r-- | MediaBrowser.Server.Implementations/MediaEncoder/MediaEncoder.cs | 67 |
1 files changed, 63 insertions, 4 deletions
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; @@ -85,6 +86,21 @@ namespace MediaBrowser.Server.Implementations.MediaEncoder } /// <summary> + /// The _semaphoreLocks + /// </summary> + private readonly ConcurrentDictionary<string, SemaphoreSlim> _semaphoreLocks = new ConcurrentDictionary<string, SemaphoreSlim>(); + + /// <summary> + /// Gets the lock. + /// </summary> + /// <param name="filename">The filename.</param> + /// <returns>System.Object.</returns> + private SemaphoreSlim GetLock(string filename) + { + return _semaphoreLocks.GetOrAdd(filename, key => new SemaphoreSlim(1, 1)); + } + + /// <summary> /// Gets the media info. /// </summary> /// <param name="inputFiles">The input files.</param> @@ -368,11 +384,40 @@ namespace MediaBrowser.Server.Implementations.MediaEncoder /// <param name="offset">The offset.</param> /// <param name="cancellationToken">The cancellation token.</param> /// <returns>Task.</returns> + 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(); + } + } + + /// <summary> + /// Converts the text subtitle to ass. + /// </summary> + /// <param name="inputPath">The input path.</param> + /// <param name="outputPath">The output path.</param> + /// <param name="language">The language.</param> + /// <param name="offset">The offset.</param> + /// <param name="cancellationToken">The cancellation token.</param> + /// <returns>Task.</returns> /// <exception cref="System.ArgumentNullException">inputPath /// or /// outputPath</exception> /// <exception cref="System.ApplicationException"></exception> - 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 /// <param name="cancellationToken">The cancellation token.</param> /// <returns>Task.</returns> /// <exception cref="System.ArgumentException">Must use inputPath list overload</exception> - 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(); + } } /// <summary> |
