diff --git a/lib/helpers/calls_helper.dart b/lib/helpers/calls_helper.dart index 92ed4a6..d98a670 100644 --- a/lib/helpers/calls_helper.dart +++ b/lib/helpers/calls_helper.dart @@ -62,4 +62,8 @@ class CallsHelper { "type": "CANDIDATE", "data": jsonEncode(candidate.toMap()) }); + + /// Mark ourselves as ready to stream to other peers + static Future markPeerReady(int callID) async => + await ws("calls/mark_ready", {"callID": callID}); } diff --git a/lib/ui/screens/call_screen.dart b/lib/ui/screens/call_screen.dart index ed64ac3..33ff9be 100644 --- a/lib/ui/screens/call_screen.dart +++ b/lib/ui/screens/call_screen.dart @@ -200,6 +200,39 @@ class _CallScreenState extends SafeState { await _renderers[userID()].initialize(); _renderers[userID()].srcObject = _localStream; setState(() {}); + + // Open stream + final peerConnection = await createPeerConnection(_conf.pluginConfig, { + "mandatory": {}, + "optional": [], + }); + + _peersConnections[userID()] = peerConnection; + await peerConnection.addStream(_localStream); + + peerConnection.onAddStream = + (s) => throw Exception("Got a new stream on main peer connection!"); + peerConnection.onIceCandidate = + (c) => CallsHelper.sendIceCandidate(convID, userID(), c); + peerConnection.onIceConnectionState = (c) { + print("New connection state: $c"); + + if (c == RTCIceConnectionState.RTCIceConnectionStateConnected) + CallsHelper.markPeerReady(convID); + }; + peerConnection.onSignalingState = (c) => print("New signaling state: $c"); + + // Create & send offer + final offer = await peerConnection.createOffer({ + "mandatory": { + "OfferToReceiveAudio": true, + "OfferToReceiveVideo": true, + }, + "optional": [], + }); + await peerConnection.setLocalDescription(offer); + + await CallsHelper.sendSessionDescription(convID, userID(), offer); } catch (e, stack) { print("Could not start streaming! $e\n$stack"); showSimpleSnack(context, tr("Could not start streaming!")); @@ -208,6 +241,12 @@ class _CallScreenState extends SafeState { /// Stop local streaming Future _stopStreaming() async { + // Close peer connection + if (_peersConnections.containsKey(userID())) { + _peersConnections[userID()].close(); + _peersConnections.remove(userID()); + } + // Stop local stream if (_localStream != null) { await _localStream.dispose();