startIceRenegotiation method

Future<void> startIceRenegotiation(
  1. String callId,
  2. String sessionId
)

Starts ICE renegotiation process when ICE connection fails

Implementation

Future<void> startIceRenegotiation(String callId, String sessionId) async {
  try {
    GlobalLogger().i('Peer :: Starting ICE renegotiation for call: $callId');
    if (_sessions[sessionId] != null) {
      onCallStateChange?.call(_sessions[sessionId]!, CallState.renegotiation);
      final peerConnection = _sessions[sessionId]?.peerConnection;
      if (peerConnection == null) {
        GlobalLogger()
            .e('Peer :: No peer connection found for session: $sessionId');
        return;
      }

      // Create constraints with IceRestart flag to force ICE restart
      final constraints = {
        'mandatory': {'IceRestart': true},
        'optional': [],
      };

      // Create new offer with ICE restart enabled
      final offer = await peerConnection.createOffer(constraints);

      // Set the local description with the new local SDP
      await peerConnection.setLocalDescription(offer);

      GlobalLogger().i(
        'Peer :: Created new offer with ICE restart, waiting for ICE candidates...',
      );

      // Set up callback for when negotiation is complete
      _setOnNegotiationComplete(() async {
        // Get the complete SDP with ICE candidates from the peer connection
        final localDescription = await peerConnection.getLocalDescription();
        if (localDescription != null && localDescription.sdp != null) {
          _sendUpdateMediaMessage(callId, sessionId, localDescription.sdp!);
        } else {
          GlobalLogger()
              .e('Peer :: No local description found with ICE candidates');
        }
      });

      // Start negotiation timer
      _startNegotiationTimer();
    } else {
      GlobalLogger().e('Peer :: No session found for ID: $sessionId');
    }
  } catch (e) {
    GlobalLogger().e('Peer :: Error during ICE renegotiation: $e');
  }
}