dynamic
initConnection(- Map<String, dynamic> data
)
Implementation
initConnection(Map<String, dynamic> data) async {
_logger.d('Viewer connect options values: $options');
if (isActive() && !data['migrate']) {
_logger.w('Viewer currently subscribed');
throw Error();
}
MillicastDirectorResponse subscriberData;
try {
subscriberData = await tokenGenerator();
} catch (e) {
_logger.e('Error generating token.');
throw Exception(e);
}
// ignore: unnecessary_null_comparison
if (subscriberData == null) {
_logger.e('Error while subscribing. Subscriber data required');
}
var signalingInstance = Signaling({
'streamName': streamName,
'url': '${subscriberData.urls[0]}?token=${subscriberData.jwt}'
});
var webRTCPeerInstance = data['migrate'] ? PeerConnection() : webRTCPeer;
await webRTCPeerInstance.createRTCPeer(options?['peerConfig']);
// Stop emiting events from the previous instances
if (stopReemitingWebRTCPeerInstanceEvents != null) {
stopReemitingWebRTCPeerInstanceEvents!();
}
if (stopReemitingSignalingInstanceEvents != null) {
stopReemitingSignalingInstanceEvents!();
}
stopReemitingWebRTCPeerInstanceEvents = reemit(webRTCPeerInstance, this,
[webRTCEvents['track'], webRTCEvents['connectionStateChange']]);
stopReemitingSignalingInstanceEvents =
reemit(signalingInstance, this, [SignalingEvents.broadcastEvent]);
Future getLocalSDPFuture = webRTCPeerInstance
.getRTCLocalSDP(options: {...options!, 'stereo': true});
Future signalingConnectFuture = signalingInstance.connect();
Iterable<Future<dynamic>> iterFuture = [
getLocalSDPFuture,
signalingConnectFuture
];
var resolvedFutures = await Future.wait(iterFuture);
String localSdp = resolvedFutures[0];
Future subscribeFuture =
signalingInstance.subscribe(localSdp, options: options);
Future<void>? setLocalDescriptionFuture = webRTCPeerInstance.peer
?.setLocalDescription(webRTCPeerInstance.sessionDescription!);
iterFuture = [subscribeFuture, setLocalDescriptionFuture!];
resolvedFutures = await Future.wait(iterFuture);
String remoteSdp = resolvedFutures[0];
await webRTCPeerInstance.setRTCRemoteSDP(remoteSdp);
_logger.i('Connected to streamName: $streamName');
Signaling? oldSignlaling = signaling;
PeerConnection? oldWebRTCPeer = webRTCPeer;
signaling = signalingInstance;
webRTCPeer = webRTCPeerInstance;
emit(SignalingEvents.connectionSuccess, this);
setReconnect();
if (data['migrate']) {
webRTCPeer.on(webRTCEvents['connectionStateChange'], webRTCPeer,
(ev, context) async {
if (ev.eventData ==
RTCIceConnectionState.RTCIceConnectionStateConnected) {
Timer(const Duration(milliseconds: 1000), () {
oldSignlaling?.close();
oldWebRTCPeer?.closeRTCPeer();
oldSignlaling = null;
oldWebRTCPeer = null;
_logger.i('Current connection migrated');
});
} else if ([
RTCIceConnectionState.RTCIceConnectionStateClosed,
RTCIceConnectionState.RTCIceConnectionStateFailed,
RTCIceConnectionState.RTCIceConnectionStateDisconnected
].contains(ev.eventData)) {
oldSignlaling?.close();
oldWebRTCPeer?.closeRTCPeer();
oldSignlaling = null;
oldWebRTCPeer = null;
}
});
}
}