Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

external_api.js 35KB

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