Skip to content
This repository was archived by the owner on Apr 21, 2025. It is now read-only.

Commit adfce89

Browse files
committed
replace execute callback with session specific complete callbacks, fixes #197
1 parent ea9f267 commit adfce89

File tree

4 files changed

+463
-192
lines changed

4 files changed

+463
-192
lines changed

react-native/android/src/main/java/com/arthenica/ffmpegkit/reactnative/FFmpegKitReactNativeModule.java

+28-13
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public class FFmpegKitReactNativeModule extends ReactContextBaseJavaModule imple
111111
// EVENTS
112112
public static final String EVENT_LOG_CALLBACK_EVENT = "FFmpegKitLogCallbackEvent";
113113
public static final String EVENT_STATISTICS_CALLBACK_EVENT = "FFmpegKitStatisticsCallbackEvent";
114-
public static final String EVENT_EXECUTE_CALLBACK_EVENT = "FFmpegKitExecuteCallbackEvent";
114+
public static final String EVENT_COMPLETE_CALLBACK_EVENT = "FFmpegKitCompleteCallbackEvent";
115115

116116
// REQUEST CODES
117117
public static final int READABLE_REQUEST_CODE = 10000;
@@ -165,9 +165,19 @@ public void onHostDestroy() {
165165
}
166166

167167
protected void registerGlobalCallbacks(final ReactApplicationContext reactContext) {
168-
FFmpegKitConfig.enableExecuteCallback(session -> {
168+
FFmpegKitConfig.enableFFmpegSessionCompleteCallback(session -> {
169169
final DeviceEventManagerModule.RCTDeviceEventEmitter jsModule = reactContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class);
170-
jsModule.emit(EVENT_EXECUTE_CALLBACK_EVENT, toMap(session));
170+
jsModule.emit(EVENT_COMPLETE_CALLBACK_EVENT, toMap(session));
171+
});
172+
173+
FFmpegKitConfig.enableFFprobeSessionCompleteCallback(session -> {
174+
final DeviceEventManagerModule.RCTDeviceEventEmitter jsModule = reactContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class);
175+
jsModule.emit(EVENT_COMPLETE_CALLBACK_EVENT, toMap(session));
176+
});
177+
178+
FFmpegKitConfig.enableMediaInformationSessionCompleteCallback(session -> {
179+
final DeviceEventManagerModule.RCTDeviceEventEmitter jsModule = reactContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class);
180+
jsModule.emit(EVENT_COMPLETE_CALLBACK_EVENT, toMap(session));
171181
});
172182

