VoIP constructor

VoIP(
  1. Client client,
  2. WebRTCDelegate delegate
)

Implementation

VoIP(this.client, this.delegate) : super() {
  // to populate groupCalls with already present calls
  for (final room in client.rooms) {
    if (room.activeGroupCallEvents.isNotEmpty) {
      for (final groupCall in room.activeGroupCallEvents) {
        createGroupCallFromRoomStateEvent(groupCall,
            emitHandleNewGroupCall: false);
      }
    }
  }

  client.onCallInvite.stream
      .listen((event) => _handleEvent(event, onCallInvite));
  client.onCallAnswer.stream
      .listen((event) => _handleEvent(event, onCallAnswer));
  client.onCallCandidates.stream
      .listen((event) => _handleEvent(event, onCallCandidates));
  client.onCallHangup.stream
      .listen((event) => _handleEvent(event, onCallHangup));
  client.onCallReject.stream
      .listen((event) => _handleEvent(event, onCallReject));
  client.onCallNegotiate.stream
      .listen((event) => _handleEvent(event, onCallNegotiate));
  client.onCallReplaces.stream
      .listen((event) => _handleEvent(event, onCallReplaces));
  client.onCallSelectAnswer.stream
      .listen((event) => _handleEvent(event, onCallSelectAnswer));
  client.onSDPStreamMetadataChangedReceived.stream.listen(
      (event) => _handleEvent(event, onSDPStreamMetadataChangedReceived));
  client.onAssertedIdentityReceived.stream
      .listen((event) => _handleEvent(event, onAssertedIdentityReceived));

  client.onRoomState.stream.listen(
    (event) async {
      if ([
        EventTypes.GroupCallPrefix,
        EventTypes.GroupCallMemberPrefix,
      ].contains(event.type)) {
        Logs().v('[VOIP] onRoomState: type ${event.toJson()}.');
        await onRoomStateChanged(event);
      }
    },
  );

  client.onToDeviceEvent.stream.listen((event) async {
    Logs().v('[VOIP] onToDeviceEvent: type ${event.toJson()}.');

    if (event.type == 'org.sdn.call_duplicate_session') {
      Logs().v('[VOIP] onToDeviceEvent: duplicate session.');
      return;
    }

    final confId = event.content['conf_id'];
    final groupCall = groupCalls[confId];
    if (groupCall == null) {
      Logs().d('[VOIP] onToDeviceEvent: groupCall is null.');
      return;
    }
    final roomId = groupCall.room.id;
    final senderId = event.senderId;
    final content = event.content;
    switch (event.type) {
      case EventTypes.CallInvite:
        await onCallInvite(roomId, senderId, content);
        break;
      case EventTypes.CallAnswer:
        await onCallAnswer(roomId, senderId, content);
        break;
      case EventTypes.CallCandidates:
        await onCallCandidates(roomId, senderId, content);
        break;
      case EventTypes.CallHangup:
        await onCallHangup(roomId, senderId, content);
        break;
      case EventTypes.CallReject:
        await onCallReject(roomId, senderId, content);
        break;
      case EventTypes.CallNegotiate:
        await onCallNegotiate(roomId, senderId, content);
        break;
      case EventTypes.CallReplaces:
        await onCallReplaces(roomId, senderId, content);
        break;
      case EventTypes.CallSelectAnswer:
        await onCallSelectAnswer(roomId, senderId, content);
        break;
      case EventTypes.CallSDPStreamMetadataChanged:
      case EventTypes.CallSDPStreamMetadataChangedPrefix:
        await onSDPStreamMetadataChangedReceived(roomId, senderId, content);
        break;
      case EventTypes.CallAssertedIdentity:
        await onAssertedIdentityReceived(roomId, senderId, content);
        break;
    }
  });

  delegate.mediaDevices.ondevicechange = _onDeviceChange;
}