-
Notifications
You must be signed in to change notification settings - Fork 165
/
Copy pathclient.js
50 lines (37 loc) · 1.59 KB
/
client.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
'use strict';
const createExample = require('../../lib/browser/example');
const description = 'This example uses node-webrtc’s RTCVideoSource and \
RTCVideoSink along with <a href="https://github.com/Automattic/node-canvas">\
node-canvas</a> to superimpose a spinning, colorful animation on top of the \
incoming video.';
const localVideo = document.createElement('video');
localVideo.autoplay = true;
localVideo.muted = true;
const remoteVideo = document.createElement('video');
remoteVideo.autoplay = true;
async function beforeAnswer(peerConnection) {
const localStream = await window.navigator.mediaDevices.getUserMedia({
audio: true,
video: true
});
localVideo.srcObject = localStream;
localStream.getTracks().forEach(track => peerConnection.addTrack(track, localStream));
const remoteStream = new MediaStream(peerConnection.getReceivers().map(receiver => receiver.track));
remoteVideo.srcObject = remoteStream;
// NOTE(mroberts): This is a hack so that we can get a callback when the
// RTCPeerConnection is closed. In the future, we can subscribe to
// "connectionstatechange" events.
const { close } = peerConnection;
peerConnection.close = function() {
remoteVideo.srcObject = null;
localVideo.srcObject = null;
localStream.getTracks().forEach(track => track.stop());
return close.apply(this, arguments);
};
}
createExample('video-compositing', description, { beforeAnswer });
const videos = document.createElement('div');
videos.className = 'grid';
videos.appendChild(localVideo);
videos.appendChild(remoteVideo);
document.body.appendChild(videos);