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

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