Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

Receiver.js 9.7KB

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