diff --git a/lib/helpers/calls_helper.dart b/lib/helpers/calls_helper.dart index 905267b..f4662e6 100644 --- a/lib/helpers/calls_helper.dart +++ b/lib/helpers/calls_helper.dart @@ -34,4 +34,8 @@ class CallsHelper { )) .toList() .cast()); + + /// Request an offer to access another peer's stream + static Future requestOffer(int callID, int peerID) async => + await ws("calls/request_offer", {"callID": callID, "peerID": peerID}); } diff --git a/lib/lists/call_members_list.dart b/lib/lists/call_members_list.dart index e444f1b..783d4ad 100644 --- a/lib/lists/call_members_list.dart +++ b/lib/lists/call_members_list.dart @@ -14,4 +14,8 @@ class CallMembersList extends AbstractList { /// Get the connection of a specific user CallMember getUser(int userID) => this.firstWhere((f) => f.userID == userID); + + /// Extract ready peers from this list + CallMembersList get readyPeers => + CallMembersList()..addAll(where((f) => f.status == MemberStatus.READY)); } diff --git a/lib/ui/screens/call_screen.dart b/lib/ui/screens/call_screen.dart index 1a9c5f7..49d9672 100644 --- a/lib/ui/screens/call_screen.dart +++ b/lib/ui/screens/call_screen.dart @@ -101,6 +101,10 @@ class _CallScreenState extends SafeState { this.listen((e) { if (e.callID == convID) _leaveCall(needConfirm: false); }); + + // Connect to ready peers + for (final peer in _membersList.readyPeers) + await this._memberReady(peer.userID); } catch (e, stack) { print("Could not initialize call! $e\n$stack"); setState(() => _error = true); @@ -128,10 +132,17 @@ class _CallScreenState extends SafeState { } /// Call this when a user started to stream media - void _memberReady(int memberID) { - _membersList.getUser(memberID).status = MemberStatus.READY; + Future _memberReady(int memberID) async { + try { + _membersList.getUser(memberID).status = MemberStatus.READY; + setState(() {}); - setState(() {}); + // Request an offer to establish a peer connection + await CallsHelper.requestOffer(convID, memberID); + } catch (e, stack) { + print("Could not connect to remote peer $e\n$stack!"); + showSimpleSnack(context, tr("Could not connect to a remote peer!")); + } } /// Call this when a user has interrupted streaming