WebRTC调用者无法接收被调用者的远程流

时间:2016-06-30 18:45:11

标签: javascript extjs webrtc

我尝试一对一地创建PeerConnection,但我无法接收被叫方的流。

我使用库ExtJS和adapter.js。这是我的WebRTC对象。

Ext.define('MyProject.WebRTC', {
  mixins: ['Ext.mixin.Observable'],

  config: {
      audio: true,
      video: true,
      offerToReceiveAudio: true,
      offerToReceiveVideo: true
  },

  isSupport: false,
  isBusy: false,
  contacts: [],
  stream: null,
  pc: null,

  constructor: function (config) {
      this.mixins.observable.constructor.call(this, config);

      if (navigator.getUserMedia || 
        navigator.mozGetUserMedia || 
        navigator.webkitGetUserMedia) {
        Ext.Loader.loadScript({
            url: Ext.getResourcePath('vendors/adapter.js'),
            onLoad: this.onLoadAdapter,
            scope: this
        });
        app.sync.on('rtc-offer', this.onOffer, this);
        app.sync.on('rtc-answer', this.onAnswer, this);
        app.sync.on('rtc-candidate', this.onReceiveCandidate, this);
        app.sync.on('rtc-close', this.onCloseConnection, this);
      }
  },

  onLoadAdapter: function () {
    var me = this;
    navigator.mediaDevices.getUserMedia({
        video: this.getVideo(),
        audio: this.getAudio()
    }).then(function(stream) {
        me.stream = stream;
        me.isSupport = true;
    })['catch'](this.onError);
  },

  createLocalPeerConnection: function () {
    if (this.pc) return true;

    var me = this;
    this.pc = new RTCPeerConnection(null);
    this.pc.addStream(this.stream);
    this.pc.onicecandidate = function(e) {
        if (e.candidate) {
            app.sync.sendWebRTCMessage(me.contacts, e.candidate);
        }
    };
    this.pc.onaddstream = function (e) {
        me.onGotRemoteStream(e);
    };
  },

  onGotRemoteStream: function (e) {
    this.fireEvent('gotRemoteStream', e.stream);
  },

  onOffer: function (desc, e) {
    this.createLocalPeerConnection();

    var remoteDesc = new RTCSessionDescription(desc),
        me = this;

    this.fireEvent('callFrom', e.srcUser);

    this.pc.setRemoteDescription(remoteDesc);
    this.contacts = [e.srcUser];

    this.pc.createAnswer().then(function (desc) {
        me.pc.setLocalDescription(desc);
        app.sync.sendWebRTCMessage(me.contacts, desc);
    }, this.onError);
  },

  onAnswer: function (desc) {
    desc = new RTCSessionDescription(desc);
    this.pc.setRemoteDescription(desc);
  },

  onReceiveCandidate: function (candidate) {
    candidate = new RTCIceCandidate(candidate);
    this.pc.addIceCandidate(candidate).then(Ext.emptyFn, this.onError);
  },

  close: function () {
    app.sync.sendWebRTCMessage(this.contacts, {type: 'close'});
  },

  onCloseConnection: function () {
    this.pc.close();
    this.pc = null;
    this.isBusy = false;
    this.fireEvent('closeConnection');
  },

  createCall: function (uid, callback, scope) {
    if (this.isBusy) return;

    this.isCalling = true;
    this.createLocalPeerConnection();
    this.contacts = [uid];

    var me = this;
    this.pc.createOffer({
        offerToReceiveAudio: this.getOfferToReceiveAudio,
        offerToReceiveVideo: this.getOfferToReceiveVideo
    }).then(function (desc) {
        me.pc.setLocalDescription(desc);
        app.sync.sendWebRTCMessage(me.contacts, desc);
        if (callback) callback.call(scope);
    }, this.onError);
  },

  onError: function (error) {
    console.error(error.toString());
  }
});

我无法理解,为什么被叫方接收到来电者的媒体流,但来电者无法接收被叫媒体流。我认为 onAnswer 方法中的代码 this.pc.setRemoteDescription(desc); 无法正常工作。

在来电者 this.pc.getRemoteStream()中等于[]。

PS: app.sync - 这是一个负责与信令服务器同步的对象。

1 个答案:

答案 0 :(得分:0)

我解决了这个问题。问题是我错误地向调用者创建了提议。

this.pc.createOffer({
    offerToReceiveAudio: this.getOfferToReceiveAudio,
    offerToReceiveVideo: this.getOfferToReceiveVideo
}).then(function (desc) {
    me.pc.setLocalDescription(desc);
    app.sync.sendWebRTCMessage(me.contacts, desc);
    if (callback) callback.call(scope);
}, this.onError);

应该是:

this.pc.createOffer({
    offerToReceiveAudio: this.getOfferToReceiveAudio(),
    offerToReceiveVideo: this.getOfferToReceiveVideo()
}).then(function (desc) {
    me.pc.setLocalDescription(desc);
    app.sync.sendWebRTCMessage(me.contacts, desc);
    if (callback) callback.call(scope);
}, this.onError);