setup method

Future<void> setup(
  1. Room room
)

Implementation

Future<void> setup(Room room) async {
  if (_room != room) {
    await _cleanUp();
    _room = room;
    _listener = _room!.createListener();
    _listener!
      ..on<LocalTrackPublishedEvent>((event) async {
        if (event.publication.encryptionType == EncryptionType.kNone ||
            isSVCCodec(event.publication.track?.codec ?? '')) {
          // no need to setup frame cryptor
          return;
        }
        var frameCryptor = await _addRtpSender(
            sender: event.publication.track!.sender!,
            identity: event.participant.identity,
            sid: event.publication.sid);
        if (kIsWeb && event.publication.track!.codec != null) {
          await frameCryptor.updateCodec(event.publication.track!.codec!);
        }
        frameCryptor.onFrameCryptorStateChanged = (trackId, state) {
          if (kDebugMode) {
            print(
                'Sender::onFrameCryptorStateChanged: $state, trackId:  $trackId');
          }
          var participant = event.participant;
          [event.participant.events, participant.room.events]
              .emit(TrackE2EEStateEvent(
            participant: participant,
            publication: event.publication,
            state: _e2eeStateFromFrameCryptoState(state),
          ));
        };
      })
      ..on<LocalTrackUnpublishedEvent>((event) async {
        for (var key in _frameCryptors.keys.toList()) {
          if (key.keys.first == event.participant.identity &&
              key.values.first == event.publication.sid) {
            var frameCryptor = _frameCryptors.remove(key);
            await frameCryptor?.setEnabled(false);
            await frameCryptor?.dispose();
          }
        }
      })
      ..on<TrackSubscribedEvent>((event) async {
        var codec = event.publication.mimeType.split('/')[1];
        if (event.publication.encryptionType == EncryptionType.kNone ||
            isSVCCodec(codec)) {
          // no need to setup frame cryptor
          return;
        }
        var frameCryptor = await _addRtpReceiver(
          receiver: event.track.receiver!,
          identity: event.participant.identity,
          sid: event.publication.sid,
        );
        if (kIsWeb) {
          await frameCryptor.updateCodec(codec.toLowerCase());
        }
        frameCryptor.onFrameCryptorStateChanged = (trackId, state) {
          if (kDebugMode) {
            print(
                'Receiver::onFrameCryptorStateChanged: $state, trackId: $trackId');
          }
          var participant = event.participant;
          [event.participant.events, participant.room.events]
              .emit(TrackE2EEStateEvent(
            participant: participant,
            publication: event.publication,
            state: _e2eeStateFromFrameCryptoState(state),
          ));
        };
      })
      ..on<TrackUnsubscribedEvent>((event) async {
        for (var key in _frameCryptors.keys.toList()) {
          if (key.keys.first == event.participant.identity &&
              key.values.first == event.publication.sid) {
            var frameCryptor = _frameCryptors.remove(key);
            await frameCryptor?.setEnabled(false);
            await frameCryptor?.dispose();
          }
        }
      });
  }
}