2017-05-03 23:57:52 +00:00
|
|
|
/* @flow */
|
|
|
|
|
|
|
|
import { getLogger } from 'jitsi-meet-logger';
|
2017-04-27 20:21:01 +00:00
|
|
|
|
2017-01-23 18:07:08 +00:00
|
|
|
import * as JitsiMeetConferenceEvents from '../../ConferenceEvents';
|
2017-05-03 23:57:52 +00:00
|
|
|
import {
|
|
|
|
openRemoteControlAuthorizationDialog
|
|
|
|
} from '../../react/features/remote-control';
|
2017-04-17 19:59:24 +00:00
|
|
|
import {
|
|
|
|
DISCO_REMOTE_CONTROL_FEATURE,
|
|
|
|
EVENT_TYPES,
|
|
|
|
PERMISSIONS_ACTIONS,
|
2017-05-02 22:39:36 +00:00
|
|
|
REMOTE_CONTROL_EVENT_NAME
|
2017-05-01 22:11:26 +00:00
|
|
|
} from '../../service/remotecontrol/Constants';
|
2017-04-28 20:24:20 +00:00
|
|
|
import { getJitsiMeetTransport } from '../transport';
|
2017-04-17 19:59:24 +00:00
|
|
|
|
2017-05-01 22:11:26 +00:00
|
|
|
import RemoteControlParticipant from './RemoteControlParticipant';
|
2016-12-20 22:15:13 +00:00
|
|
|
|
2017-05-03 23:57:52 +00:00
|
|
|
declare var APP: Object;
|
|
|
|
declare var config: Object;
|
|
|
|
declare var interfaceConfig: Object;
|
|
|
|
declare var JitsiMeetJS: Object;
|
|
|
|
|
2016-12-09 23:15:04 +00:00
|
|
|
const ConferenceEvents = JitsiMeetJS.events.conference;
|
2017-05-03 23:57:52 +00:00
|
|
|
const logger = getLogger(__filename);
|
2016-12-09 23:15:04 +00:00
|
|
|
|
2017-04-28 20:24:20 +00:00
|
|
|
/**
|
|
|
|
* The transport instance used for communication with external apps.
|
|
|
|
*
|
|
|
|
* @type {Transport}
|
|
|
|
*/
|
|
|
|
const transport = getJitsiMeetTransport();
|
2016-12-09 23:15:04 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This class represents the receiver party for a remote controller session.
|
|
|
|
* It handles "remote-control-event" events and sends them to the
|
|
|
|
* API module. From there the events can be received from wrapper application
|
|
|
|
* and executed.
|
|
|
|
*/
|
2016-12-28 00:46:55 +00:00
|
|
|
export default class Receiver extends RemoteControlParticipant {
|
2017-05-03 23:57:52 +00:00
|
|
|
_controller: ?string;
|
|
|
|
_enabled: boolean;
|
|
|
|
_hangupListener: Function;
|
|
|
|
_remoteControlEventsListener: Function;
|
|
|
|
_userLeftListener: Function;
|
|
|
|
|
2016-12-09 23:15:04 +00:00
|
|
|
/**
|
|
|
|
* Creates new instance.
|
|
|
|
*/
|
2016-12-20 22:15:13 +00:00
|
|
|
constructor() {
|
2016-12-28 00:46:55 +00:00
|
|
|
super();
|
2017-05-03 23:57:52 +00:00
|
|
|
this._controller = null;
|
2016-12-28 00:46:55 +00:00
|
|
|
this._remoteControlEventsListener
|
|
|
|
= this._onRemoteControlEvent.bind(this);
|
2017-01-06 01:18:07 +00:00
|
|
|
this._userLeftListener = this._onUserLeft.bind(this);
|
2017-01-23 18:07:08 +00:00
|
|
|
this._hangupListener = this._onHangup.bind(this);
|
2017-05-03 23:57:52 +00:00
|
|
|
|
2017-04-17 19:59:24 +00:00
|
|
|
// We expect here that even if we receive the supported event earlier
|
|
|
|
// it will be cached and we'll receive it.
|
2017-05-01 22:11:26 +00:00
|
|
|
transport.on('event', event => {
|
2017-05-02 22:39:36 +00:00
|
|
|
if (event.name === REMOTE_CONTROL_EVENT_NAME) {
|
2017-04-27 20:21:01 +00:00
|
|
|
this._onRemoteControlAPIEvent(event);
|
2017-04-17 19:59:24 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
});
|
2016-12-20 22:15:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-05-03 23:57:52 +00:00
|
|
|
* Enables / Disables the remote control.
|
|
|
|
*
|
|
|
|
* @param {boolean} enabled - The new state.
|
|
|
|
* @returns {void}
|
2016-12-20 22:15:13 +00:00
|
|
|
*/
|
2017-05-03 23:57:52 +00:00
|
|
|
_enable(enabled: boolean) {
|
|
|
|
if (this._enabled === enabled) {
|
2017-01-23 21:01:54 +00:00
|
|
|
return;
|
2017-01-06 01:18:07 +00:00
|
|
|
}
|
2017-05-03 23:57:52 +00:00
|
|
|
this._enabled = enabled;
|
|
|
|
if (enabled === true) {
|
|
|
|
logger.log('Remote control receiver enabled.');
|
|
|
|
|
2016-12-20 22:15:13 +00:00
|
|
|
// Announce remote control support.
|
|
|
|
APP.connection.addFeature(DISCO_REMOTE_CONTROL_FEATURE, true);
|
|
|
|
APP.conference.addConferenceListener(
|
|
|
|
ConferenceEvents.ENDPOINT_MESSAGE_RECEIVED,
|
2016-12-28 00:46:55 +00:00
|
|
|
this._remoteControlEventsListener);
|
2017-01-23 18:07:08 +00:00
|
|
|
APP.conference.addListener(JitsiMeetConferenceEvents.BEFORE_HANGUP,
|
|
|
|
this._hangupListener);
|
2017-01-06 01:18:07 +00:00
|
|
|
} else {
|
2017-05-03 23:57:52 +00:00
|
|
|
logger.log('Remote control receiver disabled.');
|
2017-01-06 01:18:07 +00:00
|
|
|
this._stop(true);
|
|
|
|
APP.connection.removeFeature(DISCO_REMOTE_CONTROL_FEATURE);
|
|
|
|
APP.conference.removeConferenceListener(
|
|
|
|
ConferenceEvents.ENDPOINT_MESSAGE_RECEIVED,
|
|
|
|
this._remoteControlEventsListener);
|
2017-01-23 18:07:08 +00:00
|
|
|
APP.conference.removeListener(
|
|
|
|
JitsiMeetConferenceEvents.BEFORE_HANGUP,
|
|
|
|
this._hangupListener);
|
2016-12-20 22:15:13 +00:00
|
|
|
}
|
2016-12-09 23:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes the listener for ConferenceEvents.ENDPOINT_MESSAGE_RECEIVED
|
2017-01-06 01:18:07 +00:00
|
|
|
* events. Sends stop message to the wrapper application. Optionally
|
|
|
|
* displays dialog for informing the user that remote control session
|
|
|
|
* ended.
|
2017-05-03 23:57:52 +00:00
|
|
|
*
|
|
|
|
* @param {boolean} [dontShowDialog] - If true the dialog won't be
|
|
|
|
* displayed.
|
|
|
|
* @returns {void}
|
2017-01-06 01:18:07 +00:00
|
|
|
*/
|
2017-05-03 23:57:52 +00:00
|
|
|
_stop(dontShowDialog: boolean = false) {
|
|
|
|
if (!this._controller) {
|
2017-01-06 01:18:07 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-05-03 23:57:52 +00:00
|
|
|
logger.log('Remote control receiver stop.');
|
|
|
|
this._controller = null;
|
2017-01-06 01:18:07 +00:00
|
|
|
APP.conference.removeConferenceListener(ConferenceEvents.USER_LEFT,
|
|
|
|
this._userLeftListener);
|
2017-05-03 23:57:52 +00:00
|
|
|
if (this.remoteControlExternalAuth) {
|
|
|
|
transport.sendEvent({
|
|
|
|
name: REMOTE_CONTROL_EVENT_NAME,
|
|
|
|
type: EVENT_TYPES.stop
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (!dontShowDialog) {
|
2017-01-06 01:18:07 +00:00
|
|
|
APP.UI.messageHandler.openMessageDialog(
|
2017-05-03 23:57:52 +00:00
|
|
|
'dialog.remoteControlTitle',
|
|
|
|
'dialog.remoteControlStopMessage'
|
2017-01-06 01:18:07 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-05-03 23:57:52 +00:00
|
|
|
* Calls this._stop() and sends stop message to the controller participant.
|
|
|
|
*
|
|
|
|
* @returns {void}
|
2016-12-09 23:15:04 +00:00
|
|
|
*/
|
|
|
|
stop() {
|
2017-05-03 23:57:52 +00:00
|
|
|
if (!this._controller) {
|
2017-01-06 01:18:07 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-05-03 23:57:52 +00:00
|
|
|
this.sendRemoteControlEvent(this._controller, {
|
2016-12-28 00:46:55 +00:00
|
|
|
type: EVENT_TYPES.stop
|
2017-01-06 01:18:07 +00:00
|
|
|
});
|
|
|
|
this._stop();
|
2016-12-09 23:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-01-18 00:16:18 +00:00
|
|
|
* Listens for data channel EndpointMessage events. Handles only events of
|
|
|
|
* type remote control. Sends "remote-control-event" events to the API
|
|
|
|
* module.
|
2017-05-03 23:57:52 +00:00
|
|
|
*
|
|
|
|
* @param {JitsiParticipant} participant - The controller participant.
|
|
|
|
* @param {Object} event - EndpointMessage event from the data channels.
|
|
|
|
* @param {string} event.name - The function process only events with
|
|
|
|
* name REMOTE_CONTROL_EVENT_NAME.
|
|
|
|
* @returns {void}
|
2016-12-09 23:15:04 +00:00
|
|
|
*/
|
2017-05-03 23:57:52 +00:00
|
|
|
_onRemoteControlEvent(participant: Object, event: Object) {
|
2017-05-02 22:39:36 +00:00
|
|
|
if (event.name !== REMOTE_CONTROL_EVENT_NAME) {
|
2017-05-01 22:11:26 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const remoteControlEvent = Object.assign({}, event);
|
|
|
|
|
2017-05-03 23:57:52 +00:00
|
|
|
if (this._enabled) {
|
|
|
|
if (this._controller === null
|
2017-05-02 22:39:36 +00:00
|
|
|
&& event.type === EVENT_TYPES.permissions
|
|
|
|
&& event.action === PERMISSIONS_ACTIONS.request) {
|
2017-05-03 23:57:52 +00:00
|
|
|
const userId = participant.getId();
|
|
|
|
|
|
|
|
if (!config.remoteControlExternalAuth) {
|
|
|
|
APP.store.dispatch(
|
|
|
|
openRemoteControlAuthorizationDialog(userId));
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-04-17 19:59:24 +00:00
|
|
|
// FIXME: Maybe use transport.sendRequest in this case???
|
2017-05-03 23:57:52 +00:00
|
|
|
remoteControlEvent.userId = userId;
|
2016-12-28 00:46:55 +00:00
|
|
|
remoteControlEvent.userJID = participant.getJid();
|
2017-01-06 01:18:07 +00:00
|
|
|
remoteControlEvent.displayName = participant.getDisplayName()
|
|
|
|
|| interfaceConfig.DEFAULT_REMOTE_DISPLAY_NAME;
|
|
|
|
remoteControlEvent.screenSharing
|
|
|
|
= APP.conference.isSharingScreen;
|
2017-05-03 23:57:52 +00:00
|
|
|
} else if (this._controller !== participant.getId()) {
|
2016-12-28 00:46:55 +00:00
|
|
|
return;
|
2017-05-01 22:11:26 +00:00
|
|
|
} else if (event.type === EVENT_TYPES.stop) {
|
2017-01-06 01:18:07 +00:00
|
|
|
this._stop();
|
2017-05-03 23:57:52 +00:00
|
|
|
|
2017-01-06 01:18:07 +00:00
|
|
|
return;
|
2016-12-28 00:46:55 +00:00
|
|
|
}
|
2017-05-01 22:11:26 +00:00
|
|
|
transport.sendEvent(remoteControlEvent);
|
|
|
|
} else {
|
2017-05-03 23:57:52 +00:00
|
|
|
logger.log('Remote control event is ignored because remote '
|
|
|
|
+ 'control is disabled', event);
|
2016-12-28 00:46:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-04-17 19:59:24 +00:00
|
|
|
* Handles remote control permission events.
|
2017-05-03 23:57:52 +00:00
|
|
|
*
|
|
|
|
* @param {string} userId - The user id of the participant related to the
|
2016-12-28 00:46:55 +00:00
|
|
|
* event.
|
2017-05-03 23:57:52 +00:00
|
|
|
* @param {PERMISSIONS_ACTIONS} action - The action related to the event.
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
_onRemoteControlPermissionsEvent(userId: string, action: string) {
|
|
|
|
switch (action) {
|
|
|
|
case PERMISSIONS_ACTIONS.grant:
|
|
|
|
this.grant(userId);
|
|
|
|
break;
|
|
|
|
case PERMISSIONS_ACTIONS.deny:
|
|
|
|
this.deny(userId);
|
|
|
|
break;
|
|
|
|
case PERMISSIONS_ACTIONS.error:
|
|
|
|
this.sendRemoteControlEvent(userId, {
|
|
|
|
type: EVENT_TYPES.permissions,
|
|
|
|
action
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
|
|
|
|
// Unknown action. Ignore.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Denies remote control access for user associated with the passed user id.
|
|
|
|
*
|
|
|
|
* @param {string} userId - The id associated with the user who sent the
|
|
|
|
* request for remote control authorization.
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
deny(userId: string) {
|
|
|
|
this.sendRemoteControlEvent(userId, {
|
|
|
|
type: EVENT_TYPES.permissions,
|
|
|
|
action: PERMISSIONS_ACTIONS.deny
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Grants remote control access to user associated with the passed user id.
|
|
|
|
*
|
|
|
|
* @param {string} userId - The id associated with the user who sent the
|
|
|
|
* request for remote control authorization.
|
|
|
|
* @returns {void}
|
2016-12-28 00:46:55 +00:00
|
|
|
*/
|
2017-05-03 23:57:52 +00:00
|
|
|
grant(userId: string) {
|
|
|
|
APP.conference.addConferenceListener(ConferenceEvents.USER_LEFT,
|
|
|
|
this._userLeftListener);
|
|
|
|
this._controller = userId;
|
|
|
|
logger.log(`Remote control permissions granted to: ${userId}`);
|
|
|
|
if (APP.conference.isSharingScreen) {
|
|
|
|
this.sendRemoteControlEvent(userId, {
|
|
|
|
type: EVENT_TYPES.permissions,
|
|
|
|
action: PERMISSIONS_ACTIONS.grant
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
APP.conference.toggleScreenSharing();
|
|
|
|
APP.conference.screenSharingPromise.then(() => {
|
|
|
|
if (APP.conference.isSharingScreen) {
|
|
|
|
this.sendRemoteControlEvent(userId, {
|
|
|
|
type: EVENT_TYPES.permissions,
|
|
|
|
action: PERMISSIONS_ACTIONS.grant
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
this.sendRemoteControlEvent(userId, {
|
2017-01-06 01:18:07 +00:00
|
|
|
type: EVENT_TYPES.permissions,
|
|
|
|
action: PERMISSIONS_ACTIONS.error
|
|
|
|
});
|
2017-05-03 23:57:52 +00:00
|
|
|
}
|
|
|
|
}).catch(() => {
|
|
|
|
this.sendRemoteControlEvent(userId, {
|
|
|
|
type: EVENT_TYPES.permissions,
|
|
|
|
action: PERMISSIONS_ACTIONS.error
|
2017-01-06 01:18:07 +00:00
|
|
|
});
|
2017-05-03 23:57:52 +00:00
|
|
|
});
|
2016-12-28 00:46:55 +00:00
|
|
|
}
|
2016-12-09 23:15:04 +00:00
|
|
|
}
|
2017-01-06 01:18:07 +00:00
|
|
|
|
2017-04-17 19:59:24 +00:00
|
|
|
/**
|
|
|
|
* Handles remote control events from the external app. Currently only
|
2017-05-03 23:57:52 +00:00
|
|
|
* events with type = EVENT_TYPES.supported or EVENT_TYPES.permissions.
|
|
|
|
*
|
|
|
|
* @param {RemoteControlEvent} event - The remote control event.
|
|
|
|
* @returns {void}
|
2017-04-17 19:59:24 +00:00
|
|
|
*/
|
2017-05-03 23:57:52 +00:00
|
|
|
_onRemoteControlAPIEvent(event: Object) {
|
|
|
|
switch (event.type) {
|
2017-04-27 20:21:01 +00:00
|
|
|
case EVENT_TYPES.permissions:
|
|
|
|
this._onRemoteControlPermissionsEvent(event.userId, event.action);
|
|
|
|
break;
|
2017-05-02 22:39:36 +00:00
|
|
|
case EVENT_TYPES.supported:
|
|
|
|
this._onRemoteControlSupported();
|
|
|
|
break;
|
2017-04-17 19:59:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles events for support for executing remote control events into
|
|
|
|
* the wrapper application.
|
2017-05-03 23:57:52 +00:00
|
|
|
*
|
|
|
|
* @returns {void}
|
2017-04-17 19:59:24 +00:00
|
|
|
*/
|
|
|
|
_onRemoteControlSupported() {
|
2017-05-03 23:57:52 +00:00
|
|
|
logger.log('Remote Control supported.');
|
2017-05-02 22:39:36 +00:00
|
|
|
if (config.disableRemoteControl) {
|
2017-05-03 23:57:52 +00:00
|
|
|
logger.log('Remote Control disabled.');
|
2017-05-02 22:39:36 +00:00
|
|
|
} else {
|
|
|
|
this._enable(true);
|
2017-04-17 19:59:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-06 01:18:07 +00:00
|
|
|
/**
|
|
|
|
* Calls the stop method if the other side have left.
|
2017-05-03 23:57:52 +00:00
|
|
|
*
|
|
|
|
* @param {string} id - The user id for the participant that have left.
|
|
|
|
* @returns {void}
|
2017-01-06 01:18:07 +00:00
|
|
|
*/
|
2017-05-03 23:57:52 +00:00
|
|
|
_onUserLeft(id: string) {
|
|
|
|
if (this._controller === id) {
|
2017-01-06 01:18:07 +00:00
|
|
|
this._stop();
|
|
|
|
}
|
|
|
|
}
|
2017-01-23 18:07:08 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles hangup events. Disables the receiver.
|
2017-05-03 23:57:52 +00:00
|
|
|
*
|
|
|
|
* @returns {void}
|
2017-01-23 18:07:08 +00:00
|
|
|
*/
|
|
|
|
_onHangup() {
|
2017-04-17 19:59:24 +00:00
|
|
|
this._enable(false);
|
2017-01-23 18:07:08 +00:00
|
|
|
}
|
2016-12-09 23:15:04 +00:00
|
|
|
}
|