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.

Receiver.js 10KB

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