How to call addIceCandidate in dart

Issue

I want to connect two peers with RTCPeerConnections but I am not able to add the IceCandidate from Alice to Bob.

example:

var alice = new RtcPeerConnection(
   {"iceServers": [{"url": "stun:stun.l.google.com:19302"}]}
);

var bob = new RtcPeerConnection(
    {"iceServers": [{"url": "stun:stun.l.google.com:19302"}]}
);

alice.createDataChannel("somelablel", {});

alice.onNegotiationNeeded.listen((var data){
    alice.createOffer({}).then((var offer){
        //got offer
        alice.setLocalDescription(offer);
        bob.setRemoteDescription(offer);
    }); 
});

bob.onIceCandidate.listen((evt) {
    if (evt.candidate)
        print(evt.cancelable);
    });

alice.onIceCandidate.listen((evt) {
    if(evt.candidate != null)
        //TODO: add iceCandidate to Bob
});

First version (seems to be old but is heavily used in online examples):

bob.addIceCandidate(candidatefromAlice);

Output:

Class 'RtcPeerConnection' has no instance method 
'addIceCandidate' with matching arguments.

Second try (new version with 3 parameters):

bob.addIceCandidate(candidatefromAlice, (){}, (var error){
    print(error.toString());
}); 

Output:

NotSupportedError: The implementation did not support the 
requested type of object or operation. (Dartium)    

How can I set the ICE candidates in dart without problems?

Info:

Dart VM version: 0.1.2.0_r30864 (Wed Dec 4 11:03:45 2013) on “linux_x64”
dartium : Chromium 31.0.1650.48

Solution

I found a workaround by using the js-interop library. Just use proxies:

import 'package:js/js.dart' as js;

// ...

_rpc = new js.Proxy(js.context.webkitRTCPeerConnection, js.map(iceServers));

// ...

var iceCandidate = new js.Proxy(js.context.RTCIceCandidate, 
        js.context.JSON.parse(/*your icecandidate string*/)
      );

_rpc.addIceCandidate(iceCandidate);

Answered By – NaN

Answer Checked By – Senaida (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published.