RTCPeerConnection.setRemoteDescription参数不足

时间:2019-01-08 18:02:52

标签: javascript socket.io webrtc

当前在Firefox 64中出现关注错误,该错误给出了此错误,我一直在网上搜索有效的修复程序,但是找不到。

TypeError: "Not enough arguments to RTCPeerConnection.setRemoteDescription."

曾经使用一些链接进行修复但无济于事,而且后者已过时。

https://developer.mozilla.org/en-US/docs/Web/API/RTCSessionDescription

WebRTC Firefox: Not enough arguments to RTCPeerConnection.setLocalDescription

Chrome浏览器会触发另一个错误main.js:58 Uncaught TypeError: Cannot read property 'type' of undefined,这是在存在一个if语句来检查remotedescription.type是否与offer匹配的情况下

如果有人知道修复方法,将不胜感激。

 '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', async message => {
  if (message.sdp) {
    await pc
      .setRemoteDescription(new RTCSessionDescription(message.sdp), () => {
        if (pc.remotedescription.type === 'offer') {
          pc.setLocalDescription(pc.createAnswer())
            .then(function() {
              sendMessage({ sdp: pc.localDescription });
            })
            .catch(function(err) {
              console.log(err.name + ': ' + err.message);
            });
        } else {
          pc.addIceCandidate(new RTCIceCandidate(message.candidate)).catch(
            error => console.error(error)
          );
        }
      })
      .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(function() {
      sendMessage(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');
}

1 个答案:

答案 0 :(得分:2)

您正在呼叫pc.setRemoteDescription(desc, callback),但没有提供错误回调,而是使用.catch。 Firefox不喜欢在不提供错误回调的情况下使用回调,这会导致“参数不足”错误。

不要将已弃用的回调与Promise混合使用,而应使用pc.setRemoteDescription(desc).then(() => ...).catch(...)