Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FLUT-284: Added onPeerListUpdate callback in preview #1762

Merged
merged 4 commits into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions packages/hms_room_kit/lib/src/preview/preview_store.dart
Original file line number Diff line number Diff line change
Expand Up @@ -316,4 +316,11 @@ class PreviewStore extends ChangeNotifier
}
notifyListeners();
}

@override
void onPeerListUpdate(
{required List<HMSPeer> addedPeers,
required List<HMSPeer> removedPeers}) {
log("onPeerListUpdate -> addedPeers: $addedPeers removedPeers: $removedPeers");
ygit marked this conversation as resolved.
Show resolved Hide resolved
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1516,6 +1516,34 @@ class HmssdkFlutterPlugin :
}
}
}

override fun peerListUpdated(addedPeers: ArrayList<HMSPeer>?, removedPeers: ArrayList<HMSPeer>?){
val args = HashMap<String, Any?>()
args["event_name"] = "on_peer_list_update"
val parameters = HashMap<String, Any?>()
val peersAdded = ArrayList<HashMap<String, Any?>?>()
val peersRemoved = ArrayList<HashMap<String, Any?>?>()
/**
* Here we add peers to the list after parsing the
* peer object
*/
addedPeers?.forEach { peer ->
peersAdded.add(HMSPeerExtension.toDictionary(peer))
}

removedPeers?.forEach { peer ->
peersRemoved.add(HMSPeerExtension.toDictionary(peer))
}

parameters["added_peers"] = peersAdded
parameters["removed_peers"] = peersRemoved

args["data"] = parameters

CoroutineScope(Dispatchers.Main).launch {
previewSink?.success(args)
}
}
}

/***
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1276,6 +1276,7 @@ public class SwiftHmssdkFlutterPlugin: NSObject, FlutterPlugin, HMSUpdateListene
previewEnded = false
previewSink?(data)
}

var previewEnded = false
public func on(join room: HMSRoom) {
previewEnded = true
Expand Down Expand Up @@ -1496,7 +1497,11 @@ public class SwiftHmssdkFlutterPlugin: NSObject, FlutterPlugin, HMSUpdateListene

let data = ["event_name": "on_peer_list_update", "data": parameters] as [String: Any]

eventSink?(data)
if(previewEnded){
eventSink?(data)
}else{
previewSink?(data)
}
}

// MARK: - RTC Stats Listeners
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ enum HMSPreviewUpdateListenerMethod {
onPeerUpdate,
onRoomUpdate,
onAudioDeviceChanged,
onPeerListUpdate,
unknown
}

Expand All @@ -21,6 +22,8 @@ extension HMSPreviewUpdateListenerMethodValues
return HMSPreviewUpdateListenerMethod.onRoomUpdate;
case 'on_audio_device_changed':
return HMSPreviewUpdateListenerMethod.onAudioDeviceChanged;
case 'on_peer_list_update':
return HMSPreviewUpdateListenerMethod.onPeerListUpdate;
default:
return HMSPreviewUpdateListenerMethod.unknown;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,13 @@ abstract class HMSPreviewListener {
void onAudioDeviceChanged(
{HMSAudioDevice? currentAudioDevice,
List<HMSAudioDevice>? availableAudioDevice});

///Upon joining a room with existing peers, onPeerListUpdated will be called with the list of peers present
///in the room instead of getting onPeerUpdate for each peer in the room.
///Subsequent peer joins/leaves would be notified via both onPeerUpdate and onPeerListUpdated
/// - Parameters:
/// - addedPeers: List of peers who joined the room
/// - removedPeers: List of peers who left the room
void onPeerListUpdate(
{required List<HMSPeer> addedPeers, required List<HMSPeer> removedPeers});
}
27 changes: 27 additions & 0 deletions packages/hmssdk_flutter/lib/src/service/platform_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,27 @@ abstract class PlatformService {
: null
});
break;
case HMSPreviewUpdateListenerMethod.onPeerListUpdate:
List<HMSPeer> addedPeers = [];
List<HMSPeer> removedPeers = [];

if (event.data.containsKey("added_peers") &&
event.data["added_peers"] != null) {
for (var peer in event.data["added_peers"]) {
addedPeers.add(HMSPeer.fromMap(peer));
}
}

if (event.data.containsKey("removed_peers") &&
event.data["removed_peers"] != null) {
for (var peer in event.data["removed_peers"]) {
removedPeers.add(HMSPeer.fromMap(peer));
}
}

notifyPreviewListeners(method,
{"added_peers": addedPeers, "removed_peers": removedPeers});
break;
}
});

Expand Down Expand Up @@ -614,6 +635,12 @@ abstract class PlatformService {
availableAudioDevice: arguments["available_audio_device"]);
});
break;
case HMSPreviewUpdateListenerMethod.onPeerListUpdate:
previewListeners.forEach((e) {
e.onPeerListUpdate(
addedPeers: arguments["added_peers"],
removedPeers: arguments["removed_peers"]);
});
}
}

Expand Down