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

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