diff --git a/example/pubspec.lock b/example/pubspec.lock index 055043cc..db456985 100644 --- a/example/pubspec.lock +++ b/example/pubspec.lock @@ -105,7 +105,7 @@ packages: name: dart_webrtc url: "https://pub.dartlang.org" source: hosted - version: "1.0.8" + version: "1.0.9" dbus: dependency: transitive description: @@ -232,7 +232,7 @@ packages: name: flutter_webrtc url: "https://pub.dartlang.org" source: hosted - version: "0.9.8" + version: "0.9.9+hotfix.1" google_fonts: dependency: "direct main" description: diff --git a/lib/src/core/engine.dart b/lib/src/core/engine.dart index 7d0d318f..5e3f7d5d 100644 --- a/lib/src/core/engine.dart +++ b/lib/src/core/engine.dart @@ -73,6 +73,9 @@ class Engine extends Disposable with EventsEmittable { bool _subscriberPrimary = false; String? _participantSid; + String? _connectedServerAddress; + String? get connectedServerAddress => _connectedServerAddress; + // server-provided ice servers List _serverProvidedIceServers = []; @@ -367,11 +370,27 @@ class Engine extends Disposable with EventsEmittable { signalClient.sendIceCandidate(candidate, lk_rtc.SignalTarget.PUBLISHER); }; + publisher?.pc.onIceConnectionState = + (rtc.RTCIceConnectionState state) async { + logger.fine('publisher iceConnectionState: $state'); + if (state == rtc.RTCIceConnectionState.RTCIceConnectionStateConnected) { + await _handleGettingConnectedServerAddress(publisher!.pc); + } + }; + subscriber?.pc.onIceCandidate = (rtc.RTCIceCandidate candidate) { logger.fine('subscriber onIceCandidate'); signalClient.sendIceCandidate(candidate, lk_rtc.SignalTarget.SUBSCRIBER); }; + subscriber?.pc.onIceConnectionState = + (rtc.RTCIceConnectionState state) async { + logger.fine('subscriber iceConnectionState: $state'); + if (state == rtc.RTCIceConnectionState.RTCIceConnectionStateConnected) { + await _handleGettingConnectedServerAddress(subscriber!.pc); + } + }; + publisher?.onOffer = (offer) { logger.fine('publisher onOffer'); signalClient.sendOffer(offer); @@ -529,6 +548,20 @@ class Engine extends Disposable with EventsEmittable { } } + Future _handleGettingConnectedServerAddress( + rtc.RTCPeerConnection pc) async { + try { + var remoteAddress = await getConnectedAddress(publisher!.pc); + logger.fine('Connected address: $remoteAddress'); + if (_connectedServerAddress == null || + _connectedServerAddress != remoteAddress) { + _connectedServerAddress = remoteAddress; + } + } catch (e) { + logger.warning('could not get connected server address ${e.toString()}'); + } + } + void _onDCMessage(rtc.RTCDataChannelMessage message) { // always expect binary if (!message.isBinary) { @@ -709,3 +742,49 @@ extension EngineInternalMethods on Engine { _lossyDCPub ].whereNotNull().map((e) => e.toLKInfoType()).toList(); } + +Future getConnectedAddress(rtc.RTCPeerConnection pc) async { + var selectedCandidatePairId = ''; + final candidatePairs = {}; + // id -> candidate ip + final candidates = {}; + final List stats = await pc.getStats(); + for (var v in stats) { + switch (v.type) { + case 'transport': + selectedCandidatePairId = v.values['selectedCandidatePairId'] as String; + break; + case 'candidate-pair': + if (selectedCandidatePairId == '') { + if (v.values['selected'] != null && v.values['selected'] == true) { + selectedCandidatePairId = v.id; + } + } + candidatePairs[v.id] = v; + break; + case 'remote-candidate': + var address = ''; + var port = 0; + if (v.values['address'] != null) { + address = v.values['address'] as String; + } + if (v.values['port'] != null) { + port = v.values['port'] as int; + } + candidates[v.id] = '$address:$port'; + break; + default: + } + } + + if (selectedCandidatePairId == '') { + return null; + } + + final report = candidatePairs[selectedCandidatePairId]; + if (report == null) { + return null; + } + final selectedID = report.values['remoteCandidateId'] as String; + return candidates[selectedID]; +} diff --git a/pubspec.lock b/pubspec.lock index 444ad6a1..c1337cc3 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -161,7 +161,7 @@ packages: name: dart_webrtc url: "https://pub.dartlang.org" source: hosted - version: "1.0.8" + version: "1.0.9" dbus: dependency: transitive description: @@ -267,7 +267,7 @@ packages: name: flutter_webrtc url: "https://pub.dartlang.org" source: hosted - version: "0.9.8" + version: "0.9.9+hotfix.1" glob: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index e5dbffa1..5ba9506d 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -23,8 +23,8 @@ dependencies: uuid: ^3.0.6 synchronized: ^3.0.0 protobuf: ^2.0.1 - flutter_webrtc: 0.9.8 - dart_webrtc: 1.0.8 + flutter_webrtc: 0.9.9+hotfix.1 + dart_webrtc: 1.0.9 device_info_plus: ^3.2.3 webrtc_interface: 1.0.8