use less promises to process logs and statistics

This commit is contained in:
Taner Sener 2021-12-19 21:26:14 +00:00
parent b8ff0d5995
commit 41b3df6fa8

View File

@ -20,18 +20,11 @@ export const LogRedirectionStrategy = {
} }
export const SessionState = { export const SessionState = {
CREATED: 0, CREATED: 0, RUNNING: 1, FAILED: 2, COMPLETED: 3
RUNNING: 1,
FAILED: 2,
COMPLETED: 3
} }
export const Signal = { export const Signal = {
SIGINT: 2, SIGINT: 2, SIGQUIT: 3, SIGPIPE: 13, SIGTERM: 15, SIGXCPU: 24
SIGQUIT: 3,
SIGPIPE: 13,
SIGTERM: 15,
SIGXCPU: 24
} }
class FFmpegKitReactNativeEventEmitter extends NativeEventEmitter { class FFmpegKitReactNativeEventEmitter extends NativeEventEmitter {
@ -1424,16 +1417,7 @@ class FFmpegKitFactory {
static mapToStatistics(statisticsMap) { static mapToStatistics(statisticsMap) {
if (statisticsMap !== undefined) { if (statisticsMap !== undefined) {
return new Statistics( return new Statistics(statisticsMap.sessionId, statisticsMap.videoFrameNumber, statisticsMap.videoFps, statisticsMap.videoQuality, statisticsMap.size, statisticsMap.time, statisticsMap.bitrate, statisticsMap.speed);
statisticsMap.sessionId,
statisticsMap.videoFrameNumber,
statisticsMap.videoFps,
statisticsMap.videoQuality,
statisticsMap.size,
statisticsMap.time,
statisticsMap.bitrate,
statisticsMap.speed
);
} else { } else {
return undefined; return undefined;
} }
@ -1571,99 +1555,96 @@ class FFmpegKitInitializer {
return; return;
} }
FFmpegKitConfig.getSession(sessionId).then(session => { if (FFmpegKitFactory.getLogRedirectionStrategy(sessionId) !== undefined) {
activeLogRedirectionStrategy = session.getLogRedirectionStrategy(); activeLogRedirectionStrategy = FFmpegKitFactory.getLogRedirectionStrategy(sessionId);
}
let activeLogCallback = FFmpegKitFactory.getLogCallback(sessionId);
if (activeLogCallback !== undefined) {
sessionCallbackDefined = true;
if (session.getLogCallback() !== undefined) { try {
sessionCallbackDefined = true; // NOTIFY SESSION CALLBACK DEFINED
activeLogCallback(log);
try { } catch (err) {
// NOTIFY SESSION CALLBACK DEFINED console.log("Exception thrown inside session LogCallback block.", err.stack);
session.getLogCallback()(log);
} catch (err) {
console.log("Exception thrown inside session LogCallback block.", err.stack);
}
} }
}
let globalLogCallbackFunction = FFmpegKitFactory.getGlobalLogCallback(); let globalLogCallbackFunction = FFmpegKitFactory.getGlobalLogCallback();
if (globalLogCallbackFunction !== undefined) { if (globalLogCallbackFunction !== undefined) {
globalCallbackDefined = true; globalCallbackDefined = true;
try { try {
// NOTIFY GLOBAL CALLBACK DEFINED // NOTIFY GLOBAL CALLBACK DEFINED
globalLogCallbackFunction(log); globalLogCallbackFunction(log);
} catch (err) { } catch (err) {
console.log("Exception thrown inside global LogCallback block.", err.stack); console.log("Exception thrown inside global LogCallback block.", err.stack);
}
} }
}
// EXECUTE THE LOG STRATEGY // EXECUTE THE LOG STRATEGY
switch (activeLogRedirectionStrategy) { switch (activeLogRedirectionStrategy) {
case LogRedirectionStrategy.NEVER_PRINT_LOGS: { case LogRedirectionStrategy.NEVER_PRINT_LOGS: {
return;
}
case LogRedirectionStrategy.PRINT_LOGS_WHEN_GLOBAL_CALLBACK_NOT_DEFINED: {
if (globalCallbackDefined) {
return; return;
} }
case LogRedirectionStrategy.PRINT_LOGS_WHEN_GLOBAL_CALLBACK_NOT_DEFINED: {
if (globalCallbackDefined) {
return;
}
}
break;
case LogRedirectionStrategy.PRINT_LOGS_WHEN_SESSION_CALLBACK_NOT_DEFINED: {
if (sessionCallbackDefined) {
return;
}
}
break;
case LogRedirectionStrategy.PRINT_LOGS_WHEN_NO_CALLBACKS_DEFINED: {
if (globalCallbackDefined || sessionCallbackDefined) {
return;
}
}
break;
case LogRedirectionStrategy.ALWAYS_PRINT_LOGS: {
}
break;
} }
break;
case LogRedirectionStrategy.PRINT_LOGS_WHEN_SESSION_CALLBACK_NOT_DEFINED: {
if (sessionCallbackDefined) {
return;
}
}
break;
case LogRedirectionStrategy.PRINT_LOGS_WHEN_NO_CALLBACKS_DEFINED: {
if (globalCallbackDefined || sessionCallbackDefined) {
return;
}
}
break;
case LogRedirectionStrategy.ALWAYS_PRINT_LOGS: {
}
break;
}
// PRINT LOGS // PRINT LOGS
switch (level) { switch (level) {
case Level.AV_LOG_QUIET: { case Level.AV_LOG_QUIET: {
// PRINT NO OUTPUT // PRINT NO OUTPUT
}
break;
default: {
console.log(text);
}
} }
}); break;
default: {
console.log(text);
}
}
} }
static processStatisticsCallbackEvent(event) { static processStatisticsCallbackEvent(event) {
let statistics = FFmpegKitFactory.mapToStatistics(event); let statistics = FFmpegKitFactory.mapToStatistics(event);
let sessionId = event.sessionId; let sessionId = event.sessionId;
FFmpegKitConfig.getSession(sessionId).then(session => { let activeStatisticsCallback = FFmpegKitFactory.getStatisticsCallback(sessionId);
if (session.isFFmpeg()) { if (activeStatisticsCallback !== undefined) {
if (session.getStatisticsCallback() !== undefined) { try {
try { // NOTIFY SESSION CALLBACK DEFINED
// NOTIFY SESSION CALLBACK DEFINED activeStatisticsCallback(statistics);
session.getStatisticsCallback()(statistics); } catch (err) {
} catch (err) { console.log("Exception thrown inside session StatisticsCallback block.", err.stack);
console.log("Exception thrown inside session StatisticsCallback block.", err.stack);
}
}
} }
}
let globalStatisticsCallbackFunction = FFmpegKitFactory.getGlobalStatisticsCallback(); let globalStatisticsCallbackFunction = FFmpegKitFactory.getGlobalStatisticsCallback();
if (globalStatisticsCallbackFunction !== undefined) { if (globalStatisticsCallbackFunction !== undefined) {
try { try {
// NOTIFY GLOBAL CALLBACK DEFINED // NOTIFY GLOBAL CALLBACK DEFINED
globalStatisticsCallbackFunction(statistics); globalStatisticsCallbackFunction(statistics);
} catch (err) { } catch (err) {
console.log("Exception thrown inside global StatisticsCallback block.", err.stack); console.log("Exception thrown inside global StatisticsCallback block.", err.stack);
}
} }
}); }
} }
static processExecuteCallbackEvent(event) { static processExecuteCallbackEvent(event) {