From 524072c2bc606091adb307870c71865ddca8d452 Mon Sep 17 00:00:00 2001 From: William Candillon Date: Wed, 12 Jun 2024 18:22:01 +0200 Subject: [PATCH] =?UTF-8?q?fix(=F0=9F=A4=96):=20remove=20unnecessary=20vid?= =?UTF-8?q?eo=20frame=20copies=20on=20Android=20(#2476)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- example/src/Examples/Video/Video.tsx | 2 +- package/src/external/reanimated/useVideo.ts | 22 ++++++++++++++++----- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/example/src/Examples/Video/Video.tsx b/example/src/Examples/Video/Video.tsx index da53bb435c..c8403ad243 100644 --- a/example/src/Examples/Video/Video.tsx +++ b/example/src/Examples/Video/Video.tsx @@ -16,7 +16,7 @@ import Slider from "@react-native-community/slider"; const videoURL = Platform.OS === "web" ? require("../../Tests/assets/BigBuckBunny.mp4").default - : "https://bit.ly/skia-video-short"; + : "https://bit.ly/skia-video"; export const Video = () => { const paused = useSharedValue(false); diff --git a/package/src/external/reanimated/useVideo.ts b/package/src/external/reanimated/useVideo.ts index 5969bf1ee6..d7dbd4d961 100644 --- a/package/src/external/reanimated/useVideo.ts +++ b/package/src/external/reanimated/useVideo.ts @@ -22,6 +22,19 @@ interface PlaybackOptions { volume: Animated; } +const copyFrameOnAndroid = (currentFrame: SharedValue) => { + "worklet"; + // on android we need to copy the texture before it's invalidated + if (Platform.OS === "android") { + const tex = currentFrame.value; + if (tex) { + console.log("Copying frame on Android"); + currentFrame.value = tex.makeNonTextureImage(); + tex.dispose(); + } + } +}; + const setFrame = (video: Video, currentFrame: SharedValue) => { "worklet"; const img = video.nextImage(); @@ -29,11 +42,9 @@ const setFrame = (video: Video, currentFrame: SharedValue) => { if (currentFrame.value) { currentFrame.value.dispose(); } - if (Platform.OS === "android") { - currentFrame.value = img.makeNonTextureImage(); - } else { - currentFrame.value = img; - } + currentFrame.value = img; + } else { + copyFrameOnAndroid(currentFrame); } }; @@ -111,6 +122,7 @@ export const useVideo = ( () => seek.value, (value) => { if (value !== null) { + copyFrameOnAndroid(currentFrame); video?.seek(value); currentTime.value = value; seek.value = null;