Move webrtc into big static class
parent
2e77beabc3
commit
4fdb421204
281
public/webrtc.js
281
public/webrtc.js
|
|
@ -1,142 +1,147 @@
|
|||
var localVideo;
|
||||
var localStream;
|
||||
var remoteVideo;
|
||||
var peerConnection;
|
||||
var uuid;
|
||||
var serverConnection;
|
||||
|
||||
var peerConnectionConfig = {
|
||||
'iceServers': [
|
||||
{'urls': 'stun:stun.stunprotocol.org:3478'},
|
||||
{'urls': 'stun:stun.l.google.com:19302'},
|
||||
]
|
||||
};
|
||||
|
||||
function 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 "";
|
||||
}
|
||||
|
||||
function 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=/";
|
||||
class Entropy {
|
||||
static localVideo = null;
|
||||
static localStream = null;
|
||||
static remoteVideo = null;
|
||||
static peerConnection = null;
|
||||
static uuid = null;
|
||||
static serverConnection = null;
|
||||
static peerConnectionConfig = {
|
||||
'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 pageReady() {
|
||||
Entropy.uuid = Entropy.getCookie("uuid");
|
||||
if (!Entropy.uuid) {
|
||||
Entropy.uuid = Entropy.createUUID();
|
||||
Entropy.setCookie("uuid", Entropy.uuid);
|
||||
}
|
||||
|
||||
Entropy.localVideo = document.getElementById('localVideo');
|
||||
Entropy.remoteVideo = document.getElementById('remoteVideo');
|
||||
|
||||
Entropy.serverConnection = new WebSocket('wss://' + window.location.hostname + '/abc');
|
||||
Entropy.serverConnection.onmessage = Entropy.gotMessageFromServer;
|
||||
|
||||
var constraints = {
|
||||
video: true,
|
||||
audio: false,
|
||||
};
|
||||
|
||||
if(navigator.mediaDevices.getUserMedia) {
|
||||
navigator.mediaDevices.getUserMedia(constraints).then(Entropy.getUserMediaSuccess).catch(Entropy.errorHandler);
|
||||
} else {
|
||||
alert('Your browser does not support getUserMedia API');
|
||||
}
|
||||
}
|
||||
|
||||
static getUserMediaSuccess(stream) {
|
||||
Entropy.localStream = stream;
|
||||
Entropy.localVideo.srcObject = stream;
|
||||
}
|
||||
|
||||
static start(isCaller) {
|
||||
Entropy.peerConnection = new RTCPeerConnection(Entropy.peerConnectionConfig);
|
||||
Entropy.peerConnection.onicecandidate = Entropy.gotIceCandidate;
|
||||
Entropy.peerConnection.ontrack = Entropy.gotRemoteStream;
|
||||
Entropy.peerConnection.addStream(Entropy.localStream);
|
||||
|
||||
if(isCaller) {
|
||||
Entropy.peerConnection
|
||||
.createOffer({
|
||||
'iceRestart': true,
|
||||
'voiceActivityDetection': true,
|
||||
})
|
||||
.then(Entropy.createdDescription)
|
||||
.catch(Entropy.errorHandler);
|
||||
}
|
||||
}
|
||||
|
||||
static gotMessageFromServer(message) {
|
||||
if(!Entropy.peerConnection) Entropy.start(false);
|
||||
|
||||
var signal = JSON.parse(message.data);
|
||||
|
||||
// Ignore messages from ourself
|
||||
if(signal.uuid == Entropy.uuid) return;
|
||||
|
||||
if(signal.sdp) {
|
||||
Entropy.peerConnection.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(Entropy.errorHandler);
|
||||
}
|
||||
}).catch(Entropy.errorHandler);
|
||||
} else if(signal.ice) {
|
||||
Entropy.peerConnection.addIceCandidate(new RTCIceCandidate(signal.ice)).catch(Entropy.errorHandler);
|
||||
}
|
||||
}
|
||||
|
||||
static gotIceCandidate(event) {
|
||||
if(event.candidate != null) {
|
||||
Entropy.serverConnection.send(JSON.stringify({'ice': event.candidate, 'uuid': Entropy.uuid}));
|
||||
}
|
||||
}
|
||||
|
||||
static createdDescription(description) {
|
||||
console.log('got description');
|
||||
|
||||
Entropy.peerConnection
|
||||
.setLocalDescription(description)
|
||||
.then(function() {
|
||||
Entropy.serverConnection.send(JSON.stringify({
|
||||
'sdp': Entropy.peerConnection.localDescription,
|
||||
'uuid': Entropy.uuid,
|
||||
}));
|
||||
})
|
||||
.catch(errorHandler);
|
||||
}
|
||||
|
||||
static gotRemoteStream(event) {
|
||||
console.log('got remote stream');
|
||||
Entropy.remoteVideo.srcObject = event.streams[0];
|
||||
}
|
||||
|
||||
static errorHandler(error) {
|
||||
console.log(error);
|
||||
}
|
||||
|
||||
// Taken from http://stackoverflow.com/a/105074/515584
|
||||
// Strictly speaking, it's not a real UUID, but it gets the job done here
|
||||
static createUUID() {
|
||||
function s4() {
|
||||
return Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1);
|
||||
}
|
||||
|
||||
return s4() + s4() + '-' + s4() + '-' + s4() + '-' + s4() + '-' + s4() + s4() + s4();
|
||||
}
|
||||
}
|
||||
|
||||
function pageReady() {
|
||||
uuid = getCookie("uuid");
|
||||
if (!uuid) {
|
||||
uuid = createUUID();
|
||||
setCookie("uuid", uuid);
|
||||
}
|
||||
|
||||
localVideo = document.getElementById('localVideo');
|
||||
remoteVideo = document.getElementById('remoteVideo');
|
||||
|
||||
serverConnection = new WebSocket('wss://' + window.location.hostname + '/abc?uuid=' + uuid);
|
||||
serverConnection.onmessage = gotMessageFromServer;
|
||||
|
||||
var constraints = {
|
||||
video: true,
|
||||
audio: true,
|
||||
};
|
||||
|
||||
if(navigator.mediaDevices.getUserMedia) {
|
||||
navigator.mediaDevices.getUserMedia(constraints).then(getUserMediaSuccess).catch(errorHandler);
|
||||
} else {
|
||||
alert('Your browser does not support getUserMedia API');
|
||||
}
|
||||
}
|
||||
|
||||
function getUserMediaSuccess(stream) {
|
||||
localStream = stream;
|
||||
localVideo.srcObject = stream;
|
||||
}
|
||||
|
||||
function start(isCaller) {
|
||||
peerConnection = new RTCPeerConnection(peerConnectionConfig);
|
||||
peerConnection.onicecandidate = gotIceCandidate;
|
||||
peerConnection.ontrack = gotRemoteStream;
|
||||
peerConnection.addStream(localStream);
|
||||
|
||||
if(isCaller) {
|
||||
peerConnection
|
||||
.createOffer({
|
||||
'iceRestart': true,
|
||||
'voiceActivityDetection': true,
|
||||
})
|
||||
.then(createdDescription)
|
||||
.catch(errorHandler);
|
||||
}
|
||||
}
|
||||
|
||||
function gotMessageFromServer(message) {
|
||||
if(!peerConnection) start(false);
|
||||
|
||||
var signal = JSON.parse(message.data);
|
||||
|
||||
// Ignore messages from ourself
|
||||
if(signal.uuid == uuid) return;
|
||||
|
||||
if(signal.sdp) {
|
||||
peerConnection.setRemoteDescription(new RTCSessionDescription(signal.sdp)).then(function() {
|
||||
// Only create answers in response to offers
|
||||
if(signal.sdp.type == 'offer') {
|
||||
peerConnection.createAnswer().then(createdDescription).catch(errorHandler);
|
||||
}
|
||||
}).catch(errorHandler);
|
||||
} else if(signal.ice) {
|
||||
peerConnection.addIceCandidate(new RTCIceCandidate(signal.ice)).catch(errorHandler);
|
||||
}
|
||||
}
|
||||
|
||||
function gotIceCandidate(event) {
|
||||
if(event.candidate != null) {
|
||||
serverConnection.send(JSON.stringify({'ice': event.candidate, 'uuid': uuid}));
|
||||
}
|
||||
}
|
||||
|
||||
function createdDescription(description) {
|
||||
console.log('got description');
|
||||
|
||||
peerConnection
|
||||
.setLocalDescription(description)
|
||||
.then(function() {
|
||||
serverConnection.send(JSON.stringify({
|
||||
'sdp': peerConnection.localDescription,
|
||||
'uuid': uuid,
|
||||
}));
|
||||
})
|
||||
.catch(errorHandler);
|
||||
}
|
||||
|
||||
function gotRemoteStream(event) {
|
||||
console.log('got remote stream');
|
||||
remoteVideo.srcObject = event.streams[0];
|
||||
}
|
||||
|
||||
function errorHandler(error) {
|
||||
console.log(error);
|
||||
}
|
||||
|
||||
// Taken from http://stackoverflow.com/a/105074/515584
|
||||
// Strictly speaking, it's not a real UUID, but it gets the job done here
|
||||
function createUUID() {
|
||||
function s4() {
|
||||
return Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1);
|
||||
}
|
||||
|
||||
return s4() + s4() + '-' + s4() + '-' + s4() + '-' + s4() + '-' + s4() + s4() + s4();
|
||||
Entropy.pageReady();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,142 @@
|
|||
var localVideo;
|
||||
var localStream;
|
||||
var remoteVideo;
|
||||
var peerConnection;
|
||||
var uuid;
|
||||
var serverConnection;
|
||||
|
||||
var peerConnectionConfig = {
|
||||
'iceServers': [
|
||||
{'urls': 'stun:stun.stunprotocol.org:3478'},
|
||||
{'urls': 'stun:stun.l.google.com:19302'},
|
||||
]
|
||||
};
|
||||
|
||||
function 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 "";
|
||||
}
|
||||
|
||||
function 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=/";
|
||||
}
|
||||
|
||||
function pageReady() {
|
||||
uuid = getCookie("uuid");
|
||||
if (!uuid) {
|
||||
uuid = createUUID();
|
||||
setCookie("uuid", uuid);
|
||||
}
|
||||
|
||||
localVideo = document.getElementById('localVideo');
|
||||
remoteVideo = document.getElementById('remoteVideo');
|
||||
|
||||
serverConnection = new WebSocket('wss://' + window.location.hostname + '/abc?uuid=' + uuid);
|
||||
serverConnection.onmessage = gotMessageFromServer;
|
||||
|
||||
var constraints = {
|
||||
video: true,
|
||||
audio: true,
|
||||
};
|
||||
|
||||
if(navigator.mediaDevices.getUserMedia) {
|
||||
navigator.mediaDevices.getUserMedia(constraints).then(getUserMediaSuccess).catch(errorHandler);
|
||||
} else {
|
||||
alert('Your browser does not support getUserMedia API');
|
||||
}
|
||||
}
|
||||
|
||||
function getUserMediaSuccess(stream) {
|
||||
localStream = stream;
|
||||
localVideo.srcObject = stream;
|
||||
}
|
||||
|
||||
function start(isCaller) {
|
||||
peerConnection = new RTCPeerConnection(peerConnectionConfig);
|
||||
peerConnection.onicecandidate = gotIceCandidate;
|
||||
peerConnection.ontrack = gotRemoteStream;
|
||||
peerConnection.addStream(localStream);
|
||||
|
||||
if(isCaller) {
|
||||
peerConnection
|
||||
.createOffer({
|
||||
'iceRestart': true,
|
||||
'voiceActivityDetection': true,
|
||||
})
|
||||
.then(createdDescription)
|
||||
.catch(errorHandler);
|
||||
}
|
||||
}
|
||||
|
||||
function gotMessageFromServer(message) {
|
||||
if(!peerConnection) start(false);
|
||||
|
||||
var signal = JSON.parse(message.data);
|
||||
|
||||
// Ignore messages from ourself
|
||||
if(signal.uuid == uuid) return;
|
||||
|
||||
if(signal.sdp) {
|
||||
peerConnection.setRemoteDescription(new RTCSessionDescription(signal.sdp)).then(function() {
|
||||
// Only create answers in response to offers
|
||||
if(signal.sdp.type == 'offer') {
|
||||
peerConnection.createAnswer().then(createdDescription).catch(errorHandler);
|
||||
}
|
||||
}).catch(errorHandler);
|
||||
} else if(signal.ice) {
|
||||
peerConnection.addIceCandidate(new RTCIceCandidate(signal.ice)).catch(errorHandler);
|
||||
}
|
||||
}
|
||||
|
||||
function gotIceCandidate(event) {
|
||||
if(event.candidate != null) {
|
||||
serverConnection.send(JSON.stringify({'ice': event.candidate, 'uuid': uuid}));
|
||||
}
|
||||
}
|
||||
|
||||
function createdDescription(description) {
|
||||
console.log('got description');
|
||||
|
||||
peerConnection
|
||||
.setLocalDescription(description)
|
||||
.then(function() {
|
||||
serverConnection.send(JSON.stringify({
|
||||
'sdp': peerConnection.localDescription,
|
||||
'uuid': uuid,
|
||||
}));
|
||||
})
|
||||
.catch(errorHandler);
|
||||
}
|
||||
|
||||
function gotRemoteStream(event) {
|
||||
console.log('got remote stream');
|
||||
remoteVideo.srcObject = event.streams[0];
|
||||
}
|
||||
|
||||
function errorHandler(error) {
|
||||
console.log(error);
|
||||
}
|
||||
|
||||
// Taken from http://stackoverflow.com/a/105074/515584
|
||||
// Strictly speaking, it's not a real UUID, but it gets the job done here
|
||||
function createUUID() {
|
||||
function s4() {
|
||||
return Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1);
|
||||
}
|
||||
|
||||
return s4() + s4() + '-' + s4() + '-' + s4() + '-' + s4() + '-' + s4() + s4() + s4();
|
||||
}
|
||||
|
|
@ -0,0 +1,175 @@
|
|||
class Self {
|
||||
uuid = null
|
||||
connections = []
|
||||
|
||||
constructor() {
|
||||
this.connections.push(new Stream(this.id(), {audio: true, video: true}))
|
||||
}
|
||||
|
||||
start(isCaller) {
|
||||
for(var i of this.connections)
|
||||
i.start(isCaller)
|
||||
}
|
||||
|
||||
id() {
|
||||
this.uuid = this.get("uuid")
|
||||
if (!this.uuid) {
|
||||
s4 = () => Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1)
|
||||
this.uuid = s4() + s4() + '-' + s4() + '-' + s4() + '-' + s4() + '-' + s4() + s4() + s4();
|
||||
this.set("uuid", this.uuid)
|
||||
}
|
||||
return this.uuid
|
||||
}
|
||||
|
||||
get(k) {
|
||||
var name = k + "=";
|
||||
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 "";
|
||||
}
|
||||
|
||||
set(k, v) {
|
||||
var d = new Date();
|
||||
d.setTime(d.getTime() + (1*24*60*60*1000));
|
||||
var expires = "expires="+ d.toUTCString();
|
||||
document.cookie = k + "=" + v + ";" + expires + ";path=/";
|
||||
}
|
||||
}
|
||||
|
||||
class Stream {
|
||||
serverConnection = null
|
||||
localVideo = null
|
||||
remoteVideo = null
|
||||
localStream = null
|
||||
id = null
|
||||
|
||||
constructor(id, config) {
|
||||
if (config.audio) {
|
||||
id += "a"
|
||||
}
|
||||
if (config.video) {
|
||||
id += "v"
|
||||
}
|
||||
this.id = id
|
||||
|
||||
this.serverConnection = new WebSocket('wss://' + window.location.hostname + '/abc?uuid=' + this.id)
|
||||
this.serverConnection.onmessage = (m) => this.gotMessageFromServer(m)
|
||||
|
||||
if(navigator.mediaDevices.getUserMedia) {
|
||||
navigator.mediaDevices
|
||||
.getUserMedia(config)
|
||||
.then((s) => this.getUserMediaSuccess(s))
|
||||
.catch((e) => this.errorHandler(e))
|
||||
} else {
|
||||
throw new Exception('Your browser does not support getUserMedia API')
|
||||
}
|
||||
}
|
||||
|
||||
start(isCaller) {
|
||||
this.peerConnection = new RTCPeerConnection({
|
||||
'iceServers': [
|
||||
{'urls': 'stun:stun.stunprotocol.org:3478'},
|
||||
{'urls': 'stun:stun.l.google.com:19302'},
|
||||
]
|
||||
});
|
||||
|
||||
this.peerConnection.onicecandidate = (e) => this.gotIceCandidate(e);
|
||||
this.peerConnection.ontrack = (e) => this.gotRemoteStream(e);
|
||||
this.peerConnection.addStream(this.localStream);
|
||||
|
||||
if(isCaller) {
|
||||
this.peerConnection
|
||||
.createOffer({
|
||||
'iceRestart': true,
|
||||
'voiceActivityDetection': true,
|
||||
})
|
||||
.then((d) => this.createdDescription(d))
|
||||
.catch((m) => this.errorHandler(m));
|
||||
}
|
||||
}
|
||||
|
||||
getUserMediaSuccess(stream) {
|
||||
this.localVideo = document.getElementById('localVideo');
|
||||
this.remoteVideo = document.getElementById('remoteVideo');
|
||||
|
||||
this.localStream = stream;
|
||||
this.localVideo.srcObject = stream;
|
||||
}
|
||||
|
||||
gotMessageFromServer(message) {
|
||||
if(!this.peerConnection) start(false);
|
||||
|
||||
var signal = JSON.parse(message.data);
|
||||
|
||||
// Ignore messages from ourself
|
||||
if(signal.uuid == this.id) return;
|
||||
|
||||
if(signal.sdp) {
|
||||
this.peerConnection.setRemoteDescription(new RTCSessionDescription(signal.sdp)).then(function() {
|
||||
// Only create answers in response to offers
|
||||
if(signal.sdp.type == 'offer') {
|
||||
this.peerConnection
|
||||
.createAnswer()
|
||||
.then((d) => this.createdDescription(d))
|
||||
.catch((e) => this.errorHandler(e));
|
||||
}
|
||||
}).catch((e) => this.errorHandler(e));
|
||||
} else if(signal.ice) {
|
||||
this.peerConnection
|
||||
.addIceCandidate(new RTCIceCandidate(signal.ice))
|
||||
.catch((e) => this.errorHandler(e));
|
||||
}
|
||||
}
|
||||
|
||||
gotIceCandidate(event) {
|
||||
if(event.candidate != null) {
|
||||
this.serverConnection
|
||||
.send(JSON.stringify({
|
||||
'ice': event.candidate,
|
||||
'uuid': this.id,
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
createdDescription(description) {
|
||||
console.log('got description');
|
||||
|
||||
this.peerConnection
|
||||
.setLocalDescription(description)
|
||||
.then(() => {
|
||||
this.serverConnection.send(JSON.stringify({
|
||||
'sdp': this.peerConnection.localDescription,
|
||||
'uuid': this.id,
|
||||
}));
|
||||
})
|
||||
.catch((e) => this.errorHandler(e));
|
||||
}
|
||||
|
||||
gotRemoteStream(event) {
|
||||
console.log('got remote stream');
|
||||
this.remoteVideo.srcObject = event.streams[0];
|
||||
}
|
||||
|
||||
errorHandler(error) {
|
||||
console.log(error);
|
||||
}
|
||||
}
|
||||
|
||||
var self
|
||||
|
||||
function pageReady() {
|
||||
self = new Self()
|
||||
}
|
||||
|
||||
function start(isCaller) {
|
||||
self.start(isCaller)
|
||||
}
|
||||
4
ws.go
4
ws.go
|
|
@ -3,6 +3,7 @@ package main
|
|||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
|
@ -33,10 +34,11 @@ func (ws *WS) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
|
||||
func (ws *WS) serveHTTP(w http.ResponseWriter, r *http.Request) error {
|
||||
id := r.URL.Query().Get("uuid")
|
||||
id := strings.Split(r.Header.Get("Cookie"), "=")[1]
|
||||
if len(id) == 0 {
|
||||
id = uuid.New().String()
|
||||
}
|
||||
log.Println("found id", id)
|
||||
log.Println("ws serve http", r.URL.Path)
|
||||
pooli, ok := ws.pools.Load(r.URL.Path)
|
||||
if !ok {
|
||||
|
|
|
|||
Loading…
Reference in New Issue