From fdee6fe5906048ece3d3ff43240e956588f6d2b9 Mon Sep 17 00:00:00 2001 From: cloudwebrtc Date: Fri, 21 Oct 2022 23:27:38 +0800 Subject: [PATCH] fix: sdp negotiation issue on iOS, close #335. Update lib/src/voip/call.dart chore: flutter format. --- lib/src/voip/call.dart | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/lib/src/voip/call.dart b/lib/src/voip/call.dart index 7f887be3..980dd78b 100644 --- a/lib/src/voip/call.dart +++ b/lib/src/voip/call.dart @@ -939,13 +939,18 @@ class CallSession { video_muted: localUserMediaStream!.stream!.getVideoTracks().isEmpty) }); + await pc!.setLocalDescription(answer); + setCallState(CallState.kConnecting); + + // Allow a short time for initial candidates to be gathered + await Future.delayed(Duration(milliseconds: 200)); + final res = await sendAnswerCall(room, callId, answer.sdp!, localPartyId, type: answer.type!, capabilities: callCapabilities, metadata: metadata); Logs().v('[VOIP] answer res => $res'); - await pc!.setLocalDescription(answer); - setCallState(CallState.kConnecting); + inviteOrAnswerSent = true; _answeredByUs = true; } @@ -1055,6 +1060,12 @@ class CallSession { return; } + if (pc!.iceGatheringState == + RTCIceGatheringState.RTCIceGatheringStateGathering) { + // Allow a short time for initial candidates to be gathered + await Future.delayed(Duration(milliseconds: 200)); + } + if (callHasEnded) return; final callCapabilities = CallCapabilities() @@ -1088,6 +1099,11 @@ class CallSession { Logs().i('Negotiation is needed!'); makingOffer = true; try { + // The first addTrack(audio track) on iOS will trigger + // onNegotiationNeeded, which causes creatOffer to only include + // audio m-line, add delay and wait for video track to be added, + // then createOffer can get audio/video m-line correctly. + await Future.delayed(Duration(milliseconds: 100)); final offer = await pc!.createOffer({}); await _gotLocalOffer(offer); } catch (e) {