2016-12-28 00:46:55 +00:00
|
|
|
/* global $, JitsiMeetJS, APP */
|
2017-01-19 21:13:05 +00:00
|
|
|
const logger = require("jitsi-meet-logger").getLogger(__filename);
|
2016-12-09 23:15:04 +00:00
|
|
|
import * as KeyCodes from "../keycode/keycode";
|
2016-12-28 00:46:55 +00:00
|
|
|
import {EVENT_TYPES, REMOTE_CONTROL_EVENT_TYPE, PERMISSIONS_ACTIONS}
|
2016-12-20 22:15:13 +00:00
|
|
|
from "../../service/remotecontrol/Constants";
|
2016-12-28 00:46:55 +00:00
|
|
|
import RemoteControlParticipant from "./RemoteControlParticipant";
|
2017-01-10 18:51:25 +00:00
|
|
|
import UIEvents from "../../service/UI/UIEvents";
|
2016-12-28 00:46:55 +00:00
|
|
|
|
|
|
|
const ConferenceEvents = JitsiMeetJS.events.conference;
|
2016-12-09 23:15:04 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Extract the keyboard key from the keyboard event.
|
|
|
|
* @param event {KeyboardEvent} the event.
|
|
|
|
* @returns {KEYS} the key that is pressed or undefined.
|
|
|
|
*/
|
|
|
|
function getKey(event) {
|
|
|
|
return KeyCodes.keyboardEventToKey(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Extract the modifiers from the keyboard event.
|
|
|
|
* @param event {KeyboardEvent} the event.
|
|
|
|
* @returns {Array} with possible values: "shift", "control", "alt", "command".
|
|
|
|
*/
|
|
|
|
function getModifiers(event) {
|
|
|
|
let modifiers = [];
|
|
|
|
if(event.shiftKey) {
|
|
|
|
modifiers.push("shift");
|
|
|
|
}
|
|
|
|
|
|
|
|
if(event.ctrlKey) {
|
|
|
|
modifiers.push("control");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if(event.altKey) {
|
|
|
|
modifiers.push("alt");
|
|
|
|
}
|
|
|
|
|
|
|
|
if(event.metaKey) {
|
|
|
|
modifiers.push("command");
|
|
|
|
}
|
|
|
|
|
|
|
|
return modifiers;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This class represents the controller party for a remote controller session.
|
|
|
|
* It listens for mouse and keyboard events and sends them to the receiver
|
|
|
|
* party of the remote control session.
|
|
|
|
*/
|
2016-12-28 00:46:55 +00:00
|
|
|
export default class Controller extends RemoteControlParticipant {
|
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-01-10 18:51:25 +00:00
|
|
|
this.isCollectingEvents = false;
|
2016-12-28 00:46:55 +00:00
|
|
|
this.controlledParticipant = null;
|
|
|
|
this.requestedParticipant = null;
|
2017-01-06 01:18:07 +00:00
|
|
|
this._stopListener = this._handleRemoteControlStoppedEvent.bind(this);
|
|
|
|
this._userLeftListener = this._onUserLeft.bind(this);
|
2017-01-10 18:51:25 +00:00
|
|
|
this._largeVideoChangedListener
|
|
|
|
= this._onLargeVideoIdChanged.bind(this);
|
2016-12-20 22:15:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-12-28 00:46:55 +00:00
|
|
|
* Requests permissions from the remote control receiver side.
|
|
|
|
* @param {string} userId the user id of the participant that will be
|
|
|
|
* requested.
|
2017-01-20 20:26:25 +00:00
|
|
|
* @param {JQuerySelector} eventCaptureArea the area that is going to be
|
|
|
|
* used mouse and keyboard event capture.
|
2017-01-06 01:18:07 +00:00
|
|
|
* @returns {Promise<boolean>} - resolve values:
|
|
|
|
* true - accept
|
|
|
|
* false - deny
|
|
|
|
* null - the participant has left.
|
2016-12-20 22:15:13 +00:00
|
|
|
*/
|
2017-01-20 20:26:25 +00:00
|
|
|
requestPermissions(userId, eventCaptureArea) {
|
2016-12-28 00:46:55 +00:00
|
|
|
if(!this.enabled) {
|
|
|
|
return Promise.reject(new Error("Remote control is disabled!"));
|
|
|
|
}
|
2017-01-20 20:26:25 +00:00
|
|
|
this.area = eventCaptureArea;// $("#largeVideoWrapper")
|
2017-01-23 20:58:45 +00:00
|
|
|
logger.log("Requsting remote control permissions from: " + userId);
|
2016-12-28 00:46:55 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
2017-01-06 01:18:07 +00:00
|
|
|
const clearRequest = () => {
|
|
|
|
this.requestedParticipant = null;
|
|
|
|
APP.conference.removeConferenceListener(
|
|
|
|
ConferenceEvents.ENDPOINT_MESSAGE_RECEIVED,
|
|
|
|
permissionsReplyListener);
|
|
|
|
APP.conference.removeConferenceListener(
|
|
|
|
ConferenceEvents.USER_LEFT,
|
|
|
|
onUserLeft);
|
|
|
|
};
|
|
|
|
const permissionsReplyListener = (participant, event) => {
|
2016-12-28 00:46:55 +00:00
|
|
|
let result = null;
|
|
|
|
try {
|
|
|
|
result = this._handleReply(participant, event);
|
|
|
|
} catch (e) {
|
|
|
|
reject(e);
|
|
|
|
}
|
|
|
|
if(result !== null) {
|
2017-01-06 01:18:07 +00:00
|
|
|
clearRequest();
|
2016-12-28 00:46:55 +00:00
|
|
|
resolve(result);
|
|
|
|
}
|
|
|
|
};
|
2017-01-06 01:18:07 +00:00
|
|
|
const onUserLeft = (id) => {
|
|
|
|
if(id === this.requestedParticipant) {
|
|
|
|
clearRequest();
|
|
|
|
resolve(null);
|
|
|
|
}
|
|
|
|
};
|
2016-12-28 00:46:55 +00:00
|
|
|
APP.conference.addConferenceListener(
|
|
|
|
ConferenceEvents.ENDPOINT_MESSAGE_RECEIVED,
|
|
|
|
permissionsReplyListener);
|
2017-01-06 01:18:07 +00:00
|
|
|
APP.conference.addConferenceListener(ConferenceEvents.USER_LEFT,
|
|
|
|
onUserLeft);
|
2016-12-28 00:46:55 +00:00
|
|
|
this.requestedParticipant = userId;
|
|
|
|
this._sendRemoteControlEvent(userId, {
|
|
|
|
type: EVENT_TYPES.permissions,
|
|
|
|
action: PERMISSIONS_ACTIONS.request
|
|
|
|
}, e => {
|
2017-01-06 01:18:07 +00:00
|
|
|
clearRequest();
|
2016-12-28 00:46:55 +00:00
|
|
|
reject(e);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles the reply of the permissions request.
|
|
|
|
* @param {JitsiParticipant} participant the participant that has sent the
|
|
|
|
* reply
|
2017-01-19 23:19:58 +00:00
|
|
|
* @param {RemoteControlEvent} event the remote control event.
|
2016-12-28 00:46:55 +00:00
|
|
|
*/
|
|
|
|
_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
|
|
|
|
&& userId === this.requestedParticipant) {
|
2017-01-20 20:26:25 +00:00
|
|
|
if(remoteControlEvent.action !== PERMISSIONS_ACTIONS.grant) {
|
|
|
|
this.area = null;
|
|
|
|
}
|
2017-01-06 01:18:07 +00:00
|
|
|
switch(remoteControlEvent.action) {
|
|
|
|
case PERMISSIONS_ACTIONS.grant: {
|
|
|
|
this.controlledParticipant = userId;
|
2017-01-23 20:58:45 +00:00
|
|
|
logger.log("Remote control permissions granted to: "
|
2017-01-19 21:13:05 +00:00
|
|
|
+ userId);
|
2017-01-06 01:18:07 +00:00
|
|
|
this._start();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
case PERMISSIONS_ACTIONS.deny:
|
|
|
|
return false;
|
|
|
|
case PERMISSIONS_ACTIONS.error:
|
|
|
|
throw new Error("Error occurred on receiver side");
|
|
|
|
default:
|
|
|
|
throw new Error("Unknown reply received!");
|
2016-12-28 00:46:55 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
//different message type or another user -> ignoring the message
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles remote control stopped.
|
|
|
|
* @param {JitsiParticipant} participant the participant that has sent the
|
|
|
|
* event
|
2017-01-19 23:19:58 +00:00
|
|
|
* @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 {RemoteControlEvent} event - the remote control event.
|
2016-12-28 00:46:55 +00:00
|
|
|
*/
|
|
|
|
_handleRemoteControlStoppedEvent(participant, event) {
|
|
|
|
if(this.enabled && event.type === REMOTE_CONTROL_EVENT_TYPE
|
|
|
|
&& event.event.type === EVENT_TYPES.stop
|
|
|
|
&& participant.getId() === this.controlledParticipant) {
|
|
|
|
this._stop();
|
|
|
|
}
|
2016-12-20 22:15:13 +00:00
|
|
|
}
|
2016-12-09 23:15:04 +00:00
|
|
|
|
|
|
|
/**
|
2017-01-10 18:51:25 +00:00
|
|
|
* Starts processing the mouse and keyboard events. Sets conference
|
|
|
|
* listeners. Disables keyboard events.
|
2016-12-09 23:15:04 +00:00
|
|
|
*/
|
2016-12-28 00:46:55 +00:00
|
|
|
_start() {
|
2017-01-19 21:13:05 +00:00
|
|
|
logger.log("Starting remote control controller.");
|
2017-01-10 18:51:25 +00:00
|
|
|
APP.UI.addListener(UIEvents.LARGE_VIDEO_ID_CHANGED,
|
|
|
|
this._largeVideoChangedListener);
|
2016-12-28 00:46:55 +00:00
|
|
|
APP.conference.addConferenceListener(
|
|
|
|
ConferenceEvents.ENDPOINT_MESSAGE_RECEIVED,
|
2017-01-06 01:18:07 +00:00
|
|
|
this._stopListener);
|
|
|
|
APP.conference.addConferenceListener(ConferenceEvents.USER_LEFT,
|
|
|
|
this._userLeftListener);
|
2017-01-10 18:51:25 +00:00
|
|
|
this.resume();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Disables the keyboatd shortcuts. Starts collecting remote control
|
|
|
|
* events.
|
|
|
|
*
|
|
|
|
* It can be used to resume an active remote control session wchich was
|
|
|
|
* paused with this.pause().
|
|
|
|
*/
|
|
|
|
resume() {
|
|
|
|
if(!this.enabled || this.isCollectingEvents) {
|
|
|
|
return;
|
|
|
|
}
|
2017-01-19 21:13:05 +00:00
|
|
|
logger.log("Resuming remote control controller.");
|
2017-01-10 18:51:25 +00:00
|
|
|
this.isCollectingEvents = true;
|
|
|
|
APP.keyboardshortcut.enable(false);
|
2016-12-09 23:15:04 +00:00
|
|
|
this.area.mousemove(event => {
|
|
|
|
const position = this.area.position();
|
2016-12-28 00:46:55 +00:00
|
|
|
this._sendRemoteControlEvent(this.controlledParticipant, {
|
2016-12-20 22:15:13 +00:00
|
|
|
type: EVENT_TYPES.mousemove,
|
2016-12-09 23:15:04 +00:00
|
|
|
x: (event.pageX - position.left)/this.area.width(),
|
|
|
|
y: (event.pageY - position.top)/this.area.height()
|
|
|
|
});
|
|
|
|
});
|
2016-12-20 22:15:13 +00:00
|
|
|
this.area.mousedown(this._onMouseClickHandler.bind(this,
|
|
|
|
EVENT_TYPES.mousedown));
|
|
|
|
this.area.mouseup(this._onMouseClickHandler.bind(this,
|
|
|
|
EVENT_TYPES.mouseup));
|
2016-12-09 23:15:04 +00:00
|
|
|
this.area.dblclick(
|
2016-12-20 22:15:13 +00:00
|
|
|
this._onMouseClickHandler.bind(this, EVENT_TYPES.mousedblclick));
|
2016-12-09 23:15:04 +00:00
|
|
|
this.area.contextmenu(() => false);
|
|
|
|
this.area[0].onmousewheel = event => {
|
2016-12-28 00:46:55 +00:00
|
|
|
this._sendRemoteControlEvent(this.controlledParticipant, {
|
2016-12-20 22:15:13 +00:00
|
|
|
type: EVENT_TYPES.mousescroll,
|
2016-12-09 23:15:04 +00:00
|
|
|
x: event.deltaX,
|
|
|
|
y: event.deltaY
|
|
|
|
});
|
|
|
|
};
|
2016-12-20 22:15:13 +00:00
|
|
|
$(window).keydown(this._onKeyPessHandler.bind(this,
|
|
|
|
EVENT_TYPES.keydown));
|
|
|
|
$(window).keyup(this._onKeyPessHandler.bind(this, EVENT_TYPES.keyup));
|
2016-12-09 23:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-01-06 01:18:07 +00:00
|
|
|
* Stops processing the mouse and keyboard events. Removes added listeners.
|
2017-01-10 18:51:25 +00:00
|
|
|
* Enables the keyboard shortcuts. Displays dialog to notify the user that
|
|
|
|
* remote control session has ended.
|
2016-12-09 23:15:04 +00:00
|
|
|
*/
|
2016-12-28 00:46:55 +00:00
|
|
|
_stop() {
|
2017-01-06 01:18:07 +00:00
|
|
|
if(!this.controlledParticipant) {
|
|
|
|
return;
|
|
|
|
}
|
2017-01-19 21:13:05 +00:00
|
|
|
logger.log("Stopping remote control controller.");
|
2017-01-10 18:51:25 +00:00
|
|
|
APP.UI.removeListener(UIEvents.LARGE_VIDEO_ID_CHANGED,
|
|
|
|
this._largeVideoChangedListener);
|
2016-12-28 00:46:55 +00:00
|
|
|
APP.conference.removeConferenceListener(
|
|
|
|
ConferenceEvents.ENDPOINT_MESSAGE_RECEIVED,
|
2017-01-06 01:18:07 +00:00
|
|
|
this._stopListener);
|
|
|
|
APP.conference.removeConferenceListener(ConferenceEvents.USER_LEFT,
|
|
|
|
this._userLeftListener);
|
2016-12-28 00:46:55 +00:00
|
|
|
this.controlledParticipant = null;
|
2017-01-10 18:51:25 +00:00
|
|
|
this.pause();
|
2017-01-20 20:26:25 +00:00
|
|
|
this.area = null;
|
2017-01-06 01:18:07 +00:00
|
|
|
APP.UI.messageHandler.openMessageDialog(
|
|
|
|
"dialog.remoteControlTitle",
|
|
|
|
"dialog.remoteControlStopMessage"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-01-10 18:51:25 +00:00
|
|
|
* Executes this._stop() mehtod:
|
|
|
|
* Stops processing the mouse and keyboard events. Removes added listeners.
|
|
|
|
* Enables the keyboard shortcuts. Displays dialog to notify the user that
|
|
|
|
* remote control session has ended.
|
|
|
|
*
|
|
|
|
* In addition:
|
|
|
|
* Sends stop message to the controlled participant.
|
2017-01-06 01:18:07 +00:00
|
|
|
*/
|
|
|
|
stop() {
|
|
|
|
if(!this.controlledParticipant) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this._sendRemoteControlEvent(this.controlledParticipant, {
|
|
|
|
type: EVENT_TYPES.stop
|
|
|
|
});
|
|
|
|
this._stop();
|
2016-12-09 23:15:04 +00:00
|
|
|
}
|
|
|
|
|
2017-01-10 18:51:25 +00:00
|
|
|
/**
|
|
|
|
* Pauses the collecting of events and enables the keyboard shortcus. But
|
|
|
|
* it doesn't removes any other listeners. Basically the remote control
|
|
|
|
* session will be still active after this.pause(), but no events from the
|
|
|
|
* controller side will be captured and sent.
|
|
|
|
*
|
|
|
|
* You can resume the collecting of the events with this.resume().
|
|
|
|
*/
|
|
|
|
pause() {
|
|
|
|
if(!this.controlledParticipant) {
|
|
|
|
return;
|
|
|
|
}
|
2017-01-19 21:13:05 +00:00
|
|
|
logger.log("Pausing remote control controller.");
|
2017-01-10 18:51:25 +00:00
|
|
|
this.isCollectingEvents = false;
|
|
|
|
APP.keyboardshortcut.enable(true);
|
|
|
|
this.area.off( "mousemove" );
|
|
|
|
this.area.off( "mousedown" );
|
|
|
|
this.area.off( "mouseup" );
|
|
|
|
this.area.off( "contextmenu" );
|
|
|
|
this.area.off( "dblclick" );
|
|
|
|
$(window).off( "keydown");
|
|
|
|
$(window).off( "keyup");
|
|
|
|
this.area[0].onmousewheel = undefined;
|
|
|
|
}
|
|
|
|
|
2016-12-09 23:15:04 +00:00
|
|
|
/**
|
|
|
|
* Handler for mouse click events.
|
|
|
|
* @param {String} type the type of event ("mousedown"/"mouseup")
|
|
|
|
* @param {Event} event the mouse event.
|
|
|
|
*/
|
|
|
|
_onMouseClickHandler(type, event) {
|
2016-12-28 00:46:55 +00:00
|
|
|
this._sendRemoteControlEvent(this.controlledParticipant, {
|
2016-12-09 23:15:04 +00:00
|
|
|
type: type,
|
|
|
|
button: event.which
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-01-06 01:18:07 +00:00
|
|
|
/**
|
|
|
|
* Returns true if the remote control session is started.
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
|
|
|
isStarted() {
|
|
|
|
return this.controlledParticipant !== null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the id of the requested participant
|
2017-01-23 22:06:51 +00:00
|
|
|
* @returns {string} this.requestedParticipant.
|
|
|
|
* NOTE: This id should be the result of JitsiParticipant.getId() call.
|
2017-01-06 01:18:07 +00:00
|
|
|
*/
|
|
|
|
getRequestedParticipant() {
|
|
|
|
return this.requestedParticipant;
|
|
|
|
}
|
|
|
|
|
2016-12-09 23:15:04 +00:00
|
|
|
/**
|
|
|
|
* Handler for key press events.
|
|
|
|
* @param {String} type the type of event ("keydown"/"keyup")
|
|
|
|
* @param {Event} event the key event.
|
|
|
|
*/
|
|
|
|
_onKeyPessHandler(type, event) {
|
2016-12-28 00:46:55 +00:00
|
|
|
this._sendRemoteControlEvent(this.controlledParticipant, {
|
2016-12-09 23:15:04 +00:00
|
|
|
type: type,
|
|
|
|
key: getKey(event),
|
|
|
|
modifiers: getModifiers(event),
|
|
|
|
});
|
|
|
|
}
|
2017-01-06 01:18:07 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Calls the stop method if the other side have left.
|
|
|
|
* @param {string} id - the user id for the participant that have left
|
|
|
|
*/
|
|
|
|
_onUserLeft(id) {
|
|
|
|
if(this.controlledParticipant === id) {
|
|
|
|
this._stop();
|
|
|
|
}
|
|
|
|
}
|
2017-01-10 18:51:25 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles changes of the participant displayed on the large video.
|
|
|
|
* @param {string} id - the user id for the participant that is displayed.
|
|
|
|
*/
|
|
|
|
_onLargeVideoIdChanged(id) {
|
|
|
|
if (!this.controlledParticipant) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(this.controlledParticipant == id) {
|
|
|
|
this.resume();
|
|
|
|
} else {
|
|
|
|
this.pause();
|
|
|
|
}
|
|
|
|
}
|
2016-12-09 23:15:04 +00:00
|
|
|
}
|