From 3b4c4bf464ad0f6b067affb2342d3086c6bf8679 Mon Sep 17 00:00:00 2001 From: rlauu <46294892+rlauu@users.noreply.github.com> Date: Sat, 20 Apr 2024 08:37:42 +0200 Subject: [PATCH] Bugfix-FFmpegWrapper --- .../TestAudioFingerprinting.cs | 2 +- .../Analyzers/ChromaprintAnalyzer.cs | 4 ++-- .../FFmpegWrapper.cs | 23 ++++++++----------- 3 files changed, 13 insertions(+), 16 deletions(-) diff --git a/ConfusedPolarBear.Plugin.IntroSkipper.Tests/TestAudioFingerprinting.cs b/ConfusedPolarBear.Plugin.IntroSkipper.Tests/TestAudioFingerprinting.cs index e758469..dc14dad 100644 --- a/ConfusedPolarBear.Plugin.IntroSkipper.Tests/TestAudioFingerprinting.cs +++ b/ConfusedPolarBear.Plugin.IntroSkipper.Tests/TestAudioFingerprinting.cs @@ -81,7 +81,7 @@ public class TestAudioFingerprinting {77, 5}, }; - var actual = FFmpegWrapper.CreateInvertedIndex(Guid.NewGuid(), fpr); + var actual = FFmpegWrapper.CreateInvertedIndex(Guid.NewGuid(), fpr, AnalysisMode.Introduction); Assert.Equal(expected, actual); } diff --git a/ConfusedPolarBear.Plugin.IntroSkipper/Analyzers/ChromaprintAnalyzer.cs b/ConfusedPolarBear.Plugin.IntroSkipper/Analyzers/ChromaprintAnalyzer.cs index c2eb513..30dd2a7 100644 --- a/ConfusedPolarBear.Plugin.IntroSkipper/Analyzers/ChromaprintAnalyzer.cs +++ b/ConfusedPolarBear.Plugin.IntroSkipper/Analyzers/ChromaprintAnalyzer.cs @@ -261,8 +261,8 @@ public class ChromaprintAnalyzer : IMediaFileAnalyzer var rhsRanges = new List(); // Generate inverted indexes for the left and right episodes. - var lhsIndex = FFmpegWrapper.CreateInvertedIndex(lhsId, lhsPoints); - var rhsIndex = FFmpegWrapper.CreateInvertedIndex(rhsId, rhsPoints); + var lhsIndex = FFmpegWrapper.CreateInvertedIndex(lhsId, lhsPoints, this._analysisMode); + var rhsIndex = FFmpegWrapper.CreateInvertedIndex(rhsId, rhsPoints, this._analysisMode); var indexShifts = new HashSet(); // For all audio points in the left episode, check if the right episode has a point which matches exactly. diff --git a/ConfusedPolarBear.Plugin.IntroSkipper/FFmpegWrapper.cs b/ConfusedPolarBear.Plugin.IntroSkipper/FFmpegWrapper.cs index 60de2c0..81a28c9 100644 --- a/ConfusedPolarBear.Plugin.IntroSkipper/FFmpegWrapper.cs +++ b/ConfusedPolarBear.Plugin.IntroSkipper/FFmpegWrapper.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Concurrent; using System.Collections.Generic; using System.Diagnostics; using System.Globalization; @@ -14,8 +15,6 @@ namespace ConfusedPolarBear.Plugin.IntroSkipper; /// public static class FFmpegWrapper { - private static readonly object InvertedIndexCacheLock = new(); - /// /// Used with FFmpeg's silencedetect filter to extract the start and end times of silence. /// @@ -34,7 +33,7 @@ public static class FFmpegWrapper private static Dictionary ChromaprintLogs { get; set; } = new(); - private static Dictionary> InvertedIndexCache { get; set; } = new(); + private static ConcurrentDictionary>> InvertedIndexCache { get; set; } = new(); /// /// Check that the installed version of ffmpeg supports chromaprint. @@ -137,15 +136,16 @@ public static class FFmpegWrapper /// /// Episode ID. /// Chromaprint fingerprint. + /// Mode. /// Inverted index. - public static Dictionary CreateInvertedIndex(Guid id, uint[] fingerprint) + public static Dictionary CreateInvertedIndex(Guid id, uint[] fingerprint, AnalysisMode mode) { - lock (InvertedIndexCacheLock) + var innerDictionary = InvertedIndexCache.GetOrAdd(mode, _ => new ConcurrentDictionary>()); + + // Check if cached for the ID + if (innerDictionary.TryGetValue(id, out var cached)) { - if (InvertedIndexCache.TryGetValue(id, out var cached)) - { - return cached; - } + return cached; } var invIndex = new Dictionary(); @@ -159,10 +159,7 @@ public static class FFmpegWrapper invIndex[point] = i; } - lock (InvertedIndexCacheLock) - { - InvertedIndexCache[id] = invIndex; - } + innerDictionary[id] = invIndex; return invIndex; }