Spaces:
Running
Running
Update src/components/control-tray/ControlTray.tsx
Browse files
src/components/control-tray/ControlTray.tsx
CHANGED
@@ -6,9 +6,6 @@ import cn from "classnames";
|
|
6 |
import React, { memo, ReactNode, RefObject, useEffect, useRef, useState } from "react";
|
7 |
import { useLiveAPIContext } from "../../contexts/LiveAPIContext";
|
8 |
import { AudioRecorder } from "../../lib/audio-recorder";
|
9 |
-
import { isIOS } from "../../lib/platform";
|
10 |
-
// import "./control-tray.scss"; // این فایل دیگر نباید استایلی داشته باشد یا باید حذف شود
|
11 |
-
|
12 |
// SVG Icons for Controls (همانطور که قبلا تعریف شد)
|
13 |
const SvgPauseIcon = () => <svg width="37" height="37" viewBox="0 0 37 37" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M15.9872 29.6198V8.28342C15.9872 6.25781 15.132 5.44757 12.9713 5.44757H7.52469C5.36404 5.44757 4.50879 6.25781 4.50879 8.28342V29.6198C4.50879 31.6454 5.36404 32.4556 7.52469 32.4556H12.9713C15.132 32.4556 15.9872 31.6454 15.9872 29.6198Z" fill="#BE123C"/><path opacity="0.4" d="M31.5175 29.6198V8.28342C31.5175 6.25781 30.6622 5.44757 28.5016 5.44757H23.055C20.9093 5.44757 20.0391 6.25781 20.0391 8.28342V29.6198C20.0391 31.6454 20.8943 32.4556 23.055 32.4556H28.5016C30.6622 32.4556 31.5175 31.6454 31.5175 29.6198Z" fill="#BE123C"/></svg>;
|
14 |
const SvgMicrophoneIcon = () => <svg width="38" height="38" viewBox="0 0 69 68" fill="none" xmlns="http://www.w3.org/2000/svg" transform="scale(0.55)"><path opacity="0.4" d="M49.9479 27.1824C49.0803 27.1824 48.3907 27.872 48.3907 28.7396V32.2544C48.3907 40.1293 41.984 46.5361 34.109 46.5361C26.234 46.5361 19.8273 40.1293 19.8273 32.2544V28.7173C19.8273 27.8497 19.1377 27.1601 18.2701 27.1601C17.4025 27.1601 16.7129 27.8497 16.7129 28.7173V32.2321C16.7129 41.2861 23.6758 48.7384 32.5518 49.5393V54.2776C32.5518 55.1452 33.2414 55.8348 34.109 55.8348C34.9766 55.8348 35.6662 55.1452 35.6662 54.2776V49.5393C44.52 48.7607 51.5051 41.2861 51.5051 32.2321V28.7173C51.4829 27.872 50.7933 27.1824 49.9479 27.1824Z" fill="#BE123C"/><path d="M34.1099 11.3434C28.682 11.3434 24.2773 15.7481 24.2773 21.176V32.5658C24.2773 37.9938 28.682 42.3984 34.1099 42.3984C39.5379 42.3984 43.9425 37.9938 43.9425 32.5658V21.176C43.9425 15.7481 39.5379 11.3434 34.1099 11.3434ZM37.0241 26.8042C36.8684 27.3826 36.3567 27.7608 35.7784 27.7608C35.6671 27.7608 35.5559 27.7385 35.4447 27.7163C34.5771 27.4716 33.665 27.4716 32.7974 27.7163C32.0856 27.9165 31.396 27.4938 31.218 26.8042C31.0178 26.1146 31.4404 25.4027 32.1301 25.2247C33.4426 24.8688 34.8218 24.8688 36.1343 25.2247C36.8017 25.4027 37.2021 26.1146 37.0241 26.8042ZM38.2031 22.4885C38.0029 23.0224 37.5135 23.3339 36.9796 23.3339C36.8239 23.3339 36.6904 23.3116 36.5347 23.2671C34.9775 22.6887 33.2423 22.6887 31.6852 23.2671C31.0178 23.5118 30.2614 23.1559 30.0167 22.4885C29.772 21.8212 30.128 21.0648 30.7953 20.8423C32.9309 20.0637 35.289 20.0637 37.4245 20.8423C38.0919 21.087 38.4478 21.8212 38.2031 22.4885Z" fill="#BE123C"/></svg>;
|
@@ -16,7 +13,6 @@ const SvgCameraIcon = () => <svg width="40" height="40" viewBox="0 0 40 40" fill
|
|
16 |
const SvgStopCamIcon = () => <svg width="40" height="40" viewBox="0 0 40 40" fill="none" xmlns="http://www.w3.org/2000/svg"><path opacity="0.4" d="M29.0785 10.8076L6.91966 32.9665C4.61316 31.5002 3.70703 28.8807 3.70703 26.36V13.18C3.70703 7.54555 5.89821 5.35437 11.5327 5.35437H21.4177C26.1789 5.35437 28.4854 6.9195 29.0785 10.8076Z" fill="#2252A0"/><path opacity="0.4" d="M29.2427 15.2394V26.36C29.2427 26.4918 29.2262 26.5907 29.2262 26.706C29.2262 26.8213 29.2097 26.9366 29.2097 27.052H29.2262C29.045 32.1757 26.8208 34.1856 21.417 34.1856H11.532C11.1202 34.1856 10.7412 34.1692 10.3623 34.1197L29.2427 15.2394Z" fill="#2252A0"/><path opacity="0.4" d="M29.21 27.052C29.21 26.9366 29.2264 26.8213 29.2264 26.706C29.2429 26.8213 29.2429 26.9366 29.2264 27.052H29.21Z" fill="#2252A0"/><path opacity="0.4" d="M29.2264 12.4716C29.2429 12.5869 29.2429 12.7187 29.2264 12.834C29.2264 12.7187 29.21 12.6034 29.21 12.488L29.2264 12.4716Z" fill="#2252A0"/><path d="M37.4804 13.8061V25.734C37.4804 28.0899 36.3436 29.029 35.6682 29.3749C35.3551 29.5397 34.8774 29.7209 34.2678 29.7209C33.5594 29.7209 32.6697 29.4903 31.6483 28.7654L29.2264 27.052H29.21C29.21 26.9366 29.2264 26.8213 29.2264 26.706C29.2264 26.5907 29.2429 26.4918 29.2429 26.36V15.2394L34.6302 9.85205C35.0751 9.885 35.421 10.0168 35.6682 10.1651C36.3436 10.5111 37.4804 11.4501 37.4804 13.8061Z" fill="#2252A0"/><path d="M35.8666 3.67393C35.3723 3.17968 34.565 3.17968 34.0708 3.67393L3.6744 34.0868C3.18015 34.581 3.18015 35.3883 3.6744 35.8826C3.92152 36.1132 4.23455 36.245 4.56405 36.245C4.89355 36.245 5.20657 36.1132 5.4537 35.8661L35.8666 5.45323C36.3773 4.95898 36.3773 4.16818 35.8666 3.67393Z" fill="#2252A0"/></svg>;
|
17 |
const SvgSwitchCameraIcon = () => <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"><path d="M11 19H4a2 2 0 0 1-2-2V7a2 2 0 0 1 2-2h5"/><path d="M13 5h7a2 2 0 0 1 2 2v10a2 2 0 0 1-2 2h-5"/><path d="m17 12-3-3 3-3"/><path d="m7 12 3 3-3 3"/></svg>;
|
18 |
|
19 |
-
|
20 |
export type ControlTrayProps = {
|
21 |
videoRef: RefObject<HTMLVideoElement>;
|
22 |
children?: ReactNode;
|
@@ -39,13 +35,10 @@ function ControlTray({
|
|
39 |
onAppCamToggle,
|
40 |
createLogoFunction,
|
41 |
}: ControlTrayProps) {
|
42 |
-
const { client, connected, connect, disconnect
|
43 |
-
= useLiveAPIContext();
|
44 |
-
|
45 |
const [audioRecorder] = useState(() => new AudioRecorder());
|
46 |
const [muted, setMuted] = useState(true);
|
47 |
const [inVolume, setInVolume] = useState(0);
|
48 |
-
|
49 |
const [activeLocalVideoStream, setActiveLocalVideoStream] = useState<MediaStream | null>(null);
|
50 |
const [currentFacingMode, setCurrentFacingMode] = useState<'user' | 'environment'>('user');
|
51 |
const [isSwitchingCamera, setIsSwitchingCamera] = useState(false);
|
@@ -55,13 +48,16 @@ function ControlTray({
|
|
55 |
}, [isAppMicActive]);
|
56 |
|
57 |
useEffect(() => {
|
|
|
58 |
if (isAppCamActive && !activeLocalVideoStream) {
|
|
|
59 |
startWebcam();
|
60 |
} else if (!isAppCamActive && activeLocalVideoStream) {
|
|
|
61 |
stopWebcam();
|
62 |
}
|
63 |
// eslint-disable-next-line react-hooks/exhaustive-deps
|
64 |
-
}, [isAppCamActive]);
|
65 |
|
66 |
useEffect(() => {
|
67 |
const onData = (base64: string) => {
|
@@ -71,12 +67,13 @@ function ControlTray({
|
|
71 |
};
|
72 |
if (connected && !muted && audioRecorder) {
|
73 |
audioRecorder.on("data", onData).on("volume", setInVolume).start();
|
74 |
-
} else if (audioRecorder) {
|
75 |
audioRecorder.stop();
|
76 |
}
|
77 |
return () => {
|
78 |
if (audioRecorder) {
|
79 |
-
audioRecorder.off("data", onData).off("volume", setInVolume)
|
|
|
80 |
}
|
81 |
};
|
82 |
}, [connected, client, muted, audioRecorder]);
|
@@ -91,7 +88,6 @@ function ControlTray({
|
|
91 |
const video = videoRef.current;
|
92 |
const canvas = renderCanvasRef.current;
|
93 |
if (!video || !canvas || video.readyState < video.HAVE_METADATA || video.paused || video.ended || !client) return;
|
94 |
-
|
95 |
try {
|
96 |
const ctx = canvas.getContext("2d");
|
97 |
if (!ctx) return;
|
@@ -108,7 +104,6 @@ function ControlTray({
|
|
108 |
console.error("❌ Error processing video frame:", error);
|
109 |
}
|
110 |
}
|
111 |
-
|
112 |
if (connected && activeLocalVideoStream && videoRef.current) {
|
113 |
setTimeout(sendVideoFrame, 200);
|
114 |
}
|
@@ -117,67 +112,93 @@ function ControlTray({
|
|
117 |
|
118 |
useEffect(() => {
|
119 |
if (videoRef.current) {
|
|
|
120 |
if (videoRef.current.srcObject !== activeLocalVideoStream) {
|
121 |
videoRef.current.srcObject = activeLocalVideoStream;
|
|
|
122 |
if (activeLocalVideoStream) {
|
123 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
124 |
}
|
125 |
}
|
|
|
|
|
126 |
}
|
127 |
}, [activeLocalVideoStream, videoRef]);
|
128 |
|
129 |
const ensureConnected = async () => {
|
|
|
130 |
if (!connected) {
|
131 |
try {
|
|
|
132 |
await connect();
|
|
|
|
|
133 |
} catch (err) {
|
134 |
-
console.error('❌ Connection error:', err);
|
135 |
return false;
|
136 |
}
|
137 |
}
|
|
|
138 |
return true;
|
139 |
};
|
140 |
|
141 |
const handleMicToggle = async () => {
|
|
|
142 |
if (isSwitchingCamera) return;
|
143 |
const newMicState = !isAppMicActive;
|
144 |
if (newMicState) {
|
145 |
if(!await ensureConnected()) {
|
|
|
146 |
onAppMicToggle(false);
|
147 |
return;
|
148 |
}
|
149 |
}
|
150 |
onAppMicToggle(newMicState);
|
151 |
-
|
152 |
-
// await disconnect();
|
153 |
-
}
|
154 |
};
|
155 |
|
156 |
const startWebcam = async (facingModeToTry: 'user' | 'environment' = currentFacingMode) => {
|
157 |
-
|
|
|
|
|
|
|
|
|
158 |
setIsSwitchingCamera(true);
|
159 |
|
160 |
if(!await ensureConnected()) {
|
161 |
-
|
|
|
162 |
setIsSwitchingCamera(false);
|
163 |
return;
|
164 |
}
|
|
|
165 |
|
166 |
try {
|
167 |
-
console.log(`🚀
|
168 |
const mediaStream = await navigator.mediaDevices.getUserMedia({
|
169 |
video: { facingMode: facingModeToTry },
|
170 |
audio: false,
|
171 |
});
|
|
|
172 |
setActiveLocalVideoStream(mediaStream);
|
173 |
onVideoStreamChange(mediaStream);
|
174 |
setCurrentFacingMode(facingModeToTry);
|
175 |
-
onAppCamToggle(true);
|
|
|
176 |
} catch (error) {
|
177 |
console.error(`❌ Error starting webcam with ${facingModeToTry}:`, error);
|
178 |
if (facingModeToTry === 'user' && (error as Error).name !== 'NotFoundError') {
|
179 |
try {
|
|
|
180 |
const fallbackStream = await navigator.mediaDevices.getUserMedia({ video: { facingMode: 'environment' }, audio: false });
|
|
|
181 |
setActiveLocalVideoStream(fallbackStream);
|
182 |
onVideoStreamChange(fallbackStream);
|
183 |
setCurrentFacingMode('environment');
|
@@ -195,45 +216,57 @@ function ControlTray({
|
|
195 |
}
|
196 |
} finally {
|
197 |
setIsSwitchingCamera(false);
|
|
|
198 |
}
|
199 |
};
|
200 |
|
201 |
const stopWebcam = () => {
|
|
|
202 |
if (activeLocalVideoStream) {
|
|
|
203 |
activeLocalVideoStream.getTracks().forEach(track => track.stop());
|
|
|
|
|
204 |
}
|
205 |
setActiveLocalVideoStream(null);
|
206 |
onVideoStreamChange(null);
|
207 |
onAppCamToggle(false);
|
208 |
-
|
209 |
-
// await disconnect();
|
210 |
-
}
|
211 |
};
|
212 |
|
213 |
const handleCamToggle = () => {
|
|
|
214 |
if (isAppCamActive) {
|
|
|
215 |
stopWebcam();
|
216 |
} else {
|
|
|
217 |
startWebcam();
|
218 |
}
|
219 |
};
|
220 |
|
221 |
const handleSwitchCamera = async () => {
|
222 |
-
|
|
|
|
|
|
|
|
|
223 |
|
224 |
setIsSwitchingCamera(true);
|
225 |
const targetFacingMode = currentFacingMode === 'user' ? 'environment' : 'user';
|
|
|
226 |
|
227 |
activeLocalVideoStream.getTracks().forEach(track => track.stop());
|
228 |
setActiveLocalVideoStream(null);
|
229 |
onVideoStreamChange(null);
|
230 |
|
231 |
try {
|
232 |
-
console.log(`🔄
|
233 |
const newStream = await navigator.mediaDevices.getUserMedia({
|
234 |
video: { facingMode: { exact: targetFacingMode } },
|
235 |
audio: false,
|
236 |
});
|
|
|
237 |
setActiveLocalVideoStream(newStream);
|
238 |
onVideoStreamChange(newStream);
|
239 |
setCurrentFacingMode(targetFacingMode);
|
@@ -243,12 +276,13 @@ function ControlTray({
|
|
243 |
}
|
244 |
} catch (error) {
|
245 |
console.error(`❌ Error switching camera to ${targetFacingMode}:`, error);
|
246 |
-
console.log(`Attempting to restore camera to: ${currentFacingMode}`);
|
247 |
try {
|
248 |
const restoredStream = await navigator.mediaDevices.getUserMedia({
|
249 |
video: { facingMode: currentFacingMode },
|
250 |
audio: false,
|
251 |
});
|
|
|
252 |
setActiveLocalVideoStream(restoredStream);
|
253 |
onVideoStreamChange(restoredStream);
|
254 |
if (videoRef.current) {
|
@@ -262,27 +296,25 @@ function ControlTray({
|
|
262 |
}
|
263 |
} finally {
|
264 |
setIsSwitchingCamera(false);
|
|
|
265 |
}
|
266 |
};
|
267 |
|
268 |
useEffect(() => {
|
269 |
if (!connected) {
|
|
|
270 |
if (activeLocalVideoStream) {
|
271 |
activeLocalVideoStream.getTracks().forEach(track => track.stop());
|
272 |
setActiveLocalVideoStream(null);
|
273 |
onVideoStreamChange(null);
|
274 |
onAppCamToggle(false);
|
275 |
}
|
276 |
-
|
277 |
-
if (audioRecorder.recording) { // Changed from isRecording()
|
278 |
audioRecorder.stop();
|
279 |
onAppMicToggle(false);
|
280 |
}
|
281 |
-
// --- END OF CORRECTION ---
|
282 |
}
|
283 |
-
// eslint-disable-next-line react-hooks/exhaustive-deps
|
284 |
}, [connected, audioRecorder, activeLocalVideoStream, onAppCamToggle, onAppMicToggle, onVideoStreamChange]);
|
285 |
-
// Added dependencies to useEffect based on usage
|
286 |
|
287 |
|
288 |
return (
|
|
|
6 |
import React, { memo, ReactNode, RefObject, useEffect, useRef, useState } from "react";
|
7 |
import { useLiveAPIContext } from "../../contexts/LiveAPIContext";
|
8 |
import { AudioRecorder } from "../../lib/audio-recorder";
|
|
|
|
|
|
|
9 |
// SVG Icons for Controls (همانطور که قبلا تعریف شد)
|
10 |
const SvgPauseIcon = () => <svg width="37" height="37" viewBox="0 0 37 37" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M15.9872 29.6198V8.28342C15.9872 6.25781 15.132 5.44757 12.9713 5.44757H7.52469C5.36404 5.44757 4.50879 6.25781 4.50879 8.28342V29.6198C4.50879 31.6454 5.36404 32.4556 7.52469 32.4556H12.9713C15.132 32.4556 15.9872 31.6454 15.9872 29.6198Z" fill="#BE123C"/><path opacity="0.4" d="M31.5175 29.6198V8.28342C31.5175 6.25781 30.6622 5.44757 28.5016 5.44757H23.055C20.9093 5.44757 20.0391 6.25781 20.0391 8.28342V29.6198C20.0391 31.6454 20.8943 32.4556 23.055 32.4556H28.5016C30.6622 32.4556 31.5175 31.6454 31.5175 29.6198Z" fill="#BE123C"/></svg>;
|
11 |
const SvgMicrophoneIcon = () => <svg width="38" height="38" viewBox="0 0 69 68" fill="none" xmlns="http://www.w3.org/2000/svg" transform="scale(0.55)"><path opacity="0.4" d="M49.9479 27.1824C49.0803 27.1824 48.3907 27.872 48.3907 28.7396V32.2544C48.3907 40.1293 41.984 46.5361 34.109 46.5361C26.234 46.5361 19.8273 40.1293 19.8273 32.2544V28.7173C19.8273 27.8497 19.1377 27.1601 18.2701 27.1601C17.4025 27.1601 16.7129 27.8497 16.7129 28.7173V32.2321C16.7129 41.2861 23.6758 48.7384 32.5518 49.5393V54.2776C32.5518 55.1452 33.2414 55.8348 34.109 55.8348C34.9766 55.8348 35.6662 55.1452 35.6662 54.2776V49.5393C44.52 48.7607 51.5051 41.2861 51.5051 32.2321V28.7173C51.4829 27.872 50.7933 27.1824 49.9479 27.1824Z" fill="#BE123C"/><path d="M34.1099 11.3434C28.682 11.3434 24.2773 15.7481 24.2773 21.176V32.5658C24.2773 37.9938 28.682 42.3984 34.1099 42.3984C39.5379 42.3984 43.9425 37.9938 43.9425 32.5658V21.176C43.9425 15.7481 39.5379 11.3434 34.1099 11.3434ZM37.0241 26.8042C36.8684 27.3826 36.3567 27.7608 35.7784 27.7608C35.6671 27.7608 35.5559 27.7385 35.4447 27.7163C34.5771 27.4716 33.665 27.4716 32.7974 27.7163C32.0856 27.9165 31.396 27.4938 31.218 26.8042C31.0178 26.1146 31.4404 25.4027 32.1301 25.2247C33.4426 24.8688 34.8218 24.8688 36.1343 25.2247C36.8017 25.4027 37.2021 26.1146 37.0241 26.8042ZM38.2031 22.4885C38.0029 23.0224 37.5135 23.3339 36.9796 23.3339C36.8239 23.3339 36.6904 23.3116 36.5347 23.2671C34.9775 22.6887 33.2423 22.6887 31.6852 23.2671C31.0178 23.5118 30.2614 23.1559 30.0167 22.4885C29.772 21.8212 30.128 21.0648 30.7953 20.8423C32.9309 20.0637 35.289 20.0637 37.4245 20.8423C38.0919 21.087 38.4478 21.8212 38.2031 22.4885Z" fill="#BE123C"/></svg>;
|
|
|
13 |
const SvgStopCamIcon = () => <svg width="40" height="40" viewBox="0 0 40 40" fill="none" xmlns="http://www.w3.org/2000/svg"><path opacity="0.4" d="M29.0785 10.8076L6.91966 32.9665C4.61316 31.5002 3.70703 28.8807 3.70703 26.36V13.18C3.70703 7.54555 5.89821 5.35437 11.5327 5.35437H21.4177C26.1789 5.35437 28.4854 6.9195 29.0785 10.8076Z" fill="#2252A0"/><path opacity="0.4" d="M29.2427 15.2394V26.36C29.2427 26.4918 29.2262 26.5907 29.2262 26.706C29.2262 26.8213 29.2097 26.9366 29.2097 27.052H29.2262C29.045 32.1757 26.8208 34.1856 21.417 34.1856H11.532C11.1202 34.1856 10.7412 34.1692 10.3623 34.1197L29.2427 15.2394Z" fill="#2252A0"/><path opacity="0.4" d="M29.21 27.052C29.21 26.9366 29.2264 26.8213 29.2264 26.706C29.2429 26.8213 29.2429 26.9366 29.2264 27.052H29.21Z" fill="#2252A0"/><path opacity="0.4" d="M29.2264 12.4716C29.2429 12.5869 29.2429 12.7187 29.2264 12.834C29.2264 12.7187 29.21 12.6034 29.21 12.488L29.2264 12.4716Z" fill="#2252A0"/><path d="M37.4804 13.8061V25.734C37.4804 28.0899 36.3436 29.029 35.6682 29.3749C35.3551 29.5397 34.8774 29.7209 34.2678 29.7209C33.5594 29.7209 32.6697 29.4903 31.6483 28.7654L29.2264 27.052H29.21C29.21 26.9366 29.2264 26.8213 29.2264 26.706C29.2264 26.5907 29.2429 26.4918 29.2429 26.36V15.2394L34.6302 9.85205C35.0751 9.885 35.421 10.0168 35.6682 10.1651C36.3436 10.5111 37.4804 11.4501 37.4804 13.8061Z" fill="#2252A0"/><path d="M35.8666 3.67393C35.3723 3.17968 34.565 3.17968 34.0708 3.67393L3.6744 34.0868C3.18015 34.581 3.18015 35.3883 3.6744 35.8826C3.92152 36.1132 4.23455 36.245 4.56405 36.245C4.89355 36.245 5.20657 36.1132 5.4537 35.8661L35.8666 5.45323C36.3773 4.95898 36.3773 4.16818 35.8666 3.67393Z" fill="#2252A0"/></svg>;
|
14 |
const SvgSwitchCameraIcon = () => <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"><path d="M11 19H4a2 2 0 0 1-2-2V7a2 2 0 0 1 2-2h5"/><path d="M13 5h7a2 2 0 0 1 2 2v10a2 2 0 0 1-2 2h-5"/><path d="m17 12-3-3 3-3"/><path d="m7 12 3 3-3 3"/></svg>;
|
15 |
|
|
|
16 |
export type ControlTrayProps = {
|
17 |
videoRef: RefObject<HTMLVideoElement>;
|
18 |
children?: ReactNode;
|
|
|
35 |
onAppCamToggle,
|
36 |
createLogoFunction,
|
37 |
}: ControlTrayProps) {
|
38 |
+
const { client, connected, connect, disconnect } = useLiveAPIContext();
|
|
|
|
|
39 |
const [audioRecorder] = useState(() => new AudioRecorder());
|
40 |
const [muted, setMuted] = useState(true);
|
41 |
const [inVolume, setInVolume] = useState(0);
|
|
|
42 |
const [activeLocalVideoStream, setActiveLocalVideoStream] = useState<MediaStream | null>(null);
|
43 |
const [currentFacingMode, setCurrentFacingMode] = useState<'user' | 'environment'>('user');
|
44 |
const [isSwitchingCamera, setIsSwitchingCamera] = useState(false);
|
|
|
48 |
}, [isAppMicActive]);
|
49 |
|
50 |
useEffect(() => {
|
51 |
+
console.log(`isAppCamActive effect: ${isAppCamActive}, current activeLocalVideoStream: ${activeLocalVideoStream ? 'Exists' : 'Null'}`);
|
52 |
if (isAppCamActive && !activeLocalVideoStream) {
|
53 |
+
console.log("isAppCamActive TRUE and no stream, calling startWebcam()");
|
54 |
startWebcam();
|
55 |
} else if (!isAppCamActive && activeLocalVideoStream) {
|
56 |
+
console.log("isAppCamActive FALSE and stream exists, calling stopWebcam()");
|
57 |
stopWebcam();
|
58 |
}
|
59 |
// eslint-disable-next-line react-hooks/exhaustive-deps
|
60 |
+
}, [isAppCamActive]); // activeLocalVideoStream removed to prevent re-triggering startWebcam if it fails and sets stream to null
|
61 |
|
62 |
useEffect(() => {
|
63 |
const onData = (base64: string) => {
|
|
|
67 |
};
|
68 |
if (connected && !muted && audioRecorder) {
|
69 |
audioRecorder.on("data", onData).on("volume", setInVolume).start();
|
70 |
+
} else if (audioRecorder && audioRecorder.recording) { // Check if it's recording before stopping
|
71 |
audioRecorder.stop();
|
72 |
}
|
73 |
return () => {
|
74 |
if (audioRecorder) {
|
75 |
+
audioRecorder.off("data", onData).off("volume", setInVolume);
|
76 |
+
if (audioRecorder.recording) audioRecorder.stop();
|
77 |
}
|
78 |
};
|
79 |
}, [connected, client, muted, audioRecorder]);
|
|
|
88 |
const video = videoRef.current;
|
89 |
const canvas = renderCanvasRef.current;
|
90 |
if (!video || !canvas || video.readyState < video.HAVE_METADATA || video.paused || video.ended || !client) return;
|
|
|
91 |
try {
|
92 |
const ctx = canvas.getContext("2d");
|
93 |
if (!ctx) return;
|
|
|
104 |
console.error("❌ Error processing video frame:", error);
|
105 |
}
|
106 |
}
|
|
|
107 |
if (connected && activeLocalVideoStream && videoRef.current) {
|
108 |
setTimeout(sendVideoFrame, 200);
|
109 |
}
|
|
|
112 |
|
113 |
useEffect(() => {
|
114 |
if (videoRef.current) {
|
115 |
+
console.log("VideoRef Effect: Current srcObject:", videoRef.current.srcObject ? 'Exists' : 'Null', "New activeLocalVideoStream:", activeLocalVideoStream ? 'Exists' : 'Null');
|
116 |
if (videoRef.current.srcObject !== activeLocalVideoStream) {
|
117 |
videoRef.current.srcObject = activeLocalVideoStream;
|
118 |
+
console.log("VideoRef Effect: Assigned new stream to srcObject.");
|
119 |
if (activeLocalVideoStream) {
|
120 |
+
console.log("VideoRef Effect: Attempting to play video.");
|
121 |
+
videoRef.current.play().then(() => {
|
122 |
+
console.log("VideoRef Effect: Video playback started successfully.");
|
123 |
+
}).catch(e => {
|
124 |
+
console.warn("VideoRef Effect: Video play failed:", e);
|
125 |
+
});
|
126 |
+
} else {
|
127 |
+
console.log("VideoRef Effect: activeLocalVideoStream is null, video srcObject set to null.");
|
128 |
}
|
129 |
}
|
130 |
+
} else {
|
131 |
+
console.warn("VideoRef Effect: videoRef.current is null.");
|
132 |
}
|
133 |
}, [activeLocalVideoStream, videoRef]);
|
134 |
|
135 |
const ensureConnected = async () => {
|
136 |
+
console.log("ensureConnected called. Current connected state:", connected);
|
137 |
if (!connected) {
|
138 |
try {
|
139 |
+
console.log("ensureConnected: Attempting to connect...");
|
140 |
await connect();
|
141 |
+
console.log("ensureConnected: Connection successful.");
|
142 |
+
return true;
|
143 |
} catch (err) {
|
144 |
+
console.error('❌ ensureConnected: Connection error:', err);
|
145 |
return false;
|
146 |
}
|
147 |
}
|
148 |
+
console.log("ensureConnected: Already connected.");
|
149 |
return true;
|
150 |
};
|
151 |
|
152 |
const handleMicToggle = async () => {
|
153 |
+
console.log("handleMicToggle called. isSwitchingCamera:", isSwitchingCamera, "Current isAppMicActive:", isAppMicActive);
|
154 |
if (isSwitchingCamera) return;
|
155 |
const newMicState = !isAppMicActive;
|
156 |
if (newMicState) {
|
157 |
if(!await ensureConnected()) {
|
158 |
+
console.log("handleMicToggle: ensureConnected failed while trying to turn mic ON.");
|
159 |
onAppMicToggle(false);
|
160 |
return;
|
161 |
}
|
162 |
}
|
163 |
onAppMicToggle(newMicState);
|
164 |
+
console.log("handleMicToggle: Mic state toggled to", newMicState);
|
|
|
|
|
165 |
};
|
166 |
|
167 |
const startWebcam = async (facingModeToTry: 'user' | 'environment' = currentFacingMode) => {
|
168 |
+
console.log("Attempting to start webcam. Current facingMode:", facingModeToTry, "isSwitchingCamera:", isSwitchingCamera);
|
169 |
+
if (isSwitchingCamera) {
|
170 |
+
console.log("startWebcam: Already switching camera, aborting.");
|
171 |
+
return;
|
172 |
+
}
|
173 |
setIsSwitchingCamera(true);
|
174 |
|
175 |
if(!await ensureConnected()) {
|
176 |
+
console.error("startWebcam: ensureConnected failed. Aborting webcam start.");
|
177 |
+
onAppCamToggle(false);
|
178 |
setIsSwitchingCamera(false);
|
179 |
return;
|
180 |
}
|
181 |
+
console.log("startWebcam: ensureConnected succeeded.");
|
182 |
|
183 |
try {
|
184 |
+
console.log(`🚀 Getting user media with facingMode: ${facingModeToTry}`);
|
185 |
const mediaStream = await navigator.mediaDevices.getUserMedia({
|
186 |
video: { facingMode: facingModeToTry },
|
187 |
audio: false,
|
188 |
});
|
189 |
+
console.log("startWebcam: getUserMedia successful. Stream ID:", mediaStream.id);
|
190 |
setActiveLocalVideoStream(mediaStream);
|
191 |
onVideoStreamChange(mediaStream);
|
192 |
setCurrentFacingMode(facingModeToTry);
|
193 |
+
onAppCamToggle(true);
|
194 |
+
console.log("startWebcam: Webcam started and states updated.");
|
195 |
} catch (error) {
|
196 |
console.error(`❌ Error starting webcam with ${facingModeToTry}:`, error);
|
197 |
if (facingModeToTry === 'user' && (error as Error).name !== 'NotFoundError') {
|
198 |
try {
|
199 |
+
console.log("startWebcam: Attempting fallback to 'environment' facingMode.");
|
200 |
const fallbackStream = await navigator.mediaDevices.getUserMedia({ video: { facingMode: 'environment' }, audio: false });
|
201 |
+
console.log("startWebcam: Fallback getUserMedia successful. Stream ID:", fallbackStream.id);
|
202 |
setActiveLocalVideoStream(fallbackStream);
|
203 |
onVideoStreamChange(fallbackStream);
|
204 |
setCurrentFacingMode('environment');
|
|
|
216 |
}
|
217 |
} finally {
|
218 |
setIsSwitchingCamera(false);
|
219 |
+
console.log("startWebcam: Finished attempt, isSwitchingCamera set to false.");
|
220 |
}
|
221 |
};
|
222 |
|
223 |
const stopWebcam = () => {
|
224 |
+
console.log("stopWebcam called.");
|
225 |
if (activeLocalVideoStream) {
|
226 |
+
console.log("stopWebcam: Stopping tracks for stream ID:", activeLocalVideoStream.id);
|
227 |
activeLocalVideoStream.getTracks().forEach(track => track.stop());
|
228 |
+
} else {
|
229 |
+
console.log("stopWebcam: No activeLocalVideoStream to stop.");
|
230 |
}
|
231 |
setActiveLocalVideoStream(null);
|
232 |
onVideoStreamChange(null);
|
233 |
onAppCamToggle(false);
|
234 |
+
console.log("stopWebcam: Webcam stopped and states updated.");
|
|
|
|
|
235 |
};
|
236 |
|
237 |
const handleCamToggle = () => {
|
238 |
+
console.log("handleCamToggle called. Current isAppCamActive:", isAppCamActive);
|
239 |
if (isAppCamActive) {
|
240 |
+
console.log("handleCamToggle: Current cam is active, calling stopWebcam.");
|
241 |
stopWebcam();
|
242 |
} else {
|
243 |
+
console.log("handleCamToggle: Current cam is inactive, calling startWebcam.");
|
244 |
startWebcam();
|
245 |
}
|
246 |
};
|
247 |
|
248 |
const handleSwitchCamera = async () => {
|
249 |
+
console.log("handleSwitchCamera called. isAppCamActive:", isAppCamActive, "Stream exists:", !!activeLocalVideoStream, "isSwitchingCamera:", isSwitchingCamera);
|
250 |
+
if (!isAppCamActive || !activeLocalVideoStream || isSwitchingCamera) {
|
251 |
+
console.log("handleSwitchCamera: Pre-conditions not met, aborting.");
|
252 |
+
return;
|
253 |
+
}
|
254 |
|
255 |
setIsSwitchingCamera(true);
|
256 |
const targetFacingMode = currentFacingMode === 'user' ? 'environment' : 'user';
|
257 |
+
console.log(`handleSwitchCamera: Stopping current stream tracks (Stream ID: ${activeLocalVideoStream.id}). Target facingMode: ${targetFacingMode}`);
|
258 |
|
259 |
activeLocalVideoStream.getTracks().forEach(track => track.stop());
|
260 |
setActiveLocalVideoStream(null);
|
261 |
onVideoStreamChange(null);
|
262 |
|
263 |
try {
|
264 |
+
console.log(`🔄 Attempting to switch camera to: ${targetFacingMode}`);
|
265 |
const newStream = await navigator.mediaDevices.getUserMedia({
|
266 |
video: { facingMode: { exact: targetFacingMode } },
|
267 |
audio: false,
|
268 |
});
|
269 |
+
console.log("handleSwitchCamera: Switched camera successfully. New Stream ID:", newStream.id);
|
270 |
setActiveLocalVideoStream(newStream);
|
271 |
onVideoStreamChange(newStream);
|
272 |
setCurrentFacingMode(targetFacingMode);
|
|
|
276 |
}
|
277 |
} catch (error) {
|
278 |
console.error(`❌ Error switching camera to ${targetFacingMode}:`, error);
|
279 |
+
console.log(`handleSwitchCamera: Attempting to restore camera to previous mode: ${currentFacingMode}`);
|
280 |
try {
|
281 |
const restoredStream = await navigator.mediaDevices.getUserMedia({
|
282 |
video: { facingMode: currentFacingMode },
|
283 |
audio: false,
|
284 |
});
|
285 |
+
console.log("handleSwitchCamera: Restored camera successfully. Stream ID:", restoredStream.id);
|
286 |
setActiveLocalVideoStream(restoredStream);
|
287 |
onVideoStreamChange(restoredStream);
|
288 |
if (videoRef.current) {
|
|
|
296 |
}
|
297 |
} finally {
|
298 |
setIsSwitchingCamera(false);
|
299 |
+
console.log("handleSwitchCamera: Finished attempt, isSwitchingCamera set to false.");
|
300 |
}
|
301 |
};
|
302 |
|
303 |
useEffect(() => {
|
304 |
if (!connected) {
|
305 |
+
console.log("Not connected: Stopping video and audio if active.");
|
306 |
if (activeLocalVideoStream) {
|
307 |
activeLocalVideoStream.getTracks().forEach(track => track.stop());
|
308 |
setActiveLocalVideoStream(null);
|
309 |
onVideoStreamChange(null);
|
310 |
onAppCamToggle(false);
|
311 |
}
|
312 |
+
if (audioRecorder.recording) {
|
|
|
313 |
audioRecorder.stop();
|
314 |
onAppMicToggle(false);
|
315 |
}
|
|
|
316 |
}
|
|
|
317 |
}, [connected, audioRecorder, activeLocalVideoStream, onAppCamToggle, onAppMicToggle, onVideoStreamChange]);
|
|
|
318 |
|
319 |
|
320 |
return (
|