gpt4 book ai didi

javascript - 双方远程视频均不显示

转载 作者:行者123 更新时间:2023-12-01 01:21:47 25 4
gpt4 key购买 nike

当前使用 Chrome 70、Firefox 64 和 Safari 12。

来自其他用户的远程视频未在两侧显示,我不太确定可能是什么问题。

没有来自任何浏览器的错误,这对调试代码没有帮助。

我正在使用 chrome 的内部 WebRTC 调试工具 (chrome://webrtc-internals),并且已发送或接收的数据包为零。

该工具中有一个参数是 googCandidatePair,但在通话过程中根本不会显示。 enter image description here

ICEgatheringstatechange 事件触发并声明它已完成,但仅当主机是 chrome 用户时。

我也尝试过使用

pc.oniceconnectionstatechange = () => console.log(pc.iceConnectionState);

检查 ICE 状态更改,但这根本不会触发。

我认为它可能无法正常工作的一个原因可能是由于 RTCPeerconnection 的配置方式(如图所示),Ice 候选池大小为 0,但代码本身从未说明。 enter image description here

下面有两张图片,第一张是主机是chrome的时候,第二张是接收器的时候 enter image description here enter image description here

代码如下:

'use strict';

var configuration = {
iceServers: [
{
urls: 'stun:stun.l.google.com:19302'
}
]
};
var pc = new RTCPeerConnection(configuration);

// Define action buttons.
const callButton = document.getElementById('callButton');
const hangupButton = document.getElementById('hangupButton');

/////////////////////////////////////////////

window.room = prompt('Enter room name:');

var socket = io.connect();

if (room !== '') {
console.log('Message from client: Asking to join room ' + room);
socket.emit('create or join', room);
}

socket.on('created', function(room) {
console.log('Created room ' + room);
startVideo();
});

socket.on('full', function(room) {
console.log('Message from client: Room ' + room + ' is full :^(');
});

socket.on('joined', function(room) {
console.log('joined: ' + room);
startVideo();
callButton.disabled = true;
});

socket.on('log', function(array) {
console.log.apply(console, array);
});

////////////////////////////////////////////////

async function sendMessage(message) {
console.log('Client sending message: ', message);
await socket.emit('message', message);
}

// This client receives a message
socket.on('message', message => {
if (message.sdp) {
pc.setRemoteDescription(new RTCSessionDescription(message.sdp))
.then(function() {
if (pc.setRemoteDescription.type === 'offer') {
pc.setLocalDescription(pc.createAnswer())
.then(function() {
sendMessage({ sdp: pc.localDescription });
})
.catch(function(err) {
console.log(err.name + ': ' + err.message);
});
}
})
.catch(error => console.error(error));
} else if (message.candidate) {
pc.addIceCandidate(new RTCIceCandidate(message.candidate))
.then(() => {
console.log('Candidates received');
})
.catch(error => console.error(error));
}
});

pc.onicecandidate = event => {
if (event.candidate) {
sendMessage({ candidate: event.candidate });
}
};

pc.ontrack = event => {
if (remoteVideo.srcObject !== event.streams[0]) {
remoteVideo.srcObject = event.streams[0];
console.log('Got remote stream');
}
};

////////////////////////////////////////////////////

const localVideo = document.querySelector('#localVideo');
const remoteVideo = document.querySelector('#remoteVideo');

// Set up initial action buttons status: disable call and hangup.
callButton.disabled = true;
hangupButton.disabled = true;

// Add click event handlers for buttons.
callButton.addEventListener('click', callStart);
hangupButton.addEventListener('click', hangupCall);

function startVideo() {
navigator.mediaDevices
.getUserMedia({
audio: true,
video: true
})
.then(function(stream) {
localVideo.srcObject = stream;
stream.getTracks().forEach(track => pc.addTrack(track, stream));
})
.catch(function(err) {
console.log('getUserMedia() error: ' + err.name);
});
callButton.disabled = false;
}

async function callStart() {
callButton.disabled = true;
hangupButton.disabled = false;

console.log('Sending offer to peer');
await pc
.setLocalDescription(await pc.createOffer())
.then(() => {
sendMessage({ sdp: pc.localDescription });
})
.catch(err => {
console.log(err.name + ': ' + err.message);
});
}

/////////////////////////////////////////////////////////

function hangupCall() {
pc.close();
pc = null;
callButton.disabled = false;
hangupButton.disabled = true;
console.log('Call Ended');
}

最佳答案

您正在混合您的 promise 样式,并且这里有一个错误:

          pc.setLocalDescription(pc.createAnswer()) // bug!
.then(function() {

上面将本地描述设置为 Promise 对象。要么选择async/await:

          await pc.setLocalDescription(await pc.createAnswer());

...或 .then() 链:

          pc.createAnswer()
.then(answer => pc.setLocalDescription(answer))
.then(function() {

如果您选择后者,请不要忘记return all the promises .

这是仅使用 async/await 完成的消息处理程序:

// This client receives a message
socket.on('message', async message => {
try {
if (message.sdp) {
await pc.setRemoteDescription(message.sdp);
if (pc.setRemoteDescription.type === 'offer') {
await pc.setLocalDescription(await pc.createAnswer());
sendMessage({sdp: pc.localDescription});
}
} else if (message.candidate) {
await pc.addIceCandidate(message.candidate);
console.log('Candidates received');
}
} catch (err) {
console.log(err.name + ': ' + err.message);
}
}

关于javascript - 双方远程视频均不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54163272/

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