From 80f05cb0085901c7f65a10a8bd775668b8db34fd Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Mon, 20 Apr 2020 15:50:01 +0200 Subject: [PATCH] Can request call offer --- lib/helpers/calls_helper.dart | 4 ++++ lib/lists/call_members_list.dart | 4 ++++ lib/ui/screens/call_screen.dart | 17 ++++++++++++++--- 3 files changed, 22 insertions(+), 3 deletions(-) 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