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

Controller.js 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. /* global $, JitsiMeetJS, APP */
  2. const logger = require("jitsi-meet-logger").getLogger(__filename);
  3. import * as KeyCodes from "../keycode/keycode";
  4. import {EVENT_TYPES, REMOTE_CONTROL_EVENT_TYPE, PERMISSIONS_ACTIONS}
  5. from "../../service/remotecontrol/Constants";
  6. import RemoteControlParticipant from "./RemoteControlParticipant";
  7. import UIEvents from "../../service/UI/UIEvents";
  8. const ConferenceEvents = JitsiMeetJS.events.conference;
  9. /**
  10. * Extract the keyboard key from the keyboard event.
  11. * @param event {KeyboardEvent} the event.
  12. * @returns {KEYS} the key that is pressed or undefined.
  13. */
  14. function getKey(event) {
  15. return KeyCodes.keyboardEventToKey(event);
  16. }
  17. /**
  18. * Extract the modifiers from the keyboard event.
  19. * @param event {KeyboardEvent} the event.
  20. * @returns {Array} with possible values: "shift", "control", "alt", "command".
  21. */
  22. function getModifiers(event) {
  23. let modifiers = [];
  24. if(event.shiftKey) {
  25. modifiers.push("shift");
  26. }
  27. if(event.ctrlKey) {
  28. modifiers.push("control");
  29. }
  30. if(event.altKey) {
  31. modifiers.push("alt");
  32. }
  33. if(event.metaKey) {
  34. modifiers.push("command");
  35. }
  36. return modifiers;
  37. }
  38. /**
  39. * This class represents the controller party for a remote controller session.
  40. * It listens for mouse and keyboard events and sends them to the receiver
  41. * party of the remote control session.
  42. */
  43. export default class Controller extends RemoteControlParticipant {
  44. /**
  45. * Creates new instance.
  46. */
  47. constructor() {
  48. super();
  49. this.isCollectingEvents = false;
  50. this.controlledParticipant = null;
  51. this.requestedParticipant = null;
  52. this._stopListener = this._handleRemoteControlStoppedEvent.bind(this);
  53. this._userLeftListener = this._onUserLeft.bind(this);
  54. this._largeVideoChangedListener
  55. = this._onLargeVideoIdChanged.bind(this);
  56. }
  57. /**
  58. * Requests permissions from the remote control receiver side.
  59. * @param {string} userId the user id of the participant that will be
  60. * requested.
  61. * @returns {Promise<boolean>} - resolve values:
  62. * true - accept
  63. * false - deny
  64. * null - the participant has left.
  65. */
  66. requestPermissions(userId) {
  67. if(!this.enabled) {
  68. return Promise.reject(new Error("Remote control is disabled!"));
  69. }
  70. logger.debug("Requsting remote control permissions from: " + userId);
  71. return new Promise((resolve, reject) => {
  72. const clearRequest = () => {
  73. this.requestedParticipant = null;
  74. APP.conference.removeConferenceListener(
  75. ConferenceEvents.ENDPOINT_MESSAGE_RECEIVED,
  76. permissionsReplyListener);
  77. APP.conference.removeConferenceListener(
  78. ConferenceEvents.USER_LEFT,
  79. onUserLeft);
  80. };
  81. const permissionsReplyListener = (participant, event) => {
  82. let result = null;
  83. try {
  84. result = this._handleReply(participant, event);
  85. } catch (e) {
  86. reject(e);
  87. }
  88. if(result !== null) {
  89. clearRequest();
  90. resolve(result);
  91. }
  92. };
  93. const onUserLeft = (id) => {
  94. if(id === this.requestedParticipant) {
  95. clearRequest();
  96. resolve(null);
  97. }
  98. };
  99. APP.conference.addConferenceListener(
  100. ConferenceEvents.ENDPOINT_MESSAGE_RECEIVED,
  101. permissionsReplyListener);
  102. APP.conference.addConferenceListener(ConferenceEvents.USER_LEFT,
  103. onUserLeft);
  104. this.requestedParticipant = userId;
  105. this._sendRemoteControlEvent(userId, {
  106. type: EVENT_TYPES.permissions,
  107. action: PERMISSIONS_ACTIONS.request
  108. }, e => {
  109. clearRequest();
  110. reject(e);
  111. });
  112. });
  113. }
  114. /**
  115. * Handles the reply of the permissions request.
  116. * @param {JitsiParticipant} participant the participant that has sent the
  117. * reply
  118. * @param {object} event the remote control event.
  119. */
  120. _handleReply(participant, event) {
  121. const remoteControlEvent = event.event;
  122. const userId = participant.getId();
  123. if(this.enabled && event.type === REMOTE_CONTROL_EVENT_TYPE
  124. && remoteControlEvent.type === EVENT_TYPES.permissions
  125. && userId === this.requestedParticipant) {
  126. switch(remoteControlEvent.action) {
  127. case PERMISSIONS_ACTIONS.grant: {
  128. this.controlledParticipant = userId;
  129. logger.debug("Remote control permissions granted to: "
  130. + userId);
  131. this._start();
  132. return true;
  133. }
  134. case PERMISSIONS_ACTIONS.deny:
  135. return false;
  136. case PERMISSIONS_ACTIONS.error:
  137. throw new Error("Error occurred on receiver side");
  138. default:
  139. throw new Error("Unknown reply received!");
  140. }
  141. } else {
  142. //different message type or another user -> ignoring the message
  143. return null;
  144. }
  145. }
  146. /**
  147. * Handles remote control stopped.
  148. * @param {JitsiParticipant} participant the participant that has sent the
  149. * event
  150. * @param {object} event the the remote control event.
  151. */
  152. _handleRemoteControlStoppedEvent(participant, event) {
  153. if(this.enabled && event.type === REMOTE_CONTROL_EVENT_TYPE
  154. && event.event.type === EVENT_TYPES.stop
  155. && participant.getId() === this.controlledParticipant) {
  156. this._stop();
  157. }
  158. }
  159. /**
  160. * Starts processing the mouse and keyboard events. Sets conference
  161. * listeners. Disables keyboard events.
  162. */
  163. _start() {
  164. logger.log("Starting remote control controller.");
  165. APP.UI.addListener(UIEvents.LARGE_VIDEO_ID_CHANGED,
  166. this._largeVideoChangedListener);
  167. APP.conference.addConferenceListener(
  168. ConferenceEvents.ENDPOINT_MESSAGE_RECEIVED,
  169. this._stopListener);
  170. APP.conference.addConferenceListener(ConferenceEvents.USER_LEFT,
  171. this._userLeftListener);
  172. this.resume();
  173. }
  174. /**
  175. * Disables the keyboatd shortcuts. Starts collecting remote control
  176. * events.
  177. *
  178. * It can be used to resume an active remote control session wchich was
  179. * paused with this.pause().
  180. */
  181. resume() {
  182. if(!this.enabled || this.isCollectingEvents) {
  183. return;
  184. }
  185. logger.log("Resuming remote control controller.");
  186. this.isCollectingEvents = true;
  187. APP.keyboardshortcut.enable(false);
  188. this.area = $("#largeVideoWrapper");
  189. this.area.mousemove(event => {
  190. const position = this.area.position();
  191. this._sendRemoteControlEvent(this.controlledParticipant, {
  192. type: EVENT_TYPES.mousemove,
  193. x: (event.pageX - position.left)/this.area.width(),
  194. y: (event.pageY - position.top)/this.area.height()
  195. });
  196. });
  197. this.area.mousedown(this._onMouseClickHandler.bind(this,
  198. EVENT_TYPES.mousedown));
  199. this.area.mouseup(this._onMouseClickHandler.bind(this,
  200. EVENT_TYPES.mouseup));
  201. this.area.dblclick(
  202. this._onMouseClickHandler.bind(this, EVENT_TYPES.mousedblclick));
  203. this.area.contextmenu(() => false);
  204. this.area[0].onmousewheel = event => {
  205. this._sendRemoteControlEvent(this.controlledParticipant, {
  206. type: EVENT_TYPES.mousescroll,
  207. x: event.deltaX,
  208. y: event.deltaY
  209. });
  210. };
  211. $(window).keydown(this._onKeyPessHandler.bind(this,
  212. EVENT_TYPES.keydown));
  213. $(window).keyup(this._onKeyPessHandler.bind(this, EVENT_TYPES.keyup));
  214. }
  215. /**
  216. * Stops processing the mouse and keyboard events. Removes added listeners.
  217. * Enables the keyboard shortcuts. Displays dialog to notify the user that
  218. * remote control session has ended.
  219. */
  220. _stop() {
  221. if(!this.controlledParticipant) {
  222. return;
  223. }
  224. logger.log("Stopping remote control controller.");
  225. APP.UI.removeListener(UIEvents.LARGE_VIDEO_ID_CHANGED,
  226. this._largeVideoChangedListener);
  227. APP.conference.removeConferenceListener(
  228. ConferenceEvents.ENDPOINT_MESSAGE_RECEIVED,
  229. this._stopListener);
  230. APP.conference.removeConferenceListener(ConferenceEvents.USER_LEFT,
  231. this._userLeftListener);
  232. this.controlledParticipant = null;
  233. this.pause();
  234. APP.UI.messageHandler.openMessageDialog(
  235. "dialog.remoteControlTitle",
  236. "dialog.remoteControlStopMessage"
  237. );
  238. }
  239. /**
  240. * Executes this._stop() mehtod:
  241. * Stops processing the mouse and keyboard events. Removes added listeners.
  242. * Enables the keyboard shortcuts. Displays dialog to notify the user that
  243. * remote control session has ended.
  244. *
  245. * In addition:
  246. * Sends stop message to the controlled participant.
  247. */
  248. stop() {
  249. if(!this.controlledParticipant) {
  250. return;
  251. }
  252. this._sendRemoteControlEvent(this.controlledParticipant, {
  253. type: EVENT_TYPES.stop
  254. });
  255. this._stop();
  256. }
  257. /**
  258. * Pauses the collecting of events and enables the keyboard shortcus. But
  259. * it doesn't removes any other listeners. Basically the remote control
  260. * session will be still active after this.pause(), but no events from the
  261. * controller side will be captured and sent.
  262. *
  263. * You can resume the collecting of the events with this.resume().
  264. */
  265. pause() {
  266. if(!this.controlledParticipant) {
  267. return;
  268. }
  269. logger.log("Pausing remote control controller.");
  270. this.isCollectingEvents = false;
  271. APP.keyboardshortcut.enable(true);
  272. this.area.off( "mousemove" );
  273. this.area.off( "mousedown" );
  274. this.area.off( "mouseup" );
  275. this.area.off( "contextmenu" );
  276. this.area.off( "dblclick" );
  277. $(window).off( "keydown");
  278. $(window).off( "keyup");
  279. this.area[0].onmousewheel = undefined;
  280. }
  281. /**
  282. * Handler for mouse click events.
  283. * @param {String} type the type of event ("mousedown"/"mouseup")
  284. * @param {Event} event the mouse event.
  285. */
  286. _onMouseClickHandler(type, event) {
  287. this._sendRemoteControlEvent(this.controlledParticipant, {
  288. type: type,
  289. button: event.which
  290. });
  291. }
  292. /**
  293. * Returns true if the remote control session is started.
  294. * @returns {boolean}
  295. */
  296. isStarted() {
  297. return this.controlledParticipant !== null;
  298. }
  299. /**
  300. * Returns the id of the requested participant
  301. * @returns {string} this.requestedParticipant
  302. */
  303. getRequestedParticipant() {
  304. return this.requestedParticipant;
  305. }
  306. /**
  307. * Handler for key press events.
  308. * @param {String} type the type of event ("keydown"/"keyup")
  309. * @param {Event} event the key event.
  310. */
  311. _onKeyPessHandler(type, event) {
  312. this._sendRemoteControlEvent(this.controlledParticipant, {
  313. type: type,
  314. key: getKey(event),
  315. modifiers: getModifiers(event),
  316. });
  317. }
  318. /**
  319. * Calls the stop method if the other side have left.
  320. * @param {string} id - the user id for the participant that have left
  321. */
  322. _onUserLeft(id) {
  323. if(this.controlledParticipant === id) {
  324. this._stop();
  325. }
  326. }
  327. /**
  328. * Handles changes of the participant displayed on the large video.
  329. * @param {string} id - the user id for the participant that is displayed.
  330. */
  331. _onLargeVideoIdChanged(id) {
  332. if (!this.controlledParticipant) {
  333. return;
  334. }
  335. if(this.controlledParticipant == id) {
  336. this.resume();
  337. } else {
  338. this.pause();
  339. }
  340. }
  341. }