From 3faa7a2094b9bbbf2d1a8be2b9c67276a7b1e6f0 Mon Sep 17 00:00:00 2001 From: CloudWebRTC Date: Wed, 19 Oct 2022 15:30:49 +0800 Subject: [PATCH] fix ice config issues. (#192) * fix ice config issues. * update comment. * The client has high-priority to set ice settings. --- lib/src/core/engine.dart | 53 ++++++++++++++++++++++++++++------------ lib/src/extensions.dart | 2 +- 2 files changed, 38 insertions(+), 17 deletions(-) diff --git a/lib/src/core/engine.dart b/lib/src/core/engine.dart index 5c9e5ccb..395173c8 100644 --- a/lib/src/core/engine.dart +++ b/lib/src/core/engine.dart @@ -76,7 +76,7 @@ class Engine extends Disposable with EventsEmittable { String? get connectedServerAddress => _connectedServerAddress; // server-provided ice servers - List _serverProvidedIceServers = []; + List _serverProvidedIceServers = []; late final _signalListener = signalClient.createListener(synchronized: true); @@ -337,27 +337,42 @@ class Engine extends Disposable with EventsEmittable { } } - Future _configurePeerConnections({bool? forceRelay}) async { + Future _configurePeerConnections( + {required lk_models.ClientConfigSetting forceRelay, + required List serverProvidedIceServers}) async { if (publisher != null || subscriber != null) { logger.warning('Already configured'); return; } // RTCConfiguration? config; - // use server-provided iceServers if not provided by user - final serverIceServers = - _serverProvidedIceServers.map((e) => e.toSDKType()).toList(); - RTCConfiguration rtcConfiguration = connectOptions.rtcConfiguration; - if (serverIceServers.isNotEmpty) { - // use server provided iceServers if exists + + // The server provided iceServers are only used if + // the client's iceServers are not set. + if (rtcConfiguration.iceServers == null && + serverProvidedIceServers.isNotEmpty) { rtcConfiguration = connectOptions.rtcConfiguration - .copyWith(iceServers: serverIceServers); + .copyWith(iceServers: serverProvidedIceServers); } - if (forceRelay != null && forceRelay) { - rtcConfiguration = rtcConfiguration.copyWith( - iceTransportPolicy: RTCIceTransportPolicy.relay); + // set forceRelay + if (rtcConfiguration.iceTransportPolicy == null) { + switch (forceRelay) { + case lk_models.ClientConfigSetting.ENABLED: + rtcConfiguration = rtcConfiguration.copyWith( + iceTransportPolicy: RTCIceTransportPolicy.relay, + ); + break; + case lk_models.ClientConfigSetting.DISABLED: + rtcConfiguration = rtcConfiguration.copyWith( + iceTransportPolicy: RTCIceTransportPolicy.all, + ); + break; + case lk_models.ClientConfigSetting.UNSET: + // do nothing + break; + } } publisher = await Transport.create(_peerConnectionCreate, @@ -627,16 +642,22 @@ class Engine extends Disposable with EventsEmittable { ..on((event) async { // create peer connections _subscriberPrimary = event.response.subscriberPrimary; - _serverProvidedIceServers = event.response.iceServers; _participantSid = event.response.participant.sid; + var iceServersFromServer = + event.response.iceServers.map((e) => e.toSDKType()).toList(); + + if (iceServersFromServer.isNotEmpty) { + _serverProvidedIceServers = iceServersFromServer; + } logger.fine('onConnected subscriberPrimary: ${_subscriberPrimary}, ' 'serverVersion: ${event.response.serverVersion}, ' - 'iceServers: ${event.response.iceServers}'); + 'iceServers: ${event.response.iceServers}, ' + 'forceRelay: $event.response.clientConfiguration.forceRelay'); await _configurePeerConnections( - forceRelay: event.response.clientConfiguration.forceRelay == - lk_models.ClientConfigSetting.ENABLED); + forceRelay: event.response.clientConfiguration.forceRelay, + serverProvidedIceServers: _serverProvidedIceServers); if (!_subscriberPrimary) { // for subscriberPrimary, we negotiate when necessary (lazy) diff --git a/lib/src/extensions.dart b/lib/src/extensions.dart index 0e8ae8b5..32cff246 100644 --- a/lib/src/extensions.dart +++ b/lib/src/extensions.dart @@ -24,7 +24,7 @@ extension ICEServerExt on lk_rtc.ICEServer { RTCIceServer toSDKType() => RTCIceServer( urls: urls, username: username.isNotEmpty ? username : null, - credential: credential.isNotEmpty ? username : null, + credential: credential.isNotEmpty ? credential : null, ); }