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-10-10 23:31:40 +00:00
|
|
|
import {
|
|
|
|
JitsiConferenceEvents
|
|
|
|
} from '../../react/features/base/lib-jitsi-meet';
|
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,
|
2017-06-22 21:28:57 +00:00
|
|
|
EVENTS,
|
2017-04-17 19:59:24 +00:00
|
|
|
PERMISSIONS_ACTIONS,
|
2017-06-22 21:28:57 +00:00
|
|
|
REMOTE_CONTROL_MESSAGE_NAME,
|
|
|
|
REQUESTS
|
2017-05-01 22:11:26 +00:00
|
|
|
} from '../../service/remotecontrol/Constants';
|
2017-08-17 16:43:22 +00:00
|
|
|
import * as RemoteControlEvents
|
|
|
|
from '../../service/remotecontrol/RemoteControlEvents';
|
2017-10-02 23:08:07 +00:00
|
|
|
import { Transport, PostMessageTransportBackend } 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;
|
|
|
|
|
|
|
|
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}
|
|
|
|
*/
|
2017-07-25 15:40:57 +00:00
|
|
|
const transport = new Transport({
|
|
|
|
backend: new PostMessageTransportBackend({
|
|
|
|
postisOptions: { scope: 'jitsi-remote-control' }
|
|
|
|
})
|
|
|
|
});
|
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
|
2017-06-22 21:28:57 +00:00
|
|
|
= this._onRemoteControlMessage.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-06-22 21:28:57 +00:00
|
|
|
if (event.name === REMOTE_CONTROL_MESSAGE_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(
|
2017-10-10 23:31:40 +00:00
|
|
|
JitsiConferenceEvents.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(
|
2017-10-10 23:31:40 +00:00
|
|
|
JitsiConferenceEvents.ENDPOINT_MESSAGE_RECEIVED,
|
2017-01-06 01:18:07 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-10-10 23:31:40 +00:00
|
|
|
* Removes the listener for JitsiConferenceEvents.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
|
|
|
*
|
2017-07-10 03:01:48 +00:00
|
|
|
* @param {boolean} [dontNotify] - If true - a notification about stopping
|
|
|
|
* the remote control won't be displayed.
|
2017-05-03 23:57:52 +00:00
|
|
|
* @returns {void}
|
2017-01-06 01:18:07 +00:00
|
|
|
*/
|
2017-07-10 03:01:48 +00:00
|
|
|
_stop(dontNotify: boolean = false) {
|
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
|
|
|
logger.log('Remote control receiver stop.');
|
|
|
|
this._controller = null;
|
2017-10-10 23:31:40 +00:00
|
|
|
APP.conference.removeConferenceListener(
|
|
|
|
JitsiConferenceEvents.USER_LEFT,
|
2017-01-06 01:18:07 +00:00
|
|
|
this._userLeftListener);
|
2017-06-22 21:28:57 +00:00
|
|
|
transport.sendEvent({
|
|
|
|
name: REMOTE_CONTROL_MESSAGE_NAME,
|
|
|
|
type: EVENTS.stop
|
|
|
|
});
|
2017-08-17 16:43:22 +00:00
|
|
|
this.emit(RemoteControlEvents.ACTIVE_CHANGED, false);
|
2017-07-10 03:01:48 +00:00
|
|
|
if (!dontNotify) {
|
|
|
|
APP.UI.messageHandler.notify(
|
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-06-22 21:28:57 +00:00
|
|
|
this.sendRemoteControlEndpointMessage(this._controller, {
|
|
|
|
type: EVENTS.stop
|
2017-01-06 01:18:07 +00:00
|
|
|
});
|
|
|
|
this._stop();
|
2016-12-09 23:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-06-22 21:28:57 +00:00
|
|
|
* Listens for data channel EndpointMessage. Handles only remote control
|
|
|
|
* messages. Sends the remote control messages to the external app that
|
|
|
|
* will execute them.
|
2017-05-03 23:57:52 +00:00
|
|
|
*
|
|
|
|
* @param {JitsiParticipant} participant - The controller participant.
|
2017-06-22 21:28:57 +00:00
|
|
|
* @param {Object} message - EndpointMessage from the data channels.
|
|
|
|
* @param {string} message.name - The function processes only messages with
|
|
|
|
* name REMOTE_CONTROL_MESSAGE_NAME.
|
2017-05-03 23:57:52 +00:00
|
|
|
* @returns {void}
|
2016-12-09 23:15:04 +00:00
|
|
|
*/
|
2017-06-22 21:28:57 +00:00
|
|
|
_onRemoteControlMessage(participant: Object, message: Object) {
|
|
|
|
if (message.name !== REMOTE_CONTROL_MESSAGE_NAME) {
|
2017-05-01 22:11:26 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-05-03 23:57:52 +00:00
|
|
|
if (this._enabled) {
|
|
|
|
if (this._controller === null
|
2017-06-22 21:28:57 +00:00
|
|
|
&& message.type === EVENTS.permissions
|
|
|
|
&& message.action === PERMISSIONS_ACTIONS.request) {
|
2017-05-03 23:57:52 +00:00
|
|
|
const userId = participant.getId();
|
|
|
|
|
2017-08-17 16:43:22 +00:00
|
|
|
this.emit(RemoteControlEvents.ACTIVE_CHANGED, true);
|
2017-06-22 21:28:57 +00:00
|
|
|
APP.store.dispatch(
|
|
|
|
openRemoteControlAuthorizationDialog(userId));
|
|
|
|
} else if (this._controller === participant.getId()) {
|
|
|
|
if (message.type === EVENTS.stop) {
|
|
|
|
this._stop();
|
|
|
|
} else { // forward the message
|
|
|
|
transport.sendEvent(message);
|
2017-05-03 23:57:52 +00:00
|
|
|
}
|
2017-06-22 21:28:57 +00:00
|
|
|
} // else ignore
|
2017-05-01 22:11:26 +00:00
|
|
|
} else {
|
2017-06-22 21:28:57 +00:00
|
|
|
logger.log('Remote control message is ignored because remote '
|
|
|
|
+ 'control is disabled', message);
|
2017-05-03 23:57:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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) {
|
2017-08-17 16:43:22 +00:00
|
|
|
this.emit(RemoteControlEvents.ACTIVE_CHANGED, false);
|
2017-06-22 21:28:57 +00:00
|
|
|
this.sendRemoteControlEndpointMessage(userId, {
|
|
|
|
type: EVENTS.permissions,
|
2017-05-03 23:57:52 +00:00
|
|
|
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) {
|
2017-10-10 23:31:40 +00:00
|
|
|
APP.conference.addConferenceListener(JitsiConferenceEvents.USER_LEFT,
|
2017-05-03 23:57:52 +00:00
|
|
|
this._userLeftListener);
|
|
|
|
this._controller = userId;
|
|
|
|
logger.log(`Remote control permissions granted to: ${userId}`);
|
2017-06-22 21:28:57 +00:00
|
|
|
|
|
|
|
let promise;
|
|
|
|
|
2017-07-09 21:34:08 +00:00
|
|
|
if (APP.conference.isSharingScreen
|
|
|
|
&& APP.conference.getDesktopSharingSourceType() === 'screen') {
|
2017-06-22 21:28:57 +00:00
|
|
|
promise = this._sendStartRequest();
|
2017-05-03 23:57:52 +00:00
|
|
|
} else {
|
2017-07-09 21:34:08 +00:00
|
|
|
promise = APP.conference.toggleScreenSharing(
|
|
|
|
true,
|
|
|
|
{
|
|
|
|
desktopSharingSources: [ 'screen' ]
|
|
|
|
})
|
2017-06-22 21:28:57 +00:00
|
|
|
.then(() => this._sendStartRequest());
|
|
|
|
}
|
|
|
|
|
|
|
|
promise
|
|
|
|
.then(() =>
|
|
|
|
this.sendRemoteControlEndpointMessage(userId, {
|
|
|
|
type: EVENTS.permissions,
|
|
|
|
action: PERMISSIONS_ACTIONS.grant
|
2017-06-13 19:24:34 +00:00
|
|
|
})
|
2017-06-22 21:28:57 +00:00
|
|
|
)
|
2017-07-09 21:34:08 +00:00
|
|
|
.catch(error => {
|
|
|
|
logger.error(error);
|
|
|
|
|
2017-06-22 21:28:57 +00:00
|
|
|
this.sendRemoteControlEndpointMessage(userId, {
|
|
|
|
type: EVENTS.permissions,
|
|
|
|
action: PERMISSIONS_ACTIONS.error
|
2017-01-06 01:18:07 +00:00
|
|
|
});
|
2017-06-22 21:28:57 +00:00
|
|
|
|
2017-07-10 03:01:48 +00:00
|
|
|
APP.UI.messageHandler.notify(
|
2017-07-09 21:34:08 +00:00
|
|
|
'dialog.remoteControlTitle',
|
|
|
|
'dialog.startRemoteControlErrorMessage'
|
|
|
|
);
|
|
|
|
|
|
|
|
this._stop(true);
|
2017-06-22 21:28:57 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sends remote control start request.
|
|
|
|
*
|
|
|
|
* @returns {Promise}
|
|
|
|
*/
|
|
|
|
_sendStartRequest() {
|
|
|
|
return transport.sendRequest({
|
|
|
|
name: REMOTE_CONTROL_MESSAGE_NAME,
|
|
|
|
type: REQUESTS.start,
|
|
|
|
sourceId: APP.conference.getDesktopSharingSourceId()
|
|
|
|
});
|
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-06-22 21:28:57 +00:00
|
|
|
* events with type EVENTS.supported and EVENTS.stop are
|
|
|
|
* supported.
|
2017-05-03 23:57:52 +00:00
|
|
|
*
|
|
|
|
* @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-06-22 21:28:57 +00:00
|
|
|
case EVENTS.supported:
|
2017-05-02 22:39:36 +00:00
|
|
|
this._onRemoteControlSupported();
|
|
|
|
break;
|
2017-06-22 21:28:57 +00:00
|
|
|
case EVENTS.stop:
|
|
|
|
this.stop();
|
|
|
|
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
|
|
|
}
|