Skip to content

Commit

Permalink
replace execute callback with session specific complete callbacks, fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
tanersener committed Dec 25, 2021
1 parent ea9f267 commit adfce89
Show file tree
Hide file tree
Showing 4 changed files with 463 additions and 192 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public class FFmpegKitReactNativeModule extends ReactContextBaseJavaModule imple
// EVENTS
public static final String EVENT_LOG_CALLBACK_EVENT = "FFmpegKitLogCallbackEvent";
public static final String EVENT_STATISTICS_CALLBACK_EVENT = "FFmpegKitStatisticsCallbackEvent";
public static final String EVENT_EXECUTE_CALLBACK_EVENT = "FFmpegKitExecuteCallbackEvent";
public static final String EVENT_COMPLETE_CALLBACK_EVENT = "FFmpegKitCompleteCallbackEvent";

// REQUEST CODES
public static final int READABLE_REQUEST_CODE = 10000;
Expand Down Expand Up @@ -165,9 +165,19 @@ public void onHostDestroy() {
}

protected void registerGlobalCallbacks(final ReactApplicationContext reactContext) {
FFmpegKitConfig.enableExecuteCallback(session -> {
FFmpegKitConfig.enableFFmpegSessionCompleteCallback(session -> {
final DeviceEventManagerModule.RCTDeviceEventEmitter jsModule = reactContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class);
jsModule.emit(EVENT_EXECUTE_CALLBACK_EVENT, toMap(session));
jsModule.emit(EVENT_COMPLETE_CALLBACK_EVENT, toMap(session));
});

FFmpegKitConfig.enableFFprobeSessionCompleteCallback(session -> {
final DeviceEventManagerModule.RCTDeviceEventEmitter jsModule = reactContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class);
jsModule.emit(EVENT_COMPLETE_CALLBACK_EVENT, toMap(session));
});

FFmpegKitConfig.enableMediaInformationSessionCompleteCallback(session -> {
final DeviceEventManagerModule.RCTDeviceEventEmitter jsModule = reactContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class);
jsModule.emit(EVENT_COMPLETE_CALLBACK_EVENT, toMap(session));
});

