Skip to content

Commit 4bacee7

Browse files
committed
feat: Print Frame Processor errors to Metro console
1 parent 0bbb03d commit 4bacee7

File tree

3 files changed

+49
-6
lines changed

3 files changed

+49
-6
lines changed

android/src/main/cpp/FrameProcessorRuntimeManager.cpp

+30-4
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,26 @@ CameraView* FrameProcessorRuntimeManager::findCameraViewById(int viewId) {
8282
return result->cthis();
8383
}
8484

85+
void FrameProcessorRuntimeManager::logErrorToJS(std::string message) {
86+
if (!this->jsCallInvoker_) {
87+
return;
88+
}
89+
90+
this->jsCallInvoker_->invokeAsync([this, message]() {
91+
if (this->runtime_ == nullptr) {
92+
return;
93+
}
94+
95+
auto& runtime = *this->runtime_;
96+
auto consoleError = runtime
97+
.global()
98+
.getPropertyAsObject(runtime, "console")
99+
.getPropertyAsFunction(runtime, "error");
100+
consoleError.call(runtime, jsi::String::createFromUtf8(runtime, message));
101+
});
102+
}
103+
104+
85105
// actual JSI installer
86106
void FrameProcessorRuntimeManager::installJSIBindings() {
87107
__android_log_write(ANDROID_LOG_INFO, TAG, "Installing JSI bindings...");
@@ -132,10 +152,16 @@ void FrameProcessorRuntimeManager::installJSIBindings() {
132152
auto function = std::make_shared<jsi::Function>(worklet->getValue(rt).asObject(rt).asFunction(rt));
133153

134154
// assign lambda to frame processor
135-
cameraView->setFrameProcessor([&rt, function](jni::local_ref<JImageProxy::javaobject> frame) {
136-
// create HostObject which holds the Frame (JImageProxy)
137-
auto hostObject = std::make_shared<JImageProxyHostObject>(frame);
138-
function->call(rt, jsi::Object::createFromHostObject(rt, hostObject));
155+
cameraView->setFrameProcessor([this, &rt, function](jni::local_ref<JImageProxy::javaobject> frame) {
156+
try {
157+
// create HostObject which holds the Frame (JImageProxy)
158+
auto hostObject = std::make_shared<JImageProxyHostObject>(frame);
159+
function->call(rt, jsi::Object::createFromHostObject(rt, hostObject));
160+
} catch (jsi::JSError& jsError) {
161+
auto message = "Frame Processor threw an error: " + jsError.getMessage();
162+
__android_log_write(ANDROID_LOG_ERROR, TAG, message.c_str());
163+
this->logErrorToJS(message);
164+
}
139165
});
140166

141167
__android_log_write(ANDROID_LOG_INFO, TAG, "Frame Processor set!");

android/src/main/cpp/FrameProcessorRuntimeManager.h

+1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ class FrameProcessorRuntimeManager : public jni::HybridClass<FrameProcessorRunti
5353
void initializeRuntime();
5454
void installJSIBindings();
5555
void registerPlugin(alias_ref<FrameProcessorPlugin::javaobject> plugin);
56+
void logErrorToJS(std::string message);
5657
};
5758

5859
} // namespace vision

ios/Frame Processor/FrameProcessorUtils.mm

+18-2
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,35 @@
99
#import "FrameProcessorUtils.h"
1010
#import <chrono>
1111
#import <memory>
12+
1213
#import "FrameHostObject.h"
1314
#import "Frame.h"
1415

16+
#import <React/RCTBridge.h>
17+
#import <React/RCTBridge+Private.h>
18+
#import "JSConsoleHelper.h"
19+
#import <ReactCommon/RCTTurboModule.h>
20+
1521
FrameProcessorCallback convertJSIFunctionToFrameProcessorCallback(jsi::Runtime &runtime, const jsi::Function &value) {
1622
__block auto cb = value.getFunction(runtime);
1723

1824
return ^(Frame* frame) {
19-
25+
2026
auto frameHostObject = std::make_shared<FrameHostObject>(frame);
2127
try {
2228
cb.call(runtime, jsi::Object::createFromHostObject(runtime, frameHostObject));
2329
} catch (jsi::JSError& jsError) {
24-
NSLog(@"Frame Processor threw an error: %s", jsError.getMessage().c_str());
30+
auto message = jsError.getMessage();
31+
32+
RCTBridge* bridge = [RCTBridge currentBridge];
33+
if (bridge != nil) {
34+
bridge.jsCallInvoker->invokeAsync([bridge, message]() {
35+
auto logFn = [JSConsoleHelper getLogFunctionForBridge:bridge];
36+
logFn(RCTLogLevelError, [NSString stringWithFormat:@"Frame Processor threw an error: %s", message.c_str()]);
37+
});
38+
} else {
39+
NSLog(@"Frame Processor threw an error: %s", message.c_str());
40+
}
2541
}
2642

2743
// Manually free the buffer because:

0 commit comments

Comments
 (0)