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

Receiver.js 7.2KB

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