2021-05-06 06:11:55 -06:00
|
|
|
//
|
|
|
|
// FrameHostObject.m
|
|
|
|
// VisionCamera
|
|
|
|
//
|
|
|
|
// Created by Marc Rousavy on 22.03.21.
|
2021-06-01 05:07:57 -06:00
|
|
|
// Copyright © 2021 mrousavy. All rights reserved.
|
2021-05-06 06:11:55 -06:00
|
|
|
//
|
|
|
|
|
|
|
|
#import "FrameHostObject.h"
|
|
|
|
#import <Foundation/Foundation.h>
|
|
|
|
#import <jsi/jsi.h>
|
|
|
|
|
|
|
|
std::vector<jsi::PropNameID> FrameHostObject::getPropertyNames(jsi::Runtime& rt) {
|
|
|
|
std::vector<jsi::PropNameID> result;
|
|
|
|
result.push_back(jsi::PropNameID::forUtf8(rt, std::string("toString")));
|
|
|
|
result.push_back(jsi::PropNameID::forUtf8(rt, std::string("isValid")));
|
|
|
|
result.push_back(jsi::PropNameID::forUtf8(rt, std::string("width")));
|
|
|
|
result.push_back(jsi::PropNameID::forUtf8(rt, std::string("height")));
|
|
|
|
result.push_back(jsi::PropNameID::forUtf8(rt, std::string("bytesPerRow")));
|
|
|
|
result.push_back(jsi::PropNameID::forUtf8(rt, std::string("planesCount")));
|
2021-07-06 02:08:44 -06:00
|
|
|
result.push_back(jsi::PropNameID::forUtf8(rt, std::string("close")));
|
2021-05-06 06:11:55 -06:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
jsi::Value FrameHostObject::get(jsi::Runtime& runtime, const jsi::PropNameID& propName) {
|
|
|
|
auto name = propName.utf8(runtime);
|
2021-06-01 05:07:57 -06:00
|
|
|
|
2021-05-06 06:11:55 -06:00
|
|
|
if (name == "toString") {
|
2021-07-06 02:08:44 -06:00
|
|
|
auto toString = [this] (jsi::Runtime& runtime, const jsi::Value&, const jsi::Value*, size_t) -> jsi::Value {
|
|
|
|
if (this->frame == nil) {
|
|
|
|
return jsi::String::createFromUtf8(runtime, "[closed frame]");
|
|
|
|
}
|
2021-06-09 02:57:05 -06:00
|
|
|
auto imageBuffer = CMSampleBufferGetImageBuffer(frame.buffer);
|
2021-05-06 06:11:55 -06:00
|
|
|
auto width = CVPixelBufferGetWidth(imageBuffer);
|
|
|
|
auto height = CVPixelBufferGetHeight(imageBuffer);
|
2021-06-01 05:07:57 -06:00
|
|
|
|
2021-05-06 06:11:55 -06:00
|
|
|
NSMutableString* string = [NSMutableString stringWithFormat:@"%lu x %lu Frame", width, height];
|
|
|
|
return jsi::String::createFromUtf8(runtime, string.UTF8String);
|
|
|
|
};
|
|
|
|
return jsi::Function::createFromHostFunction(runtime, jsi::PropNameID::forUtf8(runtime, "toString"), 0, toString);
|
|
|
|
}
|
2021-07-06 02:08:44 -06:00
|
|
|
if (name == "close") {
|
|
|
|
auto close = [this] (jsi::Runtime& runtime, const jsi::Value&, const jsi::Value*, size_t) -> jsi::Value {
|
|
|
|
if (this->frame == nil) {
|
|
|
|
throw jsi::JSError(runtime, "Trying to close an already closed frame! Did you call frame.close() twice?");
|
|
|
|
}
|
|
|
|
this->close();
|
|
|
|
return jsi::Value::undefined();
|
|
|
|
};
|
|
|
|
return jsi::Function::createFromHostFunction(runtime, jsi::PropNameID::forUtf8(runtime, "close"), 0, close);
|
|
|
|
}
|
2021-06-01 05:07:57 -06:00
|
|
|
|
2021-05-06 06:11:55 -06:00
|
|
|
if (name == "isValid") {
|
2021-06-09 02:57:05 -06:00
|
|
|
auto isValid = frame != nil && CMSampleBufferIsValid(frame.buffer);
|
2021-05-06 06:11:55 -06:00
|
|
|
return jsi::Value(isValid);
|
|
|
|
}
|
|
|
|
if (name == "width") {
|
2021-07-06 02:08:44 -06:00
|
|
|
this->assertIsFrameStrong(runtime, name);
|
2021-06-09 02:57:05 -06:00
|
|
|
auto imageBuffer = CMSampleBufferGetImageBuffer(frame.buffer);
|
2021-05-06 06:11:55 -06:00
|
|
|
auto width = CVPixelBufferGetWidth(imageBuffer);
|
|
|
|
return jsi::Value((double) width);
|
|
|
|
}
|
|
|
|
if (name == "height") {
|
2021-07-06 02:08:44 -06:00
|
|
|
this->assertIsFrameStrong(runtime, name);
|
2021-06-09 02:57:05 -06:00
|
|
|
auto imageBuffer = CMSampleBufferGetImageBuffer(frame.buffer);
|
2021-05-06 06:11:55 -06:00
|
|
|
auto height = CVPixelBufferGetHeight(imageBuffer);
|
|
|
|
return jsi::Value((double) height);
|
|
|
|
}
|
|
|
|
if (name == "bytesPerRow") {
|
2021-07-06 02:08:44 -06:00
|
|
|
this->assertIsFrameStrong(runtime, name);
|
2021-06-09 02:57:05 -06:00
|
|
|
auto imageBuffer = CMSampleBufferGetImageBuffer(frame.buffer);
|
2021-05-06 06:11:55 -06:00
|
|
|
auto bytesPerRow = CVPixelBufferGetPlaneCount(imageBuffer);
|
|
|
|
return jsi::Value((double) bytesPerRow);
|
|
|
|
}
|
|
|
|
if (name == "planesCount") {
|
2021-07-06 02:08:44 -06:00
|
|
|
this->assertIsFrameStrong(runtime, name);
|
2021-06-09 02:57:05 -06:00
|
|
|
auto imageBuffer = CMSampleBufferGetImageBuffer(frame.buffer);
|
2021-05-06 06:11:55 -06:00
|
|
|
auto planesCount = CVPixelBufferGetPlaneCount(imageBuffer);
|
|
|
|
return jsi::Value((double) planesCount);
|
|
|
|
}
|
2021-06-01 05:07:57 -06:00
|
|
|
|
2021-05-06 06:11:55 -06:00
|
|
|
return jsi::Value::undefined();
|
|
|
|
}
|
|
|
|
|
2021-07-06 02:08:44 -06:00
|
|
|
void FrameHostObject::assertIsFrameStrong(jsi::Runtime &runtime, const std::string &accessedPropName) {
|
|
|
|
if (frame == nil) {
|
|
|
|
auto message = "Cannot get `" + accessedPropName + "`, frame is already closed!";
|
|
|
|
throw jsi::JSError(runtime, message.c_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void FrameHostObject::close() {
|
|
|
|
if (frame != nil) {
|
|
|
|
CMSampleBufferInvalidate(frame.buffer);
|
|
|
|
// ARC will hopefully delete it lol
|
|
|
|
this->frame = nil;
|
|
|
|
}
|
2021-05-06 06:11:55 -06:00
|
|
|
}
|