fix(webrtc): 修正ICE服务器配置并增强连接诊断日志
修正Flutter控制端TURN凭据错误,移除不可达的内网地址,新增TCP中继候选与详细的ICE状态/候选日志,以解决iOS模拟器下ICE连接失败问题。
This commit is contained in:
@@ -110,10 +110,20 @@ class WebRtcController {
|
||||
'tcpCandidatePolicy': 'enabled',
|
||||
};
|
||||
|
||||
final iceUrls = (kIceServers)
|
||||
.map((e) => '${e['username'] != null ? '(${e['username']})' : ''}${e['urls']}')
|
||||
.join(', ');
|
||||
debugPrint('[WebRtcController] signaling server: ${signaling.serverUrl} | '
|
||||
'deviceId=$deviceId target=$targetDeviceId');
|
||||
debugPrint('[WebRtcController] ICE servers: $iceUrls');
|
||||
|
||||
_pc = await createPeerConnection(configuration);
|
||||
|
||||
_pc!.onIceCandidate = _onIceCandidate;
|
||||
_pc!.onIceConnectionState = _onIceConnectionState;
|
||||
_pc!.onIceGatheringState = (state) {
|
||||
debugPrint('[WebRtcController] ICE gathering state: $state');
|
||||
};
|
||||
_pc!.onTrack = _onTrack;
|
||||
_pc!.onDataChannel = _onDataChannel;
|
||||
|
||||
@@ -399,6 +409,7 @@ class WebRtcController {
|
||||
}
|
||||
|
||||
void _onIceCandidate(RTCIceCandidate candidate) {
|
||||
debugPrint('[WebRtcController] local ICE candidate: ${candidate.candidate}');
|
||||
final payload = {
|
||||
'sdpMid': candidate.sdpMid,
|
||||
'sdpMLineIndex': candidate.sdpMLineIndex,
|
||||
@@ -414,15 +425,39 @@ class WebRtcController {
|
||||
signaling.send(msg);
|
||||
}
|
||||
|
||||
/// 打印 ICE candidate-pair 统计,用于诊断连接失败原因。
|
||||
Future<void> _dumpIceStats() async {
|
||||
try {
|
||||
final stats = await _pc?.getStats();
|
||||
final pairs = <String>[];
|
||||
for (final report in stats ?? []) {
|
||||
if (report.type == 'candidate-pair') {
|
||||
final state = report.values['state'] ?? report.values['nominated'];
|
||||
pairs.add('${report.values['localCandidateId']}->'
|
||||
'${report.values['remoteCandidateId']} state=$state');
|
||||
}
|
||||
}
|
||||
debugPrint('[WebRtcController] ICE Failed. candidate-pair 数量=${pairs.length}');
|
||||
for (final p in pairs) {
|
||||
debugPrint('[WebRtcController] pair: $p');
|
||||
}
|
||||
} catch (e) {
|
||||
debugPrint('[WebRtcController] getStats 失败: $e');
|
||||
}
|
||||
}
|
||||
|
||||
void _onIceConnectionState(RTCIceConnectionState state) {
|
||||
// ignore: avoid_print
|
||||
print('ICE connection state: $state');
|
||||
if (state == RTCIceConnectionState.RTCIceConnectionStateConnected) {
|
||||
if (state == RTCIceConnectionState.RTCIceConnectionStateChecking) {
|
||||
debugPrint('[WebRtcController] ICE Checking: 正在收集/交换候选,等待连通...');
|
||||
} else if (state == RTCIceConnectionState.RTCIceConnectionStateConnected) {
|
||||
_connectedAt = DateTime.now().millisecondsSinceEpoch;
|
||||
onConnectionEstablished?.call();
|
||||
} else if (state == RTCIceConnectionState.RTCIceConnectionStateDisconnected) {
|
||||
onIceDisconnected?.call('ICE 连接已断开');
|
||||
} else if (state == RTCIceConnectionState.RTCIceConnectionStateFailed) {
|
||||
_dumpIceStats();
|
||||
onConnectionFailed?.call('ICE 连接失败');
|
||||
onDisconnected?.call();
|
||||
}
|
||||
@@ -455,6 +490,7 @@ class WebRtcController {
|
||||
payload['sdpMid'] as String?,
|
||||
payload['sdpMLineIndex'] as int?,
|
||||
);
|
||||
debugPrint('[WebRtcController] remote ICE candidate: ${candidate.candidate}');
|
||||
await _pc?.addCandidate(candidate);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user