gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 03:02:53 27 4
gpt4 key购买 nike

建立首次成功的 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com