RTCPeerConnection事件不会触发

时间:2017-02-08 09:24:04

标签: javascript angular webrtc

我无法使RTCPeerConnection正常工作。在我创建RTCPeerConnection对象后,没有任何反应。事件不会被解雇。这是创建连接的方法:

createPeerConnection() {
    console.log('new RtCPeerConnection with stun server.');
    this.myPeerConnection = new RTCPeerConnection({stunServer}]
    });

    console.log('PeerConnection is ', this.myPeerConnection);

    this.myPeerConnection.onicecandidate = this.handleICECandidateEvent;
    this.myPeerConnection.ontrack = this.handleAddTrackEvent;
    this.myPeerConnection.removeTrack = this.handleRemoveStreamEvent;
    this.myPeerConnection.oniceconnectionstatechange = this.handleICEConnectionStateChangeEvent;
    // this.myPeerConnection.onicegatheringstatechange = this.handleICEGatheringStateChangeEvent;
    // this.myPeerConnection.onsignalingstatechange = this.handleSignalingStateChangeEvent;
    this.myPeerConnection.onnegotiationneeded = this.handleNegotiationNeededEvent;
  }

我在控制台中没有错误。什么也没发生。 当用户点击另一个用户进行连接时,会调用此方法:

connect() {
    console.log('Creating RTCPeerConnetion...');
    this.createPeerConnection();
    console.log('RTCPeerConnection created');
    console.log('Creating new local stream ...');
    navigator.mediaDevices.getUserMedia({ video: true, audio: true }).then((localStream) => {
      this.localVideo.nativeElement.srcObject = localStream;
      console.log('Local stream created', localStream);
    }).catch(this.handleGetUserMediaError);
  }

我使用Firefox和Angular2,我在localhost上测试它。不知道这是否可能是原因。任何想法?

1 个答案:

答案 0 :(得分:0)

我忘记将steam添加到peerConnection,如下所示:

navigator.mediaDevices.getUserMedia({ video: true, audio: true }).then((localStream) => {
      this.localVideo.nativeElement.srcObject = localStream;
      console.log('Local stream created', localStream);
      localStream.getTracks().forEach(track =>
        this.myPeerConnection.addTrack(track, localStream)
      );