diff --git a/android/ffmpeg-kit-android-lib/src/main/java/com/arthenica/ffmpegkit/AbstractSession.java b/android/ffmpeg-kit-android-lib/src/main/java/com/arthenica/ffmpegkit/AbstractSession.java index c9697a0..6f77cbb 100644 --- a/android/ffmpeg-kit-android-lib/src/main/java/com/arthenica/ffmpegkit/AbstractSession.java +++ b/android/ffmpeg-kit-android-lib/src/main/java/com/arthenica/ffmpegkit/AbstractSession.java @@ -139,6 +139,8 @@ public abstract class AbstractSession implements Session { this.returnCode = null; this.failStackTrace = null; this.logRedirectionStrategy = logRedirectionStrategy; + + FFmpegKitConfig.addSession(this); } @Override 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 b953f12..489b2ac 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 @@ -604,7 +604,6 @@ public class FFmpegKitConfig { * @param ffmpegSession FFmpeg session which includes command options/arguments */ public static void ffmpegExecute(final FFmpegSession ffmpegSession) { - addSession(ffmpegSession); ffmpegSession.startRunning(); try { @@ -622,7 +621,6 @@ public class FFmpegKitConfig { * @param ffprobeSession FFprobe session which includes command options/arguments */ public static void ffprobeExecute(final FFprobeSession ffprobeSession) { - addSession(ffprobeSession); ffprobeSession.startRunning(); try { @@ -641,7 +639,6 @@ public class FFmpegKitConfig { * @param waitTimeout max time to wait until media information is transmitted */ public static void getMediaInformationExecute(final MediaInformationSession mediaInformationSession, final int waitTimeout) { - addSession(mediaInformationSession); mediaInformationSession.startRunning(); try { @@ -664,8 +661,6 @@ public class FFmpegKitConfig { * @param ffmpegSession FFmpeg session which includes command options/arguments */ public static void asyncFFmpegExecute(final FFmpegSession ffmpegSession) { - addSession(ffmpegSession); - AsyncFFmpegExecuteTask asyncFFmpegExecuteTask = new AsyncFFmpegExecuteTask(ffmpegSession); Future future = asyncExecutorService.submit(asyncFFmpegExecuteTask); ffmpegSession.setFuture(future); @@ -678,8 +673,6 @@ public class FFmpegKitConfig { * @param executorService executor service that will be used to run this asynchronous operation */ public static void asyncFFmpegExecute(final FFmpegSession ffmpegSession, final ExecutorService executorService) { - addSession(ffmpegSession); - AsyncFFmpegExecuteTask asyncFFmpegExecuteTask = new AsyncFFmpegExecuteTask(ffmpegSession); Future future = executorService.submit(asyncFFmpegExecuteTask); ffmpegSession.setFuture(future); @@ -691,8 +684,6 @@ public class FFmpegKitConfig { * @param ffprobeSession FFprobe session which includes command options/arguments */ public static void asyncFFprobeExecute(final FFprobeSession ffprobeSession) { - addSession(ffprobeSession); - AsyncFFprobeExecuteTask asyncFFmpegExecuteTask = new AsyncFFprobeExecuteTask(ffprobeSession); Future future = asyncExecutorService.submit(asyncFFmpegExecuteTask); ffprobeSession.setFuture(future); @@ -705,8 +696,6 @@ public class FFmpegKitConfig { * @param executorService executor service that will be used to run this asynchronous operation */ public static void asyncFFprobeExecute(final FFprobeSession ffprobeSession, final ExecutorService executorService) { - addSession(ffprobeSession); - AsyncFFprobeExecuteTask asyncFFmpegExecuteTask = new AsyncFFprobeExecuteTask(ffprobeSession); Future future = executorService.submit(asyncFFmpegExecuteTask); ffprobeSession.setFuture(future); @@ -719,8 +708,6 @@ public class FFmpegKitConfig { * @param waitTimeout max time to wait until media information is transmitted */ public static void asyncGetMediaInformationExecute(final MediaInformationSession mediaInformationSession, final int waitTimeout) { - addSession(mediaInformationSession); - AsyncGetMediaInformationTask asyncGetMediaInformationTask = new AsyncGetMediaInformationTask(mediaInformationSession, waitTimeout); Future future = asyncExecutorService.submit(asyncGetMediaInformationTask); mediaInformationSession.setFuture(future); @@ -734,8 +721,6 @@ public class FFmpegKitConfig { * @param waitTimeout max time to wait until media information is transmitted */ public static void asyncGetMediaInformationExecute(final MediaInformationSession mediaInformationSession, final ExecutorService executorService, final int waitTimeout) { - addSession(mediaInformationSession); - AsyncGetMediaInformationTask asyncGetMediaInformationTask = new AsyncGetMediaInformationTask(mediaInformationSession, waitTimeout); Future future = executorService.submit(asyncGetMediaInformationTask); mediaInformationSession.setFuture(future); diff --git a/apple/src/AbstractSession.m b/apple/src/AbstractSession.m index 2663de0..a15dc80 100644 --- a/apple/src/AbstractSession.m +++ b/apple/src/AbstractSession.m @@ -29,6 +29,8 @@ int const AbstractSessionDefaultTimeoutForAsynchronousMessagesInTransmit = 5000; static AtomicLong *sessionIdGenerator = nil; +extern void addSessionToSessionHistory(id session); + @implementation AbstractSession { long _sessionId; ExecuteCallback _executeCallback; @@ -65,6 +67,8 @@ static AtomicLong *sessionIdGenerator = nil; _returnCode = nil; _failStackTrace = nil; _logRedirectionStrategy = logRedirectionStrategy; + + addSessionToSessionHistory(self); } return self; diff --git a/apple/src/FFmpegKitConfig.m b/apple/src/FFmpegKitConfig.m index 2c1f9ae..35a7e0b 100644 --- a/apple/src/FFmpegKitConfig.m +++ b/apple/src/FFmpegKitConfig.m @@ -101,6 +101,31 @@ typedef NS_ENUM(NSUInteger, CallbackType) { StatisticsType }; +void addSessionToSessionHistory(id session) { + NSNumber* sessionIdNumber = [NSNumber numberWithLong:[session getSessionId]]; + + [sessionHistoryLock lock]; + + /* + * ASYNC SESSIONS CALL THIS METHOD TWICE + * THIS CHECK PREVENTS ADDING THE SAME SESSION TWICE + */ + 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]; + } + } + } + + [sessionHistoryLock unlock]; +} + /** * Callback data class. */ @@ -278,11 +303,11 @@ CallbackData *callbackDataRemove() { } /** - * Adds a session id to the session map. + * Registers a session id to the session map. * * @param sessionId session id */ -void addSession(long sessionId) { +void registerSessionId(long sessionId) { atomic_store(&sessionMap[sessionId % SESSION_MAP_SIZE], 1); } @@ -557,8 +582,8 @@ int executeFFmpeg(long sessionId, NSArray* arguments) { // REGISTER THE ID BEFORE STARTING THE SESSION _sessionId = sessionId; - addSession(sessionId); - + registerSessionId(sessionId); + resetMessagesInTransmit(sessionId); // RUN @@ -597,7 +622,7 @@ int executeFFprobe(long sessionId, NSArray* arguments) { // REGISTER THE ID BEFORE STARTING THE SESSION _sessionId = sessionId; - addSession(sessionId); + registerSessionId(sessionId); resetMessagesInTransmit(sessionId); @@ -846,7 +871,6 @@ int executeFFprobe(long sessionId, NSArray* arguments) { } + (void)ffmpegExecute:(FFmpegSession*)ffmpegSession { - [FFmpegKitConfig addSession:ffmpegSession]; [ffmpegSession startRunning]; @try { @@ -859,7 +883,6 @@ int executeFFprobe(long sessionId, NSArray* arguments) { } + (void)ffprobeExecute:(FFprobeSession*)ffprobeSession { - [FFmpegKitConfig addSession:ffprobeSession]; [ffprobeSession startRunning]; @try { @@ -872,7 +895,6 @@ int executeFFprobe(long sessionId, NSArray* arguments) { } + (void)getMediaInformationExecute:(MediaInformationSession*)mediaInformationSession withTimeout:(int)waitTimeout { - [FFmpegKitConfig addSession:mediaInformationSession]; [mediaInformationSession startRunning]; @try { @@ -894,8 +916,6 @@ int executeFFprobe(long sessionId, NSArray* arguments) { } + (void)asyncFFmpegExecute:(FFmpegSession*)ffmpegSession onDispatchQueue:(dispatch_queue_t)queue { - [FFmpegKitConfig addSession:ffmpegSession]; - dispatch_async(queue, ^{ [FFmpegKitConfig ffmpegExecute:ffmpegSession]; ExecuteCallback globalExecuteCallback = [FFmpegKitConfig getExecuteCallback]; @@ -915,8 +935,6 @@ int executeFFprobe(long sessionId, NSArray* arguments) { } + (void)asyncFFprobeExecute:(FFprobeSession*)ffprobeSession onDispatchQueue:(dispatch_queue_t)queue { - [FFmpegKitConfig addSession:ffprobeSession]; - dispatch_async(queue, ^{ [FFmpegKitConfig ffprobeExecute:ffprobeSession]; ExecuteCallback globalExecuteCallback = [FFmpegKitConfig getExecuteCallback]; @@ -936,8 +954,6 @@ int executeFFprobe(long sessionId, NSArray* arguments) { } + (void)asyncGetMediaInformationExecute:(MediaInformationSession*)mediaInformationSession onDispatchQueue:(dispatch_queue_t)queue withTimeout:(int)waitTimeout { - [FFmpegKitConfig addSession:mediaInformationSession]; - dispatch_async(queue, ^{ [FFmpegKitConfig getMediaInformationExecute:mediaInformationSession withTimeout:waitTimeout]; ExecuteCallback globalExecuteCallback = [FFmpegKitConfig getExecuteCallback]; @@ -1008,31 +1024,6 @@ int executeFFprobe(long sessionId, NSArray* arguments) { } } -+ (void)addSession:(id)session { - NSNumber* sessionIdNumber = [NSNumber numberWithLong:[session getSessionId]]; - - [sessionHistoryLock lock]; - - /* - * ASYNC SESSIONS CALL THIS METHOD TWICE - * THIS CHECK PREVENTS ADDING THE SAME SESSION TWICE - */ - 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]; - } - } - } - - [sessionHistoryLock unlock]; -} - + (id)getSession:(long)sessionId { [sessionHistoryLock lock];