-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathSmelterVideo.tsx
49 lines (41 loc) · 1.26 KB
/
SmelterVideo.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import React, { useCallback } from 'react';
import type Smelter from '@swmansion/smelter-web-wasm';
type VideoProps = React.DetailedHTMLProps<
React.VideoHTMLAttributes<HTMLVideoElement>,
HTMLVideoElement
>;
type CompositorVideoProps = {
outputId: string;
onVideoCreated?: (smelter: Smelter) => Promise<void>;
smelter: Smelter;
children: React.ReactElement;
} & VideoProps;
export default function CompositorVideo(props: CompositorVideoProps) {
const { outputId, onVideoCreated, children, smelter, ...videoProps } = props;
const videoRef = useCallback(
async (video: HTMLVideoElement | null) => {
if (!video) {
return;
}
if (onVideoCreated) {
await onVideoCreated(smelter);
}
const { stream } = await smelter.registerOutput(outputId, children, {
type: 'stream',
video: {
resolution: {
width: Number(videoProps.width ?? video.width),
height: Number(videoProps.height ?? video.height),
},
},
audio: true,
});
if (stream) {
video.srcObject = stream;
await video.play();
}
},
[onVideoCreated, videoProps.width, videoProps.height, smelter, outputId]
);
return <video ref={videoRef} {...videoProps} />;
}