Checkpoint audio-only Skald fork work
This commit is contained in:
@@ -7,15 +7,10 @@
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<div><button id="start">Start</button></p>
|
||||
<div><button id="start">Start</button></div>
|
||||
|
||||
<p id="status"></p>
|
||||
<p id="error"></p>
|
||||
|
||||
<div><button id="show" disabled>Show/hide yourself</button></div>
|
||||
|
||||
<div id="videos"></div>
|
||||
|
||||
<div id="chat"></div>
|
||||
|
||||
<script src="/protocol.js" defer></script>
|
||||
|
||||
+12
-177
@@ -1,4 +1,4 @@
|
||||
/* Skald client example. */
|
||||
/* Skald client example - audio-only. */
|
||||
|
||||
/**
|
||||
* The main function.
|
||||
@@ -6,7 +6,7 @@
|
||||
* @param {string} url
|
||||
*/
|
||||
async function start(url) {
|
||||
// fetch the group information
|
||||
// fetch the hall information
|
||||
let r = await fetch(url + ".status");
|
||||
if(!r.ok) {
|
||||
throw new Error(`${r.status} ${r.statusText}`);
|
||||
@@ -23,7 +23,7 @@ async function start(url) {
|
||||
if(token) {
|
||||
serverConnect(status, token);
|
||||
} else if(status.authPortal) {
|
||||
window.location.href = groupStatus.authPortal
|
||||
window.location.href = status.authPortal
|
||||
return;
|
||||
} else {
|
||||
serverConnect(status, null);
|
||||
@@ -33,7 +33,7 @@ async function start(url) {
|
||||
/**
|
||||
* Display the connection status.
|
||||
*
|
||||
* @parm {string} status
|
||||
* @param {string} status
|
||||
*/
|
||||
function displayStatus(status) {
|
||||
let c = document.getElementById('status');
|
||||
@@ -43,38 +43,29 @@ function displayStatus(status) {
|
||||
/**
|
||||
* Connect to the server.
|
||||
*
|
||||
* @parm {Object} status
|
||||
* @parm {string} token
|
||||
* @param {Object} status
|
||||
* @param {string} token
|
||||
*/
|
||||
function serverConnect(status, token) {
|
||||
// create the connection to the server
|
||||
let conn = new ServerConnection();
|
||||
conn.onconnected = async function() {
|
||||
displayStatus('Connected');
|
||||
let creds = token ?
|
||||
{type: 'token', token: token} :
|
||||
{type: 'password', password: ''};
|
||||
// join the group and wait for the onjoined callback
|
||||
await this.join("public", "example-user", creds);
|
||||
};
|
||||
conn.onchat = onChat;
|
||||
conn.onusermessage = onUserMessage;
|
||||
conn.ondownstream = onDownStream;
|
||||
conn.onclose = function() {
|
||||
displayStatus('Disconnected');
|
||||
}
|
||||
conn.onjoined = onJoined;
|
||||
|
||||
// connect and wait for the onconnected callback
|
||||
conn.connect(status.endpoint);
|
||||
}
|
||||
|
||||
/**
|
||||
* Called whenever we receive a chat message.
|
||||
*
|
||||
* @this {ServerConnection}
|
||||
* @parm {string} username
|
||||
* @parm {string} message
|
||||
*/
|
||||
function onChat(id, dest, username, time, privileged, history, kind, message) {
|
||||
let p = document.createElement('p');
|
||||
@@ -85,11 +76,6 @@ function onChat(id, dest, username, time, privileged, history, kind, message) {
|
||||
|
||||
/**
|
||||
* Called whenever we receive a user message.
|
||||
*
|
||||
* @this {ServerConnection}
|
||||
* @parm {string} username
|
||||
* @parm {string} message
|
||||
* @parm {string} kind
|
||||
*/
|
||||
function onUserMessage(id, dest, username, time, privileged, kind, error, message) {
|
||||
switch(kind) {
|
||||
@@ -113,57 +99,13 @@ function onUserMessage(id, dest, username, time, privileged, kind, error, messag
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Find the camera stream, if any.
|
||||
*
|
||||
* @parm {string} conn
|
||||
* @returns {Stream}
|
||||
* Called when we join or leave a hall.
|
||||
*/
|
||||
function cameraStream(conn) {
|
||||
for(let id in conn.up) {
|
||||
let s = conn.up[id];
|
||||
if(s.label === 'camera')
|
||||
return s;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Enable or disable the show/hide button.
|
||||
*
|
||||
* @parm{ServerConnection} conn
|
||||
* @parm{boolean} enable
|
||||
*/
|
||||
function enableShow(conn, enable) {
|
||||
let b = /** @type{HTMLButtonElement} */(document.getElementById('show'));
|
||||
if(enable) {
|
||||
b.onclick = function() {
|
||||
let s = cameraStream(conn);
|
||||
if(!s)
|
||||
showCamera(conn);
|
||||
else
|
||||
hide(conn, s);
|
||||
}
|
||||
b.disabled = false;
|
||||
} else {
|
||||
b.disabled = true;
|
||||
b.onclick = null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Called when we join or leave a group.
|
||||
*
|
||||
* @this {ServerConnection}
|
||||
* @parm {string} kind
|
||||
* @parm {string} message}
|
||||
*/
|
||||
async function onJoined(kind, group, perms, status, data, error, message) {
|
||||
async function onJoined(kind, hall, perms, status, data, error, message) {
|
||||
switch(kind) {
|
||||
case 'fail':
|
||||
displayError(message);
|
||||
enableShow(this, false);
|
||||
this.close();
|
||||
break;
|
||||
case 'redirect':
|
||||
@@ -172,15 +114,13 @@ async function onJoined(kind, group, perms, status, data, error, message) {
|
||||
return;
|
||||
case 'leave':
|
||||
displayStatus('Connected');
|
||||
enableShow(this, false);
|
||||
this.close();
|
||||
break;
|
||||
case 'join':
|
||||
case 'change':
|
||||
displayStatus(`Connected as ${this.username} in group ${this.group}.`);
|
||||
enableShow(this, true);
|
||||
// request videos from the server
|
||||
this.request({'': ['audio', 'video']});
|
||||
displayStatus(`Connected as ${this.username} in hall ${this.hall}.`);
|
||||
// request audio from the server
|
||||
this.request({'': ['audio']});
|
||||
break;
|
||||
default:
|
||||
displayError(`Unexpected state ${kind}.`);
|
||||
@@ -189,115 +129,10 @@ async function onJoined(kind, group, perms, status, data, error, message) {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a video element. We encode the stream's id in the element's id
|
||||
* in order to avoid having a global hash table that maps ids to video
|
||||
* elements.
|
||||
*
|
||||
* @parm {string} id
|
||||
* @returns {HTMLVideoElement}
|
||||
*/
|
||||
function makeVideoElement(id) {
|
||||
let v = document.createElement('video');
|
||||
v.id = 'video-' + id;
|
||||
let container = document.getElementById('videos');
|
||||
container.appendChild(v);
|
||||
return v;
|
||||
}
|
||||
|
||||
/**
|
||||
* Find the video element that shows a given id.
|
||||
*
|
||||
* @parm {string} id
|
||||
* @returns {HTMLVideoElement}
|
||||
*/
|
||||
function getVideoElement(id) {
|
||||
let v = document.getElementById('video-' + id);
|
||||
return /** @type{HTMLVideoElement} */(v);
|
||||
}
|
||||
|
||||
/**
|
||||
* Enable the camera and broadcast yourself to the group.
|
||||
*
|
||||
* @parm {ServerConnection} conn
|
||||
*/
|
||||
async function showCamera(conn) {
|
||||
let ms = await navigator.mediaDevices.getUserMedia({audio: true, video: true});
|
||||
|
||||
/* Send the new stream to the server */
|
||||
let s = conn.newUpStream();
|
||||
s.label = 'camera';
|
||||
s.setStream(ms);
|
||||
let v = makeVideoElement(s.localId);
|
||||
s.onclose = function(replace) {
|
||||
s.stream.getTracks().forEach(t => t.stop());
|
||||
v.srcObject = null;
|
||||
v.parentNode.removeChild(v);
|
||||
}
|
||||
|
||||
function addTrack(t) {
|
||||
t.oneneded = function(e) {
|
||||
ms.onaddtrack = null;
|
||||
s.onremovetrack = null;
|
||||
s.close();
|
||||
}
|
||||
s.pc.addTransceiver(t, {
|
||||
direction: 'sendonly',
|
||||
streams: [ms],
|
||||
});
|
||||
}
|
||||
|
||||
// Make sure all future tracks are added.
|
||||
s.onaddtrack = function(e) {
|
||||
addTrack(e.track);
|
||||
}
|
||||
// Add any existing tracks.
|
||||
ms.getTracks().forEach(addTrack);
|
||||
|
||||
// Connect the MediaStream to the video element and start playing.
|
||||
v.srcObject = ms;
|
||||
v.muted = true;
|
||||
v.play();
|
||||
}
|
||||
|
||||
/**
|
||||
* Stop broadcasting.
|
||||
*
|
||||
* @parm {ServerConnection} conn
|
||||
* @parm {Stream} s
|
||||
*/
|
||||
async function hide(conn, s) {
|
||||
s.stream.getTracks().forEach(t => t.stop());
|
||||
s.close();
|
||||
}
|
||||
|
||||
/**
|
||||
* Called when the server pushes a stream.
|
||||
*
|
||||
* @this {ServerConnection}
|
||||
* @parm {Stream} c
|
||||
*/
|
||||
function onDownStream(s) {
|
||||
s.onclose = function(replace) {
|
||||
let v = getVideoElement(s.localId);
|
||||
v.srcObject = null;
|
||||
v.parentNode.removeChild(v);
|
||||
}
|
||||
s.ondowntrack = function(track, transceiver, stream) {
|
||||
let v = getVideoElement(s.localId);
|
||||
if(v.srcObject !== stream)
|
||||
v.srcObject = stream;
|
||||
}
|
||||
|
||||
let v = makeVideoElement(s.localId);
|
||||
v.srcObject = s.stream;
|
||||
v.play();
|
||||
}
|
||||
|
||||
/**
|
||||
* Display an error message.
|
||||
*
|
||||
* @parm {string} message
|
||||
* @param {string} message
|
||||
*/
|
||||
function displayError(message) {
|
||||
document.getElementById('error').textContent = message;
|
||||
|
||||
Reference in New Issue
Block a user