Brain dump and minor rename

master
Bel LaPointe 2020-05-13 16:03:07 -06:00
parent 5c924ad154
commit 24eb2bc3de
2 changed files with 95 additions and 45 deletions

54
BRAINDUMP.md Normal file
View File

@ -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

View File

@ -52,7 +52,7 @@ Config.iceConfig = {
'iceTransportPolicy': 'relay', 'iceTransportPolicy': 'relay',
}; };
class View { class Log {
static write(foo, color) { static write(foo, color) {
var msg = [].slice.call(arguments[2]).join(" "); var msg = [].slice.call(arguments[2]).join(" ");
foo(msg); foo(msg);
@ -61,19 +61,19 @@ class View {
} }
static info() { static info() {
View.write(rconsole.info, "gray", arguments); Log.write(rconsole.info, "gray", arguments);
} }
static warn() { static warn() {
View.write(rconsole.warn, "orange", arguments); Log.write(rconsole.warn, "orange", arguments);
} }
static log() { static log() {
View.write(rconsole.log, "black", arguments); Log.write(rconsole.log, "black", arguments);
} }
static error() { static error() {
View.write(rconsole.error, "red", arguments); Log.write(rconsole.error, "red", arguments);
} }
} }
@ -86,14 +86,13 @@ class Preview {
} }
} }
class Server { class WS {
server = null;
constructor(address, cb) { constructor(address, cb) {
Server.server = new WebSocket(address); WS.ws = new WebSocket(address);
Server.server.onmessage = cb; WS.ws.onmessage = cb;
} }
} }
WS.ws = null;
class Remote { class Remote {
element = null; element = null;
@ -107,28 +106,29 @@ class Remote {
} }
destructor() { destructor() {
View.log("destructor called"); Log.log("destructor called");
this.element.remove(); this.element.remove();
} }
} }
class Peer { class RTC {
peer = null; conn = null;
streams = {}; streams = {};
// TODO issue: singleton for all feeds ever;; 1 peer = 1 server conn
constructor(stream) { constructor(stream) {
this.peer = new RTCPeerConnection(Config.iceConfig); this.conn = new RTCPeerConnection(Config.iceConfig);
this.peer.onicecandidate = (event) => { this.conn.onicecandidate = (event) => {
if(event.candidate != null) { 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) => { event.streams.forEach((stream) => {
this.streams[stream.id] = new Remote(stream.id, stream); this.streams[stream.id] = new Remote(stream.id, stream);
}); });
}; };
this.peer.oniceconnectionstatechange = (event) => { this.conn.oniceconnectionstatechange = (event) => {
switch (this.peer.iceConnectionState) { switch (this.conn.iceConnectionState) {
case "disconnected": case "disconnected":
case "failed": case "failed":
case "closed": case "closed":
@ -138,21 +138,17 @@ class Peer {
} }
} }
}; };
this.peer.addStream(stream); this.conn.addStream(stream);
} }
offer(cb) { offer(cb) {
this.peer this.conn
.createOffer({ .createOffer({
'iceRestart': true, 'iceRestart': true,
'voiceActivityDetection': true, 'voiceActivityDetection': true,
}) })
.then(cb) .then(cb)
.catch(View.error); .catch(Log.error);
}
destructor() {
this.peer.close();
} }
} }
@ -162,14 +158,14 @@ class Entropy {
} }
static start(isCaller) { static start(isCaller) {
Entropy.peer = new Peer(Entropy.local.stream); Entropy.rtc = new RTC(Entropy.local.stream);
if(isCaller) { if(isCaller) {
Entropy.peer.offer(Entropy.createdDescription); Entropy.rtc.offer(Entropy.createdDescription);
} }
} }
static gotMessageFromServer(message) { 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); var signal = JSON.parse(message.data);
@ -177,33 +173,33 @@ class Entropy {
if(signal.uuid == Config.getUUID()) return; if(signal.uuid == Config.getUUID()) return;
if(signal.sdp) { 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 // Only create answers in response to offers
if(signal.sdp.type == 'offer') { 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) { } 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) { static createdDescription(description) {
View.log('got description'); Log.log('got description');
Entropy.peer.peer Entropy.rtc.conn
.setLocalDescription(description) .setLocalDescription(description)
.then(function() { .then(function() {
Server.server.send(JSON.stringify({ WS.ws.send(JSON.stringify({
'sdp': Entropy.peer.peer.localDescription, 'sdp': Entropy.rtc.conn.localDescription,
'uuid': Config.getUUID(), 'uuid': Config.getUUID(),
})); }));
}) })
.catch(View.error); .catch(Log.error);
} }
} }
Entropy.local = null; Entropy.local = null;
Entropy.peer = null; Entropy.rtc = null;
class Local { class Local {
stream = null; stream = null;
@ -213,7 +209,7 @@ class Local {
case "audio": return new Audio(); case "audio": return new Audio();
case "video": return new Video(); case "video": return new Video();
case "text": return new Text(); 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", audio: this.gettype() == "audio",
}; };
if(!navigator.mediaDevices.getUserMedia) { 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) navigator.mediaDevices.getUserMedia(constraints)
.then((stream) => { .then((stream) => {
@ -232,11 +228,11 @@ class Local {
} }
this.stream = stream; this.stream = stream;
}) })
.catch(View.error); .catch(Log.error);
} }
gettype() { gettype() {
View.error("not impl"); Log.error("not impl");
} }
start() {} start() {}
@ -263,7 +259,7 @@ var streams = {}
var rconsole = console; var rconsole = console;
var console = {} var console = {}
for (var i in ["log", "info", "warn", "error"]) { for (var i in ["log", "info", "warn", "error"]) {
console[i] = View[i]; console[i] = Log[i];
} }
window.console = console; window.console = console;
@ -273,7 +269,7 @@ function pageReady() {
if (window.location.port) { if (window.location.port) {
host += ":" + window.location.port; host += ":" + window.location.port;
} }
new Server('wss://' + host + '/abc', Entropy.gotMessageFromServer); new WS('wss://' + host + '/abc', Entropy.gotMessageFromServer);
Entropy.pageReady(); Entropy.pageReady();
} }