您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

Receiver.js 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. /* @flow */
  2. import { getLogger } from 'jitsi-meet-logger';
  3. import * as JitsiMeetConferenceEvents from '../../ConferenceEvents';
  4. import {
  5. JitsiConferenceEvents
  6. } from '../../react/features/base/lib-jitsi-meet';
  7. import {
  8. openRemoteControlAuthorizationDialog
  9. } from '../../react/features/remote-control';
  10. import {
  11. DISCO_REMOTE_CONTROL_FEATURE,
  12. EVENTS,
  13. PERMISSIONS_ACTIONS,
  14. REMOTE_CONTROL_MESSAGE_NAME,
  15. REQUESTS
  16. } from '../../service/remotecontrol/Constants';
  17. import * as RemoteControlEvents
  18. from '../../service/remotecontrol/RemoteControlEvents';
  19. import { Transport, PostMessageTransportBackend } from '../transport';
  20. import RemoteControlParticipant from './RemoteControlParticipant';
  21. declare var APP: Object;
  22. declare var config: Object;
  23. declare var interfaceConfig: Object;
  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. JitsiConferenceEvents.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. JitsiConferenceEvents.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 JitsiConferenceEvents.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(
  116. JitsiConferenceEvents.USER_LEFT,
  117. this._userLeftListener);
  118. transport.sendEvent({
  119. name: REMOTE_CONTROL_MESSAGE_NAME,
  120. type: EVENTS.stop
  121. });
  122. this.emit(RemoteControlEvents.ACTIVE_CHANGED, false);
  123. if (!dontNotify) {
  124. APP.UI.messageHandler.notify(
  125. 'dialog.remoteControlTitle',
  126. 'dialog.remoteControlStopMessage'
  127. );
  128. }
  129. }
  130. /**
  131. * Calls this._stop() and sends stop message to the controller participant.
  132. *
  133. * @returns {void}
  134. */
  135. stop() {
  136. if (!this._controller) {
  137. return;
  138. }
  139. this.sendRemoteControlEndpointMessage(this._controller, {
  140. type: EVENTS.stop
  141. });
  142. this._stop();
  143. }
  144. /**
  145. * Listens for data channel EndpointMessage. Handles only remote control
  146. * messages. Sends the remote control messages to the external app that
  147. * will execute them.
  148. *
  149. * @param {JitsiParticipant} participant - The controller participant.
  150. * @param {Object} message - EndpointMessage from the data channels.
  151. * @param {string} message.name - The function processes only messages with
  152. * name REMOTE_CONTROL_MESSAGE_NAME.
  153. * @returns {void}
  154. */
  155. _onRemoteControlMessage(participant: Object, message: Object) {
  156. if (message.name !== REMOTE_CONTROL_MESSAGE_NAME) {
  157. return;
  158. }
  159. if (this._enabled) {
  160. if (this._controller === null
  161. && message.type === EVENTS.permissions
  162. && message.action === PERMISSIONS_ACTIONS.request) {
  163. const userId = participant.getId();
  164. this.emit(RemoteControlEvents.ACTIVE_CHANGED, true);
  165. APP.store.dispatch(
  166. openRemoteControlAuthorizationDialog(userId));
  167. } else if (this._controller === participant.getId()) {
  168. if (message.type === EVENTS.stop) {
  169. this._stop();
  170. } else { // forward the message
  171. transport.sendEvent(message);
  172. }
  173. } // else ignore
  174. } else {
  175. logger.log('Remote control message is ignored because remote '
  176. + 'control is disabled', message);
  177. }
  178. }
  179. /**
  180. * Denies remote control access for user associated with the passed user id.
  181. *
  182. * @param {string} userId - The id associated with the user who sent the
  183. * request for remote control authorization.
  184. * @returns {void}
  185. */
  186. deny(userId: string) {
  187. this.emit(RemoteControlEvents.ACTIVE_CHANGED, false);
  188. this.sendRemoteControlEndpointMessage(userId, {
  189. type: EVENTS.permissions,
  190. action: PERMISSIONS_ACTIONS.deny
  191. });
  192. }
  193. /**
  194. * Grants remote control access to user associated with the passed user id.
  195. *
  196. * @param {string} userId - The id associated with the user who sent the
  197. * request for remote control authorization.
  198. * @returns {void}
  199. */
  200. grant(userId: string) {
  201. APP.conference.addConferenceListener(JitsiConferenceEvents.USER_LEFT,
  202. this._userLeftListener);
  203. this._controller = userId;
  204. logger.log(`Remote control permissions granted to: ${userId}`);
  205. let promise;
  206. if (APP.conference.isSharingScreen
  207. && APP.conference.getDesktopSharingSourceType() === 'screen') {
  208. promise = this._sendStartRequest();
  209. } else {
  210. promise = APP.conference.toggleScreenSharing(
  211. true,
  212. {
  213. desktopSharingSources: [ 'screen' ]
  214. })
  215. .then(() => this._sendStartRequest());
  216. }
  217. promise
  218. .then(() =>
  219. this.sendRemoteControlEndpointMessage(userId, {
  220. type: EVENTS.permissions,
  221. action: PERMISSIONS_ACTIONS.grant
  222. })
  223. )
  224. .catch(error => {
  225. logger.error(error);
  226. this.sendRemoteControlEndpointMessage(userId, {
  227. type: EVENTS.permissions,
  228. action: PERMISSIONS_ACTIONS.error
  229. });
  230. APP.UI.messageHandler.notify(
  231. 'dialog.remoteControlTitle',
  232. 'dialog.startRemoteControlErrorMessage'
  233. );
  234. this._stop(true);
  235. });
  236. }
  237. /**
  238. * Sends remote control start request.
  239. *
  240. * @returns {Promise}
  241. */
  242. _sendStartRequest() {
  243. return transport.sendRequest({
  244. name: REMOTE_CONTROL_MESSAGE_NAME,
  245. type: REQUESTS.start,
  246. sourceId: APP.conference.getDesktopSharingSourceId()
  247. });
  248. }
  249. /**
  250. * Handles remote control events from the external app. Currently only
  251. * events with type EVENTS.supported and EVENTS.stop are
  252. * supported.
  253. *
  254. * @param {RemoteControlEvent} event - The remote control event.
  255. * @returns {void}
  256. */
  257. _onRemoteControlAPIEvent(event: Object) {
  258. switch (event.type) {
  259. case EVENTS.supported:
  260. this._onRemoteControlSupported();
  261. break;
  262. case EVENTS.stop:
  263. this.stop();
  264. break;
  265. }
  266. }
  267. /**
  268. * Handles events for support for executing remote control events into
  269. * the wrapper application.
  270. *
  271. * @returns {void}
  272. */
  273. _onRemoteControlSupported() {
  274. logger.log('Remote Control supported.');
  275. if (config.disableRemoteControl) {
  276. logger.log('Remote Control disabled.');
  277. } else {
  278. this._enable(true);
  279. }
  280. }
  281. /**
  282. * Calls the stop method if the other side have left.
  283. *
  284. * @param {string} id - The user id for the participant that have left.
  285. * @returns {void}
  286. */
  287. _onUserLeft(id: string) {
  288. if (this._controller === id) {
  289. this._stop();
  290. }
  291. }
  292. /**
  293. * Handles hangup events. Disables the receiver.
  294. *
  295. * @returns {void}
  296. */
  297. _onHangup() {
  298. this._enable(false);
  299. }
  300. }