-
Notifications
You must be signed in to change notification settings - Fork 87
/
Copy pathprocessors.tsx
89 lines (80 loc) · 2.5 KB
/
processors.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
'use client';
import * as React from 'react';
import { setLogLevel } from '@livekit/components-core';
import {
GridLayout,
LiveKitRoom,
ParticipantTile,
TrackRefContext,
useLocalParticipant,
useToken,
useTracks,
} from '@livekit/components-react';
import type { NextPage } from 'next';
import { ControlBarControls } from '@livekit/components-react';
import { LocalVideoTrack, Track } from 'livekit-client';
import { BackgroundBlur } from '@livekit/track-processors';
function Stage() {
const cameraTracks = useTracks([Track.Source.Camera]);
const screenShareTrackRef = useTracks([Track.Source.ScreenShare])[0];
const [blurEnabled, setBlurEnabled] = React.useState(false);
const [processorPending, setProcessorPending] = React.useState(false);
const { cameraTrack } = useLocalParticipant();
React.useEffect(() => {
const localCamTrack = cameraTrack?.track as LocalVideoTrack | undefined;
if (localCamTrack) {
setProcessorPending(true);
try {
if (blurEnabled && !localCamTrack.getProcessor()) {
localCamTrack.setProcessor(BackgroundBlur());
} else if (!blurEnabled) {
localCamTrack.stopProcessor();
}
} finally {
setProcessorPending(false);
}
}
}, [blurEnabled, cameraTrack]);
return (
<>
<button
className="lk-button"
disabled={processorPending}
onClick={() => setBlurEnabled((enabled) => !enabled)}
>
Toggle Blur
</button>
{screenShareTrackRef && <ParticipantTile trackRef={screenShareTrackRef} />}
<GridLayout tracks={cameraTracks}>
<TrackRefContext.Consumer>
{(trackRef) => <ParticipantTile trackRef={trackRef} />}
</TrackRefContext.Consumer>
</GridLayout>
</>
);
}
const ProcessorsExample: NextPage = () => {
const params = typeof window !== 'undefined' ? new URLSearchParams(location.search) : null;
const roomName = params?.get('room') ?? 'test-room';
const userIdentity = params?.get('user') ?? 'test-identity';
const token = useToken(process.env.NEXT_PUBLIC_LK_TOKEN_ENDPOINT, roomName, {
userInfo: {
identity: userIdentity,
name: userIdentity,
},
});
return (
<div data-lk-theme="default" style={{ height: '100vh' }}>
<LiveKitRoom
video={true}
audio={false}
token={token}
connect={true}
serverUrl={process.env.NEXT_PUBLIC_LK_SERVER_URL}
>
<Stage />
</LiveKitRoom>
</div>
);
};
export default ProcessorsExample;