From 81654ceec9a685974ff662d2ef853c1cc1c7b2ed Mon Sep 17 00:00:00 2001 From: William Candillon Date: Thu, 13 Jun 2024 09:47:13 +0200 Subject: [PATCH] =?UTF-8?q?fix(=F0=9F=93=B9):=20Video=20support=20on=20Web?= =?UTF-8?q?=20(#2477)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/docs/video.md | 4 +- package/src/external/reanimated/useVideo.ts | 41 ++------ .../external/reanimated/useVideoLoading.ts | 28 ++++++ .../reanimated/useVideoLoading.web.ts | 17 ++++ .../src/renderer/__tests__/e2e/Video.spec.tsx | 5 +- .../types/NativeBuffer/NativeBufferFactory.ts | 12 ++- package/src/skia/types/Skia.ts | 2 +- .../src/skia/web/CanvasKitWebGLBufferImpl.ts | 22 +++++ package/src/skia/web/JsiSkImageFactory.ts | 19 +++- package/src/skia/web/JsiSkia.ts | 5 +- package/src/skia/web/JsiVideo.ts | 96 +++++++++++++++++++ 11 files changed, 208 insertions(+), 43 deletions(-) create mode 100644 package/src/external/reanimated/useVideoLoading.ts create mode 100644 package/src/external/reanimated/useVideoLoading.web.ts create mode 100644 package/src/skia/web/CanvasKitWebGLBufferImpl.ts create mode 100644 package/src/skia/web/JsiVideo.ts diff --git a/docs/docs/video.md b/docs/docs/video.md index 670ed29118..213efe4efd 100644 --- a/docs/docs/video.md +++ b/docs/docs/video.md @@ -5,7 +5,9 @@ sidebar_label: Video slug: /video --- -React Native Skia provides a way to load video frames as images, enabling rich multimedia experiences within your applications. A video frame can be used anywhere a Skia image is accepted: `Image`, `ImageShader`, and `Atlas`. +React Native Skia provides a way to load video frames as images, enabling rich multimedia experiences within your applications. +A video frame can be used anywhere a Skia image is accepted: `Image`, `ImageShader`, and `Atlas`. +Videos are also supported on Web. ### Requirements diff --git a/package/src/external/reanimated/useVideo.ts b/package/src/external/reanimated/useVideo.ts index d7dbd4d961..9ec1f29084 100644 --- a/package/src/external/reanimated/useVideo.ts +++ b/package/src/external/reanimated/useVideo.ts @@ -1,17 +1,11 @@ -import { - type SharedValue, - type FrameInfo, - createWorkletRuntime, - runOnJS, - runOnRuntime, -} from "react-native-reanimated"; -import { useEffect, useMemo, useState } from "react"; +import type { SharedValue, FrameInfo } from "react-native-reanimated"; +import { useEffect, useMemo } from "react"; -import { Skia } from "../../skia/Skia"; import type { SkImage, Video } from "../../skia/types"; import { Platform } from "../../Platform"; import Rea from "./ReanimatedProxy"; +import { useVideoLoading } from "./useVideoLoading"; type Animated = SharedValue | T; @@ -28,7 +22,6 @@ const copyFrameOnAndroid = (currentFrame: SharedValue) => { if (Platform.OS === "android") { const tex = currentFrame.value; if (tex) { - console.log("Copying frame on Android"); currentFrame.value = tex.makeNonTextureImage(); tex.dispose(); } @@ -70,25 +63,6 @@ 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