【问题标题】:WebRTC - Failed to set remote answer sdp: Called in wrong state: STATE_INPROGRESSWebRTC - 无法设置远程应答 sdp:在错误状态下调用:STATE_INPROGRESS
【发布时间】:2017-05-22 14:03:39
【问题描述】:

我在这里按照示例进行操作:https://www.w3.org/TR/webrtc/#simple-peer-to-peer-example

我已经修改了代码,因为我只需要单向流:

var configuration = null; //{ "iceServers": [{ "urls": "stuns:stun.example.org" }] };
var peerConnection;

var outboundPeerStream = null;
var outboundPeerStreamSessionId = null;

var createPeerConnection = function () {
    if (peerConnection)
        return;

    peerConnection = new RTCPeerConnection(configuration);

    // send any ice candidates to the other peer
    peerConnection.onicecandidate = function (event) {
        signalrModule.sendClientNotification(JSON.stringify({ "candidate": event.candidate }));
    };

    // let the "negotiationneeded" event trigger offer generation
    peerConnection.onnegotiationneeded = peerStreamingModule.sendOfferToPeers;

    // once remote track arrives, show it in the remote video element
    peerConnection.ontrack = function (event) {
        var inboundPeerStream = event.streams[0];
        remoteStreamHelper.pushStreamToDom(inboundPeerStream, foo);
    }
}

// this gets called either on negotiationNeeded and every 30s to ensure all peers have the offer from the stream originator
peerStreamingModule.sendOfferToPeers = function () {
    peerConnection.createOffer().then(function (offer) {
        return peerConnection.setLocalDescription(offer);
    }).then(function () {
        // send the offer to the other peer
        signalrModule.sendClientNotification(JSON.stringify({ "desc": peerConnection.localDescription}));
    }).catch(logger.internalLog);
};

// this gets called by the stream originator when the stream is available to initiate streaming to peers
peerStreamingModule.initializeWithStream = function (outboundStream, sessionId) {
    outboundPeerStream = outboundStream;
    outboundPeerStreamSessionId = sessionId;
    createPeerConnection();
    peerConnection.addStream(outboundStream);
    //peerStreamingModule.sendOfferToPeers(); I don't think I need this...
}

peerStreamingModule.handleP2PEvent = function (notification) {
    if (!peerConnection)
        createPeerConnection();

    if (notification.desc) {
        var desc = notification.desc;
        // if we get an offer, we need to reply with an answer
        if (desc.type == "offer") {
            peerConnection.setRemoteDescription(desc).then(function () {
                return peerConnection.createAnswer();
            }).then(function (answer) {
                return peerConnection.setLocalDescription(answer);
            }).then(function () {
               signalrModule.sendClientNotification(JSON.stringify({ "desc": peerConnection.localDescription, "sessionId": sessionManager.thisSession().deviceSessionId() }), app.username());
            }).catch(logger.internalLog);
        } else if (desc.type == "answer") {

  peerConnection.setRemoteDescription(desc).catch(logger.internalLog);
        } else {
            logger.internalLog("Unsupported SDP type. Your code may differ here.");
        }
    } else
        pc.addIceCandidate(notification.candidate).catch(logger.internalLog);
}

这似乎有效,但我被两部分难住了:

1) WebRTC - Failed to set remote answer sdp: Called in wrong state: STATE_INPROGRESS - 这不时出现在我的日志中 - 我在上面做错了什么导致了这个吗?

2) 我是否正确实现了sendOfferToPeersinitializeWithStream?恐怕sendOfferToPeers 从发起者的间隔触发不是规范的使用方式;我的目标是确保所有同行最终都收到报价,无论他们何时加入,或者他们是否面临放弃原始报价/协商的连接问题。

【问题讨论】:

    标签: webrtc


    【解决方案1】:

    // 在协商需要时和每 30 秒调用一次,以确保所有对等方都有报价

    您不能将相同的报价发送给多个同行。它是点对点的,而不是点对点的。一对多需要每个参与者至少有一个连接,并且可能需要一个媒体服务器来扩展。

    另外,SDP 不适用于discovery。提议/答案交换只是在两个端点之间进行的脆弱的state-machine 协商,以建立单个连接。

    您应该在建立 WebRTC 连接之前解决谁与谁连接。

    【讨论】:

    • 谢谢你。我刚刚更新了我的实现,为每个“对等对”创建两个连接。这似乎在某种程度上有效,但我现在面临多个同行的问题 - 将不胜感激您可能有的任何意见:stackoverflow.com/questions/44157738/…
    猜你喜欢
    • 2016-09-21
    • 1970-01-01
    • 1970-01-01
    • 2021-05-09
    • 1970-01-01
    • 1970-01-01
    • 2018-08-04
    • 2016-10-13
    • 1970-01-01
    相关资源
    最近更新 更多