1

On network reconnection, we are trying to create an offer with a parameter:

iceRestart : true

But, at the receiver user its throwing an error:

InvalidStateError: Failed to set remote offer sdp: Called in wrong state: kHaveLocalOffer.

Basically, its trying to create a new offer instead of restarting the existing connection. What is the correct way to implement iceRestart in createOffer method?

self.constraints = [[RTCMediaConstraints alloc] 
initWithMandatoryConstraints:
@[
[[RTCPair alloc] initWithKey:@"OfferToReceiveAudio" 
value:@"true"],
[[RTCPair alloc] initWithKey:@"OfferToReceiveVideo" 
 value:@"true"],
[[RTCPair alloc] initWithKey:@"iceRestart" value:@"true"]
] optionalConstraints:nil];
[_peerConnection createOfferWithDelegate:self 
constraints:self.constraints];
Razi Tiwana
  • 1,425
  • 2
  • 13
  • 16

1 Answers1

3

InvalidStateError: kHaveLocalOffer might occur for example when you set remote SDP offer instead of answer after setting local SDP offer in the same RTCPeerConnection.

As presented in the diagram below, WebRTC can automatically recover from the disconnected state if the network conditions of both peers didn't change. So you should perform ice restart only if the iceConnectionState switched to failed or if you are sure that your device has switched the network and obtained different IP.

enter image description here

The simplest way to implement reconnects from failed state, is to define that only one peer will do the iceRestart offer, for example, the one who initiated the connection.

Some javascript pseudocode:

this.rtcPeerConnection.oniceconnectionstatechange = () => {
      if (this.rtcPeerConnection.iceConnectionState === 'failed' && this.isConnectionInitializer) {
          // createOffer({iceRestart: true})
          // set offer as local description
          // send offer to peer
      }
};