我希望逻辑没有缺陷。
第 1 步:来电者创建报价
第 2 步:调用者设置 localDescription
第三步:调用者将描述发送给被调用者
//------------------------------------------------ ------//
第四步:被调用者接收offer sets remote description
第 5 步:被调用者创建答案
第六步:被叫设置本地描述
第 7 步:被调用者将描述发送给调用者
//------------------------------------------------ ------//
第 8 步:呼叫者收到应答并设置远程描述
这是上面的代码
const socket = io();
const constraints = {
audio: true,
video: true
};
const configuration = {
iceServers: [{
"url": "stun:23.21.150.121"
}, {
"url": "stun:stun.l.google.com:19302"
}]
};
const selfView = $('#selfView')[0];
const remoteView = $('#remoteView')[0];
var pc = new RTCPeerConnection(configuration);
pc.onicecandidate = ({
candidate
}) => {
socket.emit('message', {
to: $('#remote').val(),
candidate: candidate
});
};
pc.onnegotiationneeded = async () => {
try {
await pc.setLocalDescription(await pc.createOffer());
socket.emit('message', {
to: $('#remote').val(),
desc: pc.localDescription
});
} catch (err) {
console.error(err);
}
};
pc.ontrack = (event) => {
// don't set srcObject again if it is already set.
if (remoteView.srcObject) return;
remoteView.srcObject = event.streams[0];
};
socket.on('message', async ({
from,
desc,
candidate
}) => {
$('#remote').val(from);
try {
if (desc) {
// if we get an offer, we need to reply with an answer
if (desc.type === 'offer') {
await pc.setRemoteDescription(desc);
const stream = await navigator.mediaDevices.getUserMedia(constraints);
stream.getTracks().forEach((track) => pc.addTrack(track, stream));
selfView.srcObject = stream;
await pc.setLocalDescription(await pc.createAnswer());
console.log(pc.localDescription);
socket.emit({
to: from,
desc: pc.localDescription
});
} else if (desc.type === 'answer') {
await pc.setRemoteDescription(desc).catch(err => console.log(err));
} else {
console.log('Unsupported SDP type.');
}
} else if (candidate) {
await pc.addIceCandidate(new RTCIceCandidate(candidate)).catch(err => console.log(err));
}
} catch (err) {
console.error(err);
}
});
async function start() {
try {
// get local stream, show it in self-view and add it to be sent
const stream = await requestUserMedia(constraints);
stream.getTracks().forEach((track) => pc.addTrack(track, stream));
attachMediaStream(selfView, stream);
} catch (err) {
console.error(err);
}
}
socket.on('id', (data) => {
$('#myid').text(data.id);
});
// this function is called once the caller hits connect after inserting the unique id of the callee
async function connect() {
try {
await pc.setLocalDescription(await pc.createOffer());
socket.emit('message', {
to: $('#remote').val(),
desc: pc.localDescription
});
} catch (err) {
console.error(err);
}
}
socket.on('error', data => {
console.log(data);
});
现在此代码在执行第 8 步时抛出错误
DOMException:无法在“RTCPeerConnection”上执行“setRemoteDescription”:无法设置远程报价 sdp:在错误状态下调用:kHaveLocalOffer
DOMException:无法在“RTCPeerConnection”上执行“addIceCandidate”:处理 ICE 候选者时出错
尝试调试但未发现逻辑或代码中的任何缺陷。注意到该pc
对象具有的一件奇怪的事情,localDescription
并且currentLocalDescription
我认为创建答案的被调用者必须同时具有描述类型 to beanswer
而是显示localDescription
to beoffer
和currentLocalDescription
type is answer
。
提前致谢。