在WebRTC Simple-Peer Angular 7中,视频不在其他对等端播放

时间:2018-12-16 13:34:09

标签: angular webrtc angular7 simple-peer

我正在WebRTC上工作,并在不同的浏览器页面上连接两个对等方,一个对等方是此URL http://localhost:4200/#init的发起者,另一个是视频接收方此URL http://localhost:4200/的URL。 建立成功的连接后,第一个对等方与他人共享其视频,但是我面临的问题是其他对等方未播放或接收视频。

export class SimpPeerComponent implements OnInit {

  targetpeer: any;
  peer: any;
  stream: MediaStream

  async ngOnInit() {
    try {
      // This peer is the initiator and transfering the streaming to the other connected peer 
      if (location.hash === '#init') {
        let stream = await navigator.mediaDevices.getUserMedia({ video: true })
        this.peer = new SimplePeer({
          initiator: location.hash === '#init',
          stream: stream
        })
      }
      else {
        this.peer = new SimplePeer()
      }

      // triggers when signal is sent from remote
      this.peer.on('signal', function (data) {
        console.log(JSON.stringify(data));
      })

      this.peer.on('data', (data) => {
        console.log('Received Data: ' + data)
      })

      this.peer.on('stream', (stream) => {
        // got remote video stream, now let's show it in a video tag
        this.videoElement.srcObject = stream
      })
    } catch (error) {
      console.log(error)
    }
  }

  connect() {
    this.peer.signal(this.targetpeer);
  }

  message() {
    this.peer.send('Hello world');
  }

  @ViewChild('myvideo') videoElementRef: ElementRef;
  get videoElement(): HTMLVideoElement {
    return this.videoElementRef.nativeElement
  }
}

<div class="row">
    <div class="col d-flex justify-content-center">
        <video #myvideo autoplay controls class="video mb-2"></video>
    </div>
</div>

<div class="row">
    <div class="col d-flex justify-content-center">
        <input type="text" class="form-control-sm mr-1" [(ngModel)]="targetpeer">
        <button class="btn btn-success" (click)="connect()">Connect</button>
        <button class="btn btn-primary" (click)="message()">Send</button>
    </div>
</div>

可能是什么问题?请指导!!!

1 个答案:

答案 0 :(得分:0)

我现在所做的一个简单愚蠢的错误是,现在只有chrome浏览器才支持此错误,但是我一直在Opera中运行此错误,并花了一整天的时间来解决此错误:D