File size: 9,600 Bytes
38d6fd2
142b779
7f2a14a
233e6fa
3d6be6c
7f2a14a
dea1223
50f12c9
a77f50e
548c205
cb75fe0
c2773be
7da7877
d803703
35ee466
 
 
 
142b779
 
38d6fd2
 
848947d
7f2a14a
38d6fd2
3d6be6c
233e6fa
35ee466
7da7877
d7461ed
233e6fa
d7461ed
233e6fa
38d6fd2
233e6fa
a56b75a
 
3d6be6c
a56b75a
 
 
 
3d6be6c
50f12c9
38d6fd2
 
3d6be6c
38d6fd2
 
a56b75a
233e6fa
329607b
38d6fd2
 
 
 
 
329607b
 
 
 
 
 
38d6fd2
329607b
 
 
142b779
329607b
 
 
142b779
38d6fd2
d803703
 
8cce83c
3e36429
142b779
3e36429
d803703
 
34dee1a
d803703
329607b
548c205
38d6fd2
548c205
38d6fd2
548c205
 
38d6fd2
 
 
548c205
38d6fd2
 
 
 
 
 
 
 
 
 
 
 
 
548c205
38d6fd2
548c205
38d6fd2
548c205
 
f33e5a3
548c205
38d6fd2
548c205
38d6fd2
 
548c205
 
 
f33e5a3
548c205
38d6fd2
548c205
38d6fd2
 
 
548c205
 
34dee1a
329607b
38d6fd2
548c205
 
233e6fa
 
142b779
38d6fd2
329607b
3e36429
f33e5a3
38d6fd2
f33e5a3
548c205
f33e5a3
142b779
f33e5a3
 
329607b
142b779
f33e5a3
329607b
f33e5a3
38d6fd2
329607b
f33e5a3
329607b
 
142b779
f33e5a3
38d6fd2
f33e5a3
dea1223
50f12c9
efe5b15
7da7877
c2773be
6a9ed5b
38d6fd2
 
dea1223
548c205
dea1223
eb0ac7f
 
38d6fd2
eb0ac7f
 
 
38d6fd2
 
142b779
548c205
233e6fa
38d6fd2
548c205
 
 
 
35ee466
7da7877
d7461ed
1476e59
688a2e9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// src/components/control-tray/ControlTray.tsx (نسخه نهایی با قابلیت غیرفعال شدن)

import cn from "classnames";
import React, { memo, RefObject, useEffect, useState, useCallback, useRef } from "react";
import { useAppContext } from "../../contexts/AppContext";
import { AudioRecorder } from "../../lib/audio-recorder";
import Logo from '../logo/Logo';
import { PauseIconWithSurroundPulse, microphoneIcon, cameraIcon, stopCamIcon } from '../icons';

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" className="w-[22px] h-[22px]"><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>;

export type ControlTrayProps = {
  videoRef: RefObject<HTMLVideoElement>;
  onUserSpeakingChange: (isSpeaking: boolean) => void;
  isAppMicActive: boolean;
  onAppMicToggle: (active: boolean) => void;
  isAppCamActive: boolean;
  onAppCamToggle: (active: boolean) => void;
  currentFacingMode: 'user' | 'environment';
  onFacingModeChange: (mode: 'user' | 'environment') => void;
  // ✅ پراپرتی جدید برای غیرفعال کردن کنترل‌ها
  isTimeUp: boolean; 
};

