You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /* @flow */
  2. import { getLogger } from 'jitsi-meet-logger';
  3. import * as JitsiMeetConferenceEvents from '../../ConferenceEvents';
  4. import {
  5. openRemoteControlAuthorizationDialog
  6. } from '../../react/features/remote-control';
  7. import {
  8. DISCO_REMOTE_CONTROL_FEATURE,
  9. EVENTS,
  10. PERMISSIONS_ACTIONS,
  11. REMOTE_CONTROL_MESSAGE_NAME,
  12. REQUESTS
  13. } from '../../service/remotecontrol/Constants';
  14. import * as RemoteControlEvents
  15. from '../../service/remotecontrol/RemoteControlEvents';
  16. import {
  17. Transport,
  18. PostMessageTransportBackend
  19. } from '../transport';
  20. import RemoteControlParticipant from './RemoteControlParticipant';
  21. declare var APP: Object;
  22. declare var config: Object;
  23. declare var interfaceConfig: Object;
  24. declare var JitsiMeetJS: Object;
  25. const ConferenceEvents = JitsiMeetJS.events.conference;
  26. const logger = getLogger(__filename);
  27. /**
  28. * The transport instance used for communication with external apps.
  29. *
  30. * @type {Transport}
  31. */
  32. const transport = new Transport({
  33. backend: new PostMessageTransportBackend({
  34. postisOptions: { scope: 'jitsi-remote-control' }
  35. })
  36. });
  37. /**
  38. * This class represents the receiver party for a remote controller session.
  39. * It handles "remote-control-event" events and sends them to the
  40. * API module. From there the events can be received from wrapper application
  41. * and executed.
  42. */
  43. export default class Receiver extends RemoteControlParticipant {
  44. _controller: ?string;
  45. _enabled: boolean;
  46. _hangupListener: Function;
  47. _remoteControlEventsListener: Function;
  48. _userLeftListener: Function;
  49. /**
  50. * Creates new instance.
  51. */
  52. constructor() {
  53. super();
  54. this._controller = null;
  55. this._remoteControlEventsListener
  56. = this._onRemoteControlMessage.bind(this);
  57. this._userLeftListener = this._onUserLeft.bind(this);
  58. this._hangupListener = this._onHangup.bind(this);
  59. // We expect here that even if we receive the supported event earlier
  60. // it will be cached and we'll receive it.
  61. transport.on('event', event => {
  62. if (event.name === REMOTE_CONTROL_MESSAGE_NAME) {
  63. this._onRemoteControlAPIEvent(event);
  64. return true;
  65. }
  66. return false;
  67. });
  68. }
  69. /**
  70. * Enables / Disables the remote control.
  71. *
  72. * @param {boolean} enabled - The new state.
  73. * @returns {void}
  74. */
  75. _enable(enabled: boolean) {
  76. if (this._enabled === enabled) {
  77. return;
  78. }
  79. this._enabled = enabled;
  80. if (enabled === true) {
  81. logger.log('Remote control receiver enabled.');
  82. // Announce remote control support.
  83. APP.connection.addFeature(DISCO_REMOTE_CONTROL_FEATURE, true);
  84. APP.conference.addConferenceListener(
  85. ConferenceEvents.ENDPOINT_MESSAGE_RECEIVED,
  86. this._remoteControlEventsListener);
  87. APP.conference.addListener(JitsiMeetConferenceEvents.BEFORE_HANGUP,
  88. this._hangupListener);
  89. } else {
  90. logger.log('Remote control receiver disabled.');
  91. this._stop(true);
  92. APP.connection.removeFeature(DISCO_REMOTE_CONTROL_FEATURE);
  93. APP.conference.removeConferenceListener(
  94. ConferenceEvents.ENDPOINT_MESSAGE_RECEIVED,
  95. this._remoteControlEventsListener);
  96. APP.conference.removeListener(
  97. JitsiMeetConferenceEvents.BEFORE_HANGUP,
  98. this._hangupListener);
  99. }
  100. }
  101. /**
  102. * Removes the listener for ConferenceEvents.ENDPOINT_MESSAGE_RECEIVED
  103. * events. Sends stop message to the wrapper application. Optionally
  104. * displays dialog for informing the user that remote control session
  105. * ended.
  106. *
  107. * @param {boolean} [dontNotify] - If true - a notification about stopping
  108. * the remote control won't be displayed.
  109. * @returns {void}
  110. */
  111. _stop(dontNotify: boolean = false) {
  112. if (!this._controller) {
  113. return;
  114. }
  115. logger.log('Remote control receiver stop.');
  116. this._controller = null;
  117. APP.conference.removeConferenceListener(ConferenceEvents.USER_LEFT,
  118. this._userLeftListener);
  119. transport.sendEvent({
  120. name: REMOTE_CONTROL_MESSAGE_NAME,
  121. type: EVENTS.stop
  122. });
  123. this.emit(RemoteControlEvents.ACTIVE_CHANGED, false);
  124. if (!dontNotify) {
  125. APP.UI.messageHandler.notify(
  126. 'dialog.remoteControlTitle',
  127. 'dialog.remoteControlStopMessage'
  128. );
  129. }
  130. }
  131. /**
  132. * Calls this._stop() and sends stop message to the controller participant.
  133. *
  134. * @returns {void}
  135. */
  136. stop() {
  137. if (!this._controller) {
  138. return;
  139. }
  140. this.sendRemoteControlEndpointMessage(this._controller, {
  141. type: EVENTS.stop
  142. });
  143. this._stop();
  144. }
  145. /**
  146. * Listens for data channel EndpointMessage. Handles only remote control
  147. * messages. Sends the remote control messages to the external app that
  148. * will execute them.
  149. *
  150. * @param {JitsiParticipant} participant - The controller participant.
  151. * @param {Object} message - EndpointMessage from the data channels.
  152. * @param {string} message.name - The function processes only messages with
  153. * name REMOTE_CONTROL_MESSAGE_NAME.
  154. * @returns {void}
  155. */
  156. _onRemoteControlMessage(participant: Object, message: Object) {
  157. if (message.name !== REMOTE_CONTROL_MESSAGE_NAME) {
  158. return;
  159. }
  160. if (this._enabled) {
  161. if (this._controller === null
  162. && message.type === EVENTS.permissions
  163. && message.action === PERMISSIONS_ACTIONS.request) {
  164. const userId = participant.getId();
  165. this.emit(RemoteControlEvents.ACTIVE_CHANGED, true);
  166. APP.store.dispatch(
  167. openRemoteControlAuthorizationDialog(userId));
  168. } else if (this._controller === participant.getId()) {
  169. if (message.type === EVENTS.stop) {
  170. this._stop();
  171. } else { // forward the message
  172. transport.sendEvent(message);
  173. }
  174. } // else ignore
  175. } else {
  176. logger.log('Remote control message is ignored because remote '
  177. + 'control is disabled', message);
  178. }
  179. }
  180. /**
  181. * Denies remote control access for user associated with the passed user id.
  182. *
  183. * @param {string} userId - The id associated with the user who sent the
  184. * request for remote control authorization.
  185. * @returns {void}
  186. */
  187. deny(userId: string) {
  188. this.emit(RemoteControlEvents.ACTIVE_CHANGED, false);
  189. this.sendRemoteControlEndpointMessage(userId, {
  190. type: EVENTS.permissions,
  191. action: PERMISSIONS_ACTIONS.deny
  192. });
  193. }
  194. /**
  195. * Grants remote control access to user associated with the passed user id.
  196. *
  197. * @param {string} userId - The id associated with the user who sent the
  198. * request for remote control authorization.
  199. * @returns {void}
  200. */
  201. grant(userId: string) {
  202. APP.conference.addConferenceListener(ConferenceEvents.USER_LEFT,
  203. this._userLeftListener);
  204. this._controller = userId;
  205. logger.log(`Remote control permissions granted to: ${userId}`);
  206. let promise;
  207. if (APP.conference.isSharingScreen
  208. && APP.conference.getDesktopSharingSourceType() === 'screen') {
  209. promise = this._sendStartRequest();
  210. } else {
  211. promise = APP.conference.toggleScreenSharing(
  212. true,
  213. {
  214. desktopSharingSources: [ 'screen' ]
  215. })
  216. .then(() => this._sendStartRequest());
  217. }
  218. promise
  219. .then(() =>
  220. this.sendRemoteControlEndpointMessage(userId, {
  221. type: EVENTS.permissions,
  222. action: PERMISSIONS_ACTIONS.grant
  223. })
  224. )
  225. .catch(error => {
  226. logger.error(error);
  227. this.sendRemoteControlEndpointMessage(userId, {
  228. type: EVENTS.permissions,
  229. action: PERMISSIONS_ACTIONS.error
  230. });
  231. APP.UI.messageHandler.notify(
  232. 'dialog.remoteControlTitle',
  233. 'dialog.startRemoteControlErrorMessage'
  234. );
  235. this._stop(true);
  236. });
  237. }
  238. /**
  239. * Sends remote control start request.
  240. *
  241. * @returns {Promise}
  242. */
  243. _sendStartRequest() {
  244. return transport.sendRequest({
  245. name: REMOTE_CONTROL_MESSAGE_NAME,
  246. type: REQUESTS.start,
  247. sourceId: APP.conference.getDesktopSharingSourceId()
  248. });
  249. }
  250. /**
  251. * Handles remote control events from the external app. Currently only
  252. * events with type EVENTS.supported and EVENTS.stop are
  253. * supported.
  254. *
  255. * @param {RemoteControlEvent} event - The remote control event.
  256. * @returns {void}
  257. */
  258. _onRemoteControlAPIEvent(event: Object) {
  259. switch (event.type) {
  260. case EVENTS.supported:
  261. this._onRemoteControlSupported();
  262. break;
  263. case EVENTS.stop:
  264. this.stop();
  265. break;
  266. }
  267. }
  268. /**
  269. * Handles events for support for executing remote control events into
  270. * the wrapper application.
  271. *
  272. * @returns {void}
  273. */
  274. _onRemoteControlSupported() {
  275. logger.log('Remote Control supported.');
  276. if (config.disableRemoteControl) {
  277. logger.log('Remote Control disabled.');
  278. } else {
  279. this._enable(true);
  280. }
  281. }
  282. /**
  283. * Calls the stop method if the other side have left.
  284. *
  285. * @param {string} id - The user id for the participant that have left.
  286. * @returns {void}
  287. */
  288. _onUserLeft(id: string) {
  289. if (this._controller === id) {
  290. this._stop();
  291. }
  292. }
  293. /**
  294. * Handles hangup events. Disables the receiver.
  295. *
  296. * @returns {void}
  297. */
  298. _onHangup() {
  299. this._enable(false);
  300. }
  301. }