rtcpeerconnection跟踪事件有时返回空流

问题描述 投票:1回答:1

我无法定期重现此内容,但是有时调用了我的rtcpeerconnection的跟踪事件,但该事件被提供为空流,因此我的远程视频保持空白;但是,在其他时候,我希望有人至少可以在代码上快速了解一下我是否缺少什么?

var ls, p, p2, con = {
 //   sdpSemantics: "default"
}, deets = {
    offerToReceiveAudio: 1,
    offerToReceiveVideo: 1
}
navigator.
    mediaDevices.getDisplayMedia().then(s => {
        ls = L.srcObject = s;
        
    })
s.onclick = e => {
    navigator.
    mediaDevices.getDisplayMedia().then(s => {
        ls = L.srcObject = s;
        
    })
};

c.onclick = e => {
    p = new RTCPeerConnection(con);
    p.addEventListener("icecandidate", e => oic(p, e));
    
    p2 = new RTCPeerConnection(con);
    p2.addEventListener("icecandidate", e => oic(p2, e));
    

    p.addEventListener("iceconnectionstatechange", e => oisc(p,e));

    p2.addEventListener("iceconnectionstatechange", e => oisc(p2, e))

    
    p2.addEventListener("track", grs);



    ls.getTracks()
    .forEach(t => {
        p.addTrack(t, ls);
    })

    p2.addEventListener("onnegotiationneeded", e => console.log("n", e))
  //  console.log("Trackt", ls)

    p.createOffer(deets).then(of => {
        console.log("wow!", of)
        p.setLocalDescription(of).then(() => {
            console.log("added ld")
            p2.setRemoteDescription(of)
            .then(() => {
                console.log("added 2rd")
                p2.createAnswer()
                .then(desc => {
                    console.log("added 2a")
                    p2.setLocalDescription(desc)
                    .then(() => {
                        console.log("added 2ld")
                        p.setRemoteDescription(desc)
                        .then((d) => {
                            console.log("added d")
                            console.log("set!",d)
                        });

                    })
                })
            })

        });

        

    }).catch(er => console.log("NO!", er));
};

function grs(e) {
    console.log("a TRACK tive");
    if(R.srcObject !== e.streams[0]) {
        R.srcObject = e.streams[0];
        console.log("Stramd");
    }
}

function oisc(pc, e) {
 //   console.log("CHANGED?!", pc.iceConnectionState);
}

function getName(pc) {
    return pc === p ? "p" : "p2";
}

function oic(pc, e) {
    let other = pc === p ? p2 : p;
  /*  console.log("trying to ", getName(pc), " to ", getName(other),
        "with",
        e.candidate
    )*/
    if(e.candidate)
        other
        .addIceCandidate(e.candidate)
        .then(() => {
  //s         console.log("did it?!", pc)
        }).catch(er => console.log(er))
    
}
video {
            width: 300px
        }
<button id="s">start</button>
    <button id=c>Call</button><br>
<video id="L" autoplay muted></video>
<video id=R autoplay></video>

(意识到它将无法在小部件中工作,但只需尝试将其复制并粘贴到某个地方)。

根据:https://github.com/webrtc/samples/blob/gh-pages/src/content/peerconnection/pc1/js/main.js

同样,在大多数情况下它确实起作用,但是有时它返回一个空流,并且这种情况在其他实现之前有时已经发生过

javascript webrtc video-capture mediastream rtcpeerconnection
1个回答
0
投票

look again at documentation。首先,您使用的是异步功能,因此当时无法解析,然后再调用它(例如,用户不回答提示或浏览器完全拒绝它)。第二,您没有处理错误,请在代码中添加catch()块,浏览器将回答您的问题本身

© www.soinside.com 2019 - 2024. All rights reserved.