ICE restart with SIP.js

868 Views Asked by At

What is the proper procedure to do ICE restart when using SIP.js? (v0.20.0)

This is what I'm trying:

oniceconnectionstatechange: (event) => {

    const newState = sdh.peerConnection.iceConnectionState;
    
    if (newState == 'failed') {
    
        sdh.peerConnection.restartIce();

        sdh.peerConnection.createOffer({'iceRestart': true})
            .then(function(offer) {
                return sdh.peerConnection.setLocalDescription(offer);
            });
    }

}

It seems to execute without an error, but also no result. FireFox debug-tool "about:webrtc" shows "ICE restarts: 0", so I guess it didn't even begin restart.

ps: failed state is induced by restarting RTP Engine (Kamailio setup). After RTP Engine restarts there is still audio for about 20 seconds and only when ICE state changes to "failed" audio stops.

2

There are 2 best solutions below

0
On BEST ANSWER

Found solution that fixed my problem:

sdh.peerConnectionDelegate = {
    oniceconnectionstatechange: (event) => {
        const newState = sdh.peerConnection.iceConnectionState;

        if (newState === 'disconnected') {
            sdh.peerConnection.restartIce();

            sdh.peerConnection.createOffer({'iceRestart': true})
                .then((offer) => {
                    sdh.peerConnection.setLocalDescription(offer);
                    session.sessionDescriptionHandlerModifiersReInvite = [offer];

                    session.invite()
                        .then(() => {
                            session.logger.debug('iceRestart: RE-invite completed');
                        })
                        .catch((error) => {
                            if (error instanceof RequestPendingError) {
                                session.logger.error('iceRestart: RE-invite is already in progress');
                            }
                            throw error;
                        });
                });
        }
    }
};
0
On

Need to handle at both ends, sip js end. no need to createOffer, invite method itself create an offer and wait for ice gathering if we pass the iceRestart=true flag, also we need to handle the WebSocket. we can handle the disconnected event on the session description handler.

oniceconnectionstatechange: (event) => {
  const newState = sdh.peerConnection.iceConnectionState;    
  if (newState === 'disconnected') {
    reconnectTransport();   
  }    
}

and then when new websocket connected then need to send register and then restart the ice.

function iceRestart() {
  console.log("checking ice restart condition");
  if(ctxSip.callActiveID && ctxSip.doIceRestart) {
  let session  = ctxSip.Sessions[ctxSip.callActiveID];
  if(session && session.state == SIP.SessionState.Established) {
    console.log("checking ice restart - ice restarting");
    ctxSip.doIceRestart  =false;
    let sdh = session._sessionDescriptionHandler;
    sdh.peerConnection.restartIce();
    setTimeout(function(){  
      let options = {
        sessionDescriptionHandlerOptions : {
          offerOptions : {iceRestart : true}
        }
      };
      session.invite(options).then(() => {
       session.logger.debug('iceRestart: RE-invite completed');
      }).catch((error) => { 
        console.log('iceRestart: RE-invite in progress');  
      });
    },1000);
  }
}                       
}

also if rtpengine is older than 8.5 then we check check below issue. https://github.com/sipwise/rtpengine/issues/1019 for below commit. https://github.com/sipwise/rtpengine/commit/6b49c8852712b17d039f1b10fc3c56e220ecb133