I am working on WebRTC and connecting two-peers on different browser pages, one peer which is initiator comes with this url http://localhost:4200/#init
and other which is video receiver comes with this url http://localhost:4200/
.
After building successful connection 1st peer share its video with the other but problem I am facing is other peer is not playing or receiving the video.
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>
What can be the problem? Please guide!!!