fix setSessionHistorySize method on native platforms

This commit is contained in:
Taner Sener 2022-08-22 00:56:02 +01:00
parent cf29f1c6a7
commit 6b09e0dfed
5 changed files with 65 additions and 26 deletions

View File

@ -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();
}
}
}

View File

@ -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<String> externalLibraryList) {
boolean speex = externalLibraryList.contains("speex");
boolean fribidi = externalLibraryList.contains("fribidi");

View File

@ -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() {

View File

@ -104,6 +104,16 @@ typedef NS_ENUM(NSUInteger, CallbackType) {
StatisticsType
};
void deleteExpiredSessions() {
while ([sessionHistoryList count] > sessionHistorySize) {
id<Session> first = [sessionHistoryList firstObject];
if (first != nil) {
[sessionHistoryList removeObjectAtIndex:0];
[sessionHistoryMap removeObjectForKey:[NSNumber numberWithLong:[first getSessionId]]];
}
}
}
void addSessionToSessionHistory(id<Session> session) {
NSNumber* sessionIdNumber = [NSNumber numberWithLong:[session getSessionId]];
@ -111,19 +121,12 @@ void addSessionToSessionHistory(id<Session> 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<Session> 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();
}
}

View File

@ -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<ffmpegkit::Session> session) {
std::unique_lock<std::recursive_mutex> lock(sessionMutex, std::defer_lock);
@ -155,19 +165,12 @@ void addSessionToSessionHistory(const std::shared_ptr<ffmpegkit::Session> 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();
}
}