Ezmary commited on
Commit
b9f6bcf
·
verified ·
1 Parent(s): f0ebb0b

Update src/components/control-tray/ControlTray.tsx

Browse files
src/components/control-tray/ControlTray.tsx CHANGED
@@ -3,15 +3,16 @@ Copyright 2024 Google LLC
3
  ... (لایسنس و توضیحات دیگر مثل قبل) ...
4
  */
5
  import cn from "classnames";
6
- import React, { memo, ReactNode, RefObject, useEffect, useState } from "react"; // removed useRef as renderCanvasRef is now passed if needed or handled differently
7
  import { useLiveAPIContext } from "../../contexts/LiveAPIContext";
8
  import { AudioRecorder } from "../../lib/audio-recorder";
9
 
10
- // SVG Icons for Controls (مثل قبل)
11
  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>;
12
  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 SvgCameraIcon = () => <svg width="40" height="40" viewBox="0 0 40 40" fill="none" xmlns="http://www.w3.org/2000/svg"><path opacity="0.4" d="M21.8118 5.52235H11.9265C6.29183 5.52235 4.10059 7.7136 4.10059 13.3482V26.5286C4.10059 30.318 6.16002 34.3545 11.9265 34.3545H21.8118C27.4464 34.3545 29.6376 32.1633 29.6376 26.5286V13.3482C29.6376 7.7136 27.4464 5.52235 21.8118 5.52235Z" fill="#2252A0"/><path d="M19.3406 18.9169C21.0512 18.9169 22.438 17.5302 22.438 15.8195C22.438 14.1089 21.0512 12.7222 19.3406 12.7222C17.6299 12.7222 16.2432 14.1089 16.2432 15.8195C16.2432 17.5302 17.6299 18.9169 19.3406 18.9169Z" fill="#2252A0"/><path d="M36.0629 10.3332C35.3874 9.98721 33.9705 9.5918 32.0429 10.9428L29.6045 12.6562C29.621 12.8869 29.6374 13.1011 29.6374 13.3482V26.5286C29.6374 26.7758 29.6045 26.9899 29.6045 27.2206L32.0429 28.934C33.0643 29.659 33.954 29.8896 34.6625 29.8896C35.2721 29.8896 35.7499 29.7249 36.0629 29.5601C36.7384 29.2141 37.8752 28.275 37.8752 25.919V13.9743C37.8752 11.6183 36.7384 10.6792 36.0629 10.3332Z" fill="#2252A0"/></svg>;
14
  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>;
 
15
  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>;
16
 
