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

Receiver.js 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /* global APP, JitsiMeetJS, interfaceConfig */
  2. const logger = require("jitsi-meet-logger").getLogger(__filename);
  3. import {DISCO_REMOTE_CONTROL_FEATURE, REMOTE_CONTROL_EVENT_TYPE, EVENT_TYPES,
  4. PERMISSIONS_ACTIONS} from "../../service/remotecontrol/Constants";
  5. import RemoteControlParticipant from "./RemoteControlParticipant";
  6. const ConferenceEvents = JitsiMeetJS.events.conference;
  7. /**
  8. * This class represents the receiver party for a remote controller session.
  9. * It handles "remote-control-event" events and sends them to the
  10. * API module. From there the events can be received from wrapper application
  11. * and executed.
  12. */
  13. export default class Receiver extends RemoteControlParticipant {
  14. /**
  15. * Creates new instance.
  16. * @constructor
  17. */
  18. constructor() {
  19. super();
  20. this.controller = null;
  21. this._remoteControlEventsListener
  22. = this._onRemoteControlEvent.bind(this);
  23. this._userLeftListener = this._onUserLeft.bind(this);
  24. }
  25. /**
  26. * Enables / Disables the remote control
  27. * @param {boolean} enabled the new state.
  28. */
  29. enable(enabled) {
  30. if(this.enabled !== enabled) {
  31. this.enabled = enabled;
  32. }
  33. if(enabled === true) {
  34. logger.log("Remote control receiver enabled.");
  35. // Announce remote control support.
  36. APP.connection.addFeature(DISCO_REMOTE_CONTROL_FEATURE, true);
  37. APP.conference.addConferenceListener(
  38. ConferenceEvents.ENDPOINT_MESSAGE_RECEIVED,
  39. this._remoteControlEventsListener);
  40. } else {
  41. logger.log("Remote control receiver disabled.");
  42. this._stop(true);
  43. APP.connection.removeFeature(DISCO_REMOTE_CONTROL_FEATURE);
  44. APP.conference.removeConferenceListener(
  45. ConferenceEvents.ENDPOINT_MESSAGE_RECEIVED,
  46. this._remoteControlEventsListener);
  47. }
  48. }
  49. /**
  50. * Removes the listener for ConferenceEvents.ENDPOINT_MESSAGE_RECEIVED
  51. * events. Sends stop message to the wrapper application. Optionally
  52. * displays dialog for informing the user that remote control session
  53. * ended.
  54. * @param {boolean} dontShowDialog - if true the dialog won't be displayed.
  55. */
  56. _stop(dontShowDialog = false) {
  57. if(!this.controller) {
  58. return;
  59. }
  60. logger.log("Remote control receiver stop.");
  61. this.controller = null;
  62. APP.conference.removeConferenceListener(ConferenceEvents.USER_LEFT,
  63. this._userLeftListener);
  64. APP.API.sendRemoteControlEvent({
  65. type: EVENT_TYPES.stop
  66. });
  67. if(!dontShowDialog) {
  68. APP.UI.messageHandler.openMessageDialog(
  69. "dialog.remoteControlTitle",
  70. "dialog.remoteControlStopMessage"
  71. );
  72. }
  73. }
  74. /**
  75. * Calls this._stop() and sends stop message to the controller participant
  76. */
  77. stop() {
  78. if(!this.controller) {
  79. return;
  80. }
  81. this._sendRemoteControlEvent(this.controller, {
  82. type: EVENT_TYPES.stop
  83. });
  84. this._stop();
  85. }
  86. /**
  87. * Listens for data channel EndpointMessage events. Handles only events of
  88. * type remote control. Sends "remote-control-event" events to the API
  89. * module.
  90. * @param {JitsiParticipant} participant the controller participant
  91. * @param {Object} event EndpointMessage event from the data channels.
  92. * @property {string} type property. The function process only events of
  93. * type REMOTE_CONTROL_EVENT_TYPE
  94. * @property {RemoteControlEvent} event - the remote control event.
  95. */
  96. _onRemoteControlEvent(participant, event) {
  97. if(this.enabled && event.type === REMOTE_CONTROL_EVENT_TYPE) {
  98. const remoteControlEvent = event.event;
  99. if(this.controller === null
  100. && remoteControlEvent.type === EVENT_TYPES.permissions
  101. && remoteControlEvent.action === PERMISSIONS_ACTIONS.request) {
  102. remoteControlEvent.userId = participant.getId();
  103. remoteControlEvent.userJID = participant.getJid();
  104. remoteControlEvent.displayName = participant.getDisplayName()
  105. || interfaceConfig.DEFAULT_REMOTE_DISPLAY_NAME;
  106. remoteControlEvent.screenSharing
  107. = APP.conference.isSharingScreen;
  108. } else if(this.controller !== participant.getId()) {
  109. return;
  110. } else if(remoteControlEvent.type === EVENT_TYPES.stop) {
  111. this._stop();
  112. return;
  113. }
  114. APP.API.sendRemoteControlEvent(remoteControlEvent);
  115. } else if(event.type === REMOTE_CONTROL_EVENT_TYPE) {
  116. logger.debug("Remote control event is ignored because remote "
  117. + "control is disabled", event);
  118. }
  119. }
  120. /**
  121. * Handles remote control permission events received from the API module.
  122. * @param {String} userId the user id of the participant related to the
  123. * event.
  124. * @param {PERMISSIONS_ACTIONS} action the action related to the event.
  125. */
  126. _onRemoteControlPermissionsEvent(userId, action) {
  127. if(action === PERMISSIONS_ACTIONS.grant) {
  128. APP.conference.addConferenceListener(ConferenceEvents.USER_LEFT,
  129. this._userLeftListener);
  130. this.controller = userId;
  131. logger.debug("Remote control permissions granted to: " + userId);
  132. if(!APP.conference.isSharingScreen) {
  133. APP.conference.toggleScreenSharing();
  134. APP.conference.screenSharingPromise.then(() => {
  135. if(APP.conference.isSharingScreen) {
  136. this._sendRemoteControlEvent(userId, {
  137. type: EVENT_TYPES.permissions,
  138. action: action
  139. });
  140. } else {
  141. this._sendRemoteControlEvent(userId, {
  142. type: EVENT_TYPES.permissions,
  143. action: PERMISSIONS_ACTIONS.error
  144. });
  145. }
  146. }).catch(() => {
  147. this._sendRemoteControlEvent(userId, {
  148. type: EVENT_TYPES.permissions,
  149. action: PERMISSIONS_ACTIONS.error
  150. });
  151. });
  152. return;
  153. }
  154. }
  155. this._sendRemoteControlEvent(userId, {
  156. type: EVENT_TYPES.permissions,
  157. action: action
  158. });
  159. }
  160. /**
  161. * Calls the stop method if the other side have left.
  162. * @param {string} id - the user id for the participant that have left
  163. */
  164. _onUserLeft(id) {
  165. if(this.controller === id) {
  166. this._stop();
  167. }
  168. }
  169. }