function start(id, init) {
pc = new RTCPeerConnection(configuration);
// send any ice candidates to the other peer
pc.onicecandidate = function (evt) {
chat.server.sendIceCandidate(id, JSON.stringify({ "candidate": evt.candidate }));
};
// let the "negotiationneeded" event trigger offer generation
pc.onnegotiationneeded = function () {
pc.createOffer().then(function (offer) {
return pc.setLocalDescription(offer);
})
.then(function () {
// send the offer to the other peer
chat.server.sendOffer(id, JSON.stringify({ "desc": pc.localDescription }));
})
.catch(logError);
};
// once remote video track arrives, show it in the remote video element
pc.ontrack = function (evt) {
if (evt.track.kind === "video") {
remoteView.srcObject = evt.streams[0];
}
};
var constraints = { audio: true, video: { width: 640, height: 480, facingMode: "user" } };
// get a local stream, show it in a self-view and add it to be sent
navigator.mediaDevices.getUserMedia(constraints)
.then(function (stream) {
selfView.srcObject = stream;
pc.addTrack(stream.getAudioTracks()[0], stream);
pc.addTrack(stream.getVideoTracks()[0], stream);
//stream.getTracks().forEach(track => pc.addTrack(track, stream));
})
.catch(logError);
}