From 6fa6afa01e495c41a8425ebdfa95e2de1b2fed34 Mon Sep 17 00:00:00 2001 From: Matheo Bonucia Date: Tue, 11 Jun 2024 00:58:03 -0400 Subject: [PATCH] corrigido fluxo, falta polir funcoes --- react-native-webrtc-app/client/App.js | 82 +++++++++------------------ 1 file changed, 27 insertions(+), 55 deletions(-) diff --git a/react-native-webrtc-app/client/App.js b/react-native-webrtc-app/client/App.js index f0e703e..45d0e94 100644 --- a/react-native-webrtc-app/client/App.js +++ b/react-native-webrtc-app/client/App.js @@ -54,7 +54,7 @@ export default function App({}) { const [myRamal, setMyRamal] = useState(null); const [authData, setAuthData] = useState({ protocolo: 'ws', - servidor: '192.168.115.179', + servidor: '192.168.100.14', porta: '8088', ramal: '1100', username: 'matheo', @@ -96,31 +96,6 @@ export default function App({}) { const eventoSip = new EventEmitter(); const {protocolo, servidor, porta, username, ramal, senha} = authData; - useEffect(() => { - const captureLocalMedia = async () => { - try { - const stream = await mediaDevices.getUserMedia({ - audio: true, - video: true, - }); - setLocalStream(stream); - console.log('midia local capture: ', stream); - } catch (error) { - console.error('Error capturing local media: ', error); - } - }; - - captureLocalMedia(); - - return () => { - // Cleanup - if (localStream) { - localStream.getTracks().forEach(track => track.stop()); - setLocalStream(null); - } - }; - }, []); - useEffect(() => { setMyRamal(ramal); @@ -173,32 +148,14 @@ export default function App({}) { console.log('criou peerConnection', peerConnectionRef.current); } - // Verifica se existe uma conexão na nova sessão - if (peerConnectionRef.current) { - // Adiciona a stream da mídia local ao RTCPeerConnection - console.log('ADICIONADO LOCAL STREAM 1====>', localStream); - peerConnectionRef.current.addStream(localStream); - - // Adiciona um ouvinte para o evento addstream - peerConnectionRef.current.addEventListener('addstream', event => { - // Configura o stream remoto - console.log('Midia REMOTA Capturada ADDSTREAM: ', event.stream); - setRemoteStream(event.stream); - }); - } - newSession.on('peerconnection', event => { - const pc = event.peerconnection; - peerConnectionRef.current = pc; - // Adiciona a stream da mídia local ao RTCPeerConnection - console.log('ADICIONADO LOCAL STREAM 2====>', localStream); - - pc.addStream(localStream); - - pc.addEventListener('addstream', event => { - console.log('Midia REMOTA Capturada ONADDSTREAM: ', event.stream); + const peerConnection = event.peerconnection; + //Adiciona um ouvinte para o evento onaddstream + peerConnection.onaddstream = event => { + // Configura o stream remoto + console.log('Midia REMOTA Capturada: ', event.stream); setRemoteStream(event.stream); - }); + }; }); //RECEBENDO UMA CHAMADA @@ -249,7 +206,17 @@ export default function App({}) { }); newSession.on('confirmed', function (confirmed) { - setScreen('WEBRTC_ROOM'); + //Adiciona um ouvinte para o evento onaddstream + if (peerConnectionRef.current) { + let local = peerConnectionRef.current.getLocalStreams()[0]; + setLocalStream(local); + console.log('midia local capture: ', local); + let remoto = peerConnectionRef.current.getRemoteStreams()[0]; + setRemoteStream(remoto); + setScreen('WEBRTC_ROOM'); + } + console.log('remoto:::::::::::', remoteStream); + console.log('local:::::::::::', localStream); }); newSession.on('icecandidate', function (event) { if ( @@ -294,7 +261,6 @@ export default function App({}) { peerConnectionRef.current.close(); peerConnectionRef.current = null; } - setRemoteStream(null); InCallManager.stop(); phone.stop(); }; @@ -312,6 +278,14 @@ export default function App({}) { } }; + if (session) { + session.on('confirmed', () => { + let stream = session.connection.getLocalStreams()[0]; + setLocalStream(stream); + console.log(':::::::::::: ', stream); + }); + } + const handleEndCall = async () => { if (session) { session.terminate(); @@ -338,7 +312,6 @@ export default function App({}) { ended: () => { console.log('Call ended'); InCallManager.stop(); - setRemoteStream(null); if (peerConnectionRef.current) { peerConnectionRef.current.close(); peerConnectionRef.current = null; @@ -405,7 +378,7 @@ export default function App({}) { }; let isFrontCam = true; - const toggleCamera = async () => { + const switchCamera = async () => { try { const videoTrack = await localStream.getVideoTracks()[0]; videoTrack._switchCamera(); @@ -676,7 +649,6 @@ export default function App({}) { }; const WebrtcRoomScreen = () => { - InCallManager.stopRingback(); return (