2020-08-11 17:09:31 +02:00
|
|
|
// Copyright (c) 2020 by Juliusz Chroboczek.
|
|
|
|
|
2020-12-19 17:26:16 +01:00
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
// of this software and associated documentation files (the "Software"), to deal
|
|
|
|
// in the Software without restriction, including without limitation the rights
|
|
|
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
// copies of the Software, and to permit persons to whom the Software is
|
|
|
|
// furnished to do so, subject to the following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included in
|
|
|
|
// all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
// THE SOFTWARE.
|
2020-08-11 17:09:31 +02:00
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* toHex formats an array as a hexadecimal string.
|
2020-09-20 14:33:13 +02:00
|
|
|
* @param {number[]|Uint8Array} array - the array to format
|
|
|
|
* @returns {string} - the hexadecimal representation of array
|
2020-08-11 17:09:31 +02:00
|
|
|
*/
|
|
|
|
function toHex(array) {
|
|
|
|
let a = new Uint8Array(array);
|
|
|
|
function hex(x) {
|
|
|
|
let h = x.toString(16);
|
|
|
|
if(h.length < 2)
|
|
|
|
h = '0' + h;
|
|
|
|
return h;
|
|
|
|
}
|
|
|
|
return a.reduce((x, y) => x + hex(y), '');
|
|
|
|
}
|
|
|
|
|
|
|
|
/** randomid returns a random string of 32 hex digits (16 bytes).
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
function randomid() {
|
|
|
|
let a = new Uint8Array(16);
|
|
|
|
crypto.getRandomValues(a);
|
|
|
|
return toHex(a);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ServerConnection encapsulates a websocket connection to the server and
|
|
|
|
* all the associated streams.
|
|
|
|
* @constructor
|
|
|
|
*/
|
|
|
|
function ServerConnection() {
|
|
|
|
/**
|
|
|
|
* The id of this connection.
|
2020-09-20 14:33:13 +02:00
|
|
|
*
|
2020-08-11 17:09:31 +02:00
|
|
|
* @type {string}
|
2020-09-20 14:33:13 +02:00
|
|
|
* @const
|
2020-08-11 17:09:31 +02:00
|
|
|
*/
|
|
|
|
this.id = randomid();
|
|
|
|
/**
|
|
|
|
* The group that we have joined, or nil if we haven't joined yet.
|
2020-09-20 14:33:13 +02:00
|
|
|
*
|
2020-08-11 17:09:31 +02:00
|
|
|
* @type {string}
|
|
|
|
*/
|
|
|
|
this.group = null;
|
|
|
|
/**
|
|
|
|
* The underlying websocket.
|
2020-09-20 14:33:13 +02:00
|
|
|
*
|
2020-08-11 17:09:31 +02:00
|
|
|
* @type {WebSocket}
|
|
|
|
*/
|
|
|
|
this.socket = null;
|
|
|
|
/**
|
|
|
|
* The set of all up streams, indexed by their id.
|
2020-09-20 14:33:13 +02:00
|
|
|
*
|
|
|
|
* @type {Object<string,Stream>}
|
2020-08-11 17:09:31 +02:00
|
|
|
*/
|
|
|
|
this.up = {};
|
|
|
|
/**
|
|
|
|
* The set of all down streams, indexed by their id.
|
2020-09-20 14:33:13 +02:00
|
|
|
*
|
|
|
|
* @type {Object<string,Stream>}
|
2020-08-11 17:09:31 +02:00
|
|
|
*/
|
|
|
|
this.down = {};
|
|
|
|
/**
|
|
|
|
* The ICE configuration used by all associated streams.
|
2020-09-20 14:33:13 +02:00
|
|
|
*
|
|
|
|
* @type {RTCIceServer[]}
|
2020-08-11 17:09:31 +02:00
|
|
|
*/
|
2020-08-13 15:45:19 +02:00
|
|
|
this.iceServers = null;
|
2020-08-11 17:09:31 +02:00
|
|
|
/**
|
|
|
|
* The permissions granted to this connection.
|
2020-09-20 14:33:13 +02:00
|
|
|
*
|
|
|
|
* @type {Object<string,boolean>}
|
2020-08-11 17:09:31 +02:00
|
|
|
*/
|
|
|
|
this.permissions = {};
|
2020-09-11 21:39:18 +02:00
|
|
|
/**
|
2020-12-01 18:18:55 +01:00
|
|
|
* userdata is a convenient place to attach data to a ServerConnection.
|
2020-09-11 21:39:18 +02:00
|
|
|
* It is not used by the library.
|
2020-09-20 14:33:13 +02:00
|
|
|
*
|
2020-09-28 20:58:07 +02:00
|
|
|
* @type{Object<unknown,unknown>}
|
2020-09-11 21:39:18 +02:00
|
|
|
*/
|
|
|
|
this.userdata = {};
|
2020-08-11 17:09:31 +02:00
|
|
|
|
|
|
|
/* Callbacks */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* onconnected is called when the connection has been established
|
2020-09-20 14:33:13 +02:00
|
|
|
*
|
2020-09-28 20:58:07 +02:00
|
|
|
* @type{(this: ServerConnection) => void}
|
2020-08-11 17:09:31 +02:00
|
|
|
*/
|
|
|
|
this.onconnected = null;
|
|
|
|
/**
|
|
|
|
* onclose is called when the connection is closed
|
2020-09-20 14:33:13 +02:00
|
|
|
*
|
2020-09-28 20:58:07 +02:00
|
|
|
* @type{(this: ServerConnection, code: number, reason: string) => void}
|
2020-08-11 17:09:31 +02:00
|
|
|
*/
|
|
|
|
this.onclose = null;
|
|
|
|
/**
|
|
|
|
* onuser is called whenever a user is added or removed from the group
|
2020-09-20 14:33:13 +02:00
|
|
|
*
|
2020-09-28 20:58:07 +02:00
|
|
|
* @type{(this: ServerConnection, id: string, kind: string, username: string) => void}
|
2020-08-11 17:09:31 +02:00
|
|
|
*/
|
|
|
|
this.onuser = null;
|
|
|
|
/**
|
2020-12-01 22:42:06 +01:00
|
|
|
* onjoined is called whenever we join or leave a group or whenever the
|
|
|
|
* permissions we have in a group change.
|
2020-09-20 14:33:13 +02:00
|
|
|
*
|
2020-12-01 22:42:06 +01:00
|
|
|
* kind is one of 'join', 'fail', 'change' or 'leave'.
|
|
|
|
*
|
|
|
|
* @type{(this: ServerConnection, kind: string, group: string, permissions: Object<string,boolean>, message: string) => void}
|
2020-08-11 17:09:31 +02:00
|
|
|
*/
|
2020-12-01 22:42:06 +01:00
|
|
|
this.onjoined = null;
|
2020-08-11 17:09:31 +02:00
|
|
|
/**
|
|
|
|
* ondownstream is called whenever a new down stream is added. It
|
|
|
|
* should set up the stream's callbacks; actually setting up the UI
|
|
|
|
* should be done in the stream's ondowntrack callback.
|
2020-09-20 14:33:13 +02:00
|
|
|
*
|
2020-09-28 20:58:07 +02:00
|
|
|
* @type{(this: ServerConnection, stream: Stream) => void}
|
2020-08-11 17:09:31 +02:00
|
|
|
*/
|
|
|
|
this.ondownstream = null;
|
|
|
|
/**
|
|
|
|
* onchat is called whenever a new chat message is received.
|
2020-09-20 14:33:13 +02:00
|
|
|
*
|
2020-12-28 01:42:26 +01:00
|
|
|
* @type {(this: ServerConnection, id: string, dest: string, username: string, time: number, privileged: boolean, kind: string, message: unknown) => void}
|
2020-08-11 17:09:31 +02:00
|
|
|
*/
|
|
|
|
this.onchat = null;
|
2020-11-30 14:06:14 +01:00
|
|
|
/**
|
|
|
|
* onusermessage is called when an application-specific message is
|
|
|
|
* received. Id is null when the message originated at the server,
|
|
|
|
* a user-id otherwise.
|
|
|
|
*
|
|
|
|
* 'kind' is typically one of 'error', 'warning', 'info' or 'mute'. If
|
2020-12-14 10:35:12 +01:00
|
|
|
* 'id' is non-null, 'privileged' indicates whether the message was
|
2020-11-30 14:06:14 +01:00
|
|
|
* sent by an operator.
|
|
|
|
*
|
2020-12-28 01:42:26 +01:00
|
|
|
* @type {(this: ServerConnection, id: string, dest: string, username: string, time: number, privileged: boolean, kind: string, message: unknown) => void}
|
2020-11-30 14:06:14 +01:00
|
|
|
*/
|
|
|
|
this.onusermessage = null;
|
2020-08-11 17:09:31 +02:00
|
|
|
/**
|
|
|
|
* onclearchat is called whenever the server requests that the chat
|
|
|
|
* be cleared.
|
2020-09-20 14:33:13 +02:00
|
|
|
*
|
2020-09-28 20:58:07 +02:00
|
|
|
* @type{(this: ServerConnection) => void}
|
2020-08-11 17:09:31 +02:00
|
|
|
*/
|
|
|
|
this.onclearchat = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @typedef {Object} message
|
|
|
|
* @property {string} type
|
|
|
|
* @property {string} [kind]
|
|
|
|
* @property {string} [id]
|
2020-10-01 16:52:01 +02:00
|
|
|
* @property {string} [dest]
|
2020-08-11 17:09:31 +02:00
|
|
|
* @property {string} [username]
|
|
|
|
* @property {string} [password]
|
2020-12-14 10:35:12 +01:00
|
|
|
* @property {boolean} [privileged]
|
2020-09-20 14:33:13 +02:00
|
|
|
* @property {Object<string,boolean>} [permissions]
|
2020-08-11 17:09:31 +02:00
|
|
|
* @property {string} [group]
|
2020-12-28 01:42:26 +01:00
|
|
|
* @property {unknown} [value]
|
2020-08-11 17:09:31 +02:00
|
|
|
* @property {RTCSessionDescriptionInit} [offer]
|
|
|
|
* @property {RTCSessionDescriptionInit} [answer]
|
|
|
|
* @property {RTCIceCandidate} [candidate]
|
2020-09-20 14:33:13 +02:00
|
|
|
* @property {Object<string,string>} [labels]
|
|
|
|
* @property {Object<string,(boolean|number)>} [request]
|
2020-08-11 17:09:31 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* close forcibly closes a server connection. The onclose callback will
|
|
|
|
* be called when the connection is effectively closed.
|
|
|
|
*/
|
|
|
|
ServerConnection.prototype.close = function() {
|
|
|
|
this.socket && this.socket.close(1000, 'Close requested by client');
|
|
|
|
this.socket = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* send sends a message to the server.
|
|
|
|
* @param {message} m - the message to send.
|
|
|
|
*/
|
|
|
|
ServerConnection.prototype.send = function(m) {
|
2020-08-19 14:39:40 +02:00
|
|
|
if(!this.socket || this.socket.readyState !== this.socket.OPEN) {
|
2020-09-20 14:33:13 +02:00
|
|
|
// send on a closed socket doesn't throw
|
2020-08-11 17:09:31 +02:00
|
|
|
throw(new Error('Connection is not open'));
|
|
|
|
}
|
|
|
|
return this.socket.send(JSON.stringify(m));
|
|
|
|
}
|
|
|
|
|
2020-09-20 14:33:13 +02:00
|
|
|
/**
|
|
|
|
* getIceServers fetches an ICE configuration from the server and
|
2020-08-11 17:09:31 +02:00
|
|
|
* populates the iceServers field of a ServerConnection. It is called
|
|
|
|
* lazily by connect.
|
|
|
|
*
|
2020-09-20 14:33:13 +02:00
|
|
|
* @returns {Promise<RTCIceServer[]>}
|
2020-08-14 14:44:23 +02:00
|
|
|
* @function
|
2020-08-11 17:09:31 +02:00
|
|
|
*/
|
|
|
|
ServerConnection.prototype.getIceServers = async function() {
|
|
|
|
let r = await fetch('/ice-servers.json');
|
|
|
|
if(!r.ok)
|
|
|
|
throw new Error("Couldn't fetch ICE servers: " +
|
|
|
|
r.status + ' ' + r.statusText);
|
|
|
|
let servers = await r.json();
|
|
|
|
if(!(servers instanceof Array))
|
|
|
|
throw new Error("couldn't parse ICE servers");
|
|
|
|
this.iceServers = servers;
|
|
|
|
return servers;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-12-01 18:18:55 +01:00
|
|
|
* connect connects to the server.
|
2020-08-11 17:09:31 +02:00
|
|
|
*
|
|
|
|
* @param {string} url - The URL to connect to.
|
|
|
|
* @returns {Promise<ServerConnection>}
|
2020-09-18 11:28:13 +02:00
|
|
|
* @function
|
2020-08-11 17:09:31 +02:00
|
|
|
*/
|
2020-09-18 11:28:13 +02:00
|
|
|
ServerConnection.prototype.connect = async function(url) {
|
2020-08-11 17:09:31 +02:00
|
|
|
let sc = this;
|
|
|
|
if(sc.socket) {
|
|
|
|
sc.socket.close(1000, 'Reconnecting');
|
|
|
|
sc.socket = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!sc.iceServers) {
|
|
|
|
try {
|
2020-09-18 11:28:13 +02:00
|
|
|
await sc.getIceServers();
|
2020-08-11 17:09:31 +02:00
|
|
|
} catch(e) {
|
2020-09-18 11:28:13 +02:00
|
|
|
console.warn(e);
|
2020-08-11 17:09:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-18 11:28:13 +02:00
|
|
|
sc.socket = new WebSocket(url);
|
2020-08-11 17:09:31 +02:00
|
|
|
|
2020-09-18 11:28:13 +02:00
|
|
|
return await new Promise((resolve, reject) => {
|
2020-08-11 17:09:31 +02:00
|
|
|
this.socket.onerror = function(e) {
|
|
|
|
reject(e);
|
|
|
|
};
|
|
|
|
this.socket.onopen = function(e) {
|
2020-12-01 22:42:06 +01:00
|
|
|
sc.send({
|
|
|
|
type: 'handshake',
|
|
|
|
id: sc.id,
|
|
|
|
});
|
2020-08-11 17:09:31 +02:00
|
|
|
if(sc.onconnected)
|
|
|
|
sc.onconnected.call(sc);
|
|
|
|
resolve(sc);
|
|
|
|
};
|
|
|
|
this.socket.onclose = function(e) {
|
|
|
|
sc.permissions = {};
|
|
|
|
for(let id in sc.down) {
|
|
|
|
let c = sc.down[id];
|
|
|
|
delete(sc.down[id]);
|
2020-12-05 20:27:32 +01:00
|
|
|
c.close();
|
2020-08-11 17:09:31 +02:00
|
|
|
if(c.onclose)
|
|
|
|
c.onclose.call(c);
|
|
|
|
}
|
2020-12-01 22:42:06 +01:00
|
|
|
if(sc.group && sc.onjoined)
|
|
|
|
sc.onjoined.call(sc, 'leave', sc.group, {}, '');
|
|
|
|
sc.group = null;
|
2020-08-11 17:09:31 +02:00
|
|
|
if(sc.onclose)
|
|
|
|
sc.onclose.call(sc, e.code, e.reason);
|
|
|
|
reject(new Error('websocket close ' + e.code + ' ' + e.reason));
|
|
|
|
};
|
|
|
|
this.socket.onmessage = function(e) {
|
|
|
|
let m = JSON.parse(e.data);
|
|
|
|
switch(m.type) {
|
|
|
|
case 'offer':
|
|
|
|
sc.gotOffer(m.id, m.labels, m.offer, m.kind === 'renegotiate');
|
|
|
|
break;
|
|
|
|
case 'answer':
|
|
|
|
sc.gotAnswer(m.id, m.answer);
|
|
|
|
break;
|
|
|
|
case 'renegotiate':
|
|
|
|
sc.gotRenegotiate(m.id)
|
|
|
|
break;
|
|
|
|
case 'close':
|
|
|
|
sc.gotClose(m.id);
|
|
|
|
break;
|
|
|
|
case 'abort':
|
|
|
|
sc.gotAbort(m.id);
|
|
|
|
break;
|
|
|
|
case 'ice':
|
2020-10-30 00:16:09 +01:00
|
|
|
sc.gotRemoteIce(m.id, m.candidate);
|
2020-08-11 17:09:31 +02:00
|
|
|
break;
|
|
|
|
case 'label':
|
|
|
|
sc.gotLabel(m.id, m.value);
|
|
|
|
break;
|
2020-12-01 22:42:06 +01:00
|
|
|
case 'joined':
|
|
|
|
if(sc.group) {
|
|
|
|
if(m.group !== sc.group) {
|
|
|
|
throw new Error('Joined multiple groups');
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
sc.group = m.group;
|
|
|
|
}
|
2020-08-11 17:09:31 +02:00
|
|
|
sc.permissions = m.permissions;
|
2020-12-01 22:42:06 +01:00
|
|
|
if(sc.onjoined)
|
|
|
|
sc.onjoined.call(sc, m.kind, m.group,
|
|
|
|
m.permissions || {},
|
|
|
|
m.value || null);
|
2020-08-11 17:09:31 +02:00
|
|
|
break;
|
|
|
|
case 'user':
|
|
|
|
if(sc.onuser)
|
|
|
|
sc.onuser.call(sc, m.id, m.kind, m.username);
|
|
|
|
break;
|
|
|
|
case 'chat':
|
|
|
|
if(sc.onchat)
|
2020-09-30 00:33:23 +02:00
|
|
|
sc.onchat.call(
|
2020-11-30 14:06:14 +01:00
|
|
|
sc, m.id, m.dest, m.username, m.time,
|
|
|
|
m.privileged, m.kind, m.value,
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
case 'usermessage':
|
|
|
|
if(sc.onusermessage)
|
|
|
|
sc.onusermessage.call(
|
|
|
|
sc, m.id, m.dest, m.username, m.time,
|
2020-12-14 10:35:12 +01:00
|
|
|
m.privileged, m.kind, m.value,
|
2020-09-30 00:33:23 +02:00
|
|
|
);
|
2020-08-11 17:09:31 +02:00
|
|
|
break;
|
|
|
|
case 'clearchat':
|
|
|
|
if(sc.onclearchat)
|
|
|
|
sc.onclearchat.call(sc);
|
|
|
|
break;
|
|
|
|
case 'ping':
|
|
|
|
sc.send({
|
|
|
|
type: 'pong',
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
case 'pong':
|
|
|
|
/* nothing */
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
console.warn('Unexpected server message', m.type);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-12-01 22:42:06 +01:00
|
|
|
* join requests to join a group. The onjoined callback will be called
|
|
|
|
* when we've effectively joined.
|
2020-08-11 17:09:31 +02:00
|
|
|
*
|
2020-12-01 22:42:06 +01:00
|
|
|
* @param {string} group - The name of the group to join.
|
|
|
|
* @param {string} username - the username to join as.
|
2020-09-20 14:33:13 +02:00
|
|
|
* @param {string} password - the password.
|
2020-08-11 17:09:31 +02:00
|
|
|
*/
|
2020-12-01 22:42:06 +01:00
|
|
|
ServerConnection.prototype.join = function(group, username, password) {
|
2020-08-11 17:09:31 +02:00
|
|
|
this.send({
|
2020-12-01 22:42:06 +01:00
|
|
|
type: 'join',
|
|
|
|
kind: 'join',
|
|
|
|
group: group,
|
2020-08-11 17:09:31 +02:00
|
|
|
username: username,
|
|
|
|
password: password,
|
2020-09-14 15:48:17 +02:00
|
|
|
});
|
2020-08-11 17:09:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-12-01 22:42:06 +01:00
|
|
|
* leave leaves a group. The onjoined callback will be called when we've
|
|
|
|
* effectively left.
|
2020-08-11 17:09:31 +02:00
|
|
|
*
|
|
|
|
* @param {string} group - The name of the group to join.
|
|
|
|
*/
|
2020-12-01 22:42:06 +01:00
|
|
|
ServerConnection.prototype.leave = function(group) {
|
2020-08-11 17:09:31 +02:00
|
|
|
this.send({
|
|
|
|
type: 'join',
|
2020-12-01 22:42:06 +01:00
|
|
|
kind: 'leave',
|
2020-08-11 17:09:31 +02:00
|
|
|
group: group,
|
2020-09-14 15:48:17 +02:00
|
|
|
});
|
2020-08-11 17:09:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* request sets the list of requested media types.
|
|
|
|
*
|
2020-09-18 20:09:52 +02:00
|
|
|
* @param {string} what - One of '', 'audio', 'screenshare' or 'everything'.
|
2020-08-11 17:09:31 +02:00
|
|
|
*/
|
|
|
|
ServerConnection.prototype.request = function(what) {
|
2020-09-20 14:33:13 +02:00
|
|
|
/** @type {Object<string,boolean>} */
|
2020-08-11 17:09:31 +02:00
|
|
|
let request = {};
|
|
|
|
switch(what) {
|
2020-09-18 20:09:52 +02:00
|
|
|
case '':
|
|
|
|
request = {};
|
|
|
|
break;
|
2020-08-11 17:09:31 +02:00
|
|
|
case 'audio':
|
|
|
|
request = {audio: true};
|
|
|
|
break;
|
|
|
|
case 'screenshare':
|
|
|
|
request = {audio: true, screenshare: true};
|
|
|
|
break;
|
|
|
|
case 'everything':
|
|
|
|
request = {audio: true, screenshare: true, video: true};
|
|
|
|
break;
|
|
|
|
default:
|
2020-12-01 18:18:55 +01:00
|
|
|
console.error(`Unknown value ${what} in request`);
|
2020-08-11 17:09:31 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.send({
|
|
|
|
type: 'request',
|
|
|
|
request: request,
|
|
|
|
});
|
2020-09-14 15:48:17 +02:00
|
|
|
};
|
2020-08-11 17:09:31 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* newUpStream requests the creation of a new up stream.
|
|
|
|
*
|
2020-08-24 22:31:22 +02:00
|
|
|
* @param {string} [id] - The id of the stream to create.
|
2020-08-11 17:09:31 +02:00
|
|
|
* @returns {Stream}
|
|
|
|
*/
|
|
|
|
ServerConnection.prototype.newUpStream = function(id) {
|
|
|
|
let sc = this;
|
|
|
|
if(!id) {
|
|
|
|
id = randomid();
|
|
|
|
if(sc.up[id])
|
|
|
|
throw new Error('Eek!');
|
|
|
|
}
|
|
|
|
let pc = new RTCPeerConnection({
|
2020-08-13 15:45:19 +02:00
|
|
|
iceServers: sc.iceServers || [],
|
2020-08-11 17:09:31 +02:00
|
|
|
});
|
|
|
|
if(!pc)
|
|
|
|
throw new Error("Couldn't create peer connection");
|
|
|
|
if(sc.up[id]) {
|
2020-12-05 20:27:32 +01:00
|
|
|
sc.up[id].close();
|
2020-08-11 17:09:31 +02:00
|
|
|
}
|
2020-12-05 20:27:32 +01:00
|
|
|
let c = new Stream(this, id, pc, true);
|
2020-08-11 17:09:31 +02:00
|
|
|
sc.up[id] = c;
|
|
|
|
|
|
|
|
pc.onnegotiationneeded = async e => {
|
|
|
|
await c.negotiate();
|
2020-09-14 15:48:17 +02:00
|
|
|
};
|
2020-08-11 17:09:31 +02:00
|
|
|
|
|
|
|
pc.onicecandidate = e => {
|
|
|
|
if(!e.candidate)
|
|
|
|
return;
|
2020-10-30 00:16:09 +01:00
|
|
|
c.gotLocalIce(e.candidate);
|
2020-08-11 17:09:31 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
pc.oniceconnectionstatechange = e => {
|
|
|
|
if(c.onstatus)
|
|
|
|
c.onstatus.call(c, pc.iceConnectionState);
|
2020-09-03 17:12:36 +02:00
|
|
|
if(pc.iceConnectionState === 'failed')
|
|
|
|
c.restartIce();
|
2020-09-14 15:48:17 +02:00
|
|
|
};
|
2020-08-11 17:09:31 +02:00
|
|
|
|
|
|
|
pc.ontrack = console.error;
|
|
|
|
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* chat sends a chat message to the server. The server will normally echo
|
|
|
|
* the message back to the client.
|
|
|
|
*
|
2020-11-30 14:06:14 +01:00
|
|
|
* @param {string} username - The sender's username.
|
2020-12-05 20:21:33 +01:00
|
|
|
* @param {string} kind
|
|
|
|
* - The kind of message, either '', 'me' or an application-specific type.
|
2020-11-30 14:06:14 +01:00
|
|
|
* @param {string} dest - The id to send the message to, empty for broadcast.
|
|
|
|
* @param {string} value - The text of the message.
|
2020-08-11 17:09:31 +02:00
|
|
|
*/
|
2020-11-30 14:06:14 +01:00
|
|
|
ServerConnection.prototype.chat = function(username, kind, dest, value) {
|
2020-08-11 17:09:31 +02:00
|
|
|
this.send({
|
|
|
|
type: 'chat',
|
|
|
|
id: this.id,
|
2020-10-01 16:52:01 +02:00
|
|
|
dest: dest,
|
2020-08-11 17:09:31 +02:00
|
|
|
username: username,
|
|
|
|
kind: kind,
|
2020-11-30 14:06:14 +01:00
|
|
|
value: value,
|
2020-08-11 17:09:31 +02:00
|
|
|
});
|
2020-09-14 15:48:17 +02:00
|
|
|
};
|
2020-08-11 17:09:31 +02:00
|
|
|
|
|
|
|
/**
|
2020-11-30 14:06:14 +01:00
|
|
|
* userAction sends a request to act on a user.
|
2020-08-11 17:09:31 +02:00
|
|
|
*
|
2020-11-30 15:36:59 +01:00
|
|
|
* @param {string} username - The sender's username.
|
2020-11-30 14:06:14 +01:00
|
|
|
* @param {string} kind - One of "op", "unop", "kick", "present", "unpresent".
|
|
|
|
* @param {string} dest - The id of the user to act upon.
|
|
|
|
* @param {string} [value] - An optional user-readable message.
|
2020-08-11 17:09:31 +02:00
|
|
|
*/
|
2020-11-30 15:36:59 +01:00
|
|
|
ServerConnection.prototype.userAction = function(username, kind, dest, value) {
|
2020-08-11 17:09:31 +02:00
|
|
|
this.send({
|
2020-11-30 14:06:14 +01:00
|
|
|
type: 'useraction',
|
|
|
|
id: this.id,
|
|
|
|
dest: dest,
|
2020-11-30 15:36:59 +01:00
|
|
|
username: username,
|
2020-08-11 17:09:31 +02:00
|
|
|
kind: kind,
|
2020-11-30 14:06:14 +01:00
|
|
|
value: value,
|
2020-08-11 17:09:31 +02:00
|
|
|
});
|
2020-09-14 15:48:17 +02:00
|
|
|
};
|
2020-08-11 17:09:31 +02:00
|
|
|
|
|
|
|
/**
|
2020-11-30 14:06:14 +01:00
|
|
|
* userMessage sends an application-specific message to a user.
|
2020-12-05 20:21:33 +01:00
|
|
|
* This is similar to a chat message, but is not saved in the chat history.
|
2020-08-11 17:09:31 +02:00
|
|
|
*
|
2020-11-30 15:36:59 +01:00
|
|
|
* @param {string} username - The sender's username.
|
2020-11-30 14:06:14 +01:00
|
|
|
* @param {string} kind - The kind of application-specific message.
|
2020-12-05 20:21:33 +01:00
|
|
|
* @param {string} dest - The id to send the message to, empty for broadcast.
|
2020-11-30 14:06:14 +01:00
|
|
|
* @param {string} [value] - An optional parameter.
|
|
|
|
*/
|
2020-11-30 15:36:59 +01:00
|
|
|
ServerConnection.prototype.userMessage = function(username, kind, dest, value) {
|
2020-11-30 14:06:14 +01:00
|
|
|
this.send({
|
|
|
|
type: 'usermessage',
|
|
|
|
id: this.id,
|
|
|
|
dest: dest,
|
2020-11-30 15:36:59 +01:00
|
|
|
username: username,
|
2020-11-30 14:06:14 +01:00
|
|
|
kind: kind,
|
|
|
|
value: value,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* groupAction sends a request to act on the current group.
|
|
|
|
*
|
2020-11-30 15:36:59 +01:00
|
|
|
* @param {string} username - The sender's username.
|
2020-12-05 20:21:33 +01:00
|
|
|
* @param {string} kind
|
|
|
|
* - One of 'clearchat', 'lock', 'unlock', 'record' or 'unrecord'.
|
2020-09-20 14:33:13 +02:00
|
|
|
* @param {string} [message] - An optional user-readable message.
|
2020-08-11 17:09:31 +02:00
|
|
|
*/
|
2020-11-30 15:36:59 +01:00
|
|
|
ServerConnection.prototype.groupAction = function(username, kind, message) {
|
2020-08-11 17:09:31 +02:00
|
|
|
this.send({
|
2020-11-30 14:06:14 +01:00
|
|
|
type: 'groupaction',
|
2020-11-30 15:36:59 +01:00
|
|
|
id: this.id,
|
2020-08-11 17:09:31 +02:00
|
|
|
kind: kind,
|
2020-11-30 15:36:59 +01:00
|
|
|
username: username,
|
2020-09-12 12:26:07 +02:00
|
|
|
value: message,
|
2020-08-11 17:09:31 +02:00
|
|
|
});
|
2020-09-14 15:48:17 +02:00
|
|
|
};
|
2020-08-11 17:09:31 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Called when we receive an offer from the server. Don't call this.
|
|
|
|
*
|
|
|
|
* @param {string} id
|
2020-09-20 14:33:13 +02:00
|
|
|
* @param {Object<string, string>} labels
|
2020-08-11 17:09:31 +02:00
|
|
|
* @param {RTCSessionDescriptionInit} offer
|
|
|
|
* @param {boolean} renegotiate
|
2020-08-14 14:44:23 +02:00
|
|
|
* @function
|
2020-08-11 17:09:31 +02:00
|
|
|
*/
|
|
|
|
ServerConnection.prototype.gotOffer = async function(id, labels, offer, renegotiate) {
|
|
|
|
let sc = this;
|
|
|
|
let c = sc.down[id];
|
|
|
|
if(c && !renegotiate) {
|
|
|
|
// SDP is rather inflexible as to what can be renegotiated.
|
|
|
|
// Unless the server indicates that this is a renegotiation with
|
|
|
|
// all parameters unchanged, tear down the existing connection.
|
2020-09-14 15:48:17 +02:00
|
|
|
delete(sc.down[id]);
|
2020-12-05 20:27:32 +01:00
|
|
|
c.close();
|
2020-08-11 17:09:31 +02:00
|
|
|
c = null;
|
|
|
|
}
|
|
|
|
|
2020-12-05 20:27:32 +01:00
|
|
|
if(sc.up[id])
|
|
|
|
throw new Error('Duplicate connection id');
|
|
|
|
|
2020-08-11 17:09:31 +02:00
|
|
|
if(!c) {
|
|
|
|
let pc = new RTCPeerConnection({
|
|
|
|
iceServers: this.iceServers,
|
|
|
|
});
|
2020-12-05 20:27:32 +01:00
|
|
|
c = new Stream(this, id, pc, false);
|
2020-08-11 17:09:31 +02:00
|
|
|
sc.down[id] = c;
|
|
|
|
|
|
|
|
c.pc.onicecandidate = function(e) {
|
|
|
|
if(!e.candidate)
|
|
|
|
return;
|
2020-10-30 00:16:09 +01:00
|
|
|
c.gotLocalIce(e.candidate);
|
2020-08-11 17:09:31 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
pc.oniceconnectionstatechange = e => {
|
|
|
|
if(c.onstatus)
|
|
|
|
c.onstatus.call(c, pc.iceConnectionState);
|
|
|
|
if(pc.iceConnectionState === 'failed') {
|
2020-12-05 20:44:12 +01:00
|
|
|
sc.send({
|
|
|
|
type: 'renegotiate',
|
|
|
|
id: id,
|
|
|
|
});
|
2020-08-11 17:09:31 +02:00
|
|
|
}
|
2020-09-14 15:48:17 +02:00
|
|
|
};
|
2020-08-11 17:09:31 +02:00
|
|
|
|
|
|
|
c.pc.ontrack = function(e) {
|
|
|
|
let label = e.transceiver && c.labelsByMid[e.transceiver.mid];
|
|
|
|
if(label) {
|
|
|
|
c.labels[e.track.id] = label;
|
|
|
|
} else {
|
|
|
|
console.warn("Couldn't find label for track");
|
|
|
|
}
|
|
|
|
if(c.stream !== e.streams[0]) {
|
|
|
|
c.stream = e.streams[0];
|
|
|
|
let label =
|
|
|
|
e.transceiver && c.labelsByMid[e.transceiver.mid];
|
|
|
|
c.labels[e.track.id] = label;
|
|
|
|
if(c.ondowntrack) {
|
|
|
|
c.ondowntrack.call(
|
|
|
|
c, e.track, e.transceiver, label, e.streams[0],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if(c.onlabel) {
|
|
|
|
c.onlabel.call(c, label);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
c.labelsByMid = labels;
|
|
|
|
|
|
|
|
if(sc.ondownstream)
|
|
|
|
sc.ondownstream.call(sc, c);
|
|
|
|
|
2020-12-25 18:26:30 +01:00
|
|
|
try {
|
|
|
|
await c.pc.setRemoteDescription(offer);
|
|
|
|
|
|
|
|
await c.flushRemoteIceCandidates();
|
|
|
|
|
|
|
|
let answer = await c.pc.createAnswer();
|
|
|
|
if(!answer)
|
|
|
|
throw new Error("Didn't create answer");
|
|
|
|
await c.pc.setLocalDescription(answer);
|
|
|
|
this.send({
|
|
|
|
type: 'answer',
|
|
|
|
id: id,
|
|
|
|
answer: answer,
|
|
|
|
});
|
|
|
|
} catch(e) {
|
|
|
|
try {
|
|
|
|
if(c.onerror)
|
|
|
|
c.onerror.call(c, e);
|
|
|
|
} finally {
|
|
|
|
c.abort();
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-12-05 02:52:56 +01:00
|
|
|
c.localDescriptionSent = true;
|
|
|
|
c.flushLocalIceCandidates();
|
2020-08-26 18:30:29 +02:00
|
|
|
if(c.onnegotiationcompleted)
|
|
|
|
c.onnegotiationcompleted.call(c);
|
2020-09-14 15:48:17 +02:00
|
|
|
};
|
2020-08-11 17:09:31 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Called when we receive a stream label from the server. Don't call this.
|
|
|
|
*
|
|
|
|
* @param {string} id
|
|
|
|
* @param {string} label
|
|
|
|
*/
|
|
|
|
ServerConnection.prototype.gotLabel = function(id, label) {
|
|
|
|
let c = this.down[id];
|
|
|
|
if(!c)
|
|
|
|
throw new Error('Got label for unknown id');
|
|
|
|
|
|
|
|
c.label = label;
|
|
|
|
if(c.onlabel)
|
|
|
|
c.onlabel.call(c, label);
|
2020-09-14 15:48:17 +02:00
|
|
|
};
|
2020-08-11 17:09:31 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Called when we receive an answer from the server. Don't call this.
|
|
|
|
*
|
|
|
|
* @param {string} id
|
|
|
|
* @param {RTCSessionDescriptionInit} answer
|
2020-08-14 14:44:23 +02:00
|
|
|
* @function
|
2020-08-11 17:09:31 +02:00
|
|
|
*/
|
|
|
|
ServerConnection.prototype.gotAnswer = async function(id, answer) {
|
|
|
|
let c = this.up[id];
|
|
|
|
if(!c)
|
|
|
|
throw new Error('unknown up stream');
|
|
|
|
try {
|
|
|
|
await c.pc.setRemoteDescription(answer);
|
|
|
|
} catch(e) {
|
2020-12-25 18:26:30 +01:00
|
|
|
try {
|
|
|
|
if(c.onerror)
|
|
|
|
c.onerror.call(c, e);
|
|
|
|
} finally {
|
|
|
|
c.close();
|
|
|
|
}
|
2020-08-11 17:09:31 +02:00
|
|
|
return;
|
|
|
|
}
|
2020-12-05 02:52:56 +01:00
|
|
|
await c.flushRemoteIceCandidates();
|
2020-08-26 18:30:29 +02:00
|
|
|
if(c.onnegotiationcompleted)
|
|
|
|
c.onnegotiationcompleted.call(c);
|
2020-09-14 15:48:17 +02:00
|
|
|
};
|
2020-08-11 17:09:31 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Called when we receive a renegotiation request from the server. Don't
|
|
|
|
* call this.
|
|
|
|
*
|
|
|
|
* @param {string} id
|
2020-08-14 14:44:23 +02:00
|
|
|
* @function
|
2020-08-11 17:09:31 +02:00
|
|
|
*/
|
|
|
|
ServerConnection.prototype.gotRenegotiate = async function(id) {
|
|
|
|
let c = this.up[id];
|
|
|
|
if(!c)
|
|
|
|
throw new Error('unknown up stream');
|
2020-09-03 17:12:36 +02:00
|
|
|
c.restartIce();
|
2020-09-14 15:48:17 +02:00
|
|
|
};
|
2020-08-11 17:09:31 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Called when we receive a close request from the server. Don't call this.
|
|
|
|
*
|
|
|
|
* @param {string} id
|
|
|
|
*/
|
|
|
|
ServerConnection.prototype.gotClose = function(id) {
|
|
|
|
let c = this.down[id];
|
|
|
|
if(!c)
|
|
|
|
throw new Error('unknown down stream');
|
|
|
|
delete(this.down[id]);
|
2020-12-05 20:27:32 +01:00
|
|
|
c.close();
|
2020-08-11 17:09:31 +02:00
|
|
|
if(c.onclose)
|
|
|
|
c.onclose.call(c);
|
2020-09-14 15:48:17 +02:00
|
|
|
};
|
2020-08-11 17:09:31 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Called when we receive an abort message from the server. Don't call this.
|
|
|
|
*
|
|
|
|
* @param {string} id
|
|
|
|
*/
|
|
|
|
ServerConnection.prototype.gotAbort = function(id) {
|
2020-08-13 20:11:21 +02:00
|
|
|
let c = this.up[id];
|
2020-08-11 17:09:31 +02:00
|
|
|
if(!c)
|
|
|
|
throw new Error('unknown up stream');
|
|
|
|
if(c.onabort)
|
|
|
|
c.onabort.call(c);
|
2020-09-14 15:48:17 +02:00
|
|
|
};
|
2020-08-11 17:09:31 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Called when we receive an ICE candidate from the server. Don't call this.
|
|
|
|
*
|
|
|
|
* @param {string} id
|
|
|
|
* @param {RTCIceCandidate} candidate
|
2020-08-14 14:44:23 +02:00
|
|
|
* @function
|
2020-08-11 17:09:31 +02:00
|
|
|
*/
|
2020-10-30 00:16:09 +01:00
|
|
|
ServerConnection.prototype.gotRemoteIce = async function(id, candidate) {
|
2020-08-11 17:09:31 +02:00
|
|
|
let c = this.up[id];
|
|
|
|
if(!c)
|
|
|
|
c = this.down[id];
|
|
|
|
if(!c)
|
|
|
|
throw new Error('unknown stream');
|
|
|
|
if(c.pc.remoteDescription)
|
|
|
|
await c.pc.addIceCandidate(candidate).catch(console.warn);
|
|
|
|
else
|
2020-10-30 00:16:09 +01:00
|
|
|
c.remoteIceCandidates.push(candidate);
|
2020-09-14 15:48:17 +02:00
|
|
|
};
|
2020-08-11 17:09:31 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Stream encapsulates a MediaStream, a set of tracks.
|
|
|
|
*
|
|
|
|
* A stream is said to go "up" if it is from the client to the server, and
|
|
|
|
* "down" otherwise.
|
|
|
|
*
|
|
|
|
* @param {ServerConnection} sc
|
|
|
|
* @param {string} id
|
|
|
|
* @param {RTCPeerConnection} pc
|
|
|
|
*
|
|
|
|
* @constructor
|
|
|
|
*/
|
2020-12-05 20:27:32 +01:00
|
|
|
function Stream(sc, id, pc, up) {
|
2020-08-11 17:09:31 +02:00
|
|
|
/**
|
|
|
|
* The associated ServerConnection.
|
|
|
|
*
|
|
|
|
* @type {ServerConnection}
|
2020-09-20 14:33:13 +02:00
|
|
|
* @const
|
|
|
|
*/
|
2020-08-11 17:09:31 +02:00
|
|
|
this.sc = sc;
|
|
|
|
/**
|
|
|
|
* The id of this stream.
|
|
|
|
*
|
|
|
|
* @type {string}
|
2020-09-20 14:33:13 +02:00
|
|
|
* @const
|
|
|
|
*/
|
2020-08-11 17:09:31 +02:00
|
|
|
this.id = id;
|
2020-12-05 20:27:32 +01:00
|
|
|
/**
|
|
|
|
* Indicates whether the stream is in the client->server direction.
|
|
|
|
*
|
|
|
|
* @type {boolean}
|
|
|
|
* @const
|
|
|
|
*/
|
|
|
|
this.up = up
|
2020-08-11 17:09:31 +02:00
|
|
|
/**
|
|
|
|
* For up streams, one of "local" or "screenshare".
|
|
|
|
*
|
|
|
|
* @type {string}
|
|
|
|
*/
|
|
|
|
this.kind = null;
|
|
|
|
/**
|
|
|
|
* For down streams, a user-readable label.
|
|
|
|
*
|
|
|
|
* @type {string}
|
|
|
|
*/
|
|
|
|
this.label = null;
|
|
|
|
/**
|
|
|
|
* The associated RTCPeerConnectoin. This is null before the stream
|
|
|
|
* is connected, and may change over time.
|
|
|
|
*
|
|
|
|
* @type {RTCPeerConnection}
|
|
|
|
*/
|
|
|
|
this.pc = pc;
|
|
|
|
/**
|
|
|
|
* The associated MediaStream. This is null before the stream is
|
|
|
|
* connected, and may change over time.
|
|
|
|
*
|
|
|
|
* @type {MediaStream}
|
|
|
|
*/
|
|
|
|
this.stream = null;
|
|
|
|
/**
|
|
|
|
* Track labels, indexed by track id.
|
|
|
|
*
|
2020-09-20 14:33:13 +02:00
|
|
|
* @type {Object<string,string>}
|
2020-08-11 17:09:31 +02:00
|
|
|
*/
|
|
|
|
this.labels = {};
|
|
|
|
/**
|
|
|
|
* Track labels, indexed by mid.
|
|
|
|
*
|
2020-09-20 14:33:13 +02:00
|
|
|
* @type {Object<string,string>}
|
2020-08-11 17:09:31 +02:00
|
|
|
*/
|
|
|
|
this.labelsByMid = {};
|
2020-12-05 02:52:56 +01:00
|
|
|
/**
|
|
|
|
* Indicates whether we have already sent a local description.
|
|
|
|
*
|
|
|
|
* @type {boolean}
|
|
|
|
*/
|
|
|
|
this.localDescriptionSent = false;
|
2020-08-11 17:09:31 +02:00
|
|
|
/**
|
2020-10-30 00:16:09 +01:00
|
|
|
* Buffered local ICE candidates. This will be flushed by
|
2020-12-05 02:52:56 +01:00
|
|
|
* flushLocalIceCandidates after we send a local description.
|
2020-10-30 00:16:09 +01:00
|
|
|
*
|
|
|
|
* @type {RTCIceCandidate[]}
|
|
|
|
*/
|
|
|
|
this.localIceCandidates = [];
|
|
|
|
/**
|
|
|
|
* Buffered remote ICE candidates. This will be flushed by
|
2020-12-05 02:52:56 +01:00
|
|
|
* flushRemoteIceCandidates when we get a remote SDP description.
|
2020-08-11 17:09:31 +02:00
|
|
|
*
|
2020-09-20 14:33:13 +02:00
|
|
|
* @type {RTCIceCandidate[]}
|
2020-08-11 17:09:31 +02:00
|
|
|
*/
|
2020-10-30 00:16:09 +01:00
|
|
|
this.remoteIceCandidates = [];
|
2020-08-11 17:09:31 +02:00
|
|
|
/**
|
|
|
|
* The statistics last computed by the stats handler. This is
|
2020-09-28 20:58:07 +02:00
|
|
|
* a dictionary indexed by track id, with each value a dictionary of
|
2020-08-11 17:09:31 +02:00
|
|
|
* statistics.
|
|
|
|
*
|
2020-09-28 20:58:07 +02:00
|
|
|
* @type {Object<string,unknown>}
|
2020-08-11 17:09:31 +02:00
|
|
|
*/
|
|
|
|
this.stats = {};
|
|
|
|
/**
|
|
|
|
* The id of the periodic handler that computes statistics, as
|
|
|
|
* returned by setInterval.
|
|
|
|
*
|
|
|
|
* @type {number}
|
|
|
|
*/
|
|
|
|
this.statsHandler = null;
|
2020-09-11 21:39:18 +02:00
|
|
|
/**
|
2020-12-01 18:18:55 +01:00
|
|
|
* userdata is a convenient place to attach data to a Stream.
|
2020-09-11 21:39:18 +02:00
|
|
|
* It is not used by the library.
|
2020-12-01 18:18:55 +01:00
|
|
|
*
|
2020-09-28 20:58:07 +02:00
|
|
|
* @type{Object<unknown,unknown>}
|
2020-09-11 21:39:18 +02:00
|
|
|
*/
|
|
|
|
this.userdata = {};
|
2020-08-11 17:09:31 +02:00
|
|
|
|
|
|
|
/* Callbacks */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* onclose is called when the stream is closed.
|
|
|
|
*
|
2020-09-28 20:58:07 +02:00
|
|
|
* @type{(this: Stream) => void}
|
2020-08-11 17:09:31 +02:00
|
|
|
*/
|
|
|
|
this.onclose = null;
|
|
|
|
/**
|
|
|
|
* onerror is called whenever an error occurs. If the error is
|
|
|
|
* fatal, then onclose will be called afterwards.
|
|
|
|
*
|
2020-09-28 20:58:07 +02:00
|
|
|
* @type{(this: Stream, error: unknown) => void}
|
2020-08-11 17:09:31 +02:00
|
|
|
*/
|
|
|
|
this.onerror = null;
|
2020-08-26 18:30:29 +02:00
|
|
|
/**
|
|
|
|
* onnegotiationcompleted is called whenever negotiation or
|
|
|
|
* renegotiation has completed.
|
|
|
|
*
|
2020-09-28 20:58:07 +02:00
|
|
|
* @type{(this: Stream) => void}
|
2020-08-26 18:30:29 +02:00
|
|
|
*/
|
|
|
|
this.onnegotiationcompleted = null;
|
2020-08-11 17:09:31 +02:00
|
|
|
/**
|
|
|
|
* ondowntrack is called whenever a new track is added to a stream.
|
|
|
|
* If the stream parameter differs from its previous value, then it
|
|
|
|
* indicates that the old stream has been discarded.
|
|
|
|
*
|
2020-09-28 20:58:07 +02:00
|
|
|
* @type{(this: Stream, track: MediaStreamTrack, transceiver: RTCRtpTransceiver, label: string, stream: MediaStream) => void}
|
2020-08-11 17:09:31 +02:00
|
|
|
*/
|
|
|
|
this.ondowntrack = null;
|
|
|
|
/**
|
|
|
|
* onlabel is called whenever the server sets a new label for the stream.
|
|
|
|
*
|
2020-09-28 20:58:07 +02:00
|
|
|
* @type{(this: Stream, label: string) => void}
|
2020-08-11 17:09:31 +02:00
|
|
|
*/
|
|
|
|
this.onlabel = null;
|
|
|
|
/**
|
|
|
|
* onstatus is called whenever the status of the stream changes.
|
|
|
|
*
|
2020-09-28 20:58:07 +02:00
|
|
|
* @type{(this: Stream, status: string) => void}
|
2020-08-11 17:09:31 +02:00
|
|
|
*/
|
|
|
|
this.onstatus = null;
|
|
|
|
/**
|
|
|
|
* onabort is called when the server requested that an up stream be
|
|
|
|
* closed. It is the resposibility of the client to close the stream.
|
|
|
|
*
|
2020-09-28 20:58:07 +02:00
|
|
|
* @type{(this: Stream) => void}
|
2020-08-11 17:09:31 +02:00
|
|
|
*/
|
|
|
|
this.onabort = null;
|
|
|
|
/**
|
|
|
|
* onstats is called when we have new statistics about the connection
|
|
|
|
*
|
2020-09-28 20:58:07 +02:00
|
|
|
* @type{(this: Stream, stats: Object<unknown,unknown>) => void}
|
2020-08-11 17:09:31 +02:00
|
|
|
*/
|
|
|
|
this.onstats = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-12-05 20:27:32 +01:00
|
|
|
* close closes a stream.
|
2020-12-25 18:25:55 +01:00
|
|
|
*
|
|
|
|
* For streams in the up direction, this may be called at any time. For
|
|
|
|
* streams in the down direction, this will be called automatically when
|
|
|
|
* the server signals that it is closing a stream.
|
2020-08-11 17:09:31 +02:00
|
|
|
*/
|
2020-12-05 20:27:32 +01:00
|
|
|
Stream.prototype.close = function() {
|
2020-08-11 17:09:31 +02:00
|
|
|
let c = this;
|
|
|
|
if(c.statsHandler) {
|
|
|
|
clearInterval(c.statsHandler);
|
|
|
|
c.statsHandler = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(c.stream) {
|
|
|
|
c.stream.getTracks().forEach(t => {
|
|
|
|
try {
|
|
|
|
t.stop();
|
|
|
|
} catch(e) {
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
c.pc.close();
|
|
|
|
|
2020-12-05 20:27:32 +01:00
|
|
|
if(c.up && c.localDescriptionSent) {
|
2020-08-11 17:09:31 +02:00
|
|
|
try {
|
|
|
|
c.sc.send({
|
|
|
|
type: 'close',
|
|
|
|
id: c.id,
|
|
|
|
});
|
|
|
|
} catch(e) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
c.sc = null;
|
|
|
|
};
|
|
|
|
|
2020-12-25 18:25:55 +01:00
|
|
|
/**
|
|
|
|
* abort requests that the server close a down stream.
|
|
|
|
*/
|
|
|
|
Stream.prototype.abort = function() {
|
|
|
|
let c = this;
|
|
|
|
if(c.up)
|
|
|
|
throw new Error("Abort called on an up stream");
|
|
|
|
c.sc.send({
|
|
|
|
type: 'abort',
|
|
|
|
id: c.id,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-10-30 00:16:09 +01:00
|
|
|
/**
|
|
|
|
* Called when we get a local ICE candidate. Don't call this.
|
|
|
|
*
|
|
|
|
* @param {RTCIceCandidate} candidate
|
|
|
|
* @function
|
|
|
|
*/
|
|
|
|
Stream.prototype.gotLocalIce = function(candidate) {
|
|
|
|
let c = this;
|
2020-12-05 02:52:56 +01:00
|
|
|
if(c.localDescriptionSent)
|
2020-10-30 00:16:09 +01:00
|
|
|
c.sc.send({type: 'ice',
|
|
|
|
id: c.id,
|
|
|
|
candidate: candidate,
|
|
|
|
});
|
|
|
|
else
|
|
|
|
c.localIceCandidates.push(candidate);
|
|
|
|
}
|
|
|
|
|
2020-08-11 17:09:31 +02:00
|
|
|
/**
|
2020-12-05 02:52:56 +01:00
|
|
|
* flushLocalIceCandidates flushes any buffered local ICE candidates.
|
|
|
|
* It is called when we send an offer.
|
2020-08-14 14:44:23 +02:00
|
|
|
* @function
|
2020-08-11 17:09:31 +02:00
|
|
|
*/
|
2020-12-05 02:52:56 +01:00
|
|
|
Stream.prototype.flushLocalIceCandidates = function () {
|
2020-10-30 00:16:09 +01:00
|
|
|
let c = this;
|
2020-12-05 02:52:56 +01:00
|
|
|
let candidates = c.localIceCandidates;
|
|
|
|
c.localIceCandidates = [];
|
|
|
|
candidates.forEach(candidate => {
|
2020-10-30 00:16:09 +01:00
|
|
|
try {
|
|
|
|
c.sc.send({type: 'ice',
|
|
|
|
id: c.id,
|
|
|
|
candidate: candidate,
|
|
|
|
});
|
|
|
|
} catch(e) {
|
|
|
|
console.warn(e);
|
|
|
|
}
|
|
|
|
});
|
2020-12-05 02:52:56 +01:00
|
|
|
c.localIceCandidates = [];
|
|
|
|
}
|
2020-10-30 00:16:09 +01:00
|
|
|
|
2020-12-05 02:52:56 +01:00
|
|
|
/**
|
|
|
|
* flushRemoteIceCandidates flushes any buffered remote ICE candidates. It is
|
|
|
|
* called automatically when we get a remote description.
|
|
|
|
* @function
|
|
|
|
*/
|
|
|
|
Stream.prototype.flushRemoteIceCandidates = async function () {
|
|
|
|
let c = this;
|
|
|
|
let candidates = c.remoteIceCandidates;
|
|
|
|
c.remoteIceCandidates = [];
|
2020-12-01 18:18:59 +01:00
|
|
|
/** @type {Array.<Promise<void>>} */
|
2020-08-11 17:09:31 +02:00
|
|
|
let promises = [];
|
2020-12-05 02:52:56 +01:00
|
|
|
candidates.forEach(candidate => {
|
|
|
|
promises.push(c.pc.addIceCandidate(candidate).catch(console.warn));
|
2020-08-11 17:09:31 +02:00
|
|
|
});
|
|
|
|
return await Promise.all(promises);
|
2020-09-14 15:48:17 +02:00
|
|
|
};
|
2020-08-11 17:09:31 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* negotiate negotiates or renegotiates an up stream. It is called
|
|
|
|
* automatically when required. If the client requires renegotiation, it
|
2020-09-20 14:33:13 +02:00
|
|
|
* is probably better to call restartIce which will cause negotiate to be
|
|
|
|
* called asynchronously.
|
2020-09-03 17:12:36 +02:00
|
|
|
*
|
2020-08-14 14:44:23 +02:00
|
|
|
* @function
|
2020-09-20 14:33:13 +02:00
|
|
|
* @param {boolean} [restartIce] - Whether to restart ICE.
|
2020-08-11 17:09:31 +02:00
|
|
|
*/
|
2020-09-03 17:12:36 +02:00
|
|
|
Stream.prototype.negotiate = async function (restartIce) {
|
2020-08-11 17:09:31 +02:00
|
|
|
let c = this;
|
2020-12-05 20:27:32 +01:00
|
|
|
if(!c.up)
|
|
|
|
throw new Error('not an up stream');
|
2020-08-11 17:09:31 +02:00
|
|
|
|
2020-09-20 14:33:13 +02:00
|
|
|
let options = {};
|
2020-09-03 17:12:36 +02:00
|
|
|
if(restartIce)
|
|
|
|
options = {iceRestart: true};
|
|
|
|
let offer = await c.pc.createOffer(options);
|
2020-08-11 17:09:31 +02:00
|
|
|
if(!offer)
|
|
|
|
throw(new Error("Didn't create offer"));
|
|
|
|
await c.pc.setLocalDescription(offer);
|
|
|
|
|
|
|
|
// mids are not known until this point
|
|
|
|
c.pc.getTransceivers().forEach(t => {
|
|
|
|
if(t.sender && t.sender.track) {
|
|
|
|
let label = c.labels[t.sender.track.id];
|
|
|
|
if(label)
|
|
|
|
c.labelsByMid[t.mid] = label;
|
|
|
|
else
|
|
|
|
console.warn("Couldn't find label for track");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
c.sc.send({
|
|
|
|
type: 'offer',
|
2020-12-05 02:52:56 +01:00
|
|
|
kind: this.localDescriptionSent ? 'renegotiate' : '',
|
2020-08-11 17:09:31 +02:00
|
|
|
id: c.id,
|
|
|
|
labels: c.labelsByMid,
|
|
|
|
offer: offer,
|
|
|
|
});
|
2020-12-05 02:52:56 +01:00
|
|
|
this.localDescriptionSent = true;
|
|
|
|
c.flushLocalIceCandidates();
|
2020-09-14 15:48:17 +02:00
|
|
|
};
|
2020-08-11 17:09:31 +02:00
|
|
|
|
2020-09-03 17:12:36 +02:00
|
|
|
/**
|
2020-12-05 20:44:12 +01:00
|
|
|
* restartIce causes an ICE restart on a stream. For up streams, it is
|
|
|
|
* called automatically when ICE signals that the connection has failed,
|
|
|
|
* but may also be called by the application. For down streams, it
|
|
|
|
* requests that the server perform an ICE restart. In either case,
|
|
|
|
* it returns immediately, negotiation will happen asynchronously.
|
2020-09-03 17:12:36 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
Stream.prototype.restartIce = function () {
|
|
|
|
let c = this;
|
2020-12-05 20:44:12 +01:00
|
|
|
if(!c.up) {
|
|
|
|
c.sc.send({
|
|
|
|
type: 'renegotiate',
|
|
|
|
id: c.id,
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
2020-09-03 17:12:36 +02:00
|
|
|
|
2020-09-20 14:33:13 +02:00
|
|
|
if('restartIce' in c.pc) {
|
2020-09-03 17:12:36 +02:00
|
|
|
try {
|
|
|
|
/** @ts-ignore */
|
|
|
|
c.pc.restartIce();
|
|
|
|
return;
|
|
|
|
} catch(e) {
|
|
|
|
console.warn(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// negotiate is async, but this returns immediately.
|
|
|
|
c.negotiate(true);
|
2020-09-14 15:48:17 +02:00
|
|
|
};
|
2020-09-03 17:12:36 +02:00
|
|
|
|
2020-08-11 17:09:31 +02:00
|
|
|
/**
|
|
|
|
* updateStats is called periodically, if requested by setStatsInterval,
|
|
|
|
* in order to recompute stream statistics and invoke the onstats handler.
|
|
|
|
*
|
2020-08-14 14:44:23 +02:00
|
|
|
* @function
|
2020-08-11 17:09:31 +02:00
|
|
|
*/
|
|
|
|
Stream.prototype.updateStats = async function() {
|
|
|
|
let c = this;
|
|
|
|
let old = c.stats;
|
2020-09-28 20:58:07 +02:00
|
|
|
/** @type{Object<string,unknown>} */
|
2020-08-11 17:09:31 +02:00
|
|
|
let stats = {};
|
|
|
|
|
|
|
|
let transceivers = c.pc.getTransceivers();
|
|
|
|
for(let i = 0; i < transceivers.length; i++) {
|
|
|
|
let t = transceivers[i];
|
2020-09-11 23:20:46 +02:00
|
|
|
let stid = t.sender.track && t.sender.track.id;
|
|
|
|
let rtid = t.receiver.track && t.receiver.track.id;
|
2020-08-11 17:09:31 +02:00
|
|
|
|
2020-09-11 23:20:46 +02:00
|
|
|
let report = null;
|
|
|
|
if(stid) {
|
|
|
|
try {
|
|
|
|
report = await t.sender.getStats();
|
|
|
|
} catch(e) {
|
|
|
|
}
|
2020-08-11 17:09:31 +02:00
|
|
|
}
|
|
|
|
|
2020-09-11 23:20:46 +02:00
|
|
|
if(report) {
|
|
|
|
for(let r of report.values()) {
|
|
|
|
if(stid && r.type === 'outbound-rtp') {
|
|
|
|
if(!('bytesSent' in r))
|
|
|
|
continue;
|
|
|
|
if(!stats[stid])
|
|
|
|
stats[stid] = {};
|
|
|
|
stats[stid][r.type] = {};
|
|
|
|
stats[stid][r.type].timestamp = r.timestamp;
|
|
|
|
stats[stid][r.type].bytesSent = r.bytesSent;
|
|
|
|
if(old[stid] && old[stid][r.type])
|
|
|
|
stats[stid][r.type].rate =
|
|
|
|
((r.bytesSent - old[stid][r.type].bytesSent) * 1000 /
|
|
|
|
(r.timestamp - old[stid][r.type].timestamp)) * 8;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-08-11 17:09:31 +02:00
|
|
|
|
2020-09-11 23:20:46 +02:00
|
|
|
report = null;
|
|
|
|
if(rtid) {
|
|
|
|
try {
|
|
|
|
report = await t.receiver.getStats();
|
|
|
|
} catch(e) {
|
|
|
|
console.error(e);
|
|
|
|
}
|
|
|
|
}
|
2020-08-11 17:09:31 +02:00
|
|
|
|
2020-09-11 23:20:46 +02:00
|
|
|
if(report) {
|
|
|
|
for(let r of report.values()) {
|
|
|
|
if(rtid && r.type === 'track') {
|
|
|
|
if(!('totalAudioEnergy' in r))
|
|
|
|
continue;
|
|
|
|
if(!stats[rtid])
|
|
|
|
stats[rtid] = {};
|
|
|
|
stats[rtid][r.type] = {};
|
|
|
|
stats[rtid][r.type].timestamp = r.timestamp;
|
|
|
|
stats[rtid][r.type].totalAudioEnergy = r.totalAudioEnergy;
|
|
|
|
if(old[rtid] && old[rtid][r.type])
|
|
|
|
stats[rtid][r.type].audioEnergy =
|
|
|
|
(r.totalAudioEnergy - old[rtid][r.type].totalAudioEnergy) * 1000 /
|
|
|
|
(r.timestamp - old[rtid][r.type].timestamp);
|
|
|
|
}
|
2020-08-11 17:09:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
c.stats = stats;
|
|
|
|
|
|
|
|
if(c.onstats)
|
|
|
|
c.onstats.call(c, c.stats);
|
2020-09-14 15:48:17 +02:00
|
|
|
};
|
2020-08-11 17:09:31 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* setStatsInterval sets the interval in milliseconds at which the onstats
|
|
|
|
* handler will be called. This is only useful for up streams.
|
|
|
|
*
|
2020-09-20 14:33:13 +02:00
|
|
|
* @param {number} ms - The interval in milliseconds.
|
2020-08-11 17:09:31 +02:00
|
|
|
*/
|
|
|
|
Stream.prototype.setStatsInterval = function(ms) {
|
|
|
|
let c = this;
|
|
|
|
if(c.statsHandler) {
|
|
|
|
clearInterval(c.statsHandler);
|
|
|
|
c.statsHandler = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(ms <= 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
c.statsHandler = setInterval(() => {
|
|
|
|
c.updateStats();
|
|
|
|
}, ms);
|
2020-09-14 15:48:17 +02:00
|
|
|
};
|