You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

external_api.js 42KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293
  1. import { jitsiLocalStorage } from '@jitsi/js-utils/jitsi-local-storage';
  2. import EventEmitter from 'events';
  3. import { urlObjectToString } from '../../../react/features/base/util/uri';
  4. import {
  5. PostMessageTransportBackend,
  6. Transport
  7. } from '../../transport';
  8. import {
  9. getAvailableDevices,
  10. getCurrentDevices,
  11. isDeviceChangeAvailable,
  12. isDeviceListAvailable,
  13. isMultipleAudioInputSupported,
  14. setAudioInputDevice,
  15. setAudioOutputDevice,
  16. setVideoInputDevice
  17. } from './functions';
  18. const ALWAYS_ON_TOP_FILENAMES = [
  19. 'css/all.css', 'libs/alwaysontop.min.js'
  20. ];
  21. /**
  22. * Maps the names of the commands expected by the API with the name of the
  23. * commands expected by jitsi-meet.
  24. */
  25. const commands = {
  26. addBreakoutRoom: 'add-breakout-room',
  27. answerKnockingParticipant: 'answer-knocking-participant',
  28. approveVideo: 'approve-video',
  29. askToUnmute: 'ask-to-unmute',
  30. autoAssignToBreakoutRooms: 'auto-assign-to-breakout-rooms',
  31. avatarUrl: 'avatar-url',
  32. cancelPrivateChat: 'cancel-private-chat',
  33. closeBreakoutRoom: 'close-breakout-room',
  34. displayName: 'display-name',
  35. e2eeKey: 'e2ee-key',
  36. email: 'email',
  37. grantModerator: 'grant-moderator',
  38. hangup: 'video-hangup',
  39. initiatePrivateChat: 'initiate-private-chat',
  40. joinBreakoutRoom: 'join-breakout-room',
  41. localSubject: 'local-subject',
  42. kickParticipant: 'kick-participant',
  43. muteEveryone: 'mute-everyone',
  44. overwriteConfig: 'overwrite-config',
  45. overwriteNames: 'overwrite-names',
  46. password: 'password',
  47. pinParticipant: 'pin-participant',
  48. rejectParticipant: 'reject-participant',
  49. removeBreakoutRoom: 'remove-breakout-room',
  50. resizeLargeVideo: 'resize-large-video',
  51. sendChatMessage: 'send-chat-message',
  52. sendEndpointTextMessage: 'send-endpoint-text-message',
  53. sendParticipantToRoom: 'send-participant-to-room',
  54. sendTones: 'send-tones',
  55. setFollowMe: 'set-follow-me',
  56. setLargeVideoParticipant: 'set-large-video-participant',
  57. setMediaEncryptionKey: 'set-media-encryption-key',
  58. setParticipantVolume: 'set-participant-volume',
  59. setSubtitles: 'set-subtitles',
  60. setTileView: 'set-tile-view',
  61. setVideoQuality: 'set-video-quality',
  62. startRecording: 'start-recording',
  63. startShareVideo: 'start-share-video',
  64. stopRecording: 'stop-recording',
  65. stopShareVideo: 'stop-share-video',
  66. subject: 'subject',
  67. submitFeedback: 'submit-feedback',
  68. toggleAudio: 'toggle-audio',
  69. toggleCamera: 'toggle-camera',
  70. toggleCameraMirror: 'toggle-camera-mirror',
  71. toggleChat: 'toggle-chat',
  72. toggleE2EE: 'toggle-e2ee',
  73. toggleFilmStrip: 'toggle-film-strip',
  74. toggleLobby: 'toggle-lobby',
  75. toggleModeration: 'toggle-moderation',
  76. toggleParticipantsPane: 'toggle-participants-pane',
  77. toggleRaiseHand: 'toggle-raise-hand',
  78. toggleShareAudio: 'toggle-share-audio',
  79. toggleShareScreen: 'toggle-share-screen',
  80. toggleSubtitles: 'toggle-subtitles',
  81. toggleTileView: 'toggle-tile-view',
  82. toggleVirtualBackgroundDialog: 'toggle-virtual-background',
  83. toggleVideo: 'toggle-video'
  84. };
  85. /**
  86. * Maps the names of the events expected by the API with the name of the
  87. * events expected by jitsi-meet.
  88. */
  89. const events = {
  90. 'avatar-changed': 'avatarChanged',
  91. 'audio-availability-changed': 'audioAvailabilityChanged',
  92. 'audio-mute-status-changed': 'audioMuteStatusChanged',
  93. 'breakout-rooms-updated': 'breakoutRoomsUpdated',
  94. 'browser-support': 'browserSupport',
  95. 'camera-error': 'cameraError',
  96. 'chat-updated': 'chatUpdated',
  97. 'content-sharing-participants-changed': 'contentSharingParticipantsChanged',
  98. 'data-channel-opened': 'dataChannelOpened',
  99. 'device-list-changed': 'deviceListChanged',
  100. 'display-name-change': 'displayNameChange',
  101. 'email-change': 'emailChange',
  102. 'error-occurred': 'errorOccurred',
  103. 'endpoint-text-message-received': 'endpointTextMessageReceived',
  104. 'face-landmark-detected': 'faceLandmarkDetected',
  105. 'feedback-submitted': 'feedbackSubmitted',
  106. 'feedback-prompt-displayed': 'feedbackPromptDisplayed',
  107. 'filmstrip-display-changed': 'filmstripDisplayChanged',
  108. 'iframe-dock-state-changed': 'iframeDockStateChanged',
  109. 'incoming-message': 'incomingMessage',
  110. 'knocking-participant': 'knockingParticipant',
  111. 'log': 'log',
  112. 'mic-error': 'micError',
  113. 'moderation-participant-approved': 'moderationParticipantApproved',
  114. 'moderation-participant-rejected': 'moderationParticipantRejected',
  115. 'moderation-status-changed': 'moderationStatusChanged',
  116. 'mouse-enter': 'mouseEnter',
  117. 'mouse-leave': 'mouseLeave',
  118. 'mouse-move': 'mouseMove',
  119. 'outgoing-message': 'outgoingMessage',
  120. 'participant-joined': 'participantJoined',
  121. 'participant-kicked-out': 'participantKickedOut',
  122. 'participant-left': 'participantLeft',
  123. 'participant-role-changed': 'participantRoleChanged',
  124. 'password-required': 'passwordRequired',
  125. 'proxy-connection-event': 'proxyConnectionEvent',
  126. 'raise-hand-updated': 'raiseHandUpdated',
  127. 'recording-link-available': 'recordingLinkAvailable',
  128. 'recording-status-changed': 'recordingStatusChanged',
  129. 'video-ready-to-close': 'readyToClose',
  130. 'video-conference-joined': 'videoConferenceJoined',
  131. 'video-conference-left': 'videoConferenceLeft',
  132. 'video-availability-changed': 'videoAvailabilityChanged',
  133. 'video-mute-status-changed': 'videoMuteStatusChanged',
  134. 'video-quality-changed': 'videoQualityChanged',
  135. 'screen-sharing-status-changed': 'screenSharingStatusChanged',
  136. 'dominant-speaker-changed': 'dominantSpeakerChanged',
  137. 'subject-change': 'subjectChange',
  138. 'suspend-detected': 'suspendDetected',
  139. 'tile-view-changed': 'tileViewChanged',
  140. 'toolbar-button-clicked': 'toolbarButtonClicked'
  141. };
  142. /**
  143. * Last id of api object.
  144. *
  145. * @type {number}
  146. */
  147. let id = 0;
  148. /**
  149. * Adds given number to the numberOfParticipants property of given APIInstance.
  150. *
  151. * @param {JitsiMeetExternalAPI} APIInstance - The instance of the API.
  152. * @param {int} number - The number of participants to be added to
  153. * numberOfParticipants property (this parameter can be negative number if the
  154. * numberOfParticipants should be decreased).
  155. * @returns {void}
  156. */
  157. function changeParticipantNumber(APIInstance, number) {
  158. APIInstance._numberOfParticipants += number;
  159. }
  160. /**
  161. * Generates the URL for the iframe.
  162. *
  163. * @param {string} domain - The domain name of the server that hosts the
  164. * conference.
  165. * @param {string} [options] - Another optional parameters.
  166. * @param {Object} [options.configOverwrite] - Object containing configuration
  167. * options defined in config.js to be overridden.
  168. * @param {Object} [options.interfaceConfigOverwrite] - Object containing
  169. * configuration options defined in interface_config.js to be overridden.
  170. * @param {string} [options.jwt] - The JWT token if needed by jitsi-meet for
  171. * authentication.
  172. * @param {string} [options.lang] - The meeting's default language.
  173. * @param {string} [options.roomName] - The name of the room to join.
  174. * @returns {string} The URL.
  175. */
  176. function generateURL(domain, options = {}) {
  177. return urlObjectToString({
  178. ...options,
  179. url: `https://${domain}/#jitsi_meet_external_api_id=${id}`
  180. });
  181. }
  182. /**
  183. * Parses the arguments passed to the constructor. If the old format is used
  184. * the function translates the arguments to the new format.
  185. *
  186. * @param {Array} args - The arguments to be parsed.
  187. * @returns {Object} JS object with properties.
  188. */
  189. function parseArguments(args) {
  190. if (!args.length) {
  191. return {};
  192. }
  193. const firstArg = args[0];
  194. switch (typeof firstArg) {
  195. case 'string': // old arguments format
  196. case 'undefined': {
  197. // Not sure which format but we are trying to parse the old
  198. // format because if the new format is used everything will be undefined
  199. // anyway.
  200. const [
  201. roomName,
  202. width,
  203. height,
  204. parentNode,
  205. configOverwrite,
  206. interfaceConfigOverwrite,
  207. jwt,
  208. onload,
  209. lang
  210. ] = args;
  211. return {
  212. roomName,
  213. width,
  214. height,
  215. parentNode,
  216. configOverwrite,
  217. interfaceConfigOverwrite,
  218. jwt,
  219. onload,
  220. lang
  221. };
  222. }
  223. case 'object': // new arguments format
  224. return args[0];
  225. default:
  226. throw new Error('Can\'t parse the arguments!');
  227. }
  228. }
  229. /**
  230. * Compute valid values for height and width. If a number is specified it's
  231. * treated as pixel units. If the value is expressed in px, em, pt or
  232. * percentage, it's used as is.
  233. *
  234. * @param {any} value - The value to be parsed.
  235. * @returns {string|undefined} The parsed value that can be used for setting
  236. * sizes through the style property. If invalid value is passed the method
  237. * returns undefined.
  238. */
  239. function parseSizeParam(value) {
  240. let parsedValue;
  241. // This regex parses values of the form 100px, 100em, 100pt or 100%.
  242. // Values like 100 or 100px are handled outside of the regex, and
  243. // invalid values will be ignored and the minimum will be used.
  244. const re = /([0-9]*\.?[0-9]+)(em|pt|px|%)$/;
  245. if (typeof value === 'string' && String(value).match(re) !== null) {
  246. parsedValue = value;
  247. } else if (typeof value === 'number') {
  248. parsedValue = `${value}px`;
  249. }
  250. return parsedValue;
  251. }
  252. /**
  253. * The IFrame API interface class.
  254. */
  255. export default class JitsiMeetExternalAPI extends EventEmitter {
  256. /**
  257. * Constructs new API instance. Creates iframe and loads Jitsi Meet in it.
  258. *
  259. * @param {string} domain - The domain name of the server that hosts the
  260. * conference.
  261. * @param {Object} [options] - Optional arguments.
  262. * @param {string} [options.roomName] - The name of the room to join.
  263. * @param {number|string} [options.width] - Width of the iframe. Check
  264. * parseSizeParam for format details.
  265. * @param {number|string} [options.height] - Height of the iframe. Check
  266. * parseSizeParam for format details.
  267. * @param {DOMElement} [options.parentNode] - The node that will contain the
  268. * iframe.
  269. * @param {Object} [options.configOverwrite] - Object containing
  270. * configuration options defined in config.js to be overridden.
  271. * @param {Object} [options.interfaceConfigOverwrite] - Object containing
  272. * configuration options defined in interface_config.js to be overridden.
  273. * @param {string} [options.jwt] - The JWT token if needed by jitsi-meet for
  274. * authentication.
  275. * @param {string} [options.lang] - The meeting's default language.
  276. * @param {string} [options.onload] - The onload function that will listen
  277. * for iframe onload event.
  278. * @param {Array<Object>} [options.invitees] - Array of objects containing
  279. * information about new participants that will be invited in the call.
  280. * @param {Array<Object>} [options.devices] - Array of objects containing
  281. * information about the initial devices that will be used in the call.
  282. * @param {Object} [options.userInfo] - Object containing information about
  283. * the participant opening the meeting.
  284. * @param {string} [options.e2eeKey] - The key used for End-to-End encryption.
  285. * THIS IS EXPERIMENTAL.
  286. */
  287. constructor(domain, ...args) {
  288. super();
  289. const {
  290. roomName = '',
  291. width = '100%',
  292. height = '100%',
  293. parentNode = document.body,
  294. configOverwrite = {},
  295. interfaceConfigOverwrite = {},
  296. jwt = undefined,
  297. lang = undefined,
  298. onload = undefined,
  299. invitees,
  300. devices,
  301. userInfo,
  302. e2eeKey
  303. } = parseArguments(args);
  304. const localStorageContent = jitsiLocalStorage.getItem('jitsiLocalStorage');
  305. this._parentNode = parentNode;
  306. this._url = generateURL(domain, {
  307. configOverwrite,
  308. interfaceConfigOverwrite,
  309. jwt,
  310. lang,
  311. roomName,
  312. devices,
  313. userInfo,
  314. appData: {
  315. localStorageContent
  316. }
  317. });
  318. this._createIFrame(height, width, onload);
  319. this._transport = new Transport({
  320. backend: new PostMessageTransportBackend({
  321. postisOptions: {
  322. allowedOrigin: new URL(this._url).origin,
  323. scope: `jitsi_meet_external_api_${id}`,
  324. window: this._frame.contentWindow
  325. }
  326. })
  327. });
  328. if (Array.isArray(invitees) && invitees.length > 0) {
  329. this.invite(invitees);
  330. }
  331. this._tmpE2EEKey = e2eeKey;
  332. this._isLargeVideoVisible = true;
  333. this._numberOfParticipants = 0;
  334. this._participants = {};
  335. this._myUserID = undefined;
  336. this._onStageParticipant = undefined;
  337. this._setupListeners();
  338. id++;
  339. }
  340. /**
  341. * Creates the iframe element.
  342. *
  343. * @param {number|string} height - The height of the iframe. Check
  344. * parseSizeParam for format details.
  345. * @param {number|string} width - The with of the iframe. Check
  346. * parseSizeParam for format details.
  347. * @param {Function} onload - The function that will listen
  348. * for onload event.
  349. * @returns {void}
  350. *
  351. * @private
  352. */
  353. _createIFrame(height, width, onload) {
  354. const frameName = `jitsiConferenceFrame${id}`;
  355. this._frame = document.createElement('iframe');
  356. this._frame.allow = 'camera; microphone; display-capture; autoplay; clipboard-write';
  357. this._frame.src = this._url;
  358. this._frame.name = frameName;
  359. this._frame.id = frameName;
  360. this._setSize(height, width);
  361. this._frame.setAttribute('allowFullScreen', 'true');
  362. this._frame.style.border = 0;
  363. if (onload) {
  364. // waits for iframe resources to load
  365. // and fires event when it is done
  366. this._frame.onload = onload;
  367. }
  368. this._frame = this._parentNode.appendChild(this._frame);
  369. }
  370. /**
  371. * Returns arrays with the all resources for the always on top feature.
  372. *
  373. * @returns {Array<string>}
  374. */
  375. _getAlwaysOnTopResources() {
  376. const iframeWindow = this._frame.contentWindow;
  377. const iframeDocument = iframeWindow.document;
  378. let baseURL = '';
  379. const base = iframeDocument.querySelector('base');
  380. if (base && base.href) {
  381. baseURL = base.href;
  382. } else {
  383. const { protocol, host } = iframeWindow.location;
  384. baseURL = `${protocol}//${host}`;
  385. }
  386. return ALWAYS_ON_TOP_FILENAMES.map(
  387. filename => new URL(filename, baseURL).href
  388. );
  389. }
  390. /**
  391. * Returns the formatted display name of a participant.
  392. *
  393. * @param {string} participantId - The id of the participant.
  394. * @returns {string} The formatted display name.
  395. */
  396. _getFormattedDisplayName(participantId) {
  397. const { formattedDisplayName }
  398. = this._participants[participantId] || {};
  399. return formattedDisplayName;
  400. }
  401. /**
  402. * Returns the id of the on stage participant.
  403. *
  404. * @returns {string} - The id of the on stage participant.
  405. */
  406. _getOnStageParticipant() {
  407. return this._onStageParticipant;
  408. }
  409. /**
  410. * Getter for the large video element in Jitsi Meet.
  411. *
  412. * @returns {HTMLElement|undefined} - The large video.
  413. */
  414. _getLargeVideo() {
  415. const iframe = this.getIFrame();
  416. if (!this._isLargeVideoVisible
  417. || !iframe
  418. || !iframe.contentWindow
  419. || !iframe.contentWindow.document) {
  420. return;
  421. }
  422. return iframe.contentWindow.document.getElementById('largeVideo');
  423. }
  424. /**
  425. * Getter for participant specific video element in Jitsi Meet.
  426. *
  427. * @param {string|undefined} participantId - Id of participant to return the video for.
  428. *
  429. * @returns {HTMLElement|undefined} - The requested video. Will return the local video
  430. * by default if participantId is undefined.
  431. */
  432. _getParticipantVideo(participantId) {
  433. const iframe = this.getIFrame();
  434. if (!iframe
  435. || !iframe.contentWindow
  436. || !iframe.contentWindow.document) {
  437. return;
  438. }
  439. if (typeof participantId === 'undefined' || participantId === this._myUserID) {
  440. return iframe.contentWindow.document.getElementById('localVideo_container');
  441. }
  442. return iframe.contentWindow.document.querySelector(`#participant_${participantId} video`);
  443. }
  444. /**
  445. * Sets the size of the iframe element.
  446. *
  447. * @param {number|string} height - The height of the iframe.
  448. * @param {number|string} width - The with of the iframe.
  449. * @returns {void}
  450. *
  451. * @private
  452. */
  453. _setSize(height, width) {
  454. const parsedHeight = parseSizeParam(height);
  455. const parsedWidth = parseSizeParam(width);
  456. if (parsedHeight !== undefined) {
  457. this._height = height;
  458. this._frame.style.height = parsedHeight;
  459. }
  460. if (parsedWidth !== undefined) {
  461. this._width = width;
  462. this._frame.style.width = parsedWidth;
  463. }
  464. }
  465. /**
  466. * Setups listeners that are used internally for JitsiMeetExternalAPI.
  467. *
  468. * @returns {void}
  469. *
  470. * @private
  471. */
  472. _setupListeners() {
  473. this._transport.on('event', ({ name, ...data }) => {
  474. const userID = data.id;
  475. switch (name) {
  476. case 'video-conference-joined': {
  477. if (typeof this._tmpE2EEKey !== 'undefined') {
  478. this.executeCommand(commands.e2eeKey, this._tmpE2EEKey);
  479. this._tmpE2EEKey = undefined;
  480. }
  481. this._myUserID = userID;
  482. this._participants[userID] = {
  483. avatarURL: data.avatarURL
  484. };
  485. }
  486. // eslint-disable-next-line no-fallthrough
  487. case 'participant-joined': {
  488. this._participants[userID] = this._participants[userID] || {};
  489. this._participants[userID].displayName = data.displayName;
  490. this._participants[userID].formattedDisplayName
  491. = data.formattedDisplayName;
  492. changeParticipantNumber(this, 1);
  493. break;
  494. }
  495. case 'participant-left':
  496. changeParticipantNumber(this, -1);
  497. delete this._participants[userID];
  498. break;
  499. case 'display-name-change': {
  500. const user = this._participants[userID];
  501. if (user) {
  502. user.displayName = data.displayname;
  503. user.formattedDisplayName = data.formattedDisplayName;
  504. }
  505. break;
  506. }
  507. case 'email-change': {
  508. const user = this._participants[userID];
  509. if (user) {
  510. user.email = data.email;
  511. }
  512. break;
  513. }
  514. case 'avatar-changed': {
  515. const user = this._participants[userID];
  516. if (user) {
  517. user.avatarURL = data.avatarURL;
  518. }
  519. break;
  520. }
  521. case 'on-stage-participant-changed':
  522. this._onStageParticipant = userID;
  523. this.emit('largeVideoChanged');
  524. break;
  525. case 'large-video-visibility-changed':
  526. this._isLargeVideoVisible = data.isVisible;
  527. this.emit('largeVideoChanged');
  528. break;
  529. case 'video-conference-left':
  530. changeParticipantNumber(this, -1);
  531. delete this._participants[this._myUserID];
  532. break;
  533. case 'video-quality-changed':
  534. this._videoQuality = data.videoQuality;
  535. break;
  536. case 'local-storage-changed':
  537. jitsiLocalStorage.setItem('jitsiLocalStorage', data.localStorageContent);
  538. // Since this is internal event we don't need to emit it to the consumer of the API.
  539. return true;
  540. }
  541. const eventName = events[name];
  542. if (eventName) {
  543. this.emit(eventName, data);
  544. return true;
  545. }
  546. return false;
  547. });
  548. }
  549. /**
  550. * Adds event listener to Meet Jitsi.
  551. *
  552. * @param {string} event - The name of the event.
  553. * @param {Function} listener - The listener.
  554. * @returns {void}
  555. *
  556. * @deprecated
  557. * NOTE: This method is not removed for backward comatability purposes.
  558. */
  559. addEventListener(event, listener) {
  560. this.on(event, listener);
  561. }
  562. /**
  563. * Adds event listeners to Meet Jitsi.
  564. *
  565. * @param {Object} listeners - The object key should be the name of
  566. * the event and value - the listener.
  567. * Currently we support the following
  568. * events:
  569. * {@code log} - receives event notifications whenever information has
  570. * been logged and has a log level specified within {@code config.apiLogLevels}.
  571. * The listener will receive object with the following structure:
  572. * {{
  573. * logLevel: the message log level
  574. * arguments: an array of strings that compose the actual log message
  575. * }}
  576. * {@code chatUpdated} - receives event notifications about chat state being
  577. * updated. The listener will receive object with the following structure:
  578. * {{
  579. * 'unreadCount': unreadCounter, // the unread message(s) counter,
  580. * 'isOpen': isOpen, // whether the chat panel is open or not
  581. * }}
  582. * {@code incomingMessage} - receives event notifications about incoming
  583. * messages. The listener will receive object with the following structure:
  584. * {{
  585. * 'from': from,//JID of the user that sent the message
  586. * 'nick': nick,//the nickname of the user that sent the message
  587. * 'message': txt//the text of the message
  588. * }}
  589. * {@code outgoingMessage} - receives event notifications about outgoing
  590. * messages. The listener will receive object with the following structure:
  591. * {{
  592. * 'message': txt//the text of the message
  593. * }}
  594. * {@code displayNameChanged} - receives event notifications about display
  595. * name change. The listener will receive object with the following
  596. * structure:
  597. * {{
  598. * jid: jid,//the JID of the participant that changed his display name
  599. * displayname: displayName //the new display name
  600. * }}
  601. * {@code participantJoined} - receives event notifications about new
  602. * participant.
  603. * The listener will receive object with the following structure:
  604. * {{
  605. * jid: jid //the jid of the participant
  606. * }}
  607. * {@code participantLeft} - receives event notifications about the
  608. * participant that left the room.
  609. * The listener will receive object with the following structure:
  610. * {{
  611. * jid: jid //the jid of the participant
  612. * }}
  613. * {@code videoConferenceJoined} - receives event notifications about the
  614. * local user has successfully joined the video conference.
  615. * The listener will receive object with the following structure:
  616. * {{
  617. * roomName: room //the room name of the conference
  618. * }}
  619. * {@code videoConferenceLeft} - receives event notifications about the
  620. * local user has left the video conference.
  621. * The listener will receive object with the following structure:
  622. * {{
  623. * roomName: room //the room name of the conference
  624. * }}
  625. * {@code screenSharingStatusChanged} - receives event notifications about
  626. * turning on/off the local user screen sharing.
  627. * The listener will receive object with the following structure:
  628. * {{
  629. * on: on //whether screen sharing is on
  630. * }}
  631. * {@code dominantSpeakerChanged} - receives event notifications about
  632. * change in the dominant speaker.
  633. * The listener will receive object with the following structure:
  634. * {{
  635. * id: participantId //participantId of the new dominant speaker
  636. * }}
  637. * {@code suspendDetected} - receives event notifications about detecting suspend event in host computer.
  638. * {@code readyToClose} - all hangup operations are completed and Jitsi Meet
  639. * is ready to be disposed.
  640. * @returns {void}
  641. *
  642. * @deprecated
  643. * NOTE: This method is not removed for backward comatability purposes.
  644. */
  645. addEventListeners(listeners) {
  646. for (const event in listeners) { // eslint-disable-line guard-for-in
  647. this.addEventListener(event, listeners[event]);
  648. }
  649. }
  650. /**
  651. * Captures the screenshot of the large video.
  652. *
  653. * @returns {Promise<string>} - Resolves with a base64 encoded image data of the screenshot
  654. * if large video is detected, an error otherwise.
  655. */
  656. captureLargeVideoScreenshot() {
  657. return this._transport.sendRequest({
  658. name: 'capture-largevideo-screenshot'
  659. });
  660. }
  661. /**
  662. * Removes the listeners and removes the Jitsi Meet frame.
  663. *
  664. * @returns {void}
  665. */
  666. dispose() {
  667. this.emit('_willDispose');
  668. this._transport.dispose();
  669. this.removeAllListeners();
  670. if (this._frame && this._frame.parentNode) {
  671. this._frame.parentNode.removeChild(this._frame);
  672. }
  673. }
  674. /**
  675. * Executes command. The available commands are:
  676. * {@code displayName} - Sets the display name of the local participant to
  677. * the value passed in the arguments array.
  678. * {@code subject} - Sets the subject of the conference, the value passed
  679. * in the arguments array. Note: Available only for moderator.
  680. *
  681. * {@code toggleAudio} - Mutes / unmutes audio with no arguments.
  682. * {@code toggleVideo} - Mutes / unmutes video with no arguments.
  683. * {@code toggleFilmStrip} - Hides / shows the filmstrip with no arguments.
  684. *
  685. * If the command doesn't require any arguments the parameter should be set
  686. * to empty array or it may be omitted.
  687. *
  688. * @param {string} name - The name of the command.
  689. * @returns {void}
  690. */
  691. executeCommand(name, ...args) {
  692. if (!(name in commands)) {
  693. console.error('Not supported command name.');
  694. return;
  695. }
  696. this._transport.sendEvent({
  697. data: args,
  698. name: commands[name]
  699. });
  700. }
  701. /**
  702. * Executes commands. The available commands are:
  703. * {@code displayName} - Sets the display name of the local participant to
  704. * the value passed in the arguments array.
  705. * {@code toggleAudio} - Mutes / unmutes audio. No arguments.
  706. * {@code toggleVideo} - Mutes / unmutes video. No arguments.
  707. * {@code toggleFilmStrip} - Hides / shows the filmstrip. No arguments.
  708. * {@code toggleChat} - Hides / shows chat. No arguments.
  709. * {@code toggleShareScreen} - Starts / stops screen sharing. No arguments.
  710. *
  711. * @param {Object} commandList - The object with commands to be executed.
  712. * The keys of the object are the commands that will be executed and the
  713. * values are the arguments for the command.
  714. * @returns {void}
  715. */
  716. executeCommands(commandList) {
  717. for (const key in commandList) { // eslint-disable-line guard-for-in
  718. this.executeCommand(key, commandList[key]);
  719. }
  720. }
  721. /**
  722. * Returns Promise that resolves with a list of available devices.
  723. *
  724. * @returns {Promise}
  725. */
  726. getAvailableDevices() {
  727. return getAvailableDevices(this._transport);
  728. }
  729. /**
  730. * Gets a list of the currently sharing participant id's.
  731. *
  732. * @returns {Promise} - Resolves with the list of participant id's currently sharing.
  733. */
  734. getContentSharingParticipants() {
  735. return this._transport.sendRequest({
  736. name: 'get-content-sharing-participants'
  737. });
  738. }
  739. /**
  740. * Returns Promise that resolves with current selected devices.
  741. *
  742. * @returns {Promise}
  743. */
  744. getCurrentDevices() {
  745. return getCurrentDevices(this._transport);
  746. }
  747. /**
  748. * Returns any custom avatars backgrounds.
  749. *
  750. * @returns {Promise} - Resolves with the list of custom avatar backgrounds.
  751. */
  752. getCustomAvatarBackgrounds() {
  753. return this._transport.sendRequest({
  754. name: 'get-custom-avatar-backgrounds'
  755. });
  756. }
  757. /**
  758. * Returns the current livestream url.
  759. *
  760. * @returns {Promise} - Resolves with the current livestream URL if exists, with
  761. * undefined if not and rejects on failure.
  762. */
  763. getLivestreamUrl() {
  764. return this._transport.sendRequest({
  765. name: 'get-livestream-url'
  766. });
  767. }
  768. /**
  769. * Returns the conference participants information.
  770. *
  771. * @returns {Array<Object>} - Returns an array containing participants
  772. * information like participant id, display name, avatar URL and email.
  773. */
  774. getParticipantsInfo() {
  775. const participantIds = Object.keys(this._participants);
  776. const participantsInfo = Object.values(this._participants);
  777. participantsInfo.forEach((participant, idx) => {
  778. participant.participantId = participantIds[idx];
  779. });
  780. return participantsInfo;
  781. }
  782. /**
  783. * Returns the current video quality setting.
  784. *
  785. * @returns {number}
  786. */
  787. getVideoQuality() {
  788. return this._videoQuality;
  789. }
  790. /**
  791. * Check if the audio is available.
  792. *
  793. * @returns {Promise} - Resolves with true if the audio available, with
  794. * false if not and rejects on failure.
  795. */
  796. isAudioAvailable() {
  797. return this._transport.sendRequest({
  798. name: 'is-audio-available'
  799. });
  800. }
  801. /**
  802. * Returns Promise that resolves with true if the device change is available
  803. * and with false if not.
  804. *
  805. * @param {string} [deviceType] - Values - 'output', 'input' or undefined.
  806. * Default - 'input'.
  807. * @returns {Promise}
  808. */
  809. isDeviceChangeAvailable(deviceType) {
  810. return isDeviceChangeAvailable(this._transport, deviceType);
  811. }
  812. /**
  813. * Returns Promise that resolves with true if the device list is available
  814. * and with false if not.
  815. *
  816. * @returns {Promise}
  817. */
  818. isDeviceListAvailable() {
  819. return isDeviceListAvailable(this._transport);
  820. }
  821. /**
  822. * Returns Promise that resolves with true if multiple audio input is supported
  823. * and with false if not.
  824. *
  825. * @returns {Promise}
  826. */
  827. isMultipleAudioInputSupported() {
  828. return isMultipleAudioInputSupported(this._transport);
  829. }
  830. /**
  831. * Invite people to the call.
  832. *
  833. * @param {Array<Object>} invitees - The invitees.
  834. * @returns {Promise} - Resolves on success and rejects on failure.
  835. */
  836. invite(invitees) {
  837. if (!Array.isArray(invitees) || invitees.length === 0) {
  838. return Promise.reject(new TypeError('Invalid Argument'));
  839. }
  840. return this._transport.sendRequest({
  841. name: 'invite',
  842. invitees
  843. });
  844. }
  845. /**
  846. * Returns the audio mute status.
  847. *
  848. * @returns {Promise} - Resolves with the audio mute status and rejects on
  849. * failure.
  850. */
  851. isAudioMuted() {
  852. return this._transport.sendRequest({
  853. name: 'is-audio-muted'
  854. });
  855. }
  856. /**
  857. * Returns the audio disabled status.
  858. *
  859. * @returns {Promise} - Resolves with the audio disabled status and rejects on
  860. * failure.
  861. */
  862. isAudioDisabled() {
  863. return this._transport.sendRequest({
  864. name: 'is-audio-disabled'
  865. });
  866. }
  867. /**
  868. * Returns the moderation on status on the given mediaType.
  869. *
  870. * @param {string} mediaType - The media type for which to check moderation.
  871. * @returns {Promise} - Resolves with the moderation on status and rejects on
  872. * failure.
  873. */
  874. isModerationOn(mediaType) {
  875. return this._transport.sendRequest({
  876. name: 'is-moderation-on',
  877. mediaType
  878. });
  879. }
  880. /**
  881. * Returns force muted status of the given participant id for the given media type.
  882. *
  883. * @param {string} participantId - The id of the participant to check.
  884. * @param {string} mediaType - The media type for which to check.
  885. * @returns {Promise} - Resolves with the force muted status and rejects on
  886. * failure.
  887. */
  888. isParticipantForceMuted(participantId, mediaType) {
  889. return this._transport.sendRequest({
  890. name: 'is-participant-force-muted',
  891. participantId,
  892. mediaType
  893. });
  894. }
  895. /**
  896. * Returns whether the participants pane is open.
  897. *
  898. * @returns {Promise} - Resolves with true if the participants pane is open
  899. * and with false if not.
  900. */
  901. isParticipantsPaneOpen() {
  902. return this._transport.sendRequest({
  903. name: 'is-participants-pane-open'
  904. });
  905. }
  906. /**
  907. * Returns screen sharing status.
  908. *
  909. * @returns {Promise} - Resolves with screensharing status and rejects on failure.
  910. */
  911. isSharingScreen() {
  912. return this._transport.sendRequest({
  913. name: 'is-sharing-screen'
  914. });
  915. }
  916. /**
  917. * Returns wether meeting is started silent.
  918. *
  919. * @returns {Promise} - Resolves with start silent status.
  920. */
  921. isStartSilent() {
  922. return this._transport.sendRequest({
  923. name: 'is-start-silent'
  924. });
  925. }
  926. /**
  927. * Returns the avatar URL of a participant.
  928. *
  929. * @param {string} participantId - The id of the participant.
  930. * @returns {string} The avatar URL.
  931. */
  932. getAvatarURL(participantId) {
  933. const { avatarURL } = this._participants[participantId] || {};
  934. return avatarURL;
  935. }
  936. /**
  937. * Gets the deployment info.
  938. *
  939. * @returns {Promise} - Resolves with the deployment info object.
  940. */
  941. getDeploymentInfo() {
  942. return this._transport.sendRequest({
  943. name: 'deployment-info'
  944. });
  945. }
  946. /**
  947. * Returns the display name of a participant.
  948. *
  949. * @param {string} participantId - The id of the participant.
  950. * @returns {string} The display name.
  951. */
  952. getDisplayName(participantId) {
  953. const { displayName } = this._participants[participantId] || {};
  954. return displayName;
  955. }
  956. /**
  957. * Returns the email of a participant.
  958. *
  959. * @param {string} participantId - The id of the participant.
  960. * @returns {string} The email.
  961. */
  962. getEmail(participantId) {
  963. const { email } = this._participants[participantId] || {};
  964. return email;
  965. }
  966. /**
  967. * Returns the iframe that loads Jitsi Meet.
  968. *
  969. * @returns {HTMLElement} The iframe.
  970. */
  971. getIFrame() {
  972. return this._frame;
  973. }
  974. /**
  975. * Returns the number of participants in the conference. The local
  976. * participant is included.
  977. *
  978. * @returns {int} The number of participants in the conference.
  979. */
  980. getNumberOfParticipants() {
  981. return this._numberOfParticipants;
  982. }
  983. /**
  984. * Check if the video is available.
  985. *
  986. * @returns {Promise} - Resolves with true if the video available, with
  987. * false if not and rejects on failure.
  988. */
  989. isVideoAvailable() {
  990. return this._transport.sendRequest({
  991. name: 'is-video-available'
  992. });
  993. }
  994. /**
  995. * Returns the audio mute status.
  996. *
  997. * @returns {Promise} - Resolves with the audio mute status and rejects on
  998. * failure.
  999. */
  1000. isVideoMuted() {
  1001. return this._transport.sendRequest({
  1002. name: 'is-video-muted'
  1003. });
  1004. }
  1005. /**
  1006. * Returns the list of breakout rooms.
  1007. *
  1008. * @returns {Promise} Resolves with the list of breakout rooms.
  1009. */
  1010. listBreakoutRooms() {
  1011. return this._transport.sendRequest({
  1012. name: 'list-breakout-rooms'
  1013. });
  1014. }
  1015. /**
  1016. * Pins a participant's video on to the stage view.
  1017. *
  1018. * @param {string} participantId - Participant id (JID) of the participant
  1019. * that needs to be pinned on the stage view.
  1020. * @returns {void}
  1021. */
  1022. pinParticipant(participantId) {
  1023. this.executeCommand('pinParticipant', participantId);
  1024. }
  1025. /**
  1026. * Removes event listener.
  1027. *
  1028. * @param {string} event - The name of the event.
  1029. * @returns {void}
  1030. *
  1031. * @deprecated
  1032. * NOTE: This method is not removed for backward comatability purposes.
  1033. */
  1034. removeEventListener(event) {
  1035. this.removeAllListeners(event);
  1036. }
  1037. /**
  1038. * Removes event listeners.
  1039. *
  1040. * @param {Array<string>} eventList - Array with the names of the events.
  1041. * @returns {void}
  1042. *
  1043. * @deprecated
  1044. * NOTE: This method is not removed for backward comatability purposes.
  1045. */
  1046. removeEventListeners(eventList) {
  1047. eventList.forEach(event => this.removeEventListener(event));
  1048. }
  1049. /**
  1050. * Resizes the large video container as per the dimensions provided.
  1051. *
  1052. * @param {number} width - Width that needs to be applied on the large video container.
  1053. * @param {number} height - Height that needs to be applied on the large video container.
  1054. * @returns {void}
  1055. */
  1056. resizeLargeVideo(width, height) {
  1057. if (width <= this._width && height <= this._height) {
  1058. this.executeCommand('resizeLargeVideo', width, height);
  1059. }
  1060. }
  1061. /**
  1062. * Passes an event along to the local conference participant to establish
  1063. * or update a direct peer connection. This is currently used for developing
  1064. * wireless screensharing with room integration and it is advised against to
  1065. * use as its api may change.
  1066. *
  1067. * @param {Object} event - An object with information to pass along.
  1068. * @param {Object} event.data - The payload of the event.
  1069. * @param {string} event.from - The jid of the sender of the event. Needed
  1070. * when a reply is to be sent regarding the event.
  1071. * @returns {void}
  1072. */
  1073. sendProxyConnectionEvent(event) {
  1074. this._transport.sendEvent({
  1075. data: [ event ],
  1076. name: 'proxy-connection-event'
  1077. });
  1078. }
  1079. /**
  1080. * Sets the audio input device to the one with the label or id that is
  1081. * passed.
  1082. *
  1083. * @param {string} label - The label of the new device.
  1084. * @param {string} deviceId - The id of the new device.
  1085. * @returns {Promise}
  1086. */
  1087. setAudioInputDevice(label, deviceId) {
  1088. return setAudioInputDevice(this._transport, label, deviceId);
  1089. }
  1090. /**
  1091. * Sets the audio output device to the one with the label or id that is
  1092. * passed.
  1093. *
  1094. * @param {string} label - The label of the new device.
  1095. * @param {string} deviceId - The id of the new device.
  1096. * @returns {Promise}
  1097. */
  1098. setAudioOutputDevice(label, deviceId) {
  1099. return setAudioOutputDevice(this._transport, label, deviceId);
  1100. }
  1101. /**
  1102. * Displays the given participant on the large video. If no participant id is specified,
  1103. * dominant and pinned speakers will be taken into consideration while selecting the
  1104. * the large video participant.
  1105. *
  1106. * @param {string} participantId - Jid of the participant to be displayed on the large video.
  1107. * @returns {void}
  1108. */
  1109. setLargeVideoParticipant(participantId) {
  1110. this.executeCommand('setLargeVideoParticipant', participantId);
  1111. }
  1112. /**
  1113. * Sets the video input device to the one with the label or id that is
  1114. * passed.
  1115. *
  1116. * @param {string} label - The label of the new device.
  1117. * @param {string} deviceId - The id of the new device.
  1118. * @returns {Promise}
  1119. */
  1120. setVideoInputDevice(label, deviceId) {
  1121. return setVideoInputDevice(this._transport, label, deviceId);
  1122. }
  1123. /**
  1124. * Starts a file recording or streaming session depending on the passed on params.
  1125. * For RTMP streams, `rtmpStreamKey` must be passed on. `rtmpBroadcastID` is optional.
  1126. * For youtube streams, `youtubeStreamKey` must be passed on. `youtubeBroadcastID` is optional.
  1127. * For dropbox recording, recording `mode` should be `file` and a dropbox oauth2 token must be provided.
  1128. * For file recording, recording `mode` should be `file` and optionally `shouldShare` could be passed on.
  1129. * No other params should be passed.
  1130. *
  1131. * @param {Object} options - An object with config options to pass along.
  1132. * @param { string } options.mode - Recording mode, either `file` or `stream`.
  1133. * @param { string } options.dropboxToken - Dropbox oauth2 token.
  1134. * @param { boolean } options.shouldShare - Whether the recording should be shared with the participants or not.
  1135. * Only applies to certain jitsi meet deploys.
  1136. * @param { string } options.rtmpStreamKey - The RTMP stream key.
  1137. * @param { string } options.rtmpBroadcastID - The RTMP broacast ID.
  1138. * @param { string } options.youtubeStreamKey - The youtube stream key.
  1139. * @param { string } options.youtubeBroadcastID - The youtube broacast ID.
  1140. * @returns {void}
  1141. */
  1142. startRecording(options) {
  1143. this.executeCommand('startRecording', options);
  1144. }
  1145. /**
  1146. * Stops a recording or streaming session that is in progress.
  1147. *
  1148. * @param {string} mode - `file` or `stream`.
  1149. * @returns {void}
  1150. */
  1151. stopRecording(mode) {
  1152. this.executeCommand('stopRecording', mode);
  1153. }
  1154. /**
  1155. * Sets e2ee enabled/disabled.
  1156. *
  1157. * @param {boolean} enabled - The new value for e2ee enabled.
  1158. * @returns {void}
  1159. */
  1160. toggleE2EE(enabled) {
  1161. this.executeCommand('toggleE2EE', enabled);
  1162. }
  1163. /**
  1164. * Sets the key and keyIndex for e2ee.
  1165. *
  1166. * @param {Object} keyInfo - Json containing key information.
  1167. * @param {CryptoKey} [keyInfo.encryptionKey] - The encryption key.
  1168. * @param {number} [keyInfo.index] - The index of the encryption key.
  1169. * @returns {void}
  1170. */
  1171. async setMediaEncryptionKey(keyInfo) {
  1172. const { key, index } = keyInfo;
  1173. if (key) {
  1174. const exportedKey = await crypto.subtle.exportKey('raw', key);
  1175. this.executeCommand('setMediaEncryptionKey', JSON.stringify({
  1176. exportedKey: Array.from(new Uint8Array(exportedKey)),
  1177. index }));
  1178. } else {
  1179. this.executeCommand('setMediaEncryptionKey', JSON.stringify({
  1180. exportedKey: false,
  1181. index }));
  1182. }
  1183. }
  1184. }