123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753 |
- // @flow
-
- import { openDialog } from '../base/dialog';
- import { JitsiConferenceEvents } from '../base/lib-jitsi-meet';
- import { getParticipantDisplayName, getPinnedParticipant, pinParticipant } from '../base/participants';
- import { getLocalVideoTrack } from '../base/tracks';
- import { showNotification } from '../notifications';
-
- import {
- CAPTURE_EVENTS,
- REMOTE_CONTROL_ACTIVE,
- SET_REQUESTED_PARTICIPANT,
- SET_CONTROLLER,
- SET_RECEIVER_ENABLED,
- SET_RECEIVER_TRANSPORT,
- SET_CONTROLLED_PARTICIPANT
- } from './actionTypes';
- import { RemoteControlAuthorizationDialog } from './components';
- import {
- DISCO_REMOTE_CONTROL_FEATURE,
- EVENTS,
- REMOTE_CONTROL_MESSAGE_NAME,
- PERMISSIONS_ACTIONS,
- REQUESTS
- } from './constants';
- import {
- getKey,
- getModifiers,
- getRemoteConrolEventCaptureArea,
- isRemoteControlEnabled,
- sendRemoteControlEndpointMessage
- } from './functions';
- import logger from './logger';
-
- /**
- * Listeners.
- */
- let permissionsReplyListener, receiverEndpointMessageListener, stopListener;
-
- declare var APP: Object;
- declare var $: Function;
-
- /**
- * Signals that the remote control authorization dialog should be displayed.
- *
- * @param {string} participantId - The id of the participant who is requesting
- * the authorization.
- * @returns {{
- * type: OPEN_DIALOG,
- * component: {RemoteControlAuthorizationDialog},
- * componentProps: {
- * participantId: {string}
- * }
- * }}
- * @public
- */
- export function openRemoteControlAuthorizationDialog(participantId: string) {
- return openDialog(RemoteControlAuthorizationDialog, { participantId });
- }
-
- /**
- * Sets the remote control active property.
- *
- * @param {boolean} active - The new value for the active property.
- * @returns {Function}
- */
- export function setRemoteControlActive(active: boolean) {
- return (dispatch: Function, getState: Function) => {
- const state = getState();
- const { active: oldActive } = state['features/remote-control'];
- const { conference } = state['features/base/conference'];
-
- if (active !== oldActive) {
- dispatch({
- type: REMOTE_CONTROL_ACTIVE,
- active
- });
- conference.setLocalParticipantProperty('remoteControlSessionStatus', active);
- }
- };
- }
-
- /**
- * Requests permissions from the remote control receiver side.
- *
- * @param {string} userId - The user id of the participant that will be
- * requested.
- * @returns {Function}
- */
- export function requestRemoteControl(userId: string) {
- return (dispatch: Function, getState: Function) => {
- const state = getState();
- const enabled = isRemoteControlEnabled(state);
-
- if (!enabled) {
- return Promise.reject(new Error('Remote control is disabled!'));
- }
-
- dispatch(setRemoteControlActive(true));
-
- logger.log(`Requsting remote control permissions from: ${userId}`);
-
- const { conference } = state['features/base/conference'];
-
-
- permissionsReplyListener = (participant, event) => {
- dispatch(processPermissionRequestReply(participant.getId(), event));
- };
-
- conference.on(JitsiConferenceEvents.ENDPOINT_MESSAGE_RECEIVED, permissionsReplyListener);
-
- dispatch({
- type: SET_REQUESTED_PARTICIPANT,
- requestedParticipant: userId
- });
-
- if (!sendRemoteControlEndpointMessage(
- conference,
- userId,
- {
- type: EVENTS.permissions,
- action: PERMISSIONS_ACTIONS.request
- })) {
- dispatch(clearRequest());
- }
- };
- }
-
- /**
- * Handles permission request replies on the controller side.
- *
- * @param {string} participantId - The participant that sent the request.
- * @param {EndpointMessage} event - The permission request event.
- * @returns {Function}
- */
- export function processPermissionRequestReply(participantId: string, event: Object) {
- return (dispatch: Function, getState: Function) => {
- const state = getState();
- const { action, name, type } = event;
- const { requestedParticipant } = state['features/remote-control'].controller;
-
- if (isRemoteControlEnabled(state) && name === REMOTE_CONTROL_MESSAGE_NAME && type === EVENTS.permissions
- && participantId === requestedParticipant) {
- let descriptionKey, permissionGranted = false;
-
- switch (action) {
- case PERMISSIONS_ACTIONS.grant: {
- dispatch({
- type: SET_CONTROLLED_PARTICIPANT,
- controlled: participantId
- });
-
- logger.log('Remote control permissions granted!', participantId);
- logger.log('Starting remote control controller.');
-
- const { conference } = state['features/base/conference'];
-
- stopListener = (participant, stopEvent) => {
- dispatch(handleRemoteControlStoppedEvent(participant.getId(), stopEvent));
- };
-
- conference.on(JitsiConferenceEvents.ENDPOINT_MESSAGE_RECEIVED, stopListener);
-
- dispatch(resume());
-
- permissionGranted = true;
- descriptionKey = 'dialog.remoteControlAllowedMessage';
- break;
- }
- case PERMISSIONS_ACTIONS.deny:
- logger.log('Remote control permissions denied!', participantId);
- descriptionKey = 'dialog.remoteControlDeniedMessage';
- break;
- case PERMISSIONS_ACTIONS.error:
- logger.error('Error occurred on receiver side');
- descriptionKey = 'dialog.remoteControlErrorMessage';
- break;
- default:
- logger.error('Unknown reply received!');
- descriptionKey = 'dialog.remoteControlErrorMessage';
- }
-
- dispatch(clearRequest());
-
- if (!permissionGranted) {
- dispatch(setRemoteControlActive(false));
- }
-
- dispatch(showNotification({
- descriptionArguments: { user: getParticipantDisplayName(state, participantId) },
- descriptionKey,
- titleKey: 'dialog.remoteControlTitle'
- }));
-
- if (permissionGranted) {
- // the remote control permissions has been granted
- // pin the controlled participant
- const pinnedParticipant = getPinnedParticipant(state);
- const pinnedId = pinnedParticipant?.id;
-
- if (pinnedId !== participantId) {
- dispatch(pinParticipant(participantId));
- }
- }
- } else {
- // different message type or another user -> ignoring the message
- }
- };
- }
-
- /**
- * Handles remote control stopped.
- *
- * @param {string} participantId - The ID of the participant that has sent the event.
- * @param {EndpointMessage} event - EndpointMessage event from the data channels.
- * @property {string} type - The function process only events with name REMOTE_CONTROL_MESSAGE_NAME.
- * @returns {void}
- */
- export function handleRemoteControlStoppedEvent(participantId: Object, event: Object) {
- return (dispatch: Function, getState: Function) => {
- const state = getState();
- const { name, type } = event;
- const { controlled } = state['features/remote-control'].controller;
-
- if (isRemoteControlEnabled(state) && name === REMOTE_CONTROL_MESSAGE_NAME && type === EVENTS.stop
- && participantId === controlled) {
- dispatch(stopController());
- }
- };
- }
-
- /**
- * 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.
- *
- * @param {boolean} notifyRemoteParty - If true a endpoint message to the controlled participant will be sent.
- * @returns {void}
- */
- export function stopController(notifyRemoteParty: boolean = false) {
- return (dispatch: Function, getState: Function) => {
- const state = getState();
- const { controlled } = state['features/remote-control'].controller;
-
- if (!controlled) {
- return;
- }
-
- const { conference } = state['features/base/conference'];
-
- if (notifyRemoteParty) {
- sendRemoteControlEndpointMessage(conference, controlled, {
- type: EVENTS.stop
- });
- }
-
- logger.log('Stopping remote control controller.');
-
- conference.off(JitsiConferenceEvents.ENDPOINT_MESSAGE_RECEIVED, stopListener);
- stopListener = undefined;
-
- dispatch(pause());
-
- dispatch({
- type: SET_CONTROLLED_PARTICIPANT,
- controlled: undefined
- });
-
- dispatch(setRemoteControlActive(false));
- dispatch(showNotification({
- descriptionKey: 'dialog.remoteControlStopMessage',
- titleKey: 'dialog.remoteControlTitle'
- }));
- };
- }
-
- /**
- * Clears a pending permission request.
- *
- * @returns {Function}
- */
- export function clearRequest() {
- return (dispatch: Function, getState: Function) => {
- const { conference } = getState()['features/base/conference'];
-
- dispatch({
- type: SET_REQUESTED_PARTICIPANT,
- requestedParticipant: undefined
- });
-
- conference.off(JitsiConferenceEvents.ENDPOINT_MESSAGE_RECEIVED, permissionsReplyListener);
- permissionsReplyListener = undefined;
- };
- }
-
-
- /**
- * Sets that transport object that is used by the receiver to communicate with the native part of the remote control
- * implementation.
- *
- * @param {Transport} transport - The transport to be set.
- * @returns {{
- * type: SET_RECEIVER_TRANSPORT,
- * transport: Transport
- * }}
- */
- export function setReceiverTransport(transport: Object) {
- return {
- type: SET_RECEIVER_TRANSPORT,
- transport
- };
- }
-
- /**
- * Enables the receiver functionality.
- *
- * @returns {Function}
- */
- export function enableReceiver() {
- return (dispatch: Function, getState: Function) => {
- const state = getState();
- const { enabled } = state['features/remote-control'].receiver;
-
- if (enabled) {
- return;
- }
-
- const { connection } = state['features/base/connection'];
- const { conference } = state['features/base/conference'];
-
- if (!connection || !conference) {
- logger.error('Couldn\'t enable the remote receiver! The connection or conference instance is undefined!');
-
- return;
- }
-
- dispatch({
- type: SET_RECEIVER_ENABLED,
- enabled: true
- });
-
- connection.addFeature(DISCO_REMOTE_CONTROL_FEATURE, true);
- receiverEndpointMessageListener = (participant, message) => {
- dispatch(endpointMessageReceived(participant.getId(), message));
- };
- conference.on(JitsiConferenceEvents.ENDPOINT_MESSAGE_RECEIVED, receiverEndpointMessageListener);
- };
- }
-
- /**
- * Disables the receiver functionality.
- *
- * @returns {Function}
- */
- export function disableReceiver() {
- return (dispatch: Function, getState: Function) => {
- const state = getState();
- const { enabled } = state['features/remote-control'].receiver;
-
- if (!enabled) {
- return;
- }
-
- const { connection } = state['features/base/connection'];
- const { conference } = state['features/base/conference'];
-
- if (!connection || !conference) {
- logger.error('Couldn\'t enable the remote receiver! The connection or conference instance is undefined!');
-
- return;
- }
-
- logger.log('Remote control receiver disabled.');
-
- dispatch({
- type: SET_RECEIVER_ENABLED,
- enabled: false
- });
-
- dispatch(stopReceiver(true));
-
- connection.removeFeature(DISCO_REMOTE_CONTROL_FEATURE);
- conference.off(JitsiConferenceEvents.ENDPOINT_MESSAGE_RECEIVED, receiverEndpointMessageListener);
- };
- }
-
- /**
- * Stops a remote control session on the receiver side.
- *
- * @param {boolean} [dontNotifyLocalParty] - If true - a notification about stopping
- * the remote control won't be displayed.
- * @param {boolean} [dontNotifyRemoteParty] - If true a endpoint message to the controller participant will be sent.
- * @returns {Function}
- */
- export function stopReceiver(dontNotifyLocalParty: boolean = false, dontNotifyRemoteParty: boolean = false) {
- return (dispatch: Function, getState: Function) => {
- const state = getState();
- const { receiver } = state['features/remote-control'];
- const { controller, transport } = receiver;
-
- if (!controller) {
- return;
- }
-
- const { conference } = state['features/base/conference'];
-
- if (!dontNotifyRemoteParty) {
- sendRemoteControlEndpointMessage(conference, controller, {
- type: EVENTS.stop
- });
- }
-
- dispatch({
- type: SET_CONTROLLER,
- controller: undefined
- });
-
- transport.sendEvent({
- name: REMOTE_CONTROL_MESSAGE_NAME,
- type: EVENTS.stop
- });
-
- dispatch(setRemoteControlActive(false));
-
- if (!dontNotifyLocalParty) {
- dispatch(showNotification({
- descriptionKey: 'dialog.remoteControlStopMessage',
- titleKey: 'dialog.remoteControlTitle'
- }));
- }
- };
- }
-
-
- /**
- * Handles only remote control endpoint messages.
- *
- * @param {string} participantId - The controller participant ID.
- * @param {Object} message - EndpointMessage from the data channels.
- * @param {string} message.name - The function processes only messages with
- * name REMOTE_CONTROL_MESSAGE_NAME.
- * @returns {Function}
- */
- export function endpointMessageReceived(participantId: string, message: Object) {
- return (dispatch: Function, getState: Function) => {
- const { action, name, type } = message;
-
- if (name !== REMOTE_CONTROL_MESSAGE_NAME) {
- return;
- }
-
- const state = getState();
- const { receiver } = state['features/remote-control'];
- const { enabled, transport } = receiver;
-
- if (enabled) {
- const { controller } = receiver;
-
- if (!controller && type === EVENTS.permissions && action === PERMISSIONS_ACTIONS.request) {
- dispatch(setRemoteControlActive(true));
- dispatch(openRemoteControlAuthorizationDialog(participantId));
- } else if (controller === participantId) {
- if (type === EVENTS.stop) {
- dispatch(stopReceiver(false, true));
- } else { // forward the message
- transport.sendEvent(message);
- }
- } // else ignore
- } else {
- logger.log('Remote control message is ignored because remote control is disabled', message);
- }
- };
- }
-
- /**
- * Denies remote control access for user associated with the passed user id.
- *
- * @param {string} participantId - The id associated with the user who sent the
- * request for remote control authorization.
- * @returns {Function}
- */
- export function deny(participantId: string) {
- return (dispatch: Function, getState: Function) => {
- const state = getState();
- const { conference } = state['features/base/conference'];
-
- dispatch(setRemoteControlActive(false));
- sendRemoteControlEndpointMessage(conference, participantId, {
- type: EVENTS.permissions,
- action: PERMISSIONS_ACTIONS.deny
- });
- };
- }
-
- /**
- * Sends start remote control request to the native implementation.
- *
- * @returns {Function}
- */
- export function sendStartRequest() {
- return (dispatch: Function, getState: Function) => {
- const state = getState();
- const tracks = state['features/base/tracks'];
- const track = getLocalVideoTrack(tracks);
- const { sourceId } = track?.jitsiTrack || {};
- const { transport } = state['features/remote-control'].receiver;
-
- return transport.sendRequest({
- name: REMOTE_CONTROL_MESSAGE_NAME,
- type: REQUESTS.start,
- sourceId
- });
- };
- }
-
- /**
- * Grants remote control access to user associated with the passed user id.
- *
- * @param {string} participantId - The id associated with the user who sent the
- * request for remote control authorization.
- * @returns {Function}
- */
- export function grant(participantId: string) {
- return (dispatch: Function, getState: Function) => {
- dispatch({
- type: SET_CONTROLLER,
- controller: participantId
- });
- logger.log(`Remote control permissions granted to: ${participantId}`);
-
- let promise;
- const state = getState();
- const tracks = state['features/base/tracks'];
- const track = getLocalVideoTrack(tracks);
- const isScreenSharing = track?.videoType === 'desktop';
- const { sourceType } = track?.jitsiTrack || {};
-
- if (isScreenSharing && sourceType === 'screen') {
- promise = dispatch(sendStartRequest());
- } else {
- // FIXME: Use action here once toggleScreenSharing is moved to redux.
- promise = APP.conference.toggleScreenSharing(
- true,
- {
- desktopSharingSources: [ 'screen' ]
- })
- .then(() => dispatch(sendStartRequest()));
- }
-
- const { conference } = state['features/base/conference'];
-
- promise
- .then(() => sendRemoteControlEndpointMessage(conference, participantId, {
- type: EVENTS.permissions,
- action: PERMISSIONS_ACTIONS.grant
- }))
- .catch(error => {
- logger.error(error);
-
- sendRemoteControlEndpointMessage(conference, participantId, {
- type: EVENTS.permissions,
- action: PERMISSIONS_ACTIONS.error
- });
-
- dispatch(showNotification({
- descriptionKey: 'dialog.startRemoteControlErrorMessage',
- titleKey: 'dialog.remoteControlTitle'
- }));
-
- dispatch(stopReceiver(true));
- });
- };
- }
-
- /**
- * Handler for mouse click events on the controller side.
- *
- * @param {string} type - The type of event ("mousedown"/"mouseup").
- * @param {Event} event - The mouse event.
- * @returns {Function}
- */
- export function mouseClicked(type: string, event: Object) {
- return (dispatch: Function, getState: Function) => {
- const state = getState();
- const { conference } = state['features/base/conference'];
- const { controller } = state['features/remote-control'];
-
- sendRemoteControlEndpointMessage(conference, controller.controlled, {
- type,
- button: event.which
- });
- };
- }
-
- /**
- * Handles mouse moved events on the controller side.
- *
- * @param {Event} event - The mouse event.
- * @returns {Function}
- */
- export function mouseMoved(event: Object) {
- return (dispatch: Function, getState: Function) => {
- const area = getRemoteConrolEventCaptureArea();
-
- if (!area) {
- return;
- }
-
- const position = area.position();
- const state = getState();
- const { conference } = state['features/base/conference'];
- const { controller } = state['features/remote-control'];
-
- sendRemoteControlEndpointMessage(conference, controller.controlled, {
- type: EVENTS.mousemove,
- x: (event.pageX - position.left) / area.width(),
- y: (event.pageY - position.top) / area.height()
- });
- };
- }
-
- /**
- * Handles mouse scroll events on the controller side.
- *
- * @param {Event} event - The mouse event.
- * @returns {Function}
- */
- export function mouseScrolled(event: Object) {
- return (dispatch: Function, getState: Function) => {
- const state = getState();
- const { conference } = state['features/base/conference'];
- const { controller } = state['features/remote-control'];
-
- sendRemoteControlEndpointMessage(conference, controller.controlled, {
- type: EVENTS.mousescroll,
- x: event.deltaX,
- y: event.deltaY
- });
- };
- }
-
- /**
- * Handles key press events on the controller side..
- *
- * @param {string} type - The type of event ("keydown"/"keyup").
- * @param {Event} event - The key event.
- * @returns {Function}
- */
- export function keyPressed(type: string, event: Object) {
- return (dispatch: Function, getState: Function) => {
- const state = getState();
- const { conference } = state['features/base/conference'];
- const { controller } = state['features/remote-control'];
-
- sendRemoteControlEndpointMessage(conference, controller.controlled, {
- type,
- key: getKey(event),
- modifiers: getModifiers(event)
- });
- };
- }
-
- /**
- * Disables the keyboatd shortcuts. Starts collecting remote control
- * events. It can be used to resume an active remote control session which
- * was paused with the pause action.
- *
- * @returns {Function}
- */
- export function resume() {
- return (dispatch: Function, getState: Function) => {
- const area = getRemoteConrolEventCaptureArea();
- const state = getState();
- const { controller } = state['features/remote-control'];
- const { controlled, isCapturingEvents } = controller;
-
- if (!isRemoteControlEnabled(state) || !area || !controlled || isCapturingEvents) {
- return;
- }
-
- logger.log('Resuming remote control controller.');
-
- // FIXME: Once the keyboard shortcuts are using react/redux.
- APP.keyboardshortcut.enable(false);
-
- area.mousemove(event => {
- dispatch(mouseMoved(event));
- });
- area.mousedown(event => dispatch(mouseClicked(EVENTS.mousedown, event)));
- area.mouseup(event => dispatch(mouseClicked(EVENTS.mouseup, event)));
- area.dblclick(event => dispatch(mouseClicked(EVENTS.mousedblclick, event)));
- area.contextmenu(() => false);
- area[0].onwheel = event => {
- event.preventDefault();
- event.stopPropagation();
- dispatch(mouseScrolled(event));
-
- return false;
- };
- $(window).keydown(event => dispatch(keyPressed(EVENTS.keydown, event)));
- $(window).keyup(event => dispatch(keyPressed(EVENTS.keyup, event)));
-
- dispatch({
- type: CAPTURE_EVENTS,
- isCapturingEvents: true
- });
- };
- }
-
-
- /**
- * 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 the pause action, but no events from the
- * controller side will be captured and sent. You can resume the collecting
- * of the events with the resume action.
- *
- * @returns {Function}
- */
- export function pause() {
- return (dispatch: Function, getState: Function) => {
- const state = getState();
- const { controller } = state['features/remote-control'];
- const { controlled, isCapturingEvents } = controller;
-
- if (!isRemoteControlEnabled(state) || !controlled || !isCapturingEvents) {
- return;
- }
-
- logger.log('Pausing remote control controller.');
-
- // FIXME: Once the keyboard shortcuts are using react/redux.
- APP.keyboardshortcut.enable(true);
-
- const area = getRemoteConrolEventCaptureArea();
-
- if (area) {
- area.off('contextmenu');
- area.off('dblclick');
- area.off('mousedown');
- area.off('mousemove');
- area.off('mouseup');
- area[0].onwheel = undefined;
- }
-
- $(window).off('keydown');
- $(window).off('keyup');
-
- dispatch({
- type: CAPTURE_EVENTS,
- isCapturingEvents: false
- });
- };
- }
|