handleMessage method
Implementation
@override
Future<void> handleMessage(SignalingMessage message) async {
final payload = message.payload['payload'];
switch (message.type) {
case SignalingMessageType.offer:
if (isOfferer ||
(pc != null &&
(await pc!.getSignalingState()) != SignalingState.stable)) {
return;
}
if (pc == null) await initialize();
await pc!.setRemoteDescription(
SessionDescription(sdp: payload['sdp'], type: SdpType.offer),
);
hasRemoteDescription = true;
// Process pending candidates
for (var cand in _pendingCandidates) {
await pc!.addIceCandidate(
IceCandidate(
candidate: cand['candidate'],
sdpMid: cand['sdpMid'],
sdpMLineIndex: cand['sdpMLineIndex'],
),
);
}
_pendingCandidates.clear();
emit('call', this);
break;
case SignalingMessageType.answer:
await pc!.setRemoteDescription(
SessionDescription(sdp: payload['sdp'], type: SdpType.answer),
);
hasRemoteDescription = true;
// Process pending candidates
for (var cand in _pendingCandidates) {
await pc!.addIceCandidate(
IceCandidate(
candidate: cand['candidate'],
sdpMid: cand['sdpMid'],
sdpMLineIndex: cand['sdpMLineIndex'],
),
);
}
_pendingCandidates.clear();
break;
case SignalingMessageType.candidate:
if (!hasRemoteDescription || pc == null) {
_pendingCandidates.add(payload);
} else {
await pc!.addIceCandidate(
IceCandidate(
candidate: payload['candidate'],
sdpMid: payload['sdpMid'],
sdpMLineIndex: payload['sdpMLineIndex'],
),
);
}
break;
case SignalingMessageType.mediaState:
_remoteAudioEnabled = payload['audio'] ?? true;
_remoteVideoEnabled = payload['video'] ?? true;
emit(
ConnectionEvent.mediaState.name,
this,
RemoteStreamChange(
audio: _remoteAudioEnabled,
video: _remoteVideoEnabled,
),
);
break;
default:
break;
}
}