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

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