Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

API.js 40KB

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