xonecas.io
4828
Sr

stream-together / commit 085ea02

Refactored the logic around switching devices

Camera and video tracks are now only created once
Refactored Virtual Background hook to also create the toggle handler

sean2024-06-29 085ea02890485d064676263fd3e9dbf2d5af06ce parent bb4f3cf

6 files changed

components/agora-host.tsx+27−4
@@ -12,6 +12,7 @@ import { useCallback, useState } from "react";
1212 import { ImSpinner10 } from "react-icons/im";
1313 import { IoEye } from "react-icons/io5";
1414
15+import useVirtualBackground from "@/hooks/use-agora-virtual-background";
1516 import useCamera from "@/hooks/use-camera";
1617 import useMicrophone from "@/hooks/use-microphone";
1718 import { appId, channel, HOST, OWNER, token } from "@/lib/config";
@@ -32,8 +33,18 @@ export default function AgoraHost({
3233 const [callStarted, setCallStarted] = useState(false);
3334 const [videoStarted, setVideoStarted] = useState(false);
3435 const [audioStarted, setAudioStarted] = useState(false);
35- const { localCameraTrack } = useCamera(videoStarted);
36- const { localMicrophoneTrack } = useMicrophone(audioStarted);
36+ const {
37+ localCameraTrack,
38+ availableCameras,
39+ selectedCameraId,
40+ setSelectedCameraId,
41+ } = useCamera(videoStarted);
42+ const {
43+ localMicrophoneTrack,
44+ availableMicrophones,
45+ selectedMicrophoneId,
46+ setSelectedMicrophoneId,
47+ } = useMicrophone(audioStarted);
3748 const isConnected = useIsConnected();
3849 const remoteUsers = useRemoteUsers();
3950 const otherHosts = remoteUsers.filter((u) => u.uid === HOST);
@@ -59,6 +70,10 @@ export default function AgoraHost({
5970 setVideoStarted(!videoStarted);
6071 }, [videoStarted]);
6172
73+ const { backgroundBlurred, handleBlurBackground } = useVirtualBackground({
74+ localCameraTrack,
75+ });
76+
6277 return (
6378 <div className="w-full flex flex-col justify-center items-center gap-4">
6479 <div className="flex flex-col md:flex-row gap-8 w-full justify-center items-center">
@@ -84,8 +99,16 @@ export default function AgoraHost({
8499 >
85100 <div className="p-4 bg-stone-800 rounded-2xl flex flex-col gap-4 text-left">
86101 <StreamSettings
87- audioStarted={audioStarted}
88- videoStarted={videoStarted}
102+ {...{
103+ availableCameras,
104+ selectedCameraId,
105+ setSelectedCameraId,
106+ availableMicrophones,
107+ selectedMicrophoneId,
108+ setSelectedMicrophoneId,
109+ backgroundBlurred,
110+ handleBlurBackground,
111+ }}
89112 />
90113
91114 <div className="text-center">
components/stream-settings.tsx+30−40
@@ -1,47 +1,39 @@
1+import {
2+ ICameraVideoTrack,
3+ ILocalVideoTrack,
4+ IMicrophoneAudioTrack,
5+} from "agora-rtc-react";
16 import { useCallback, useState } from "react";
27
3-import useVirtualBackground from "@/hooks/agora-virtual-background";
8+import useVirtualBackground from "@/hooks/use-agora-virtual-background";
49 import useCamera from "@/hooks/use-camera";
510 import useMicrophone from "@/hooks/use-microphone";
611
712 export default function StreamSettings({
8- audioStarted,
9- videoStarted,
13+ availableCameras,
14+ selectedCameraId,
15+ setSelectedCameraId,
16+ availableMicrophones,
17+ selectedMicrophoneId,
18+ setSelectedMicrophoneId,
19+ backgroundBlurred,
20+ handleBlurBackground,
1021 }: Readonly<{
11- audioStarted: boolean;
12- videoStarted: boolean;
22+ availableCameras: {
23+ id: string;
24+ label: string;
25+ }[];
26+ selectedCameraId: string;
27+ setSelectedCameraId: (arg0: string) => void;
28+ availableMicrophones: {
29+ id: string;
30+ label: string;
31+ }[];
32+ selectedMicrophoneId: string;
33+ setSelectedMicrophoneId: (arg0: string) => void;
34+ backgroundBlurred: boolean;
35+ handleBlurBackground: () => void;
1336 }>) {
14- const {
15- localCameraTrack,
16- availableCameras,
17- selectedCameraId,
18- setSelectedCameraId,
19- } = useCamera(videoStarted);
20-
21- const {
22- availableMicrophones,
23- selectedMicrophoneId,
24- setSelectedMicrophoneId,
25- } = useMicrophone(audioStarted);
26-
27- const virtualBackgroundRef = useVirtualBackground({ localCameraTrack });
28- const [backgroundBlurred, setBackgroundBlurred] = useState(false);
29-
30- const handleBlurBackground = useCallback(() => {
31- const enableBlur = async () => {
32- if (backgroundBlurred) {
33- virtualBackgroundRef.current?.disable();
34- } else {
35- virtualBackgroundRef.current?.setOptions({
36- type: "blur",
37- blurDegree: 2,
38- });
39- virtualBackgroundRef.current?.enable();
40- }
41- };
42- void enableBlur();
43- }, [virtualBackgroundRef, backgroundBlurred]);
44-
4537 return (
4638 <>
4739 <div>
@@ -68,10 +60,8 @@ export default function StreamSettings({
6860 <label className="flex flex-row gap-2 pt-2">
6961 <input
7062 type="checkbox"
71- onChange={() => {
72- void handleBlurBackground();
73- setBackgroundBlurred(!backgroundBlurred);
74- }}
63+ defaultChecked={backgroundBlurred}
64+ onClick={handleBlurBackground}
7565 />
7666 <p>Blur your background?</p>
7767 </label>
hooks/use-agora-ai-noise-reduction.ts+0−0
hooks/use-agora-virtual-background.ts+21−3
@@ -4,7 +4,7 @@ import VirtualBackgroundExtension, {
44 IVirtualBackgroundProcessor,
55 } from "agora-extension-virtual-background";
66 import AgoraRTC, { ICameraVideoTrack } from "agora-rtc-react";
7-import { useEffect, useRef } from "react";
7+import { useCallback, useEffect, useRef, useState } from "react";
88
99 export default function useVirtualBackground({
1010 localCameraTrack,
@@ -13,6 +13,7 @@ export default function useVirtualBackground({
1313 }) {
1414 const extension = useRef(new VirtualBackgroundExtension());
1515 const processor = useRef<IVirtualBackgroundProcessor>();
16+ const [backgroundBlurred, setBackgroundBlurred] = useState(false);
1617
1718 useEffect(() => {
1819 const initializeExtension = async () => {
@@ -23,7 +24,7 @@ export default function useVirtualBackground({
2324 return;
2425 }
2526
26- if (localCameraTrack) {
27+ if (localCameraTrack && !processor.current) {
2728 try {
2829 processor.current = extension.current.createProcessor();
2930 await processor.current.init();
@@ -48,5 +49,22 @@ export default function useVirtualBackground({
4849 };
4950 }, [localCameraTrack]);
5051
51- return processor;
52+ const handleBlurBackground = useCallback(() => {
53+ const enableBlur = async () => {
54+ if (processor.current?.enabled) {
55+ processor.current?.disable();
56+ setBackgroundBlurred(false);
57+ } else {
58+ processor.current?.setOptions({
59+ type: "blur",
60+ blurDegree: 2,
61+ });
62+ processor.current?.enable();
63+ setBackgroundBlurred(true);
64+ }
65+ };
66+ void enableBlur();
67+ }, [processor]);
68+
69+ return { backgroundBlurred, handleBlurBackground };
5270 }
hooks/use-camera.ts+4−0
@@ -22,6 +22,10 @@ export default function useCamera(videoStarted: boolean) {
2222 cameraId: selectedCameraId,
2323 });
2424
25+ useEffect(() => {
26+ localCameraTrack?.setDevice(selectedCameraId);
27+ }, [localCameraTrack, selectedCameraId]);
28+
2529 return {
2630 localCameraTrack,
2731 availableCameras,
hooks/use-microphone.ts+13−7
@@ -1,7 +1,7 @@
11 import AgoraRTC, { useLocalMicrophoneTrack } from "agora-rtc-react";
22 import { useEffect, useState } from "react";
33
4-import useNoiseReduction from "./agora-ai-noise-reduction";
4+import useNoiseReduction from "./use-agora-ai-noise-reduction";
55
66 export default function useMicrophone(audioStarted: boolean) {
77 const [availableMicrophones, setAvailableMicrophones] = useState<
@@ -11,19 +11,25 @@ export default function useMicrophone(audioStarted: boolean) {
1111 const [selectedMicrophoneId, setSelectedMicrophoneId] = useState("");
1212
1313 useEffect(() => {
14- const getCameras = async () => {
15- const cameras = await AgoraRTC.getCameras();
14+ const getMicrophones = async () => {
15+ const microphones = await AgoraRTC.getCameras();
1616 setAvailableMicrophones(
17- cameras.map((c) => ({ id: c.deviceId, label: c.label })),
17+ microphones.map((c) => ({ id: c.deviceId, label: c.label })),
1818 );
19- setSelectedMicrophoneId(cameras[0].deviceId);
19+ setSelectedMicrophoneId(microphones[0].deviceId);
2020 };
21- void getCameras();
21+ void getMicrophones();
2222 }, []);
2323
24- const { localMicrophoneTrack } = useLocalMicrophoneTrack(audioStarted);
24+ const { localMicrophoneTrack } = useLocalMicrophoneTrack(audioStarted, {
25+ microphoneId: selectedMicrophoneId,
26+ });
2527 useNoiseReduction({ localMicrophoneTrack });
2628
29+ useEffect(() => {
30+ localMicrophoneTrack?.setDevice(selectedMicrophoneId);
31+ }, [localMicrophoneTrack, selectedMicrophoneId]);
32+
2733 return {
2834 localMicrophoneTrack,
2935 availableMicrophones,