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.

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340
  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. * Disposes the allocated resources.
  1207. *
  1208. * @returns {void}
  1209. */
  1210. dispose() {
  1211. if (this._enabled) {
  1212. this._enabled = false;
  1213. }
  1214. }
  1215. }
  1216. export default new API();