const ControlTray: React.FC<ControlTrayProps> = ({ videoRef, onUserSpeakingChange, isAppMicActive, onAppMicToggle, isAppCamActive, onAppCamToggle, currentFacingMode, onFacingModeChange, isTimeUp }) => {
  const { client, connected, connect, volume } = useAppContext();
  const audioRecorderRef = useRef<AudioRecorder | null>(null);
  const [activeLocalVideoStream, setActiveLocalVideoStream] = useState<MediaStream | null>(null);
  const [isSwitchingCamera, setIsSwitchingCamera] = useState(false);
  const renderCanvasRef = React.useRef<HTMLCanvasElement>(null);
  const [userVolume, setUserVolume] = useState(0);

  useEffect(() => {
    if (!audioRecorderRef.current) audioRecorderRef.current = new AudioRecorder();
    const audioRecorder = audioRecorderRef.current;
    const handleAudioData = (base64: string, vol: number) => {
      if (client && connected && isAppMicActive) {
        if (base64) client.sendRealtimeInput([{ mimeType: "audio/pcm;rate=16000", data: base64 }]);
        setUserVolume(vol);
        onUserSpeakingChange(vol > 0.01);
      }
    };
    const onStop = () => { setUserVolume(0); onUserSpeakingChange(false); };
    if (isAppMicActive && connected) {
      audioRecorder.on("data", handleAudioData);
      audioRecorder.on("stop", onStop);
      if (!audioRecorder.recording) audioRecorder.start();
    } else if (audioRecorder?.recording) { audioRecorder.stop(); }
    return () => { if (audioRecorder) { audioRecorder.off("data", handleAudioData); audioRecorder.off("stop", onStop); } };
  }, [isAppMicActive, connected, onUserSpeakingChange, client]);
  
  useEffect(() => {
    if (videoRef.current) {
      if (videoRef.current.srcObject !== activeLocalVideoStream) {
        videoRef.current.srcObject = activeLocalVideoStream;
        if (activeLocalVideoStream) videoRef.current.play().catch(e => console.warn("Video play failed:", e));
      }
    }
  }, [activeLocalVideoStream, videoRef]);

  const startWebcam = useCallback(async (facingModeToTry: 'user' | 'environment') => {
    if (isSwitchingCamera) return;
    setIsSwitchingCamera(true);
    if (activeLocalVideoStream) activeLocalVideoStream.getTracks().forEach(track => track.stop());
    try {
      const mediaStream = await navigator.mediaDevices.getUserMedia({ video: { facingMode: facingModeToTry }, audio: false });
      setActiveLocalVideoStream(mediaStream);
      onFacingModeChange(facingModeToTry);
    } catch (error) {
      console.error(`❌ Start WC err ${facingModeToTry}:`, error);
      setActiveLocalVideoStream(null);
      onAppCamToggle(false);
    } finally { setIsSwitchingCamera(false); }
  }, [isSwitchingCamera, activeLocalVideoStream, onFacingModeChange, onAppCamToggle]);

  useEffect(() => {
    if (isAppCamActive && !activeLocalVideoStream && !isSwitchingCamera) {
      startWebcam(currentFacingMode);
    } else if (!isAppCamActive && activeLocalVideoStream) {
      activeLocalVideoStream.getTracks().forEach(track => track.stop());
      setActiveLocalVideoStream(null);
    }
  }, [isAppCamActive, activeLocalVideoStream, isSwitchingCamera, startWebcam, currentFacingMode]);

  useEffect(() => {
    let timeoutId = -1;
    function sendVideoFrame() {
      if (connected && activeLocalVideoStream && client && videoRef.current) {
        const video = videoRef.current;
        const canvas = renderCanvasRef.current;
        if (!canvas || video.readyState < video.HAVE_METADATA || video.paused || video.ended) {
          if (activeLocalVideoStream) timeoutId = window.setTimeout(sendVideoFrame, 1000 / 4);
          return;
        }
        try {
          const ctx = canvas.getContext("2d");
          if (!ctx) return;
          const scale = 0.5;
          canvas.width = video.videoWidth * scale;
          canvas.height = video.videoHeight * scale;
          if (canvas.width > 0 && canvas.height > 0) {
            ctx.drawImage(video, 0, 0, canvas.width, canvas.height);
            const base64 = canvas.toDataURL("image/jpeg", 0.8);
            const data = base64.slice(base64.indexOf(",") + 1);
            if (data) client.sendRealtimeInput([{ mimeType: "image/jpeg", data }]);
          }
        } catch (error) { console.error("❌ Error frame:", error); }
      }
      if (connected && activeLocalVideoStream) timeoutId = window.setTimeout(sendVideoFrame, 1000 / 4);
    }
    if (connected && activeLocalVideoStream && videoRef.current) timeoutId = window.setTimeout(sendVideoFrame, 200);
    return () => clearTimeout(timeoutId);
  }, [connected, activeLocalVideoStream, client, videoRef, renderCanvasRef]);

  const ensureConnectedAndReady = async (): Promise<boolean> => {
    if (isTimeUp) return false; // اگر زمان تمام شده، اجازه اتصال نده
    if (!connected) {
      try { await connect(); return true; }
      catch (err) { console.error('❌ CT Connect err:', err); return false; }
    }
    return true;
  };

  const handleMicToggle = async () => {
    if (isSwitchingCamera || isTimeUp) return;
    const newMicState = !isAppMicActive;
    if (newMicState && !(await ensureConnectedAndReady())) {
      onAppMicToggle(false); return;
    }
    onAppMicToggle(newMicState);
  };

  const handleCamToggle = async () => {
    if (isSwitchingCamera || isTimeUp) return;
    const newCamState = !isAppCamActive;
    if (newCamState) {
        if (!(await ensureConnectedAndReady())) { onAppCamToggle(false); return; }
        if (!isAppMicActive) onAppMicToggle(true);
        onAppCamToggle(true);
    } else { onAppCamToggle(false); }
  };

  const handleSwitchCamera = async () => {
    if (!isAppCamActive || !activeLocalVideoStream || isSwitchingCamera || isTimeUp) return;
    setIsSwitchingCamera(true);
    const targetFacingMode = currentFacingMode === 'user' ? 'environment' : 'user';
    activeLocalVideoStream.getTracks().forEach(track => track.stop());
    setActiveLocalVideoStream(null);
    try {
      const newStream = await navigator.mediaDevices.getUserMedia({ video: { facingMode: { exact: targetFacingMode } }, audio: false });
      setActiveLocalVideoStream(newStream);
      onFacingModeChange(targetFacingMode);
    } catch (error) {
      console.error(`❌ Switch Cam err to ${targetFacingMode}:`, error);
      try {
        const restoredStream = await navigator.mediaDevices.getUserMedia({ video: { facingMode: {exact: currentFacingMode } }, audio: false });
        setActiveLocalVideoStream(restoredStream);
      } catch (restoreError) {
        console.error(`❌ Restore Cam err to ${currentFacingMode}:`, restoreError);
        setActiveLocalVideoStream(null);
        onAppCamToggle(false);
      }
    } finally { setIsSwitchingCamera(false); }
  };
  
  const isSpeaking = userVolume > 0.01;

  return (
    <footer id="footer-controls" className="footer-controls-html-like">
      <canvas style={{ display: "none" }} ref={renderCanvasRef} />
      {/* ✅ غیرفعال کردن دکمه‌ها وقتی زمان تمام شده */}
      <div id="mic-button" className={cn("control-button mic-button-color", { "disabled": isTimeUp })} onClick={handleMicToggle}>
        {isAppMicActive ? <PauseIconWithSurroundPulse userVolume={userVolume} /> : microphoneIcon}
      </div>
      
      {isAppCamActive && (
        <div id="small-logo-footer-container" className="small-logo-footer-html-like">
            <Logo isMini={true} isActive={true} isAi={false} speakingVolume={volume} isUserSpeaking={isSpeaking}/>
        </div>
      )}

      <div id="cam-button-wrapper" className="control-button-wrapper">
        <div id="cam-button" className={cn("control-button cam-button-color", { "disabled": isTimeUp })} onClick={handleCamToggle}>
          {isAppCamActive ? stopCamIcon : cameraIcon}
        </div>
        <div id="switch-camera-button-container" className={cn("switch-camera-button-container", { visible: isAppCamActive && !isSwitchingCamera })}>
          <button id="switch-camera-button" aria-label="Switch Camera" className="switch-camera-button-content group" onClick={handleSwitchCamera} disabled={!isAppCamActive || isSwitchingCamera || isTimeUp}>
            <SvgSwitchCameraIcon/>
          </button>
        </div>
      </div>
    </footer>
  );
};

export default memo(ControlTray);