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 41KB

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