Jitsi Meet - Secure, Simple and Scalable Video Conferences that you use as a standalone app or embed in your web application.
TypeScript debiandeep-videojitsijitsi-meetscalable-video-conferencessfuvideovideo-communicationvideo-conferencingwebrtc
Merge branch 'abstract_transport2'
Lyubo Marinov75a486ff9665
diff --cc modules/API/API.jsindex 436435734d,b41f847f23..a429e435c7--- a/modules/API/API.js+++ b/modules/API/API.js@@@ -231,16 -205,14 +205,15 @@@ class API* nickname.** @param {string} id - User id.- * @param {string} displayName - User nickname.+ * @param {string} displayname - User nickname.* @returns {void}*/- notifyDisplayNameChanged(id, displayName) {- triggerEvent(- 'display-name-change',- {- displayname: displayName,- id- });+ notifyDisplayNameChanged(id, displayname) {- this._sendEvent({ name: 'display-name-change',++ this._sendEvent({++ name: 'display-name-change',+ displayname,+ id+ });}/**diff --cc modules/API/constants.jsindex 0000000000,3bbf0e5dcb..6f2c592da7mode 000000,100644..100644--- a/modules/API/constants.js+++ b/modules/API/constants.js@@@ -1,0 -1,8 +1,9 @@@+ declare var getConfigParamsFromUrl: Function;++ /**+ * JitsiMeetExternalAPI id - unique for a webpage.+ */+ export const API_ID+ = typeof getConfigParamsFromUrl === 'function'- ? getConfigParamsFromUrl().jitsi_meet_external_api_id : undefined;++ ? getConfigParamsFromUrl().jitsi_meet_external_api_id++ : undefined;diff --cc modules/remotecontrol/Controller.jsindex 44f3381985,0adb15ea5b..51f7776fb6--- a/modules/remotecontrol/Controller.js+++ b/modules/remotecontrol/Controller.js@@@ -1,8 -1,8 +1,11 @@@/* global $, JitsiMeetJS, APP */const logger = require("jitsi-meet-logger").getLogger(__filename);import * as KeyCodes from "../keycode/keycode";--import {EVENT_TYPES, REMOTE_CONTROL_EVENT_TYPE, PERMISSIONS_ACTIONS}-- from "../../service/remotecontrol/Constants";++import {++ EVENT_TYPES,++ PERMISSIONS_ACTIONS,++ REMOTE_CONTROL_EVENT_NAME++} from "../../service/remotecontrol/Constants";import RemoteControlParticipant from "./RemoteControlParticipant";import UIEvents from "../../service/UI/UIEvents";@@@ -132,15 -132,14 +135,15 @@@ export default class Controller extend* @param {RemoteControlEvent} event the remote control event.*/_handleReply(participant, event) {- const remoteControlEvent = event.event;const userId = participant.getId();- if(this.enabled && event.type === REMOTE_CONTROL_EVENT_TYPE- && remoteControlEvent.type === EVENT_TYPES.permissions- if(this.enabled && event.name === REMOTE_CONTROL_EVENT_TYPE- && event.type === EVENT_TYPES.permissions-- && userId === this.requestedParticipant) {- if(remoteControlEvent.action !== PERMISSIONS_ACTIONS.grant) {++ if(this.enabled++ && event.name === REMOTE_CONTROL_EVENT_NAME++ && event.type === EVENT_TYPES.permissions++ && userId === this.requestedParticipant) {+ if(event.action !== PERMISSIONS_ACTIONS.grant) {this.area = null;}- switch(remoteControlEvent.action) {+ switch(event.action) {case PERMISSIONS_ACTIONS.grant: {this.controlledParticipant = userId;logger.log("Remote control permissions granted to: "@@@ -166,14 -165,14 +169,15 @@@* @param {JitsiParticipant} participant the participant that has sent the* event* @param {Object} event EndpointMessage event from the data channels.- * @property {string} type property. The function process only events of- * type REMOTE_CONTROL_EVENT_TYPE+ * @property {string} type property. The function process only events with- * name REMOTE_CONTROL_EVENT_TYPE++ * name REMOTE_CONTROL_EVENT_NAME* @property {RemoteControlEvent} event - the remote control event.*/_handleRemoteControlStoppedEvent(participant, event) {- if(this.enabled && event.type === REMOTE_CONTROL_EVENT_TYPE- && event.event.type === EVENT_TYPES.stop- if(this.enabled && event.name === REMOTE_CONTROL_EVENT_TYPE- && event.type === EVENT_TYPES.stop-- && participant.getId() === this.controlledParticipant) {++ if(this.enabled++ && event.name === REMOTE_CONTROL_EVENT_NAME++ && event.type === EVENT_TYPES.stop++ && participant.getId() === this.controlledParticipant) {this._stop();}}diff --cc modules/remotecontrol/Receiver.jsindex cf60636176,50ba939a29..fcf3bc25b3--- a/modules/remotecontrol/Receiver.js+++ b/modules/remotecontrol/Receiver.js@@@ -1,11 -1,25 +1,25 @@@- /* global APP, JitsiMeetJS, interfaceConfig */- const logger = require("jitsi-meet-logger").getLogger(__filename);- import {DISCO_REMOTE_CONTROL_FEATURE, REMOTE_CONTROL_EVENT_TYPE, EVENT_TYPES,- PERMISSIONS_ACTIONS} from "../../service/remotecontrol/Constants";- import RemoteControlParticipant from "./RemoteControlParticipant";+ /* global APP, config, interfaceConfig, JitsiMeetJS */+import * as JitsiMeetConferenceEvents from '../../ConferenceEvents';+ import {+ DISCO_REMOTE_CONTROL_FEATURE,+ EVENT_TYPES,+ PERMISSIONS_ACTIONS,- REMOTE_CONTROL_EVENT_TYPE++ REMOTE_CONTROL_EVENT_NAME+ } from '../../service/remotecontrol/Constants';+ import { getJitsiMeetTransport } from '../transport';++ import RemoteControlParticipant from './RemoteControlParticipant';const ConferenceEvents = JitsiMeetJS.events.conference;+ const logger = require("jitsi-meet-logger").getLogger(__filename);++ /**+ * The transport instance used for communication with external apps.+ *+ * @type {Transport}+ */+ const transport = getJitsiMeetTransport();/*** This class represents the receiver party for a remote controller session.@@@ -25,6 -39,17 +39,17 @@@ export default class Receiver extends R= this._onRemoteControlEvent.bind(this);this._userLeftListener = this._onUserLeft.bind(this);this._hangupListener = this._onHangup.bind(this);+ // We expect here that even if we receive the supported event earlier+ // it will be cached and we'll receive it.+ transport.on('event', event => {- if (event.name === REMOTE_CONTROL_EVENT_TYPE) {++ if (event.name === REMOTE_CONTROL_EVENT_NAME) {+ this._onRemoteControlAPIEvent(event);++ return true;+ }++ return false;+ });}/**@@@ -73,7 -98,8 +98,8 @@@this.controller = null;APP.conference.removeConferenceListener(ConferenceEvents.USER_LEFT,this._userLeftListener);- APP.API.sendRemoteControlEvent({+ transport.sendEvent({- name: REMOTE_CONTROL_EVENT_TYPE,++ name: REMOTE_CONTROL_EVENT_NAME,type: EVENT_TYPES.stop});if(!dontShowDialog) {@@@ -103,16 -129,22 +129,22 @@@* module.* @param {JitsiParticipant} participant the controller participant* @param {Object} event EndpointMessage event from the data channels.- * @property {string} type property. The function process only events of- * type REMOTE_CONTROL_EVENT_TYPE+ * @property {string} type property. The function process only events with- * name REMOTE_CONTROL_EVENT_TYPE++ * name REMOTE_CONTROL_EVENT_NAME* @property {RemoteControlEvent} event - the remote control event.*/_onRemoteControlEvent(participant, event) {- if(this.enabled && event.type === REMOTE_CONTROL_EVENT_TYPE) {- const remoteControlEvent = event.event;- if(this.controller === null- && remoteControlEvent.type === EVENT_TYPES.permissions- && remoteControlEvent.action === PERMISSIONS_ACTIONS.request) {- if (event.name !== REMOTE_CONTROL_EVENT_TYPE) {++ if (event.name !== REMOTE_CONTROL_EVENT_NAME) {+ return;+ }++ const remoteControlEvent = Object.assign({}, event);++ if (this.enabled) {+ if (this.controller === null- && event.type === EVENT_TYPES.permissions- && event.action === PERMISSIONS_ACTIONS.request) {++ && event.type === EVENT_TYPES.permissions++ && event.action === PERMISSIONS_ACTIONS.request) {+ // FIXME: Maybe use transport.sendRequest in this case???remoteControlEvent.userId = participant.getId();remoteControlEvent.userJID = participant.getJid();remoteControlEvent.displayName = participant.getDisplayName()@@@ -173,6 -205,35 +205,35 @@@});}+ /**+ * Handles remote control events from the external app. Currently only+ * events with type = EVENT_TYPES.supported or EVENT_TYPES.permissions+ * @param {RemoteControlEvent} event the remote control event.+ */+ _onRemoteControlAPIEvent(event) {+ switch(event.type) {- case EVENT_TYPES.supported:- this._onRemoteControlSupported();- break;+ case EVENT_TYPES.permissions:+ this._onRemoteControlPermissionsEvent(event.userId, event.action);+ break;++ case EVENT_TYPES.supported:++ this._onRemoteControlSupported();++ break;+ }+ }++ /**+ * Handles events for support for executing remote control events into+ * the wrapper application.+ */+ _onRemoteControlSupported() {+ logger.log("Remote Control supported.");- if (!config.disableRemoteControl) {- this._enable(true);- } else {++ if (config.disableRemoteControl) {+ logger.log("Remote Control disabled.");++ } else {++ this._enable(true);+ }+ }+/*** Calls the stop method if the other side have left.* @param {string} id - the user id for the participant that have leftdiff --cc modules/remotecontrol/RemoteControl.jsindex 86f1f51e50,466662471c..3cc629116a--- a/modules/remotecontrol/RemoteControl.js+++ b/modules/remotecontrol/RemoteControl.js@@@ -1,9 -1,11 +1,12 @@@/* global APP, config */- const logger = require("jitsi-meet-logger").getLogger(__filename);- import Controller from "./Controller";- import Receiver from "./Receiver";- import {EVENT_TYPES, DISCO_REMOTE_CONTROL_FEATURE}- from "../../service/remotecontrol/Constants";+++ import { DISCO_REMOTE_CONTROL_FEATURE }+ from '../../service/remotecontrol/Constants';++ import Controller from './Controller';+ import Receiver from './Receiver';++ const logger = require('jitsi-meet-logger').getLogger(__filename);/*** Implements the remote control functionality.@@@ -80,9 -43,10 +44,9 @@@ class RemoteControl* the user supports remote control and with false if not.*/checkUserRemoteControlSupport(user) {- return user.getFeatures().then(features =>- features.has(DISCO_REMOTE_CONTROL_FEATURE), () => false- );+ return user.getFeatures().then(+ features => features.has(DISCO_REMOTE_CONTROL_FEATURE),- () => false- );++ () => false);}}diff --cc modules/remotecontrol/RemoteControlParticipant.jsindex 0f283ba9ff,48bf779743..410cd4a0ba--- a/modules/remotecontrol/RemoteControlParticipant.js+++ b/modules/remotecontrol/RemoteControlParticipant.js@@@ -1,7 -1,7 +1,10 @@@/* global APP */++++import {++ REMOTE_CONTROL_EVENT_NAME++} from "../../service/remotecontrol/Constants";++const logger = require("jitsi-meet-logger").getLogger(__filename);--import {REMOTE_CONTROL_EVENT_TYPE}-- from "../../service/remotecontrol/Constants";export default class RemoteControlParticipant {/**@@@ -26,15 -26,17 +29,20 @@@*/_sendRemoteControlEvent(to, event, onDataChannelFail = () => {}) {if(!this.enabled || !to) {-- logger.warn("Remote control: Skip sending remote control event."-- + " Params:", this.enable, to);++ logger.warn(++ "Remote control: Skip sending remote control event. Params:",++ this.enable,++ to);return;}try{- APP.conference.sendEndpointMessage(to,- {type: REMOTE_CONTROL_EVENT_TYPE, event});+ APP.conference.sendEndpointMessage(to, {- name: REMOTE_CONTROL_EVENT_TYPE,++ name: REMOTE_CONTROL_EVENT_NAME,+ ...event+ });} catch (e) {-- logger.error("Failed to send EndpointMessage via the datachannels",++ logger.error(++ "Failed to send EndpointMessage via the datachannels",e);onDataChannelFail(e);}diff --cc modules/transport/Transport.jsindex 0000000000,0b7b395e1a..678ad3277fmode 000000,100644..100644--- a/modules/transport/Transport.js+++ b/modules/transport/Transport.js@@@ -1,0 -1,265 +1,265 @@@+ import {+ MESSAGE_TYPE_EVENT,- MESSAGE_TYPE_RESPONSE,- MESSAGE_TYPE_REQUEST++ MESSAGE_TYPE_REQUEST,++ MESSAGE_TYPE_RESPONSE+ } from './constants';++ /**+ * Stores the currnet transport backend that have to be used. Also implements+ * request/response mechanism.+ */+ export default class Transport {+ /**+ * Creates new instance.+ *+ * @param {Object} options - Optional parameters for configuration of the+ * transport backend.+ */+ constructor({ backend } = {}) {++ /**++ * Maps an event name and listener that have been added to the Transport++ * instance.++ *++ * @type {Map<string, Function>}++ */++ this._listeners = new Map();+++ /**+ * The request ID counter used for the id property of the request. This+ * property is used to match the responses with the request.+ *+ * @type {number}+ */+ this._requestID = 0;++ /**+ * Maps an IDs of the requests and handlers that will process the+ * responses of those requests.+ *+ * @type {Map<number, Function>}+ */+ this._responseHandlers = new Map();+- /**- * Maps an event name and listener that have been added to the Transport- * instance.- *- * @type {Map<string, Function>}- */- this._listeners = new Map();-+ /**+ * A set with the events and requests that were received but not+ * processed by any listener. They are later passed on every new+ * listener until they are processed.+ *+ * @type {Set<Object>}+ */+ this._unprocessedMessages = new Set();++ /**+ * Alias.+ */+ this.addListener = this.on;++ if (backend) {+ this.setBackend(backend);+ }+ }++ /**+ * Disposes the current transport backend.+ *+ * @returns {void}+ */+ _disposeBackend() {+ if (this._backend) {+ this._backend.dispose();+ this._backend = null;+ }+ }++ /**+ * Handles incoming messages from the transport backend.+ *+ * @param {Object} message - The message.+ * @returns {void}+ */+ _onMessageReceived(message) {+ if (message.type === MESSAGE_TYPE_RESPONSE) {+ const handler = this._responseHandlers.get(message.id);++ if (handler) {+ handler(message);+ this._responseHandlers.delete(message.id);+ }+ } else if (message.type === MESSAGE_TYPE_REQUEST) {+ this.emit('request', message.data, (result, error) => {+ this._backend.send({+ type: MESSAGE_TYPE_RESPONSE,+ error,+ id: message.id,+ result+ });+ });+ } else {+ this.emit('event', message.data);+ }+ }++ /**+ * Disposes the allocated resources.+ *+ * @returns {void}+ */+ dispose() {+ this._responseHandlers.clear();+ this._unprocessedMessages.clear();+ this.removeAllListeners();+ this._disposeBackend();+ }++ /**+ * Calls each of the listeners registered for the event named eventName, in+ * the order they were registered, passing the supplied arguments to each.+ *+ * @param {string} eventName - The name of the event.+ * @returns {boolean} True if the event has been processed by any listener,+ * false otherwise.+ */+ emit(eventName, ...args) {+ const listenersForEvent = this._listeners.get(eventName);+ let isProcessed = false;++ if (listenersForEvent && listenersForEvent.size) {+ listenersForEvent.forEach(listener => {+ isProcessed = listener(...args) || isProcessed;+ });+ }++ if (!isProcessed) {+ this._unprocessedMessages.add(args);+ }++ return isProcessed;+ }++ /**+ * Adds the listener function to the listeners collection for the event+ * named eventName.+ *+ * @param {string} eventName - The name of the event.+ * @param {Function} listener - The listener that will be added.+ * @returns {Transport} References to the instance of Transport class, so+ * that calls can be chained.+ */+ on(eventName, listener) {+ let listenersForEvent = this._listeners.get(eventName);++ if (!listenersForEvent) {+ listenersForEvent = new Set();+ this._listeners.set(eventName, listenersForEvent);+ }++ listenersForEvent.add(listener);++ this._unprocessedMessages.forEach(args => {+ if (listener(...args)) {+ this._unprocessedMessages.delete(args);+ }+ });++ return this;+ }++ /**+ * Removes all listeners, or those of the specified eventName.+ *+ * @param {string} [eventName] - The name of the event. If this parameter is+ * not specified all listeners will be removed.+ * @returns {Transport} References to the instance of Transport class, so+ * that calls can be chained.+ */+ removeAllListeners(eventName) {+ if (eventName) {+ this._listeners.delete(eventName);+ } else {+ this._listeners.clear();+ }++ return this;+ }++ /**+ * Removes the listener function from the listeners collection for the event+ * named eventName.+ *+ * @param {string} eventName - The name of the event.+ * @param {Function} listener - The listener that will be removed.+ * @returns {Transport} References to the instance of Transport class, so+ * that calls can be chained.+ */+ removeListener(eventName, listener) {+ const listenersForEvent = this._listeners.get(eventName);++ if (listenersForEvent) {+ listenersForEvent.delete(listener);+ }++ return this;+ }++ /**+ * Sends the passed event.+ *+ * @param {Object} event - The event to be sent.+ * @returns {void}+ */+ sendEvent(event = {}) {+ if (this._backend) {+ this._backend.send({+ type: MESSAGE_TYPE_EVENT,+ data: event+ });+ }+ }++ /**+ * Sending request.+ *+ * @param {Object} request - The request to be sent.+ * @returns {Promise}+ */+ sendRequest(request) {+ if (!this._backend) {+ return Promise.reject(new Error('No transport backend defined!'));+ }++ this._requestID++;++ const id = this._requestID;++ return new Promise((resolve, reject) => {+ this._responseHandlers.set(id, ({ error, result }) => {+ if (result) {+ resolve(result);+ } else if (error) {+ reject(error);+ } else { // no response+ reject(new Error('Unexpected response format!'));+ }+ });++ this._backend.send({+ type: MESSAGE_TYPE_REQUEST,+ data: request,+ id+ });+ });+ }++ /**+ * Changes the current backend transport.+ *+ * @param {Object} backend - The new transport backend that will be used.+ * @returns {void}+ */+ setBackend(backend) {+ this._disposeBackend();++ this._backend = backend;+ this._backend.setReceiveCallback(this._onMessageReceived.bind(this));+ }+ }diff --cc modules/transport/constants.jsindex 0000000000,02a125a26c..54fb87cf0bmode 000000,100644..100644--- a/modules/transport/constants.js+++ b/modules/transport/constants.js@@@ -1,0 -1,20 +1,20 @@@+ /**+ * The message type for events.+ *+ * @type {string}+ */+ export const MESSAGE_TYPE_EVENT = 'event';++ /**- * The message type for responses.++ * The message type for requests.+ *+ * @type {string}+ */-export const MESSAGE_TYPE_RESPONSE = 'response';++export const MESSAGE_TYPE_REQUEST = 'request';++ /**- * The message type for requests.++ * The message type for responses.+ *+ * @type {string}+ */-export const MESSAGE_TYPE_REQUEST = 'request';++export const MESSAGE_TYPE_RESPONSE = 'response';diff --cc modules/transport/index.jsindex 0000000000,4fefa93aa3..53db0877efmode 000000,100644..100644--- a/modules/transport/index.js+++ b/modules/transport/index.js@@@ -1,0 -1,59 +1,57 @@@+ // FIXME: change to '../API' when we update to webpack2. If we do this now all+ // files from API modules will be included in external_api.js.+ import { API_ID } from '../API/constants';+ import { getJitsiMeetGlobalNS } from '../util/helpers';+-import Transport from './Transport';+ import PostMessageTransportBackend from './PostMessageTransportBackend';++import Transport from './Transport';++ export {- Transport,- PostMessageTransportBackend++ PostMessageTransportBackend,++ Transport+ };++ /**+ * Option for the default low level transport.+ *+ * @type {Object}+ */+ const postisOptions = {};++ if (typeof API_ID === 'number') {+ postisOptions.scope = `jitsi_meet_external_api_${API_ID}`;+ }++ /**+ * The instance of Transport class that will be used by Jitsi Meet.+ *+ * @type {Transport}+ */+ let transport;++ /**+ * Returns the instance of Transport class that will be used by Jitsi Meet.+ *+ * @returns {Transport}+ */+ export function getJitsiMeetTransport() {+ if (!transport) {+ transport = new Transport({+ backend: new PostMessageTransportBackend({+ enableLegacyFormat: true,+ postisOptions+ })+ });+ }++ return transport;+ }++ /**+ * Sets the transport to passed transport.+ *+ * @param {Object} externalTransportBackend - The new transport.+ * @returns {void}+ */-getJitsiMeetGlobalNS().setExternalTransportBackend = function(- externalTransportBackend) {++getJitsiMeetGlobalNS().setExternalTransportBackend = externalTransportBackend =>+ transport.setBackend(externalTransportBackend);-};diff --cc modules/util/helpers.jsindex 55408151a1,e824bb7865..39d173d49d--- a/modules/util/helpers.js+++ b/modules/util/helpers.js@@@ -16,35 -16,35 +16,6 @@@ export function createDeferred()return deferred;}--/**-- * Reload page.-- */--export function reload() {-- window.location.reload();--}----/**-- * Redirects to a specific new URL by replacing the current location (in the-- * history).-- *-- * @param {string} url the URL pointing to the location where the user should-- * be redirected to.-- */--export function replace(url) {-- window.location.replace(url);--}----/**-- * Prints the error and reports it to the global error handler.-- *-- * @param e {Error} the error-- * @param msg {string} [optional] the message printed in addition to the error-- */--export function reportError(e, msg = "") {-- logger.error(msg, e);-- window.onerror && window.onerror(msg, null, null, null, e);--}--/*** Creates a debounced function that delays invoking func until after wait* milliseconds have elapsed since the last time the debounced function was@@@ -74,3 -74,21 +45,49 @@@ export function debounce(fn, wait = 0,}};}++ /**+ * Returns the namespace for all global variables, functions, etc that we need.+ *+ * @returns {Object} the namespace.+ *- * NOTE: After reactifying everything this should be the only place where- * we store everything that needs to be global (for some reason).++ * NOTE: After React-ifying everything this should be the only global.+ */+ export function getJitsiMeetGlobalNS() {- if(!window.JitsiMeetJS) {- window.JitsiMeetJS = { };++ if (!window.JitsiMeetJS) {++ window.JitsiMeetJS = {};+ }- if(!window.JitsiMeetJS.app) {- window.JitsiMeetJS.app = { };++ if (!window.JitsiMeetJS.app) {++ window.JitsiMeetJS.app = {};+ }+ return window.JitsiMeetJS.app;+ }++++/**++ * Reload page.++ */++export function reload() {++ window.location.reload();++}++++/**++ * Redirects to a specific new URL by replacing the current location (in the++ * history).++ *++ * @param {string} url the URL pointing to the location where the user should++ * be redirected to.++ */++export function replace(url) {++ window.location.replace(url);++}++++/**++ * Prints the error and reports it to the global error handler.++ *++ * @param e {Error} the error++ * @param msg {string} [optional] the message printed in addition to the error++ */++export function reportError(e, msg = "") {++ logger.error(msg, e);++ window.onerror && window.onerror(msg, null, null, null, e);++}diff --cc service/remotecontrol/Constants.jsindex c856185ac8,c856185ac8..e295614c61--- a/service/remotecontrol/Constants.js+++ b/service/remotecontrol/Constants.js@@@ -37,7 -37,7 +37,7 @@@ export const PERMISSIONS_ACTIONS =/*** The type of remote control events sent trough the API module.*/--export const REMOTE_CONTROL_EVENT_TYPE = "remote-control-event";++export const REMOTE_CONTROL_EVENT_NAME = "remote-control-event";/*** The remote control event.