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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /* global APP, config, interfaceConfig, JitsiMeetJS */
  2. import * as JitsiMeetConferenceEvents from '../../ConferenceEvents';
  3. import {
  4. DISCO_REMOTE_CONTROL_FEATURE,
  5. EVENT_TYPES,
  6. PERMISSIONS_ACTIONS,
  7. REMOTE_CONTROL_EVENT_NAME
  8. } from '../../service/remotecontrol/Constants';
  9. import { getJitsiMeetTransport } from '../transport';
  10. import RemoteControlParticipant from './RemoteControlParticipant';
  11. const ConferenceEvents = JitsiMeetJS.events.conference;
  12. const logger = require("jitsi-meet-logger").getLogger(__filename);
  13. /**
  14. * The transport instance used for communication with external apps.
  15. *
  16. * @type {Transport}
  17. */
  18. const transport = getJitsiMeetTransport();
  19. /**
  20. * This class represents the receiver party for a remote controller session.
  21. * It handles "remote-control-event" events and sends them to the
  22. * API module. From there the events can be received from wrapper application
  23. * and executed.
  24. */
  25. export default class Receiver extends RemoteControlParticipant {
  26. /**
  27. * Creates new instance.
  28. * @constructor
  29. */
  30. constructor() {
  31. super();
  32. this.controller = null;
  33. this._remoteControlEventsListener
  34. = this._onRemoteControlEvent.bind(this);
  35. this._userLeftListener = this._onUserLeft.bind(this);
  36. this._hangupListener = this._onHangup.bind(this);
  37. // We expect here that even if we receive the supported event earlier
  38. // it will be cached and we'll receive it.
  39. transport.on('event', event => {
  40. if (event.name === REMOTE_CONTROL_EVENT_NAME) {
  41. this._onRemoteControlAPIEvent(event);
  42. return true;
  43. }
  44. return false;
  45. });
  46. }
  47. /**
  48. * Enables / Disables the remote control
  49. * @param {boolean} enabled the new state.
  50. */
  51. _enable(enabled) {
  52. if(this.enabled === enabled) {
  53. return;
  54. }
  55. this.enabled = enabled;
  56. if(enabled === true) {
  57. logger.log("Remote control receiver enabled.");
  58. // Announce remote control support.
  59. APP.connection.addFeature(DISCO_REMOTE_CONTROL_FEATURE, true);
  60. APP.conference.addConferenceListener(
  61. ConferenceEvents.ENDPOINT_MESSAGE_RECEIVED,
  62. this._remoteControlEventsListener);
  63. APP.conference.addListener(JitsiMeetConferenceEvents.BEFORE_HANGUP,
  64. this._hangupListener);
  65. } else {
  66. logger.log("Remote control receiver disabled.");
  67. this._stop(true);
  68. APP.connection.removeFeature(DISCO_REMOTE_CONTROL_FEATURE);
  69. APP.conference.removeConferenceListener(
  70. ConferenceEvents.ENDPOINT_MESSAGE_RECEIVED,
  71. this._remoteControlEventsListener);
  72. APP.conference.removeListener(
  73. JitsiMeetConferenceEvents.BEFORE_HANGUP,
  74. this._hangupListener);
  75. }
  76. }
  77. /**
  78. * Removes the listener for ConferenceEvents.ENDPOINT_MESSAGE_RECEIVED
  79. * events. Sends stop message to the wrapper application. Optionally
  80. * displays dialog for informing the user that remote control session
  81. * ended.
  82. * @param {boolean} dontShowDialog - if true the dialog won't be displayed.
  83. */
  84. _stop(dontShowDialog = false) {
  85. if(!this.controller) {
  86. return;
  87. }
  88. logger.log("Remote control receiver stop.");
  89. this.controller = null;
  90. APP.conference.removeConferenceListener(ConferenceEvents.USER_LEFT,
  91. this._userLeftListener);
  92. transport.sendEvent({
  93. name: REMOTE_CONTROL_EVENT_NAME,
  94. type: EVENT_TYPES.stop
  95. });
  96. if(!dontShowDialog) {
  97. APP.UI.messageHandler.openMessageDialog(
  98. "dialog.remoteControlTitle",
  99. "dialog.remoteControlStopMessage"
  100. );
  101. }
  102. }
  103. /**
  104. * Calls this._stop() and sends stop message to the controller participant
  105. */
  106. stop() {
  107. if(!this.controller) {
  108. return;
  109. }
  110. this._sendRemoteControlEvent(this.controller, {
  111. type: EVENT_TYPES.stop
  112. });
  113. this._stop();
  114. }
  115. /**
  116. * Listens for data channel EndpointMessage events. Handles only events of
  117. * type remote control. Sends "remote-control-event" events to the API
  118. * module.
  119. * @param {JitsiParticipant} participant the controller participant
  120. * @param {Object} event EndpointMessage event from the data channels.
  121. * @property {string} type property. The function process only events with
  122. * name REMOTE_CONTROL_EVENT_NAME
  123. * @property {RemoteControlEvent} event - the remote control event.
  124. */
  125. _onRemoteControlEvent(participant, event) {
  126. if (event.name !== REMOTE_CONTROL_EVENT_NAME) {
  127. return;
  128. }
  129. const remoteControlEvent = Object.assign({}, event);
  130. if (this.enabled) {
  131. if (this.controller === null
  132. && event.type === EVENT_TYPES.permissions
  133. && event.action === PERMISSIONS_ACTIONS.request) {
  134. // FIXME: Maybe use transport.sendRequest in this case???
  135. remoteControlEvent.userId = participant.getId();
  136. remoteControlEvent.userJID = participant.getJid();
  137. remoteControlEvent.displayName = participant.getDisplayName()
  138. || interfaceConfig.DEFAULT_REMOTE_DISPLAY_NAME;
  139. remoteControlEvent.screenSharing
  140. = APP.conference.isSharingScreen;
  141. } else if (this.controller !== participant.getId()) {
  142. return;
  143. } else if (event.type === EVENT_TYPES.stop) {
  144. this._stop();
  145. return;
  146. }
  147. transport.sendEvent(remoteControlEvent);
  148. } else {
  149. logger.log("Remote control event is ignored because remote "
  150. + "control is disabled", event);
  151. }
  152. }
  153. /**
  154. * Handles remote control permission events.
  155. * @param {String} userId the user id of the participant related to the
  156. * event.
  157. * @param {PERMISSIONS_ACTIONS} action the action related to the event.
  158. */
  159. _onRemoteControlPermissionsEvent(userId, action) {
  160. if (action === PERMISSIONS_ACTIONS.grant) {
  161. APP.conference.addConferenceListener(ConferenceEvents.USER_LEFT,
  162. this._userLeftListener);
  163. this.controller = userId;
  164. logger.log("Remote control permissions granted to: " + userId);
  165. if(!APP.conference.isSharingScreen) {
  166. APP.conference.toggleScreenSharing();
  167. APP.conference.screenSharingPromise.then(() => {
  168. if(APP.conference.isSharingScreen) {
  169. this._sendRemoteControlEvent(userId, {
  170. type: EVENT_TYPES.permissions,
  171. action: action
  172. });
  173. } else {
  174. this._sendRemoteControlEvent(userId, {
  175. type: EVENT_TYPES.permissions,
  176. action: PERMISSIONS_ACTIONS.error
  177. });
  178. }
  179. }).catch(() => {
  180. this._sendRemoteControlEvent(userId, {
  181. type: EVENT_TYPES.permissions,
  182. action: PERMISSIONS_ACTIONS.error
  183. });
  184. });
  185. return;
  186. }
  187. }
  188. this._sendRemoteControlEvent(userId, {
  189. type: EVENT_TYPES.permissions,
  190. action
  191. });
  192. }
  193. /**
  194. * Handles remote control events from the external app. Currently only
  195. * events with type = EVENT_TYPES.supported or EVENT_TYPES.permissions
  196. * @param {RemoteControlEvent} event the remote control event.
  197. */
  198. _onRemoteControlAPIEvent(event) {
  199. switch(event.type) {
  200. case EVENT_TYPES.permissions:
  201. this._onRemoteControlPermissionsEvent(event.userId, event.action);
  202. break;
  203. case EVENT_TYPES.supported:
  204. this._onRemoteControlSupported();
  205. break;
  206. }
  207. }
  208. /**
  209. * Handles events for support for executing remote control events into
  210. * the wrapper application.
  211. */
  212. _onRemoteControlSupported() {
  213. logger.log("Remote Control supported.");
  214. if (config.disableRemoteControl) {
  215. logger.log("Remote Control disabled.");
  216. } else {
  217. this._enable(true);
  218. }
  219. }
  220. /**
  221. * Calls the stop method if the other side have left.
  222. * @param {string} id - the user id for the participant that have left
  223. */
  224. _onUserLeft(id) {
  225. if(this.controller === id) {
  226. this._stop();
  227. }
  228. }
  229. /**
  230. * Handles hangup events. Disables the receiver.
  231. */
  232. _onHangup() {
  233. this._enable(false);
  234. }
  235. }