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

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