|
40 | 40 | return result;
|
41 | 41 | }
|
42 | 42 |
|
43 |
| -Frame* FrameHostObject::getFrame() { |
44 |
| - if (!_frame.isValid) [[unlikely]] { |
45 |
| - throw std::runtime_error("Frame is already closed! " |
46 |
| - "Are you trying to access the Image data outside of a Frame Processor's lifetime?\n" |
47 |
| - "- If you want to use `console.log(frame)`, use `console.log(frame.toString())` instead.\n" |
48 |
| - "- If you want to do async processing, use `runAsync(...)` instead.\n" |
49 |
| - "- If you want to use runOnJS, increment it's ref-count: `frame.incrementRefCount()`"); |
50 |
| - } |
51 |
| - return _frame; |
52 |
| -} |
53 |
| - |
54 | 43 | #define JSI_FUNC [=](jsi::Runtime & runtime, const jsi::Value& thisValue, const jsi::Value* arguments, size_t count) -> jsi::Value
|
55 | 44 |
|
56 | 45 | jsi::Value FrameHostObject::get(jsi::Runtime& runtime, const jsi::PropNameID& propName) {
|
57 | 46 | auto name = propName.utf8(runtime);
|
58 | 47 |
|
59 | 48 | // Properties
|
60 | 49 | if (name == "width") {
|
61 |
| - Frame* frame = getFrame(); |
62 |
| - return jsi::Value((double)frame.width); |
| 50 | + return jsi::Value((double)_frame.width); |
63 | 51 | }
|
64 | 52 | if (name == "height") {
|
65 |
| - Frame* frame = getFrame(); |
66 |
| - return jsi::Value((double)frame.height); |
| 53 | + return jsi::Value((double)_frame.height); |
67 | 54 | }
|
68 | 55 | if (name == "orientation") {
|
69 |
| - Frame* frame = getFrame(); |
70 |
| - NSString* orientation = [NSString stringWithParsed:frame.orientation]; |
| 56 | + NSString* orientation = [NSString stringWithParsed:_frame.orientation]; |
71 | 57 | return jsi::String::createFromUtf8(runtime, orientation.UTF8String);
|
72 | 58 | }
|
73 | 59 | if (name == "isMirrored") {
|
74 |
| - Frame* frame = getFrame(); |
75 |
| - return jsi::Value(frame.isMirrored); |
| 60 | + return jsi::Value(_frame.isMirrored); |
76 | 61 | }
|
77 | 62 | if (name == "timestamp") {
|
78 |
| - Frame* frame = getFrame(); |
79 |
| - return jsi::Value(frame.timestamp); |
| 63 | + return jsi::Value(_frame.timestamp); |
80 | 64 | }
|
81 | 65 | if (name == "pixelFormat") {
|
82 |
| - Frame* frame = getFrame(); |
83 |
| - return jsi::String::createFromUtf8(runtime, frame.pixelFormat.UTF8String); |
| 66 | + return jsi::String::createFromUtf8(runtime, _frame.pixelFormat.UTF8String); |
84 | 67 | }
|
85 | 68 | if (name == "isValid") {
|
86 |
| - // unsafely access the Frame and try to see if it's valid |
87 |
| - Frame* frame = _frame; |
88 |
| - return jsi::Value(frame != nil && frame.isValid); |
| 69 | + return jsi::Value(_frame != nil && _frame.isValid); |
89 | 70 | }
|
90 | 71 | if (name == "bytesPerRow") {
|
91 |
| - Frame* frame = getFrame(); |
92 |
| - return jsi::Value((double)frame.bytesPerRow); |
| 72 | + return jsi::Value((double)_frame.bytesPerRow); |
93 | 73 | }
|
94 | 74 | if (name == "planesCount") {
|
95 |
| - Frame* frame = getFrame(); |
96 |
| - return jsi::Value((double)frame.planesCount); |
| 75 | + return jsi::Value((double)_frame.planesCount); |
97 | 76 | }
|
98 | 77 |
|
99 | 78 | // Internal methods
|
|
116 | 95 | if (name == "getNativeBuffer") {
|
117 | 96 | auto getNativeBuffer = JSI_FUNC {
|
118 | 97 | // Box-cast to uintptr (just 64-bit address)
|
119 |
| - Frame* frame = getFrame(); |
120 |
| - CMSampleBufferRef sampleBuffer = frame.buffer; |
| 98 | + CMSampleBufferRef sampleBuffer = _frame.buffer; |
121 | 99 | CVPixelBufferRef pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
|
122 | 100 | uintptr_t pointer = reinterpret_cast<uintptr_t>(pixelBuffer);
|
123 | 101 | jsi::HostFunctionType deleteFunc = [=](jsi::Runtime& runtime, const jsi::Value& thisArg, const jsi::Value* args,
|
|
137 | 115 | if (name == "toArrayBuffer") {
|
138 | 116 | auto toArrayBuffer = JSI_FUNC {
|
139 | 117 | // Get CPU readable Pixel Buffer from Frame and write it to a jsi::ArrayBuffer
|
140 |
| - Frame* frame = getFrame(); |
141 |
| - auto pixelBuffer = CMSampleBufferGetImageBuffer(frame.buffer); |
| 118 | + auto pixelBuffer = CMSampleBufferGetImageBuffer(_frame.buffer); |
142 | 119 | auto bytesPerRow = CVPixelBufferGetBytesPerRow(pixelBuffer);
|
143 | 120 | auto height = CVPixelBufferGetHeight(pixelBuffer);
|
144 | 121 |
|
|
172 | 149 | if (name == "toString") {
|
173 | 150 | auto toString = JSI_FUNC {
|
174 | 151 | // Print debug description (width, height)
|
175 |
| - Frame* frame = getFrame(); |
176 |
| - NSMutableString* string = [NSMutableString stringWithFormat:@"%lu x %lu %@ Frame", frame.width, frame.height, frame.pixelFormat]; |
| 152 | + if (!_frame.isValid) { |
| 153 | + return jsi::String::createFromUtf8(runtime, "[closed frame]"); |
| 154 | + } |
| 155 | + NSMutableString* string = [NSMutableString stringWithFormat:@"%lu x %lu %@ Frame", _frame.width, _frame.height, _frame.pixelFormat]; |
177 | 156 | return jsi::String::createFromUtf8(runtime, string.UTF8String);
|
178 | 157 | };
|
179 | 158 | return jsi::Function::createFromHostFunction(runtime, jsi::PropNameID::forUtf8(runtime, "toString"), 0, toString);
|
|
0 commit comments