javascript - rtcpeerconnection 跟踪事件有时返回空流

标签 javascript webrtc video-capture mediastream rtcpeerconnection

建立首次成功的 RTC 连接的最佳方法是什么?

下面的代码有时有效,有时无效。我认为这是在 createAnswer 之前或之后调用 addIceCandidate 的问题,而且我不知道哪个更好,或者这是否是问题所在(为什么它不是一直起作用,只需将其粘贴到浏览器中,然后尝试几次,您应该会看到至少有时“调用”按钮不是一直起作用):

<body>
    <style>
        video {
            width: 300px
        }
    </style>
    <button id="s">start</button>
    <button id=c>Call</button><br>
<video id="L" autoplay muted></video>
<video id=R autoplay></video>
<video id=R2 autoplay></video>
<video id=R3 autoplay></video>
<script>
var ls, p, p2, bpl, bpr, con = {
 //   sdpSemantics: "default"
}, deets = {
    offerToReceiveAudio: 1,
    offerToReceiveVideo: 1
}

function g() {
    navigator.
    mediaDevices.getDisplayMedia().then(s => {
        ls = L.srcObject = s;

    })
}
s.onclick = e => {
    g()
};

function wow(data={}) {
    let local  = new RTCPeerConnection(con),
        remote = new RTCPeerConnection(con);

    local .addEventListener("icecandidate", e => oic(remote, e));
    remote.addEventListener("icecandidate", e => oic(local , e));

    remote.addEventListener("track", e => grs(data.video, e));

    data
        .localStream
        .getTracks()
        .forEach(t => {
            local.addTrack(t, data.localStream);
        });

    local.createOffer(deets).then(offer => {
        local .setLocalDescription(offer);
        remote.setRemoteDescription(offer);

        remote.createAnswer().then(answer => {
            remote.setLocalDescription(answer);
            local .setRemoteDescription(answer);
        })
    });
}

c.onclick = e => {
    let localStream = ls;
    wow({
        video: R,
        localStream
    });

    wow({
        video: R2,
        localStream
    });

    wow({
        video: R3,
        localStream
    });

};

function grs(vid,e) { 
    if(vid.srcObject !== e.streams[0]) {
        vid.srcObject = e.streams[0];
    }
}

function oic(pc, e) {
    let other = pc;
    if(e.candidate)
        other
        .addIceCandidate(e.candidate)
}
</script>
</video>
</body>

请注意,有时视频流会稍后进入并为空。

最佳答案

我之前使用PeerConnection API时遇到了同样的问题,问题是Unified Plan SDP格式,也许值得一读。

关于javascript - rtcpeerconnection 跟踪事件有时返回空流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60825809/

相关文章:

javascript - Firebase Nodejs返回值错误

node.js - peerjs连接打开,但没有收到数据

php - 使用 PHP 生成 Javascript 文件

javascript - JQgrid内联编辑第二次之前选择的行不编辑?

webrtc - SFM 和 SFU 有什么区别?

c++ - 使用 ycbcr 格式捕获相机图像

swift - 如果视频超过 12 秒则没有声音

c++ - 自定义 avi/MP4 文件编写器

javascript - d3如何做并发动画?

javascript - 在 UWP javascript 应用程序的 iframe 中获取 getUserMedia 的用户权限