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.
|
2022-03-23 00:08:16 +01:00
|
|
|
*
|
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), '');
|
|
|
|
}
|
|
|
|
|
2021-01-31 19:00:09 +01:00
|
|
|
/**
|
|
|
|
* newRandomId returns a random string of 32 hex digits (16 bytes).
|
|
|
|
*
|
2020-08-11 17:09:31 +02:00
|
|
|
* @returns {string}
|
|
|
|
*/
|
2021-01-31 19:00:09 +01:00
|
|
|
function newRandomId() {
|
2020-08-11 17:09:31 +02:00
|
|
|
let a = new Uint8Array(16);
|
|
|
|
crypto.getRandomValues(a);
|
|
|
|
return toHex(a);
|
|
|
|
}
|
|
|
|
|
2021-01-31 19:00:09 +01:00
|
|
|
let localIdCounter = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* newLocalId returns a string that is unique in this session.
|
|
|
|
*
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
function newLocalId() {
|
|
|
|
let id = `${localIdCounter}`
|
|
|
|
localIdCounter++;
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
2021-04-27 18:58:21 +02:00
|
|
|
/**
|
|
|
|
* @typedef {Object} user
|
|
|
|
* @property {string} username
|
2022-02-19 23:43:44 +01:00
|
|
|
* @property {Array<string>} permissions
|
2022-01-29 22:54:44 +01:00
|
|
|
* @property {Object<string,any>} data
|
2022-03-25 17:06:13 +01:00
|
|
|
* @property {Object<string,Object<string,boolean>>} streams
|
2021-04-27 18:58:21 +02:00
|
|
|
*/
|
|
|
|
|
2020-08-11 17:09:31 +02:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
*/
|
2021-01-31 19:00:09 +01:00
|
|
|
this.id = newRandomId();
|
2020-08-11 17:09:31 +02:00
|
|
|
/**
|
2021-01-03 12:04:39 +01:00
|
|
|
* The group that we have joined, or null 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;
|
2021-01-03 12:04:39 +01:00
|
|
|
/**
|
|
|
|
* The username we joined as.
|
2021-04-27 18:58:21 +02:00
|
|
|
*
|
|
|
|
* @type {string}
|
2021-01-03 12:04:39 +01:00
|
|
|
*/
|
|
|
|
this.username = null;
|
2021-04-27 18:58:21 +02:00
|
|
|
/**
|
|
|
|
* The set of users in this group, including ourself.
|
|
|
|
*
|
|
|
|
* @type {Object<string,user>}
|
|
|
|
*/
|
|
|
|
this.users = {};
|
2020-08-11 17:09:31 +02:00
|
|
|
/**
|
|
|
|
* The underlying websocket.
|
2020-09-20 14:33:13 +02:00
|
|
|
*
|
2020-08-11 17:09:31 +02:00
|
|
|
* @type {WebSocket}
|
|
|
|
*/
|
|
|
|
this.socket = null;
|
2022-12-16 17:54:46 +01:00
|
|
|
/**
|
|
|
|
* The negotiated protocol version.
|
|
|
|
*
|
|
|
|
* @type {string}
|
|
|
|
*/
|
|
|
|
this.version = null;
|
2020-08-11 17:09:31 +02:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
*
|
2020-12-28 02:25:46 +01:00
|
|
|
* @type {RTCConfiguration}
|
2020-08-11 17:09:31 +02:00
|
|
|
*/
|
2020-12-28 02:25:46 +01:00
|
|
|
this.rtcConfiguration = null;
|
2020-08-11 17:09:31 +02:00
|
|
|
/**
|
|
|
|
* The permissions granted to this connection.
|
2020-09-20 14:33:13 +02:00
|
|
|
*
|
2022-02-19 23:43:44 +01:00
|
|
|
* @type {Array<string>}
|
2020-08-11 17:09:31 +02:00
|
|
|
*/
|
2022-02-19 23:43:44 +01: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
|
|
|
*
|
2024-06-10 19:45:23 +02:00
|
|
|
* @type {Object<unknown,unknown>}
|
2020-09-11 21:39:18 +02:00
|
|
|
*/
|
|
|
|
this.userdata = {};
|
2024-06-10 19:45:23 +02:00
|
|
|
/**
|
|
|
|
* The time at which we last received a message from the server.
|
|
|
|
*
|
|
|
|
* @type {number}
|
|
|
|
*/
|
|
|
|
this.lastServerMessage = null;
|
|
|
|
/**
|
|
|
|
* The interval handler which checks for liveness.
|
|
|
|
*
|
|
|
|
* @type {number}
|
|
|
|
*/
|
|
|
|
this.pingHandler = null;
|
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;
|
2024-06-10 19:41:23 +02:00
|
|
|
/**
|
|
|
|
* onerror is called whenever a fatal error occurs. The stream will
|
|
|
|
* then be closed, and onclose called normally.
|
|
|
|
*
|
|
|
|
* @type{(this: ServerConnection, error: unknown) => void}
|
|
|
|
*/
|
|
|
|
this.onerror = null;
|
2020-08-11 17:09:31 +02:00
|
|
|
/**
|
|
|
|
* 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;
|
2022-03-22 18:22:18 +01:00
|
|
|
/**
|
|
|
|
* onpeerconnection is called before we establish a new peer connection.
|
|
|
|
* It may either return null, or a new RTCConfiguration that overrides
|
|
|
|
* the value obtained from the server.
|
|
|
|
*
|
2022-03-23 00:11:12 +01:00
|
|
|
* @type{(this: ServerConnection) => RTCConfiguration}
|
2022-03-22 18:22:18 +01:00
|
|
|
*/
|
|
|
|
this.onpeerconnection = null;
|
2020-08-11 17:09:31 +02:00
|
|
|
/**
|
2021-04-27 18:58:21 +02:00
|
|
|
* onuser is called whenever a user in the group changes. The users
|
|
|
|
* array has already been updated.
|
2020-09-20 14:33:13 +02:00
|
|
|
*
|
2021-04-27 18:58:21 +02:00
|
|
|
* @type{(this: ServerConnection, id: string, kind: 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'.
|
|
|
|
*
|
2023-04-01 13:28:24 +02:00
|
|
|
* @type{(this: ServerConnection, kind: string, group: string, permissions: Array<string>, status: Object<string,any>, data: Object<string,any>, error: string, 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
|
|
|
*
|
2024-08-11 19:00:17 +02:00
|
|
|
* @type {(this: ServerConnection, id: string, source: string, dest: string, username: string, time: Date, privileged: boolean, history: boolean, kind: string, message: string) => 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.
|
|
|
|
*
|
2023-03-31 21:04:19 +02:00
|
|
|
* @type {(this: ServerConnection, id: string, dest: string, username: string, time: Date, privileged: boolean, kind: string, error: string, message: unknown) => void}
|
2020-11-30 14:06:14 +01:00
|
|
|
*/
|
|
|
|
this.onusermessage = null;
|
2022-07-23 20:52:04 +02:00
|
|
|
/**
|
2022-08-04 19:14:35 +02:00
|
|
|
* The set of files currently being transferred.
|
|
|
|
*
|
2022-07-23 20:52:04 +02:00
|
|
|
* @type {Object<string,TransferredFile>}
|
|
|
|
*/
|
|
|
|
this.transferredFiles = {};
|
|
|
|
/**
|
2022-08-04 19:14:35 +02:00
|
|
|
* onfiletransfer is called whenever a peer offers a file transfer.
|
|
|
|
*
|
|
|
|
* If the transfer is accepted, it should set up the file transfer
|
|
|
|
* callbacks and return immediately. It may also throw an exception
|
|
|
|
* in order to reject the file transfer.
|
|
|
|
*
|
2022-07-23 20:52:04 +02:00
|
|
|
* @type {(this: ServerConnection, f: TransferredFile) => void}
|
|
|
|
*/
|
|
|
|
this.onfiletransfer = null;
|
2020-08-11 17:09:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @typedef {Object} message
|
|
|
|
* @property {string} type
|
2022-09-02 14:30:51 +02:00
|
|
|
* @property {Array<string>} [version]
|
2020-08-11 17:09:31 +02:00
|
|
|
* @property {string} [kind]
|
2022-12-16 17:54:46 +01:00
|
|
|
* @property {string} [error]
|
2020-08-11 17:09:31 +02:00
|
|
|
* @property {string} [id]
|
2021-01-31 19:00:09 +01:00
|
|
|
* @property {string} [replace]
|
2021-01-03 12:04:39 +01:00
|
|
|
* @property {string} [source]
|
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]
|
2021-10-29 23:37:05 +02:00
|
|
|
* @property {string} [token]
|
2020-12-14 10:35:12 +01:00
|
|
|
* @property {boolean} [privileged]
|
2022-02-19 23:43:44 +01:00
|
|
|
* @property {Array<string>} [permissions]
|
2021-04-28 14:45:45 +02:00
|
|
|
* @property {Object<string,any>} [status]
|
2022-01-29 22:54:44 +01:00
|
|
|
* @property {Object<string,any>} [data]
|
2020-08-11 17:09:31 +02:00
|
|
|
* @property {string} [group]
|
2020-12-28 01:42:26 +01:00
|
|
|
* @property {unknown} [value]
|
2021-01-11 16:22:04 +01:00
|
|
|
* @property {boolean} [noecho]
|
2022-12-16 17:54:46 +01:00
|
|
|
* @property {string|number} [time]
|
2021-01-03 17:00:58 +01:00
|
|
|
* @property {string} [sdp]
|
2020-08-11 17:09:31 +02:00
|
|
|
* @property {RTCIceCandidate} [candidate]
|
2021-04-28 17:00:50 +02:00
|
|
|
* @property {string} [label]
|
2021-05-14 18:39:39 +02:00
|
|
|
* @property {Object<string,Array<string>>|Array<string>} [request]
|
2020-12-28 02:25:46 +01:00
|
|
|
* @property {Object<string,any>} [rtcConfiguration]
|
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;
|
2020-12-26 17:28:44 +01:00
|
|
|
};
|
2020-08-11 17:09:31 +02:00
|
|
|
|
2024-06-10 19:41:23 +02:00
|
|
|
/**
|
|
|
|
* error forcibly closes a server connection and invokes the onerror
|
|
|
|
* callback. The onclose callback will be invoked when the connection
|
|
|
|
* is effectively closed.
|
|
|
|
*
|
|
|
|
* @param {any} e
|
|
|
|
*/
|
|
|
|
ServerConnection.prototype.error = function(e) {
|
|
|
|
if(this.onerror)
|
|
|
|
this.onerror.call(this, e);
|
|
|
|
this.close();
|
|
|
|
};
|
|
|
|
|
2020-08-11 17:09:31 +02:00
|
|
|
/**
|
|
|
|
* 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-12-26 17:28:44 +01:00
|
|
|
};
|
2020-08-11 17:09:31 +02:00
|
|
|
|
|
|
|
/**
|
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.
|
2020-09-18 11:28:13 +02:00
|
|
|
* @function
|
2020-08-11 17:09:31 +02:00
|
|
|
*/
|
2024-06-10 19:41:23 +02:00
|
|
|
ServerConnection.prototype.connect = function(url) {
|
2020-08-11 17:09:31 +02:00
|
|
|
let sc = this;
|
2024-06-10 19:41:23 +02:00
|
|
|
if(sc.socket)
|
|
|
|
throw new Error("Attempting to connect stale connection");
|
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
|
|
|
|
2024-06-10 19:45:23 +02:00
|
|
|
this.pingHandler = setInterval(e => {
|
|
|
|
if(!sc.lastServerMessage) {
|
|
|
|
sc.error(new Error('Timeout'));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let d = new Date().valueOf() - sc.lastServerMessage;
|
|
|
|
if(d > 65000) {
|
|
|
|
sc.error(new Error('Timeout'));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(sc.version && d >= 15000)
|
|
|
|
sc.send({type: 'ping'});
|
|
|
|
}, 10000);
|
|
|
|
|
2024-06-10 19:41:23 +02:00
|
|
|
this.socket.onerror = function(e) {
|
|
|
|
if(sc.onerror)
|
|
|
|
sc.onerror.call(sc, new Error('Socket error: ' + e));
|
|
|
|
};
|
|
|
|
this.socket.onopen = function(e) {
|
|
|
|
try {
|
2020-12-01 22:42:06 +01:00
|
|
|
sc.send({
|
|
|
|
type: 'handshake',
|
2023-08-28 23:07:01 +02:00
|
|
|
version: ['2'],
|
2020-12-01 22:42:06 +01:00
|
|
|
id: sc.id,
|
|
|
|
});
|
2024-06-10 19:41:23 +02:00
|
|
|
} catch(e) {
|
|
|
|
sc.error(e);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
this.socket.onclose = function(e) {
|
|
|
|
sc.permissions = [];
|
|
|
|
for(let id in sc.up) {
|
|
|
|
let c = sc.up[id];
|
|
|
|
c.close();
|
|
|
|
}
|
|
|
|
for(let id in sc.down) {
|
|
|
|
let c = sc.down[id];
|
|
|
|
c.close();
|
|
|
|
}
|
|
|
|
for(let id in sc.users) {
|
|
|
|
delete(sc.users[id]);
|
|
|
|
if(sc.onuser)
|
|
|
|
sc.onuser.call(sc, id, 'delete');
|
|
|
|
}
|
|
|
|
if(sc.group && sc.onjoined)
|
|
|
|
sc.onjoined.call(sc, 'leave', sc.group, [], {}, {}, '', '');
|
|
|
|
sc.group = null;
|
|
|
|
sc.username = null;
|
2024-06-10 19:45:23 +02:00
|
|
|
if(sc.pingHandler) {
|
|
|
|
clearInterval(sc.pingHandler);
|
|
|
|
sc.pingHandler = null;
|
|
|
|
}
|
2024-06-10 19:41:23 +02:00
|
|
|
if(sc.onclose)
|
|
|
|
sc.onclose.call(sc, e.code, e.reason);
|
|
|
|
};
|
|
|
|
this.socket.onmessage = function(e) {
|
|
|
|
let m;
|
|
|
|
try {
|
|
|
|
m = JSON.parse(e.data);
|
|
|
|
} catch(e) {
|
|
|
|
sc.error(e);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(m.type !== 'handshake' && !sc.version) {
|
|
|
|
sc.error(new Error("Server didn't send handshake"));
|
|
|
|
return;
|
|
|
|
}
|
2024-06-10 19:45:23 +02:00
|
|
|
sc.lastServerMessage = new Date().valueOf();
|
2024-06-10 19:41:23 +02:00
|
|
|
switch(m.type) {
|
|
|
|
case 'handshake': {
|
|
|
|
if((m.version instanceof Array) && m.version.includes('2')) {
|
|
|
|
sc.version = '2';
|
|
|
|
} else {
|
|
|
|
sc.version = null;
|
|
|
|
sc.error(new Error(`Unknown protocol version ${m.version}`));
|
|
|
|
return;
|
|
|
|
}
|
2020-08-11 17:09:31 +02:00
|
|
|
if(sc.onconnected)
|
|
|
|
sc.onconnected.call(sc);
|
2024-06-10 19:41:23 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'offer':
|
|
|
|
sc.gotOffer(m.id, m.label, m.source, m.username,
|
|
|
|
m.sdp, m.replace);
|
|
|
|
break;
|
|
|
|
case 'answer':
|
|
|
|
sc.gotAnswer(m.id, m.sdp);
|
|
|
|
break;
|
|
|
|
case 'renegotiate':
|
|
|
|
sc.gotRenegotiate(m.id);
|
|
|
|
break;
|
|
|
|
case 'close':
|
|
|
|
sc.gotClose(m.id);
|
|
|
|
break;
|
|
|
|
case 'abort':
|
|
|
|
sc.gotAbort(m.id);
|
|
|
|
break;
|
|
|
|
case 'ice':
|
|
|
|
sc.gotRemoteIce(m.id, m.candidate);
|
|
|
|
break;
|
|
|
|
case 'joined':
|
|
|
|
if(m.kind === 'leave' || m.kind === 'fail') {
|
|
|
|
for(let id in sc.users) {
|
|
|
|
delete(sc.users[id]);
|
|
|
|
if(sc.onuser)
|
|
|
|
sc.onuser.call(sc, id, 'delete');
|
2022-12-16 17:54:46 +01:00
|
|
|
}
|
2024-06-10 19:41:23 +02:00
|
|
|
sc.username = null;
|
|
|
|
sc.permissions = [];
|
|
|
|
sc.rtcConfiguration = null;
|
|
|
|
} else if(m.kind === 'join' || m.kind == 'change') {
|
|
|
|
if(m.kind === 'join' && sc.group) {
|
|
|
|
throw new Error('Joined multiple groups');
|
|
|
|
} else if(m.kind === 'change' && m.group != sc.group) {
|
|
|
|
console.warn('join(change) for inconsistent group');
|
|
|
|
break;
|
2021-04-27 18:58:21 +02:00
|
|
|
}
|
2024-06-10 19:41:23 +02:00
|
|
|
sc.group = m.group;
|
|
|
|
sc.username = m.username;
|
|
|
|
sc.permissions = m.permissions || [];
|
|
|
|
sc.rtcConfiguration = m.rtcConfiguration || null;
|
|
|
|
}
|
|
|
|
if(sc.onjoined)
|
|
|
|
sc.onjoined.call(sc, m.kind, m.group,
|
|
|
|
m.permissions || [],
|
|
|
|
m.status, m.data,
|
|
|
|
m.error || null, m.value || null);
|
|
|
|
break;
|
|
|
|
case 'user':
|
|
|
|
switch(m.kind) {
|
|
|
|
case 'add':
|
|
|
|
if(m.id in sc.users)
|
|
|
|
console.warn(`Duplicate user ${m.id} ${m.username}`);
|
|
|
|
sc.users[m.id] = {
|
|
|
|
username: m.username,
|
|
|
|
permissions: m.permissions || [],
|
|
|
|
data: m.data || {},
|
|
|
|
streams: {},
|
|
|
|
};
|
2020-08-11 17:09:31 +02:00
|
|
|
break;
|
2024-06-10 19:41:23 +02:00
|
|
|
case 'change':
|
|
|
|
if(!(m.id in sc.users)) {
|
|
|
|
console.warn(`Unknown user ${m.id} ${m.username}`);
|
2021-04-27 18:58:21 +02:00
|
|
|
sc.users[m.id] = {
|
|
|
|
username: m.username,
|
2022-02-19 23:43:44 +01:00
|
|
|
permissions: m.permissions || [],
|
2022-01-29 22:54:44 +01:00
|
|
|
data: m.data || {},
|
2022-03-25 17:06:13 +01:00
|
|
|
streams: {},
|
2021-04-27 18:58:21 +02:00
|
|
|
};
|
2024-06-10 19:41:23 +02:00
|
|
|
} else {
|
|
|
|
sc.users[m.id].username = m.username;
|
|
|
|
sc.users[m.id].permissions = m.permissions || [];
|
|
|
|
sc.users[m.id].data = m.data || {};
|
2021-04-27 18:58:21 +02:00
|
|
|
}
|
2020-08-11 17:09:31 +02:00
|
|
|
break;
|
2024-06-10 19:41:23 +02:00
|
|
|
case 'delete':
|
|
|
|
if(!(m.id in sc.users))
|
|
|
|
console.warn(`Unknown user ${m.id} ${m.username}`);
|
|
|
|
for(let t in sc.transferredFiles) {
|
|
|
|
let f = sc.transferredFiles[t];
|
|
|
|
if(f.userid === m.id)
|
|
|
|
f.fail('user has gone away');
|
|
|
|
}
|
|
|
|
delete(sc.users[m.id]);
|
2020-08-11 17:09:31 +02:00
|
|
|
break;
|
|
|
|
default:
|
2024-06-10 19:41:23 +02:00
|
|
|
console.warn(`Unknown user action ${m.kind}`);
|
2020-08-11 17:09:31 +02:00
|
|
|
return;
|
|
|
|
}
|
2024-06-10 19:41:23 +02:00
|
|
|
if(sc.onuser)
|
|
|
|
sc.onuser.call(sc, m.id, m.kind);
|
|
|
|
break;
|
|
|
|
case 'chat':
|
|
|
|
case 'chathistory':
|
|
|
|
if(sc.onchat)
|
|
|
|
sc.onchat.call(
|
2024-08-11 19:00:17 +02:00
|
|
|
sc, m.id, m.source, m.dest, m.username, parseTime(m.time),
|
2024-06-10 19:41:23 +02:00
|
|
|
m.privileged, m.type === 'chathistory', m.kind,
|
|
|
|
'' + m.value,
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
case 'usermessage':
|
|
|
|
if(m.kind === 'filetransfer')
|
|
|
|
sc.fileTransfer(m.source, m.username, m.value);
|
|
|
|
else if(sc.onusermessage)
|
|
|
|
sc.onusermessage.call(
|
|
|
|
sc, m.source, m.dest, m.username, parseTime(m.time),
|
|
|
|
m.privileged, m.kind, m.error, m.value,
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
case 'ping':
|
|
|
|
sc.send({
|
|
|
|
type: 'pong',
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
case 'pong':
|
|
|
|
/* nothing */
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
console.warn('Unexpected server message', m.type);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
};
|
2020-12-26 17:28:44 +01:00
|
|
|
};
|
2020-08-11 17:09:31 +02:00
|
|
|
|
2022-12-16 17:54:46 +01:00
|
|
|
/**
|
|
|
|
* Protocol version 1 uses integers for dates, later versions use dates in
|
|
|
|
* ISO 8601 format. This function takes a date in either format and
|
|
|
|
* returns a Date object.
|
|
|
|
*
|
|
|
|
* @param {string|number} value
|
|
|
|
* @returns {Date}
|
|
|
|
*/
|
|
|
|
function parseTime(value) {
|
|
|
|
if(!value)
|
|
|
|
return null;
|
|
|
|
try {
|
|
|
|
return new Date(value);
|
|
|
|
} catch(e) {
|
|
|
|
console.warn(`Couldn't parse ${value}:`, e);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-11 17:09:31 +02:00
|
|
|
/**
|
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.
|
2021-10-29 23:37:05 +02:00
|
|
|
* @param {string|Object} credentials - password or authServer.
|
2022-02-05 13:45:32 +01:00
|
|
|
* @param {Object<string,any>} [data] - the initial associated data.
|
2020-08-11 17:09:31 +02:00
|
|
|
*/
|
2021-10-29 23:37:05 +02:00
|
|
|
ServerConnection.prototype.join = async function(group, username, credentials, data) {
|
2022-01-25 18:16:42 +01:00
|
|
|
let m = {
|
2020-12-01 22:42:06 +01:00
|
|
|
type: 'join',
|
|
|
|
kind: 'join',
|
|
|
|
group: group,
|
2022-01-25 18:16:42 +01:00
|
|
|
};
|
2023-04-01 13:28:24 +02:00
|
|
|
if(typeof username !== 'undefined' && username !== null)
|
|
|
|
m.username = username;
|
|
|
|
|
2021-10-29 23:37:05 +02:00
|
|
|
if((typeof credentials) === 'string') {
|
|
|
|
m.password = credentials;
|
|
|
|
} else {
|
|
|
|
switch(credentials.type) {
|
|
|
|
case 'password':
|
|
|
|
m.password = credentials.password;
|
|
|
|
break;
|
|
|
|
case 'token':
|
|
|
|
m.token = credentials.token;
|
|
|
|
break;
|
|
|
|
case 'authServer':
|
|
|
|
let r = await fetch(credentials.authServer, {
|
|
|
|
method: "POST",
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
|
|
|
body: JSON.stringify({
|
|
|
|
location: credentials.location,
|
|
|
|
username: username,
|
|
|
|
password: credentials.password,
|
|
|
|
}),
|
|
|
|
});
|
|
|
|
if(!r.ok)
|
|
|
|
throw new Error(
|
2022-08-03 15:00:50 +02:00
|
|
|
`The authorisation server said ${r.status} ${r.statusText}`,
|
2021-10-29 23:37:05 +02:00
|
|
|
);
|
2022-08-03 15:00:50 +02:00
|
|
|
if(r.status === 204) {
|
|
|
|
// no data, fallback to password auth
|
2022-08-02 18:12:17 +02:00
|
|
|
m.password = credentials.password;
|
2022-08-03 15:00:50 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
let ctype = r.headers.get("Content-Type");
|
|
|
|
if(!ctype)
|
|
|
|
throw new Error(
|
|
|
|
"The authorisation server didn't return a content type",
|
|
|
|
);
|
|
|
|
let semi = ctype.indexOf(";");
|
|
|
|
if(semi >= 0)
|
|
|
|
ctype = ctype.slice(0, semi);
|
|
|
|
ctype = ctype.trim();
|
|
|
|
switch(ctype.toLowerCase()) {
|
|
|
|
case 'application/jwt':
|
|
|
|
let data = await r.text();
|
|
|
|
if(!data)
|
|
|
|
throw new Error(
|
|
|
|
"The authorisation server returned empty token",
|
|
|
|
);
|
2022-08-02 18:12:17 +02:00
|
|
|
m.token = data;
|
2022-08-03 15:00:50 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new Error(`The authorisation server returned ${ctype}`);
|
|
|
|
break;
|
|
|
|
}
|
2021-10-29 23:37:05 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new Error(`Unknown credentials type ${credentials.type}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-05 13:45:32 +01:00
|
|
|
if(data)
|
|
|
|
m.data = data;
|
2021-10-29 23:37:05 +02:00
|
|
|
|
2022-01-25 18:16:42 +01:00
|
|
|
this.send(m);
|
2020-12-26 17:28:44 +01: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-12-26 17:28:44 +01:00
|
|
|
};
|
2020-08-11 17:09:31 +02:00
|
|
|
|
|
|
|
/**
|
2021-05-14 18:39:39 +02:00
|
|
|
* request sets the list of requested tracks
|
2020-08-11 17:09:31 +02:00
|
|
|
*
|
2021-04-28 17:00:50 +02:00
|
|
|
* @param {Object<string,Array<string>>} what
|
2021-05-14 18:39:39 +02:00
|
|
|
* - A dictionary that maps labels to a sequence of 'audio', 'video'
|
|
|
|
* or 'video-low. An entry with an empty label '' provides the default.
|
2020-08-11 17:09:31 +02:00
|
|
|
*/
|
|
|
|
ServerConnection.prototype.request = function(what) {
|
|
|
|
this.send({
|
|
|
|
type: 'request',
|
2021-04-28 17:00:50 +02:00
|
|
|
request: what,
|
2020-08-11 17:09:31 +02:00
|
|
|
});
|
2020-09-14 15:48:17 +02:00
|
|
|
};
|
2020-08-11 17:09:31 +02:00
|
|
|
|
2021-01-31 19:00:09 +01:00
|
|
|
/**
|
2022-03-23 00:08:16 +01:00
|
|
|
* findByLocalId finds an active connection with the given localId.
|
|
|
|
* It returns null if none was find.
|
|
|
|
*
|
2021-01-31 19:00:09 +01:00
|
|
|
* @param {string} localId
|
|
|
|
* @returns {Stream}
|
|
|
|
*/
|
|
|
|
ServerConnection.prototype.findByLocalId = function(localId) {
|
|
|
|
if(!localId)
|
|
|
|
return null;
|
|
|
|
|
2021-04-24 14:51:14 +02:00
|
|
|
let sc = this;
|
|
|
|
|
|
|
|
for(let id in sc.up) {
|
|
|
|
let s = sc.up[id];
|
|
|
|
if(s.localId === localId)
|
2021-01-31 19:00:09 +01:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2022-03-22 18:22:18 +01:00
|
|
|
/**
|
2022-03-23 00:08:16 +01:00
|
|
|
* getRTCConfiguration returns the RTCConfiguration that should be used
|
|
|
|
* with this peer connection. This usually comes from the server, but may
|
|
|
|
* be overridden by the onpeerconnection callback.
|
|
|
|
*
|
2022-03-22 18:22:18 +01:00
|
|
|
* @returns {RTCConfiguration}
|
|
|
|
*/
|
2022-03-23 00:11:12 +01:00
|
|
|
ServerConnection.prototype.getRTCConfiguration = function() {
|
2022-03-22 18:22:18 +01:00
|
|
|
if(this.onpeerconnection) {
|
2022-03-23 00:11:12 +01:00
|
|
|
let conf = this.onpeerconnection.call(this);
|
2022-03-22 18:22:18 +01:00
|
|
|
if(conf !== null)
|
|
|
|
return conf;
|
|
|
|
}
|
|
|
|
return this.rtcConfiguration;
|
|
|
|
}
|
|
|
|
|
2020-08-11 17:09:31 +02:00
|
|
|
/**
|
|
|
|
* newUpStream requests the creation of a new up stream.
|
|
|
|
*
|
2021-01-31 19:00:09 +01:00
|
|
|
* @param {string} [localId]
|
|
|
|
* - The local id of the stream to create. If a stream already exists with
|
|
|
|
* the same local id, it is replaced with the new stream.
|
2020-08-11 17:09:31 +02:00
|
|
|
* @returns {Stream}
|
|
|
|
*/
|
2021-01-31 19:00:09 +01:00
|
|
|
ServerConnection.prototype.newUpStream = function(localId) {
|
2020-08-11 17:09:31 +02:00
|
|
|
let sc = this;
|
2021-01-31 19:00:09 +01:00
|
|
|
let id = newRandomId();
|
|
|
|
if(sc.up[id])
|
|
|
|
throw new Error('Eek!');
|
|
|
|
|
2021-02-14 18:06:50 +01:00
|
|
|
if(typeof RTCPeerConnection === 'undefined')
|
|
|
|
throw new Error("This browser doesn't support WebRTC");
|
|
|
|
|
2022-03-22 18:22:18 +01:00
|
|
|
|
2022-03-23 00:11:12 +01:00
|
|
|
let pc = new RTCPeerConnection(sc.getRTCConfiguration());
|
2020-08-11 17:09:31 +02:00
|
|
|
if(!pc)
|
|
|
|
throw new Error("Couldn't create peer connection");
|
2021-01-14 14:56:15 +01:00
|
|
|
|
2021-01-31 19:00:09 +01:00
|
|
|
let oldId = null;
|
|
|
|
if(localId) {
|
|
|
|
let old = sc.findByLocalId(localId);
|
|
|
|
oldId = old && old.id;
|
|
|
|
if(old)
|
|
|
|
old.close(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
let c = new Stream(this, id, localId || newLocalId(), pc, true);
|
|
|
|
if(oldId)
|
|
|
|
c.replace = oldId;
|
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;
|
2020-12-26 17:28:44 +01:00
|
|
|
};
|
2020-08-11 17:09:31 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* chat sends a chat message to the server. The server will normally echo
|
|
|
|
* the message back to the client.
|
|
|
|
*
|
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
|
|
|
*/
|
2021-01-03 12:04:39 +01:00
|
|
|
ServerConnection.prototype.chat = function(kind, dest, value) {
|
2020-08-11 17:09:31 +02:00
|
|
|
this.send({
|
|
|
|
type: 'chat',
|
2021-01-03 12:04:39 +01:00
|
|
|
source: this.id,
|
2020-10-01 16:52:01 +02:00
|
|
|
dest: dest,
|
2021-01-03 12:04:39 +01:00
|
|
|
username: this.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
|
|
|
* userAction sends a request to act on a user.
|
2020-08-11 17:09:31 +02:00
|
|
|
*
|
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.
|
2021-04-28 14:45:45 +02:00
|
|
|
* @param {any} [value] - An action-dependent parameter.
|
2020-08-11 17:09:31 +02:00
|
|
|
*/
|
2021-01-03 12:04:39 +01:00
|
|
|
ServerConnection.prototype.userAction = function(kind, dest, value) {
|
2020-08-11 17:09:31 +02:00
|
|
|
this.send({
|
2020-11-30 14:06:14 +01:00
|
|
|
type: 'useraction',
|
2021-01-03 12:04:39 +01:00
|
|
|
source: this.id,
|
2020-11-30 14:06:14 +01:00
|
|
|
dest: dest,
|
2021-01-03 12:04:39 +01:00
|
|
|
username: this.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 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.
|
2021-02-26 16:22:55 +01:00
|
|
|
* @param {unknown} [value] - An optional parameter.
|
2021-01-11 16:22:04 +01:00
|
|
|
* @param {boolean} [noecho] - If set, don't echo back the message to the sender.
|
2020-11-30 14:06:14 +01:00
|
|
|
*/
|
2021-01-11 16:22:04 +01:00
|
|
|
ServerConnection.prototype.userMessage = function(kind, dest, value, noecho) {
|
2020-11-30 14:06:14 +01:00
|
|
|
this.send({
|
|
|
|
type: 'usermessage',
|
2021-01-03 12:04:39 +01:00
|
|
|
source: this.id,
|
2020-11-30 14:06:14 +01:00
|
|
|
dest: dest,
|
2021-01-03 12:04:39 +01:00
|
|
|
username: this.username,
|
2020-11-30 14:06:14 +01:00
|
|
|
kind: kind,
|
|
|
|
value: value,
|
2021-01-11 16:22:04 +01:00
|
|
|
noecho: noecho,
|
2020-11-30 14:06:14 +01:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* groupAction sends a request to act on the current group.
|
|
|
|
*
|
2020-12-05 20:21:33 +01:00
|
|
|
* @param {string} kind
|
2022-10-08 23:02:11 +02:00
|
|
|
* @param {any} [data]
|
2020-08-11 17:09:31 +02:00
|
|
|
*/
|
2022-10-08 23:02:11 +02:00
|
|
|
ServerConnection.prototype.groupAction = function(kind, data) {
|
2020-08-11 17:09:31 +02:00
|
|
|
this.send({
|
2020-11-30 14:06:14 +01:00
|
|
|
type: 'groupaction',
|
2021-01-03 12:04:39 +01:00
|
|
|
source: this.id,
|
2020-08-11 17:09:31 +02:00
|
|
|
kind: kind,
|
2021-01-03 12:04:39 +01:00
|
|
|
username: this.username,
|
2022-10-08 23:02:11 +02:00
|
|
|
value: data,
|
2020-08-11 17:09:31 +02:00
|
|
|
});
|
2020-09-14 15:48:17 +02:00
|
|
|
};
|
2020-08-11 17:09:31 +02:00
|
|
|
|
|
|
|
/**
|
2022-03-23 00:08:16 +01:00
|
|
|
* gotOffer is called when we receive an offer from the server. Don't call this.
|
2020-08-11 17:09:31 +02:00
|
|
|
*
|
|
|
|
* @param {string} id
|
2021-04-28 17:00:50 +02:00
|
|
|
* @param {string} label
|
2021-01-03 12:04:39 +01:00
|
|
|
* @param {string} source
|
|
|
|
* @param {string} username
|
2021-01-03 17:00:58 +01:00
|
|
|
* @param {string} sdp
|
2021-01-31 19:00:09 +01:00
|
|
|
* @param {string} replace
|
2020-08-14 14:44:23 +02:00
|
|
|
* @function
|
2020-08-11 17:09:31 +02:00
|
|
|
*/
|
2021-04-28 17:00:50 +02:00
|
|
|
ServerConnection.prototype.gotOffer = async function(id, label, source, username, sdp, replace) {
|
2020-08-11 17:09:31 +02:00
|
|
|
let sc = this;
|
2021-02-14 18:06:50 +01:00
|
|
|
|
|
|
|
if(sc.up[id]) {
|
|
|
|
console.error("Duplicate connection id");
|
|
|
|
sc.send({
|
|
|
|
type: 'abort',
|
|
|
|
id: id,
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
2020-12-05 20:27:32 +01:00
|
|
|
|
2021-01-31 19:00:09 +01:00
|
|
|
let oldLocalId = null;
|
|
|
|
|
|
|
|
if(replace) {
|
|
|
|
let old = sc.down[replace];
|
|
|
|
if(old) {
|
|
|
|
oldLocalId = old.localId;
|
|
|
|
old.close(true);
|
|
|
|
} else
|
|
|
|
console.error("Replacing unknown stream");
|
|
|
|
}
|
|
|
|
|
|
|
|
let c = sc.down[id];
|
|
|
|
if(c && oldLocalId)
|
|
|
|
console.error("Replacing duplicate stream");
|
|
|
|
|
2020-08-11 17:09:31 +02:00
|
|
|
if(!c) {
|
2021-02-14 18:06:50 +01:00
|
|
|
let pc;
|
|
|
|
try {
|
2022-03-23 00:11:12 +01:00
|
|
|
pc = new RTCPeerConnection(sc.getRTCConfiguration());
|
2021-02-14 18:06:50 +01:00
|
|
|
} catch(e) {
|
|
|
|
console.error(e);
|
|
|
|
sc.send({
|
|
|
|
type: 'abort',
|
|
|
|
id: id,
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
2021-01-31 19:00:09 +01:00
|
|
|
c = new Stream(this, id, oldLocalId || newLocalId(), pc, false);
|
2021-04-28 17:00:50 +02:00
|
|
|
c.label = label;
|
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) {
|
2021-09-16 17:17:36 +02:00
|
|
|
if(e.streams.length < 1) {
|
|
|
|
console.error("Got track with no stream");
|
|
|
|
return;
|
|
|
|
}
|
2021-01-04 20:37:16 +01:00
|
|
|
c.stream = e.streams[0];
|
2022-03-25 17:06:13 +01:00
|
|
|
let changed = recomputeUserStreams(sc, source);
|
2021-01-04 20:37:16 +01:00
|
|
|
if(c.ondowntrack) {
|
|
|
|
c.ondowntrack.call(
|
2021-04-28 17:00:50 +02:00
|
|
|
c, e.track, e.transceiver, e.streams[0],
|
2021-01-04 20:37:16 +01:00
|
|
|
);
|
2020-08-11 17:09:31 +02:00
|
|
|
}
|
2021-04-29 19:03:07 +02:00
|
|
|
if(changed && sc.onuser)
|
|
|
|
sc.onuser.call(sc, source, "change");
|
2020-08-11 17:09:31 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-01-03 12:04:39 +01:00
|
|
|
c.source = source;
|
|
|
|
c.username = username;
|
2020-08-11 17:09:31 +02:00
|
|
|
|
|
|
|
if(sc.ondownstream)
|
|
|
|
sc.ondownstream.call(sc, c);
|
|
|
|
|
2020-12-25 18:26:30 +01:00
|
|
|
try {
|
2021-01-03 17:00:58 +01:00
|
|
|
await c.pc.setRemoteDescription({
|
|
|
|
type: 'offer',
|
|
|
|
sdp: sdp,
|
|
|
|
});
|
2020-12-25 18:26:30 +01:00
|
|
|
|
|
|
|
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,
|
2021-01-11 20:27:39 +01:00
|
|
|
sdp: c.pc.localDescription.sdp,
|
2020-12-25 18:26:30 +01:00
|
|
|
});
|
|
|
|
} 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
|
|
|
|
|
|
|
/**
|
2022-03-23 00:08:16 +01:00
|
|
|
* gotAnswer is called when we receive an answer from the server. Don't
|
|
|
|
* call this.
|
2020-08-11 17:09:31 +02:00
|
|
|
*
|
|
|
|
* @param {string} id
|
2021-01-03 17:00:58 +01:00
|
|
|
* @param {string} sdp
|
2020-08-14 14:44:23 +02:00
|
|
|
* @function
|
2020-08-11 17:09:31 +02:00
|
|
|
*/
|
2021-01-03 17:00:58 +01:00
|
|
|
ServerConnection.prototype.gotAnswer = async function(id, sdp) {
|
2020-08-11 17:09:31 +02:00
|
|
|
let c = this.up[id];
|
|
|
|
if(!c)
|
|
|
|
throw new Error('unknown up stream');
|
|
|
|
try {
|
2021-01-03 17:00:58 +01:00
|
|
|
await c.pc.setRemoteDescription({
|
|
|
|
type: 'answer',
|
|
|
|
sdp: sdp,
|
|
|
|
});
|
2020-08-11 17:09:31 +02:00
|
|
|
} catch(e) {
|
2020-12-25 18:26:30 +01:00
|
|
|
try {
|
|
|
|
if(c.onerror)
|
|
|
|
c.onerror.call(c, e);
|
|
|
|
} finally {
|
2021-01-17 20:20:35 +01:00
|
|
|
c.close();
|
2020-12-25 18:26:30 +01:00
|
|
|
}
|
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
|
|
|
|
|
|
|
/**
|
2022-03-23 00:08:16 +01:00
|
|
|
* gotRenegotiate is called when we receive a renegotiation request from
|
|
|
|
* the server. Don't call this.
|
2020-08-11 17:09:31 +02:00
|
|
|
*
|
|
|
|
* @param {string} id
|
2020-08-14 14:44:23 +02:00
|
|
|
* @function
|
2020-08-11 17:09:31 +02:00
|
|
|
*/
|
2021-04-24 14:53:24 +02:00
|
|
|
ServerConnection.prototype.gotRenegotiate = function(id) {
|
2020-08-11 17:09:31 +02:00
|
|
|
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
|
|
|
|
|
|
|
/**
|
2022-03-23 00:08:16 +01:00
|
|
|
* gotClose is called when we receive a close request from the server.
|
|
|
|
* Don't call this.
|
2020-08-11 17:09:31 +02:00
|
|
|
*
|
|
|
|
* @param {string} id
|
|
|
|
*/
|
|
|
|
ServerConnection.prototype.gotClose = function(id) {
|
|
|
|
let c = this.down[id];
|
2021-11-27 21:30:07 +01:00
|
|
|
if(!c) {
|
2021-07-16 23:10:13 +02:00
|
|
|
console.warn('unknown down stream', id);
|
2021-11-27 21:30:07 +01:00
|
|
|
return;
|
|
|
|
}
|
2020-12-05 20:27:32 +01:00
|
|
|
c.close();
|
2020-09-14 15:48:17 +02:00
|
|
|
};
|
2020-08-11 17:09:31 +02:00
|
|
|
|
|
|
|
/**
|
2022-03-23 00:08:16 +01:00
|
|
|
* gotAbort is called when we receive an abort message from the server.
|
|
|
|
* Don't call this.
|
2020-08-11 17:09:31 +02:00
|
|
|
*
|
|
|
|
* @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');
|
2021-04-27 17:10:34 +02:00
|
|
|
c.close();
|
2020-09-14 15:48:17 +02:00
|
|
|
};
|
2020-08-11 17:09:31 +02:00
|
|
|
|
|
|
|
/**
|
2022-03-23 00:08:16 +01:00
|
|
|
* gotRemoteIce is called when we receive an ICE candidate from the server.
|
|
|
|
* Don't call this.
|
2020-08-11 17:09:31 +02:00
|
|
|
*
|
|
|
|
* @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
|
2021-01-31 19:00:09 +01:00
|
|
|
* @param {string} localId
|
2020-08-11 17:09:31 +02:00
|
|
|
* @param {RTCPeerConnection} pc
|
|
|
|
*
|
|
|
|
* @constructor
|
|
|
|
*/
|
2021-01-31 19:00:09 +01:00
|
|
|
function Stream(sc, id, localId, 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;
|
2021-01-31 19:00:09 +01:00
|
|
|
/**
|
|
|
|
* The local id of this stream.
|
|
|
|
*
|
|
|
|
* @type {string}
|
|
|
|
* @const
|
|
|
|
*/
|
|
|
|
this.localId = localId;
|
2020-12-05 20:27:32 +01:00
|
|
|
/**
|
|
|
|
* Indicates whether the stream is in the client->server direction.
|
|
|
|
*
|
|
|
|
* @type {boolean}
|
|
|
|
* @const
|
|
|
|
*/
|
2020-12-26 17:28:44 +01:00
|
|
|
this.up = up;
|
2020-08-11 17:09:31 +02:00
|
|
|
/**
|
2021-01-03 12:04:39 +01:00
|
|
|
* For down streams, the id of the client that created the stream.
|
|
|
|
*
|
|
|
|
* @type {string}
|
|
|
|
*/
|
|
|
|
this.source = null;
|
|
|
|
/**
|
|
|
|
* For down streams, the username of the client who created the stream.
|
2020-08-11 17:09:31 +02:00
|
|
|
*
|
|
|
|
* @type {string}
|
|
|
|
*/
|
2021-01-03 12:04:39 +01:00
|
|
|
this.username = null;
|
2020-08-11 17:09:31 +02:00
|
|
|
/**
|
2021-01-03 12:04:39 +01:00
|
|
|
* The associated RTCPeerConnection. This is null before the stream
|
2020-08-11 17:09:31 +02:00
|
|
|
* 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;
|
|
|
|
/**
|
2021-04-28 17:00:50 +02:00
|
|
|
* The label assigned by the originator to this stream.
|
2020-08-11 17:09:31 +02:00
|
|
|
*
|
2021-04-28 17:00:50 +02:00
|
|
|
* @type {string}
|
2020-08-11 17:09:31 +02:00
|
|
|
*/
|
2021-04-28 17:00:50 +02:00
|
|
|
this.label = null;
|
2021-01-31 19:00:09 +01:00
|
|
|
/**
|
|
|
|
* The id of the stream that we are currently replacing.
|
|
|
|
*
|
|
|
|
* @type {string}
|
|
|
|
*/
|
|
|
|
this.replace = null;
|
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 */
|
|
|
|
|
|
|
|
/**
|
2021-01-17 20:20:35 +01:00
|
|
|
* onclose is called when the stream is closed. Replace will be true
|
|
|
|
* if the stream is being replaced by another one with the same id.
|
2020-08-11 17:09:31 +02:00
|
|
|
*
|
2021-01-17 20:20:35 +01:00
|
|
|
* @type{(this: Stream, replace: boolean) => void}
|
2020-08-11 17:09:31 +02:00
|
|
|
*/
|
|
|
|
this.onclose = null;
|
|
|
|
/**
|
2021-07-30 17:40:15 +02:00
|
|
|
* onerror is called whenever a fatal error occurs. The stream will
|
|
|
|
* then be closed, and onclose called normally.
|
2020-08-11 17:09:31 +02:00
|
|
|
*
|
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.
|
|
|
|
*
|
2021-04-28 17:00:50 +02:00
|
|
|
* @type{(this: Stream, track: MediaStreamTrack, transceiver: RTCRtpTransceiver, stream: MediaStream) => void}
|
2020-08-11 17:09:31 +02:00
|
|
|
*/
|
|
|
|
this.ondowntrack = 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;
|
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
|
2022-03-25 17:06:13 +01:00
|
|
|
/**
|
|
|
|
* setStream sets the stream of an upwards connection.
|
|
|
|
*
|
|
|
|
* @param {MediaStream} stream
|
|
|
|
*/
|
|
|
|
Stream.prototype.setStream = function(stream) {
|
|
|
|
let c = this;
|
|
|
|
c.stream = stream;
|
|
|
|
let changed = recomputeUserStreams(c.sc, c.sc.id);
|
|
|
|
if(changed && c.sc.onuser)
|
|
|
|
c.sc.onuser.call(c.sc, c.sc.id, "change");
|
|
|
|
}
|
|
|
|
|
2020-08-11 17:09:31 +02:00
|
|
|
/**
|
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.
|
2021-01-12 02:23:22 +01:00
|
|
|
*
|
2021-01-17 20:20:35 +01:00
|
|
|
* @param {boolean} [replace]
|
|
|
|
* - true if the stream is being replaced by another one with the same id
|
2020-08-11 17:09:31 +02:00
|
|
|
*/
|
2021-01-17 20:20:35 +01:00
|
|
|
Stream.prototype.close = function(replace) {
|
2020-08-11 17:09:31 +02:00
|
|
|
let c = this;
|
2021-01-14 14:56:15 +01:00
|
|
|
|
|
|
|
if(!c.sc) {
|
|
|
|
console.warn('Closing closed stream');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-08-11 17:09:31 +02:00
|
|
|
if(c.statsHandler) {
|
|
|
|
clearInterval(c.statsHandler);
|
|
|
|
c.statsHandler = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
c.pc.close();
|
|
|
|
|
2021-01-31 19:00:09 +01:00
|
|
|
if(c.up && !replace && c.localDescriptionSent) {
|
2020-08-11 17:09:31 +02:00
|
|
|
try {
|
|
|
|
c.sc.send({
|
|
|
|
type: 'close',
|
|
|
|
id: c.id,
|
|
|
|
});
|
|
|
|
} catch(e) {
|
|
|
|
}
|
|
|
|
}
|
2021-01-12 02:23:22 +01:00
|
|
|
|
2022-03-25 17:06:13 +01:00
|
|
|
let userid;
|
2021-01-14 14:56:15 +01:00
|
|
|
if(c.up) {
|
2022-03-25 17:06:13 +01:00
|
|
|
userid = c.sc.id;
|
2021-01-14 14:56:15 +01:00
|
|
|
if(c.sc.up[c.id] === c)
|
|
|
|
delete(c.sc.up[c.id]);
|
|
|
|
else
|
|
|
|
console.warn('Closing unknown stream');
|
|
|
|
} else {
|
2022-03-25 17:06:13 +01:00
|
|
|
userid = c.source;
|
2021-01-14 14:56:15 +01:00
|
|
|
if(c.sc.down[c.id] === c)
|
|
|
|
delete(c.sc.down[c.id]);
|
|
|
|
else
|
|
|
|
console.warn('Closing unknown stream');
|
|
|
|
}
|
2022-03-25 17:06:13 +01:00
|
|
|
let changed = recomputeUserStreams(c.sc, userid);
|
|
|
|
if(changed && c.sc.onuser)
|
|
|
|
c.sc.onuser.call(c.sc, userid, "change");
|
2021-01-14 14:56:15 +01:00
|
|
|
|
2021-01-17 20:20:35 +01:00
|
|
|
if(c.onclose)
|
|
|
|
c.onclose.call(c, replace);
|
2023-08-29 01:42:48 +02:00
|
|
|
|
|
|
|
c.sc = null;
|
2020-08-11 17:09:31 +02:00
|
|
|
};
|
|
|
|
|
2021-04-29 19:03:07 +02:00
|
|
|
/**
|
2022-03-25 17:06:13 +01:00
|
|
|
* recomputeUserStreams recomputes the user.streams array for a given user.
|
2022-03-23 00:08:16 +01:00
|
|
|
* It returns true if anything changed.
|
|
|
|
*
|
2021-04-29 19:03:07 +02:00
|
|
|
* @param {ServerConnection} sc
|
|
|
|
* @param {string} id
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
2022-03-25 17:06:13 +01:00
|
|
|
function recomputeUserStreams(sc, id) {
|
2021-04-29 19:03:07 +02:00
|
|
|
let user = sc.users[id];
|
|
|
|
if(!user) {
|
|
|
|
console.warn("recomputing streams for unknown user");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-03-25 17:06:13 +01:00
|
|
|
let streams = id === sc.id ? sc.up : sc.down;
|
|
|
|
let old = user.streams;
|
|
|
|
user.streams = {};
|
|
|
|
for(id in streams) {
|
|
|
|
let c = streams[id];
|
2021-05-13 04:09:56 +02:00
|
|
|
if(!c.stream)
|
|
|
|
continue;
|
2022-03-25 17:06:13 +01:00
|
|
|
if(!user.streams[c.label])
|
|
|
|
user.streams[c.label] = {};
|
2021-04-29 19:03:07 +02:00
|
|
|
c.stream.getTracks().forEach(t => {
|
2022-03-25 17:06:13 +01:00
|
|
|
user.streams[c.label][t.kind] = true;
|
2021-04-29 19:03:07 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-03-25 17:06:13 +01:00
|
|
|
return JSON.stringify(old) != JSON.stringify(user.streams);
|
2021-04-29 19:03:07 +02:00
|
|
|
}
|
|
|
|
|
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-12-26 17:28:44 +01:00
|
|
|
};
|
2020-12-25 18:25:55 +01:00
|
|
|
|
2020-10-30 00:16:09 +01:00
|
|
|
/**
|
2022-03-23 00:08:16 +01:00
|
|
|
* gotLocalIce is Called when we get a local ICE candidate. Don't call this.
|
2020-10-30 00:16:09 +01:00
|
|
|
*
|
|
|
|
* @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-12-26 17:28:44 +01:00
|
|
|
};
|
2020-10-30 00:16:09 +01:00
|
|
|
|
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.
|
2022-03-23 00:08:16 +01:00
|
|
|
*
|
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-12-26 17:28:44 +01:00
|
|
|
};
|
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.
|
2022-03-23 00:08:16 +01:00
|
|
|
*
|
2020-12-05 02:52:56 +01:00
|
|
|
* @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);
|
|
|
|
|
|
|
|
c.sc.send({
|
|
|
|
type: 'offer',
|
2021-01-03 12:04:39 +01:00
|
|
|
source: c.sc.id,
|
|
|
|
username: c.sc.username,
|
2020-12-05 02:52:56 +01:00
|
|
|
kind: this.localDescriptionSent ? 'renegotiate' : '',
|
2020-08-11 17:09:31 +02:00
|
|
|
id: c.id,
|
2021-01-31 19:00:09 +01:00
|
|
|
replace: this.replace,
|
2021-04-28 17:00:50 +02:00
|
|
|
label: c.label,
|
2021-01-11 20:27:39 +01:00
|
|
|
sdp: c.pc.localDescription.sdp,
|
2020-08-11 17:09:31 +02:00
|
|
|
});
|
2020-12-05 02:52:56 +01:00
|
|
|
this.localDescriptionSent = true;
|
2021-01-31 19:00:09 +01:00
|
|
|
this.replace = null;
|
2020-12-05 02:52:56 +01:00
|
|
|
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 {
|
|
|
|
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
|
|
|
|
2021-05-14 18:39:39 +02:00
|
|
|
/**
|
|
|
|
* request sets the list of tracks. If this is not called, or called with
|
|
|
|
* a null argument, then the default is provided by ServerConnection.request.
|
|
|
|
*
|
|
|
|
* @param {Array<string>} what - a sequence of 'audio', 'video' or 'video-low'.
|
|
|
|
*/
|
|
|
|
Stream.prototype.request = function(what) {
|
|
|
|
let c = this;
|
|
|
|
c.sc.send({
|
|
|
|
type: 'requestStream',
|
|
|
|
id: c.id,
|
|
|
|
request: what,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
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') {
|
2021-04-29 17:03:25 +02:00
|
|
|
let id = stid;
|
2022-03-20 22:30:44 +01:00
|
|
|
// Firefox doesn't implement rid, use ssrc
|
|
|
|
// to discriminate simulcast tracks.
|
|
|
|
id = id + '-' + r.ssrc;
|
2020-09-11 23:20:46 +02:00
|
|
|
if(!('bytesSent' in r))
|
|
|
|
continue;
|
2021-04-29 17:03:25 +02:00
|
|
|
if(!stats[id])
|
|
|
|
stats[id] = {};
|
|
|
|
stats[id][r.type] = {};
|
|
|
|
stats[id][r.type].timestamp = r.timestamp;
|
|
|
|
stats[id][r.type].bytesSent = r.bytesSent;
|
|
|
|
if(old[id] && old[id][r.type])
|
|
|
|
stats[id][r.type].rate =
|
|
|
|
((r.bytesSent - old[id][r.type].bytesSent) * 1000 /
|
|
|
|
(r.timestamp - old[id][r.type].timestamp)) * 8;
|
2020-09-11 23:20:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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()) {
|
2023-08-26 07:29:15 +02:00
|
|
|
if(rtid && r.type === 'inbound-rtp') {
|
2020-09-11 23:20:46 +02:00
|
|
|
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
|
|
|
};
|
2022-07-23 20:52:04 +02:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A file in the process of being transferred.
|
|
|
|
* These are stored in the ServerConnection.transferredFiles dictionary.
|
|
|
|
*
|
2022-08-04 19:14:35 +02:00
|
|
|
* State transitions:
|
|
|
|
* @example
|
|
|
|
* '' -> inviting -> connecting -> connected -> done -> closed
|
|
|
|
* any -> cancelled -> closed
|
|
|
|
*
|
|
|
|
*
|
2022-07-23 20:52:04 +02:00
|
|
|
* @parm {ServerConnection} sc
|
|
|
|
* @parm {string} userid
|
|
|
|
* @parm {string} rid
|
|
|
|
* @parm {boolean} up
|
|
|
|
* @parm {string} username
|
|
|
|
* @parm {string} mimetype
|
|
|
|
* @parm {number} size
|
|
|
|
* @constructor
|
|
|
|
*/
|
|
|
|
function TransferredFile(sc, userid, id, up, username, name, mimetype, size) {
|
2022-08-04 19:14:35 +02:00
|
|
|
/**
|
|
|
|
* The server connection this file is associated with.
|
|
|
|
*
|
|
|
|
* @type {ServerConnection}
|
|
|
|
*/
|
2022-07-23 20:52:04 +02:00
|
|
|
this.sc = sc;
|
2022-08-04 19:14:35 +02:00
|
|
|
/** The id of the remote peer.
|
|
|
|
*
|
|
|
|
* @type {string}
|
|
|
|
*/
|
2022-07-23 20:52:04 +02:00
|
|
|
this.userid = userid;
|
2022-08-04 19:14:35 +02:00
|
|
|
/**
|
|
|
|
* The id of this file transfer.
|
|
|
|
*
|
|
|
|
* @type {string}
|
|
|
|
*/
|
2022-07-23 20:52:04 +02:00
|
|
|
this.id = id;
|
2024-11-02 13:36:09 +01:00
|
|
|
/**
|
|
|
|
* The negotiated file-transfer protocol version.
|
|
|
|
*
|
|
|
|
* This is the version of the file-transfer protocol, and is not
|
|
|
|
* necessarily equal to the version of the Galene protocol used by the
|
|
|
|
* server connection.
|
|
|
|
*
|
|
|
|
* @type {string}
|
|
|
|
*/
|
|
|
|
this.version = null;
|
2022-08-04 19:14:35 +02:00
|
|
|
/**
|
|
|
|
* True if this is an upload.
|
|
|
|
*
|
|
|
|
* @type {boolean}
|
|
|
|
*/
|
2022-07-23 20:52:04 +02:00
|
|
|
this.up = up;
|
2022-08-04 19:14:35 +02:00
|
|
|
/**
|
|
|
|
* The state of this file transfer. See the description of the
|
|
|
|
* constructor for possible state transitions.
|
|
|
|
*
|
|
|
|
* @type {string}
|
|
|
|
*/
|
2022-07-23 20:52:04 +02:00
|
|
|
this.state = '';
|
2022-08-04 19:14:35 +02:00
|
|
|
/**
|
|
|
|
* The username of the remote peer.
|
|
|
|
*
|
|
|
|
* @type {string}
|
|
|
|
*/
|
2022-07-23 20:52:04 +02:00
|
|
|
this.username = username;
|
2022-08-04 19:14:35 +02:00
|
|
|
/**
|
|
|
|
* The name of the file being transferred.
|
|
|
|
*
|
|
|
|
* @type {string}
|
|
|
|
*/
|
2022-07-23 20:52:04 +02:00
|
|
|
this.name = name;
|
2022-08-04 19:14:35 +02:00
|
|
|
/**
|
|
|
|
* The MIME type of the file being transferred.
|
|
|
|
*
|
|
|
|
* @type {string}
|
|
|
|
*/
|
2022-07-23 20:52:04 +02:00
|
|
|
this.mimetype = mimetype;
|
2022-08-04 19:14:35 +02:00
|
|
|
/**
|
|
|
|
* The size in bytes of the file being transferred.
|
|
|
|
*
|
|
|
|
* @type {number}
|
|
|
|
*/
|
2022-07-23 20:52:04 +02:00
|
|
|
this.size = size;
|
2022-08-04 19:14:35 +02:00
|
|
|
/**
|
|
|
|
* The file being uploaded. Unused for downloads.
|
|
|
|
*
|
|
|
|
* @type {File}
|
|
|
|
*/
|
2022-07-23 20:52:04 +02:00
|
|
|
this.file = null;
|
2022-08-04 19:14:35 +02:00
|
|
|
/**
|
|
|
|
* The peer connection used for the transfer.
|
|
|
|
*
|
|
|
|
* @type {RTCPeerConnection}
|
|
|
|
*/
|
2022-07-23 20:52:04 +02:00
|
|
|
this.pc = null;
|
2022-08-04 19:14:35 +02:00
|
|
|
/**
|
|
|
|
* The datachannel used for the transfer.
|
|
|
|
*
|
|
|
|
* @type {RTCDataChannel}
|
|
|
|
*/
|
2022-07-23 20:52:04 +02:00
|
|
|
this.dc = null;
|
2022-08-04 19:14:35 +02:00
|
|
|
/**
|
|
|
|
* Buffered remote ICE candidates.
|
|
|
|
*
|
|
|
|
* @type {Array<RTCIceCandidateInit>}
|
|
|
|
*/
|
2022-07-23 20:52:04 +02:00
|
|
|
this.candidates = [];
|
2022-08-04 19:14:35 +02:00
|
|
|
/**
|
|
|
|
* The data received to date, stored as a list of blobs or array buffers,
|
|
|
|
* depending on what the browser supports.
|
|
|
|
*
|
|
|
|
* @type {Array<Blob|ArrayBuffer>}
|
|
|
|
*/
|
2022-07-23 20:52:04 +02:00
|
|
|
this.data = [];
|
2022-08-04 19:14:35 +02:00
|
|
|
/**
|
|
|
|
* The total size of the data received to date.
|
|
|
|
*
|
|
|
|
* @type {number}
|
|
|
|
*/
|
2022-07-23 20:52:04 +02:00
|
|
|
this.datalen = 0;
|
2022-08-04 19:14:35 +02:00
|
|
|
/**
|
|
|
|
* The main filetransfer callback.
|
|
|
|
*
|
|
|
|
* This is called whenever the state of the transfer changes,
|
|
|
|
* but may also be called multiple times in a single state, for example
|
|
|
|
* in order to display a progress bar. Call this.cancel in order
|
|
|
|
* to cancel the transfer.
|
|
|
|
*
|
|
|
|
* @type {(this: TransferredFile, type: string, [data]: string) => void}
|
|
|
|
*/
|
2022-07-23 20:52:04 +02:00
|
|
|
this.onevent = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The full id of this file transfer, used as a key in the transferredFiles
|
|
|
|
* dictionary.
|
|
|
|
*/
|
|
|
|
TransferredFile.prototype.fullid = function() {
|
2024-08-11 16:45:29 +02:00
|
|
|
return this.userid + '-' + this.id;
|
2022-07-23 20:52:04 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieve a transferred file from the transferredFiles dictionary.
|
|
|
|
*
|
|
|
|
* @param {string} userid
|
|
|
|
* @param {string} fileid
|
|
|
|
* @returns {TransferredFile}
|
|
|
|
*/
|
2024-08-11 16:45:29 +02:00
|
|
|
ServerConnection.prototype.getTransferredFile = function(userid, fileid) {
|
|
|
|
return this.transferredFiles[userid + '-' + fileid];
|
2022-07-23 20:52:04 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Close a file transfer and remove it from the transferredFiles dictionary.
|
|
|
|
* Do not call this, call 'cancel' instead.
|
|
|
|
*/
|
|
|
|
TransferredFile.prototype.close = function() {
|
|
|
|
let f = this;
|
|
|
|
if(f.state === 'closed')
|
|
|
|
return;
|
|
|
|
if(f.state !== 'done' && f.state !== 'cancelled')
|
|
|
|
console.warn(
|
|
|
|
`TransferredFile.close called in unexpected state ${f.state}`,
|
|
|
|
);
|
|
|
|
if(f.dc) {
|
|
|
|
f.dc.onclose = null;
|
|
|
|
f.dc.onerror = null;
|
|
|
|
f.dc.onmessage = null;
|
|
|
|
}
|
|
|
|
if(f.pc)
|
|
|
|
f.pc.close();
|
|
|
|
f.dc = null;
|
|
|
|
f.pc = null;
|
|
|
|
f.data = [];
|
|
|
|
f.datalen = 0;
|
|
|
|
delete(f.sc.transferredFiles[f.fullid()]);
|
|
|
|
f.event('closed');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-01-22 22:24:51 +01:00
|
|
|
* Buffer a chunk of data received during a file transfer.
|
|
|
|
* Do not call this, it is called automatically when data is received.
|
2022-07-23 20:52:04 +02:00
|
|
|
*
|
|
|
|
* @param {Blob|ArrayBuffer} data
|
|
|
|
*/
|
|
|
|
TransferredFile.prototype.bufferData = function(data) {
|
|
|
|
let f = this;
|
|
|
|
if(f.up)
|
|
|
|
throw new Error('buffering data in the wrong direction');
|
|
|
|
if(data instanceof Blob) {
|
|
|
|
f.datalen += data.size;
|
|
|
|
} else if(data instanceof ArrayBuffer) {
|
|
|
|
f.datalen += data.byteLength;
|
|
|
|
} else {
|
|
|
|
throw new Error('unexpected type for received data');
|
|
|
|
}
|
|
|
|
f.data.push(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retreive the data buffered during a file transfer. Don't call this.
|
|
|
|
*
|
|
|
|
* @returns {Blob}
|
|
|
|
*/
|
|
|
|
TransferredFile.prototype.getBufferedData = function() {
|
|
|
|
let f = this;
|
|
|
|
if(f.up)
|
|
|
|
throw new Error('buffering data in wrong direction');
|
|
|
|
let blob = new Blob(f.data, {type: f.mimetype});
|
|
|
|
if(blob.size != f.datalen)
|
|
|
|
throw new Error('Inconsistent data size');
|
|
|
|
f.data = [];
|
|
|
|
f.datalen = 0;
|
|
|
|
return blob;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the file's state, and call the onevent callback.
|
|
|
|
*
|
|
|
|
* This calls the callback even if the state didn't change, which is
|
|
|
|
* useful if the client needs to display a progress bar.
|
|
|
|
*
|
|
|
|
* @param {string} state
|
|
|
|
* @param {any} [data]
|
|
|
|
*/
|
|
|
|
TransferredFile.prototype.event = function(state, data) {
|
|
|
|
let f = this;
|
|
|
|
f.state = state;
|
|
|
|
if(f.onevent)
|
|
|
|
f.onevent.call(f, state, data);
|
|
|
|
}
|
|
|
|
|
2024-11-02 13:36:09 +01:00
|
|
|
/**
|
|
|
|
* Send a cancel message for a file transfer.
|
|
|
|
*
|
|
|
|
* Don't call this, call TransferredFile.cancel instead.
|
|
|
|
*
|
|
|
|
* @param {ServerConnection} sc
|
|
|
|
* @param {string} userid
|
|
|
|
* @param {string} id
|
|
|
|
* @param {string|Error} [message]
|
|
|
|
*/
|
|
|
|
function sendFileCancel(sc, userid, id, message) {
|
|
|
|
let m = {
|
|
|
|
type: 'cancel',
|
|
|
|
id: id,
|
|
|
|
};
|
|
|
|
if(message)
|
|
|
|
m.message = message.toString();
|
|
|
|
sc.userMessage('filetransfer', userid, m);
|
|
|
|
}
|
|
|
|
|
2022-07-23 20:52:04 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Cancel a file transfer.
|
|
|
|
*
|
|
|
|
* Depending on the state, this will either forcibly close the connection,
|
|
|
|
* send a handshake, or do nothing. It will set the state to cancelled.
|
|
|
|
*
|
2024-11-02 13:36:09 +01:00
|
|
|
* @param {string|Error} [message]
|
2022-07-23 20:52:04 +02:00
|
|
|
*/
|
2024-11-02 13:36:09 +01:00
|
|
|
TransferredFile.prototype.cancel = function(message) {
|
2022-07-23 20:52:04 +02:00
|
|
|
let f = this;
|
|
|
|
if(f.state === 'closed')
|
|
|
|
return;
|
2024-11-02 13:36:09 +01:00
|
|
|
if(f.state !== '' && f.state !== 'done' && f.state !== 'cancelled')
|
|
|
|
sendFileCancel(f.sc, f.userid, f.id, message);
|
2022-07-23 20:52:04 +02:00
|
|
|
if(f.state !== 'done' && f.state !== 'cancelled')
|
2024-11-02 13:36:09 +01:00
|
|
|
f.event('cancelled', message);
|
2022-07-23 20:52:04 +02:00
|
|
|
f.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Forcibly terminate a file transfer.
|
|
|
|
*
|
|
|
|
* This is like cancel, but will not attempt to handshake.
|
|
|
|
* Use cancel instead of this, unless you know what you are doing.
|
|
|
|
*
|
|
|
|
* @param {string|Error} [data]
|
|
|
|
*/
|
|
|
|
TransferredFile.prototype.fail = function(data) {
|
|
|
|
let f = this;
|
|
|
|
if(f.state === 'done' || f.state === 'cancelled' || f.state === 'closed')
|
|
|
|
return;
|
|
|
|
f.event('cancelled', data);
|
|
|
|
f.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initiate a file upload.
|
|
|
|
*
|
|
|
|
* This will cause the onfiletransfer callback to be called, at which
|
|
|
|
* point you should set up the onevent callback.
|
|
|
|
*
|
|
|
|
* @param {string} id
|
|
|
|
* @param {File} file
|
|
|
|
*/
|
|
|
|
ServerConnection.prototype.sendFile = function(id, file) {
|
|
|
|
let sc = this;
|
|
|
|
let fileid = newRandomId();
|
|
|
|
let user = sc.users[id];
|
|
|
|
if(!user)
|
|
|
|
throw new Error('offering upload to unknown user');
|
|
|
|
let f = new TransferredFile(
|
|
|
|
sc, id, fileid, true, user.username, file.name, file.type, file.size,
|
|
|
|
);
|
|
|
|
f.file = file;
|
|
|
|
|
|
|
|
try {
|
|
|
|
if(sc.onfiletransfer)
|
|
|
|
sc.onfiletransfer.call(sc, f);
|
|
|
|
else
|
|
|
|
throw new Error('this client does not implement file transfer');
|
|
|
|
} catch(e) {
|
|
|
|
f.cancel(e);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-08-11 17:38:55 +02:00
|
|
|
if(f.state === 'closed') {
|
|
|
|
// the client cancelled the transfer
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-07-23 20:52:04 +02:00
|
|
|
sc.transferredFiles[f.fullid()] = f;
|
|
|
|
sc.userMessage('filetransfer', id, {
|
|
|
|
type: 'invite',
|
2024-11-02 13:36:09 +01:00
|
|
|
version: ["1"],
|
2022-07-23 20:52:04 +02:00
|
|
|
id: fileid,
|
|
|
|
name: f.name,
|
|
|
|
size: f.size,
|
|
|
|
mimetype: f.mimetype,
|
|
|
|
});
|
|
|
|
f.event('inviting');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Receive a file.
|
|
|
|
*
|
|
|
|
* Call this after the onfiletransfer callback has yielded an incoming
|
|
|
|
* file (up field set to false). If you wish to reject the file transfer,
|
|
|
|
* call cancel instead.
|
|
|
|
*/
|
|
|
|
TransferredFile.prototype.receive = async function() {
|
|
|
|
let f = this;
|
|
|
|
if(f.up)
|
|
|
|
throw new Error('Receiving in wrong direction');
|
|
|
|
if(f.pc)
|
|
|
|
throw new Error('Download already in progress');
|
2022-08-29 20:06:01 +02:00
|
|
|
let pc = new RTCPeerConnection(f.sc.getRTCConfiguration());
|
2022-07-23 20:52:04 +02:00
|
|
|
if(!pc) {
|
|
|
|
let err = new Error("Couldn't create peer connection");
|
|
|
|
f.fail(err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
f.pc = pc;
|
|
|
|
f.event('connecting');
|
|
|
|
|
|
|
|
f.candidates = [];
|
|
|
|
pc.onsignalingstatechange = function(e) {
|
|
|
|
if(pc.signalingState === 'stable') {
|
|
|
|
f.candidates.forEach(c => pc.addIceCandidate(c).catch(console.warn));
|
|
|
|
f.candidates = [];
|
|
|
|
}
|
|
|
|
};
|
|
|
|
pc.onicecandidate = function(e) {
|
2022-08-29 20:06:01 +02:00
|
|
|
f.sc.userMessage('filetransfer', f.userid, {
|
2024-10-28 15:52:59 +01:00
|
|
|
type: 'ice',
|
2022-07-23 20:52:04 +02:00
|
|
|
id: f.id,
|
|
|
|
candidate: e.candidate,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
f.dc = pc.createDataChannel('file');
|
|
|
|
f.data = [];
|
|
|
|
f.datalen = 0;
|
|
|
|
f.dc.onclose = function(e) {
|
|
|
|
f.cancel('remote peer closed connection');
|
|
|
|
};
|
|
|
|
f.dc.onmessage = function(e) {
|
|
|
|
f.receiveData(e.data).catch(e => f.cancel(e));
|
|
|
|
};
|
|
|
|
f.dc.onerror = function(e) {
|
|
|
|
/** @ts-ignore */
|
|
|
|
let err = e.error;
|
|
|
|
f.cancel(err)
|
|
|
|
};
|
|
|
|
let offer = await pc.createOffer();
|
|
|
|
if(!offer) {
|
|
|
|
f.cancel(new Error("Couldn't create offer"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
await pc.setLocalDescription(offer);
|
|
|
|
f.sc.userMessage('filetransfer', f.userid, {
|
|
|
|
type: 'offer',
|
2024-11-02 13:36:09 +01:00
|
|
|
version: [f.version],
|
2022-07-23 20:52:04 +02:00
|
|
|
id: f.id,
|
|
|
|
sdp: pc.localDescription.sdp,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-01-22 22:24:51 +01:00
|
|
|
* Negotiate a file transfer on the sender side.
|
|
|
|
* Don't call this, it is called automatically we receive an offer.
|
2022-07-23 20:52:04 +02:00
|
|
|
*
|
|
|
|
* @param {string} sdp
|
|
|
|
*/
|
|
|
|
TransferredFile.prototype.answer = async function(sdp) {
|
|
|
|
let f = this;
|
|
|
|
if(!f.up)
|
|
|
|
throw new Error('Sending file in wrong direction');
|
|
|
|
if(f.pc)
|
|
|
|
throw new Error('Transfer already in progress');
|
2022-08-29 20:06:01 +02:00
|
|
|
let pc = new RTCPeerConnection(f.sc.getRTCConfiguration());
|
2022-07-23 20:52:04 +02:00
|
|
|
if(!pc) {
|
|
|
|
let err = new Error("Couldn't create peer connection");
|
|
|
|
f.fail(err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
f.pc = pc;
|
|
|
|
f.event('connecting');
|
|
|
|
|
|
|
|
f.candidates = [];
|
|
|
|
pc.onicecandidate = function(e) {
|
2022-08-29 20:06:01 +02:00
|
|
|
f.sc.userMessage('filetransfer', f.userid, {
|
2024-10-28 15:52:59 +01:00
|
|
|
type: 'ice',
|
2022-07-23 20:52:04 +02:00
|
|
|
id: f.id,
|
|
|
|
candidate: e.candidate,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
pc.onsignalingstatechange = function(e) {
|
|
|
|
if(pc.signalingState === 'stable') {
|
|
|
|
f.candidates.forEach(c => pc.addIceCandidate(c).catch(console.warn));
|
|
|
|
f.candidates = [];
|
|
|
|
}
|
|
|
|
};
|
|
|
|
pc.ondatachannel = function(e) {
|
|
|
|
if(f.dc) {
|
|
|
|
f.cancel(new Error('Duplicate datachannel'));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
f.dc = /** @type{RTCDataChannel} */(e.channel);
|
|
|
|
f.dc.onclose = function(e) {
|
|
|
|
f.cancel('remote peer closed connection');
|
|
|
|
};
|
|
|
|
f.dc.onerror = function(e) {
|
|
|
|
/** @ts-ignore */
|
|
|
|
let err = e.error;
|
|
|
|
f.cancel(err);
|
|
|
|
}
|
|
|
|
f.dc.onmessage = function(e) {
|
|
|
|
if(e.data === 'done' && f.datalen === f.size) {
|
|
|
|
f.event('done');
|
|
|
|
f.dc.onclose = null;
|
|
|
|
f.dc.onerror = null;
|
|
|
|
f.close();
|
|
|
|
} else {
|
|
|
|
f.cancel(new Error('unexpected data from receiver'));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
f.send().catch(e => f.cancel(e));
|
|
|
|
};
|
|
|
|
|
|
|
|
await pc.setRemoteDescription({
|
|
|
|
type: 'offer',
|
|
|
|
sdp: sdp,
|
|
|
|
});
|
|
|
|
|
|
|
|
let answer = await pc.createAnswer();
|
|
|
|
if(!answer)
|
|
|
|
throw new Error("Couldn't create answer");
|
|
|
|
await pc.setLocalDescription(answer);
|
2022-08-29 20:06:01 +02:00
|
|
|
f.sc.userMessage('filetransfer', f.userid, {
|
2022-07-23 20:52:04 +02:00
|
|
|
type: 'answer',
|
|
|
|
id: f.id,
|
|
|
|
sdp: pc.localDescription.sdp,
|
|
|
|
});
|
|
|
|
|
|
|
|
f.event('connected');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Transfer file data. Don't call this, it is called automatically
|
|
|
|
* after negotiation completes.
|
|
|
|
*/
|
|
|
|
TransferredFile.prototype.send = async function() {
|
|
|
|
let f = this;
|
|
|
|
if(!f.up)
|
|
|
|
throw new Error('sending in wrong direction');
|
|
|
|
let r = f.file.stream().getReader();
|
|
|
|
|
|
|
|
f.dc.bufferedAmountLowThreshold = 65536;
|
|
|
|
|
2023-01-22 22:24:51 +01:00
|
|
|
/** @param {Uint8Array} a */
|
2022-07-23 20:52:04 +02:00
|
|
|
async function write(a) {
|
|
|
|
while(f.dc.bufferedAmount > f.dc.bufferedAmountLowThreshold) {
|
|
|
|
await new Promise((resolve, reject) => {
|
|
|
|
if(!f.dc) {
|
|
|
|
reject(new Error('File is closed.'));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
f.dc.onbufferedamountlow = function(e) {
|
|
|
|
if(!f.dc) {
|
|
|
|
reject(new Error('File is closed.'));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
f.dc.onbufferedamountlow = null;
|
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
f.dc.send(a);
|
|
|
|
f.datalen += a.length;
|
|
|
|
// we're already in the connected state, but invoke callbacks to
|
|
|
|
// that the application can display progress
|
|
|
|
f.event('connected');
|
|
|
|
}
|
|
|
|
|
|
|
|
while(true) {
|
|
|
|
let v = await r.read();
|
|
|
|
if(v.done)
|
|
|
|
break;
|
|
|
|
let data = v.value;
|
|
|
|
if(!(data instanceof Uint8Array))
|
|
|
|
throw new Error('Unexpected type for chunk');
|
|
|
|
/* Base SCTP only supports up to 16kB data chunks. There are
|
|
|
|
extensions to handle larger chunks, but they don't interoperate
|
|
|
|
between browsers, so we chop the file into small pieces. */
|
|
|
|
if(data.length <= 16384) {
|
|
|
|
await write(data);
|
|
|
|
} else {
|
|
|
|
for(let i = 0; i < v.value.length; i += 16384) {
|
2023-01-22 22:24:51 +01:00
|
|
|
let d = data.subarray(i, Math.min(i + 16384, data.length));
|
2022-07-23 20:52:04 +02:00
|
|
|
await write(d);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-08-04 19:14:35 +02:00
|
|
|
* Called after we receive an answer. Don't call this.
|
|
|
|
*
|
2022-07-23 20:52:04 +02:00
|
|
|
* @param {string} sdp
|
|
|
|
*/
|
|
|
|
TransferredFile.prototype.receiveFile = async function(sdp) {
|
|
|
|
let f = this;
|
|
|
|
if(f.up)
|
|
|
|
throw new Error('Receiving in wrong direction');
|
|
|
|
await f.pc.setRemoteDescription({
|
|
|
|
type: 'answer',
|
|
|
|
sdp: sdp,
|
|
|
|
});
|
|
|
|
f.event('connected');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-08-04 19:14:35 +02:00
|
|
|
* Called whenever we receive a chunk of data. Don't call this.
|
|
|
|
*
|
2022-07-23 20:52:04 +02:00
|
|
|
* @param {Blob|ArrayBuffer} data
|
|
|
|
*/
|
|
|
|
TransferredFile.prototype.receiveData = async function(data) {
|
|
|
|
let f = this;
|
|
|
|
if(f.up)
|
|
|
|
throw new Error('Receiving in wrong direction');
|
|
|
|
f.bufferData(data);
|
|
|
|
|
|
|
|
if(f.datalen < f.size) {
|
|
|
|
f.event('connected');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
f.dc.onmessage = null;
|
|
|
|
|
|
|
|
if(f.datalen != f.size) {
|
2023-01-22 22:24:51 +01:00
|
|
|
f.cancel('extra data at end of file');
|
2022-07-23 20:52:04 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let blob = f.getBufferedData();
|
2023-01-22 22:24:51 +01:00
|
|
|
if(blob.size != f.size) {
|
|
|
|
f.cancel("inconsistent data size (this shouldn't happen)");
|
|
|
|
return;
|
|
|
|
}
|
2022-07-23 20:52:04 +02:00
|
|
|
f.event('done', blob);
|
|
|
|
|
2023-01-22 22:24:51 +01:00
|
|
|
// we've received the whole file. Send the final handshake, but don't
|
|
|
|
// complain if the peer has closed the channel in the meantime.
|
2022-07-23 20:52:04 +02:00
|
|
|
await new Promise((resolve, reject) => {
|
|
|
|
let timer = setTimeout(function(e) { resolve(); }, 2000);
|
|
|
|
f.dc.onclose = function(e) {
|
|
|
|
clearTimeout(timer);
|
|
|
|
resolve();
|
|
|
|
};
|
|
|
|
f.dc.onerror = function(e) {
|
|
|
|
clearTimeout(timer);
|
|
|
|
resolve();
|
|
|
|
};
|
|
|
|
f.dc.send('done');
|
|
|
|
});
|
|
|
|
|
|
|
|
f.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-08-04 19:14:35 +02:00
|
|
|
* fileTransfer handles a usermessage of kind 'filetransfer'. Don't call
|
|
|
|
* this, it is called automatically as needed.
|
|
|
|
*
|
2022-07-23 20:52:04 +02:00
|
|
|
* @param {string} id
|
|
|
|
* @param {string} username
|
|
|
|
* @param {object} message
|
|
|
|
*/
|
|
|
|
ServerConnection.prototype.fileTransfer = function(id, username, message) {
|
|
|
|
let sc = this;
|
|
|
|
switch(message.type) {
|
|
|
|
case 'invite': {
|
2024-11-02 13:36:09 +01:00
|
|
|
/** @type {string} */
|
|
|
|
let version;
|
|
|
|
if((message.version instanceof Array) && message.version.includes('1')) {
|
|
|
|
version = '1';
|
|
|
|
} else {
|
|
|
|
sendFileCancel(sc, id, message.id,
|
|
|
|
`Unknown protocol version ${message.version}; ` +
|
|
|
|
'perhaps you need to upgrade your client ?');
|
|
|
|
return;
|
|
|
|
}
|
2024-12-01 21:40:50 +01:00
|
|
|
|
2022-07-23 20:52:04 +02:00
|
|
|
let f = new TransferredFile(
|
|
|
|
sc, id, message.id, false, username,
|
|
|
|
message.name, message.mimetype, message.size,
|
|
|
|
);
|
2024-11-02 13:36:09 +01:00
|
|
|
f.version = version;
|
2022-07-23 20:52:04 +02:00
|
|
|
f.state = 'inviting';
|
|
|
|
|
2024-12-01 21:40:50 +01:00
|
|
|
let fid = f.fullid();
|
|
|
|
if(fid in sc.transferredFiles) {
|
|
|
|
sendFileCancel(sc, id, message.id,
|
|
|
|
'Duplicate file transfer id; ' +
|
|
|
|
'perhaps you have tried to send a file to yourself?');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-07-23 20:52:04 +02:00
|
|
|
try {
|
|
|
|
if(sc.onfiletransfer)
|
|
|
|
sc.onfiletransfer.call(sc, f);
|
2023-01-22 22:24:51 +01:00
|
|
|
else {
|
2022-07-23 20:52:04 +02:00
|
|
|
f.cancel('this client does not implement file transfer');
|
2023-01-22 22:24:51 +01:00
|
|
|
return;
|
|
|
|
}
|
2022-07-23 20:52:04 +02:00
|
|
|
} catch(e) {
|
|
|
|
f.cancel(e);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-08-11 16:45:29 +02:00
|
|
|
sc.transferredFiles[fid] = f;
|
2022-07-23 20:52:04 +02:00
|
|
|
break;
|
|
|
|
}
|
2024-11-02 13:36:09 +01:00
|
|
|
case 'offer': {
|
|
|
|
let f = sc.getTransferredFile(id, message.id);
|
|
|
|
if(!f) {
|
|
|
|
console.error(`Unexpected ${message.type} for file transfer`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if((message.version instanceof Array) && message.version.includes('1')) {
|
|
|
|
f.version = '1';
|
|
|
|
} else {
|
|
|
|
f.cancel(`Unknown protocol version ${message.version}; ` +
|
|
|
|
'perhaps you need to upgrade your client ?'
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
f.answer(message.sdp).catch(e => f.cancel(e));
|
|
|
|
break;
|
|
|
|
}
|
2022-07-23 20:52:04 +02:00
|
|
|
case 'answer': {
|
2024-08-11 16:45:29 +02:00
|
|
|
let f = sc.getTransferredFile(id, message.id);
|
2022-07-23 20:52:04 +02:00
|
|
|
if(!f) {
|
2024-08-11 16:45:29 +02:00
|
|
|
console.error(`Unexpected ${message.type} for file transfer`);
|
2022-07-23 20:52:04 +02:00
|
|
|
return;
|
|
|
|
}
|
2024-11-02 13:36:09 +01:00
|
|
|
f.receiveFile(message.sdp).catch(e => f.cancel(e));
|
2022-07-23 20:52:04 +02:00
|
|
|
break;
|
|
|
|
}
|
2024-08-11 16:45:29 +02:00
|
|
|
case 'ice':
|
|
|
|
{
|
|
|
|
let f = sc.getTransferredFile(id, message.id);
|
2022-07-23 20:52:04 +02:00
|
|
|
if(!f || !f.pc) {
|
|
|
|
console.warn(`Unexpected ${message.type} for file transfer`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(f.pc.signalingState === 'stable')
|
|
|
|
f.pc.addIceCandidate(message.candidate).catch(console.warn);
|
|
|
|
else
|
|
|
|
f.candidates.push(message.candidate);
|
|
|
|
break;
|
|
|
|
}
|
2024-08-11 16:45:29 +02:00
|
|
|
case 'cancel': {
|
|
|
|
let f = sc.getTransferredFile(id, message.id);
|
2022-07-23 20:52:04 +02:00
|
|
|
if(!f) {
|
|
|
|
console.error(`Unexpected ${message.type} for file transfer`);
|
|
|
|
return;
|
|
|
|
}
|
2024-11-02 13:36:09 +01:00
|
|
|
f.event('cancelled', message.message || null);
|
2022-07-23 20:52:04 +02:00
|
|
|
f.close();
|
|
|
|
break;
|
|
|
|
}
|
2024-08-11 16:45:29 +02:00
|
|
|
case 'upice':
|
|
|
|
case 'downice':
|
2024-10-30 17:59:07 +01:00
|
|
|
case 'reject':
|
2024-08-11 16:45:29 +02:00
|
|
|
case 'abort': {
|
|
|
|
let f = sc.getTransferredFile(id, message.id);
|
|
|
|
if(f)
|
|
|
|
f.cancel(`Obsolete file transfer message ${message.type};` +
|
|
|
|
' please upgrade your client');
|
|
|
|
return;
|
|
|
|
}
|
2022-07-23 20:52:04 +02:00
|
|
|
default:
|
|
|
|
console.error(`Unknown filetransfer message ${message.type}`);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|