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.

API.js 41KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353
  1. // @flow
  2. import Logger from 'jitsi-meet-logger';
  3. import {
  4. createApiEvent,
  5. sendAnalytics
  6. } from '../../react/features/analytics';
  7. import {
  8. getCurrentConference,
  9. sendTones,
  10. setFollowMe,
  11. setPassword,
  12. setSubject
  13. } from '../../react/features/base/conference';
  14. import { overwriteConfig, getWhitelistedJSON } from '../../react/features/base/config';
  15. import { toggleDialog } from '../../react/features/base/dialog/actions';
  16. import { parseJWTFromURLParams } from '../../react/features/base/jwt';
  17. import JitsiMeetJS, { JitsiRecordingConstants } from '../../react/features/base/lib-jitsi-meet';
  18. import { MEDIA_TYPE } from '../../react/features/base/media';
  19. import {
  20. getLocalParticipant,
  21. getParticipantById,
  22. pinParticipant,
  23. kickParticipant,
  24. raiseHand,
  25. isParticipantModerator
  26. } from '../../react/features/base/participants';
  27. import { updateSettings } from '../../react/features/base/settings';
  28. import { isToggleCameraEnabled, toggleCamera } from '../../react/features/base/tracks';
  29. import {
  30. sendMessage,
  31. setPrivateMessageRecipient,
  32. toggleChat
  33. } from '../../react/features/chat/actions';
  34. import { openChat } from '../../react/features/chat/actions.web';
  35. import {
  36. processExternalDeviceRequest
  37. } from '../../react/features/device-selection/functions';
  38. import { isEnabled as isDropboxEnabled } from '../../react/features/dropbox';
  39. import { toggleE2EE } from '../../react/features/e2ee/actions';
  40. import { invite } from '../../react/features/invite';
  41. import {
  42. selectParticipantInLargeVideo
  43. } from '../../react/features/large-video/actions.any';
  44. import {
  45. captureLargeVideoScreenshot,
  46. resizeLargeVideo
  47. } from '../../react/features/large-video/actions.web';
  48. import { toggleLobbyMode } from '../../react/features/lobby/actions';
  49. import { RECORDING_TYPES } from '../../react/features/recording/constants';
  50. import { getActiveSession } from '../../react/features/recording/functions';
  51. import { isScreenAudioSupported } from '../../react/features/screen-share';
  52. import { startScreenShareFlow, startAudioScreenShareFlow } from '../../react/features/screen-share/actions';
  53. import { playSharedVideo, stopSharedVideo } from '../../react/features/shared-video/actions.any';
  54. import { toggleTileView, setTileView } from '../../react/features/video-layout';
  55. import { muteAllParticipants } from '../../react/features/video-menu/actions';
  56. import { setVideoQuality } from '../../react/features/video-quality';
  57. import VirtualBackgroundDialog from '../../react/features/virtual-background/components/VirtualBackgroundDialog';
  58. import { getJitsiMeetTransport } from '../transport';
  59. import { API_ID, ENDPOINT_TEXT_MESSAGE_NAME } from './constants';
  60. const logger = Logger.getLogger(__filename);
  61. declare var APP: Object;
  62. /**
  63. * List of the available commands.
  64. */
  65. let commands = {};
  66. /**
  67. * The transport instance used for communication with external apps.
  68. *
  69. * @type {Transport}
  70. */
  71. const transport = getJitsiMeetTransport();
  72. /**
  73. * The current audio availability.
  74. *
  75. * @type {boolean}
  76. */
  77. let audioAvailable = true;
  78. /**
  79. * The current video availability.
  80. *
  81. * @type {boolean}
  82. */
  83. let videoAvailable = true;
  84. /**
  85. * Initializes supported commands.
  86. *
  87. * @returns {void}
  88. */
  89. function initCommands() {
  90. commands = {
  91. 'display-name': displayName => {
  92. sendAnalytics(createApiEvent('display.name.changed'));
  93. APP.conference.changeLocalDisplayName(displayName);
  94. },
  95. 'mute-everyone': mediaType => {
  96. const muteMediaType = mediaType ? mediaType : MEDIA_TYPE.AUDIO;
  97. sendAnalytics(createApiEvent('muted-everyone'));
  98. const localParticipant = getLocalParticipant(APP.store.getState());
  99. const exclude = [];
  100. if (localParticipant && isParticipantModerator(localParticipant)) {
  101. exclude.push(localParticipant.id);
  102. }
  103. APP.store.dispatch(muteAllParticipants(exclude, muteMediaType));
  104. },
  105. 'toggle-lobby': isLobbyEnabled => {
  106. APP.store.dispatch(toggleLobbyMode(isLobbyEnabled));
  107. },
  108. 'password': password => {
  109. const { conference, passwordRequired }
  110. = APP.store.getState()['features/base/conference'];
  111. if (passwordRequired) {
  112. sendAnalytics(createApiEvent('submit.password'));
  113. APP.store.dispatch(setPassword(
  114. passwordRequired,
  115. passwordRequired.join,
  116. password
  117. ));
  118. } else {
  119. sendAnalytics(createApiEvent('password.changed'));
  120. APP.store.dispatch(setPassword(
  121. conference,
  122. conference.lock,
  123. password
  124. ));
  125. }
  126. },
  127. 'pin-participant': id => {
  128. logger.debug('Pin participant command received');
  129. sendAnalytics(createApiEvent('participant.pinned'));
  130. APP.store.dispatch(pinParticipant(id));
  131. },
  132. 'proxy-connection-event': event => {
  133. APP.conference.onProxyConnectionEvent(event);
  134. },
  135. 'resize-large-video': (width, height) => {
  136. logger.debug('Resize large video command received');
  137. sendAnalytics(createApiEvent('largevideo.resized'));
  138. APP.store.dispatch(resizeLargeVideo(width, height));
  139. },
  140. 'send-tones': (options = {}) => {
  141. const { duration, tones, pause } = options;
  142. APP.store.dispatch(sendTones(tones, duration, pause));
  143. },
  144. 'set-follow-me': value => {
  145. logger.debug('Set follow me command received');
  146. if (value) {
  147. sendAnalytics(createApiEvent('follow.me.set'));
  148. } else {
  149. sendAnalytics(createApiEvent('follow.me.unset'));
  150. }
  151. APP.store.dispatch(setFollowMe(value));
  152. },
  153. 'set-large-video-participant': participantId => {
  154. logger.debug('Set large video participant command received');
  155. sendAnalytics(createApiEvent('largevideo.participant.set'));
  156. APP.store.dispatch(selectParticipantInLargeVideo(participantId));
  157. },
  158. 'subject': subject => {
  159. sendAnalytics(createApiEvent('subject.changed'));
  160. APP.store.dispatch(setSubject(subject));
  161. },
  162. 'submit-feedback': feedback => {
  163. sendAnalytics(createApiEvent('submit.feedback'));
  164. APP.conference.submitFeedback(feedback.score, feedback.message);
  165. },
  166. 'toggle-audio': () => {
  167. sendAnalytics(createApiEvent('toggle-audio'));
  168. logger.log('Audio toggle: API command received');
  169. APP.conference.toggleAudioMuted(false /* no UI */);
  170. },
  171. 'toggle-video': () => {
  172. sendAnalytics(createApiEvent('toggle-video'));
  173. logger.log('Video toggle: API command received');
  174. APP.conference.toggleVideoMuted(false /* no UI */);
  175. },
  176. 'toggle-film-strip': () => {
  177. sendAnalytics(createApiEvent('film.strip.toggled'));
  178. APP.UI.toggleFilmstrip();
  179. },
  180. 'toggle-camera': () => {
  181. if (!isToggleCameraEnabled(APP.store.getState())) {
  182. return;
  183. }
  184. APP.store.dispatch(toggleCamera());
  185. },
  186. 'toggle-camera-mirror': () => {
  187. const state = APP.store.getState();
  188. const { localFlipX: currentFlipX } = state['features/base/settings'];
  189. APP.store.dispatch(updateSettings({ localFlipX: !currentFlipX }));
  190. },
  191. 'toggle-chat': () => {
  192. sendAnalytics(createApiEvent('chat.toggled'));
  193. APP.store.dispatch(toggleChat());
  194. },
  195. 'toggle-raise-hand': () => {
  196. const localParticipant = getLocalParticipant(APP.store.getState());
  197. if (!localParticipant) {
  198. return;
  199. }
  200. const { raisedHand } = localParticipant;
  201. sendAnalytics(createApiEvent('raise-hand.toggled'));
  202. APP.store.dispatch(raiseHand(!raisedHand));
  203. },
  204. 'toggle-share-audio': () => {
  205. sendAnalytics(createApiEvent('audio.screen.sharing.toggled'));
  206. if (isScreenAudioSupported()) {
  207. APP.store.dispatch(startAudioScreenShareFlow());
  208. return;
  209. }
  210. logger.error('Audio screen sharing is not supported by the current platform!');
  211. },
  212. /**
  213. * Callback to invoke when the "toggle-share-screen" command is received.
  214. *
  215. * @param {Object} options - Additional details of how to perform
  216. * the action. Note this parameter is undocumented and experimental.
  217. * @param {boolean} options.enable - Whether trying to enable screen
  218. * sharing or to turn it off.
  219. * @returns {void}
  220. */
  221. 'toggle-share-screen': (options = {}) => {
  222. sendAnalytics(createApiEvent('screen.sharing.toggled'));
  223. toggleScreenSharing(options.enable);
  224. },
  225. 'toggle-tile-view': () => {
  226. sendAnalytics(createApiEvent('tile-view.toggled'));
  227. APP.store.dispatch(toggleTileView());
  228. },
  229. 'set-tile-view': enabled => {
  230. APP.store.dispatch(setTileView(enabled));
  231. },
  232. 'video-hangup': (showFeedbackDialog = true) => {
  233. sendAnalytics(createApiEvent('video.hangup'));
  234. APP.conference.hangup(showFeedbackDialog);
  235. },
  236. 'email': email => {
  237. sendAnalytics(createApiEvent('email.changed'));
  238. APP.conference.changeLocalEmail(email);
  239. },
  240. 'avatar-url': avatarUrl => {
  241. sendAnalytics(createApiEvent('avatar.url.changed'));
  242. APP.conference.changeLocalAvatarUrl(avatarUrl);
  243. },
  244. 'send-chat-message': (message, to, ignorePrivacy = false) => {
  245. logger.debug('Send chat message command received');
  246. if (to) {
  247. const participant = getParticipantById(APP.store.getState(), to);
  248. if (participant) {
  249. APP.store.dispatch(setPrivateMessageRecipient(participant));
  250. } else {
  251. logger.error(`Participant with id ${to} not found!`);
  252. return;
  253. }
  254. } else {
  255. APP.store.dispatch(setPrivateMessageRecipient());
  256. }
  257. APP.store.dispatch(sendMessage(message, ignorePrivacy));
  258. },
  259. 'send-endpoint-text-message': (to, text) => {
  260. logger.debug('Send endpoint message command received');
  261. try {
  262. APP.conference.sendEndpointMessage(to, {
  263. name: ENDPOINT_TEXT_MESSAGE_NAME,
  264. text
  265. });
  266. } catch (err) {
  267. logger.error('Failed sending endpoint text message', err);
  268. }
  269. },
  270. 'toggle-e2ee': enabled => {
  271. logger.debug('Toggle E2EE key command received');
  272. APP.store.dispatch(toggleE2EE(enabled));
  273. },
  274. 'set-video-quality': frameHeight => {
  275. logger.debug('Set video quality command received');
  276. sendAnalytics(createApiEvent('set.video.quality'));
  277. APP.store.dispatch(setVideoQuality(frameHeight));
  278. },
  279. 'start-share-video': url => {
  280. logger.debug('Share video command received');
  281. sendAnalytics(createApiEvent('share.video.start'));
  282. APP.store.dispatch(playSharedVideo(url));
  283. },
  284. 'stop-share-video': () => {
  285. logger.debug('Share video command received');
  286. sendAnalytics(createApiEvent('share.video.stop'));
  287. APP.store.dispatch(stopSharedVideo());
  288. },
  289. /**
  290. * Starts a file recording or streaming session depending on the passed on params.
  291. * For RTMP streams, `rtmpStreamKey` must be passed on. `rtmpBroadcastID` is optional.
  292. * For youtube streams, `youtubeStreamKey` must be passed on. `youtubeBroadcastID` is optional.
  293. * For dropbox recording, recording `mode` should be `file` and a dropbox oauth2 token must be provided.
  294. * For file recording, recording `mode` should be `file` and optionally `shouldShare` could be passed on.
  295. * No other params should be passed.
  296. *
  297. * @param { string } arg.mode - Recording mode, either `file` or `stream`.
  298. * @param { string } arg.dropboxToken - Dropbox oauth2 token.
  299. * @param { string } arg.rtmpStreamKey - The RTMP stream key.
  300. * @param { string } arg.rtmpBroadcastID - The RTMP braodcast ID.
  301. * @param { boolean } arg.shouldShare - Whether the recording should be shared with the participants or not.
  302. * Only applies to certain jitsi meet deploys.
  303. * @param { string } arg.youtubeStreamKey - The youtube stream key.
  304. * @param { string } arg.youtubeBroadcastID - The youtube broacast ID.
  305. * @returns {void}
  306. */
  307. 'start-recording': ({
  308. mode,
  309. dropboxToken,
  310. shouldShare,
  311. rtmpStreamKey,
  312. rtmpBroadcastID,
  313. youtubeStreamKey,
  314. youtubeBroadcastID
  315. }) => {
  316. const state = APP.store.getState();
  317. const conference = getCurrentConference(state);
  318. if (!conference) {
  319. logger.error('Conference is not defined');
  320. return;
  321. }
  322. if (dropboxToken && !isDropboxEnabled(state)) {
  323. logger.error('Failed starting recording: dropbox is not enabled on this deployment');
  324. return;
  325. }
  326. if (mode === JitsiRecordingConstants.mode.STREAM && !(youtubeStreamKey || rtmpStreamKey)) {
  327. logger.error('Failed starting recording: missing youtube or RTMP stream key');
  328. return;
  329. }
  330. let recordingConfig;
  331. if (mode === JitsiRecordingConstants.mode.FILE) {
  332. if (dropboxToken) {
  333. recordingConfig = {
  334. mode: JitsiRecordingConstants.mode.FILE,
  335. appData: JSON.stringify({
  336. 'file_recording_metadata': {
  337. 'upload_credentials': {
  338. 'service_name': RECORDING_TYPES.DROPBOX,
  339. 'token': dropboxToken
  340. }
  341. }
  342. })
  343. };
  344. } else {
  345. recordingConfig = {
  346. mode: JitsiRecordingConstants.mode.FILE,
  347. appData: JSON.stringify({
  348. 'file_recording_metadata': {
  349. 'share': shouldShare
  350. }
  351. })
  352. };
  353. }
  354. } else if (mode === JitsiRecordingConstants.mode.STREAM) {
  355. recordingConfig = {
  356. broadcastId: youtubeBroadcastID || rtmpBroadcastID,
  357. mode: JitsiRecordingConstants.mode.STREAM,
  358. streamId: youtubeStreamKey || rtmpStreamKey
  359. };
  360. } else {
  361. logger.error('Invalid recording mode provided');
  362. return;
  363. }
  364. conference.startRecording(recordingConfig);
  365. },
  366. /**
  367. * Stops a recording or streaming in progress.
  368. *
  369. * @param {string} mode - `file` or `stream`.
  370. * @returns {void}
  371. */
  372. 'stop-recording': mode => {
  373. const state = APP.store.getState();
  374. const conference = getCurrentConference(state);
  375. if (!conference) {
  376. logger.error('Conference is not defined');
  377. return;
  378. }
  379. if (![ JitsiRecordingConstants.mode.FILE, JitsiRecordingConstants.mode.STREAM ].includes(mode)) {
  380. logger.error('Invalid recording mode provided!');
  381. return;
  382. }
  383. const activeSession = getActiveSession(state, mode);
  384. if (activeSession && activeSession.id) {
  385. conference.stopRecording(activeSession.id);
  386. } else {
  387. logger.error('No recording or streaming session found');
  388. }
  389. },
  390. 'initiate-private-chat': participantId => {
  391. const state = APP.store.getState();
  392. const participant = getParticipantById(state, participantId);
  393. if (participant) {
  394. const { isOpen: isChatOpen } = state['features/chat'];
  395. if (!isChatOpen) {
  396. APP.store.dispatch(toggleChat());
  397. }
  398. APP.store.dispatch(openChat(participant));
  399. } else {
  400. logger.error('No participant found for the given participantId');
  401. }
  402. },
  403. 'cancel-private-chat': () => {
  404. APP.store.dispatch(setPrivateMessageRecipient());
  405. },
  406. 'kick-participant': participantId => {
  407. APP.store.dispatch(kickParticipant(participantId));
  408. },
  409. 'overwrite-config': config => {
  410. const whitelistedConfig = getWhitelistedJSON('config', config);
  411. APP.store.dispatch(overwriteConfig(whitelistedConfig));
  412. },
  413. 'toggle-virtual-background': () => {
  414. APP.store.dispatch(toggleDialog(VirtualBackgroundDialog));
  415. }
  416. };
  417. transport.on('event', ({ data, name }) => {
  418. if (name && commands[name]) {
  419. commands[name](...data);
  420. return true;
  421. }
  422. return false;
  423. });
  424. transport.on('request', (request, callback) => {
  425. const { dispatch, getState } = APP.store;
  426. if (processExternalDeviceRequest(dispatch, getState, request, callback)) {
  427. return true;
  428. }
  429. const { name } = request;
  430. switch (name) {
  431. case 'capture-largevideo-screenshot' :
  432. APP.store.dispatch(captureLargeVideoScreenshot())
  433. .then(dataURL => {
  434. let error;
  435. if (!dataURL) {
  436. error = new Error('No large video found!');
  437. }
  438. callback({
  439. error,
  440. dataURL
  441. });
  442. });
  443. break;
  444. case 'invite': {
  445. const { invitees } = request;
  446. if (!Array.isArray(invitees) || invitees.length === 0) {
  447. callback({
  448. error: new Error('Unexpected format of invitees')
  449. });
  450. break;
  451. }
  452. // The store should be already available because API.init is called
  453. // on appWillMount action.
  454. APP.store.dispatch(
  455. invite(invitees, true))
  456. .then(failedInvitees => {
  457. let error;
  458. let result;
  459. if (failedInvitees.length) {
  460. error = new Error('One or more invites failed!');
  461. } else {
  462. result = true;
  463. }
  464. callback({
  465. error,
  466. result
  467. });
  468. });
  469. break;
  470. }
  471. case 'is-audio-muted':
  472. callback(APP.conference.isLocalAudioMuted());
  473. break;
  474. case 'is-video-muted':
  475. callback(APP.conference.isLocalVideoMuted());
  476. break;
  477. case 'is-audio-available':
  478. callback(audioAvailable);
  479. break;
  480. case 'is-video-available':
  481. callback(videoAvailable);
  482. break;
  483. case 'is-sharing-screen':
  484. callback(Boolean(APP.conference.isSharingScreen));
  485. break;
  486. case 'get-content-sharing-participants': {
  487. const tracks = getState()['features/base/tracks'];
  488. const sharingParticipantIds = tracks.filter(tr => tr.videoType === 'desktop').map(t => t.participantId);
  489. callback({
  490. sharingParticipantIds
  491. });
  492. break;
  493. }
  494. case 'get-livestream-url': {
  495. const state = APP.store.getState();
  496. const conference = getCurrentConference(state);
  497. let livestreamUrl;
  498. if (conference) {
  499. const activeSession = getActiveSession(state, JitsiRecordingConstants.mode.STREAM);
  500. livestreamUrl = activeSession?.liveStreamViewURL;
  501. } else {
  502. logger.error('Conference is not defined');
  503. }
  504. callback({
  505. livestreamUrl
  506. });
  507. break;
  508. }
  509. case 'get-custom-avatar-backgrounds' : {
  510. callback({
  511. avatarBackgrounds: APP.store.getState()['features/dynamic-branding'].avatarBackgrounds
  512. });
  513. break;
  514. }
  515. default:
  516. return false;
  517. }
  518. return true;
  519. });
  520. }
  521. /**
  522. * Check whether the API should be enabled or not.
  523. *
  524. * @returns {boolean}
  525. */
  526. function shouldBeEnabled() {
  527. return (
  528. typeof API_ID === 'number'
  529. // XXX Enable the API when a JSON Web Token (JWT) is specified in
  530. // the location/URL because then it is very likely that the Jitsi
  531. // Meet (Web) app is being used by an external/wrapping (Web) app
  532. // and, consequently, the latter will need to communicate with the
  533. // former. (The described logic is merely a heuristic though.)
  534. || parseJWTFromURLParams());
  535. }
  536. /**
  537. * Executes on toggle-share-screen command.
  538. *
  539. * @param {boolean} [enable] - Whether this toggle is to explicitly enable or
  540. * disable screensharing. If not defined, the application will automatically
  541. * attempt to toggle between enabled and disabled. This boolean is useful for
  542. * explicitly setting desired screensharing state.
  543. * @returns {void}
  544. */
  545. function toggleScreenSharing(enable) {
  546. if (JitsiMeetJS.isDesktopSharingEnabled()) {
  547. APP.store.dispatch(startScreenShareFlow(enable));
  548. }
  549. }
  550. /**
  551. * Removes sensitive data from a mouse event.
  552. *
  553. * @param {MouseEvent} event - The mouse event to sanitize.
  554. * @returns {Object}
  555. */
  556. function sanitizeMouseEvent(event: MouseEvent) {
  557. const {
  558. clientX,
  559. clientY,
  560. movementX,
  561. movementY,
  562. offsetX,
  563. offsetY,
  564. pageX,
  565. pageY,
  566. x,
  567. y,
  568. screenX,
  569. screenY
  570. } = event;
  571. return {
  572. clientX,
  573. clientY,
  574. movementX,
  575. movementY,
  576. offsetX,
  577. offsetY,
  578. pageX,
  579. pageY,
  580. x,
  581. y,
  582. screenX,
  583. screenY
  584. };
  585. }
  586. /**
  587. * Implements API class that communicates with external API class and provides
  588. * interface to access Jitsi Meet features by external applications that embed
  589. * Jitsi Meet.
  590. */
  591. class API {
  592. _enabled: boolean;
  593. /**
  594. * Initializes the API. Setups message event listeners that will receive
  595. * information from external applications that embed Jitsi Meet. It also
  596. * sends a message to the external application that API is initialized.
  597. *
  598. * @param {Object} options - Optional parameters.
  599. * @returns {void}
  600. */
  601. init() {
  602. if (!shouldBeEnabled()) {
  603. return;
  604. }
  605. /**
  606. * Current status (enabled/disabled) of API.
  607. *
  608. * @private
  609. * @type {boolean}
  610. */
  611. this._enabled = true;
  612. initCommands();
  613. }
  614. /**
  615. * Notify external application (if API is enabled) that the large video
  616. * visibility changed.
  617. *
  618. * @param {boolean} isHidden - True if the large video is hidden and false
  619. * otherwise.
  620. * @returns {void}
  621. */
  622. notifyLargeVideoVisibilityChanged(isHidden: boolean) {
  623. this._sendEvent({
  624. name: 'large-video-visibility-changed',
  625. isVisible: !isHidden
  626. });
  627. }
  628. /**
  629. * Notifies the external application (spot) that the local jitsi-participant
  630. * has a status update.
  631. *
  632. * @param {Object} event - The message to pass onto spot.
  633. * @returns {void}
  634. */
  635. sendProxyConnectionEvent(event: Object) {
  636. this._sendEvent({
  637. name: 'proxy-connection-event',
  638. ...event
  639. });
  640. }
  641. /**
  642. * Sends event to the external application.
  643. *
  644. * @param {Object} event - The event to be sent.
  645. * @returns {void}
  646. */
  647. _sendEvent(event: Object = {}) {
  648. if (this._enabled) {
  649. transport.sendEvent(event);
  650. }
  651. }
  652. /**
  653. * Notify external application (if API is enabled) that the chat state has been updated.
  654. *
  655. * @param {number} unreadCount - The unread messages counter.
  656. * @param {boolean} isOpen - True if the chat panel is open.
  657. * @returns {void}
  658. */
  659. notifyChatUpdated(unreadCount: number, isOpen: boolean) {
  660. this._sendEvent({
  661. name: 'chat-updated',
  662. unreadCount,
  663. isOpen
  664. });
  665. }
  666. /**
  667. * Notify external application (if API is enabled) that message was sent.
  668. *
  669. * @param {string} message - Message body.
  670. * @param {boolean} privateMessage - True if the message was a private message.
  671. * @returns {void}
  672. */
  673. notifySendingChatMessage(message: string, privateMessage: boolean) {
  674. this._sendEvent({
  675. name: 'outgoing-message',
  676. message,
  677. privateMessage
  678. });
  679. }
  680. /**
  681. * Notify external application (if API is enabled) that the mouse has entered inside the iframe.
  682. *
  683. * @param {MouseEvent} event - The mousemove event.
  684. * @returns {void}
  685. */
  686. notifyMouseEnter(event: MouseEvent) {
  687. this._sendEvent({
  688. name: 'mouse-enter',
  689. event: sanitizeMouseEvent(event)
  690. });
  691. }
  692. /**
  693. * Notify external application (if API is enabled) that the mouse has entered inside the iframe.
  694. *
  695. * @param {MouseEvent} event - The mousemove event.
  696. * @returns {void}
  697. */
  698. notifyMouseLeave(event: MouseEvent) {
  699. this._sendEvent({
  700. name: 'mouse-leave',
  701. event: sanitizeMouseEvent(event)
  702. });
  703. }
  704. /**
  705. * Notify external application (if API is enabled) that the mouse has moved inside the iframe.
  706. *
  707. * @param {MouseEvent} event - The mousemove event.
  708. * @returns {void}
  709. */
  710. notifyMouseMove(event: MouseEvent) {
  711. this._sendEvent({
  712. name: 'mouse-move',
  713. event: sanitizeMouseEvent(event)
  714. });
  715. }
  716. /**
  717. * Notify external application that the video quality setting has changed.
  718. *
  719. * @param {number} videoQuality - The video quality. The number represents the maximum height of the video streams.
  720. * @returns {void}
  721. */
  722. notifyVideoQualityChanged(videoQuality: number) {
  723. this._sendEvent({
  724. name: 'video-quality-changed',
  725. videoQuality
  726. });
  727. }
  728. /**
  729. * Notify external application (if API is enabled) that message was
  730. * received.
  731. *
  732. * @param {Object} options - Object with the message properties.
  733. * @returns {void}
  734. */
  735. notifyReceivedChatMessage(
  736. { body, id, nick, privateMessage, ts }: {
  737. body: *, id: string, nick: string, privateMessage: boolean, ts: *
  738. } = {}) {
  739. if (APP.conference.isLocalId(id)) {
  740. return;
  741. }
  742. this._sendEvent({
  743. name: 'incoming-message',
  744. from: id,
  745. message: body,
  746. nick,
  747. privateMessage,
  748. stamp: ts
  749. });
  750. }
  751. /**
  752. * Notify external application (if API is enabled) that user joined the
  753. * conference.
  754. *
  755. * @param {string} id - User id.
  756. * @param {Object} props - The display name of the user.
  757. * @returns {void}
  758. */
  759. notifyUserJoined(id: string, props: Object) {
  760. this._sendEvent({
  761. name: 'participant-joined',
  762. id,
  763. ...props
  764. });
  765. }
  766. /**
  767. * Notify external application (if API is enabled) that user left the
  768. * conference.
  769. *
  770. * @param {string} id - User id.
  771. * @returns {void}
  772. */
  773. notifyUserLeft(id: string) {
  774. this._sendEvent({
  775. name: 'participant-left',
  776. id
  777. });
  778. }
  779. /**
  780. * Notify external application (if API is enabled) that the user role
  781. * has changed.
  782. *
  783. * @param {string} id - User id.
  784. * @param {string} role - The new user role.
  785. * @returns {void}
  786. */
  787. notifyUserRoleChanged(id: string, role: string) {
  788. this._sendEvent({
  789. name: 'participant-role-changed',
  790. id,
  791. role
  792. });
  793. }
  794. /**
  795. * Notify external application (if API is enabled) that user changed their
  796. * avatar.
  797. *
  798. * @param {string} id - User id.
  799. * @param {string} avatarURL - The new avatar URL of the participant.
  800. * @returns {void}
  801. */
  802. notifyAvatarChanged(id: string, avatarURL: string) {
  803. this._sendEvent({
  804. name: 'avatar-changed',
  805. avatarURL,
  806. id
  807. });
  808. }
  809. /**
  810. * Notify external application (if API is enabled) that user received
  811. * a text message through datachannels.
  812. *
  813. * @param {Object} data - The event data.
  814. * @returns {void}
  815. */
  816. notifyEndpointTextMessageReceived(data: Object) {
  817. this._sendEvent({
  818. name: 'endpoint-text-message-received',
  819. data
  820. });
  821. }
  822. /**
  823. * Notify external application (if API is enabled) that the list of sharing participants changed.
  824. *
  825. * @param {Object} data - The event data.
  826. * @returns {void}
  827. */
  828. notifySharingParticipantsChanged(data: Object) {
  829. this._sendEvent({
  830. name: 'content-sharing-participants-changed',
  831. data
  832. });
  833. }
  834. /**
  835. * Notify external application (if API is enabled) that the device list has
  836. * changed.
  837. *
  838. * @param {Object} devices - The new device list.
  839. * @returns {void}
  840. */
  841. notifyDeviceListChanged(devices: Object) {
  842. this._sendEvent({
  843. name: 'device-list-changed',
  844. devices
  845. });
  846. }
  847. /**
  848. * Notify external application (if API is enabled) that user changed their
  849. * nickname.
  850. *
  851. * @param {string} id - User id.
  852. * @param {string} displayname - User nickname.
  853. * @param {string} formattedDisplayName - The display name shown in Jitsi
  854. * meet's UI for the user.
  855. * @returns {void}
  856. */
  857. notifyDisplayNameChanged(
  858. id: string,
  859. { displayName, formattedDisplayName }: Object) {
  860. this._sendEvent({
  861. name: 'display-name-change',
  862. displayname: displayName,
  863. formattedDisplayName,
  864. id
  865. });
  866. }
  867. /**
  868. * Notify external application (if API is enabled) that user changed their
  869. * email.
  870. *
  871. * @param {string} id - User id.
  872. * @param {string} email - The new email of the participant.
  873. * @returns {void}
  874. */
  875. notifyEmailChanged(
  876. id: string,
  877. { email }: Object) {
  878. this._sendEvent({
  879. name: 'email-change',
  880. email,
  881. id
  882. });
  883. }
  884. /**
  885. * Notify external application (if API is enabled) that the an error has been logged.
  886. *
  887. * @param {string} logLevel - The message log level.
  888. * @param {Array} args - Array of strings composing the log message.
  889. * @returns {void}
  890. */
  891. notifyLog(logLevel: string, args: Array<string>) {
  892. this._sendEvent({
  893. name: 'log',
  894. logLevel,
  895. args
  896. });
  897. }
  898. /**
  899. * Notify external application (if API is enabled) that the conference has
  900. * been joined.
  901. *
  902. * @param {string} roomName - The room name.
  903. * @param {string} id - The id of the local user.
  904. * @param {Object} props - The display name and avatar URL of the local
  905. * user.
  906. * @returns {void}
  907. */
  908. notifyConferenceJoined(roomName: string, id: string, props: Object) {
  909. this._sendEvent({
  910. name: 'video-conference-joined',
  911. roomName,
  912. id,
  913. ...props
  914. });
  915. }
  916. /**
  917. * Notify external application (if API is enabled) that local user has left the conference.
  918. *
  919. * @param {string} roomName - User id.
  920. * @returns {void}
  921. */
  922. notifyConferenceLeft(roomName: string) {
  923. this._sendEvent({
  924. name: 'video-conference-left',
  925. roomName
  926. });
  927. }
  928. /**
  929. * Notify external application (if API is enabled) that we are ready to be
  930. * closed.
  931. *
  932. * @returns {void}
  933. */
  934. notifyReadyToClose() {
  935. this._sendEvent({ name: 'video-ready-to-close' });
  936. }
  937. /**
  938. * Notify external application (if API is enabled) that a suspend event in host computer.
  939. *
  940. * @returns {void}
  941. */
  942. notifySuspendDetected() {
  943. this._sendEvent({ name: 'suspend-detected' });
  944. }
  945. /**
  946. * Notify external application (if API is enabled) for audio muted status
  947. * changed.
  948. *
  949. * @param {boolean} muted - The new muted status.
  950. * @returns {void}
  951. */
  952. notifyAudioMutedStatusChanged(muted: boolean) {
  953. this._sendEvent({
  954. name: 'audio-mute-status-changed',
  955. muted
  956. });
  957. }
  958. /**
  959. * Notify external application (if API is enabled) for video muted status
  960. * changed.
  961. *
  962. * @param {boolean} muted - The new muted status.
  963. * @returns {void}
  964. */
  965. notifyVideoMutedStatusChanged(muted: boolean) {
  966. this._sendEvent({
  967. name: 'video-mute-status-changed',
  968. muted
  969. });
  970. }
  971. /**
  972. * Notify external application (if API is enabled) for audio availability
  973. * changed.
  974. *
  975. * @param {boolean} available - True if available and false otherwise.
  976. * @returns {void}
  977. */
  978. notifyAudioAvailabilityChanged(available: boolean) {
  979. audioAvailable = available;
  980. this._sendEvent({
  981. name: 'audio-availability-changed',
  982. available
  983. });
  984. }
  985. /**
  986. * Notify external application (if API is enabled) for video available
  987. * status changed.
  988. *
  989. * @param {boolean} available - True if available and false otherwise.
  990. * @returns {void}
  991. */
  992. notifyVideoAvailabilityChanged(available: boolean) {
  993. videoAvailable = available;
  994. this._sendEvent({
  995. name: 'video-availability-changed',
  996. available
  997. });
  998. }
  999. /**
  1000. * Notify external application (if API is enabled) that the on stage
  1001. * participant has changed.
  1002. *
  1003. * @param {string} id - User id of the new on stage participant.
  1004. * @returns {void}
  1005. */
  1006. notifyOnStageParticipantChanged(id: string) {
  1007. this._sendEvent({
  1008. name: 'on-stage-participant-changed',
  1009. id
  1010. });
  1011. }
  1012. /**
  1013. * Notify external application of an unexpected camera-related error having
  1014. * occurred.
  1015. *
  1016. * @param {string} type - The type of the camera error.
  1017. * @param {string} message - Additional information about the error.
  1018. * @returns {void}
  1019. */
  1020. notifyOnCameraError(type: string, message: string) {
  1021. this._sendEvent({
  1022. name: 'camera-error',
  1023. type,
  1024. message
  1025. });
  1026. }
  1027. /**
  1028. * Notify external application of an unexpected mic-related error having
  1029. * occurred.
  1030. *
  1031. * @param {string} type - The type of the mic error.
  1032. * @param {string} message - Additional information about the error.
  1033. * @returns {void}
  1034. */
  1035. notifyOnMicError(type: string, message: string) {
  1036. this._sendEvent({
  1037. name: 'mic-error',
  1038. type,
  1039. message
  1040. });
  1041. }
  1042. /**
  1043. * Notify external application (if API is enabled) that conference feedback
  1044. * has been submitted. Intended to be used in conjunction with the
  1045. * submit-feedback command to get notified if feedback was submitted.
  1046. *
  1047. * @param {string} error - A failure message, if any.
  1048. * @returns {void}
  1049. */
  1050. notifyFeedbackSubmitted(error: string) {
  1051. this._sendEvent({
  1052. name: 'feedback-submitted',
  1053. error
  1054. });
  1055. }
  1056. /**
  1057. * Notify external application (if API is enabled) that the feedback prompt
  1058. * has been displayed.
  1059. *
  1060. * @returns {void}
  1061. */
  1062. notifyFeedbackPromptDisplayed() {
  1063. this._sendEvent({ name: 'feedback-prompt-displayed' });
  1064. }
  1065. /**
  1066. * Notify external application (if API is enabled) that the display
  1067. * configuration of the filmstrip has been changed.
  1068. *
  1069. * @param {boolean} visible - Whether or not the filmstrip has been set to
  1070. * be displayed or hidden.
  1071. * @returns {void}
  1072. */
  1073. notifyFilmstripDisplayChanged(visible: boolean) {
  1074. this._sendEvent({
  1075. name: 'filmstrip-display-changed',
  1076. visible
  1077. });
  1078. }
  1079. /**
  1080. * Notify external application of a participant, remote or local, being
  1081. * removed from the conference by another participant.
  1082. *
  1083. * @param {string} kicked - The ID of the participant removed from the
  1084. * conference.
  1085. * @param {string} kicker - The ID of the participant that removed the
  1086. * other participant.
  1087. * @returns {void}
  1088. */
  1089. notifyKickedOut(kicked: Object, kicker: Object) {
  1090. this._sendEvent({
  1091. name: 'participant-kicked-out',
  1092. kicked,
  1093. kicker
  1094. });
  1095. }
  1096. /**
  1097. * Notify external application of the current meeting requiring a password
  1098. * to join.
  1099. *
  1100. * @returns {void}
  1101. */
  1102. notifyOnPasswordRequired() {
  1103. this._sendEvent({ name: 'password-required' });
  1104. }
  1105. /**
  1106. * Notify external application (if API is enabled) that the screen sharing
  1107. * has been turned on/off.
  1108. *
  1109. * @param {boolean} on - True if screen sharing is enabled.
  1110. * @param {Object} details - Additional information about the screen
  1111. * sharing.
  1112. * @param {string} details.sourceType - Type of device or window the screen
  1113. * share is capturing.
  1114. * @returns {void}
  1115. */
  1116. notifyScreenSharingStatusChanged(on: boolean, details: Object) {
  1117. this._sendEvent({
  1118. name: 'screen-sharing-status-changed',
  1119. on,
  1120. details
  1121. });
  1122. }
  1123. /**
  1124. * Notify external application (if API is enabled) that the dominant speaker
  1125. * has been turned on/off.
  1126. *
  1127. * @param {string} id - Id of the dominant participant.
  1128. * @returns {void}
  1129. */
  1130. notifyDominantSpeakerChanged(id: string) {
  1131. this._sendEvent({
  1132. name: 'dominant-speaker-changed',
  1133. id
  1134. });
  1135. }
  1136. /**
  1137. * Notify external application (if API is enabled) that the conference
  1138. * changed their subject.
  1139. *
  1140. * @param {string} subject - Conference subject.
  1141. * @returns {void}
  1142. */
  1143. notifySubjectChanged(subject: string) {
  1144. this._sendEvent({
  1145. name: 'subject-change',
  1146. subject
  1147. });
  1148. }
  1149. /**
  1150. * Notify external application (if API is enabled) that tile view has been
  1151. * entered or exited.
  1152. *
  1153. * @param {string} enabled - True if tile view is currently displayed, false
  1154. * otherwise.
  1155. * @returns {void}
  1156. */
  1157. notifyTileViewChanged(enabled: boolean) {
  1158. this._sendEvent({
  1159. name: 'tile-view-changed',
  1160. enabled
  1161. });
  1162. }
  1163. /**
  1164. * Notify external application (if API is enabled) that the localStorage has changed.
  1165. *
  1166. * @param {string} localStorageContent - The new localStorageContent.
  1167. * @returns {void}
  1168. */
  1169. notifyLocalStorageChanged(localStorageContent: string) {
  1170. this._sendEvent({
  1171. name: 'local-storage-changed',
  1172. localStorageContent
  1173. });
  1174. }
  1175. /**
  1176. * Notify external application (if API is enabled) that user updated their hand raised.
  1177. *
  1178. * @param {string} id - User id.
  1179. * @param {boolean} handRaised - Whether user has raised hand.
  1180. * @returns {void}
  1181. */
  1182. notifyRaiseHandUpdated(id: string, handRaised: boolean) {
  1183. this._sendEvent({
  1184. name: 'raise-hand-updated',
  1185. handRaised,
  1186. id
  1187. });
  1188. }
  1189. /**
  1190. * Notify external application (if API is enabled) that recording has started or stopped.
  1191. *
  1192. * @param {boolean} on - True if recording is on, false otherwise.
  1193. * @param {string} mode - Stream or file.
  1194. * @param {string} error - Error type or null if success.
  1195. * @returns {void}
  1196. */
  1197. notifyRecordingStatusChanged(on: boolean, mode: string, error?: string) {
  1198. this._sendEvent({
  1199. name: 'recording-status-changed',
  1200. on,
  1201. mode,
  1202. error
  1203. });
  1204. }
  1205. /**
  1206. * Notify external application (if API is enabled) that an error occured.
  1207. *
  1208. * @param {Object} error - The error.
  1209. * @returns {void}
  1210. */
  1211. notifyError(error: Object) {
  1212. this._sendEvent({
  1213. name: 'error',
  1214. error
  1215. });
  1216. }
  1217. /**
  1218. * Disposes the allocated resources.
  1219. *
  1220. * @returns {void}
  1221. */
  1222. dispose() {
  1223. if (this._enabled) {
  1224. this._enabled = false;
  1225. }
  1226. }
  1227. }
  1228. export default new API();