173183
FFmpegKitConfig.enableLogCallback(log -> {
@@ -359,7 +369,7 @@ public void ffmpegSessionGetAllStatistics(final Double sessionId, final Double w
359369
if (session == null) {
360370
promise.reject("SESSION_NOT_FOUND", "Session not found.");
361371
} else {
362-
if (session instanceof FFmpegSession) {
372+
if (session.isFFmpeg()) {
363373
final int timeout;
364374
if (isValidPositiveNumber(waitTimeout)) {
365375
timeout = waitTimeout.intValue();
@@ -384,7 +394,7 @@ public void ffmpegSessionGetStatistics(final Double sessionId, final Promise pro
384394
if (session == null) {
385395
promise.reject("SESSION_NOT_FOUND", "Session not found.");
386396
} else {
387-
if (session instanceof FFmpegSession) {
397+
if (session.isFFmpeg()) {
388398
final List<Statistics> statistics = ((FFmpegSession) session).getStatistics();
389399
promise.resolve(toStatisticsArray(statistics));
390400
} else {
@@ -580,7 +590,7 @@ public void ffmpegSessionExecute(final Double sessionId, final Promise promise)
580590
if (session == null) {
581591
promise.reject("SESSION_NOT_FOUND", "Session not found.");
582592
} else {
583-
if (session instanceof FFmpegSession) {
593+
if (session.isFFmpeg()) {
584594
final FFmpegSessionExecuteTask ffmpegSessionExecuteTask = new FFmpegSessionExecuteTask((FFmpegSession) session, promise);
585595
asyncExecutorService.submit(ffmpegSessionExecuteTask);
586596
} else {
@@ -599,7 +609,7 @@ public void ffprobeSessionExecute(final Double sessionId, final Promise promise)
599609
if (session == null) {
600610
promise.reject("SESSION_NOT_FOUND", "Session not found.");
601611
} else {
602-
if (session instanceof FFprobeSession) {
612+
if (session.isFFprobe()) {
603613
final FFprobeSessionExecuteTask ffprobeSessionExecuteTask = new FFprobeSessionExecuteTask((FFprobeSession) session, promise);
604614
asyncExecutorService.submit(ffprobeSessionExecuteTask);
605615
} else {
@@ -618,7 +628,7 @@ public void mediaInformationSessionExecute(final Double sessionId, final Double
618628
if (session == null) {
619629
promise.reject("SESSION_NOT_FOUND", "Session not found.");
620630
} else {
621-
if (session instanceof MediaInformationSession) {
631+
if (session.isMediaInformation()) {
622632
final int timeout;
623633
if (isValidPositiveNumber(waitTimeout)) {
624634
timeout = waitTimeout.intValue();
@@ -643,7 +653,7 @@ public void asyncFFmpegSessionExecute(final Double sessionId, final Promise prom
643653
if (session == null) {
644654
promise.reject("SESSION_NOT_FOUND", "Session not found.");
645655
} else {
646-
if (session instanceof FFmpegSession) {
656+
if (session.isFFmpeg()) {
647657
FFmpegKitConfig.asyncFFmpegExecute((FFmpegSession) session);
648658
promise.resolve(null);
649659
} else {
@@ -662,7 +672,7 @@ public void asyncFFprobeSessionExecute(final Double sessionId, final Promise pro
662672
if (session == null) {
663673
promise.reject("SESSION_NOT_FOUND", "Session not found.");
664674
} else {
665-
if (session instanceof FFprobeSession) {
675+
if (session.isFFprobe()) {
666676
FFmpegKitConfig.asyncFFprobeExecute((FFprobeSession) session);
667677
promise.resolve(null);
668678
} else {
@@ -681,7 +691,7 @@ public void asyncMediaInformationSessionExecute(final Double sessionId, final Do
681691
if (session == null) {
682692
promise.reject("SESSION_NOT_FOUND", "Session not found.");
683693
} else {
684-
if (session instanceof MediaInformationSession) {
694+
if (session.isMediaInformation()) {
685695
final int timeout;
686696
if (isValidPositiveNumber(waitTimeout)) {
687697
timeout = waitTimeout.intValue();
@@ -932,7 +942,12 @@ public void getFFmpegSessions(final Promise promise) {
932942

933943
@ReactMethod
934944
public void getFFprobeSessions(final Promise promise) {
935-
promise.resolve(toSessionArray(FFprobeKit.listSessions()));
945+
promise.resolve(toSessionArray(FFprobeKit.listFFprobeSessions()));
946+
}
947+
948+
@ReactMethod
949+
public void getMediaInformationSessions(final Promise promise) {
950+
promise.resolve(toSessionArray(FFprobeKit.listMediaInformationSessions()));
936951
}
937952

938953
// Packages
@@ -980,7 +995,7 @@ protected static WritableMap toMap(final Session session) {
980995
sessionMap.putString(KEY_SESSION_COMMAND, session.getCommand());
981996

982997
if (session.isFFprobe()) {
983-
if (session instanceof MediaInformationSession) {
998+
if (session.isMediaInformation()) {
984999
final MediaInformationSession mediaInformationSession = (MediaInformationSession) session;
9851000
final MediaInformation mediaInformation = mediaInformationSession.getMediaInformation();
9861001
if (mediaInformation != null) {

react-native/ios/FFmpegKitReactNativeModule.m

+31-17
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
// EVENTS
6262
static NSString *const EVENT_LOG_CALLBACK_EVENT = @"FFmpegKitLogCallbackEvent";
6363
static NSString *const EVENT_STATISTICS_CALLBACK_EVENT = @"FFmpegKitStatisticsCallbackEvent";
64-
static NSString *const EVENT_EXECUTE_CALLBACK_EVENT = @"FFmpegKitExecuteCallbackEvent";
64+
static NSString *const EVENT_COMPLETE_CALLBACK_EVENT = @"FFmpegKitCompleteCallbackEvent";
6565

6666
extern int const AbstractSessionDefaultTimeoutForAsynchronousMessagesInTransmit;
6767

@@ -91,15 +91,25 @@ - (instancetype)init {
9191

9292
[array addObject:EVENT_LOG_CALLBACK_EVENT];
9393
[array addObject:EVENT_STATISTICS_CALLBACK_EVENT];
94-
[array addObject:EVENT_EXECUTE_CALLBACK_EVENT];
94+
[array addObject:EVENT_COMPLETE_CALLBACK_EVENT];
9595

9696
return array;
9797
}
9898

9999
- (void)registerGlobalCallbacks {
100-
[FFmpegKitConfig enableExecuteCallback:^(id<Session> session){
100+
[FFmpegKitConfig enableFFmpegSessionCompleteCallback:^(FFmpegSession* session){
101101
NSDictionary *dictionary = [FFmpegKitReactNativeModule toSessionDictionary:session];
102-
[self sendEventWithName:EVENT_EXECUTE_CALLBACK_EVENT body:dictionary];
102+
[self sendEventWithName:EVENT_COMPLETE_CALLBACK_EVENT body:dictionary];
103+
}];
104+
105+
[FFmpegKitConfig enableFFprobeSessionCompleteCallback:^(FFprobeSession* session){
106+
NSDictionary *dictionary = [FFmpegKitReactNativeModule toSessionDictionary:session];
107+
[self sendEventWithName:EVENT_COMPLETE_CALLBACK_EVENT body:dictionary];
108+
}];
109+
110+
[FFmpegKitConfig enableMediaInformationSessionCompleteCallback:^(MediaInformationSession* session){
111+
NSDictionary *dictionary = [FFmpegKitReactNativeModule toSessionDictionary:session];
112+
[self sendEventWithName:EVENT_COMPLETE_CALLBACK_EVENT body:dictionary];
103113
}];
104114

105115
[FFmpegKitConfig enableLogCallback: ^(Log* log){
@@ -234,7 +244,7 @@ - (void)registerGlobalCallbacks {
234244
// FFmpegSession
235245

236246
RCT_EXPORT_METHOD(ffmpegSession:(NSArray*)arguments resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {
237-
FFmpegSession* session = [[FFmpegSession alloc] init:arguments withExecuteCallback:nil withLogCallback:nil withStatisticsCallback:nil withLogRedirectionStrategy:LogRedirectionStrategyNeverPrintLogs];
247+
FFmpegSession* session = [[FFmpegSession alloc] init:arguments withCompleteCallback:nil withLogCallback:nil withStatisticsCallback:nil withLogRedirectionStrategy:LogRedirectionStrategyNeverPrintLogs];
238248
resolve([FFmpegKitReactNativeModule toSessionDictionary:session]);
239249
}
240250

@@ -243,7 +253,7 @@ - (void)registerGlobalCallbacks {
243253
if (session == nil) {
244254
reject(@"SESSION_NOT_FOUND", @"Session not found.", nil);
245255
} else {
246-
if ([session isMemberOfClass:[FFmpegSession class]]) {
256+
if ([session isFFmpeg]) {
247257
int timeout;
248258
if ([FFmpegKitReactNativeModule isValidPositiveNumber:waitTimeout]) {
249259
timeout = waitTimeout;
@@ -263,7 +273,7 @@ - (void)registerGlobalCallbacks {
263273
if (session == nil) {
264274
reject(@"SESSION_NOT_FOUND", @"Session not found.", nil);
265275
} else {
266-
if ([session isMemberOfClass:[FFmpegSession class]]) {
276+
if ([session isFFmpeg]) {
267277
NSArray* statistics = [(FFmpegSession*)session getStatistics];
268278
resolve([FFmpegKitReactNativeModule toStatisticsArray:statistics]);
269279
} else {
@@ -275,14 +285,14 @@ - (void)registerGlobalCallbacks {
275285
// FFprobeSession
276286

277287
RCT_EXPORT_METHOD(ffprobeSession:(NSArray*)arguments resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {
278-
FFprobeSession* session = [[FFprobeSession alloc] init:arguments withExecuteCallback:nil withLogCallback:nil withLogRedirectionStrategy:LogRedirectionStrategyNeverPrintLogs];
288+
FFprobeSession* session = [[FFprobeSession alloc] init:arguments withCompleteCallback:nil withLogCallback:nil withLogRedirectionStrategy:LogRedirectionStrategyNeverPrintLogs];
279289
resolve([FFmpegKitReactNativeModule toSessionDictionary:session]);
280290
}
281291

282292
// MediaInformationSession
283293

284294
RCT_EXPORT_METHOD(mediaInformationSession:(NSArray*)arguments resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {
285-
MediaInformationSession* session = [[MediaInformationSession alloc] init:arguments withExecuteCallback:nil withLogCallback:nil];
295+
MediaInformationSession* session = [[MediaInformationSession alloc] init:arguments withCompleteCallback:nil withLogCallback:nil];
286296
resolve([FFmpegKitReactNativeModule toSessionDictionary:session]);
287297
}
288298

@@ -409,7 +419,7 @@ - (void)registerGlobalCallbacks {
409419
if (session == nil) {
410420
reject(@"SESSION_NOT_FOUND", @"Session not found.", nil);
411421
} else {
412-
if ([session isMemberOfClass:[FFmpegSession class]]) {
422+
if ([session isFFmpeg]) {
413423
dispatch_async(asyncDispatchQueue, ^{
414424
[FFmpegKitConfig ffmpegExecute:(FFmpegSession*)session];
415425
resolve(nil);
@@ -425,7 +435,7 @@ - (void)registerGlobalCallbacks {
425435
if (session == nil) {
426436
reject(@"SESSION_NOT_FOUND", @"Session not found.", nil);
427437
} else {
428-
if ([session isMemberOfClass:[FFprobeSession class]]) {
438+
if ([session isFFprobe]) {
429439
dispatch_async(asyncDispatchQueue, ^{
430440
[FFmpegKitConfig ffprobeExecute:(FFprobeSession*)session];
431441
resolve(nil);
@@ -441,7 +451,7 @@ - (void)registerGlobalCallbacks {
441451
if (session == nil) {
442452
reject(@"SESSION_NOT_FOUND", @"Session not found.", nil);
443453
} else {
444-
if ([session isMemberOfClass:[MediaInformationSession class]]) {
454+
if ([session isMediaInformation]) {
445455
int timeout;
446456
if ([FFmpegKitReactNativeModule isValidPositiveNumber:waitTimeout]) {
447457
timeout = waitTimeout;
@@ -462,7 +472,7 @@ - (void)registerGlobalCallbacks {
462472
if (session == nil) {
463473
reject(@"SESSION_NOT_FOUND", @"Session not found.", nil);
464474
} else {
465-
if ([session isMemberOfClass:[FFmpegSession class]]) {
475+
if ([session isFFmpeg]) {
466476
[FFmpegKitConfig asyncFFmpegExecute:(FFmpegSession*)session];
467477
resolve(nil);
468478
} else {
@@ -476,7 +486,7 @@ - (void)registerGlobalCallbacks {
476486
if (session == nil) {
477487
reject(@"SESSION_NOT_FOUND", @"Session not found.", nil);
478488
} else {
479-
if ([session isMemberOfClass:[FFprobeSession class]]) {
489+
if ([session isFFprobe]) {
480490
[FFmpegKitConfig asyncFFprobeExecute:(FFprobeSession*)session];
481491
resolve(nil);
482492
} else {
@@ -490,7 +500,7 @@ - (void)registerGlobalCallbacks {
490500
if (session == nil) {
491501
reject(@"SESSION_NOT_FOUND", @"Session not found.", nil);
492502
} else {
493-
if ([session isMemberOfClass:[MediaInformationSession class]]) {
503+
if ([session isMediaInformation]) {
494504
int timeout;
495505
if ([FFmpegKitReactNativeModule isValidPositiveNumber:waitTimeout]) {
496506
timeout = waitTimeout;
@@ -652,7 +662,11 @@ - (void)registerGlobalCallbacks {
652662
// FFprobeKit
653663

654664
RCT_EXPORT_METHOD(getFFprobeSessions:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {
655-
resolve([FFmpegKitReactNativeModule toSessionArray:[FFprobeKit listSessions]]);
665+
resolve([FFmpegKitReactNativeModule toSessionArray:[FFprobeKit listFFprobeSessions]]);
666+
}
667+
668+
RCT_EXPORT_METHOD(getMediaInformationSessions:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {
669+
resolve([FFmpegKitReactNativeModule toSessionArray:[FFprobeKit listMediaInformationSessions]]);
656670
}
657671

658672
// Packages
@@ -695,7 +709,7 @@ + (NSDictionary*)toSessionDictionary:(id<Session>) session {
695709
dictionary[KEY_SESSION_COMMAND] = [session getCommand];
696710

697711
if ([session isFFprobe]) {
698-
if ([(AbstractSession*)session isMemberOfClass:[MediaInformationSession class]]) {
712+
if ([session isMediaInformation]) {
699713
MediaInformationSession *mediaInformationSession = (MediaInformationSession*)session;
700714
dictionary[KEY_SESSION_MEDIA_INFORMATION] = [FFmpegKitReactNativeModule toMediaInformationDictionary:[mediaInformationSession getMediaInformation]];
701715
dictionary[KEY_SESSION_TYPE] = [NSNumber numberWithInt:SESSION_TYPE_MEDIA_INFORMATION];

0 commit comments

Comments
 (0)