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

external_api.js 37KB

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