at least it wroks on some browsers
parent
572bab2f00
commit
ff9baa4d4c
|
|
@ -1,7 +1,9 @@
|
|||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<!--
|
||||
<script src="https://webrtc.github.io/adapter/adapter-latest.js"></script>
|
||||
-->
|
||||
<script src="webrtc.js"></script>
|
||||
<style>
|
||||
video {
|
||||
|
|
@ -26,5 +28,8 @@
|
|||
<script type="text/javascript">
|
||||
pageReady();
|
||||
</script>
|
||||
|
||||
<div id="log">
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
|
|
|
|||
|
|
@ -47,8 +47,39 @@ class Config {
|
|||
}
|
||||
}
|
||||
|
||||
function pageReady() {
|
||||
Entropy.pageReady();
|
||||
class View {
|
||||
static write(color, msg) {
|
||||
document.getElementById("log").innerHTML +=
|
||||
'<br><span style="color: ' + color + ';">' + msg + '</span>';
|
||||
}
|
||||
|
||||
static info(msg) {
|
||||
rconsole.info(msg);
|
||||
View.write("gray", msg);
|
||||
}
|
||||
|
||||
static warn(msg) {
|
||||
rconsole.warn(msg);
|
||||
View.write("orange", msg);
|
||||
}
|
||||
|
||||
static log(msg) {
|
||||
rconsole.log(msg);
|
||||
View.write("black", msg);
|
||||
}
|
||||
|
||||
static error(msg) {
|
||||
rconsole.error(msg);
|
||||
View.write("red", msg);
|
||||
}
|
||||
}
|
||||
|
||||
class Controller {
|
||||
static serverConnection;
|
||||
|
||||
constructor(address) {
|
||||
Controller.serverConnection = new WebSocket(address);
|
||||
}
|
||||
}
|
||||
|
||||
class Entropy {
|
||||
|
|
@ -56,7 +87,6 @@ class Entropy {
|
|||
static localStream = null;
|
||||
static remoteVideo = null;
|
||||
static peerConnection = null;
|
||||
static serverConnection = null;
|
||||
|
||||
static pageReady() {
|
||||
Config.getUUID();
|
||||
|
|
@ -64,16 +94,16 @@ class Entropy {
|
|||
Entropy.localVideo = document.getElementById('localVideo');
|
||||
Entropy.remoteVideo = document.getElementById('remoteVideo');
|
||||
|
||||
Entropy.serverConnection = new WebSocket('wss://' + window.location.hostname + '/abc');
|
||||
Entropy.serverConnection.onmessage = Entropy.gotMessageFromServer;
|
||||
new Controller('wss://' + window.location.hostname + '/abc');
|
||||
Controller.serverConnection.onmessage = Entropy.gotMessageFromServer;
|
||||
|
||||
var constraints = {
|
||||
video: true,
|
||||
audio: true,
|
||||
audio: false,
|
||||
};
|
||||
|
||||
if(navigator.mediaDevices.getUserMedia) {
|
||||
navigator.mediaDevices.getUserMedia(constraints).then(Entropy.getUserMediaSuccess).catch(Entropy.errorHandler);
|
||||
navigator.mediaDevices.getUserMedia(constraints).then(Entropy.getUserMediaSuccess).catch(View.error);
|
||||
} else {
|
||||
alert('Your browser does not support getUserMedia API');
|
||||
}
|
||||
|
|
@ -97,7 +127,7 @@ class Entropy {
|
|||
'voiceActivityDetection': true,
|
||||
})
|
||||
.then(Entropy.createdDescription)
|
||||
.catch(Entropy.errorHandler);
|
||||
.catch(View.error);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -113,40 +143,51 @@ class Entropy {
|
|||
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);
|
||||
Entropy.peerConnection.createAnswer().then(Entropy.createdDescription).catch(View.error);
|
||||
}
|
||||
}).catch(Entropy.errorHandler);
|
||||
}).catch(View.error);
|
||||
} else if(signal.ice) {
|
||||
Entropy.peerConnection.addIceCandidate(new RTCIceCandidate(signal.ice)).catch(Entropy.errorHandler);
|
||||
Entropy.peerConnection.addIceCandidate(new RTCIceCandidate(signal.ice)).catch(View.error);
|
||||
}
|
||||
}
|
||||
|
||||
static gotIceCandidate(event) {
|
||||
if(event.candidate != null) {
|
||||
Entropy.serverConnection.send(JSON.stringify({'ice': event.candidate, 'uuid': Config.getUUID()}));
|
||||
Controller.serverConnection.send(JSON.stringify({'ice': event.candidate, 'uuid': Config.getUUID()}));
|
||||
}
|
||||
}
|
||||
|
||||
static createdDescription(description) {
|
||||
console.log('got description');
|
||||
View.log('got description');
|
||||
|
||||
Entropy.peerConnection
|
||||
.setLocalDescription(description)
|
||||
.then(function() {
|
||||
Entropy.serverConnection.send(JSON.stringify({
|
||||
Controller.serverConnection.send(JSON.stringify({
|
||||
'sdp': Entropy.peerConnection.localDescription,
|
||||
'uuid': Config.getUUID(),
|
||||
}));
|
||||
})
|
||||
.catch(errorHandler);
|
||||
.catch(View.error);
|
||||
}
|
||||
|
||||
static gotRemoteStream(event) {
|
||||
console.log('got remote stream');
|
||||
View.log('got remote stream');
|
||||
Entropy.remoteVideo.srcObject = event.streams[0];
|
||||
}
|
||||
}
|
||||
|
||||
static errorHandler(error) {
|
||||
console.log(error);
|
||||
function pageReady() {
|
||||
Entropy.pageReady();
|
||||
}
|
||||
|
||||
function start(b) {
|
||||
Entropy.start(b);
|
||||
}
|
||||
|
||||
var rconsole = console;
|
||||
var console = {}
|
||||
for (var i in ["log", "info", "warn", "error"]) {
|
||||
console[i] = View[i];
|
||||
}
|
||||
window.console = console;
|
||||
|
|
|
|||
Loading…
Reference in New Issue