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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334
  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. default:
  510. return false;
  511. }
  512. return true;
  513. });
  514. }
  515. /**
  516. * Check whether the API should be enabled or not.
  517. *
  518. * @returns {boolean}
  519. */
  520. function shouldBeEnabled() {
  521. return (
  522. typeof API_ID === 'number'
  523. // XXX Enable the API when a JSON Web Token (JWT) is specified in
  524. // the location/URL because then it is very likely that the Jitsi
  525. // Meet (Web) app is being used by an external/wrapping (Web) app
  526. // and, consequently, the latter will need to communicate with the
  527. // former. (The described logic is merely a heuristic though.)
  528. || parseJWTFromURLParams());
  529. }
  530. /**
  531. * Executes on toggle-share-screen command.
  532. *
  533. * @param {boolean} [enable] - Whether this toggle is to explicitly enable or
  534. * disable screensharing. If not defined, the application will automatically
  535. * attempt to toggle between enabled and disabled. This boolean is useful for
  536. * explicitly setting desired screensharing state.
  537. * @returns {void}
  538. */
  539. function toggleScreenSharing(enable) {
  540. if (JitsiMeetJS.isDesktopSharingEnabled()) {
  541. APP.store.dispatch(startScreenShareFlow(enable));
  542. }
  543. }
  544. /**
  545. * Removes sensitive data from a mouse event.
  546. *
  547. * @param {MouseEvent} event - The mouse event to sanitize.
  548. * @returns {Object}
  549. */
  550. function sanitizeMouseEvent(event: MouseEvent) {
  551. const {
  552. clientX,
  553. clientY,
  554. movementX,
  555. movementY,
  556. offsetX,
  557. offsetY,
  558. pageX,
  559. pageY,
  560. x,
  561. y,
  562. screenX,
  563. screenY
  564. } = event;
  565. return {
  566. clientX,
  567. clientY,
  568. movementX,
  569. movementY,
  570. offsetX,
  571. offsetY,
  572. pageX,
  573. pageY,
  574. x,
  575. y,
  576. screenX,
  577. screenY
  578. };
  579. }
  580. /**
  581. * Implements API class that communicates with external API class and provides
  582. * interface to access Jitsi Meet features by external applications that embed
  583. * Jitsi Meet.
  584. */
  585. class API {
  586. _enabled: boolean;
  587. /**
  588. * Initializes the API. Setups message event listeners that will receive
  589. * information from external applications that embed Jitsi Meet. It also
  590. * sends a message to the external application that API is initialized.
  591. *
  592. * @param {Object} options - Optional parameters.
  593. * @returns {void}
  594. */
  595. init() {
  596. if (!shouldBeEnabled()) {
  597. return;
  598. }
  599. /**
  600. * Current status (enabled/disabled) of API.
  601. *
  602. * @private
  603. * @type {boolean}
  604. */
  605. this._enabled = true;
  606. initCommands();
  607. }
  608. /**
  609. * Notify external application (if API is enabled) that the large video
  610. * visibility changed.
  611. *
  612. * @param {boolean} isHidden - True if the large video is hidden and false
  613. * otherwise.
  614. * @returns {void}
  615. */
  616. notifyLargeVideoVisibilityChanged(isHidden: boolean) {
  617. this._sendEvent({
  618. name: 'large-video-visibility-changed',
  619. isVisible: !isHidden
  620. });
  621. }
  622. /**
  623. * Notifies the external application (spot) that the local jitsi-participant
  624. * has a status update.
  625. *
  626. * @param {Object} event - The message to pass onto spot.
  627. * @returns {void}
  628. */
  629. sendProxyConnectionEvent(event: Object) {
  630. this._sendEvent({
  631. name: 'proxy-connection-event',
  632. ...event
  633. });
  634. }
  635. /**
  636. * Sends event to the external application.
  637. *
  638. * @param {Object} event - The event to be sent.
  639. * @returns {void}
  640. */
  641. _sendEvent(event: Object = {}) {
  642. if (this._enabled) {
  643. transport.sendEvent(event);
  644. }
  645. }
  646. /**
  647. * Notify external application (if API is enabled) that the chat state has been updated.
  648. *
  649. * @param {number} unreadCount - The unread messages counter.
  650. * @param {boolean} isOpen - True if the chat panel is open.
  651. * @returns {void}
  652. */
  653. notifyChatUpdated(unreadCount: number, isOpen: boolean) {
  654. this._sendEvent({
  655. name: 'chat-updated',
  656. unreadCount,
  657. isOpen
  658. });
  659. }
  660. /**
  661. * Notify external application (if API is enabled) that message was sent.
  662. *
  663. * @param {string} message - Message body.
  664. * @param {boolean} privateMessage - True if the message was a private message.
  665. * @returns {void}
  666. */
  667. notifySendingChatMessage(message: string, privateMessage: boolean) {
  668. this._sendEvent({
  669. name: 'outgoing-message',
  670. message,
  671. privateMessage
  672. });
  673. }
  674. /**
  675. * Notify external application (if API is enabled) that the mouse has entered inside the iframe.
  676. *
  677. * @param {MouseEvent} event - The mousemove event.
  678. * @returns {void}
  679. */
  680. notifyMouseEnter(event: MouseEvent) {
  681. this._sendEvent({
  682. name: 'mouse-enter',
  683. event: sanitizeMouseEvent(event)
  684. });
  685. }
  686. /**
  687. * Notify external application (if API is enabled) that the mouse has entered inside the iframe.
  688. *
  689. * @param {MouseEvent} event - The mousemove event.
  690. * @returns {void}
  691. */
  692. notifyMouseLeave(event: MouseEvent) {
  693. this._sendEvent({
  694. name: 'mouse-leave',
  695. event: sanitizeMouseEvent(event)
  696. });
  697. }
  698. /**
  699. * Notify external application (if API is enabled) that the mouse has moved inside the iframe.
  700. *
  701. * @param {MouseEvent} event - The mousemove event.
  702. * @returns {void}
  703. */
  704. notifyMouseMove(event: MouseEvent) {
  705. this._sendEvent({
  706. name: 'mouse-move',
  707. event: sanitizeMouseEvent(event)
  708. });
  709. }
  710. /**
  711. * Notify external application that the video quality setting has changed.
  712. *
  713. * @param {number} videoQuality - The video quality. The number represents the maximum height of the video streams.
  714. * @returns {void}
  715. */
  716. notifyVideoQualityChanged(videoQuality: number) {
  717. this._sendEvent({
  718. name: 'video-quality-changed',
  719. videoQuality
  720. });
  721. }
  722. /**
  723. * Notify external application (if API is enabled) that message was
  724. * received.
  725. *
  726. * @param {Object} options - Object with the message properties.
  727. * @returns {void}
  728. */
  729. notifyReceivedChatMessage(
  730. { body, id, nick, privateMessage, ts }: {
  731. body: *, id: string, nick: string, privateMessage: boolean, ts: *
  732. } = {}) {
  733. if (APP.conference.isLocalId(id)) {
  734. return;
  735. }
  736. this._sendEvent({
  737. name: 'incoming-message',
  738. from: id,
  739. message: body,
  740. nick,
  741. privateMessage,
  742. stamp: ts
  743. });
  744. }
  745. /**
  746. * Notify external application (if API is enabled) that user joined the
  747. * conference.
  748. *
  749. * @param {string} id - User id.
  750. * @param {Object} props - The display name of the user.
  751. * @returns {void}
  752. */
  753. notifyUserJoined(id: string, props: Object) {
  754. this._sendEvent({
  755. name: 'participant-joined',
  756. id,
  757. ...props
  758. });
  759. }
  760. /**
  761. * Notify external application (if API is enabled) that user left the
  762. * conference.
  763. *
  764. * @param {string} id - User id.
  765. * @returns {void}
  766. */
  767. notifyUserLeft(id: string) {
  768. this._sendEvent({
  769. name: 'participant-left',
  770. id
  771. });
  772. }
  773. /**
  774. * Notify external application (if API is enabled) that the user role
  775. * has changed.
  776. *
  777. * @param {string} id - User id.
  778. * @param {string} role - The new user role.
  779. * @returns {void}
  780. */
  781. notifyUserRoleChanged(id: string, role: string) {
  782. this._sendEvent({
  783. name: 'participant-role-changed',
  784. id,
  785. role
  786. });
  787. }
  788. /**
  789. * Notify external application (if API is enabled) that user changed their
  790. * avatar.
  791. *
  792. * @param {string} id - User id.
  793. * @param {string} avatarURL - The new avatar URL of the participant.
  794. * @returns {void}
  795. */
  796. notifyAvatarChanged(id: string, avatarURL: string) {
  797. this._sendEvent({
  798. name: 'avatar-changed',
  799. avatarURL,
  800. id
  801. });
  802. }
  803. /**
  804. * Notify external application (if API is enabled) that user received
  805. * a text message through datachannels.
  806. *
  807. * @param {Object} data - The event data.
  808. * @returns {void}
  809. */
  810. notifyEndpointTextMessageReceived(data: Object) {
  811. this._sendEvent({
  812. name: 'endpoint-text-message-received',
  813. data
  814. });
  815. }
  816. /**
  817. * Notify external application (if API is enabled) that the list of sharing participants changed.
  818. *
  819. * @param {Object} data - The event data.
  820. * @returns {void}
  821. */
  822. notifySharingParticipantsChanged(data: Object) {
  823. this._sendEvent({
  824. name: 'content-sharing-participants-changed',
  825. data
  826. });
  827. }
  828. /**
  829. * Notify external application (if API is enabled) that the device list has
  830. * changed.
  831. *
  832. * @param {Object} devices - The new device list.
  833. * @returns {void}
  834. */
  835. notifyDeviceListChanged(devices: Object) {
  836. this._sendEvent({
  837. name: 'device-list-changed',
  838. devices
  839. });
  840. }
  841. /**
  842. * Notify external application (if API is enabled) that user changed their
  843. * nickname.
  844. *
  845. * @param {string} id - User id.
  846. * @param {string} displayname - User nickname.
  847. * @param {string} formattedDisplayName - The display name shown in Jitsi
  848. * meet's UI for the user.
  849. * @returns {void}
  850. */
  851. notifyDisplayNameChanged(
  852. id: string,
  853. { displayName, formattedDisplayName }: Object) {
  854. this._sendEvent({
  855. name: 'display-name-change',
  856. displayname: displayName,
  857. formattedDisplayName,
  858. id
  859. });
  860. }
  861. /**
  862. * Notify external application (if API is enabled) that user changed their
  863. * email.
  864. *
  865. * @param {string} id - User id.
  866. * @param {string} email - The new email of the participant.
  867. * @returns {void}
  868. */
  869. notifyEmailChanged(
  870. id: string,
  871. { email }: Object) {
  872. this._sendEvent({
  873. name: 'email-change',
  874. email,
  875. id
  876. });
  877. }
  878. /**
  879. * Notify external application (if API is enabled) that the an error has been logged.
  880. *
  881. * @param {string} logLevel - The message log level.
  882. * @param {Array} args - Array of strings composing the log message.
  883. * @returns {void}
  884. */
  885. notifyLog(logLevel: string, args: Array<string>) {
  886. this._sendEvent({
  887. name: 'log',
  888. logLevel,
  889. args
  890. });
  891. }
  892. /**
  893. * Notify external application (if API is enabled) that the conference has
  894. * been joined.
  895. *
  896. * @param {string} roomName - The room name.
  897. * @param {string} id - The id of the local user.
  898. * @param {Object} props - The display name and avatar URL of the local
  899. * user.
  900. * @returns {void}
  901. */
  902. notifyConferenceJoined(roomName: string, id: string, props: Object) {
  903. this._sendEvent({
  904. name: 'video-conference-joined',
  905. roomName,
  906. id,
  907. ...props
  908. });
  909. }
  910. /**
  911. * Notify external application (if API is enabled) that local user has left the conference.
  912. *
  913. * @param {string} roomName - User id.
  914. * @returns {void}
  915. */
  916. notifyConferenceLeft(roomName: string) {
  917. this._sendEvent({
  918. name: 'video-conference-left',
  919. roomName
  920. });
  921. }
  922. /**
  923. * Notify external application (if API is enabled) that we are ready to be
  924. * closed.
  925. *
  926. * @returns {void}
  927. */
  928. notifyReadyToClose() {
  929. this._sendEvent({ name: 'video-ready-to-close' });
  930. }
  931. /**
  932. * Notify external application (if API is enabled) that a suspend event in host computer.
  933. *
  934. * @returns {void}
  935. */
  936. notifySuspendDetected() {
  937. this._sendEvent({ name: 'suspend-detected' });
  938. }
  939. /**
  940. * Notify external application (if API is enabled) for audio muted status
  941. * changed.
  942. *
  943. * @param {boolean} muted - The new muted status.
  944. * @returns {void}
  945. */
  946. notifyAudioMutedStatusChanged(muted: boolean) {
  947. this._sendEvent({
  948. name: 'audio-mute-status-changed',
  949. muted
  950. });
  951. }
  952. /**
  953. * Notify external application (if API is enabled) for video muted status
  954. * changed.
  955. *
  956. * @param {boolean} muted - The new muted status.
  957. * @returns {void}
  958. */
  959. notifyVideoMutedStatusChanged(muted: boolean) {
  960. this._sendEvent({
  961. name: 'video-mute-status-changed',
  962. muted
  963. });
  964. }
  965. /**
  966. * Notify external application (if API is enabled) for audio availability
  967. * changed.
  968. *
  969. * @param {boolean} available - True if available and false otherwise.
  970. * @returns {void}
  971. */
  972. notifyAudioAvailabilityChanged(available: boolean) {
  973. audioAvailable = available;
  974. this._sendEvent({
  975. name: 'audio-availability-changed',
  976. available
  977. });
  978. }
  979. /**
  980. * Notify external application (if API is enabled) for video available
  981. * status changed.
  982. *
  983. * @param {boolean} available - True if available and false otherwise.
  984. * @returns {void}
  985. */
  986. notifyVideoAvailabilityChanged(available: boolean) {
  987. videoAvailable = available;
  988. this._sendEvent({
  989. name: 'video-availability-changed',
  990. available
  991. });
  992. }
  993. /**
  994. * Notify external application (if API is enabled) that the on stage
  995. * participant has changed.
  996. *
  997. * @param {string} id - User id of the new on stage participant.
  998. * @returns {void}
  999. */
  1000. notifyOnStageParticipantChanged(id: string) {
  1001. this._sendEvent({
  1002. name: 'on-stage-participant-changed',
  1003. id
  1004. });
  1005. }
  1006. /**
  1007. * Notify external application of an unexpected camera-related error having
  1008. * occurred.
  1009. *
  1010. * @param {string} type - The type of the camera error.
  1011. * @param {string} message - Additional information about the error.
  1012. * @returns {void}
  1013. */
  1014. notifyOnCameraError(type: string, message: string) {
  1015. this._sendEvent({
  1016. name: 'camera-error',
  1017. type,
  1018. message
  1019. });
  1020. }
  1021. /**
  1022. * Notify external application of an unexpected mic-related error having
  1023. * occurred.
  1024. *
  1025. * @param {string} type - The type of the mic error.
  1026. * @param {string} message - Additional information about the error.
  1027. * @returns {void}
  1028. */
  1029. notifyOnMicError(type: string, message: string) {
  1030. this._sendEvent({
  1031. name: 'mic-error',
  1032. type,
  1033. message
  1034. });
  1035. }
  1036. /**
  1037. * Notify external application (if API is enabled) that conference feedback
  1038. * has been submitted. Intended to be used in conjunction with the
  1039. * submit-feedback command to get notified if feedback was submitted.
  1040. *
  1041. * @param {string} error - A failure message, if any.
  1042. * @returns {void}
  1043. */
  1044. notifyFeedbackSubmitted(error: string) {
  1045. this._sendEvent({
  1046. name: 'feedback-submitted',
  1047. error
  1048. });
  1049. }
  1050. /**
  1051. * Notify external application (if API is enabled) that the feedback prompt
  1052. * has been displayed.
  1053. *
  1054. * @returns {void}
  1055. */
  1056. notifyFeedbackPromptDisplayed() {
  1057. this._sendEvent({ name: 'feedback-prompt-displayed' });
  1058. }
  1059. /**
  1060. * Notify external application (if API is enabled) that the display
  1061. * configuration of the filmstrip has been changed.
  1062. *
  1063. * @param {boolean} visible - Whether or not the filmstrip has been set to
  1064. * be displayed or hidden.
  1065. * @returns {void}
  1066. */
  1067. notifyFilmstripDisplayChanged(visible: boolean) {
  1068. this._sendEvent({
  1069. name: 'filmstrip-display-changed',
  1070. visible
  1071. });
  1072. }
  1073. /**
  1074. * Notify external application of a participant, remote or local, being
  1075. * removed from the conference by another participant.
  1076. *
  1077. * @param {string} kicked - The ID of the participant removed from the
  1078. * conference.
  1079. * @param {string} kicker - The ID of the participant that removed the
  1080. * other participant.
  1081. * @returns {void}
  1082. */
  1083. notifyKickedOut(kicked: Object, kicker: Object) {
  1084. this._sendEvent({
  1085. name: 'participant-kicked-out',
  1086. kicked,
  1087. kicker
  1088. });
  1089. }
  1090. /**
  1091. * Notify external application of the current meeting requiring a password
  1092. * to join.
  1093. *
  1094. * @returns {void}
  1095. */
  1096. notifyOnPasswordRequired() {
  1097. this._sendEvent({ name: 'password-required' });
  1098. }
  1099. /**
  1100. * Notify external application (if API is enabled) that the screen sharing
  1101. * has been turned on/off.
  1102. *
  1103. * @param {boolean} on - True if screen sharing is enabled.
  1104. * @param {Object} details - Additional information about the screen
  1105. * sharing.
  1106. * @param {string} details.sourceType - Type of device or window the screen
  1107. * share is capturing.
  1108. * @returns {void}
  1109. */
  1110. notifyScreenSharingStatusChanged(on: boolean, details: Object) {
  1111. this._sendEvent({
  1112. name: 'screen-sharing-status-changed',
  1113. on,
  1114. details
  1115. });
  1116. }
  1117. /**
  1118. * Notify external application (if API is enabled) that the dominant speaker
  1119. * has been turned on/off.
  1120. *
  1121. * @param {string} id - Id of the dominant participant.
  1122. * @returns {void}
  1123. */
  1124. notifyDominantSpeakerChanged(id: string) {
  1125. this._sendEvent({
  1126. name: 'dominant-speaker-changed',
  1127. id
  1128. });
  1129. }
  1130. /**
  1131. * Notify external application (if API is enabled) that the conference
  1132. * changed their subject.
  1133. *
  1134. * @param {string} subject - Conference subject.
  1135. * @returns {void}
  1136. */
  1137. notifySubjectChanged(subject: string) {
  1138. this._sendEvent({
  1139. name: 'subject-change',
  1140. subject
  1141. });
  1142. }
  1143. /**
  1144. * Notify external application (if API is enabled) that tile view has been
  1145. * entered or exited.
  1146. *
  1147. * @param {string} enabled - True if tile view is currently displayed, false
  1148. * otherwise.
  1149. * @returns {void}
  1150. */
  1151. notifyTileViewChanged(enabled: boolean) {
  1152. this._sendEvent({
  1153. name: 'tile-view-changed',
  1154. enabled
  1155. });
  1156. }
  1157. /**
  1158. * Notify external application (if API is enabled) that the localStorage has changed.
  1159. *
  1160. * @param {string} localStorageContent - The new localStorageContent.
  1161. * @returns {void}
  1162. */
  1163. notifyLocalStorageChanged(localStorageContent: string) {
  1164. this._sendEvent({
  1165. name: 'local-storage-changed',
  1166. localStorageContent
  1167. });
  1168. }
  1169. /**
  1170. * Notify external application (if API is enabled) that user updated their hand raised.
  1171. *
  1172. * @param {string} id - User id.
  1173. * @param {boolean} handRaised - Whether user has raised hand.
  1174. * @returns {void}
  1175. */
  1176. notifyRaiseHandUpdated(id: string, handRaised: boolean) {
  1177. this._sendEvent({
  1178. name: 'raise-hand-updated',
  1179. handRaised,
  1180. id
  1181. });
  1182. }
  1183. /**
  1184. * Notify external application (if API is enabled) that recording has started or stopped.
  1185. *
  1186. * @param {boolean} on - True if recording is on, false otherwise.
  1187. * @param {string} mode - Stream or file.
  1188. * @param {string} error - Error type or null if success.
  1189. * @returns {void}
  1190. */
  1191. notifyRecordingStatusChanged(on: boolean, mode: string, error?: string) {
  1192. this._sendEvent({
  1193. name: 'recording-status-changed',
  1194. on,
  1195. mode,
  1196. error
  1197. });
  1198. }
  1199. /**
  1200. * Disposes the allocated resources.
  1201. *
  1202. * @returns {void}
  1203. */
  1204. dispose() {
  1205. if (this._enabled) {
  1206. this._enabled = false;
  1207. }
  1208. }
  1209. }
  1210. export default new API();