17
  export type ControlTrayProps = {
@@ -36,22 +37,20 @@ const ControlTray: React.FC<ControlTrayProps> = ({
36
  onAppCamToggle,
37
  createLogoFunction,
38
  }) => {
39
- const { client, connected, connect } = useLiveAPIContext(); // disconnect is handled in AppInternalLogic
40
  const [audioRecorder] = useState(() => new AudioRecorder());
41
  const [activeLocalVideoStream, setActiveLocalVideoStream] = useState<MediaStream | null>(null);
42
  const [currentFacingMode, setCurrentFacingMode] = useState<'user' | 'environment'>('user');
43
  const [isSwitchingCamera, setIsSwitchingCamera] = useState(false);
44
  const renderCanvasRef = React.useRef<HTMLCanvasElement>(null);
45
 
46
-
47
  useEffect(() => {
48
  if (isAppCamActive && !activeLocalVideoStream && !isSwitchingCamera) {
49
  startWebcam();
50
  } else if (!isAppCamActive && activeLocalVideoStream) {
51
  stopWebcam();
52
  }
53
- // eslint-disable-next-line react-hooks/exhaustive-deps
54
- }, [isAppCamActive]);
55
 
56
  useEffect(() => {
57
  const onData = (base64: string) => {
@@ -96,9 +95,7 @@ const ControlTray: React.FC<ControlTrayProps> = ({
96
  client.sendRealtimeInput([{ mimeType: "image/jpeg", data }]);
97
  }
98
  }
99
- } catch (error) {
100
- console.error("❌ Error processing video frame:", error);
101
- }
102
  }
103
  if (connected && activeLocalVideoStream) {
104
  timeoutId = window.setTimeout(sendVideoFrame, 1000 / 4);
@@ -110,7 +107,6 @@ const ControlTray: React.FC<ControlTrayProps> = ({
110
  return () => clearTimeout(timeoutId);
111
  }, [connected, activeLocalVideoStream, client, videoRef, renderCanvasRef]);
112
 
113
-
114
  useEffect(() => {
115
  if (videoRef.current) {
116
  if (videoRef.current.srcObject !== activeLocalVideoStream) {
@@ -124,13 +120,8 @@ const ControlTray: React.FC<ControlTrayProps> = ({
124
 
125
  const ensureConnectedAndReady = async (): Promise<boolean> => {
126
  if (!connected) {
127
- try {
128
- await connect();
129
- return true;
130
- } catch (err) {
131
- console.error('❌ ControlTray: Connection error:', err);
132
- return false;
133
- }
134
  }
135
  return true;
136
  };
@@ -138,12 +129,8 @@ const ControlTray: React.FC<ControlTrayProps> = ({
138
  const handleMicToggle = async () => {
139
  if (isSwitchingCamera) return;
140
  const newMicState = !isAppMicActive;
141
-
142
- if (newMicState) {
143
- if (!(await ensureConnectedAndReady())) {
144
- onAppMicToggle(false);
145
- return;
146
- }
147
  }
148
  onAppMicToggle(newMicState);
149
  };
@@ -152,46 +139,30 @@ const ControlTray: React.FC<ControlTrayProps> = ({
152
  if (isSwitchingCamera) return;
153
  setIsSwitchingCamera(true);
154
  try {
155
- const mediaStream = await navigator.mediaDevices.getUserMedia({
156
- video: { facingMode: facingModeToTry },
157
- audio: false,
158
- });
159
  setActiveLocalVideoStream(mediaStream);
160
  onVideoStreamChange(mediaStream);
161
  setCurrentFacingMode(facingModeToTry);
162
  } catch (error) {
163
- console.error(`❌ Error starting webcam with ${facingModeToTry}:`, error);
164
- setActiveLocalVideoStream(null);
165
- onVideoStreamChange(null);
166
- onAppCamToggle(false);
167
- } finally {
168
- setIsSwitchingCamera(false);
169
- }
170
  };
171
 
172
  const stopWebcam = () => {
173
- if (activeLocalVideoStream) {
174
- activeLocalVideoStream.getTracks().forEach(track => track.stop());
175
- }
176
- setActiveLocalVideoStream(null);
177
- onVideoStreamChange(null);
178
  };
179
 
180
  const handleCamToggle = async () => {
181
  if (isSwitchingCamera) return;
182
  const newCamState = !isAppCamActive;
183
-
184
  if (newCamState) {
185
- if (!(await ensureConnectedAndReady())) {
186
- onAppCamToggle(false);
187
- return;
188
- }
189
- if (!isAppMicActive) {
190
- onAppMicToggle(true);
191
- }
192
- onAppCamToggle(true);
193
  } else {
194
- onAppCamToggle(false);
195
  }
196
  };
197
 
@@ -201,82 +172,77 @@ const ControlTray: React.FC<ControlTrayProps> = ({
201
  const targetFacingMode = currentFacingMode === 'user' ? 'environment' : 'user';
202
  activeLocalVideoStream.getTracks().forEach(track => track.stop());
203
  try {
204
- const newStream = await navigator.mediaDevices.getUserMedia({
205
- video: { facingMode: { exact: targetFacingMode } },
206
- audio: false,
207
- });
208
- setActiveLocalVideoStream(newStream);
209
- onVideoStreamChange(newStream);
210
- setCurrentFacingMode(targetFacingMode);
211
  } catch (error) {
212
- console.error(`❌ Error switching camera to ${targetFacingMode}:`, error);
213
  try {
214
  const restoredStream = await navigator.mediaDevices.getUserMedia({ video: { facingMode: currentFacingMode }, audio: false });
215
- setActiveLocalVideoStream(restoredStream);
216
- onVideoStreamChange(restoredStream);
217
  } catch (restoreError) {
218
- console.error('❌ Error restoring camera:', restoreError);
219
- setActiveLocalVideoStream(null);
220
- onVideoStreamChange(null);
221
- onAppCamToggle(false);
222
  }
223
- } finally {
224
- setIsSwitchingCamera(false);
225
- }
226
  };
227
 
228
  // کلاس‌های footer-controls از HTML مرجع برای موقعیت‌یابی و چیدمان
 
 
 
229
  const footerClasses = cn(
230
- "footer-controls w-full flex gap-4 absolute bottom-0 p-8 items-center z-50", // Tailwind: md:p-12
231
  {
232
- "layout-default justify-between": !isAppCamActive, // justify-between از HTML
233
- "layout-with-small-logo justify-around": isAppCamActive, // justify-around از HTML
234
  }
235
  );
236
 
237
  return (
 
238
  <footer id="footer-controls" className={footerClasses}>
239
  <canvas style={{ display: "none" }} ref={renderCanvasRef} />
240
 
241
- {/* Cam Button Wrapper (Now on the Left) */}
242
  <div id="cam-button-wrapper" className="relative flex justify-center">
243
  <button
244
  id="cam-button"
245
  className="control-button cam-button-color" // کلاس‌های HTML مرجع
246
  onClick={handleCamToggle}
247
  disabled={isSwitchingCamera || !supportsVideo}
248
- title={isAppCamActive ? "Stop Camera & Mic (if not used)" : "Start Camera & Mic"}
249
  >
250
  {isAppCamActive ? <SvgStopCamIcon /> : <SvgCameraIcon />}
251
  </button>
252
- {/* Switch Camera Button - نمایش فقط وقتی دوربین فعال است */}
253
  <div
254
  id="switch-camera-button-container"
255
  className={cn(
256
- "switch-camera-button-container", // کلاس‌های HTML مرجع
257
- { visible: isAppCamActive && !isSwitchingCamera }
258
  )}
259
  >
260
  <button
261
  id="switch-camera-button"
262
  aria-label="Switch Camera"
263
- className="switch-camera-button-content group" // کلاس‌های HTML مرجع
264
  onClick={handleSwitchCamera}
265
  disabled={!isAppCamActive || isSwitchingCamera}
266
  >
267
- <SvgSwitchCameraIcon/>
 
268
  </button>
269
  </div>
270
  </div>
271
 
272
  {/* Small Logo Container (Center, if cam active) */}
273
  {isAppCamActive && (
274
- <div id="small-logo-container" className="flex items-center justify-center"> {/* کلاس HTML مرجع */}
275
  {createLogoFunction(true, isAppCamActive, 'human')}
276
  </div>
277
  )}
278
 
279
- {/* Mic Button (Now on the Right) */}
280
  <button
281
  id="mic-button"
282
  className="control-button mic-button-color" // کلاس‌های HTML مرجع
 
3
  ... (لایسنس و توضیحات دیگر مثل قبل) ...
4
  */
5
  import cn from "classnames";
6
+ import React, { memo, ReactNode, RefObject, useEffect, useState } from "react";
7
  import { useLiveAPIContext } from "../../contexts/LiveAPIContext";
8
  import { AudioRecorder } from "../../lib/audio-recorder";
9
 
10
+ // SVG Icons for Controls (همانطور که قبلا تعریف شد)
11
  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>;
12
  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 SvgCameraIcon = () => <svg width="40" height="40" viewBox="0 0 40 40" fill="none" xmlns="http://www.w3.org/2000/svg"><path opacity="0.4" d="M21.8118 5.52235H11.9265C6.29183 5.52235 4.10059 7.7136 4.10059 13.3482V26.5286C4.10059 30.318 6.16002 34.3545 11.9265 34.3545H21.8118C27.4464 34.3545 29.6376 32.1633 29.6376 26.5286V13.3482C29.6376 7.7136 27.4464 5.52235 21.8118 5.52235Z" fill="#2252A0"/><path d="M19.3406 18.9169C21.0512 18.9169 22.438 17.5302 22.438 15.8195C22.438 14.1089 21.0512 12.7222 19.3406 12.7222C17.6299 12.7222 16.2432 14.1089 16.2432 15.8195C16.2432 17.5302 17.6299 18.9169 19.3406 18.9169Z" fill="#2252A0"/><path d="M36.0629 10.3332C35.3874 9.98721 33.9705 9.5918 32.0429 10.9428L29.6045 12.6562C29.621 12.8869 29.6374 13.1011 29.6374 13.3482V26.5286C29.6374 26.7758 29.6045 26.9899 29.6045 27.2206L32.0429 28.934C33.0643 29.659 33.954 29.8896 34.6625 29.8896C35.2721 29.8896 35.7499 29.7249 36.0629 29.5601C36.7384 29.2141 37.8752 28.275 37.8752 25.919V13.9743C37.8752 11.6183 36.7384 10.6792 36.0629 10.3332Z" fill="#2252A0"/></svg>;
14
  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>;
15
+ // آیکون تعویض دوربین از HTML شما (با stroke="currentColor" برای ارث‌بری رنگ)
16
  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>;
17
 
18
  export type ControlTrayProps = {
 
37
  onAppCamToggle,
38
  createLogoFunction,
39
  }) => {
40
+ const { client, connected, connect } = useLiveAPIContext();
41
  const [audioRecorder] = useState(() => new AudioRecorder());
42
  const [activeLocalVideoStream, setActiveLocalVideoStream] = useState<MediaStream | null>(null);
43
  const [currentFacingMode, setCurrentFacingMode] = useState<'user' | 'environment'>('user');
44
  const [isSwitchingCamera, setIsSwitchingCamera] = useState(false);
45
  const renderCanvasRef = React.useRef<HTMLCanvasElement>(null);
46
 
 
47
  useEffect(() => {
48
  if (isAppCamActive && !activeLocalVideoStream && !isSwitchingCamera) {
49
  startWebcam();
50
  } else if (!isAppCamActive && activeLocalVideoStream) {
51
  stopWebcam();
52
  }
53
+ }, [isAppCamActive, activeLocalVideoStream, isSwitchingCamera]); // Dependencies updated
 
54
 
55
  useEffect(() => {
56
  const onData = (base64: string) => {
 
95
  client.sendRealtimeInput([{ mimeType: "image/jpeg", data }]);
96
  }
97
  }
98
+ } catch (error) { console.error("❌ Error frame:", error); }
 
 
99
  }
100
  if (connected && activeLocalVideoStream) {
101
  timeoutId = window.setTimeout(sendVideoFrame, 1000 / 4);
 
107
  return () => clearTimeout(timeoutId);
108
  }, [connected, activeLocalVideoStream, client, videoRef, renderCanvasRef]);
109
 
 
110
  useEffect(() => {
111
  if (videoRef.current) {
112
  if (videoRef.current.srcObject !== activeLocalVideoStream) {
 
120
 
121
  const ensureConnectedAndReady = async (): Promise<boolean> => {
122
  if (!connected) {
123
+ try { await connect(); return true; }
124
+ catch (err) { console.error('❌ CT Connect err:', err); return false; }
 
 
 
 
 
125
  }
126
  return true;
127
  };
 
129
  const handleMicToggle = async () => {
130
  if (isSwitchingCamera) return;
131
  const newMicState = !isAppMicActive;
132
+ if (newMicState && !(await ensureConnectedAndReady())) {
133
+ onAppMicToggle(false); return;
 
 
 
 
134
  }
135
  onAppMicToggle(newMicState);
136
  };
 
139
  if (isSwitchingCamera) return;
140
  setIsSwitchingCamera(true);
141
  try {
142
+ const mediaStream = await navigator.mediaDevices.getUserMedia({ video: { facingMode: facingModeToTry }, audio: false });
 
 
 
143
  setActiveLocalVideoStream(mediaStream);
144
  onVideoStreamChange(mediaStream);
145
  setCurrentFacingMode(facingModeToTry);
146
  } catch (error) {
147
+ console.error(`❌ Start WC err ${facingModeToTry}:`, error);
148
+ setActiveLocalVideoStream(null); onVideoStreamChange(null); onAppCamToggle(false);
149
+ } finally { setIsSwitchingCamera(false); }
 
 
 
 
150
  };
151
 
152
  const stopWebcam = () => {
153
+ if (activeLocalVideoStream) activeLocalVideoStream.getTracks().forEach(track => track.stop());
154
+ setActiveLocalVideoStream(null); onVideoStreamChange(null);
 
 
 
155
  };
156
 
157
  const handleCamToggle = async () => {
158
  if (isSwitchingCamera) return;
159
  const newCamState = !isAppCamActive;
 
160
  if (newCamState) {
161
+ if (!(await ensureConnectedAndReady())) { onAppCamToggle(false); return; }
162
+ if (!isAppMicActive) onAppMicToggle(true);
163
+ onAppCamToggle(true); // This will trigger startWebcam via useEffect
 
 
 
 
 
164
  } else {
165
+ onAppCamToggle(false); // This will trigger stopWebcam via useEffect
166
  }
167
  };
168
 
 
172
  const targetFacingMode = currentFacingMode === 'user' ? 'environment' : 'user';
173
  activeLocalVideoStream.getTracks().forEach(track => track.stop());
174
  try {
175
+ const newStream = await navigator.mediaDevices.getUserMedia({ video: { facingMode: { exact: targetFacingMode } }, audio: false });
176
+ setActiveLocalVideoStream(newStream); onVideoStreamChange(newStream); setCurrentFacingMode(targetFacingMode);
 
 
 
 
 
177
  } catch (error) {
178
+ console.error(`❌ Switch Cam err ${targetFacingMode}:`, error);
179
  try {
180
  const restoredStream = await navigator.mediaDevices.getUserMedia({ video: { facingMode: currentFacingMode }, audio: false });
181
+ setActiveLocalVideoStream(restoredStream); onVideoStreamChange(restoredStream);
 
182
  } catch (restoreError) {
183
+ console.error('❌ Restore Cam err:', restoreError);
184
+ setActiveLocalVideoStream(null); onVideoStreamChange(null); onAppCamToggle(false);
 
 
185
  }
186
+ } finally { setIsSwitchingCamera(false); }
 
 
187
  };
188
 
189
  // کلاس‌های footer-controls از HTML مرجع برای موقعیت‌یابی و چیدمان
190
+ // "w-full flex gap-4 absolute bottom-0 p-8 items-center z-50"
191
+ // padding: 2rem 3rem; (p-8 md:px-12)
192
+ // gap: 1rem; (gap-4)
193
  const footerClasses = cn(
194
+ "w-full flex gap-4 absolute bottom-0 p-8 md:px-12 items-center z-50",
195
  {
196
+ "justify-between": !isAppCamActive, // layout-default از HTML
197
+ "justify-around": isAppCamActive, // layout-with-small-logo از HTML
198
  }
199
  );
200
 
201
  return (
202
+ // استفاده از کلاس‌های دقیق از HTML برای footer-controls
203
  <footer id="footer-controls" className={footerClasses}>
204
  <canvas style={{ display: "none" }} ref={renderCanvasRef} />
205
 
206
+ {/* Cam Button Wrapper (Now on the Left in HTML) */}
207
  <div id="cam-button-wrapper" className="relative flex justify-center">
208
  <button
209
  id="cam-button"
210
  className="control-button cam-button-color" // کلاس‌های HTML مرجع
211
  onClick={handleCamToggle}
212
  disabled={isSwitchingCamera || !supportsVideo}
213
+ title={isAppCamActive ? "Stop Camera & Mic" : "Start Camera & Mic"}
214
  >
215
  {isAppCamActive ? <SvgStopCamIcon /> : <SvgCameraIcon />}
216
  </button>
217
+ {/* Switch Camera Button */}
218
  <div
219
  id="switch-camera-button-container"
220
  className={cn(
221
+ "switch-camera-button-container", // استایل از App.scss
222
+ { visible: isAppCamActive && !isSwitchingCamera } // کلاس visible از App.scss
223
  )}
224
  >
225
  <button
226
  id="switch-camera-button"
227
  aria-label="Switch Camera"
228
+ className="switch-camera-button-content group" // استایل از App.scss
229
  onClick={handleSwitchCamera}
230
  disabled={!isAppCamActive || isSwitchingCamera}
231
  >
232
+ {/* آیکون با stroke="currentColor" از App.scss رنگ می‌گیرد */}
233
+ <SvgSwitchCameraIcon/>
234
  </button>
235
  </div>
236
  </div>
237
 
238
  {/* Small Logo Container (Center, if cam active) */}
239
  {isAppCamActive && (
240
+ <div id="small-logo-container" className="flex items-center justify-center"> {/* کلاس از HTML */}
241
  {createLogoFunction(true, isAppCamActive, 'human')}
242
  </div>
243
  )}
244
 
245
+ {/* Mic Button (Now on the Right in HTML) */}
246
  <button
247
  id="mic-button"
248
  className="control-button mic-button-color" // کلاس‌های HTML مرجع