-
Notifications
You must be signed in to change notification settings - Fork 459
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(📹): Video support on Web (#2477)
- Loading branch information
1 parent
524072c
commit 81654ce
Showing
11 changed files
with
208 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { useEffect, useState } from "react"; | ||
import { | ||
createWorkletRuntime, | ||
runOnJS, | ||
runOnRuntime, | ||
} from "react-native-reanimated"; | ||
|
||
import type { Video } from "../../skia/types"; | ||
import { Skia } from "../../skia"; | ||
|
||
const runtime = createWorkletRuntime("video-metadata-runtime"); | ||
|
||
type VideoSource = string | null; | ||
|
||
export const useVideoLoading = (source: VideoSource) => { | ||
const [video, setVideo] = useState<Video | null>(null); | ||
const cb = (src: string) => { | ||
"worklet"; | ||
const vid = Skia.Video(src) as Video; | ||
runOnJS(setVideo)(vid); | ||
}; | ||
useEffect(() => { | ||
if (source) { | ||
runOnRuntime(runtime, cb)(source); | ||
} | ||
}, [source]); | ||
return video; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { useEffect, useState } from "react"; | ||
|
||
import type { Video } from "../../skia/types"; | ||
import { Skia } from "../../skia"; | ||
|
||
type VideoSource = string | null; | ||
|
||
export const useVideoLoading = (source: VideoSource) => { | ||
const [video, setVideo] = useState<Video | null>(null); | ||
useEffect(() => { | ||
if (source) { | ||
const vid = Skia.Video(source) as Promise<Video>; | ||
vid.then((v) => setVideo(v)); | ||
} | ||
}, [source]); | ||
return video; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import type { Surface, TextureSource, Image } from "canvaskit-wasm"; | ||
|
||
import { CanvasKitWebGLBuffer } from "../types"; | ||
|
||
export class CanvasKitWebGLBufferImpl extends CanvasKitWebGLBuffer { | ||
public image: Image | null = null; | ||
|
||
constructor(public surface: Surface, private source: TextureSource) { | ||
super(); | ||
} | ||
|
||
toImage() { | ||
if (this.image === null) { | ||
this.image = this.surface.makeImageFromTextureSource(this.source); | ||
} | ||
if (this.image === null) { | ||
throw new Error("Failed to create image from texture source"); | ||
} | ||
this.surface.updateTextureFromSource(this.image, this.source); | ||
return this.image; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import type { CanvasKit, Surface } from "canvaskit-wasm"; | ||
|
||
import type { CanvasKitWebGLBuffer, Video, ImageFactory } from "../types"; | ||
|
||
import { CanvasKitWebGLBufferImpl } from "./CanvasKitWebGLBufferImpl"; | ||
import { JsiSkImageFactory } from "./JsiSkImageFactory"; | ||
|
||
export const createVideo = async ( | ||
CanvasKit: CanvasKit, | ||
url: string | ||
): Promise<Video> => { | ||
const video = document.createElement("video"); | ||
return new Promise((resolve, reject) => { | ||
video.src = url; | ||
video.style.display = "none"; | ||
video.crossOrigin = "anonymous"; | ||
video.volume = 0; | ||
video.addEventListener("loadedmetadata", () => { | ||
document.body.appendChild(video); | ||
resolve(new JsiVideo(new JsiSkImageFactory(CanvasKit), video)); | ||
}); | ||
video.addEventListener("error", () => { | ||
reject(new Error(`Failed to load video from URL: ${url}`)); | ||
}); | ||
}); | ||
}; | ||
|
||
export class JsiVideo implements Video { | ||
__typename__ = "Video" as const; | ||
|
||
private webglBuffer: CanvasKitWebGLBuffer | null = null; | ||
|
||
constructor( | ||
private ImageFactory: ImageFactory, | ||
private videoElement: HTMLVideoElement | ||
) { | ||
document.body.appendChild(this.videoElement); | ||
} | ||
|
||
duration() { | ||
return this.videoElement.duration * 1000; | ||
} | ||
|
||
framerate(): number { | ||
throw new Error("Video.frame is not available on React Native Web"); | ||
} | ||
|
||
setSurface(surface: Surface) { | ||
// If we have the surface, we can use the WebGL buffer which is slightly faster | ||
// This is because WebGL cannot be shared across contextes. | ||
// This can be removed with WebGPU | ||
this.webglBuffer = new CanvasKitWebGLBufferImpl(surface, this.videoElement); | ||
} | ||
|
||
nextImage() { | ||
return this.ImageFactory.MakeImageFromNativeBuffer( | ||
this.webglBuffer ? this.webglBuffer : this.videoElement | ||
); | ||
} | ||
|
||
seek(time: number) { | ||
if (isNaN(time)) { | ||
throw new Error(`Invalid time: ${time}`); | ||
} | ||
this.videoElement.currentTime = time / 1000; | ||
} | ||
|
||
rotation() { | ||
return 0 as const; | ||
} | ||
|
||
size() { | ||
return { | ||
width: this.videoElement.videoWidth, | ||
height: this.videoElement.videoHeight, | ||
}; | ||
} | ||
|
||
pause() { | ||
this.videoElement.pause(); | ||
} | ||
|
||
play() { | ||
this.videoElement.play(); | ||
} | ||
|
||
setVolume(volume: number) { | ||
this.videoElement.volume = volume; | ||
} | ||
|
||
dispose() { | ||
if (this.videoElement.parentNode) { | ||
this.videoElement.parentNode.removeChild(this.videoElement); | ||
} | ||
} | ||
} |