263 lines
6.0 KiB
JavaScript
Executable File
263 lines
6.0 KiB
JavaScript
Executable File
class Config {
|
|
static iceConfig = {
|
|
'iceServers': [
|
|
{'urls': 'stun:stun.stunprotocol.org:3478'},
|
|
{'urls': 'stun:stun.l.google.com:19302'},
|
|
]
|
|
};
|
|
|
|
static getCookie(cname) {
|
|
var name = cname + "=";
|
|
var decodedCookie = decodeURIComponent(document.cookie);
|
|
var ca = decodedCookie.split(';');
|
|
for(var i = 0; i <ca.length; i++) {
|
|
var c = ca[i];
|
|
while (c.charAt(0) == ' ') {
|
|
c = c.substring(1);
|
|
}
|
|
if (c.indexOf(name) == 0) {
|
|
return c.substring(name.length, c.length);
|
|
}
|
|
}
|
|
return "";
|
|
}
|
|
|
|
static setCookie(cname, cvalue) {
|
|
var d = new Date();
|
|
d.setTime(d.getTime() + (1*24*60*60*1000));
|
|
var expires = "expires="+ d.toUTCString();
|
|
document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
|
|
}
|
|
|
|
static getUUID() {
|
|
var uuid = Config.getCookie('uuid');
|
|
if (!uuid) {
|
|
uuid = Config.createUUID();
|
|
Config.setCookie('uuid');
|
|
}
|
|
return uuid;
|
|
}
|
|
|
|
static createUUID() {
|
|
function s4() {
|
|
return Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1);
|
|
}
|
|
|
|
return s4() + s4() + '-' + s4() + '-' + s4() + '-' + s4() + '-' + s4() + s4() + s4();
|
|
}
|
|
}
|
|
|
|
class View {
|
|
static write(foo, color) {
|
|
var msg = [].slice.call(arguments[2]).join(" ");
|
|
foo(msg);
|
|
document.getElementById("log").innerHTML +=
|
|
'<br><span style="color: ' + color + ';">' + msg + '</span>';
|
|
}
|
|
|
|
static info() {
|
|
View.write(rconsole.info, "gray", arguments);
|
|
}
|
|
|
|
static warn() {
|
|
View.write(rconsole.warn, "orange", arguments);
|
|
}
|
|
|
|
static log() {
|
|
View.write(rconsole.log, "black", arguments);
|
|
}
|
|
|
|
static error() {
|
|
View.write(rconsole.error, "red", arguments);
|
|
}
|
|
}
|
|
|
|
class Remote {
|
|
element = null;
|
|
|
|
constructor(id, stream) {
|
|
this.element = document.getElementById(id);
|
|
this.element.srcObject = stream;
|
|
}
|
|
}
|
|
|
|
class Preview {
|
|
element = null;
|
|
|
|
constructor(id, stream) {
|
|
this.element = document.getElementById(id);
|
|
this.element.srcObject = stream;
|
|
}
|
|
}
|
|
|
|
class Server {
|
|
static server;
|
|
|
|
constructor(address, 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 local = null;
|
|
static peer = null;
|
|
|
|
static pageReady() {
|
|
Entropy.local = Local.build("video");
|
|
}
|
|
|
|
static start(isCaller) {
|
|
Entropy.peer = new Peer(Entropy.local.stream);
|
|
if(isCaller) {
|
|
Entropy.peer.offer(Entropy.createdDescription);
|
|
}
|
|
}
|
|
|
|
static gotMessageFromServer(message) {
|
|
if(!Entropy.peer.peer) Entropy.start(false);
|
|
|
|
var signal = JSON.parse(message.data);
|
|
|
|
// Ignore messages from ourself
|
|
if(signal.uuid == Config.getUUID()) return;
|
|
|
|
if(signal.sdp) {
|
|
Entropy.peer.peer.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);
|
|
}
|
|
}).catch(View.error);
|
|
} else if(signal.ice) {
|
|
Entropy.peer.peer.addIceCandidate(new RTCIceCandidate(signal.ice)).catch(View.error);
|
|
}
|
|
}
|
|
|
|
static createdDescription(description) {
|
|
View.log('got description');
|
|
|
|
Entropy.peer.peer
|
|
.setLocalDescription(description)
|
|
.then(function() {
|
|
Server.server.send(JSON.stringify({
|
|
'sdp': Entropy.peer.peer.localDescription,
|
|
'uuid': Config.getUUID(),
|
|
}));
|
|
})
|
|
.catch(View.error);
|
|
}
|
|
}
|
|
|
|
class Local {
|
|
stream = null;
|
|
|
|
static build(type) {
|
|
switch(type) {
|
|
case "audio": return new Audio();
|
|
case "video": return new Video();
|
|
case "text": return new Text();
|
|
default: View.error("unknown build", type);
|
|
}
|
|
}
|
|
|
|
constructor() {
|
|
var constraints = {
|
|
video: this.gettype() == "video",
|
|
audio: this.gettype() == "audio",
|
|
};
|
|
if(!navigator.mediaDevices.getUserMedia) {
|
|
View.error('Your browser does not support getUserMedia API');
|
|
}
|
|
navigator.mediaDevices.getUserMedia(constraints)
|
|
.then((stream) => {
|
|
if (this.gettype() == "video") {
|
|
new Preview('preview', stream);
|
|
}
|
|
this.stream = stream;
|
|
})
|
|
.catch(View.error);
|
|
}
|
|
|
|
gettype() {
|
|
View.error("not impl");
|
|
}
|
|
|
|
start() {}
|
|
|
|
stop() {}
|
|
}
|
|
|
|
class Audio extends Local {
|
|
gettype() {
|
|
return "audio";
|
|
}
|
|
}
|
|
|
|
class Video extends Local {
|
|
gettype() {
|
|
return "video";
|
|
}
|
|
}
|
|
|
|
class Text extends Local {
|
|
}
|
|
|
|
var streams = {}
|
|
var rconsole = console;
|
|
var console = {}
|
|
for (var i in ["log", "info", "warn", "error"]) {
|
|
console[i] = View[i];
|
|
}
|
|
window.console = console;
|
|
|
|
function pageReady() {
|
|
Config.getUUID();
|
|
new Server('wss://' + window.location.hostname + '/abc', Entropy.gotMessageFromServer);
|
|
Entropy.pageReady();
|
|
}
|
|
|
|
function start(b, type = null) {
|
|
Entropy.start(b);
|
|
}
|
|
|
|
function toggle(type, caller) {
|
|
//start(true, type);
|
|
var exists = Object.keys(streams);
|
|
if (caller.checked && ! (type in exists)) {
|
|
streams[type] = Local.build(type);
|
|
streams[type].start();
|
|
} else if (type in exists) {
|
|
streams[type].stop();
|
|
delete streams[type];
|
|
}
|
|
}
|
|
|