initWithInvite method

Future<void> initWithInvite(
  1. CallType type,
  2. RTCSessionDescription offer,
  3. SDPStreamMetadata? metadata,
  4. int lifetime,
  5. bool isGroupCall,
)

Implementation

Future<void> initWithInvite(CallType type, RTCSessionDescription offer,
    SDPStreamMetadata? metadata, int lifetime, bool isGroupCall) async {
  if (!isGroupCall) {
    // glare fixes
    final prevCallId = voip.incomingCallRoomId[room.id];
    if (prevCallId != null) {
      // This is probably an outbound call, but we already have a incoming invite, so let's terminate it.
      final prevCall =
          voip.calls[VoipId(roomId: room.id, callId: prevCallId)];
      if (prevCall != null) {
        if (prevCall._inviteOrAnswerSent) {
          Logs().d('[glare] invite or answer sent, lex compare now');
          if (callId.compareTo(prevCall.callId) > 0) {
            Logs().d(
                '[glare] new call $callId needs to be canceled because the older one ${prevCall.callId} has a smaller lex');
            await hangup(reason: CallErrorCode.unknownError);
            voip.currentCID =
                VoipId(roomId: room.id, callId: prevCall.callId);
          } else {
            Logs().d(
                '[glare] nice, lex of newer call $callId is smaller auto accept this here');

            /// These fixes do not work all the time because sometimes the code
            /// is at an unrecoverable stage (invite already sent when we were
            /// checking if we want to send a invite), so commented out answering
            /// automatically to prevent unknown cases
            // await answer();
            // return;
          }
        } else {
          Logs().d(
              '[glare] ${prevCall.callId} was still preparing prev call, nvm now cancel it');
          await prevCall.hangup(reason: CallErrorCode.unknownError);
        }
      }
    }
  }

  await _preparePeerConnection();
  if (metadata != null) {
    _updateRemoteSDPStreamMetadata(metadata);
  }
  await pc!.setRemoteDescription(offer);

  /// only add local stream if it is not a group call.
  if (!isGroupCall) {
    final stream = await _getUserMedia(type);
    if (stream != null) {
      await addLocalStream(stream, SDPStreamMetadataPurpose.Usermedia);
    } else {
      // we don't have a localstream, call probably crashed
      // for sanity
      if (state == CallState.kEnded) {
        return;
      }
    }
  }

  setCallState(CallState.kRinging);

  _ringingTimer = Timer(CallTimeouts.callInviteLifetime, () {
    if (state == CallState.kRinging) {
      Logs().v('[VOIP] Call invite has expired. Hanging up.');

      fireCallEvent(CallStateChange.kHangup);
      hangup(reason: CallErrorCode.inviteTimeout);
    }
    _ringingTimer?.cancel();
    _ringingTimer = null;
  });
}