Skip to content

Commit

Permalink
Supports getting the connected remote address. (#177)
Browse files Browse the repository at this point in the history
* chore: Supports getting the connected remote address.

* chore: Merge duplicate code.

* chore: update.

* update pub deps.

* Migrate to webrtc standard stats.
  • Loading branch information
cloudwebrtc authored Oct 12, 2022
1 parent 03c1fdc commit eaacbf7
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 6 deletions.
4 changes: 2 additions & 2 deletions example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
79 changes: 79 additions & 0 deletions lib/src/core/engine.dart
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ class Engine extends Disposable with EventsEmittable<EngineEvent> {
bool _subscriberPrimary = false;
String? _participantSid;

String? _connectedServerAddress;
String? get connectedServerAddress => _connectedServerAddress;

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

Expand Down Expand Up @@ -367,11 +370,27 @@ class Engine extends Disposable with EventsEmittable<EngineEvent> {
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);
Expand Down Expand Up @@ -529,6 +548,20 @@ class Engine extends Disposable with EventsEmittable<EngineEvent> {
}
}

Future<void> _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) {
Expand Down Expand Up @@ -709,3 +742,49 @@ extension EngineInternalMethods on Engine {
_lossyDCPub
].whereNotNull().map((e) => e.toLKInfoType()).toList();
}

Future<String?> getConnectedAddress(rtc.RTCPeerConnection pc) async {
var selectedCandidatePairId = '';
final candidatePairs = <String, rtc.StatsReport>{};
// id -> candidate ip
final candidates = <String, String>{};
final List<rtc.StatsReport> 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];
}
4 changes: 2 additions & 2 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit eaacbf7

Please sign in to comment.