leaveRoom method

  1. @override
Future<void> leaveRoom()
override

Implementation

@override
Future<void> leaveRoom() async {
  try {
    if (_mParticipant == null) return;

    if (_currentRoomId != null) {
      _wsEmitter.leaveRoom(_currentRoomId!);
    }

    _resetRoomState();

    final disposeOperations = <Future>[];

    for (final subscriber in _remoteSubscribers.values) {
      disposeOperations.add(subscriber.dispose());
    }

    if (_localCameraStream != null) {
      final tracks = _localCameraStream!.getTracks();
      for (final track in tracks) {
        track.stop();
      }
      disposeOperations.add(_localCameraStream!.dispose());
    }

    if (_mParticipant != null) {
      disposeOperations.add(_mParticipant!.dispose());
    }

    disposeOperations.addAll([
      stopScreenShare(stayInRoom: false),
    ]);

    await Future.wait(disposeOperations);

    // Clear collections efficiently
    _remoteSubscribers.clear();
    _mParticipant = null;
    _localCameraStream = null;
    _videoStats.dispose();
    _audioStats.dispose();
    _e2eeManager.dispose();

    _notify(CallbackEvents.roomEnded);

    // Clear for next time
    disableVirtualBg(reset: true);
  } catch (error) {
    WaterbusLogger().bug(error.toString());
  }
}