1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2024-11-22 21:09:21 +00:00

Create peer connection for remote streams

This commit is contained in:
Pierre HUBERT 2020-04-20 17:24:42 +02:00
parent 473402149a
commit 45d903bcf7
3 changed files with 55 additions and 4 deletions

View File

@ -182,7 +182,9 @@ class WebSocketHelper {
peerID: msg.data["peerID"],
candidate: signalData.containsKey("candidate")
? RTCIceCandidate(
signalData["candidate"], null, signalData["sdpMLineIndex"])
signalData["candidate"],
"${signalData["sdpMLineIndex"]}" /* fix plugin crash */,
signalData["sdpMLineIndex"])
: null,
sessionDescription: signalData.containsKey("type")
? RTCSessionDescription(signalData["sdp"], signalData["type"])

View File

@ -10,4 +10,9 @@ class CallConfig {
const CallConfig({
@required this.iceServers,
}) : assert(iceServers != null);
/// Turn this call configuration into the right for the WebRTC plugin
Map<String, dynamic> get pluginConfig => {
"iceServers": iceServers.map((f) => {"url": f}).toList()
};
}

View File

@ -13,6 +13,8 @@ import 'package:comunic/utils/account_utils.dart';
import 'package:comunic/utils/intl_utils.dart';
import 'package:comunic/utils/ui_utils.dart';
import 'package:flutter/material.dart';
import 'package:flutter_webrtc/rtc_peerconnection.dart';
import 'package:flutter_webrtc/webrtc.dart';
/// Call screen
///
@ -41,6 +43,7 @@ class _CallScreenState extends SafeState<CallScreen> {
var _error = false;
CallMembersList _membersList;
UsersList _usersList;
final _peersConnections = Map<int, RTCPeerConnection>();
@override
void initState() {
@ -141,6 +144,18 @@ class _CallScreenState extends SafeState<CallScreen> {
_membersList.getUser(memberID).status = MemberStatus.READY;
setState(() {});
// Create peer connection
final peerConnection = await createPeerConnection(_conf.pluginConfig, {
"mandatory": {
"OfferToReceiveAudio": true,
"OfferToReceiveVideo":
_conversation.callCapabilities == CallCapabilities.VIDEO,
},
"optional": [],
});
_peersConnections[memberID] = peerConnection;
// Request an offer to establish a peer connection
await CallsHelper.requestOffer(convID, memberID);
} catch (e, stack) {
@ -150,9 +165,38 @@ class _CallScreenState extends SafeState<CallScreen> {
}
/// Call this method each time we get a new signal
void _newSignal(NewCallSignalEvent ev) {
print(
"${ev.peerID} - ${ev.sessionDescription != null ? ev.sessionDescription.toMap() : ev.candidate.toMap()}");
void _newSignal(NewCallSignalEvent ev) async {
try {
// Check if we can process this message
if (!_peersConnections.containsKey(ev.peerID)) {
print(
"Could not process a signal for the connection with peer ${ev.peerID}!");
return;
}
// Check the kind of signal
// SessionDescription
if (ev.sessionDescription != null) {
await _peersConnections[ev.peerID]
.setRemoteDescription(ev.sessionDescription);
// Send answer if required
if (ev.sessionDescription.type == "offer") {
final answer = await _peersConnections[ev.peerID].createAnswer({});
//TODO : Send answer back to server
print("ANSWER TO SEND ${answer.toMap()}");
}
}
// Ice Candidate
else {
await _peersConnections[ev.peerID].addCandidate(ev.candidate);
}
} catch (e, stack) {
print("Error while handling new signal ! $e\n$stack");
showSimpleSnack(context, tr("Error while processing new signal!"));
}
}
/// Call this when a user has interrupted streaming