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

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