Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onaddstream never called after re-negotiation

I'm creating a test video chat app with WebRTC. I have two peers connected successfully. I would like to add a way for a user to "re-connect" (disconnect and connect with another user. For testing purposes the "other" user is the same user as before). So, when a button is selected this code is called:

send(userId, toUserId, {disconnect: "true"});
pc.removeStream(localStream);
pc.close();
pc = new PeerConnection(servers, options);
remoteVideo.src = "";

In the above code, using the signaling method, one message is sent to the other user to alert them to disconnect. Then, the local stream is removed from the peer connection, the connection is closed, new PeerConnection is created, and the remote video is set to blank while the other video is being loaded. After that I have some code that finds another user to connect to (the same user for testing cases) and proceed to negotiate (follows the same code that worked before for the negotiation).

Using logging, it seems that every step is reached (offer/answer/icecandidate). The problem is that the onaddstream listener is never reached. Yes, pc.addStream() is called before an offer is sent. I am using a variable which contains the stream (localStream) so I won't have to ask for permission again. I'm not sure if that is the problem or not. My question: why is onaddstream not being called?

EDIT: Here's a little more code. The method that is called when the button is clicked:

function restart(){
    send(userId, toUserId, {disconnect: "true"});
    pc.removeStream(localStream);
    pc.close();
    pc = new PeerConnection(servers, options);
    remoteVideo.src = "";
    $.ajax({
        url: "http://localhost:8080/UserInterface/rs/message/creds/" + '#{sessionHandler.username}',
        type: "GET",
        success: function(data){
            data = JSON.parse(data);
            if (data != null){
                toUserId = data.toUserId;
                activeUser = data.activeUser;
                activeUser = JSON.parse(activeUser);
            }
            pc.addStream(localStream);
            sendOffer();
        }
    });
}

In the above code, the localStream variable is obtained when the getUserMedia() method is called the first time. This way I don't have to call it again when I need the stream. The sendOffer() method:

function sendOffer(){
    pc.onicecandidate = iceCandidateFound;
    if (activeUser){
        $.ajax({
            url: "http://localhost:8080/UserInterface/rs/message/sessId/" + toUserId,
            type: "GET",
            success: function(data){
                toSessionId = data;
                pc.createOffer(function (offer) {
                    pc.setLocalDescription(offer);
                    var offerMessage = {
                        "offer": offer,
                    };
                    send(userId, toUserId, offerMessage);
                }, errorHandler, constraints);
            }
        });
    }
}

Using logging and chrome://webrtc-internals, it's visible all the appropriate steps are gone through successfully (ex. offer/answer). And the rest of the code including signaling is all the same as used upon the first connection which worked fine. The only problem is that onaddstream is never called.

like image 473
chRyNaN Avatar asked Jan 25 '26 04:01

chRyNaN


1 Answers

The solution to my problem was very simple and something I completely overlooked. I forgot to re-state the event listener onaddstream on the new PeerConnection object. Before I had something like this:

pc.onaddstream = function(ev){
    console.log("onaddstream");
    remoteVideo.src = URL.createObjectURL(ev.stream);
};

Which worked fine for the first video connection. But then, in the restart() method I re-instantiated the pc object. Like so:

pc = new PeerConnection(servers, options);

However, I did not put the event listener back on the object. So, what I did was make a function:

function remoteStreamAdded(ev){
    console.log("onaddstream");
    remoteVideo.src = URL.createObjectURL(ev.stream);
}

And, whenever I needed to place the event listener I can call this method, like so:

pc = new PeerConnection(servers, options);
pc.onaddstream = remoteStreamAdded;
like image 126
chRyNaN Avatar answered Jan 26 '26 20:01

chRyNaN