xonecas.io
4828
Sr

stream-together / commit daa5c89

Adds bluring if background feature

Modified the scripts to include the requird files for AI Noise canceling
as well.

sean2024-06-28 daa5c899a0c9b9949890c5ebcf1256a43a9a4457 parent b87bab2

7 files changed

components/agora-host-video.tsx+70−49
@@ -20,6 +20,7 @@ import { IoVideocamOutline as VideoOnIcon } from "react-icons/io5";
2020 import { IoVideocamOffOutline as VideoOffIcon } from "react-icons/io5";
2121
2222 import useNoiseReduction from "@/hooks/agora-ai-noise-reduction";
23+import useVirtualBackground from "@/hooks/agora-virtual-background";
2324
2425 import { Modal } from "./modal";
2526 import PrimaryButton from "./primary-button";
@@ -78,6 +79,19 @@ export default function AgoraHost() {
7879 setVideoStarted(!videoStarted);
7980 }, [videoStarted]);
8081
82+ const virtualBackgroundRef = useVirtualBackground({ localCameraTrack });
83+
84+ const handleBlurBackground = useCallback(() => {
85+ const enableBlur = async () => {
86+ virtualBackgroundRef.current?.setOptions({
87+ type: "blur",
88+ blurDegree: 2,
89+ });
90+ virtualBackgroundRef.current?.enable();
91+ };
92+ void enableBlur();
93+ }, [virtualBackgroundRef]);
94+
8195 useJoin(
8296 {
8397 appid: appId,
@@ -146,62 +160,69 @@ export default function AgoraHost() {
146160 </div>
147161 </PrimaryButton>
148162 </button>
149- <Modal
150- openButton={
151- <button
152- onClick={() => setShowSettingsModal(!showSettingsModal)}
153- >
154- <SecondaryButton>
155- <div className="py-2 px-4 flex flex-row gap-2 items-center">
156- <GearIcon size="2em" />
157- <p>Settings</p>
158- </div>
159- </SecondaryButton>
160- </button>
161- }
162- showModal={showSettingsModal}
163- >
164- <div className="p-4 bg-stone-800 rounded-2xl flex flex-col gap-4">
165- <div>
166- <h4 className="font-bold pb-4">
167- Select your video device:
168- </h4>
169- {availableCameras.map((c) => {
170- return (
171- <label key={c.id} className="flex flex-row gap-2">
172- <input
173- type="radio"
174- name="selected-camera"
175- value={c.id}
176- checked={c.id === selectedCameraId}
177- onChange={(e) => {
178- const id = e.target.value;
179- setSelectedCameraId(id);
180- }}
181- />
182- <div>{c.label}</div>
183- </label>
184- );
185- })}
186- </div>
187-
188- <div className="text-center">
189- <button onClick={() => setShowSettingsModal(false)}>
190- <PrimaryButton>
191- <p className="px-4 py-2">Close</p>
192- </PrimaryButton>
193- </button>
194- </div>
195- </div>
196- </Modal>
197163 </div>
198164 )}
199165 </div>
200166 </LocalUser>
201167 </div>
202168
169+ <Modal
170+ openButton={
171+ <button onClick={() => setShowSettingsModal(!showSettingsModal)}>
172+ <SecondaryButton>
173+ <div className="py-2 px-4 flex flex-row gap-2 items-center">
174+ <GearIcon size="2em" />
175+ <p>Settings</p>
176+ </div>
177+ </SecondaryButton>
178+ </button>
179+ }
180+ showModal={showSettingsModal}
181+ >
182+ <div className="p-4 bg-stone-800 rounded-2xl flex flex-col gap-4">
183+ <div>
184+ <h4 className="font-bold pb-4">Select your video device:</h4>
185+ {availableCameras.map((c) => {
186+ return (
187+ <label key={c.id} className="flex flex-row gap-2">
188+ <input
189+ type="radio"
190+ name="selected-camera"
191+ value={c.id}
192+ checked={c.id === selectedCameraId}
193+ onChange={(e) => {
194+ const id = e.target.value;
195+ setSelectedCameraId(id);
196+ }}
197+ />
198+ <div>{c.label}</div>
199+ </label>
200+ );
201+ })}
202+ </div>
203+
204+ <div>
205+ <h4 className="font-bold pb-4">Blur your background?</h4>
206+ <div className="flex flex-row justify-center">
207+ <button onClick={handleBlurBackground}>
208+ <SecondaryButton>
209+ <p className="px-4 py-2">Yes</p>
210+ </SecondaryButton>
211+ </button>
212+ </div>
213+ </div>
214+
215+ <div className="text-center">
216+ <button onClick={() => setShowSettingsModal(false)}>
217+ <PrimaryButton>
218+ <p className="px-4 py-2">Close</p>
219+ </PrimaryButton>
220+ </button>
221+ </div>
222+ </div>
223+ </Modal>
224+
203225 {otherHosts.map((host) => {
204- console.log(host);
205226 return (
206227 <div
207228 key={host.uid}
hooks/agora-ai-noise-reduction.ts+1−0
@@ -22,6 +22,7 @@ export default function useNoiseReduction({
2222 useEffect(() => {
2323 const initializeExtension = async () => {
2424 AgoraRTC.registerExtensions([extension.current]);
25+
2526 if (!extension.current.checkCompatibility()) {
2627 console.error("Your browser does not support AI Denoiser!");
2728 return;
hooks/agora-virtual-background.ts+52−0
@@ -0,0 +1,52 @@
1+"use client";
2+
3+import VirtualBackgroundExtension, {
4+ IVirtualBackgroundProcessor,
5+} from "agora-extension-virtual-background";
6+import AgoraRTC, { ICameraVideoTrack } from "agora-rtc-react";
7+import { useEffect, useRef } from "react";
8+
9+export default function useVirtualBackground({
10+ localCameraTrack,
11+}: {
12+ localCameraTrack: ICameraVideoTrack | null;
13+}) {
14+ const extension = useRef(new VirtualBackgroundExtension());
15+ const processor = useRef<IVirtualBackgroundProcessor>();
16+
17+ useEffect(() => {
18+ const initializeExtension = async () => {
19+ AgoraRTC.registerExtensions([extension.current]);
20+
21+ if (!extension.current.checkCompatibility()) {
22+ console.error("Your browser does not support Virtual background!");
23+ return;
24+ }
25+
26+ if (localCameraTrack) {
27+ try {
28+ processor.current = extension.current.createProcessor();
29+ await processor.current.init();
30+ localCameraTrack
31+ .pipe(processor.current)
32+ .pipe(localCameraTrack.processorDestination);
33+ } catch (error) {
34+ console.error(error);
35+ }
36+ }
37+ };
38+
39+ void initializeExtension();
40+
41+ return () => {
42+ const disableExtension = async () => {
43+ processor.current?.unpipe();
44+ localCameraTrack?.unpipe();
45+ await processor.current?.disable();
46+ };
47+ void disableExtension();
48+ };
49+ }, [localCameraTrack]);
50+
51+ return processor;
52+}
package-lock.json+9−0
@@ -9,6 +9,7 @@
99 "version": "0.1.0",
1010 "dependencies": {
1111 "agora-extension-ai-denoiser": "^1.1.0",
12+ "agora-extension-virtual-background": "^1.2.0",
1213 "agora-rtc-react": "^2.2.0",
1314 "agora-rtc-sdk-ng": "^4.21.0",
1415 "next": "14.2.4",
@@ -675,6 +676,14 @@
675676 "agora-rtc-sdk-ng": ">=4.15.0"
676677 }
677678 },
679+ "node_modules/agora-extension-virtual-background": {
680+ "version": "1.2.0",
681+ "resolved": "https://registry.npmjs.org/agora-extension-virtual-background/-/agora-extension-virtual-background-1.2.0.tgz",
682+ "integrity": "sha512-VbnEY/WDMS1ZKiN84hgiYuGp1YOCj5wdT3oOp95THQlCAG0sKHwcijnaFBTuCTW+CAWPgU0BckxYGh+T3UxVag==",
683+ "peerDependencies": {
684+ "agora-rtc-sdk-ng": ">=4.15.0"
685+ }
686+ },
678687 "node_modules/agora-rtc-react": {
679688 "version": "2.2.0",
680689 "resolved": "https://registry.npmjs.org/agora-rtc-react/-/agora-rtc-react-2.2.0.tgz",
package.json+1−0
@@ -10,6 +10,7 @@
1010 },
1111 "dependencies": {
1212 "agora-extension-ai-denoiser": "^1.1.0",
13+ "agora-extension-virtual-background": "^1.2.0",
1314 "agora-rtc-react": "^2.2.0",
1415 "agora-rtc-sdk-ng": "^4.21.0",
1516 "next": "14.2.4",
public/external/external/denoiser-wasm-simd.wasm+0−0
Binary file, not shown.
public/external/external/denoiser-wasm.wasm+0−0
Binary file, not shown.