Skip to content

Commit

Permalink
fix ice config issues. (#192)
Browse files Browse the repository at this point in the history
* fix ice config issues.

* update comment.

* The client has high-priority to set ice settings.
  • Loading branch information
cloudwebrtc authored Oct 19, 2022
1 parent 7acfb3f commit 3faa7a2
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 17 deletions.
53 changes: 37 additions & 16 deletions lib/src/core/engine.dart
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class Engine extends Disposable with EventsEmittable<EngineEvent> {
String? get connectedServerAddress => _connectedServerAddress;

// server-provided ice servers
List<lk_rtc.ICEServer> _serverProvidedIceServers = [];
List<RTCIceServer> _serverProvidedIceServers = [];

late final _signalListener = signalClient.createListener(synchronized: true);

Expand Down Expand Up @@ -337,27 +337,42 @@ class Engine extends Disposable with EventsEmittable<EngineEvent> {
}
}

Future<void> _configurePeerConnections({bool? forceRelay}) async {
Future<void> _configurePeerConnections(
{required lk_models.ClientConfigSetting forceRelay,
required List<RTCIceServer> 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,
Expand Down Expand Up @@ -627,16 +642,22 @@ class Engine extends Disposable with EventsEmittable<EngineEvent> {
..on<SignalJoinResponseEvent>((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)
Expand Down
2 changes: 1 addition & 1 deletion lib/src/extensions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
}

Expand Down

0 comments on commit 3faa7a2

Please sign in to comment.