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

Receiver.js 5.9KB

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