您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

external_api.js 35KB

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