From 6b09e0dfedbe0b7c06b6bdfb16ee217bbfddcc45 Mon Sep 17 00:00:00 2001 From: Taner Sener Date: Mon, 22 Aug 2022 00:56:02 +0100 Subject: [PATCH] fix setSessionHistorySize method on native platforms --- .../arthenica/ffmpegkit/FFmpegKitConfig.java | 25 +++++++++++++------ .../ffmpegkit/FFmpegKitConfigTest.java | 20 +++++++++++++++ .../ffmpegkit/FFmpegSessionTest.java | 2 +- apple/src/FFmpegKitConfig.m | 22 +++++++++------- linux/src/FFmpegKitConfig.cpp | 22 +++++++++------- 5 files changed, 65 insertions(+), 26 deletions(-) diff --git a/android/ffmpeg-kit-android-lib/src/main/java/com/arthenica/ffmpegkit/FFmpegKitConfig.java b/android/ffmpeg-kit-android-lib/src/main/java/com/arthenica/ffmpegkit/FFmpegKitConfig.java index c1e99fd..3f82cc3 100644 --- a/android/ffmpeg-kit-android-lib/src/main/java/com/arthenica/ffmpegkit/FFmpegKitConfig.java +++ b/android/ffmpeg-kit-android-lib/src/main/java/com/arthenica/ffmpegkit/FFmpegKitConfig.java @@ -1086,6 +1086,22 @@ public class FFmpegKitConfig { throw new IllegalArgumentException("Session history size must not exceed the hard limit!"); } else if (sessionHistorySize > 0) { FFmpegKitConfig.sessionHistorySize = sessionHistorySize; + deleteExpiredSessions(); + } + } + + /** + * Deletes expired sessions. + */ + private static void deleteExpiredSessions() { + while (sessionHistoryList.size() > sessionHistorySize) { + try { + Session expiredSession = sessionHistoryList.remove(0); + if (expiredSession != null) { + sessionHistoryMap.remove(expiredSession.getSessionId()); + } + } catch (final IndexOutOfBoundsException ignored) { + } } } @@ -1099,18 +1115,13 @@ public class FFmpegKitConfig { /* * ASYNC SESSIONS CALL THIS METHOD TWICE - * THIS CHECK PREVENTS ADDING THE SAME SESSION TWICE + * THIS CHECK PREVENTS ADDING THE SAME SESSION AGAIN */ final boolean sessionAlreadyAdded = sessionHistoryMap.containsKey(session.getSessionId()); if (!sessionAlreadyAdded) { sessionHistoryMap.put(session.getSessionId(), session); sessionHistoryList.add(session); - if (sessionHistoryList.size() > sessionHistorySize) { - try { - sessionHistoryList.remove(0); - } catch (final IndexOutOfBoundsException ignored) { - } - } + deleteExpiredSessions(); } } } diff --git a/android/ffmpeg-kit-android-lib/src/test/java/com/arthenica/ffmpegkit/FFmpegKitConfigTest.java b/android/ffmpeg-kit-android-lib/src/test/java/com/arthenica/ffmpegkit/FFmpegKitConfigTest.java index 40f0b1c..0934ec0 100644 --- a/android/ffmpeg-kit-android-lib/src/test/java/com/arthenica/ffmpegkit/FFmpegKitConfigTest.java +++ b/android/ffmpeg-kit-android-lib/src/test/java/com/arthenica/ffmpegkit/FFmpegKitConfigTest.java @@ -19,6 +19,8 @@ package com.arthenica.ffmpegkit; +import static com.arthenica.ffmpegkit.FFmpegSessionTest.TEST_ARGUMENTS; + import org.junit.Assert; import org.junit.Test; @@ -167,6 +169,24 @@ public class FFmpegKitConfigTest { Assert.assertEquals("mp4", extension); } + @Test + public void setSessionHistorySize() { + int newSize = 15; + FFmpegKitConfig.setSessionHistorySize(newSize); + + for (int i = 1; i <= (newSize + 5); i++) { + new FFmpegSession(TEST_ARGUMENTS); + Assert.assertTrue(FFmpegKitConfig.getSessions().size() <= newSize); + } + + newSize = 3; + FFmpegKitConfig.setSessionHistorySize(newSize); + for (int i = 1; i <= (newSize + 5); i++) { + new FFmpegSession(TEST_ARGUMENTS); + Assert.assertTrue(FFmpegKitConfig.getSessions().size() <= newSize); + } + } + private String listToPackageName(final List externalLibraryList) { boolean speex = externalLibraryList.contains("speex"); boolean fribidi = externalLibraryList.contains("fribidi"); diff --git a/android/ffmpeg-kit-android-lib/src/test/java/com/arthenica/ffmpegkit/FFmpegSessionTest.java b/android/ffmpeg-kit-android-lib/src/test/java/com/arthenica/ffmpegkit/FFmpegSessionTest.java index 488da35..e84e34d 100644 --- a/android/ffmpeg-kit-android-lib/src/test/java/com/arthenica/ffmpegkit/FFmpegSessionTest.java +++ b/android/ffmpeg-kit-android-lib/src/test/java/com/arthenica/ffmpegkit/FFmpegSessionTest.java @@ -26,7 +26,7 @@ import java.util.List; public class FFmpegSessionTest { - private static final String[] TEST_ARGUMENTS = new String[]{"argument1", "argument2"}; + static final String[] TEST_ARGUMENTS = new String[]{"argument1", "argument2"}; @Test public void constructorTest() { diff --git a/apple/src/FFmpegKitConfig.m b/apple/src/FFmpegKitConfig.m index db08744..3194471 100644 --- a/apple/src/FFmpegKitConfig.m +++ b/apple/src/FFmpegKitConfig.m @@ -104,6 +104,16 @@ typedef NS_ENUM(NSUInteger, CallbackType) { StatisticsType }; +void deleteExpiredSessions() { + while ([sessionHistoryList count] > sessionHistorySize) { + id first = [sessionHistoryList firstObject]; + if (first != nil) { + [sessionHistoryList removeObjectAtIndex:0]; + [sessionHistoryMap removeObjectForKey:[NSNumber numberWithLong:[first getSessionId]]]; + } + } +} + void addSessionToSessionHistory(id session) { NSNumber* sessionIdNumber = [NSNumber numberWithLong:[session getSessionId]]; @@ -111,19 +121,12 @@ void addSessionToSessionHistory(id session) { /* * ASYNC SESSIONS CALL THIS METHOD TWICE - * THIS CHECK PREVENTS ADDING THE SAME SESSION TWICE + * THIS CHECK PREVENTS ADDING THE SAME SESSION AGAIN */ if ([sessionHistoryMap objectForKey:sessionIdNumber] == nil) { [sessionHistoryMap setObject:session forKey:sessionIdNumber]; [sessionHistoryList addObject:session]; - if ([sessionHistoryList count] > sessionHistorySize) { - id first = [sessionHistoryList firstObject]; - if (first != nil) { - NSNumber* key = [NSNumber numberWithLong:[first getSessionId]]; - [sessionHistoryList removeObject:key]; - [sessionHistoryMap removeObjectForKey:key]; - } - } + deleteExpiredSessions(); } [sessionHistoryLock unlock]; @@ -1169,6 +1172,7 @@ int executeFFprobe(long sessionId, NSArray* arguments) { @throw([NSException exceptionWithName:NSInvalidArgumentException reason:@"Session history size must not exceed the hard limit!" userInfo:nil]); } else if (pSessionHistorySize > 0) { sessionHistorySize = pSessionHistorySize; + deleteExpiredSessions(); } } diff --git a/linux/src/FFmpegKitConfig.cpp b/linux/src/FFmpegKitConfig.cpp index dd1e9c9..6c23028 100644 --- a/linux/src/FFmpegKitConfig.cpp +++ b/linux/src/FFmpegKitConfig.cpp @@ -146,6 +146,16 @@ static bool fs_create_dir(const std::string& s) { return true; } +void deleteExpiredSessions() { + while (sessionHistoryList.size() > sessionHistorySize) { + auto first = sessionHistoryList.front(); + if (first != nullptr) { + sessionHistoryList.pop_front(); + sessionHistoryMap.erase(first->getSessionId()); + } + } +} + void addSessionToSessionHistory(const std::shared_ptr session) { std::unique_lock lock(sessionMutex, std::defer_lock); @@ -155,19 +165,12 @@ void addSessionToSessionHistory(const std::shared_ptr sessio /* * ASYNC SESSIONS CALL THIS METHOD TWICE - * THIS CHECK PREVENTS ADDING THE SAME SESSION TWICE + * THIS CHECK PREVENTS ADDING THE SAME SESSION AGAIN */ if (sessionHistoryMap.count(sessionId) == 0) { sessionHistoryMap.insert({sessionId, session}); sessionHistoryList.push_back(session); - if (sessionHistoryList.size() > sessionHistorySize) { - auto first = sessionHistoryList.front(); - if (first != nullptr) { - auto expiredSessionId = first->getSessionId(); - sessionHistoryList.pop_front(); - sessionHistoryMap.erase(expiredSessionId); - } - } + deleteExpiredSessions(); } lock.unlock(); @@ -1194,6 +1197,7 @@ void ffmpegkit::FFmpegKitConfig::setSessionHistorySize(const int newSessionHisto throw std::runtime_error("Session history size must not exceed the hard limit!"); } else if (newSessionHistorySize > 0) { sessionHistorySize = newSessionHistorySize; + deleteExpiredSessions(); } }