【问题标题】:onaddstream never called after re-negotiation重新协商后从未调用 onaddstream
【发布时间】:2014-12-06 04:07:04
【问题描述】:

我正在使用 WebRTC 创建一个测试视频聊天应用。我有两个对等点连接成功。我想为用户添加一种“重新连接”的方式(断开连接并与另一个用户连接。出于测试目的,“其他”用户与以前的用户相同)。所以,当一个按钮被选中时,这个代码被调用:

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

在上面的代码中,使用信令方法,向其他用户发送一条消息以提醒他们断开连接。然后,从对等连接中删除本地流,关闭连接,创建新的 PeerConnection,并且在加载其他视频时将远程视频设置为空白。之后,我有一些代码可以找到另一个要连接的用户(测试用例的同一用户)并继续进行协商(遵循之前用于协商的相同代码)。

使用日志记录,似乎每一步都已完成(offer/answer/icecandidate)。问题是永远无法到达 onaddstream 侦听器。是的,在发送报价之前调用 pc.addStream()。我正在使用一个包含流(localStream)的变量,所以我不必再次请求许可。我不确定这是否是问题所在。我的问题:为什么不调用 onaddstream?

编辑:这里还有一点代码。点击按钮时调用的方法:

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();
        }
    });
}

在上面的代码中,第一次调用getUserMedia()方法时得到了localStream变量。这样,当我需要流时,我不必再次调用它。 sendOffer() 方法:

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);
            }
        });
    }
}

使用日志记录和 chrome://webrtc-internals,可以看到所有适当的步骤都已成功完成(例如 offer/answer)。包括信号在内的其余代码与第一次连接时使用的代码相同,并且工作正常。唯一的问题是永远不会调用 onaddstream。

【问题讨论】:

  • 你能发布你的代码吗?或者至少链接项目?双方都在完全重建连接吗?
  • @BenjaminTrent 我添加了更多代码,不幸的是我没有该项目的链接。似乎两个对等点都是完全连接的。我使用了 webrtc-internals,它显示了所有连接成功的步骤。
  • 是否被告知重启?还是只有一个重新启动而另一个对等点保持不变?
  • 双方重启。一个对等方将断开连接消息发送给另一个。收到断开连接消息后,其他对等方也会重新启动。
  • 我实际上有一个类似的错误,我没有将事件侦听器设置为娱乐。很高兴你明白了:)

标签: javascript google-chrome firefox webrtc


【解决方案1】:

我的问题的解决方案非常简单,但我完全忽略了这一点。我忘记在新的 PeerConnection 对象上重新声明事件监听器 onaddstream。在我有这样的事情之前:

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

第一个视频连接效果很好。但是,在 restart() 方法中,我重新实例化了 pc 对象。像这样:

pc = new PeerConnection(servers, options);

但是,我没有将事件侦听器放回对象上。所以,我所做的就是创建一个函数:

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

而且,每当我需要放置事件侦听器时,我都可以调用此方法,如下所示:

pc = new PeerConnection(servers, options);
pc.onaddstream = remoteStreamAdded;

【讨论】:

    猜你喜欢
    • 2013-01-07
    • 1970-01-01
    • 1970-01-01
    • 2015-09-29
    • 2018-09-22
    • 1970-01-01
    • 2016-03-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多