【发布时间】:2022-08-21 19:49:39
【问题描述】:
在调用另一个用户时收到此错误 \“未捕获(承诺中)DOMException:无法在 \'RTCPeerConnection\' 上执行 \'setLocalDescription\':无法设置本地报价 sdp:在错误状态下调用:have-remote-offer\”。
接收者能够看到本地和远程流,但调用者看不到远程流。
switch(type){
case \'client-candidate\':
if(pc.localDescription){
await pc.addIceCandidate(new RTCIceCandidate(data));
}
break;
case \'is-client-ready\':
if(!pc){
await getConn();
}
if(!localStream){
await getCam();
}
if(pc.iceConnectionState === \'connected\'){
send(\'clent-already-oncall\', null, presentUserId, null);
}else{
displayCall(incomingData);
$(document).on(\'click\', \'#declineBtn\', function(){
send(\'client-rejected\', sendTo, currentUserName, presentUserId, null);
$(\'#incomingCallModel\').modal(\'toggle\');
});
$(document).on(\'click\', \'#answerBtn\', function(){
send(\'client-is-ready\', sendTo, currentUserName, presentUserId, null);
$(\'#incomingCallModel\').modal(\'toggle\');
createOffer(sendTo, presentUserId);
});
}
break;
case \'client-answer\':
if(pc.localDescription){
await pc.setRemoteDescription(data);
}
break;
case \'client-offer\':
createAnswer(sendTo, presentUserId, data);
break;
case \'client-is-ready\':
createOffer(sendTo, presentUserId);
break;
case \'client-rejected\':
alert(\'Client has rejected your call\');
break;
case \'clent-already-oncall\':
setTimeout(() => {
alert(\'client is on other call\');
window.location.reload(true);
}, 2000);
break;
}
让电脑;
function getConn(){
if(!pc){
pc = new RTCPeerConnection();
}
}
async function getCam(){
try{
if(!pc){
await getConn();
}
mediaStream = await window.navigator.mediaDevices.getUserMedia(mediaConst);
localVideo.srcObject = mediaStream;
localStream = mediaStream;
localStream.getTracks().forEach(track => pc.addTrack(track, localStream));
}catch(error){
// alert(\'Camera not found\')
console.log(error);
}
}
function displayCall(incomingData){
$(\'.incoming-call-user-name\').text(incomingData.callingUserName);
let calligUserProfile = $(\"#allUserList\").find(\"[data-id=\'\" + incomingData.callingUserId + \"\']\").data(\"profile\");
$(\'.calling-user-pic\').attr(\'src\', calligUserProfile);
$(\'#incomingCallModel\').modal(\'show\');
}
function send(type, sendTo, callingUserName, callingUserId, data){
socket.emit(\'sendChatRequest\', JSON.stringify({
type: type,
receiverId: sendTo,
callingUserName: callingUserName,
callingUserId: callingUserId,
data: data,
}));
}
async function createOffer(sendTo, calledById){
if(!pc){
await getConn();
}
if(!localStream){
await getCam();
}
await pc.createOffer(options);
await sendIceCandidate(sendTo,calledById);
await pc.setLocalDescription(pc.localDescription);
send(\'client-offer\', sendTo, currentUserName, calledById, pc.localDescription);
}
async function sendIceCandidate(sendTo, calledById){
pc.onicecandidate = event => {
if(event.candidate !== null){
send(\'client-candidate\', sendTo, currentUserName, calledById, event.candidate);
}
}
pc.ontrack = event => {
console.log(event)
$(\'#video\').removeClass(\'hide\');
$(\'#video\').addClass(\'show\');
$(\'#videoCall\').addClass(\'hide\');
if (remoteVideo.srcObject) return;
remoteVideo.srcObject = event.streams[0];
}
}
async function createAnswer(sendTo, presentUserId, data){
if(!pc){
await getConn();
}
if(!localStream){
await getCam();
}
await sendIceCandidate(sendTo, presentUserId);
await pc.setRemoteDescription(data);
await pc.createAnswer();
await pc.setLocalDescription(pc.localDescription);
send(\'client-answer\', sendTo, currentUserName, presentUserId, pc.localDescription);
}
标签: socket.io webrtc videocall