Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

Receiver.js 9.9KB

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