livekit / client-sdk-flutter

Flutter Client SDK for LiveKit
https://docs.livekit.io
Apache License 2.0
264 stars 132 forks source link

Frequent onRoomDidUpdate Callback Triggering in Livekit Flutter SDK ver2.2.2 #590

Closed kendo6666 closed 1 month ago

kendo6666 commented 1 month ago

We have observed that the _onRoomDidUpdate callback is being triggered almost every second, which leads to the _sortParticipants method being called frequently. This behavior is causing performance concerns in our application.

Code Snippet:

 @override
  Future<void> connect() async {
    final url = certificate.liveURL!;
    final token = certificate.token!;
    final busyLineUsers = certificate.busyLineUserIDList ?? [];
    if (busyLineUsers.isNotEmpty) {
      widget.onBusyLine?.call();
      widget.onClose?.call();
      return;
    }
    // Try to connect to a room
    // This will throw an Exception if it fails for any reason.
    try {
      //create new room
      _room = Room();

      E2EEOptions? e2eeOptions;
      if (widget.e2eeKey != null && widget.e2eeKey!.isNotEmpty) {
        final keyProvider = await BaseKeyProvider.create();
        e2eeOptions = E2EEOptions(keyProvider: keyProvider);
        await keyProvider.setKey(widget.e2eeKey!);
      }

      await Hardware.instance.setPreferSpeakerOutput(true);

      // Create a Listener before connecting
      _listener = _room?.createListener();
      // Try to connect to the room
      // This will throw an Exception if it fails for any reason.
      await _room?.connect(url, token,
          roomOptions: RoomOptions(
              dynacast: true,
              adaptiveStream: true,
              defaultCameraCaptureOptions: const CameraCaptureOptions(params: VideoParametersPresets.h720_169),
              defaultVideoPublishOptions: VideoPublishOptions(
                  simulcast: true,
                  videoCodec: e2eeOptions == null ? 'VP8' : 'VP9',
                  backupVideoCodec: BackupVideoCodec(),
                  videoEncoding: const VideoEncoding(
                    maxBitrate: 5 * 1000 * 1000,
                    maxFramerate: 18,
                  )),
            e2eeOptions: e2eeOptions));
      if (!mounted) return;
      _room?.addListener(_onRoomDidUpdate);
      if (null != _listener) _setUpListeners();
      if (null != _room) roomDidUpdateSubject.add(_room!);

  void _onRoomDidUpdate() {
    _sortParticipants();
  }

Environment: Livekit SDK Version: 2.2.2 Device/OS: iOS

Observed Behavior: The _onRoomDidUpdate callback is being triggered almost every second. This frequent triggering leads to the _sortParticipants method being called repeatedly, causing performance issues.

Is it because I used the SDK incorrectly?

Thank you for your attention to this matter. Best regard

cloudwebrtc commented 1 month ago

_listener contains a collection of all events. You need to subdivide different events to listen.

for more information, see event types

    _listener
    ..on<RoomDisconnectedEvent>((event) async {
    })
    ..on<ParticipantEvent>((event) {
      print('Participant event');
    })
    ..on<RoomRecordingStatusChanged>((event) {

    })
    ..on<RoomAttemptReconnectEvent>((event) {
    })
    ..on<LocalTrackSubscribedEvent>((event) {
    })
    ..on<LocalTrackPublishedEvent>((_) => _sortParticipants())
    ..on<LocalTrackUnpublishedEvent>((_) => _sortParticipants())
    ..on<TrackSubscribedEvent>((_) => _sortParticipants())
    ..on<TrackUnsubscribedEvent>((_) => _sortParticipants())
    ..on<TrackE2EEStateEvent>(_onE2EEStateEvent)
    ..on<ParticipantNameUpdatedEvent>((event) {
    })
    ..on<ParticipantMetadataUpdatedEvent>((event) {
    })
    ..on<RoomMetadataChangedEvent>((event) {
    })
    ..on<DataReceivedEvent>((event) {
    })
    ..on<AudioPlaybackStatusChanged>((event) async {
    });