chore: Null safety for voip.dart.

This commit is contained in:
cloudwebrtc 2021-11-10 21:47:16 +08:00
parent 09e24fd33e
commit 0822e3809d
1 changed files with 33 additions and 33 deletions

View File

@ -272,7 +272,7 @@ class CallSession {
CallState state = CallState.kFledgling; CallState state = CallState.kFledgling;
bool get isOutgoing => direction == Direction.kOutgoing; bool get isOutgoing => direction == Direction.kOutgoing;
bool get isRinging => state == CallState.kRinging; bool get isRinging => state == CallState.kRinging;
late RTCPeerConnection pc; RTCPeerConnection? pc;
List<RTCIceCandidate> remoteCandidates = <RTCIceCandidate>[]; List<RTCIceCandidate> remoteCandidates = <RTCIceCandidate>[];
List<RTCIceCandidate> localCandidates = <RTCIceCandidate>[]; List<RTCIceCandidate> localCandidates = <RTCIceCandidate>[];
late AssertedIdentity remoteAssertedIdentity; late AssertedIdentity remoteAssertedIdentity;
@ -287,7 +287,7 @@ class CallSession {
bool ignoreOffer = false; bool ignoreOffer = false;
String facingMode = 'user'; String facingMode = 'user';
late Client client; late Client client;
late String remotePartyId; String? remotePartyId;
late User remoteUser; late User remoteUser;
late CallParty hangupParty; late CallParty hangupParty;
late String hangupReason; late String hangupReason;
@ -341,7 +341,7 @@ class CallSession {
_updateRemoteSDPStreamMetadata(metadata); _updateRemoteSDPStreamMetadata(metadata);
} }
await pc.setRemoteDescription(offer); await pc!.setRemoteDescription(offer);
setCallState(CallState.kRinging); setCallState(CallState.kRinging);
@ -369,8 +369,8 @@ class CallSession {
if (direction == Direction.kOutgoing) { if (direction == Direction.kOutgoing) {
setCallState(CallState.kConnecting); setCallState(CallState.kConnecting);
await pc.setRemoteDescription(answer); await pc!.setRemoteDescription(answer);
remoteCandidates.forEach((candidate) => pc.addCandidate(candidate)); remoteCandidates.forEach((candidate) => pc!.addCandidate(candidate));
} }
} }
@ -382,7 +382,7 @@ class CallSession {
// https://developer.mozilla.org/en-US/docs/Web/API/WebRTC_API/Perfect_negotiation // https://developer.mozilla.org/en-US/docs/Web/API/WebRTC_API/Perfect_negotiation
final offerCollision = ((description.type == 'offer') && final offerCollision = ((description.type == 'offer') &&
(makingOffer || (makingOffer ||
pc.signalingState != RTCSignalingState.RTCSignalingStateStable)); pc!.signalingState != RTCSignalingState.RTCSignalingStateStable));
ignoreOffer = !polite && offerCollision; ignoreOffer = !polite && offerCollision;
if (ignoreOffer) { if (ignoreOffer) {
@ -397,13 +397,13 @@ class CallSession {
} }
try { try {
await pc.setRemoteDescription(description); await pc!.setRemoteDescription(description);
if (description.type == 'offer') { if (description.type == 'offer') {
final answer = await pc.createAnswer(); final answer = await pc!.createAnswer();
await room.sendCallNegotiate( await room.sendCallNegotiate(
callId, lifetimeMs, localPartyId, answer.sdp!, callId, lifetimeMs, localPartyId, answer.sdp!,
type: answer.type!); type: answer.type!);
await pc.setLocalDescription(answer); await pc!.setLocalDescription(answer);
} }
} catch (e) { } catch (e) {
_getLocalOfferFailed(e); _getLocalOfferFailed(e);
@ -457,7 +457,7 @@ class CallSession {
if (pc != null && inviteOrAnswerSent && remotePartyId != null) { if (pc != null && inviteOrAnswerSent && remotePartyId != null) {
try { try {
await pc.addCandidate(candidate); await pc!.addCandidate(candidate);
} catch (e) { } catch (e) {
Logs().e('[VOIP] onCandidatesReceived => ${e.toString()}'); Logs().e('[VOIP] onCandidatesReceived => ${e.toString()}');
} }
@ -467,7 +467,7 @@ class CallSession {
}); });
if (pc != null && if (pc != null &&
pc.iceConnectionState == pc!.iceConnectionState ==
RTCIceConnectionState.RTCIceConnectionStateDisconnected) { RTCIceConnectionState.RTCIceConnectionStateDisconnected) {
_restartIce(); _restartIce();
} }
@ -511,7 +511,7 @@ class CallSession {
} }
} else { } else {
for (final sender in screensharingSenders) { for (final sender in screensharingSenders) {
await pc.removeTrack(sender); await pc!.removeTrack(sender);
} }
for (final track in localScreenSharingStream!.stream!.getTracks()) { for (final track in localScreenSharingStream!.stream!.getTracks()) {
await track.stop(); await track.stop();
@ -548,12 +548,12 @@ class CallSession {
if (purpose == SDPStreamMetadataPurpose.Screenshare) { if (purpose == SDPStreamMetadataPurpose.Screenshare) {
screensharingSenders.clear(); screensharingSenders.clear();
stream.getTracks().forEach((track) async { stream.getTracks().forEach((track) async {
screensharingSenders.add(await pc.addTrack(track, stream)); screensharingSenders.add(await pc!.addTrack(track, stream));
}); });
} else if (purpose == SDPStreamMetadataPurpose.Usermedia) { } else if (purpose == SDPStreamMetadataPurpose.Usermedia) {
usermediaSenders.clear(); usermediaSenders.clear();
stream.getTracks().forEach((track) async { stream.getTracks().forEach((track) async {
usermediaSenders.add(await pc.addTrack(track, stream)); usermediaSenders.add(await pc!.addTrack(track, stream));
}); });
} }
emit(CallEvent.kFeedsChanged, streams); emit(CallEvent.kFeedsChanged, streams);
@ -629,7 +629,7 @@ class CallSession {
void setRemoteOnHold(bool onHold) async { void setRemoteOnHold(bool onHold) async {
if (isRemoteOnHold == onHold) return; if (isRemoteOnHold == onHold) return;
remoteOnHold = onHold; remoteOnHold = onHold;
final transceivers = await pc.getTransceivers(); final transceivers = await pc!.getTransceivers();
for (final transceiver in transceivers) { for (final transceiver in transceivers) {
await transceiver.setDirection(onHold await transceiver.setDirection(onHold
? TransceiverDirection.SendOnly ? TransceiverDirection.SendOnly
@ -646,7 +646,7 @@ class CallSession {
var callOnHold = true; var callOnHold = true;
// We consider a call to be on hold only if *all* the tracks are on hold // We consider a call to be on hold only if *all* the tracks are on hold
// (is this the right thing to do?) // (is this the right thing to do?)
final transceivers = await pc.getTransceivers(); final transceivers = await pc!.getTransceivers();
for (final transceiver in transceivers) { for (final transceiver in transceivers) {
final currentDirection = await transceiver.getCurrentDirection(); final currentDirection = await transceiver.getCurrentDirection();
Logs() Logs()
@ -684,8 +684,8 @@ class CallSession {
if (direction == Direction.kIncoming) { if (direction == Direction.kIncoming) {
setCallState(CallState.kCreateAnswer); setCallState(CallState.kCreateAnswer);
final answer = await pc.createAnswer({}); final answer = await pc!.createAnswer({});
remoteCandidates.forEach((candidate) => pc.addCandidate(candidate)); remoteCandidates.forEach((candidate) => pc!.addCandidate(candidate));
final callCapabilities = CallCapabilities() final callCapabilities = CallCapabilities()
..dtmf = false ..dtmf = false
@ -703,7 +703,7 @@ class CallSession {
capabilities: callCapabilities, capabilities: callCapabilities,
metadata: metadata); metadata: metadata);
Logs().v('[VOIP] answer res => $res'); Logs().v('[VOIP] answer res => $res');
await pc.setLocalDescription(answer); await pc!.setLocalDescription(answer);
setCallState(CallState.kConnecting); setCallState(CallState.kConnecting);
inviteOrAnswerSent = true; inviteOrAnswerSent = true;
_answeredByUs = true; _answeredByUs = true;
@ -740,7 +740,7 @@ class CallSession {
} }
void sendDTMF(String tones) async { void sendDTMF(String tones) async {
final senders = await pc.getSenders(); final senders = await pc!.getSenders();
for (final sender in senders) { for (final sender in senders) {
if (sender.track != null && sender.track!.kind == 'audio') { if (sender.track != null && sender.track!.kind == 'audio') {
await sender.dtmfSender.insertDTMF(tones); await sender.dtmfSender.insertDTMF(tones);
@ -773,7 +773,7 @@ class CallSession {
} }
} }
void onRejectReceived(String reason) { void onRejectReceived(String? reason) {
Logs().v('[VOIP] Reject received for call ID ' + callId); Logs().v('[VOIP] Reject received for call ID ' + callId);
// No need to check party_id for reject because if we'd received either // No need to check party_id for reject because if we'd received either
// an answer or reject, we wouldn't be in state InviteSent // an answer or reject, we wouldn't be in state InviteSent
@ -797,7 +797,7 @@ class CallSession {
} }
try { try {
await pc.setLocalDescription(offer); await pc!.setLocalDescription(offer);
} catch (err) { } catch (err) {
Logs().d('Error setting local description! ${err.toString()}'); Logs().d('Error setting local description! ${err.toString()}');
terminate(CallParty.kLocal, CallErrorCode.SetLocalDescription, true); terminate(CallParty.kLocal, CallErrorCode.SetLocalDescription, true);
@ -836,7 +836,7 @@ class CallSession {
Logs().i('Negotiation is needed!'); Logs().i('Negotiation is needed!');
makingOffer = true; makingOffer = true;
try { try {
final offer = await pc.createOffer({}); final offer = await pc!.createOffer({});
await _gotLocalOffer(offer); await _gotLocalOffer(offer);
} catch (e) { } catch (e) {
_getLocalOfferFailed(e); _getLocalOfferFailed(e);
@ -850,13 +850,13 @@ class CallSession {
try { try {
pc = await _createPeerConnection(); pc = await _createPeerConnection();
pc.onRenegotiationNeeded = onNegotiationNeeded; pc!.onRenegotiationNeeded = onNegotiationNeeded;
pc.onIceCandidate = (RTCIceCandidate candidate) async { pc!.onIceCandidate = (RTCIceCandidate candidate) async {
//Logs().v('[VOIP] onIceCandidate => ${candidate.toMap().toString()}'); //Logs().v('[VOIP] onIceCandidate => ${candidate.toMap().toString()}');
localCandidates.add(candidate); localCandidates.add(candidate);
}; };
pc.onIceGatheringState = (RTCIceGatheringState state) async { pc!.onIceGatheringState = (RTCIceGatheringState state) async {
Logs().v('[VOIP] IceGatheringState => ${state.toString()}'); Logs().v('[VOIP] IceGatheringState => ${state.toString()}');
if (state == RTCIceGatheringState.RTCIceGatheringStateGathering) { if (state == RTCIceGatheringState.RTCIceGatheringStateGathering) {
Timer(Duration(milliseconds: 3000), () async { Timer(Duration(milliseconds: 3000), () async {
@ -873,7 +873,7 @@ class CallSession {
} }
} }
}; };
pc.onIceConnectionState = (RTCIceConnectionState state) { pc!.onIceConnectionState = (RTCIceConnectionState state) {
Logs().v('[VOIP] RTCIceConnectionState => ${state.toString()}'); Logs().v('[VOIP] RTCIceConnectionState => ${state.toString()}');
if (state == RTCIceConnectionState.RTCIceConnectionStateConnected) { if (state == RTCIceConnectionState.RTCIceConnectionStateConnected) {
localCandidates.clear(); localCandidates.clear();
@ -899,8 +899,8 @@ class CallSession {
}); });
streams.clear(); streams.clear();
if (pc != null) { if (pc != null) {
await pc.close(); await pc!.close();
await pc.dispose(); await pc!.dispose();
} }
} }
@ -945,8 +945,8 @@ class CallSession {
// Needs restart ice on session.pc and renegotiation. // Needs restart ice on session.pc and renegotiation.
iceGatheringFinished = false; iceGatheringFinished = false;
final desc = final desc =
await pc.createOffer(_getOfferAnswerConstraints(iceRestart: true)); await pc!.createOffer(_getOfferAnswerConstraints(iceRestart: true));
await pc.setLocalDescription(desc); await pc!.setLocalDescription(desc);
localCandidates.clear(); localCandidates.clear();
} }
@ -1101,7 +1101,7 @@ class CallSession {
terminate(CallParty.kLocal, CallErrorCode.NoUserMedia, false); terminate(CallParty.kLocal, CallErrorCode.NoUserMedia, false);
} }
void onSelectAnswerReceived(String selectedPartyId) { void onSelectAnswerReceived(String? selectedPartyId) {
if (direction != Direction.kIncoming) { if (direction != Direction.kIncoming) {
Logs().w('Got select_answer for an outbound call: ignoring'); Logs().w('Got select_answer for an outbound call: ignoring');
return; return;
@ -1325,7 +1325,7 @@ class VoIP with WidgetsBindingObserver {
/// Send select_answer event. /// Send select_answer event.
await event.room.selectCallAnswer( await event.room.selectCallAnswer(
callId, lifetimeMs, localPartyId!, call.remotePartyId); callId, lifetimeMs, localPartyId!, call.remotePartyId!);
} else { } else {
Logs().v('[VOIP] onCallAnswer: Session [$callId] not found!'); Logs().v('[VOIP] onCallAnswer: Session [$callId] not found!');
} }