8000 Handle addCandidate correctly. · coder-wuyan/flutter-webrtc@21d07d5 · GitHub
[go: up one dir, main page]

Skip to content

Commit 21d07d5

Browse files
committed
Handle addCandidate correctly.
1 parent 06e7f0c commit 21d07d5

File tree

2 files changed

+34
-11
lines changed

2 files changed

+34
-11
lines changed

example/lib/src/data_channel_sample.dart

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,17 @@ class _DataChannelSampleState extends State<DataChannelSample> {
3737
print(state);
3838
}
3939

40-
void _onCandidate(RTCIceCandidate candidate) {
41-
print('onCandidate: ' + candidate.candidate);
42-
_peerConnection.addCandidate(candidate);
43-
setState(() {
44-
_sdp += '\n';
45-
_sdp += candidate.candidate;
46-
});
40+
void _onCandidate(RTCIceCandidate candidate) async {
41+
try {
42+
print('onCandidate: ' + candidate.candidate);
43+
await _peerConnection.addCandidate(candidate);
44+
setState(() {
45+
_sdp += '\n';
46+
_sdp += candidate.candidate;
47+
});
48+
} catch (e) {
49+
print('_onCandidate, addCandidate failed => ${e.toString()}');
50+
}
4751
}
4852

4953
void _onRenegotiationNeeded() {
@@ -125,8 +129,8 @@ class _DataChannelSampleState extends State<DataChannelSample> {
125129

126130
_sdp = description.sdp;
127131
//change for loopback.
128-
//description.type = 'answer';
129-
//_peerConnection.setRemoteDescription(description);
132+
description.type = 'answer';
133+
await _peerConnection.setRemoteDescription(description);
130134
} catch (e) {
131135
print(e.toString());
132136
}

lib/src/web/rtc_peerconnection_impl.dart

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ import 'media_stream_track_impl.dart';
2020
import 'rtc_data_channel_impl.dart';
2121
import 'rtc_dtmf_sender_impl.dart';
2222

23+
extension RtcPeerConnection$Ext on html.RtcPeerConnection {
24+
js.JsObject get jsObject => js.JsObject.fromBrowserObject(this);
25+
}
26+
2327
/*
2428
* PeerConnection
2529
*/
@@ -194,8 +198,23 @@ class RTCPeerConnectionWeb extends RTCPeerConnection {
194198

195199
@override
196200
Future<void> addCandidate(RTCIceCandidate candidate) async {
197-
await jsutil.promiseToFuture(
198-
jsutil.callMethod(_jsPc, 'addIceCandidate', [_iceToJs(candidate)]));
201+
try {
202+
Completer completer = Completer<void>();
203+
var success = js.allowInterop(() {
204+
print('addCandidate success');
205+
completer.complete();
206+
});
207+
var failure = js.allowInterop((e) {
208+
print('addCandidate falied => ${e.toString()}');
209+
completer.completeError(e);
210+
});
211+
jsutil.callMethod(
212+
_jsPc, 'addIceCandidate', [_iceToJs(candidate), success, failure]);
213+
214+
return completer.future;
215+
} catch (e) {
216+
print(e.toString());
217+
}
199218
}
200219

201220
@override

0 commit comments

Comments
 (0)
0