I am new to WebRTC and currently trying to implement a Peer to Peer Video chat in my Angular2 project. I am using webrtc-adapter.js npm for setting up the connection. The remote video remains blank, and only the local webcam video is working.
I have referred the link":
Remote VideoStream not working with WebRTC
And I have checked in chrome://webrtc-internals/ to trace the connectivity, but not been able to find the exact cause.
Below is my code:
setupRTC() {
var log = msg => "<p>" + msg + "</p>";
var failed = e => log(e + ", line " + e.lineNumber);
this.pc = new RTCPeerConnection(SERVERS, DEFAULT_CONSTRAINTS);
this.remotepc = new RTCPeerConnection(SERVERS, DEFAULT_CONSTRAINTS);
var add = (pc, ca) => ca && pc.addIceCandidate(ca).catch(failed);
this.pc.onicecandidate = e => add(this.remotepc, e.candidate);
this.remotepc.onicecandidate = e => add(this.pc, e.candidate);
this.pc.ontrack = e => (this.remoteVideo.nativeElement.srcObject = e.streams[0]);
this.pc.oniceconnectionstatechange = e => log(this.pc.iceConnectionState);
}
The Video chat is initiated on click of a button event as below:
startVideo() {
debugger;
let nav = <any>navigator;
var log = msg => "<p>" + msg + "</p>";
var failed = e => log(e + ", line " + e.lineNumber);
nav.mediaDevices.getUserMedia({ video: true, audio: true })
.then(stream => this.pc.addStream(this.localVideo.nativeElement.srcObject = stream))
.then(() => this.pc.createOffer())
.then(offer => this.pc.setLocalDescription(offer))
.then(() => this.remotepc.setRemoteDescription(this.pc.localDescription))
.then(() => this.remotepc.createAnswer())
.then(answer => this.remotepc.setLocalDescription(answer))
.then(() => this.pc.setRemoteDescription(this.remotepc.localDescription))
.catch(failed);
}
In the HTML, I have a popup :
<modal #myModal3 cancelButtonLabel="Close" (onClose)="showCam()">
<modal-header>
<h4>Video Chat</h4>
</modal-header>
<modal-content>
<div id="callPage" class="call-page">
<video id = "localVideo" [src]="localVideo" autoplay></video>
<video id = "remoteVideo" [src]="remoteVideo" autoplay></video>
<div class="row text-center">
<div class="col-md-12">
<button id="callBtn" (click)="startVideo()">Call</button>
<button id="hangUpBtn">Hang Up</button><div id="div"></div>
</div>
</div>
</div>
</modal-content>
</modal>
Any Suggestions?