making peer
parent
71053a1851
commit
92e94e40fe
|
|
@ -1,5 +1,5 @@
|
||||||
class Config {
|
class Config {
|
||||||
static peerConnectionConfig = {
|
static iceConfig = {
|
||||||
'iceServers': [
|
'iceServers': [
|
||||||
{'urls': 'stun:stun.stunprotocol.org:3478'},
|
{'urls': 'stun:stun.stunprotocol.org:3478'},
|
||||||
{'urls': 'stun:stun.l.google.com:19302'},
|
{'urls': 'stun:stun.l.google.com:19302'},
|
||||||
|
|
@ -90,42 +90,59 @@ class Preview {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class Controller {
|
class Server {
|
||||||
static serverConnection;
|
static server;
|
||||||
|
|
||||||
constructor(address, cb) {
|
constructor(address, cb) {
|
||||||
Controller.serverConnection = new WebSocket(address);
|
Server.server = new WebSocket(address);
|
||||||
Controller.serverConnection.onmessage = cb;
|
Server.server.onmessage = cb;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class Peer {
|
||||||
|
peer;
|
||||||
|
|
||||||
|
constructor(stream) {
|
||||||
|
this.peer = new RTCPeerConnection(Config.iceConfig);
|
||||||
|
this.peer.onicecandidate = this.gotIceCandidate;
|
||||||
|
this.peer.ontrack = (event) => new Remote('remoteVideo', event.streams[0]);
|
||||||
|
this.peer.addStream(stream);
|
||||||
|
}
|
||||||
|
|
||||||
|
gotIceCandidate(event) {
|
||||||
|
if(event.candidate != null) {
|
||||||
|
Server.server.send(JSON.stringify({'ice': event.candidate, 'uuid': Config.getUUID()}));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
offer(cb) {
|
||||||
|
this.peer
|
||||||
|
.createOffer({
|
||||||
|
'iceRestart': true,
|
||||||
|
'voiceActivityDetection': true,
|
||||||
|
})
|
||||||
|
.then(cb)
|
||||||
|
.catch(View.error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class Entropy {
|
class Entropy {
|
||||||
static peerConnection = null;
|
|
||||||
static local = null;
|
static local = null;
|
||||||
|
static peer = null;
|
||||||
|
|
||||||
static pageReady() {
|
static pageReady() {
|
||||||
Entropy.local = Local.build("video");
|
Entropy.local = Local.build("video");
|
||||||
}
|
}
|
||||||
|
|
||||||
static start(isCaller) {
|
static start(isCaller) {
|
||||||
Entropy.peerConnection = new RTCPeerConnection(Config.peerConnectionConfig);
|
Entropy.peer = new Peer(Entropy.local.stream);
|
||||||
Entropy.peerConnection.onicecandidate = Entropy.gotIceCandidate;
|
if(isCaller) {
|
||||||
Entropy.peerConnection.ontrack = (event) => new Remote('remoteVideo', event.streams[0]);
|
Entropy.peer.offer(Entropy.createdDescription);
|
||||||
Entropy.peerConnection.addStream(Entropy.local.stream);
|
}
|
||||||
|
|
||||||
if(isCaller) {
|
|
||||||
Entropy.peerConnection
|
|
||||||
.createOffer({
|
|
||||||
'iceRestart': true,
|
|
||||||
'voiceActivityDetection': true,
|
|
||||||
})
|
|
||||||
.then(Entropy.createdDescription)
|
|
||||||
.catch(View.error);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static gotMessageFromServer(message) {
|
static gotMessageFromServer(message) {
|
||||||
if(!Entropy.peerConnection) Entropy.start(false);
|
if(!Entropy.peer.peer) Entropy.start(false);
|
||||||
|
|
||||||
var signal = JSON.parse(message.data);
|
var signal = JSON.parse(message.data);
|
||||||
|
|
||||||
|
|
@ -133,31 +150,31 @@ class Entropy {
|
||||||
if(signal.uuid == Config.getUUID()) return;
|
if(signal.uuid == Config.getUUID()) return;
|
||||||
|
|
||||||
if(signal.sdp) {
|
if(signal.sdp) {
|
||||||
Entropy.peerConnection.setRemoteDescription(new RTCSessionDescription(signal.sdp)).then(function() {
|
Entropy.peer.peer.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') {
|
||||||
Entropy.peerConnection.createAnswer().then(Entropy.createdDescription).catch(View.error);
|
Entropy.peer.peer.createAnswer().then(Entropy.createdDescription).catch(View.error);
|
||||||
}
|
}
|
||||||
}).catch(View.error);
|
}).catch(View.error);
|
||||||
} else if(signal.ice) {
|
} else if(signal.ice) {
|
||||||
Entropy.peerConnection.addIceCandidate(new RTCIceCandidate(signal.ice)).catch(View.error);
|
Entropy.peer.peer.addIceCandidate(new RTCIceCandidate(signal.ice)).catch(View.error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static gotIceCandidate(event) {
|
static gotIceCandidate(event) {
|
||||||
if(event.candidate != null) {
|
if(event.candidate != null) {
|
||||||
Controller.serverConnection.send(JSON.stringify({'ice': event.candidate, 'uuid': Config.getUUID()}));
|
Server.server.send(JSON.stringify({'ice': event.candidate, 'uuid': Config.getUUID()}));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static createdDescription(description) {
|
static createdDescription(description) {
|
||||||
View.log('got description');
|
View.log('got description');
|
||||||
|
|
||||||
Entropy.peerConnection
|
Entropy.peer.peer
|
||||||
.setLocalDescription(description)
|
.setLocalDescription(description)
|
||||||
.then(function() {
|
.then(function() {
|
||||||
Controller.serverConnection.send(JSON.stringify({
|
Server.server.send(JSON.stringify({
|
||||||
'sdp': Entropy.peerConnection.localDescription,
|
'sdp': Entropy.peer.peer.localDescription,
|
||||||
'uuid': Config.getUUID(),
|
'uuid': Config.getUUID(),
|
||||||
}));
|
}));
|
||||||
})
|
})
|
||||||
|
|
@ -229,7 +246,7 @@ window.console = console;
|
||||||
|
|
||||||
function pageReady() {
|
function pageReady() {
|
||||||
Config.getUUID();
|
Config.getUUID();
|
||||||
new Controller('wss://' + window.location.hostname + '/abc', Entropy.gotMessageFromServer);
|
new Server('wss://' + window.location.hostname + '/abc', Entropy.gotMessageFromServer);
|
||||||
Entropy.pageReady();
|
Entropy.pageReady();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -241,12 +258,7 @@ function toggle(type, caller) {
|
||||||
//start(true, type);
|
//start(true, type);
|
||||||
var exists = Object.keys(streams);
|
var exists = Object.keys(streams);
|
||||||
if (caller.checked && ! (type in exists)) {
|
if (caller.checked && ! (type in exists)) {
|
||||||
switch(type) {
|
streams[type] = Local.build(type);
|
||||||
case "audio": streams[type] = new Audio(); break;
|
|
||||||
case "video": streams[type] = new Video(); break;
|
|
||||||
case "text": streams[type] = new Text(); break;
|
|
||||||
default: View.error("unknown type", type); return;
|
|
||||||
}
|
|
||||||
streams[type].start();
|
streams[type].start();
|
||||||
} else if (type in exists) {
|
} else if (type in exists) {
|
||||||
streams[type].stop();
|
streams[type].stop();
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue