From 24eb2bc3de3dedf9c6ce07ee02b37fa6a551f366 Mon Sep 17 00:00:00 2001 From: Bel LaPointe Date: Wed, 13 May 2020 16:03:07 -0600 Subject: [PATCH] Brain dump and minor rename --- BRAINDUMP.md | 54 ++++++++++++++++++++++++++++++ public/webrtc.js | 86 +++++++++++++++++++++++------------------------- 2 files changed, 95 insertions(+), 45 deletions(-) create mode 100644 BRAINDUMP.md diff --git a/BRAINDUMP.md b/BRAINDUMP.md new file mode 100644 index 0000000..a655567 --- /dev/null +++ b/BRAINDUMP.md @@ -0,0 +1,54 @@ +# Classes + +## Config - helpers + +### Static + +* [gs]etCookie +* createUUID +* getUUID + +## Log - logger + +### Static + +* info +* warn +* error +* log + +## Preview - self-video feed element + +* element + +## WS - websocket wrapper + +### Static + +* ws + +## RTC - RTC connection to TURN + +* conn (rtc conection) +* streams {id: RTCMediaStream} + +## Remote - local display of stream + +* element + +## Local - local stream out + +### Text, Video, Audio + +* stream (MediaStream) + +## Entropy - monolith + +### Static + +* pageReady - init Local preview video +* start - new RTC conn, sends offer message +* gotMessageFromServer - listens for events from server, starts stream if called, responds to offer messages, responds to TURN connect +* createdDescription +* local - Local +* rtc - RTC diff --git a/public/webrtc.js b/public/webrtc.js index cce21fb..7dc467c 100755 --- a/public/webrtc.js +++ b/public/webrtc.js @@ -52,7 +52,7 @@ Config.iceConfig = { 'iceTransportPolicy': 'relay', }; -class View { +class Log { static write(foo, color) { var msg = [].slice.call(arguments[2]).join(" "); foo(msg); @@ -61,19 +61,19 @@ class View { } static info() { - View.write(rconsole.info, "gray", arguments); + Log.write(rconsole.info, "gray", arguments); } static warn() { - View.write(rconsole.warn, "orange", arguments); + Log.write(rconsole.warn, "orange", arguments); } static log() { - View.write(rconsole.log, "black", arguments); + Log.write(rconsole.log, "black", arguments); } static error() { - View.write(rconsole.error, "red", arguments); + Log.write(rconsole.error, "red", arguments); } } @@ -86,14 +86,13 @@ class Preview { } } -class Server { - server = null; - +class WS { constructor(address, cb) { - Server.server = new WebSocket(address); - Server.server.onmessage = cb; + WS.ws = new WebSocket(address); + WS.ws.onmessage = cb; } } +WS.ws = null; class Remote { element = null; @@ -107,28 +106,29 @@ class Remote { } destructor() { - View.log("destructor called"); + Log.log("destructor called"); this.element.remove(); } } -class Peer { - peer = null; +class RTC { + conn = null; streams = {}; + // TODO issue: singleton for all feeds ever;; 1 peer = 1 server conn constructor(stream) { - this.peer = new RTCPeerConnection(Config.iceConfig); - this.peer.onicecandidate = (event) => { + this.conn = new RTCPeerConnection(Config.iceConfig); + this.conn.onicecandidate = (event) => { if(event.candidate != null) { - Server.server.send(JSON.stringify({'ice': event.candidate, 'uuid': Config.getUUID()})); + WS.ws.send(JSON.stringify({'ice': event.candidate, 'uuid': Config.getUUID()})); } }; - this.peer.ontrack = (event) => { + this.conn.ontrack = (event) => { event.streams.forEach((stream) => { this.streams[stream.id] = new Remote(stream.id, stream); }); }; - this.peer.oniceconnectionstatechange = (event) => { - switch (this.peer.iceConnectionState) { + this.conn.oniceconnectionstatechange = (event) => { + switch (this.conn.iceConnectionState) { case "disconnected": case "failed": case "closed": @@ -138,21 +138,17 @@ class Peer { } } }; - this.peer.addStream(stream); + this.conn.addStream(stream); } offer(cb) { - this.peer + this.conn .createOffer({ 'iceRestart': true, 'voiceActivityDetection': true, }) .then(cb) - .catch(View.error); - } - - destructor() { - this.peer.close(); + .catch(Log.error); } } @@ -162,14 +158,14 @@ class Entropy { } static start(isCaller) { - Entropy.peer = new Peer(Entropy.local.stream); + Entropy.rtc = new RTC(Entropy.local.stream); if(isCaller) { - Entropy.peer.offer(Entropy.createdDescription); + Entropy.rtc.offer(Entropy.createdDescription); } } static gotMessageFromServer(message) { - if(!Entropy.peer || !Entropy.peer.peer) Entropy.start(false); + if(!Entropy.rtc || !Entropy.rtc.conn) Entropy.start(false); var signal = JSON.parse(message.data); @@ -177,33 +173,33 @@ class Entropy { if(signal.uuid == Config.getUUID()) return; if(signal.sdp) { - Entropy.peer.peer.setRemoteDescription(new RTCSessionDescription(signal.sdp)).then(function() { + Entropy.rtc.conn.setRemoteDescription(new RTCSessionDescription(signal.sdp)).then(function() { // Only create answers in response to offers if(signal.sdp.type == 'offer') { - Entropy.peer.peer.createAnswer().then(Entropy.createdDescription).catch(View.error); + Entropy.rtc.conn.createAnswer().then(Entropy.createdDescription).catch(Log.error); } - }).catch(View.error); + }).catch(Log.error); } else if(signal.ice) { - Entropy.peer.peer.addIceCandidate(new RTCIceCandidate(signal.ice)).catch(View.error); + Entropy.rtc.conn.addIceCandidate(new RTCIceCandidate(signal.ice)).catch(Log.error); } } static createdDescription(description) { - View.log('got description'); + Log.log('got description'); - Entropy.peer.peer + Entropy.rtc.conn .setLocalDescription(description) .then(function() { - Server.server.send(JSON.stringify({ - 'sdp': Entropy.peer.peer.localDescription, + WS.ws.send(JSON.stringify({ + 'sdp': Entropy.rtc.conn.localDescription, 'uuid': Config.getUUID(), })); }) - .catch(View.error); + .catch(Log.error); } } Entropy.local = null; -Entropy.peer = null; +Entropy.rtc = null; class Local { stream = null; @@ -213,7 +209,7 @@ class Local { case "audio": return new Audio(); case "video": return new Video(); case "text": return new Text(); - default: View.error("unknown build", type); + default: Log.error("unknown build", type); } } @@ -223,7 +219,7 @@ class Local { audio: this.gettype() == "audio", }; if(!navigator.mediaDevices.getUserMedia) { - View.error('Your browser does not support getUserMedia API'); + Log.error('Your browser does not support getUserMedia API'); } navigator.mediaDevices.getUserMedia(constraints) .then((stream) => { @@ -232,11 +228,11 @@ class Local { } this.stream = stream; }) - .catch(View.error); + .catch(Log.error); } gettype() { - View.error("not impl"); + Log.error("not impl"); } start() {} @@ -263,7 +259,7 @@ var streams = {} var rconsole = console; var console = {} for (var i in ["log", "info", "warn", "error"]) { - console[i] = View[i]; + console[i] = Log[i]; } window.console = console; @@ -273,7 +269,7 @@ function pageReady() { if (window.location.port) { host += ":" + window.location.port; } - new Server('wss://' + host + '/abc', Entropy.gotMessageFromServer); + new WS('wss://' + host + '/abc', Entropy.gotMessageFromServer); Entropy.pageReady(); }