FFmpegKitConfig.enableLogCallback(log -> {
Expand Down Expand Up @@ -359,7 +369,7 @@ public void ffmpegSessionGetAllStatistics(final Double sessionId, final Double w
if (session == null) {
promise.reject("SESSION_NOT_FOUND", "Session not found.");
} else {
if (session instanceof FFmpegSession) {
if (session.isFFmpeg()) {
final int timeout;
if (isValidPositiveNumber(waitTimeout)) {
timeout = waitTimeout.intValue();
Expand All @@ -384,7 +394,7 @@ public void ffmpegSessionGetStatistics(final Double sessionId, final Promise pro
if (session == null) {
promise.reject("SESSION_NOT_FOUND", "Session not found.");
} else {
if (session instanceof FFmpegSession) {
if (session.isFFmpeg()) {
final List<Statistics> statistics = ((FFmpegSession) session).getStatistics();
promise.resolve(toStatisticsArray(statistics));
} else {
Expand Down Expand Up @@ -580,7 +590,7 @@ public void ffmpegSessionExecute(final Double sessionId, final Promise promise)
if (session == null) {
promise.reject("SESSION_NOT_FOUND", "Session not found.");
} else {
if (session instanceof FFmpegSession) {
if (session.isFFmpeg()) {
final FFmpegSessionExecuteTask ffmpegSessionExecuteTask = new FFmpegSessionExecuteTask((FFmpegSession) session, promise);
asyncExecutorService.submit(ffmpegSessionExecuteTask);
} else {
Expand All @@ -599,7 +609,7 @@ public void ffprobeSessionExecute(final Double sessionId, final Promise promise)
if (session == null) {
promise.reject("SESSION_NOT_FOUND", "Session not found.");
} else {
if (session instanceof FFprobeSession) {
if (session.isFFprobe()) {
final FFprobeSessionExecuteTask ffprobeSessionExecuteTask = new FFprobeSessionExecuteTask((FFprobeSession) session, promise);
asyncExecutorService.submit(ffprobeSessionExecuteTask);
} else {
Expand All @@ -618,7 +628,7 @@ public void mediaInformationSessionExecute(final Double sessionId, final Double
if (session == null) {
promise.reject("SESSION_NOT_FOUND", "Session not found.");
} else {
if (session instanceof MediaInformationSession) {
if (session.isMediaInformation()) {
final int timeout;
if (isValidPositiveNumber(waitTimeout)) {
timeout = waitTimeout.intValue();
Expand All @@ -643,7 +653,7 @@ public void asyncFFmpegSessionExecute(final Double sessionId, final Promise prom
if (session == null) {
promise.reject("SESSION_NOT_FOUND", "Session not found.");
} else {
if (session instanceof FFmpegSession) {
if (session.isFFmpeg()) {
FFmpegKitConfig.asyncFFmpegExecute((FFmpegSession) session);
promise.resolve(null);
} else {
Expand All @@ -662,7 +672,7 @@ public void asyncFFprobeSessionExecute(final Double sessionId, final Promise pro
if (session == null) {
promise.reject("SESSION_NOT_FOUND", "Session not found.");
} else {
if (session instanceof FFprobeSession) {
if (session.isFFprobe()) {
FFmpegKitConfig.asyncFFprobeExecute((FFprobeSession) session);
promise.resolve(null);
} else {
Expand All @@ -681,7 +691,7 @@ public void asyncMediaInformationSessionExecute(final Double sessionId, final Do
if (session == null) {
promise.reject("SESSION_NOT_FOUND", "Session not found.");
} else {
if (session instanceof MediaInformationSession) {
if (session.isMediaInformation()) {
final int timeout;
if (isValidPositiveNumber(waitTimeout)) {
timeout = waitTimeout.intValue();
Expand Down Expand Up @@ -932,7 +942,12 @@ public void getFFmpegSessions(final Promise promise) {

@ReactMethod
public void getFFprobeSessions(final Promise promise) {
promise.resolve(toSessionArray(FFprobeKit.listSessions()));
promise.resolve(toSessionArray(FFprobeKit.listFFprobeSessions()));
}

@ReactMethod
public void getMediaInformationSessions(final Promise promise) {
promise.resolve(toSessionArray(FFprobeKit.listMediaInformationSessions()));
}

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

if (session.isFFprobe()) {
if (session instanceof MediaInformationSession) {
if (session.isMediaInformation()) {
final MediaInformationSession mediaInformationSession = (MediaInformationSession) session;
final MediaInformation mediaInformation = mediaInformationSession.getMediaInformation();
if (mediaInformation != null) {
Expand Down
48 changes: 31 additions & 17 deletions react-native/ios/FFmpegKitReactNativeModule.m
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
// EVENTS
static NSString *const EVENT_LOG_CALLBACK_EVENT = @"FFmpegKitLogCallbackEvent";
static NSString *const EVENT_STATISTICS_CALLBACK_EVENT = @"FFmpegKitStatisticsCallbackEvent";
static NSString *const EVENT_EXECUTE_CALLBACK_EVENT = @"FFmpegKitExecuteCallbackEvent";
static NSString *const EVENT_COMPLETE_CALLBACK_EVENT = @"FFmpegKitCompleteCallbackEvent";

extern int const AbstractSessionDefaultTimeoutForAsynchronousMessagesInTransmit;

Expand Down Expand Up @@ -91,15 +91,25 @@ - (instancetype)init {

[array addObject:EVENT_LOG_CALLBACK_EVENT];
[array addObject:EVENT_STATISTICS_CALLBACK_EVENT];
[array addObject:EVENT_EXECUTE_CALLBACK_EVENT];
[array addObject:EVENT_COMPLETE_CALLBACK_EVENT];

return array;
}

- (void)registerGlobalCallbacks {
[FFmpegKitConfig enableExecuteCallback:^(id<Session> session){
[FFmpegKitConfig enableFFmpegSessionCompleteCallback:^(FFmpegSession* session){
NSDictionary *dictionary = [FFmpegKitReactNativeModule toSessionDictionary:session];
[self sendEventWithName:EVENT_EXECUTE_CALLBACK_EVENT body:dictionary];
[self sendEventWithName:EVENT_COMPLETE_CALLBACK_EVENT body:dictionary];
}];

[FFmpegKitConfig enableFFprobeSessionCompleteCallback:^(FFprobeSession* session){
NSDictionary *dictionary = [FFmpegKitReactNativeModule toSessionDictionary:session];
[self sendEventWithName:EVENT_COMPLETE_CALLBACK_EVENT body:dictionary];
}];

[FFmpegKitConfig enableMediaInformationSessionCompleteCallback:^(MediaInformationSession* session){
NSDictionary *dictionary = [FFmpegKitReactNativeModule toSessionDictionary:session];
[self sendEventWithName:EVENT_COMPLETE_CALLBACK_EVENT body:dictionary];
}];

[FFmpegKitConfig enableLogCallback: ^(Log* log){
Expand Down Expand Up @@ -234,7 +244,7 @@ - (void)registerGlobalCallbacks {
// FFmpegSession

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

Expand All @@ -243,7 +253,7 @@ - (void)registerGlobalCallbacks {
if (session == nil) {
reject(@"SESSION_NOT_FOUND", @"Session not found.", nil);
} else {
if ([session isMemberOfClass:[FFmpegSession class]]) {
if ([session isFFmpeg]) {
int timeout;
if ([FFmpegKitReactNativeModule isValidPositiveNumber:waitTimeout]) {
timeout = waitTimeout;
Expand All @@ -263,7 +273,7 @@ - (void)registerGlobalCallbacks {
if (session == nil) {
reject(@"SESSION_NOT_FOUND", @"Session not found.", nil);
} else {
if ([session isMemberOfClass:[FFmpegSession class]]) {
if ([session isFFmpeg]) {
NSArray* statistics = [(FFmpegSession*)session getStatistics];
resolve([FFmpegKitReactNativeModule toStatisticsArray:statistics]);
} else {
Expand All @@ -275,14 +285,14 @@ - (void)registerGlobalCallbacks {
// FFprobeSession

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

// MediaInformationSession

RCT_EXPORT_METHOD(mediaInformationSession:(NSArray*)arguments resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {
MediaInformationSession* session = [[MediaInformationSession alloc] init:arguments withExecuteCallback:nil withLogCallback:nil];
MediaInformationSession* session = [[MediaInformationSession alloc] init:arguments withCompleteCallback:nil withLogCallback:nil];
resolve([FFmpegKitReactNativeModule toSessionDictionary:session]);
}

Expand Down Expand Up @@ -409,7 +419,7 @@ - (void)registerGlobalCallbacks {
if (session == nil) {
reject(@"SESSION_NOT_FOUND", @"Session not found.", nil);
} else {
if ([session isMemberOfClass:[FFmpegSession class]]) {
if ([session isFFmpeg]) {
dispatch_async(asyncDispatchQueue, ^{
[FFmpegKitConfig ffmpegExecute:(FFmpegSession*)session];
resolve(nil);
Expand All @@ -425,7 +435,7 @@ - (void)registerGlobalCallbacks {
if (session == nil) {
reject(@"SESSION_NOT_FOUND", @"Session not found.", nil);
} else {
if ([session isMemberOfClass:[FFprobeSession class]]) {
if ([session isFFprobe]) {
dispatch_async(asyncDispatchQueue, ^{
[FFmpegKitConfig ffprobeExecute:(FFprobeSession*)session];
resolve(nil);
Expand All @@ -441,7 +451,7 @@ - (void)registerGlobalCallbacks {
if (session == nil) {
reject(@"SESSION_NOT_FOUND", @"Session not found.", nil);
} else {
if ([session isMemberOfClass:[MediaInformationSession class]]) {
if ([session isMediaInformation]) {
int timeout;
if ([FFmpegKitReactNativeModule isValidPositiveNumber:waitTimeout]) {
timeout = waitTimeout;
Expand All @@ -462,7 +472,7 @@ - (void)registerGlobalCallbacks {
if (session == nil) {
reject(@"SESSION_NOT_FOUND", @"Session not found.", nil);
} else {
if ([session isMemberOfClass:[FFmpegSession class]]) {
if ([session isFFmpeg]) {
[FFmpegKitConfig asyncFFmpegExecute:(FFmpegSession*)session];
resolve(nil);
} else {
Expand All @@ -476,7 +486,7 @@ - (void)registerGlobalCallbacks {
if (session == nil) {
reject(@"SESSION_NOT_FOUND", @"Session not found.", nil);
} else {
if ([session isMemberOfClass:[FFprobeSession class]]) {
if ([session isFFprobe]) {
[FFmpegKitConfig asyncFFprobeExecute:(FFprobeSession*)session];
resolve(nil);
} else {
Expand All @@ -490,7 +500,7 @@ - (void)registerGlobalCallbacks {
if (session == nil) {
reject(@"SESSION_NOT_FOUND", @"Session not found.", nil);
} else {
if ([session isMemberOfClass:[MediaInformationSession class]]) {
if ([session isMediaInformation]) {
int timeout;
if ([FFmpegKitReactNativeModule isValidPositiveNumber:waitTimeout]) {
timeout = waitTimeout;
Expand Down Expand Up @@ -652,7 +662,11 @@ - (void)registerGlobalCallbacks {
// FFprobeKit

RCT_EXPORT_METHOD(getFFprobeSessions:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {
resolve([FFmpegKitReactNativeModule toSessionArray:[FFprobeKit listSessions]]);
resolve([FFmpegKitReactNativeModule toSessionArray:[FFprobeKit listFFprobeSessions]]);
}

RCT_EXPORT_METHOD(getMediaInformationSessions:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {
resolve([FFmpegKitReactNativeModule toSessionArray:[FFprobeKit listMediaInformationSessions]]);
}

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

if ([session isFFprobe]) {
if ([(AbstractSession*)session isMemberOfClass:[MediaInformationSession class]]) {
if ([session isMediaInformation]) {
MediaInformationSession *mediaInformationSession = (MediaInformationSession*)session;
dictionary[KEY_SESSION_MEDIA_INFORMATION] = [FFmpegKitReactNativeModule toMediaInformationDictionary:[mediaInformationSession getMediaInformation]];
dictionary[KEY_SESSION_TYPE] = [NSNumber numberWithInt:SESSION_TYPE_MEDIA_INFORMATION];
Expand Down
Loading

0 comments on commit adfce89

Please sign in to comment.