minor amendments to peer manager and peer connection factory
parent
3d24417faa
commit
11fcb2cb99
|
@ -2,6 +2,7 @@ import "package:rxdart/rxdart.dart";
|
|||
import "../resources/repository.dart";
|
||||
import "../models/user_model.dart";
|
||||
|
||||
// TODO: SHOULD BE A INHERITED SCOPED BLOC Widget
|
||||
class ContactBloc {
|
||||
final _repository = Repository();
|
||||
final _contactsFetcher = PublishSubject<List<User>>();
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
import 'dart:async';
|
||||
import 'dart:convert';
|
||||
import 'package:eventsource/eventsource.dart';
|
||||
import 'package:flutter_webrtc/webrtc.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
import "dart:async";
|
||||
import "dart:convert";
|
||||
import "package:eventsource/eventsource.dart";
|
||||
import "package:flutter_webrtc/webrtc.dart";
|
||||
import "package:http/http.dart" as http;
|
||||
|
||||
// Available utility enums
|
||||
enum SignalingResponse { SUCCESSFULL, NO_DEVICE, NO_DATA }
|
||||
|
@ -25,14 +25,12 @@ class PeerConnectionFactory {
|
|||
//Callbacks
|
||||
OnMessageCallback onMessageCallback;
|
||||
|
||||
// could be const
|
||||
Map<String, dynamic> _iceServers = {
|
||||
final Map<String, dynamic> _iceServers = {
|
||||
"iceServers": [
|
||||
{"url": "stun:stun.l.google.com:19302"}
|
||||
]
|
||||
};
|
||||
|
||||
// could be const
|
||||
final Map<String, dynamic> _config = {
|
||||
"mandatory": {},
|
||||
"optional": [
|
||||
|
@ -40,7 +38,6 @@ class PeerConnectionFactory {
|
|||
]
|
||||
};
|
||||
|
||||
// could be const
|
||||
final Map<String, dynamic> _constraints = {
|
||||
"mandatory": {
|
||||
"OfferToReceiveAudio": true,
|
||||
|
@ -50,11 +47,13 @@ class PeerConnectionFactory {
|
|||
};
|
||||
|
||||
PeerConnectionFactory(this._localUserId, this._localDeviceId, this._stream,
|
||||
this.onMessageCallback);
|
||||
this.onMessageCallback) {
|
||||
_initialize();
|
||||
}
|
||||
|
||||
// initialize() method sets up a subscription to the eventsource and
|
||||
// attaches a callback to it
|
||||
initialize() async {
|
||||
_initialize() async {
|
||||
_signalingServer = await EventSource.connect(
|
||||
"localhost:10201/subscribe/$_localUserId/device/$_localDeviceId");
|
||||
_signalingServer.listen((event) {
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
import "package:flutter_webrtc/webrtc.dart";
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'dart:convert';
|
||||
import 'dart:async';
|
||||
import "package:http/http.dart" as http;
|
||||
import "dart:convert";
|
||||
import "dart:async";
|
||||
import "peer_connection_factory.dart";
|
||||
|
||||
class PeerManager {
|
||||
|
@ -16,8 +16,18 @@ class PeerManager {
|
|||
Map<String, RTCPeerConnection> _currentConnections;
|
||||
|
||||
PeerManager(this._selfUserId, this._selfDeviceId) {
|
||||
_peerConnectionFactory =
|
||||
PeerConnectionFactory(_selfUserId, _selfDeviceId, _localStream, signalEventHandler);
|
||||
_initialize();
|
||||
}
|
||||
|
||||
_initialize() async {
|
||||
final Map<String, dynamic> mediaConstraints = {
|
||||
"audio": true,
|
||||
"video": false
|
||||
};
|
||||
|
||||
_localStream = await navigator.getUserMedia(mediaConstraints);
|
||||
_peerConnectionFactory = PeerConnectionFactory(
|
||||
_selfUserId, _selfDeviceId, _localStream, _signalEventHandler);
|
||||
}
|
||||
|
||||
addPeer(String userId) async {
|
||||
|
@ -26,12 +36,11 @@ class PeerManager {
|
|||
List<dynamic> deviceIds = _decoder.convert(response.body);
|
||||
|
||||
deviceIds.forEach((deviceId) async {
|
||||
RTCPeerConnection connection = await _peerConnectionFactory
|
||||
.newPeerConnection(userId, deviceId);
|
||||
RTCPeerConnection connection =
|
||||
await _peerConnectionFactory.newPeerConnection(userId, deviceId);
|
||||
|
||||
// Handle streams being added and removed remotely
|
||||
connection.onAddStream =
|
||||
(stream) => _currentStreams.add(stream);
|
||||
connection.onAddStream = (stream) => _currentStreams.add(stream);
|
||||
connection.onRemoveStream =
|
||||
(stream) => _currentStreams.removeWhere((it) => stream.id == it.id);
|
||||
|
||||
|
@ -40,9 +49,15 @@ class PeerManager {
|
|||
});
|
||||
}
|
||||
|
||||
leaveAll(void) async {}
|
||||
leaveAll() async {
|
||||
// DO WE NEED TO CLOSE THE REMOTE STREAMS???
|
||||
_currentConnections.forEach((key, value) {
|
||||
_peerConnectionFactory.leavePeerConnection(value);
|
||||
_currentConnections[key] = null;
|
||||
});
|
||||
}
|
||||
|
||||
signalEventHandler(Map<String, dynamic> data) async {
|
||||
_signalEventHandler(Map<String, dynamic> data) async {
|
||||
switch (data["type"]) {
|
||||
case SignalType.CANDIDATE:
|
||||
String userId = data["fromUser"];
|
||||
|
@ -60,8 +75,8 @@ class PeerManager {
|
|||
String userId = data["fromUser"];
|
||||
String deviceId = data["fromDevice"];
|
||||
|
||||
RTCPeerConnection connection = await _peerConnectionFactory
|
||||
.newPeerConnection(userId, deviceId);
|
||||
RTCPeerConnection connection =
|
||||
await _peerConnectionFactory.newPeerConnection(userId, deviceId);
|
||||
_currentConnections["$userId-$deviceId"] = connection;
|
||||
|
||||
connection.setRemoteDescription(
|
||||
|
|
Loading…
Reference in New Issue