webrtc:createAnswer适用于chrome但使用firefox触发错误

时间:2014-04-15 17:28:59

标签: webrtc

function createPeerConnection() {
  try {
    pc = new RTCPeerConnection(null, pc_constraints);
    pc.onicecandidate = handleIceCandidate;
    pc.onaddstream = handleRemoteStreamAdded;
    pc.onremovestream = handleRemoteStreamRemoved;
    console.log('Created RTCPeerConnnection');
  } catch (e) {
    console.log('Failed to create PeerConnection, exception: ' + e.message);
    alert('Cannot create RTCPeerConnection object.');
      return;
  }

  try {
      // Reliable Data Channels not yet supported in Chrome
      sendChannel = pc.createDataChannel("sendDataChannel",
        {reliable: false});
      sendChannel.onmessage = handleMessage;
      trace('Created send data channel');
    } catch (e) {
      alert('Failed to create data channel. ' +
            'You need Chrome M25 or later with RtpDataChannel enabled');
      trace('createDataChannel() failed with exception: ' + e.message);
    }
    sendChannel.onopen = handleSendChannelStateChange;
    sendChannel.onclose = handleSendChannelStateChange;
    pc.ondatachannel = gotReceiveChannel;
}

    function doAnswer() {
      console.log('Sending answer to peer.');
      pc.createAnswer(setLocalAndSendMessage, null, sdpConstraints);
    }

我收到了错误:

TypeError: Argument 2 of mozRTCPeerConnection.createAnswer is not an object.

2 个答案:

答案 0 :(得分:1)

以下代码应该适用于Firefox:

function doAnswer() {
  console.log('Sending answer to peer.');

  pc.createAnswer(setLocalAndSendMessage, handleCreateAnswerError, sdpConstraints);
}

function setLocalAndSendMessage(sessionDescription) {
  sessionDescription.sdp = preferOpus(sessionDescription.sdp);
  pc.setLocalDescription(sessionDescription);

  console.log('setLocalAndSendMessage sending message' , sessionDescription);

  sendMessage(sessionDescription);
}

function handleCreateAnswerError(error) {
  console.log('createAnswer() error: ', e);
}

可以在documentation for createAnswer中找到Firefox中失败的原因。问题是Firefox不会让你为错误处理程序传递null。所有这些要求是你自己编写,然后将其传递给createAnswer。不传递null,你实际上应该传递一个函数(对象)来处理错误。

对于迟到的回复感到抱歉,迟到总比没有好!

答案 1 :(得分:0)

这是怎么回事?它是那些@ xsockets使用的......

pc.createAnswer(setLocalAndSendMessage,function (ex) { self.onerror(ex); }, sdpConstraints);