Add central state
parent
e4d498a501
commit
9cb092d74e
|
|
@ -1,9 +1,12 @@
|
||||||
let localVideo;
|
|
||||||
let localStream;
|
let _s = {
|
||||||
let remoteVideo;
|
localVideo: undefined,
|
||||||
let peerConnection;
|
localStream: undefined,
|
||||||
let uuid;
|
remoteVideo: undefined,
|
||||||
let serverConnection;
|
peerConnection: undefined,
|
||||||
|
uuid: undefined,
|
||||||
|
serverConnection: undefined
|
||||||
|
};
|
||||||
|
|
||||||
const peerConnectionConfig = {
|
const peerConnectionConfig = {
|
||||||
'iceServers': [
|
'iceServers': [
|
||||||
|
|
@ -13,13 +16,13 @@ const peerConnectionConfig = {
|
||||||
};
|
};
|
||||||
|
|
||||||
function pageReady() {
|
function pageReady() {
|
||||||
uuid = createUUID();
|
_s.uuid = createUUID();
|
||||||
|
|
||||||
localVideo = document.getElementById('localVideo');
|
_s.localVideo = document.getElementById('localVideo');
|
||||||
remoteVideo = document.getElementById('remoteVideo');
|
_s.remoteVideo = document.getElementById('remoteVideo');
|
||||||
|
|
||||||
serverConnection = new WebSocket('wss://' + window.location.hostname + ':8443');
|
_s.serverConnection = new WebSocket('wss://' + window.location.hostname + ':8443');
|
||||||
serverConnection.onmessage = gotMessageFromServer;
|
_s.serverConnection.onmessage = gotMessageFromServer;
|
||||||
|
|
||||||
const constraints = {
|
const constraints = {
|
||||||
video: true,
|
video: true,
|
||||||
|
|
@ -34,57 +37,57 @@ function pageReady() {
|
||||||
}
|
}
|
||||||
|
|
||||||
function getUserMediaSuccess(stream) {
|
function getUserMediaSuccess(stream) {
|
||||||
localStream = stream;
|
_s.localStream = stream;
|
||||||
localVideo.srcObject = stream;
|
_s.localVideo.srcObject = stream;
|
||||||
}
|
}
|
||||||
|
|
||||||
function start(isCaller) {
|
function start(isCaller) {
|
||||||
peerConnection = new RTCPeerConnection(peerConnectionConfig);
|
_s.peerConnection = new RTCPeerConnection(peerConnectionConfig);
|
||||||
peerConnection.onicecandidate = gotIceCandidate;
|
_s.peerConnection.onicecandidate = gotIceCandidate;
|
||||||
peerConnection.ontrack = gotRemoteStream;
|
_s.peerConnection.ontrack = gotRemoteStream;
|
||||||
peerConnection.addStream(localStream);
|
_s.peerConnection.addStream(_s.localStream);
|
||||||
|
|
||||||
if(isCaller) {
|
if(isCaller) {
|
||||||
peerConnection.createOffer().then(createdDescription).catch(errorHandler);
|
_s.peerConnection.createOffer().then(createdDescription).catch(errorHandler);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function gotMessageFromServer(message) {
|
function gotMessageFromServer(message) {
|
||||||
if(!peerConnection) start(false);
|
if(!_s.peerConnection) start(false);
|
||||||
const signal = JSON.parse(message.data);
|
const signal = JSON.parse(message.data);
|
||||||
|
|
||||||
// Ignore messages from ourself
|
// Ignore messages from ourself
|
||||||
if(signal.uuid == uuid) return;
|
if(signal.uuid == _s.uuid) return;
|
||||||
|
|
||||||
if(signal.sdp) {
|
if(signal.sdp) {
|
||||||
peerConnection.setRemoteDescription(new RTCSessionDescription(signal.sdp)).then(function() {
|
_s.peerConnection.setRemoteDescription(new RTCSessionDescription(signal.sdp)).then(function() {
|
||||||
// Only create answers in response to offers
|
// Only create answers in response to offers
|
||||||
if(signal.sdp.type == 'offer') {
|
if(signal.sdp.type == 'offer') {
|
||||||
peerConnection.createAnswer().then(createdDescription).catch(errorHandler);
|
_s.peerConnection.createAnswer().then(createdDescription).catch(errorHandler);
|
||||||
}
|
}
|
||||||
}).catch(errorHandler);
|
}).catch(errorHandler);
|
||||||
} else if(signal.ice) {
|
} else if(signal.ice) {
|
||||||
peerConnection.addIceCandidate(new RTCIceCandidate(signal.ice)).catch(errorHandler);
|
_s.peerConnection.addIceCandidate(new RTCIceCandidate(signal.ice)).catch(errorHandler);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function gotIceCandidate(event) {
|
function gotIceCandidate(event) {
|
||||||
if(event.candidate != null) {
|
if(event.candidate != null) {
|
||||||
serverConnection.send(JSON.stringify({'ice': event.candidate, 'uuid': uuid}));
|
_s.serverConnection.send(JSON.stringify({'ice': event.candidate, 'uuid': _s.uuid}));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function createdDescription(description) {
|
function createdDescription(description) {
|
||||||
console.log('got description');
|
console.log('got description');
|
||||||
|
|
||||||
peerConnection.setLocalDescription(description).then(function() {
|
_s.peerConnection.setLocalDescription(description).then(function() {
|
||||||
serverConnection.send(JSON.stringify({'sdp': peerConnection.localDescription, 'uuid': uuid}));
|
_s.serverConnection.send(JSON.stringify({'sdp': _s.peerConnection.localDescription, 'uuid': _s.uuid}));
|
||||||
}).catch(errorHandler);
|
}).catch(errorHandler);
|
||||||
}
|
}
|
||||||
|
|
||||||
function gotRemoteStream(event) {
|
function gotRemoteStream(event) {
|
||||||
console.log('got remote stream');
|
console.log('got remote stream');
|
||||||
remoteVideo.srcObject = event.streams[0];
|
_s.remoteVideo.srcObject = event.streams[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
function errorHandler(error) {
|
function errorHandler(error) {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue