Supports getting the connected remote address. (#177)

* chore: Supports getting the connected remote address.

* chore: Merge duplicate code.

* chore: update.

* update pub deps.

* Migrate to webrtc standard stats.
This commit is contained in:
CloudWebRTC
2022-10-12 10:58:24 +08:00
committed by GitHub
parent 03c1fdcf89
commit eaacbf77c5
4 changed files with 85 additions and 6 deletions
+2 -2
View File
@@ -105,7 +105,7 @@ packages:
name: dart_webrtc name: dart_webrtc
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.0.8" version: "1.0.9"
dbus: dbus:
dependency: transitive dependency: transitive
description: description:
@@ -232,7 +232,7 @@ packages:
name: flutter_webrtc name: flutter_webrtc
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "0.9.8" version: "0.9.9+hotfix.1"
google_fonts: google_fonts:
dependency: "direct main" dependency: "direct main"
description: description:
+79
View File
@@ -73,6 +73,9 @@ class Engine extends Disposable with EventsEmittable<EngineEvent> {
bool _subscriberPrimary = false; bool _subscriberPrimary = false;
String? _participantSid; String? _participantSid;
String? _connectedServerAddress;
String? get connectedServerAddress => _connectedServerAddress;
// server-provided ice servers // server-provided ice servers
List<lk_rtc.ICEServer> _serverProvidedIceServers = []; List<lk_rtc.ICEServer> _serverProvidedIceServers = [];
@@ -367,11 +370,27 @@ class Engine extends Disposable with EventsEmittable<EngineEvent> {
signalClient.sendIceCandidate(candidate, lk_rtc.SignalTarget.PUBLISHER); 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) { subscriber?.pc.onIceCandidate = (rtc.RTCIceCandidate candidate) {
logger.fine('subscriber onIceCandidate'); logger.fine('subscriber onIceCandidate');
signalClient.sendIceCandidate(candidate, lk_rtc.SignalTarget.SUBSCRIBER); 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) { publisher?.onOffer = (offer) {
logger.fine('publisher onOffer'); logger.fine('publisher onOffer');
signalClient.sendOffer(offer); signalClient.sendOffer(offer);
@@ -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) { void _onDCMessage(rtc.RTCDataChannelMessage message) {
// always expect binary // always expect binary
if (!message.isBinary) { if (!message.isBinary) {
@@ -709,3 +742,49 @@ extension EngineInternalMethods on Engine {
_lossyDCPub _lossyDCPub
].whereNotNull().map((e) => e.toLKInfoType()).toList(); ].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];
}
+2 -2
View File
@@ -161,7 +161,7 @@ packages:
name: dart_webrtc name: dart_webrtc
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.0.8" version: "1.0.9"
dbus: dbus:
dependency: transitive dependency: transitive
description: description:
@@ -267,7 +267,7 @@ packages:
name: flutter_webrtc name: flutter_webrtc
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "0.9.8" version: "0.9.9+hotfix.1"
glob: glob:
dependency: transitive dependency: transitive
description: description:
+2 -2
View File
@@ -23,8 +23,8 @@ dependencies:
uuid: ^3.0.6 uuid: ^3.0.6
synchronized: ^3.0.0 synchronized: ^3.0.0
protobuf: ^2.0.1 protobuf: ^2.0.1
flutter_webrtc: 0.9.8 flutter_webrtc: 0.9.9+hotfix.1
dart_webrtc: 1.0.8 dart_webrtc: 1.0.9
device_info_plus: ^3.2.3 device_info_plus: ^3.2.3
webrtc_interface: 1.0.8 webrtc_interface: 1.0.8