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

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