/* eslint-disable */ // [TSM.ID].[11031972] -- All Rights Reserved. Proprietary & Confidential. import React, { useEffect, useRef, useState, useCallback } from 'react'; import { Socket } from '../lib/zero-socket'; interface QuantumCallProps { room: string; socket: Socket | null; username: string; onClose: () => void; isAudioOnly?: boolean; } export function QuantumP2PCall({ room, socket, username, onClose, isAudioOnly = false }: QuantumCallProps) { const localVideoRef = useRef(null); const remoteVideoRef = useRef(null); const pcRef = useRef(null); const localStreamRef = useRef(null); const remoteStreamRef = useRef(null); // Audio Analyser Refs const audioContextRef = useRef(null); const analyserRef = useRef(null); const dataArrayRef = useRef(null); const animationFrameRef = useRef(0); const [isMicOn, setIsMicOn] = useState(true); const [isCameraOn, setIsCameraOn] = useState(!isAudioOnly); const [connectionState, setConnectionState] = useState('Membuka Terowongan Kuantum...'); // Audio Aura Level (0 to 100) const [remoteAudioLevel, setRemoteAudioLevel] = useState(0); // Zero-UI Cinematic Mode const [showControls, setShowControls] = useState(true); const idleTimerRef = useRef(null); // Draggable PiP (Picture in Picture) const [pipPos, setPipPos] = useState({ x: window.innerWidth - 220, y: window.innerHeight - 320 }); const [isDragging, setIsDragging] = useState(false); const dragOffset = useRef({ x: 0, y: 0 }); const resetIdleTimer = useCallback(() => { setShowControls(true); if (idleTimerRef.current) clearTimeout(idleTimerRef.current); idleTimerRef.current = setTimeout(() => setShowControls(false), 3000); }, []); useEffect(() => { window.addEventListener('mousemove', resetIdleTimer); resetIdleTimer(); return () => { window.removeEventListener('mousemove', resetIdleTimer); if (idleTimerRef.current) clearTimeout(idleTimerRef.current); }; }, [resetIdleTimer]); useEffect(() => { if (!socket) return; // Phase 83: Omni-Relay Inject const configuration = { iceServers: [ { urls: 'stun:stun.l.google.com:19302' }, { urls: 'stun:stun1.l.google.com:19302' }, { urls: 'turn:160.187.143.172:3478', username: 'xcu ULTRA', credential: 'quantum_mesh' } ] }; const pc = new RTCPeerConnection(configuration); pcRef.current = pc; pc.onicecandidate = (event) => { if (event.candidate) { socket.emit('quantum_candidate', { target: getTargetFromRoom(room), sender: username, candidate: event.candidate, room }); } }; pc.onconnectionstatechange = () => { setConnectionState(pc.connectionState); }; pc.ontrack = (event) => { if (remoteVideoRef.current && event.streams[0]) { remoteStreamRef.current = event.streams[0]; remoteVideoRef.current.srcObject = event.streams[0]; setupAudioAnalyser(event.streams[0]); } }; navigator.mediaDevices.getUserMedia({ video: !isAudioOnly ? { width: { ideal: 1280 }, height: { ideal: 720 } } : false, audio: true }).then((stream) => { localStreamRef.current = stream; if (localVideoRef.current && !isAudioOnly) { localVideoRef.current.srcObject = stream; } stream.getTracks().forEach((track) => pc.addTrack(track, stream)); createOffer(); }).catch(e => { console.error("Gagal mengakses media:", e); setConnectionState("Akses Kamera/Mic Ditolak!"); }); socket.on('quantum_offer_received', async (data: any) => { if (data.caller !== username && pcRef.current) { await pcRef.current.setRemoteDescription(new RTCSessionDescription(data.sdp)); const answer = await pcRef.current.createAnswer(); await pcRef.current.setLocalDescription(answer); socket.emit('quantum_answer', { target: data.caller, responder: username, sdp: answer, room }); } }); socket.on('quantum_answer_received', async (data: any) => { if (data.responder !== username && pcRef.current) { await pcRef.current.setRemoteDescription(new RTCSessionDescription(data.sdp)); } }); socket.on('quantum_candidate_received', async (data: any) => { if (data.sender !== username && pcRef.current) { try { await pcRef.current.addIceCandidate(new RTCIceCandidate(data.candidate)); } catch(e) { console.error(e); } } }); socket.on('quantum_call_ended_broadcast', (data: any) => { if (data.sender !== username) { handleEndCall(); } }); return () => { socket.off('quantum_offer_received'); socket.off('quantum_answer_received'); socket.off('quantum_candidate_received'); socket.off('quantum_call_ended_broadcast'); cleanup(); }; }, [socket, room]); const setupAudioAnalyser = (stream: MediaStream) => { try { const audioCtx = new (window.AudioContext || (window as any).webkitAudioContext)(); const analyser = audioCtx.createAnalyser(); analyser.fftSize = 256; const source = audioCtx.createMediaStreamSource(stream); source.connect(analyser); audioContextRef.current = audioCtx; analyserRef.current = analyser; dataArrayRef.current = new Uint8Array(analyser.frequencyBinCount); const updateAura = () => { if (!analyserRef.current || !dataArrayRef.current) return; analyserRef.current.getByteFrequencyData(dataArrayRef.current as any); let sum = 0; for (let i = 0; i < dataArrayRef.current.length; i++) { sum += dataArrayRef.current[i]; } const average = sum / dataArrayRef.current.length; // Normalize 0-100 const level = Math.min(100, Math.round((average / 255) * 100 * 2)); setRemoteAudioLevel(level); animationFrameRef.current = requestAnimationFrame(updateAura); }; updateAura(); } catch (e) { console.error("Audio Context Error", e); } }; const cleanup = () => { if (animationFrameRef.current) cancelAnimationFrame(animationFrameRef.current); if (audioContextRef.current) audioContextRef.current.close(); if (localStreamRef.current) localStreamRef.current.getTracks().forEach(track => track.stop()); if (pcRef.current) pcRef.current.close(); }; const createOffer = async () => { if (!pcRef.current || !socket) return; try { const offer = await pcRef.current.createOffer(); await pcRef.current.setLocalDescription(offer); socket.emit('quantum_offer', { target: getTargetFromRoom(room), caller: username, sdp: offer, room }); } catch (e) { console.error("Offer creation failed", e); } }; const getTargetFromRoom = (roomId: string) => { if (roomId.startsWith('DM_')) { const users = roomId.replace('DM_', '').split('_'); return users.find(u => u !== username) || ''; } return ''; }; const toggleMic = () => { if (localStreamRef.current) { localStreamRef.current.getAudioTracks().forEach(track => track.enabled = !isMicOn); setIsMicOn(!isMicOn); } }; const toggleCamera = () => { if (localStreamRef.current) { localStreamRef.current.getVideoTracks().forEach(track => track.enabled = !isCameraOn); setIsCameraOn(!isCameraOn); } }; const handleEndCall = () => { if (socket) { socket.emit('quantum_call_ended', { sender: username, room, target: getTargetFromRoom(room) }); } cleanup(); onClose(); }; // Drag Logic const handleMouseDown = (e: React.MouseEvent) => { setIsDragging(true); dragOffset.current = { x: e.clientX - pipPos.x, y: e.clientY - pipPos.y }; }; const handleMouseMove = (e: React.MouseEvent) => { if (!isDragging) return; setPipPos({ x: e.clientX - dragOffset.current.x, y: e.clientY - dragOffset.current.y }); }; const handleMouseUp = () => setIsDragging(false); // Deep Sapphire Blue Aura Calculation const auraBlur = 20 + (remoteAudioLevel * 0.8); const auraSpread = remoteAudioLevel * 0.5; const boxShadowStyle = `0 0 ${auraBlur}px ${auraSpread}px rgba(15, 82, 186, ${remoteAudioLevel > 10 ? 0.6 : 0.1})`; return (
{/* Status Bar */}
{connectionState}
{/* Main Remote Video (Deep Sapphire Blue Aura) */}
{/* Glassmorphism Border */}
{/* Draggable PiP (Local Video) */}
{isCameraOn ? (
{/* Cinematic Controls (Zero-UI) */}
{/* Mic Toggle */} {/* Camera Toggle */} {/* End Call */}
); }