From 92e94e40fe53d3eb35ce8137d222fc6cf2c227f8 Mon Sep 17 00:00:00 2001 From: bel Date: Sun, 10 May 2020 12:45:50 -0600 Subject: [PATCH] making peer --- public/webrtc.js | 82 +++++++++++++++++++++++++++--------------------- 1 file changed, 47 insertions(+), 35 deletions(-) diff --git a/public/webrtc.js b/public/webrtc.js index 968f2f4..0845831 100755 --- a/public/webrtc.js +++ b/public/webrtc.js @@ -1,5 +1,5 @@ class Config { - static peerConnectionConfig = { + static iceConfig = { 'iceServers': [ {'urls': 'stun:stun.stunprotocol.org:3478'}, {'urls': 'stun:stun.l.google.com:19302'}, @@ -90,42 +90,59 @@ class Preview { } } -class Controller { - static serverConnection; +class Server { + static server; constructor(address, cb) { - Controller.serverConnection = new WebSocket(address); - Controller.serverConnection.onmessage = cb; + Server.server = new WebSocket(address); + 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 { - static peerConnection = null; static local = null; + static peer = null; static pageReady() { Entropy.local = Local.build("video"); } static start(isCaller) { - Entropy.peerConnection = new RTCPeerConnection(Config.peerConnectionConfig); - Entropy.peerConnection.onicecandidate = Entropy.gotIceCandidate; - Entropy.peerConnection.ontrack = (event) => new Remote('remoteVideo', event.streams[0]); - Entropy.peerConnection.addStream(Entropy.local.stream); - - if(isCaller) { - Entropy.peerConnection - .createOffer({ - 'iceRestart': true, - 'voiceActivityDetection': true, - }) - .then(Entropy.createdDescription) - .catch(View.error); - } + Entropy.peer = new Peer(Entropy.local.stream); + if(isCaller) { + Entropy.peer.offer(Entropy.createdDescription); + } } static gotMessageFromServer(message) { - if(!Entropy.peerConnection) Entropy.start(false); + if(!Entropy.peer.peer) Entropy.start(false); var signal = JSON.parse(message.data); @@ -133,31 +150,31 @@ class Entropy { if(signal.uuid == Config.getUUID()) return; 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 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); } 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) { 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) { View.log('got description'); - Entropy.peerConnection + Entropy.peer.peer .setLocalDescription(description) .then(function() { - Controller.serverConnection.send(JSON.stringify({ - 'sdp': Entropy.peerConnection.localDescription, + Server.server.send(JSON.stringify({ + 'sdp': Entropy.peer.peer.localDescription, 'uuid': Config.getUUID(), })); }) @@ -229,7 +246,7 @@ window.console = console; function pageReady() { Config.getUUID(); - new Controller('wss://' + window.location.hostname + '/abc', Entropy.gotMessageFromServer); + new Server('wss://' + window.location.hostname + '/abc', Entropy.gotMessageFromServer); Entropy.pageReady(); } @@ -241,12 +258,7 @@ function toggle(type, caller) { //start(true, type); var exists = Object.keys(streams); if (caller.checked && ! (type in exists)) { - switch(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] = Local.build(type); streams[type].start(); } else if (type in exists) { streams[type].stop();