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

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