Skip to content

Commit

Permalink
rename some parameters.
Browse files Browse the repository at this point in the history
  • Loading branch information
cloudwebrtc committed Dec 22, 2023
1 parent adbff11 commit 02dcc51
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 50 deletions.
41 changes: 21 additions & 20 deletions lib/src/core/room.dart
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ class Room extends DisposableChangeNotifier with EventsEmittable<RoomEvent> {
RoomOptions get roomOptions => engine.roomOptions;

/// map of SID to RemoteParticipant
UnmodifiableMapView<String, RemoteParticipant> get participants =>
UnmodifiableMapView(_participants);
final _participants = <String, RemoteParticipant>{};
UnmodifiableMapView<String, RemoteParticipant> get remoteParticipants =>
UnmodifiableMapView(_remoteParticipants);
final _remoteParticipants = <String, RemoteParticipant>{};

/// the current participant
LocalParticipant? get localParticipant => _localParticipant;
Expand Down Expand Up @@ -314,7 +314,7 @@ class Room extends DisposableChangeNotifier with EventsEmittable<RoomEvent> {
'allowed:${event.allowed}');

// find participant
final participant = _participants[event.participantSid];
final participant = _remoteParticipants[event.participantSid];
if (participant == null) {
return;
}
Expand Down Expand Up @@ -364,9 +364,9 @@ class Room extends DisposableChangeNotifier with EventsEmittable<RoomEvent> {
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;
Expand All @@ -386,7 +386,7 @@ class Room extends DisposableChangeNotifier with EventsEmittable<RoomEvent> {
// 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();
Expand Down Expand Up @@ -462,7 +462,7 @@ class Room extends DisposableChangeNotifier with EventsEmittable<RoomEvent> {

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);
Expand All @@ -485,7 +485,7 @@ class Room extends DisposableChangeNotifier with EventsEmittable<RoomEvent> {
);
}

_participants[sid] = participant;
_remoteParticipants[sid] = participant;

return participant;
}
Expand All @@ -500,7 +500,7 @@ class Room extends DisposableChangeNotifier with EventsEmittable<RoomEvent> {
continue;
}

final isNew = !_participants.containsKey(info.sid);
final isNew = !_remoteParticipants.containsKey(info.sid);

if (info.state == lk_models.ParticipantInfo_State.DISCONNECTED &&
!isNew) {
Expand Down Expand Up @@ -532,7 +532,7 @@ class Room extends DisposableChangeNotifier with EventsEmittable<RoomEvent> {
};

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;

Expand Down Expand Up @@ -560,7 +560,7 @@ class Room extends DisposableChangeNotifier with EventsEmittable<RoomEvent> {
// localParticipant & remote participants
final allParticipants = <String, Participant>{
if (localParticipant != null) localParticipant!.sid: localParticipant!,
..._participants,
..._remoteParticipants,
};

for (final speaker in speakers) {
Expand Down Expand Up @@ -592,7 +592,7 @@ class Room extends DisposableChangeNotifier with EventsEmittable<RoomEvent> {
if (entry.participantSid == localParticipant?.sid) {
participant = localParticipant;
} else {
participant = _participants[entry.participantSid];
participant = _remoteParticipants[entry.participantSid];
}

if (participant != null) {
Expand All @@ -606,7 +606,7 @@ class Room extends DisposableChangeNotifier with EventsEmittable<RoomEvent> {
List<lk_rtc.StreamStateInfo> 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];
Expand All @@ -626,7 +626,8 @@ class Room extends DisposableChangeNotifier with EventsEmittable<RoomEvent> {
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);
Expand All @@ -642,7 +643,7 @@ class Room extends DisposableChangeNotifier with EventsEmittable<RoomEvent> {
}

Future<void> _handleParticipantDisconnect(String sid) async {
final participant = _participants.remove(sid);
final participant = _remoteParticipants.remove(sid);
if (participant == null) {
return;
}
Expand All @@ -655,7 +656,7 @@ class Room extends DisposableChangeNotifier with EventsEmittable<RoomEvent> {
Future<void> _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,
Expand All @@ -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();
Expand Down Expand Up @@ -758,7 +759,7 @@ extension RoomHardwareManagementMethods on Room {
/// Set audio output device.
Future<void> 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);
}
Expand Down
27 changes: 27 additions & 0 deletions lib/src/participant/local.dart
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,33 @@ class LocalParticipant extends Participant<LocalTrackPublication> {
.whereType<LocalTrackPublication<LocalAudioTrack>>()
.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<LocalTrackPublication?> setCameraEnabled(bool enabled,
{CameraCaptureOptions? cameraCaptureOptions}) async {
Expand Down
70 changes: 47 additions & 23 deletions lib/src/participant/participant.dart
Original file line number Diff line number Diff line change
Expand Up @@ -220,33 +220,28 @@ abstract class Participant<T extends TrackPublication>
trackPublications[pub.sid] = pub;
}

// Must be implemented by subclasses.
Future<void> unpublishTrack(String trackSid, {bool notify = true});

/// Convenience method to unpublish all tracks.
Future<void> 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<T> getTrackPublications() {
return trackPublications.values.toList();
}

/// Tries to find a [TrackPublication] by its [TrackSource]. Otherwise, will
Expand All @@ -272,6 +267,35 @@ abstract class Participant<T extends TrackPublication>
e.kind == lk_models.TrackType.AUDIO));
}

// Must be implemented by subclasses.
Future<void> unpublishTrack(String trackSid, {bool notify = true});

/// Convenience method to unpublish all tracks.
Future<void> 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;
Expand Down
31 changes: 26 additions & 5 deletions lib/src/participant/remote.dart
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,30 @@ class RemoteParticipant extends Participant<RemoteTrackPublication> {
.cast<RemoteTrackPublication>()
.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;
}

Expand All @@ -97,7 +118,7 @@ class RemoteParticipant extends Participant<RemoteTrackPublication> {
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');
Expand Down Expand Up @@ -185,7 +206,7 @@ class RemoteParticipant extends Participant<RemoteTrackPublication> {
final newPubs = <RemoteTrackPublication>{};

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) {
Expand Down
4 changes: 2 additions & 2 deletions test/core/room_e2e_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ void main() {

await room.events.waitFor<ParticipantConnectedEvent>(
duration: const Duration(seconds: 1));
expect(room.participants.length, 1);
expect(room.remoteParticipants.length, 1);
});

test('participant disconnect', () async {
Expand All @@ -85,7 +85,7 @@ void main() {

await room.events.waitFor<ParticipantDisconnectedEvent>(
duration: const Duration(seconds: 1));
expect(room.participants.length, 0);
expect(room.remoteParticipants.length, 0);
});

test('participant metadata changed', () async {
Expand Down

0 comments on commit 02dcc51

Please sign in to comment.