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 11KB

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