【发布时间】:2019-04-23 04:51:56
【问题描述】:
你好,我是 WebRTC 的新手,我试过这段代码
const yourVideo = document.querySelector("#face_cam_vid");
const theirVideo = document.querySelector("#thevid");
(async () => {
if (!("mediaDevices" in navigator) || !("RTCPeerConnection" in window)) {
alert("Sorry, your browser does not support WebRTC.");
return;
}
const stream = await navigator.mediaDevices.getUserMedia({video: true,
audio: true});
yourVideo.srcObject = stream;
const configuration = {
iceServers: [{urls: "stun:stun.1.google.com:19302"}]
};
const yours = new RTCPeerConnection(configuration);
const theirs = new RTCPeerConnection(configuration);
for (const track of stream.getTracks()) {
yours.addTrack(track, stream);
}
theirs.ontrack = e => theirVideo.srcObject = e.streams[0];
yours.onicecandidate = e => theirs.addIceCandidate(e.candidate);
theirs.onicecandidate = e => yours.addIceCandidate(e.candidate);
const offer = await yours.createOffer();
await yours.setLocalDescription(offer);
await theirs.setRemoteDescription(offer);
const answer = await theirs.createAnswer();
await theirs.setLocalDescription(answer);
await yours.setRemoteDescription(answer);
})();
它有效,但部分有效 https://imgur.com/a/nG7Xif6 。简而言之,我正在创建 ONE-to-ONE 随机视频聊天,就像在 omegle 中一样,但是这段代码在我的本地流中同时显示“远程”(陌生人)和“本地”(“我的”)视频,但我想要的是,用户等待第二个用户进行视频聊天,当第三个用户进入时,它应该等待第四个等等。我希望有人能帮助我
【问题讨论】:
标签: javascript webrtc