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

Receiver.js 7.2KB

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