From 7d391ae7f740a396d1668fd52013df2ba622ecae Mon Sep 17 00:00:00 2001 From: William Candillon Date: Wed, 12 Jun 2024 12:54:25 +0200 Subject: [PATCH] =?UTF-8?q?fix(=F0=9F=93=B8):=20load=20video=20metadata=20?= =?UTF-8?q?on=20dedicated=20thread=20(#2473)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../android/cpp/jni/JniPlatformContext.cpp | 7 +++-- package/src/external/reanimated/useVideo.ts | 31 +++++++++++++++++-- 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/package/android/cpp/jni/JniPlatformContext.cpp b/package/android/cpp/jni/JniPlatformContext.cpp index 2edb19f67f..ba3e70e0c9 100644 --- a/package/android/cpp/jni/JniPlatformContext.cpp +++ b/package/android/cpp/jni/JniPlatformContext.cpp @@ -77,6 +77,8 @@ TSelf JniPlatformContext::initHybrid(jni::alias_ref jThis, jni::global_ref JniPlatformContext::createVideo(const std::string &url) { + jni::Environment::ensureCurrentThreadIsAttached(); + jni::ThreadScope ts; // Manages JNI thread attachment/detachment // Get the JNI environment @@ -85,14 +87,13 @@ JniPlatformContext::createVideo(const std::string &url) { // Convert std::string to jstring jstring jUrl = env->NewStringUTF(url.c_str()); - // Get the method ID for the createVideo method - // Replace "Lcom/yourpackage/RNSkVideo;" with the actual return type - // descriptor static auto method = javaPart_->getClass()->getMethod("createVideo"); // Call the method and receive a local reference to the video object auto videoObject = method(javaPart_.get(), jUrl); + env->DeleteLocalRef(jUrl); + // Clean up the jstring local reference auto result = jni::make_global(videoObject); return result; diff --git a/package/src/external/reanimated/useVideo.ts b/package/src/external/reanimated/useVideo.ts index 0d9638e840..5969bf1ee6 100644 --- a/package/src/external/reanimated/useVideo.ts +++ b/package/src/external/reanimated/useVideo.ts @@ -1,5 +1,11 @@ -import type { SharedValue, FrameInfo } from "react-native-reanimated"; -import { useEffect, useMemo } from "react"; +import { + type SharedValue, + type FrameInfo, + createWorkletRuntime, + runOnJS, + runOnRuntime, +} from "react-native-reanimated"; +import { useEffect, useMemo, useState } from "react"; import { Skia } from "../../skia/Skia"; import type { SkImage, Video } from "../../skia/types"; @@ -53,11 +59,30 @@ const disposeVideo = (video: Video | null) => { video?.dispose(); }; +const runtime = createWorkletRuntime("video-metadata-runtime"); + +type VideoSource = string | null; + +const useVideoLoading = (source: VideoSource) => { + const [video, setVideo] = useState