handleServerResponse method
Handles the server response
Implementation
Future<void> handleServerResponse(Map<String, dynamic> json) async {
final rpcId = json[JsonConstants.id];
final result = json[JsonConstants.result];
if (result.containsKey('value') && result['value'] == 'pong') {
_logger.info('pong');
} else if (rpcId == idJoinRoom) {
final localParticipant = session.localParticipant;
final localConnectionId = result[JsonConstants.id];
localParticipant!.connectionId = localConnectionId;
if (result.containsKey(JsonConstants.iceServers)) {
final jsonIceServers = result[JsonConstants.iceServers];
final List<Map<String, dynamic>> iceServers = [];
for (final Map<String, dynamic> iceServer in jsonIceServers) {
if (iceServer.containsKey("urls")) {
final urls = (iceServer['urls'] as List)
.map((url) => url.toString())
.toList();
final Map<String, dynamic> iceServerBuilder = {
'urls': urls,
'username': iceServer['username'] ?? '',
'credential': iceServer['credential'] ?? '',
};
iceServers.add(iceServerBuilder);
}
if (iceServer.containsKey("url")) {
final Map<String, dynamic> iceServerBuilder = {
'urls': [iceServer['url']],
'username': iceServer['username'] ?? '',
'credential': iceServer['credential'] ?? '',
};
iceServers.add(iceServerBuilder);
}
}
session.setIceServers(iceServers);
}
final localPeerConnection = await session.createLocalPeerConnection();
localParticipant.peerConnection = localPeerConnection;
final sdpConstraints = <String, String>{
'offerToReceiveAudio': 'false',
'offerToReceiveVideo': 'false',
};
session.createOfferForPublishing(sdpConstraints);
if (result[JsonConstants.value].isNotEmpty) {
addRemoteParticipantsAlreadyInRoom(result);
}
} else if (rpcId == idLeaveRoom) {
webSocket?.close(WebSocketStatus.goingAway);
} else if (rpcId == idPublishVideo) {
final localParticipant = session.localParticipant;
final remoteSdpAnswer = RTCSessionDescription(
result['sdpAnswer'],
'answer',
);
localParticipant!.peerConnection!.setRemoteDescription(remoteSdpAnswer);
} else if (idsPrepareReceiveVideo.containsKey(rpcId)) {
final participantAndStream = idsPrepareReceiveVideo.remove(rpcId);
final remoteParticipant =
session.remoteParticipants[participantAndStream!.first];
final streamId = participantAndStream.second;
final remoteSdpOffer = RTCSessionDescription(
result['sdpOffer'],
'offer',
);
remoteParticipant?.peerConnection
?.setRemoteDescription(remoteSdpOffer)
.then((_) {
subscriptionInitiatedFromServer(remoteParticipant, streamId);
});
} else if (idsReceiveVideo.containsKey(rpcId)) {
final id = idsReceiveVideo.remove(rpcId);
if (mediaServer == 'kurento') {
final sessionDescription = RTCSessionDescription(
result['sdpAnswer'],
'answer',
);
session.remoteParticipants[id]!.peerConnection!
.setRemoteDescription(sessionDescription);
}
} else if (idsOnIceCandidate.contains(rpcId)) {
idsOnIceCandidate.remove(rpcId);
} else {
_logger.severe('Unrecognized server response: $result');
}
}