feat: Implement Frame.close()
(#229)
* Implement `Frame.close()` * close frame in dtor * Update JImageProxyHostObject.cpp * fix close * Check if closed * remove a few logs * r * fix `isValid` and `isReady` * Add JImage * Release JNI frame ref on destroy * fix pod setup * Fix isValid call * Fix `close` not returning a function * throw error if closed twice * iOS: Schedule `console.error` call on JS thread * Android: Log Frame Processor Error to JS * fix syntax * Check if valid `toString()` * Update Frame.ts * Remove `isReady` * Fix JImage accessors * remove `JImage` C++ sources * Throw error if accessing props on closed Frame * Delete `JImage.h`
This commit is contained in:
@@ -102,7 +102,6 @@ void FrameProcessorRuntimeManager::logErrorToJS(const std::string& message) {
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
// actual JSI installer
|
||||
void FrameProcessorRuntimeManager::installJSIBindings() {
|
||||
__android_log_write(ANDROID_LOG_INFO, TAG, "Installing JSI bindings...");
|
||||
|
@@ -14,31 +14,38 @@ using namespace jni;
|
||||
|
||||
int JImageProxy::getWidth() {
|
||||
static const auto getWidthMethod = getClass()->getMethod<jint()>("getWidth");
|
||||
return getWidthMethod(javaClassLocal());
|
||||
return getWidthMethod(self());
|
||||
}
|
||||
|
||||
int JImageProxy::getHeight() {
|
||||
static const auto getWidthMethod = getClass()->getMethod<jint()>("getHeight");
|
||||
return getWidthMethod(javaClassLocal());
|
||||
return getWidthMethod(self());
|
||||
}
|
||||
|
||||
bool JImageProxy::getIsValid() {
|
||||
return true;
|
||||
static const auto utilsClass = findClassStatic("com/mrousavy/camera/frameprocessor/ImageProxyUtils");
|
||||
static const auto isImageProxyValidMethod = utilsClass->getStaticMethod<jboolean(JImageProxy::javaobject)>("isImageProxyValid");
|
||||
return isImageProxyValidMethod(utilsClass, self());
|
||||
}
|
||||
|
||||
int JImageProxy::getPlaneCount() {
|
||||
static const auto getPlanesMethod = getClass()->getMethod<JArrayClass<jobject>()>("getPlanes");
|
||||
auto planes = getPlanesMethod(javaClassLocal());
|
||||
auto planes = getPlanesMethod(self());
|
||||
return planes->size();
|
||||
}
|
||||
|
||||
int JImageProxy::getBytesPerRow() {
|
||||
static const auto getPlanesMethod = getClass()->getMethod<JArrayClass<jobject>()>("getPlanes");
|
||||
auto planes = getPlanesMethod(javaClassLocal());
|
||||
auto planes = getPlanesMethod(self());
|
||||
auto firstPlane = planes->getElement(0);
|
||||
|
||||
static const auto getRowStrideMethod = findClassLocal("android/media/Image$PlaneProxy")->getMethod<int()>("getRowStride");
|
||||
return getRowStrideMethod(firstPlane.get());
|
||||
}
|
||||
|
||||
void JImageProxy::close() {
|
||||
static const auto closeMethod = getClass()->getMethod<void()>("close");
|
||||
closeMethod(self());
|
||||
}
|
||||
|
||||
} // namespace vision
|
||||
|
@@ -18,6 +18,8 @@ struct JImageProxy : public facebook::jni::JavaClass<JImageProxy> {
|
||||
bool getIsValid();
|
||||
int getPlaneCount();
|
||||
int getBytesPerRow();
|
||||
void close();
|
||||
|
||||
};
|
||||
|
||||
} // namespace vision
|
||||
|
@@ -13,11 +13,11 @@ std::vector<jsi::PropNameID> JImageProxyHostObject::getPropertyNames(jsi::Runtim
|
||||
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("isReady")));
|
||||
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")));
|
||||
result.push_back(jsi::PropNameID::forUtf8(rt, std::string("close")));
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -25,7 +25,10 @@ jsi::Value JImageProxyHostObject::get(jsi::Runtime& runtime, const jsi::PropName
|
||||
auto name = propNameId.utf8(runtime);
|
||||
|
||||
if (name == "toString") {
|
||||
auto toString = [this] (jsi::Runtime& runtime, const jsi::Value& thisValue, const jsi::Value* arguments, size_t count) -> jsi::Value {
|
||||
auto toString = [this] (jsi::Runtime& runtime, const jsi::Value&, const jsi::Value*, size_t) -> jsi::Value {
|
||||
if (!this->frame) {
|
||||
return jsi::String::createFromUtf8(runtime, "[closed frame]");
|
||||
}
|
||||
auto width = this->frame->getWidth();
|
||||
auto height = this->frame->getHeight();
|
||||
auto str = std::to_string(width) + " x " + std::to_string(height) + " Frame";
|
||||
@@ -33,32 +36,57 @@ jsi::Value JImageProxyHostObject::get(jsi::Runtime& runtime, const jsi::PropName
|
||||
};
|
||||
return jsi::Function::createFromHostFunction(runtime, jsi::PropNameID::forUtf8(runtime, "toString"), 0, toString);
|
||||
}
|
||||
if (name == "close") {
|
||||
auto close = [this] (jsi::Runtime& runtime, const jsi::Value&, const jsi::Value*, size_t) -> jsi::Value {
|
||||
if (!this->frame) {
|
||||
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);
|
||||
}
|
||||
|
||||
if (name == "isValid") {
|
||||
return jsi::Value(this->frame->getIsValid());
|
||||
}
|
||||
if (name == "isReady") {
|
||||
return jsi::Value(this->frame->getIsValid());
|
||||
return jsi::Value(this->frame && this->frame->getIsValid());
|
||||
}
|
||||
if (name == "width") {
|
||||
this->assertIsFrameStrong(runtime, name);
|
||||
return jsi::Value(this->frame->getWidth());
|
||||
}
|
||||
if (name == "height") {
|
||||
this->assertIsFrameStrong(runtime, name);
|
||||
return jsi::Value(this->frame->getHeight());
|
||||
}
|
||||
if (name == "bytesPerRow") {
|
||||
this->assertIsFrameStrong(runtime, name);
|
||||
return jsi::Value(this->frame->getBytesPerRow());
|
||||
}
|
||||
if (name == "planesCount") {
|
||||
this->assertIsFrameStrong(runtime, name);
|
||||
return jsi::Value(this->frame->getPlaneCount());
|
||||
}
|
||||
|
||||
return jsi::Value::undefined();
|
||||
}
|
||||
|
||||
void JImageProxyHostObject::assertIsFrameStrong(jsi::Runtime& runtime, const std::string& accessedPropName) {
|
||||
if (!this->frame) {
|
||||
auto message = "Cannot get `" + accessedPropName + "`, frame is already closed!";
|
||||
throw jsi::JSError(runtime, message.c_str());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
JImageProxyHostObject::~JImageProxyHostObject() {
|
||||
__android_log_write(ANDROID_LOG_INFO, TAG, "Destroying JImageProxyHostObject...");
|
||||
this->close();
|
||||
}
|
||||
|
||||
void JImageProxyHostObject::close() {
|
||||
if (this->frame) {
|
||||
this->frame->close();
|
||||
this->frame.release();
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace vision
|
||||
|
@@ -22,14 +22,17 @@ class JSI_EXPORT JImageProxyHostObject : public jsi::HostObject {
|
||||
|
||||
public:
|
||||
jsi::Value get(jsi::Runtime &, const jsi::PropNameID &name) override;
|
||||
|
||||
std::vector<jsi::PropNameID> getPropertyNames(jsi::Runtime &rt) override;
|
||||
|
||||
void close();
|
||||
|
||||
public:
|
||||
jni::local_ref<JImageProxy> frame;
|
||||
|
||||
private:
|
||||
static auto constexpr TAG = "VisionCamera";
|
||||
|
||||
void assertIsFrameStrong(jsi::Runtime& runtime, const std::string& accessedPropName);
|
||||
};
|
||||
|
||||
} // namespace vision
|
||||
|
@@ -0,0 +1,24 @@
|
||||
package com.mrousavy.camera.frameprocessor;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.media.Image;
|
||||
import androidx.camera.core.ImageProxy;
|
||||
import com.facebook.proguard.annotations.DoNotStrip;
|
||||
|
||||
public class ImageProxyUtils {
|
||||
@SuppressLint("UnsafeOptInUsageError")
|
||||
@DoNotStrip
|
||||
public static boolean isImageProxyValid(ImageProxy imageProxy) {
|
||||
try {
|
||||
Image image = imageProxy.getImage();
|
||||
if (image == null) return false;
|
||||
// will throw an exception if the image is already closed
|
||||
imageProxy.getImage().getCropRect();
|
||||
// no exception thrown, image must still be valid.
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
// exception thrown, image has already been closed.
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user