Spaces:
Sleeping
Sleeping
enhance the UI and better view
Browse files- App.tsx +18 -9
- components/Chat.tsx +171 -0
- components/VideoRoom.tsx +17 -4
- hooks/useWebRTC.ts +20 -5
- server.js +8 -3
App.tsx
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
import React, { useState, useRef, useEffect } from 'react';
|
| 2 |
import { CodeInput } from './components/CodeInput';
|
| 3 |
import { VideoRoom } from './components/VideoRoom';
|
| 4 |
import { AppState, CallState } from './types';
|
|
@@ -23,7 +23,7 @@ const AppContent: React.FC = () => {
|
|
| 23 |
const videoRef = useRef<HTMLVideoElement>(null);
|
| 24 |
const { showToast } = useToast();
|
| 25 |
|
| 26 |
-
const handleCodeComplete = (code: string, name: string = 'Guest', language?: string) => {
|
| 27 |
setState(prev => ({
|
| 28 |
...prev,
|
| 29 |
code,
|
|
@@ -32,7 +32,7 @@ const AppContent: React.FC = () => {
|
|
| 32 |
}));
|
| 33 |
setIsPreJoin(true);
|
| 34 |
startCamera();
|
| 35 |
-
};
|
| 36 |
|
| 37 |
const startCamera = async () => {
|
| 38 |
try {
|
|
@@ -61,7 +61,7 @@ const AppContent: React.FC = () => {
|
|
| 61 |
}
|
| 62 |
};
|
| 63 |
|
| 64 |
-
const handleJoinCall = () => {
|
| 65 |
// Stop the preview stream tracks before joining, as VideoRoom will request its own
|
| 66 |
// Or better yet, pass this stream to VideoRoom? For now, let's stop it to avoid conflicts/double usage if not handled.
|
| 67 |
// Actually, VideoRoom uses useWebRTC which gets its own stream. Let's stop this one.
|
|
@@ -77,22 +77,22 @@ const AppContent: React.FC = () => {
|
|
| 77 |
setState(prev => ({ ...prev, callState: CallState.CONNECTED }));
|
| 78 |
showToast(`Joined room: ${state.code}`, "success");
|
| 79 |
}, 1500);
|
| 80 |
-
};
|
| 81 |
|
| 82 |
-
const handleBack = () => {
|
| 83 |
if (localStream) {
|
| 84 |
localStream.getTracks().forEach(track => track.stop());
|
| 85 |
setLocalStream(null);
|
| 86 |
}
|
| 87 |
setIsPreJoin(false);
|
| 88 |
setState(prev => ({ ...prev, code: '' }));
|
| 89 |
-
};
|
| 90 |
|
| 91 |
-
const handleEndCall = () => {
|
| 92 |
setState({ callState: CallState.IDLE, code: '', userName: '', targetLanguage: '' });
|
| 93 |
setIsPreJoin(false);
|
| 94 |
showToast("Call ended", "info");
|
| 95 |
-
};
|
| 96 |
|
| 97 |
useEffect(() => {
|
| 98 |
if (videoRef.current && localStream) {
|
|
@@ -163,6 +163,13 @@ const AppContent: React.FC = () => {
|
|
| 163 |
</div>
|
| 164 |
</div>
|
| 165 |
</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 166 |
</motion.div>
|
| 167 |
)}
|
| 168 |
|
|
@@ -247,6 +254,8 @@ const AppContent: React.FC = () => {
|
|
| 247 |
userName={state.userName || 'Guest'}
|
| 248 |
targetLanguage={state.targetLanguage}
|
| 249 |
onEndCall={handleEndCall}
|
|
|
|
|
|
|
| 250 |
/>
|
| 251 |
)}
|
| 252 |
</motion.div>
|
|
|
|
| 1 |
+
import React, { useState, useRef, useEffect, useCallback } from 'react';
|
| 2 |
import { CodeInput } from './components/CodeInput';
|
| 3 |
import { VideoRoom } from './components/VideoRoom';
|
| 4 |
import { AppState, CallState } from './types';
|
|
|
|
| 23 |
const videoRef = useRef<HTMLVideoElement>(null);
|
| 24 |
const { showToast } = useToast();
|
| 25 |
|
| 26 |
+
const handleCodeComplete = useCallback((code: string, name: string = 'Guest', language?: string) => {
|
| 27 |
setState(prev => ({
|
| 28 |
...prev,
|
| 29 |
code,
|
|
|
|
| 32 |
}));
|
| 33 |
setIsPreJoin(true);
|
| 34 |
startCamera();
|
| 35 |
+
}, []);
|
| 36 |
|
| 37 |
const startCamera = async () => {
|
| 38 |
try {
|
|
|
|
| 61 |
}
|
| 62 |
};
|
| 63 |
|
| 64 |
+
const handleJoinCall = useCallback(() => {
|
| 65 |
// Stop the preview stream tracks before joining, as VideoRoom will request its own
|
| 66 |
// Or better yet, pass this stream to VideoRoom? For now, let's stop it to avoid conflicts/double usage if not handled.
|
| 67 |
// Actually, VideoRoom uses useWebRTC which gets its own stream. Let's stop this one.
|
|
|
|
| 77 |
setState(prev => ({ ...prev, callState: CallState.CONNECTED }));
|
| 78 |
showToast(`Joined room: ${state.code}`, "success");
|
| 79 |
}, 1500);
|
| 80 |
+
}, [localStream, state.code, showToast]);
|
| 81 |
|
| 82 |
+
const handleBack = useCallback(() => {
|
| 83 |
if (localStream) {
|
| 84 |
localStream.getTracks().forEach(track => track.stop());
|
| 85 |
setLocalStream(null);
|
| 86 |
}
|
| 87 |
setIsPreJoin(false);
|
| 88 |
setState(prev => ({ ...prev, code: '' }));
|
| 89 |
+
}, [localStream]);
|
| 90 |
|
| 91 |
+
const handleEndCall = useCallback(() => {
|
| 92 |
setState({ callState: CallState.IDLE, code: '', userName: '', targetLanguage: '' });
|
| 93 |
setIsPreJoin(false);
|
| 94 |
showToast("Call ended", "info");
|
| 95 |
+
}, [showToast]);
|
| 96 |
|
| 97 |
useEffect(() => {
|
| 98 |
if (videoRef.current && localStream) {
|
|
|
|
| 163 |
</div>
|
| 164 |
</div>
|
| 165 |
</div>
|
| 166 |
+
|
| 167 |
+
{/* Footer */}
|
| 168 |
+
<div className="absolute bottom-8 left-1/2 -translate-x-1/2 text-center">
|
| 169 |
+
<p className="text-sm text-zinc-500">
|
| 170 |
+
Powered by <span className="text-transparent bg-clip-text bg-gradient-to-r from-cyan-400 to-purple-500 font-semibold">Gemini-3 Pro</span> by <span className="text-cyan-400 font-medium">Ameer Azam</span>
|
| 171 |
+
</p>
|
| 172 |
+
</div>
|
| 173 |
</motion.div>
|
| 174 |
)}
|
| 175 |
|
|
|
|
| 254 |
userName={state.userName || 'Guest'}
|
| 255 |
targetLanguage={state.targetLanguage}
|
| 256 |
onEndCall={handleEndCall}
|
| 257 |
+
initialMuted={isMuted}
|
| 258 |
+
initialVideoOff={isVideoOff}
|
| 259 |
/>
|
| 260 |
)}
|
| 261 |
</motion.div>
|
components/Chat.tsx
ADDED
|
@@ -0,0 +1,171 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import React, { useState, useRef, useEffect } from 'react';
|
| 2 |
+
import { Send, MessageSquare, X } from 'lucide-react';
|
| 3 |
+
import { motion, AnimatePresence } from 'framer-motion';
|
| 4 |
+
|
| 5 |
+
interface Message {
|
| 6 |
+
id: number;
|
| 7 |
+
text: string;
|
| 8 |
+
userName: string;
|
| 9 |
+
timestamp: Date;
|
| 10 |
+
isOwn: boolean;
|
| 11 |
+
}
|
| 12 |
+
|
| 13 |
+
interface ChatProps {
|
| 14 |
+
socket: any;
|
| 15 |
+
code: string;
|
| 16 |
+
userName: string;
|
| 17 |
+
}
|
| 18 |
+
|
| 19 |
+
export const Chat: React.FC<ChatProps> = ({ socket, code, userName }) => {
|
| 20 |
+
const [isOpen, setIsOpen] = useState(false);
|
| 21 |
+
const [messages, setMessages] = useState<Message[]>([]);
|
| 22 |
+
const [inputText, setInputText] = useState('');
|
| 23 |
+
const [unreadCount, setUnreadCount] = useState(0);
|
| 24 |
+
const messagesEndRef = useRef<HTMLDivElement>(null);
|
| 25 |
+
|
| 26 |
+
useEffect(() => {
|
| 27 |
+
if (!socket) return;
|
| 28 |
+
|
| 29 |
+
socket.on('chat-message', (data: { text: string, userName: string }) => {
|
| 30 |
+
const newMessage: Message = {
|
| 31 |
+
id: Date.now(),
|
| 32 |
+
text: data.text,
|
| 33 |
+
userName: data.userName,
|
| 34 |
+
timestamp: new Date(),
|
| 35 |
+
isOwn: false,
|
| 36 |
+
};
|
| 37 |
+
setMessages(prev => [...prev, newMessage]);
|
| 38 |
+
if (!isOpen) {
|
| 39 |
+
setUnreadCount(prev => prev + 1);
|
| 40 |
+
}
|
| 41 |
+
});
|
| 42 |
+
|
| 43 |
+
return () => {
|
| 44 |
+
socket.off('chat-message');
|
| 45 |
+
};
|
| 46 |
+
}, [socket, isOpen]);
|
| 47 |
+
|
| 48 |
+
useEffect(() => {
|
| 49 |
+
messagesEndRef.current?.scrollIntoView({ behavior: 'smooth' });
|
| 50 |
+
}, [messages]);
|
| 51 |
+
|
| 52 |
+
useEffect(() => {
|
| 53 |
+
if (isOpen) {
|
| 54 |
+
setUnreadCount(0);
|
| 55 |
+
}
|
| 56 |
+
}, [isOpen]);
|
| 57 |
+
|
| 58 |
+
const handleSend = () => {
|
| 59 |
+
if (!inputText.trim() || !socket) return;
|
| 60 |
+
|
| 61 |
+
const newMessage: Message = {
|
| 62 |
+
id: Date.now(),
|
| 63 |
+
text: inputText,
|
| 64 |
+
userName: userName || 'You',
|
| 65 |
+
timestamp: new Date(),
|
| 66 |
+
isOwn: true,
|
| 67 |
+
};
|
| 68 |
+
|
| 69 |
+
setMessages(prev => [...prev, newMessage]);
|
| 70 |
+
socket.emit('chat-message', { text: inputText, userName: userName || 'Guest', roomId: code });
|
| 71 |
+
setInputText('');
|
| 72 |
+
};
|
| 73 |
+
|
| 74 |
+
const handleKeyPress = (e: React.KeyboardEvent) => {
|
| 75 |
+
if (e.key === 'Enter' && !e.shiftKey) {
|
| 76 |
+
e.preventDefault();
|
| 77 |
+
handleSend();
|
| 78 |
+
}
|
| 79 |
+
};
|
| 80 |
+
|
| 81 |
+
return (
|
| 82 |
+
<>
|
| 83 |
+
{/* Chat Toggle Button */}
|
| 84 |
+
<motion.button
|
| 85 |
+
onClick={() => setIsOpen(!isOpen)}
|
| 86 |
+
className="fixed bottom-28 right-6 z-50 p-4 rounded-full bg-gradient-to-r from-cyan-600 to-blue-600 text-white shadow-lg hover:from-cyan-500 hover:to-blue-500 transition-all"
|
| 87 |
+
whileHover={{ scale: 1.1 }}
|
| 88 |
+
whileTap={{ scale: 0.9 }}
|
| 89 |
+
>
|
| 90 |
+
{isOpen ? <X className="w-6 h-6" /> : <MessageSquare className="w-6 h-6" />}
|
| 91 |
+
{unreadCount > 0 && !isOpen && (
|
| 92 |
+
<span className="absolute -top-1 -right-1 bg-red-500 text-white text-xs rounded-full w-5 h-5 flex items-center justify-center font-bold">
|
| 93 |
+
{unreadCount}
|
| 94 |
+
</span>
|
| 95 |
+
)}
|
| 96 |
+
</motion.button>
|
| 97 |
+
|
| 98 |
+
{/* Chat Panel */}
|
| 99 |
+
<AnimatePresence>
|
| 100 |
+
{isOpen && (
|
| 101 |
+
<motion.div
|
| 102 |
+
initial={{ opacity: 0, x: 100 }}
|
| 103 |
+
animate={{ opacity: 1, x: 0 }}
|
| 104 |
+
exit={{ opacity: 0, x: 100 }}
|
| 105 |
+
className="fixed bottom-28 right-6 z-40 w-80 h-96 bg-zinc-900/95 backdrop-blur-xl border border-white/10 rounded-2xl shadow-2xl flex flex-col overflow-hidden"
|
| 106 |
+
>
|
| 107 |
+
{/* Chat Header */}
|
| 108 |
+
<div className="bg-gradient-to-r from-cyan-600 to-blue-600 p-4 flex items-center justify-between">
|
| 109 |
+
<div className="flex items-center gap-2">
|
| 110 |
+
<MessageSquare className="w-5 h-5 text-white" />
|
| 111 |
+
<h3 className="font-bold text-white">Room Chat</h3>
|
| 112 |
+
</div>
|
| 113 |
+
<span className="text-xs text-white/80">Room {code}</span>
|
| 114 |
+
</div>
|
| 115 |
+
|
| 116 |
+
{/* Messages */}
|
| 117 |
+
<div className="flex-1 overflow-y-auto p-4 space-y-3">
|
| 118 |
+
{messages.length === 0 ? (
|
| 119 |
+
<div className="text-center text-zinc-500 text-sm mt-8">
|
| 120 |
+
No messages yet. Start the conversation!
|
| 121 |
+
</div>
|
| 122 |
+
) : (
|
| 123 |
+
messages.map((msg) => (
|
| 124 |
+
<div
|
| 125 |
+
key={msg.id}
|
| 126 |
+
className={`flex flex-col ${msg.isOwn ? 'items-end' : 'items-start'}`}
|
| 127 |
+
>
|
| 128 |
+
<div className={`max-w-[80%] rounded-2xl px-4 py-2 ${msg.isOwn
|
| 129 |
+
? 'bg-gradient-to-r from-cyan-600 to-blue-600 text-white'
|
| 130 |
+
: 'bg-zinc-800 text-white'
|
| 131 |
+
}`}>
|
| 132 |
+
{!msg.isOwn && (
|
| 133 |
+
<p className="text-xs font-bold mb-1 text-cyan-400">{msg.userName}</p>
|
| 134 |
+
)}
|
| 135 |
+
<p className="text-sm break-words">{msg.text}</p>
|
| 136 |
+
</div>
|
| 137 |
+
<span className="text-[10px] text-zinc-500 mt-1">
|
| 138 |
+
{msg.timestamp.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' })}
|
| 139 |
+
</span>
|
| 140 |
+
</div>
|
| 141 |
+
))
|
| 142 |
+
)}
|
| 143 |
+
<div ref={messagesEndRef} />
|
| 144 |
+
</div>
|
| 145 |
+
|
| 146 |
+
{/* Input */}
|
| 147 |
+
<div className="p-4 border-t border-white/10">
|
| 148 |
+
<div className="flex gap-2">
|
| 149 |
+
<input
|
| 150 |
+
type="text"
|
| 151 |
+
value={inputText}
|
| 152 |
+
onChange={(e) => setInputText(e.target.value)}
|
| 153 |
+
onKeyPress={handleKeyPress}
|
| 154 |
+
placeholder="Type a message..."
|
| 155 |
+
className="flex-1 bg-zinc-800 text-white px-4 py-2 rounded-xl focus:outline-none focus:ring-2 focus:ring-cyan-500 text-sm"
|
| 156 |
+
/>
|
| 157 |
+
<button
|
| 158 |
+
onClick={handleSend}
|
| 159 |
+
disabled={!inputText.trim()}
|
| 160 |
+
className="p-2 rounded-xl bg-gradient-to-r from-cyan-600 to-blue-600 text-white disabled:opacity-50 disabled:cursor-not-allowed hover:from-cyan-500 hover:to-blue-500 transition-all"
|
| 161 |
+
>
|
| 162 |
+
<Send className="w-5 h-5" />
|
| 163 |
+
</button>
|
| 164 |
+
</div>
|
| 165 |
+
</div>
|
| 166 |
+
</motion.div>
|
| 167 |
+
)}
|
| 168 |
+
</AnimatePresence>
|
| 169 |
+
</>
|
| 170 |
+
);
|
| 171 |
+
};
|
components/VideoRoom.tsx
CHANGED
|
@@ -5,6 +5,7 @@ import { useGeminiTranscription } from '../hooks/useGeminiTranscription';
|
|
| 5 |
import { useGeminiDubbing } from '../hooks/useGeminiDubbing';
|
| 6 |
import { useAudioLevel } from '../hooks/useAudioLevel';
|
| 7 |
import { useToast } from './Toast';
|
|
|
|
| 8 |
import { motion, AnimatePresence } from 'framer-motion';
|
| 9 |
|
| 10 |
interface VideoRoomProps {
|
|
@@ -12,6 +13,8 @@ interface VideoRoomProps {
|
|
| 12 |
userName: string;
|
| 13 |
targetLanguage?: string;
|
| 14 |
onEndCall: () => void;
|
|
|
|
|
|
|
| 15 |
}
|
| 16 |
|
| 17 |
interface Caption {
|
|
@@ -20,18 +23,18 @@ interface Caption {
|
|
| 20 |
id: number;
|
| 21 |
}
|
| 22 |
|
| 23 |
-
export const VideoRoom: React.FC<VideoRoomProps> = ({ code, userName, targetLanguage, onEndCall }) => {
|
| 24 |
const localVideoRef = useRef<HTMLVideoElement>(null);
|
| 25 |
const { showToast } = useToast();
|
| 26 |
|
| 27 |
-
const [isMuted, setIsMuted] = useState(
|
| 28 |
-
const [isVideoOff, setIsVideoOff] = useState(
|
| 29 |
const [captions, setCaptions] = useState<Caption[]>([]);
|
| 30 |
const [showCaptions, setShowCaptions] = useState(true);
|
| 31 |
const [participantCount, setParticipantCount] = useState(1);
|
| 32 |
const [peerNames, setPeerNames] = useState<Record<string, string>>({});
|
| 33 |
|
| 34 |
-
const { localStream, remoteStreams, connectionState, replaceAudioTrack, disconnect, socket } = useWebRTC(code, onEndCall);
|
| 35 |
const { isSpeaking: isLocalSpeaking } = useAudioLevel(localStream);
|
| 36 |
|
| 37 |
useEffect(() => {
|
|
@@ -324,6 +327,16 @@ export const VideoRoom: React.FC<VideoRoomProps> = ({ code, userName, targetLang
|
|
| 324 |
<PhoneOff className="w-5 h-5 fill-current" />
|
| 325 |
</button>
|
| 326 |
</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 327 |
</div>
|
| 328 |
);
|
| 329 |
};
|
|
|
|
| 5 |
import { useGeminiDubbing } from '../hooks/useGeminiDubbing';
|
| 6 |
import { useAudioLevel } from '../hooks/useAudioLevel';
|
| 7 |
import { useToast } from './Toast';
|
| 8 |
+
import { Chat } from './Chat';
|
| 9 |
import { motion, AnimatePresence } from 'framer-motion';
|
| 10 |
|
| 11 |
interface VideoRoomProps {
|
|
|
|
| 13 |
userName: string;
|
| 14 |
targetLanguage?: string;
|
| 15 |
onEndCall: () => void;
|
| 16 |
+
initialMuted?: boolean;
|
| 17 |
+
initialVideoOff?: boolean;
|
| 18 |
}
|
| 19 |
|
| 20 |
interface Caption {
|
|
|
|
| 23 |
id: number;
|
| 24 |
}
|
| 25 |
|
| 26 |
+
export const VideoRoom: React.FC<VideoRoomProps> = ({ code, userName, targetLanguage, onEndCall, initialMuted = false, initialVideoOff = false }) => {
|
| 27 |
const localVideoRef = useRef<HTMLVideoElement>(null);
|
| 28 |
const { showToast } = useToast();
|
| 29 |
|
| 30 |
+
const [isMuted, setIsMuted] = useState(initialMuted);
|
| 31 |
+
const [isVideoOff, setIsVideoOff] = useState(initialVideoOff);
|
| 32 |
const [captions, setCaptions] = useState<Caption[]>([]);
|
| 33 |
const [showCaptions, setShowCaptions] = useState(true);
|
| 34 |
const [participantCount, setParticipantCount] = useState(1);
|
| 35 |
const [peerNames, setPeerNames] = useState<Record<string, string>>({});
|
| 36 |
|
| 37 |
+
const { localStream, remoteStreams, connectionState, replaceAudioTrack, disconnect, socket } = useWebRTC(code, onEndCall, initialMuted, initialVideoOff);
|
| 38 |
const { isSpeaking: isLocalSpeaking } = useAudioLevel(localStream);
|
| 39 |
|
| 40 |
useEffect(() => {
|
|
|
|
| 327 |
<PhoneOff className="w-5 h-5 fill-current" />
|
| 328 |
</button>
|
| 329 |
</div>
|
| 330 |
+
|
| 331 |
+
{/* Chat Component */}
|
| 332 |
+
<Chat socket={socket} code={code} userName={userName} />
|
| 333 |
+
|
| 334 |
+
{/* Footer */}
|
| 335 |
+
<div className="absolute bottom-2 left-1/2 -translate-x-1/2 z-30 text-center pointer-events-none">
|
| 336 |
+
<p className="text-xs text-zinc-500">
|
| 337 |
+
vibe coded by <span className="text-transparent bg-clip-text bg-gradient-to-r from-cyan-400 to-purple-500 font-semibold">Gemini-3 Pro</span> By <span className="text-cyan-400 font-medium">Ameer Azam</span>
|
| 338 |
+
</p>
|
| 339 |
+
</div>
|
| 340 |
</div>
|
| 341 |
);
|
| 342 |
};
|
hooks/useWebRTC.ts
CHANGED
|
@@ -17,7 +17,7 @@ export interface WebRTCUse {
|
|
| 17 |
socket: Socket | null;
|
| 18 |
}
|
| 19 |
|
| 20 |
-
export const useWebRTC = (roomId: string, onDisconnect: () => void): WebRTCUse => {
|
| 21 |
const [localStream, setLocalStream] = useState<MediaStream | null>(null);
|
| 22 |
const [remoteStreams, setRemoteStreams] = useState<Map<string, MediaStream>>(new Map());
|
| 23 |
const [connectionState, setConnectionState] = useState<'connecting' | 'connected' | 'failed' | 'disconnected'>('connecting');
|
|
@@ -73,6 +73,9 @@ export const useWebRTC = (roomId: string, onDisconnect: () => void): WebRTCUse =
|
|
| 73 |
});
|
| 74 |
}, []);
|
| 75 |
|
|
|
|
|
|
|
|
|
|
| 76 |
useEffect(() => {
|
| 77 |
// Initialize Socket
|
| 78 |
socketRef.current = io('/', {
|
|
@@ -85,6 +88,10 @@ export const useWebRTC = (roomId: string, onDisconnect: () => void): WebRTCUse =
|
|
| 85 |
if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {
|
| 86 |
navigator.mediaDevices.getUserMedia({ video: true, audio: true })
|
| 87 |
.then((stream) => {
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
localStreamRef.current = stream;
|
| 89 |
setLocalStream(stream);
|
| 90 |
|
|
@@ -118,11 +125,19 @@ export const useWebRTC = (roomId: string, onDisconnect: () => void): WebRTCUse =
|
|
| 118 |
});
|
| 119 |
|
| 120 |
socket.on('user-joined', (payload: { signal: any, callerID: string }) => {
|
| 121 |
-
|
| 122 |
-
|
|
|
|
|
|
|
| 123 |
});
|
| 124 |
|
| 125 |
socket.on('offer', async (payload: { offer: RTCSessionDescriptionInit, from: string }) => {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 126 |
const peer = peersRef.current.get(payload.from);
|
| 127 |
if (peer) {
|
| 128 |
await peer.setRemoteDescription(payload.offer);
|
|
@@ -160,13 +175,13 @@ export const useWebRTC = (roomId: string, onDisconnect: () => void): WebRTCUse =
|
|
| 160 |
|
| 161 |
socket.on('full', () => {
|
| 162 |
alert('Room is full!');
|
| 163 |
-
|
| 164 |
});
|
| 165 |
|
| 166 |
return () => {
|
| 167 |
cleanup();
|
| 168 |
};
|
| 169 |
-
}, [roomId,
|
| 170 |
|
| 171 |
return {
|
| 172 |
localStream,
|
|
|
|
| 17 |
socket: Socket | null;
|
| 18 |
}
|
| 19 |
|
| 20 |
+
export const useWebRTC = (roomId: string, onDisconnect: () => void, initialMuted: boolean = false, initialVideoOff: boolean = false): WebRTCUse => {
|
| 21 |
const [localStream, setLocalStream] = useState<MediaStream | null>(null);
|
| 22 |
const [remoteStreams, setRemoteStreams] = useState<Map<string, MediaStream>>(new Map());
|
| 23 |
const [connectionState, setConnectionState] = useState<'connecting' | 'connected' | 'failed' | 'disconnected'>('connecting');
|
|
|
|
| 73 |
});
|
| 74 |
}, []);
|
| 75 |
|
| 76 |
+
const onDisconnectRef = useRef(onDisconnect);
|
| 77 |
+
onDisconnectRef.current = onDisconnect;
|
| 78 |
+
|
| 79 |
useEffect(() => {
|
| 80 |
// Initialize Socket
|
| 81 |
socketRef.current = io('/', {
|
|
|
|
| 88 |
if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {
|
| 89 |
navigator.mediaDevices.getUserMedia({ video: true, audio: true })
|
| 90 |
.then((stream) => {
|
| 91 |
+
// Apply initial state immediately
|
| 92 |
+
stream.getAudioTracks().forEach(track => track.enabled = !initialMuted);
|
| 93 |
+
stream.getVideoTracks().forEach(track => track.enabled = !initialVideoOff);
|
| 94 |
+
|
| 95 |
localStreamRef.current = stream;
|
| 96 |
setLocalStream(stream);
|
| 97 |
|
|
|
|
| 125 |
});
|
| 126 |
|
| 127 |
socket.on('user-joined', (payload: { signal: any, callerID: string }) => {
|
| 128 |
+
if (localStreamRef.current) {
|
| 129 |
+
const peer = createPeer(payload.callerID, socket.id!, localStreamRef.current);
|
| 130 |
+
peersRef.current.set(payload.callerID, peer);
|
| 131 |
+
}
|
| 132 |
});
|
| 133 |
|
| 134 |
socket.on('offer', async (payload: { offer: RTCSessionDescriptionInit, from: string }) => {
|
| 135 |
+
// If peer doesn't exist (e.g. offer arrived before user-joined, or race condition), create it
|
| 136 |
+
if (!peersRef.current.has(payload.from) && localStreamRef.current) {
|
| 137 |
+
const peer = createPeer(payload.from, socket.id!, localStreamRef.current);
|
| 138 |
+
peersRef.current.set(payload.from, peer);
|
| 139 |
+
}
|
| 140 |
+
|
| 141 |
const peer = peersRef.current.get(payload.from);
|
| 142 |
if (peer) {
|
| 143 |
await peer.setRemoteDescription(payload.offer);
|
|
|
|
| 175 |
|
| 176 |
socket.on('full', () => {
|
| 177 |
alert('Room is full!');
|
| 178 |
+
if (onDisconnectRef.current) onDisconnectRef.current();
|
| 179 |
});
|
| 180 |
|
| 181 |
return () => {
|
| 182 |
cleanup();
|
| 183 |
};
|
| 184 |
+
}, [roomId, cleanup, createPeer, initialMuted, initialVideoOff]); // Removed onDisconnect from deps
|
| 185 |
|
| 186 |
return {
|
| 187 |
localStream,
|
server.js
CHANGED
|
@@ -46,8 +46,8 @@ io.on('connection', (socket) => {
|
|
| 46 |
// Send list of existing users to the new user
|
| 47 |
socket.emit('all-users', otherUsers);
|
| 48 |
|
| 49 |
-
// Notify others that a new user joined
|
| 50 |
-
socket.to(roomId).emit('user-joined', { signal: null, callerID: socket.id });
|
| 51 |
|
| 52 |
} else {
|
| 53 |
socket.emit('full');
|
|
@@ -77,10 +77,15 @@ io.on('connection', (socket) => {
|
|
| 77 |
socket.to(roomId).emit('caption', { text, userName });
|
| 78 |
});
|
| 79 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
// Exchange User Info (Broadcast to room)
|
| 81 |
socket.on('user-info', ({ userName, roomId }) => {
|
| 82 |
// Broadcast to others in the room that this user joined
|
| 83 |
-
socket.to(roomId).emit('user-joined', { userName });
|
| 84 |
// Send user info to room
|
| 85 |
socket.to(roomId).emit('user-info', { userName, id: socket.id });
|
| 86 |
});
|
|
|
|
| 46 |
// Send list of existing users to the new user
|
| 47 |
socket.emit('all-users', otherUsers);
|
| 48 |
|
| 49 |
+
// Notify others that a new user joined - REMOVED to avoid double notification with user-info
|
| 50 |
+
// socket.to(roomId).emit('user-joined', { signal: null, callerID: socket.id });
|
| 51 |
|
| 52 |
} else {
|
| 53 |
socket.emit('full');
|
|
|
|
| 77 |
socket.to(roomId).emit('caption', { text, userName });
|
| 78 |
});
|
| 79 |
|
| 80 |
+
// Relay Chat Messages (Broadcast to room)
|
| 81 |
+
socket.on('chat-message', ({ text, userName, roomId }) => {
|
| 82 |
+
socket.to(roomId).emit('chat-message', { text, userName });
|
| 83 |
+
});
|
| 84 |
+
|
| 85 |
// Exchange User Info (Broadcast to room)
|
| 86 |
socket.on('user-info', ({ userName, roomId }) => {
|
| 87 |
// Broadcast to others in the room that this user joined
|
| 88 |
+
socket.to(roomId).emit('user-joined', { userName, callerID: socket.id });
|
| 89 |
// Send user info to room
|
| 90 |
socket.to(roomId).emit('user-info', { userName, id: socket.id });
|
| 91 |
});
|