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

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