From 02dcc51387beee2860acda67bcb216c30166735d Mon Sep 17 00:00:00 2001 From: cloudwebrtc Date: Fri, 22 Dec 2023 13:30:51 +0800 Subject: [PATCH] rename some parameters. --- lib/src/core/room.dart | 41 ++++++++-------- lib/src/participant/local.dart | 27 +++++++++++ lib/src/participant/participant.dart | 70 +++++++++++++++++++--------- lib/src/participant/remote.dart | 31 ++++++++++-- test/core/room_e2e_test.dart | 4 +- 5 files changed, 123 insertions(+), 50 deletions(-) diff --git a/lib/src/core/room.dart b/lib/src/core/room.dart index 3d635a970..3fab398c4 100644 --- a/lib/src/core/room.dart +++ b/lib/src/core/room.dart @@ -62,9 +62,9 @@ class Room extends DisposableChangeNotifier with EventsEmittable { RoomOptions get roomOptions => engine.roomOptions; /// map of SID to RemoteParticipant - UnmodifiableMapView get participants => - UnmodifiableMapView(_participants); - final _participants = {}; + UnmodifiableMapView get remoteParticipants => + UnmodifiableMapView(_remoteParticipants); + final _remoteParticipants = {}; /// the current participant LocalParticipant? get localParticipant => _localParticipant; @@ -314,7 +314,7 @@ class Room extends DisposableChangeNotifier with EventsEmittable { 'allowed:${event.allowed}'); // find participant - final participant = _participants[event.participantSid]; + final participant = _remoteParticipants[event.participantSid]; if (participant == null) { return; } @@ -364,9 +364,9 @@ class Room extends DisposableChangeNotifier with EventsEmittable { events.emit(const RoomRestartingEvent()); // clean up RemoteParticipants - var copy = _participants.values.toList(); + var copy = _remoteParticipants.values.toList(); - _participants.clear(); + _remoteParticipants.clear(); _activeSpeakers.clear(); // reset params _name = null; @@ -386,7 +386,7 @@ class Room extends DisposableChangeNotifier with EventsEmittable { // re-publish all tracks await localParticipant?.rePublishAllTracks(); - for (var participant in participants.values) { + for (var participant in remoteParticipants.values) { for (var pub in participant.trackPublications.values) { if (pub.subscribed) { pub.sendUpdateTrackSettings(); @@ -462,7 +462,7 @@ class Room extends DisposableChangeNotifier with EventsEmittable { RemoteParticipant _getOrCreateRemoteParticipant( String sid, lk_models.ParticipantInfo? info) { - RemoteParticipant? participant = _participants[sid]; + RemoteParticipant? participant = _remoteParticipants[sid]; if (participant != null) { if (info != null) { participant.updateFromInfo(info); @@ -485,7 +485,7 @@ class Room extends DisposableChangeNotifier with EventsEmittable { ); } - _participants[sid] = participant; + _remoteParticipants[sid] = participant; return participant; } @@ -500,7 +500,7 @@ class Room extends DisposableChangeNotifier with EventsEmittable { continue; } - final isNew = !_participants.containsKey(info.sid); + final isNew = !_remoteParticipants.containsKey(info.sid); if (info.state == lk_models.ParticipantInfo_State.DISCONNECTED && !isNew) { @@ -532,7 +532,7 @@ class Room extends DisposableChangeNotifier with EventsEmittable { }; for (final speaker in speakers) { - Participant? p = _participants[speaker.sid]; + Participant? p = _remoteParticipants[speaker.sid]; if (speaker.sid == localParticipant?.sid) p = localParticipant; if (p == null) continue; @@ -560,7 +560,7 @@ class Room extends DisposableChangeNotifier with EventsEmittable { // localParticipant & remote participants final allParticipants = { if (localParticipant != null) localParticipant!.sid: localParticipant!, - ..._participants, + ..._remoteParticipants, }; for (final speaker in speakers) { @@ -592,7 +592,7 @@ class Room extends DisposableChangeNotifier with EventsEmittable { if (entry.participantSid == localParticipant?.sid) { participant = localParticipant; } else { - participant = _participants[entry.participantSid]; + participant = _remoteParticipants[entry.participantSid]; } if (participant != null) { @@ -606,7 +606,7 @@ class Room extends DisposableChangeNotifier with EventsEmittable { List updates) async { for (final update in updates) { // try to find RemoteParticipant - final participant = participants[update.participantSid]; + final participant = remoteParticipants[update.participantSid]; if (participant == null) continue; // try to find RemoteTrackPublication final trackPublication = participant.trackPublications[update.trackSid]; @@ -626,7 +626,8 @@ class Room extends DisposableChangeNotifier with EventsEmittable { final senderSid = dataPacketEvent.packet.participantSid; RemoteParticipant? senderParticipant; if (senderSid.isNotEmpty) { - senderParticipant = participants[dataPacketEvent.packet.participantSid]; + senderParticipant = + remoteParticipants[dataPacketEvent.packet.participantSid]; } // participant.delegate?.onDataReceived(participant, event.packet.payload); @@ -642,7 +643,7 @@ class Room extends DisposableChangeNotifier with EventsEmittable { } Future _handleParticipantDisconnect(String sid) async { - final participant = _participants.remove(sid); + final participant = _remoteParticipants.remove(sid); if (participant == null) { return; } @@ -655,7 +656,7 @@ class Room extends DisposableChangeNotifier with EventsEmittable { Future _sendSyncState() async { final sendUnSub = connectOptions.autoSubscribe; final participantTracks = - participants.values.map((e) => e.participantTracks()); + remoteParticipants.values.map((e) => e.participantTracks()); engine.sendSyncState( subscription: lk_rtc.UpdateSubscription( participantTracks: participantTracks, @@ -674,12 +675,12 @@ extension RoomPrivateMethods on Room { logger.fine('[${objectId}] cleanUp()'); // clean up RemoteParticipants - var participants = _participants.values.toList(); + var participants = _remoteParticipants.values.toList(); for (final participant in participants) { // RemoteParticipant is responsible for disposing resources await participant.dispose(); } - _participants.clear(); + _remoteParticipants.clear(); // clean up LocalParticipant await localParticipant?.unpublishAllTracks(); @@ -758,7 +759,7 @@ extension RoomHardwareManagementMethods on Room { /// Set audio output device. Future setAudioOutputDevice(MediaDevice device) async { if (lkPlatformIs(PlatformType.web)) { - participants.forEach((_, participant) { + remoteParticipants.forEach((_, participant) { for (var audioTrack in participant.audioTracks) { audioTrack.track?.setSinkId(device.deviceId); } diff --git a/lib/src/participant/local.dart b/lib/src/participant/local.dart index 44165261a..9d5b21e21 100644 --- a/lib/src/participant/local.dart +++ b/lib/src/participant/local.dart @@ -457,6 +457,33 @@ class LocalParticipant extends Participant { .whereType>() .toList(); + @override + LocalTrackPublication? getTrackPublicationByName(String name) { + final track = super.getTrackPublicationByName(name); + if (track != null) { + return track; + } + return null; + } + + @override + LocalTrackPublication? getTrackPublicationBySid(String sid) { + final track = super.getTrackPublicationBySid(sid); + if (track != null) { + return track; + } + return null; + } + + @override + LocalTrackPublication? getTrackPublicationBySource(TrackSource source) { + final track = super.getTrackPublicationBySid(sid); + if (track != null) { + return track; + } + return null; + } + /// Shortcut for publishing a [TrackSource.camera] Future setCameraEnabled(bool enabled, {CameraCaptureOptions? cameraCaptureOptions}) async { diff --git a/lib/src/participant/participant.dart b/lib/src/participant/participant.dart index 4fe963239..355a73cb1 100644 --- a/lib/src/participant/participant.dart +++ b/lib/src/participant/participant.dart @@ -220,33 +220,28 @@ abstract class Participant trackPublications[pub.sid] = pub; } - // Must be implemented by subclasses. - Future unpublishTrack(String trackSid, {bool notify = true}); - - /// Convenience method to unpublish all tracks. - Future unpublishAllTracks( - {bool notify = true, bool? stopOnUnpublish}) async { - final trackSids = trackPublications.keys.toSet(); - for (final trackid in trackSids) { - await unpublishTrack(trackid, notify: notify); - } - } - - /// Convenience property to check whether [TrackSource.camera] is published or not. - bool isCameraEnabled() { - return !(getTrackPublicationBySource(TrackSource.camera)?.muted ?? true); + /// get a [TrackPublication] by its sid. + /// returns null when not found. + T? getTrackPublicationBySid(String sid) { + final pub = trackPublications[sid]; + if (pub is T) return pub; + return null; } - /// Convenience property to check whether [TrackSource.microphone] is published or not. - bool isMicrophoneEnabled() { - return !(getTrackPublicationBySource(TrackSource.microphone)?.muted ?? - true); + /// get a [TrackPublication] by its name. + /// returns null when not found. + T? getTrackPublicationByName(String name) { + for (final pub in trackPublications.values) { + if (pub.name == name) { + return pub; + } + } + return null; } - /// Convenience property to check whether [TrackSource.screenShareVideo] is published or not. - bool isScreenShareEnabled() { - return !(getTrackPublicationBySource(TrackSource.screenShareVideo)?.muted ?? - true); + /// get all [TrackPublication]s. + List getTrackPublications() { + return trackPublications.values.toList(); } /// Tries to find a [TrackPublication] by its [TrackSource]. Otherwise, will @@ -272,6 +267,35 @@ abstract class Participant e.kind == lk_models.TrackType.AUDIO)); } + // Must be implemented by subclasses. + Future unpublishTrack(String trackSid, {bool notify = true}); + + /// Convenience method to unpublish all tracks. + Future unpublishAllTracks( + {bool notify = true, bool? stopOnUnpublish}) async { + final trackSids = trackPublications.keys.toSet(); + for (final trackid in trackSids) { + await unpublishTrack(trackid, notify: notify); + } + } + + /// Convenience property to check whether [TrackSource.camera] is published or not. + bool isCameraEnabled() { + return !(getTrackPublicationBySource(TrackSource.camera)?.muted ?? true); + } + + /// Convenience property to check whether [TrackSource.microphone] is published or not. + bool isMicrophoneEnabled() { + return !(getTrackPublicationBySource(TrackSource.microphone)?.muted ?? + true); + } + + /// Convenience property to check whether [TrackSource.screenShareVideo] is published or not. + bool isScreenShareEnabled() { + return !(getTrackPublicationBySource(TrackSource.screenShareVideo)?.muted ?? + true); + } + /// (Equality operator) [Participant.hashCode] is same as [sid.hashCode]. @override int get hashCode => sid.hashCode; diff --git a/lib/src/participant/remote.dart b/lib/src/participant/remote.dart index f7e01b0c3..748f51153 100644 --- a/lib/src/participant/remote.dart +++ b/lib/src/participant/remote.dart @@ -78,9 +78,30 @@ class RemoteParticipant extends Participant { .cast() .toList(); - RemoteTrackPublication? getTrackPublication(String sid) { - final pub = trackPublications[sid]; - if (pub is RemoteTrackPublication) return pub; + @override + RemoteTrackPublication? getTrackPublicationByName(String name) { + final track = super.getTrackPublicationByName(name); + if (track != null) { + return track; + } + return null; + } + + @override + RemoteTrackPublication? getTrackPublicationBySid(String sid) { + final track = super.getTrackPublicationBySid(sid); + if (track != null) { + return track; + } + return null; + } + + @override + RemoteTrackPublication? getTrackPublicationBySource(TrackSource source) { + final track = super.getTrackPublicationBySid(sid); + if (track != null) { + return track; + } return null; } @@ -97,7 +118,7 @@ class RemoteParticipant extends Participant { logger.fine('addSubscribedMediaTrack()'); // If publication doesn't exist yet... - RemoteTrackPublication? pub = getTrackPublication(trackSid); + RemoteTrackPublication? pub = getTrackPublicationBySid(trackSid); if (pub == null) { logger.fine('addSubscribedMediaTrack() pub is null, will wait...'); logger.fine('addSubscribedMediaTrack() tracks: $trackPublications'); @@ -185,7 +206,7 @@ class RemoteParticipant extends Participant { final newPubs = {}; for (final trackInfo in info.tracks) { - RemoteTrackPublication? pub = getTrackPublication(trackInfo.sid); + RemoteTrackPublication? pub = getTrackPublicationBySid(trackInfo.sid); if (pub == null) { final RemoteTrackPublication pub; if (trackInfo.type == lk_models.TrackType.VIDEO) { diff --git a/test/core/room_e2e_test.dart b/test/core/room_e2e_test.dart index b064585bd..6d3d413c6 100644 --- a/test/core/room_e2e_test.dart +++ b/test/core/room_e2e_test.dart @@ -64,7 +64,7 @@ void main() { await room.events.waitFor( duration: const Duration(seconds: 1)); - expect(room.participants.length, 1); + expect(room.remoteParticipants.length, 1); }); test('participant disconnect', () async { @@ -85,7 +85,7 @@ void main() { await room.events.waitFor( duration: const Duration(seconds: 1)); - expect(room.participants.length, 0); + expect(room.remoteParticipants.length, 0); }); test('participant metadata changed', () async {