2017-05-03 23:57:52 +00:00
|
|
|
/* @flow */
|
|
|
|
|
2017-08-17 16:43:22 +00:00
|
|
|
import EventEmitter from 'events';
|
2017-05-03 23:57:52 +00:00
|
|
|
import { getLogger } from 'jitsi-meet-logger';
|
2017-05-02 22:39:36 +00:00
|
|
|
|
|
|
|
import {
|
2017-06-22 21:28:57 +00:00
|
|
|
REMOTE_CONTROL_MESSAGE_NAME
|
2017-05-03 23:57:52 +00:00
|
|
|
} from '../../service/remotecontrol/Constants';
|
|
|
|
|
|
|
|
const logger = getLogger(__filename);
|
2017-05-02 22:39:36 +00:00
|
|
|
|
2017-05-03 23:57:52 +00:00
|
|
|
declare var APP: Object;
|
2016-12-28 00:46:55 +00:00
|
|
|
|
2017-05-03 23:57:52 +00:00
|
|
|
/**
|
|
|
|
* Implements common logic for Receiver class and Controller class.
|
|
|
|
*/
|
2017-08-17 16:43:22 +00:00
|
|
|
export default class RemoteControlParticipant extends EventEmitter {
|
2017-05-03 23:57:52 +00:00
|
|
|
_enabled: boolean;
|
|
|
|
|
2016-12-28 00:46:55 +00:00
|
|
|
/**
|
|
|
|
* Creates new instance.
|
|
|
|
*/
|
|
|
|
constructor() {
|
2017-08-17 16:43:22 +00:00
|
|
|
super();
|
2017-05-03 23:57:52 +00:00
|
|
|
this._enabled = false;
|
2016-12-28 00:46:55 +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-28 00:46:55 +00:00
|
|
|
*/
|
2017-05-03 23:57:52 +00:00
|
|
|
enable(enabled: boolean) {
|
|
|
|
this._enabled = enabled;
|
2016-12-28 00:46:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-06-22 21:28:57 +00:00
|
|
|
* Sends remote control message to other participant trough data channel.
|
2017-05-03 23:57:52 +00:00
|
|
|
*
|
|
|
|
* @param {string} to - The participant who will receive the event.
|
|
|
|
* @param {RemoteControlEvent} event - The remote control event.
|
|
|
|
* @param {Function} onDataChannelFail - Handler for data channel failure.
|
|
|
|
* @returns {void}
|
2016-12-28 00:46:55 +00:00
|
|
|
*/
|
2017-06-22 21:28:57 +00:00
|
|
|
sendRemoteControlEndpointMessage(
|
2017-05-03 23:57:52 +00:00
|
|
|
to: ?string,
|
|
|
|
event: Object,
|
|
|
|
onDataChannelFail: ?Function) {
|
|
|
|
if (!this._enabled || !to) {
|
2017-05-02 22:39:36 +00:00
|
|
|
logger.warn(
|
2017-05-03 23:57:52 +00:00
|
|
|
'Remote control: Skip sending remote control event. Params:',
|
2017-05-02 22:39:36 +00:00
|
|
|
this.enable,
|
|
|
|
to);
|
2017-05-03 23:57:52 +00:00
|
|
|
|
2016-12-28 00:46:55 +00:00
|
|
|
return;
|
2017-01-19 21:13:05 +00:00
|
|
|
}
|
2017-05-03 23:57:52 +00:00
|
|
|
try {
|
2017-05-01 22:11:26 +00:00
|
|
|
APP.conference.sendEndpointMessage(to, {
|
2017-06-22 21:28:57 +00:00
|
|
|
name: REMOTE_CONTROL_MESSAGE_NAME,
|
2017-05-01 22:11:26 +00:00
|
|
|
...event
|
|
|
|
});
|
2016-12-28 00:46:55 +00:00
|
|
|
} catch (e) {
|
2017-05-02 22:39:36 +00:00
|
|
|
logger.error(
|
2017-05-03 23:57:52 +00:00
|
|
|
'Failed to send EndpointMessage via the datachannels',
|
2017-01-19 21:13:05 +00:00
|
|
|
e);
|
2017-05-03 23:57:52 +00:00
|
|
|
if (typeof onDataChannelFail === 'function') {
|
|
|
|
onDataChannelFail(e);
|
|
|
|
}
|
2016-12-28 00:46:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|