@@ -338,6 +338,44 @@ export const VoipProvider: React.FC<VoipProviderProps> = ({ children }) => {
338338 setIsMuted ( false ) ;
339339 } , [ stopVoiceActivityDetection ] ) ;
340340
341+ // Initialize peer connections with a list of participants
342+ const initializePeerConnections = useCallback ( async ( callId : string , currentParticipants : VoipParticipant [ ] ) => {
343+ if ( ! user || ! socket || ! connected ) return ;
344+
345+ console . log ( '[VOIP] Initializing peer connections for participants:' , currentParticipants . length ) ;
346+
347+ for ( const participant of currentParticipants ) {
348+ // Skip self
349+ if ( participant . user_id === user . id ) continue ;
350+
351+ try {
352+ // If we already have a connection, check its state
353+ if ( peerConnectionsRef . current . has ( participant . user_id ) ) {
354+ const pc = peerConnectionsRef . current . get ( participant . user_id ) ?. connection ;
355+ if ( pc && ( pc . connectionState === 'connected' || pc . connectionState === 'connecting' ) ) {
356+ console . log ( `[VOIP] Connection already healthy for ${ participant . user_id } ` ) ;
357+ continue ;
358+ }
359+ // Close unhealthy connection
360+ if ( pc ) pc . close ( ) ;
361+ }
362+
363+ console . log ( `[VOIP] Creating offer for ${ participant . user_id } ` ) ;
364+ const pc = createPeerConnection ( participant . user_id ) ;
365+ const offer = await pc . createOffer ( ) ;
366+ await pc . setLocalDescription ( offer ) ;
367+
368+ socket . emit ( 'voip_offer' , {
369+ call_id : callId ,
370+ target_user_id : participant . user_id ,
371+ offer : offer
372+ } ) ;
373+ } catch ( error ) {
374+ console . error ( `[VOIP] Failed to create offer for ${ participant . user_id } :` , error ) ;
375+ }
376+ }
377+ } , [ user , socket , connected , createPeerConnection ] ) ;
378+
341379 // Create call
342380 const createCall = useCallback ( async ( roomId : string , roomType : 'group' | 'dm' , roomName ?: string ) => {
343381 if ( ! socket || ! connected ) {
@@ -585,24 +623,8 @@ export const VoipProvider: React.FC<VoipProviderProps> = ({ children }) => {
585623 }
586624 setConnectionStatus ( 'connected' ) ;
587625
588- // Create offers to all existing participants
589- for ( const participant of data . call . participants || [ ] ) {
590- if ( participant . user_id !== user ?. id ) {
591- try {
592- const pc = createPeerConnection ( participant . user_id ) ;
593- const offer = await pc . createOffer ( ) ;
594- await pc . setLocalDescription ( offer ) ;
595-
596- socket . emit ( 'voip_offer' , {
597- call_id : data . call . id ,
598- target_user_id : participant . user_id ,
599- offer : offer
600- } ) ;
601- } catch ( error ) {
602- console . error ( '[VOIP] Failed to create offer:' , error ) ;
603- }
604- }
605- }
626+ // Initialize connections with all existing participants
627+ await initializePeerConnections ( data . call . id , data . call . participants || [ ] ) ;
606628 } ,
607629
608630 'voip_user_joined' : async ( data ) => {
@@ -835,6 +857,9 @@ export const VoipProvider: React.FC<VoipProviderProps> = ({ children }) => {
835857 localStreamRef . current = await getUserMedia ( savedDeviceId || undefined ) ;
836858 startVoiceActivityDetection ( localStreamRef . current ) ;
837859 console . log ( '[VOIP] Reconnected to call:' , data . call . id ) ;
860+
861+ // Initialize connections with participants
862+ await initializePeerConnections ( data . call . id , data . call . participants || [ ] ) ;
838863 } catch ( error ) {
839864 console . error ( '[VOIP] Failed to restore microphone on reconnection:' , error ) ;
840865 }
@@ -916,7 +941,8 @@ export const VoipProvider: React.FC<VoipProviderProps> = ({ children }) => {
916941 const handleReconnect = ( ) => {
917942 if ( activeCall ) {
918943 console . log ( '[VOIP] Socket reconnected, restoring call...' ) ;
919- socket . emit ( 'voip_heartbeat' , { call_id : activeCall . id } ) ;
944+ // Emit join_call to ensure socket is re-added to the signaling room
945+ socket . emit ( 'voip_join_call' , { call_id : activeCall . id } ) ;
920946 setConnectionStatus ( 'connected' ) ;
921947 }
922948 } ;
0 commit comments