handleRemoteCandidate method

void handleRemoteCandidate(
  1. String callId,
  2. String candidateStr,
  3. String sdpMid,
  4. int sdpMLineIndex,
)

Handles a remote ICE candidate received via trickle ICE

Implementation

void handleRemoteCandidate(
  String callId,
  String candidateStr,
  String sdpMid,
  int sdpMLineIndex,
) {
  try {
    GlobalLogger().i(
      'Peer :: Handling remote candidate for call $callId: $candidateStr',
    );

    // Find the session for this call
    final Session? session = _sessions[_selfId];

    if (session != null && session.peerConnection != null) {
      // Create RTCIceCandidate from the received candidate string
      final candidate = RTCIceCandidate(
        candidateStr,
        sdpMid,
        sdpMLineIndex,
      );

      // Add the candidate to the peer connection
      session.peerConnection!.addCandidate(candidate).then((_) {
        GlobalLogger().i('Peer :: Successfully added remote candidate');
      }).catchError((error) {
        GlobalLogger().e('Peer :: Error adding remote candidate: $error');
      });
    } else {
      GlobalLogger().w(
        'Peer :: No session or peer connection available for call $callId',
      );
      // Store the candidate for later if session is not ready yet
      final Session? pendingSession = _sessions[_selfId];
      if (pendingSession != null) {
        pendingSession.remoteCandidates.add(
          RTCIceCandidate(
            candidateStr,
            sdpMid,
            sdpMLineIndex,
          ),
        );
        GlobalLogger()
            .i('Peer :: Stored remote candidate for later processing');
      }
    }
  } catch (e) {
    GlobalLogger().e('Peer :: Error handling remote candidate: $e');
  }
}