Skip to content

Create polished demo videos without editing skills. Mac/Windows/Linux

Avoid RAF webcam redraw loop with video frame callbacks

wizardAEI1c2df999688c
diff --git a/src/components/video-editor/VideoPlayback.tsx b/src/components/video-editor/VideoPlayback.tsx
index ddca6db8..38ba7bce 100644
--- a/src/components/video-editor/VideoPlayback.tsx
+++ b/src/components/video-editor/VideoPlayback.tsx
@@ -1453,6 +1453,9 @@ const VideoPlayback = forwardRef<VideoPlaybackRef, VideoPlaybackProps>(
if (!hasVideoFrameCallback) {
return;
}
+ if (videoFrameCallback !== null) {
+ return;
+ }
videoFrameCallback = webcamVideo.requestVideoFrameCallback(() => {
videoFrameCallback = null;
drawWebcamFrame(true);
@@ -1461,12 +1464,21 @@ const VideoPlayback = forwardRef<VideoPlaybackRef, VideoPlaybackProps>(
};
const drawOnAnimationFrame = () => {
- drawWebcamFrame(!hasVideoFrameCallback);
+ drawWebcamFrame(true);
animationFrame = requestAnimationFrame(drawOnAnimationFrame);
};
const handleDrawableMediaEvent = () => {
syncWebcamMedia();
+ if (hasVideoFrameCallback) {
+ if (animationFrame !== null) {
+ cancelAnimationFrame(animationFrame);
+ animationFrame = null;
+ }
+ drawWebcamFrame(true);
+ queueVideoFrameCallback();
+ return;
+ }
if (animationFrame !== null) {
cancelAnimationFrame(animationFrame);
}
@@ -1476,8 +1488,12 @@ const VideoPlayback = forwardRef<VideoPlaybackRef, VideoPlaybackProps>(
webcamVideo.addEventListener("loadeddata", handleDrawableMediaEvent);
webcamVideo.addEventListener("canplay", handleDrawableMediaEvent);
webcamVideo.addEventListener("seeked", handleDrawableMediaEvent);
- queueVideoFrameCallback();
- drawOnAnimationFrame();
+ if (hasVideoFrameCallback) {
+ drawWebcamFrame(true);
+ queueVideoFrameCallback();
+ } else {
+ drawOnAnimationFrame();
+ }
return () => {
if (animationFrame !== null) {