Skip to content

Commit 8b25503

Browse files
authored
Add @livekit/track-processors example (#951)
1 parent cb45f42 commit 8b25503

File tree

4 files changed

+116
-0
lines changed

4 files changed

+116
-0
lines changed

examples/nextjs/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"dependencies": {
1212
"@livekit/components-react": "workspace:*",
1313
"@livekit/components-styles": "workspace:*",
14+
"@livekit/track-processors": "^0.3.2",
1415
"livekit-client": "^2.4.0",
1516
"livekit-server-sdk": "^1.2.7",
1617
"next": "^12.3.4",

examples/nextjs/pages/index.tsx

+4
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ const EXAMPLE_ROUTES = {
1717
title: 'Clubhouse clone build with LiveKit components',
1818
href: () => `/clubhouse`,
1919
},
20+
processors: {
21+
title: 'Example usage of @livekit/track-processors for background blur',
22+
href: () => `/processors`,
23+
},
2024
} as const;
2125

2226
const Home: NextPage = () => {

examples/nextjs/pages/processors.tsx

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import * as React from 'react';
2+
import { setLogLevel } from '@livekit/components-core';
3+
import {
4+
GridLayout,
5+
LiveKitRoom,
6+
ParticipantTile,
7+
TrackRefContext,
8+
useLocalParticipant,
9+
useToken,
10+
useTracks,
11+
} from '@livekit/components-react';
12+
import type { NextPage } from 'next';
13+
import { ControlBarControls } from '@livekit/components-react';
14+
import { LocalVideoTrack, Track } from 'livekit-client';
15+
import { BackgroundBlur } from '@livekit/track-processors';
16+
17+
function Stage() {
18+
const cameraTracks = useTracks([Track.Source.Camera]);
19+
const screenShareTrackRef = useTracks([Track.Source.ScreenShare])[0];
20+
21+
const [blurEnabled, setBlurEnabled] = React.useState(false);
22+
const [processorPending, setProcessorPending] = React.useState(false);
23+
const { cameraTrack } = useLocalParticipant();
24+
25+
React.useEffect(() => {
26+
const localCamTrack = cameraTrack?.track as LocalVideoTrack | undefined;
27+
if (localCamTrack) {
28+
setProcessorPending(true);
29+
try {
30+
if (blurEnabled && !localCamTrack.getProcessor()) {
31+
localCamTrack.setProcessor(BackgroundBlur());
32+
} else if (!blurEnabled) {
33+
localCamTrack.stopProcessor();
34+
}
35+
} finally {
36+
setProcessorPending(false);
37+
}
38+
}
39+
}, [blurEnabled, cameraTrack]);
40+
41+
return (
42+
<>
43+
<button
44+
className="lk-button"
45+
disabled={processorPending}
46+
onClick={() => setBlurEnabled((enabled) => !enabled)}
47+
>
48+
Toggle Blur
49+
</button>
50+
{screenShareTrackRef && <ParticipantTile trackRef={screenShareTrackRef} />}
51+
<GridLayout tracks={cameraTracks}>
52+
<TrackRefContext.Consumer>
53+
{(trackRef) => <ParticipantTile trackRef={trackRef} />}
54+
</TrackRefContext.Consumer>
55+
</GridLayout>
56+
</>
57+
);
58+
}
59+
60+
const ProcessorsExample: NextPage = () => {
61+
const params = typeof window !== 'undefined' ? new URLSearchParams(location.search) : null;
62+
const roomName = params?.get('room') ?? 'test-room';
63+
const userIdentity = params?.get('user') ?? 'test-identity';
64+
65+
const token = useToken(process.env.NEXT_PUBLIC_LK_TOKEN_ENDPOINT, roomName, {
66+
userInfo: {
67+
identity: userIdentity,
68+
name: userIdentity,
69+
},
70+
});
71+
72+
return (
73+
<div data-lk-theme="default" style={{ height: '100vh' }}>
74+
<LiveKitRoom
75+
video={true}
76+
audio={false}
77+
token={token}
78+
connect={true}
79+
serverUrl={process.env.NEXT_PUBLIC_LK_SERVER_URL}
80+
>
81+
<Stage />
82+
</LiveKitRoom>
83+
</div>
84+
);
85+
};
86+
87+
export default ProcessorsExample;

pnpm-lock.yaml

+24
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)