4

I have been trying to get data chat working using webrtc. It was working previously in google chrome and suddenly stopped working, I have narrowed down the issue to 'ondatachannel' callback function not getting triggered. The exact same code works fine in Mozilla.

Here's the overall code:

app.pc_config = 
        {'iceServers': [
            {'url': 'stun:stun.l.google.com:19302'}
        ]};
app.pc_constraints = {
            'optional': [
              /*  {'DtlsSrtpKeyAgreement': true},*/
                {'RtpDataChannels': true}
            ]};

    var localConnection = null, remoteConnection = null;
        try {
            localConnection = new app.RTCPeerConnection(app.pc_config, app.pc_constraints);
            localConnection.onicecandidate = app.setIceCandidate;
            localConnection.onaddstream = app.handleRemoteStreamAdded;
            localConnection.onremovestream = app.handleRemoteStreamRemoved;
        }
        catch (e) {
            console.log('Failed to create PeerConnection, exception: ' + e.message);
            return;
        }           
        isStarted = true;

In Create Channel that follows this:

 var localConnection = app.localConnection;
        var sendChannel = null;
        try {
            sendChannel = localConnection.createDataChannel(app.currentchannel,
                {reliable: false});
            sendChannel.onopen = app.handleOpenState;
            sendChannel.onclose = app.handleCloseState;
            sendChannel.onerror = app.handleErrorState;
            sendChannel.onmessage = app.handleMessage;
            console.log('created send channel');

        } catch (e) {
            console.log('channel creation failed ' + e.message);
        }
        if (!app.isInitiator){
            localConnection.ondatachannel = app.gotReceiveChannel;
        }
        app.sendChannel = sendChannel;

I create Offer:

app.localConnection.createOffer(app.gotLocalDescription, app.handleError);

and Answer:

app.localConnection.createAnswer(app.gotLocalDescription, app.handleError);

the offer and answer get created successfully, candidates are exchanged and onicecandidate event is triggered at both ends! Local Description and RemoteDescription are set on both respective ends.

I have a pusher server for signalling, I am able to send and receive messages through the pusher server successfully.

The same webrtc code works for audio/video = true, the only issue is when I try to create datachannel. The only step that does not get executed is the callback function not getting executed i.e "gotReceiveChannel"

I'm starting to think it's the version of chrome.. I am not able to get the GitHub example working in chrome either: (Step4 for data chat) https://bitbucket.org/webrtc/codelab

While the same code works in Mozilla.

The sendChannel from the offerer has a "readyState" of "connecting"

Any help much appreciated.

lrleon
  • 2,610
  • 3
  • 25
  • 38
  • are you creating the datachannel after creating the offer? – mido Dec 01 '15 at 01:37
  • Shouldn't answer be created using the remoteConnection variable and ondatachannel callback should be attached for the same? I mean you should create dataChannel only for the initiator and not both ways right? Please correct me if I am wrong. Also @mido22 has put a valid point, if the data channel is created after creating the offer you might have to create the offer again and renegotiate. – Obscure Geek Dec 01 '15 at 06:08
  • I created datachannel before creating the offer. And even though I am creating datachannel on both ends, the second client's datachannel would get overridden when the callback is called, I saw a github example designed this way! – Ranjani Rajagopalan Dec 01 '15 at 14:55
  • Why don't you remove `createDataChannel()` on the receiving end and see if that makes `ondatachannel` fire in Chrome? I would also remove `RtpDataChannels` as that is obsolete and non-standard. – jib Dec 02 '15 at 00:58
  • may this answer will helps: https://stackoverflow.com/questions/43788872/how-are-data-channels-negotiated-between-two-peers-with-webrtc/43788873#43788873 – Erlang Parasu May 06 '22 at 08:52

0 Answers0