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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793
  1. // @flow
  2. import * as JitsiMeetConferenceEvents from '../../ConferenceEvents';
  3. import {
  4. createApiEvent,
  5. sendAnalytics
  6. } from '../../react/features/analytics';
  7. import {
  8. sendTones,
  9. setPassword,
  10. setSubject
  11. } from '../../react/features/base/conference';
  12. import { parseJWTFromURLParams } from '../../react/features/base/jwt';
  13. import { invite } from '../../react/features/invite';
  14. import { toggleTileView } from '../../react/features/video-layout';
  15. import { getJitsiMeetTransport } from '../transport';
  16. import { API_ID } from './constants';
  17. import {
  18. processExternalDeviceRequest
  19. } from '../../react/features/device-selection/functions';
  20. const logger = require('jitsi-meet-logger').getLogger(__filename);
  21. declare var APP: Object;
  22. /**
  23. * List of the available commands.
  24. */
  25. let commands = {};
  26. /**
  27. * The state of screen sharing(started/stopped) before the screen sharing is
  28. * enabled and initialized.
  29. * NOTE: This flag help us to cache the state and use it if toggle-share-screen
  30. * was received before the initialization.
  31. */
  32. let initialScreenSharingState = false;
  33. /**
  34. * The transport instance used for communication with external apps.
  35. *
  36. * @type {Transport}
  37. */
  38. const transport = getJitsiMeetTransport();
  39. /**
  40. * The current audio availability.
  41. *
  42. * @type {boolean}
  43. */
  44. let audioAvailable = true;
  45. /**
  46. * The current video availability.
  47. *
  48. * @type {boolean}
  49. */
  50. let videoAvailable = true;
  51. /**
  52. * Initializes supported commands.
  53. *
  54. * @returns {void}
  55. */
  56. function initCommands() {
  57. commands = {
  58. 'display-name': displayName => {
  59. sendAnalytics(createApiEvent('display.name.changed'));
  60. APP.conference.changeLocalDisplayName(displayName);
  61. },
  62. 'password': password => {
  63. const { conference, passwordRequired }
  64. = APP.store.getState()['features/base/conference'];
  65. if (passwordRequired) {
  66. sendAnalytics(createApiEvent('submit.password'));
  67. APP.store.dispatch(setPassword(
  68. passwordRequired,
  69. passwordRequired.join,
  70. password
  71. ));
  72. } else {
  73. sendAnalytics(createApiEvent('password.changed'));
  74. APP.store.dispatch(setPassword(
  75. conference,
  76. conference.lock,
  77. password
  78. ));
  79. }
  80. },
  81. 'proxy-connection-event': event => {
  82. APP.conference.onProxyConnectionEvent(event);
  83. },
  84. 'send-tones': (options = {}) => {
  85. const { duration, tones, pause } = options;
  86. APP.store.dispatch(sendTones(tones, duration, pause));
  87. },
  88. 'subject': subject => {
  89. sendAnalytics(createApiEvent('subject.changed'));
  90. APP.store.dispatch(setSubject(subject));
  91. },
  92. 'submit-feedback': feedback => {
  93. sendAnalytics(createApiEvent('submit.feedback'));
  94. APP.conference.submitFeedback(feedback.score, feedback.message);
  95. },
  96. 'toggle-audio': () => {
  97. sendAnalytics(createApiEvent('toggle-audio'));
  98. logger.log('Audio toggle: API command received');
  99. APP.conference.toggleAudioMuted(false /* no UI */);
  100. },
  101. 'toggle-video': () => {
  102. sendAnalytics(createApiEvent('toggle-video'));
  103. logger.log('Video toggle: API command received');
  104. APP.conference.toggleVideoMuted(false /* no UI */);
  105. },
  106. 'toggle-film-strip': () => {
  107. sendAnalytics(createApiEvent('film.strip.toggled'));
  108. APP.UI.toggleFilmstrip();
  109. },
  110. 'toggle-chat': () => {
  111. sendAnalytics(createApiEvent('chat.toggled'));
  112. APP.UI.toggleChat();
  113. },
  114. /**
  115. * Callback to invoke when the "toggle-share-screen" command is received.
  116. *
  117. * @param {Object} options - Additional details of how to perform
  118. * the action. Note this parameter is undocumented and experimental.
  119. * @param {boolean} options.enable - Whether trying to enable screen
  120. * sharing or to turn it off.
  121. * @returns {void}
  122. */
  123. 'toggle-share-screen': (options = {}) => {
  124. sendAnalytics(createApiEvent('screen.sharing.toggled'));
  125. toggleScreenSharing(options.enable);
  126. },
  127. 'toggle-tile-view': () => {
  128. sendAnalytics(createApiEvent('tile-view.toggled'));
  129. APP.store.dispatch(toggleTileView());
  130. },
  131. 'video-hangup': (showFeedbackDialog = true) => {
  132. sendAnalytics(createApiEvent('video.hangup'));
  133. APP.conference.hangup(showFeedbackDialog);
  134. },
  135. 'email': email => {
  136. sendAnalytics(createApiEvent('email.changed'));
  137. APP.conference.changeLocalEmail(email);
  138. },
  139. 'avatar-url': avatarUrl => {
  140. sendAnalytics(createApiEvent('avatar.url.changed'));
  141. APP.conference.changeLocalAvatarUrl(avatarUrl);
  142. }
  143. };
  144. transport.on('event', ({ data, name }) => {
  145. if (name && commands[name]) {
  146. commands[name](...data);
  147. return true;
  148. }
  149. return false;
  150. });
  151. transport.on('request', (request, callback) => {
  152. const { dispatch, getState } = APP.store;
  153. if (processExternalDeviceRequest(dispatch, getState, request, callback)) {
  154. return true;
  155. }
  156. const { name } = request;
  157. switch (name) {
  158. case 'invite': {
  159. const { invitees } = request;
  160. if (!Array.isArray(invitees) || invitees.length === 0) {
  161. callback({
  162. error: new Error('Unexpected format of invitees')
  163. });
  164. break;
  165. }
  166. // The store should be already available because API.init is called
  167. // on appWillMount action.
  168. APP.store.dispatch(
  169. invite(invitees, true))
  170. .then(failedInvitees => {
  171. let error;
  172. let result;
  173. if (failedInvitees.length) {
  174. error = new Error('One or more invites failed!');
  175. } else {
  176. result = true;
  177. }
  178. callback({
  179. error,
  180. result
  181. });
  182. });
  183. break;
  184. }
  185. case 'is-audio-muted':
  186. callback(APP.conference.isLocalAudioMuted());
  187. break;
  188. case 'is-video-muted':
  189. callback(APP.conference.isLocalVideoMuted());
  190. break;
  191. case 'is-audio-available':
  192. callback(audioAvailable);
  193. break;
  194. case 'is-video-available':
  195. callback(videoAvailable);
  196. break;
  197. default:
  198. return false;
  199. }
  200. return true;
  201. });
  202. }
  203. /**
  204. * Listens for desktop/screen sharing enabled events and toggles the screen
  205. * sharing if needed.
  206. *
  207. * @param {boolean} enabled - Current screen sharing enabled status.
  208. * @returns {void}
  209. */
  210. function onDesktopSharingEnabledChanged(enabled = false) {
  211. if (enabled && initialScreenSharingState) {
  212. toggleScreenSharing();
  213. }
  214. }
  215. /**
  216. * Check whether the API should be enabled or not.
  217. *
  218. * @returns {boolean}
  219. */
  220. function shouldBeEnabled() {
  221. return (
  222. typeof API_ID === 'number'
  223. // XXX Enable the API when a JSON Web Token (JWT) is specified in
  224. // the location/URL because then it is very likely that the Jitsi
  225. // Meet (Web) app is being used by an external/wrapping (Web) app
  226. // and, consequently, the latter will need to communicate with the
  227. // former. (The described logic is merely a heuristic though.)
  228. || parseJWTFromURLParams());
  229. }
  230. /**
  231. * Executes on toggle-share-screen command.
  232. *
  233. * @param {boolean} [enable] - Whether this toggle is to explicitly enable or
  234. * disable screensharing. If not defined, the application will automatically
  235. * attempt to toggle between enabled and disabled. This boolean is useful for
  236. * explicitly setting desired screensharing state.
  237. * @returns {void}
  238. */
  239. function toggleScreenSharing(enable) {
  240. if (APP.conference.isDesktopSharingEnabled) {
  241. // eslint-disable-next-line no-empty-function
  242. APP.conference.toggleScreenSharing(enable).catch(() => {});
  243. } else {
  244. initialScreenSharingState = !initialScreenSharingState;
  245. }
  246. }
  247. /**
  248. * Implements API class that communicates with external API class and provides
  249. * interface to access Jitsi Meet features by external applications that embed
  250. * Jitsi Meet.
  251. */
  252. class API {
  253. _enabled: boolean;
  254. /**
  255. * Initializes the API. Setups message event listeners that will receive
  256. * information from external applications that embed Jitsi Meet. It also
  257. * sends a message to the external application that API is initialized.
  258. *
  259. * @param {Object} options - Optional parameters.
  260. * @returns {void}
  261. */
  262. init() {
  263. if (!shouldBeEnabled()) {
  264. return;
  265. }
  266. /**
  267. * Current status (enabled/disabled) of API.
  268. *
  269. * @private
  270. * @type {boolean}
  271. */
  272. this._enabled = true;
  273. APP.conference.addListener(
  274. JitsiMeetConferenceEvents.DESKTOP_SHARING_ENABLED_CHANGED,
  275. onDesktopSharingEnabledChanged);
  276. initCommands();
  277. }
  278. /**
  279. * Notify external application (if API is enabled) that the large video
  280. * visibility changed.
  281. *
  282. * @param {boolean} isHidden - True if the large video is hidden and false
  283. * otherwise.
  284. * @returns {void}
  285. */
  286. notifyLargeVideoVisibilityChanged(isHidden: boolean) {
  287. this._sendEvent({
  288. name: 'large-video-visibility-changed',
  289. isVisible: !isHidden
  290. });
  291. }
  292. /**
  293. * Notifies the external application (spot) that the local jitsi-participant
  294. * has a status update.
  295. *
  296. * @param {Object} event - The message to pass onto spot.
  297. * @returns {void}
  298. */
  299. sendProxyConnectionEvent(event: Object) {
  300. this._sendEvent({
  301. name: 'proxy-connection-event',
  302. ...event
  303. });
  304. }
  305. /**
  306. * Sends event to the external application.
  307. *
  308. * @param {Object} event - The event to be sent.
  309. * @returns {void}
  310. */
  311. _sendEvent(event: Object = {}) {
  312. if (this._enabled) {
  313. transport.sendEvent(event);
  314. }
  315. }
  316. /**
  317. * Notify external application (if API is enabled) that message was sent.
  318. *
  319. * @param {string} message - Message body.
  320. * @returns {void}
  321. */
  322. notifySendingChatMessage(message: string) {
  323. this._sendEvent({
  324. name: 'outgoing-message',
  325. message
  326. });
  327. }
  328. /**
  329. * Notify external application (if API is enabled) that message was
  330. * received.
  331. *
  332. * @param {Object} options - Object with the message properties.
  333. * @returns {void}
  334. */
  335. notifyReceivedChatMessage(
  336. { body, id, nick, ts }: {
  337. body: *, id: string, nick: string, ts: *
  338. } = {}) {
  339. if (APP.conference.isLocalId(id)) {
  340. return;
  341. }
  342. this._sendEvent({
  343. name: 'incoming-message',
  344. from: id,
  345. message: body,
  346. nick,
  347. stamp: ts
  348. });
  349. }
  350. /**
  351. * Notify external application (if API is enabled) that user joined the
  352. * conference.
  353. *
  354. * @param {string} id - User id.
  355. * @param {Object} props - The display name of the user.
  356. * @returns {void}
  357. */
  358. notifyUserJoined(id: string, props: Object) {
  359. this._sendEvent({
  360. name: 'participant-joined',
  361. id,
  362. ...props
  363. });
  364. }
  365. /**
  366. * Notify external application (if API is enabled) that user left the
  367. * conference.
  368. *
  369. * @param {string} id - User id.
  370. * @returns {void}
  371. */
  372. notifyUserLeft(id: string) {
  373. this._sendEvent({
  374. name: 'participant-left',
  375. id
  376. });
  377. }
  378. /**
  379. * Notify external application (if API is enabled) that user changed their
  380. * avatar.
  381. *
  382. * @param {string} id - User id.
  383. * @param {string} avatarURL - The new avatar URL of the participant.
  384. * @returns {void}
  385. */
  386. notifyAvatarChanged(id: string, avatarURL: string) {
  387. this._sendEvent({
  388. name: 'avatar-changed',
  389. avatarURL,
  390. id
  391. });
  392. }
  393. /**
  394. * Notify external application (if API is enabled) that the device list has
  395. * changed.
  396. *
  397. * @param {Object} devices - The new device list.
  398. * @returns {void}
  399. */
  400. notifyDeviceListChanged(devices: Object) {
  401. this._sendEvent({
  402. name: 'device-list-changed',
  403. devices });
  404. }
  405. /**
  406. * Notify external application (if API is enabled) that user changed their
  407. * nickname.
  408. *
  409. * @param {string} id - User id.
  410. * @param {string} displayname - User nickname.
  411. * @param {string} formattedDisplayName - The display name shown in Jitsi
  412. * meet's UI for the user.
  413. * @returns {void}
  414. */
  415. notifyDisplayNameChanged(
  416. id: string,
  417. { displayName, formattedDisplayName }: Object) {
  418. this._sendEvent({
  419. name: 'display-name-change',
  420. displayname: displayName,
  421. formattedDisplayName,
  422. id
  423. });
  424. }
  425. /**
  426. * Notify external application (if API is enabled) that user changed their
  427. * email.
  428. *
  429. * @param {string} id - User id.
  430. * @param {string} email - The new email of the participant.
  431. * @returns {void}
  432. */
  433. notifyEmailChanged(
  434. id: string,
  435. { email }: Object) {
  436. this._sendEvent({
  437. name: 'email-change',
  438. email,
  439. id
  440. });
  441. }
  442. /**
  443. * Notify external application (if API is enabled) that the conference has
  444. * been joined.
  445. *
  446. * @param {string} roomName - The room name.
  447. * @param {string} id - The id of the local user.
  448. * @param {Object} props - The display name and avatar URL of the local
  449. * user.
  450. * @returns {void}
  451. */
  452. notifyConferenceJoined(roomName: string, id: string, props: Object) {
  453. this._sendEvent({
  454. name: 'video-conference-joined',
  455. roomName,
  456. id,
  457. ...props
  458. });
  459. }
  460. /**
  461. * Notify external application (if API is enabled) that user changed their
  462. * nickname.
  463. *
  464. * @param {string} roomName - User id.
  465. * @returns {void}
  466. */
  467. notifyConferenceLeft(roomName: string) {
  468. this._sendEvent({
  469. name: 'video-conference-left',
  470. roomName
  471. });
  472. }
  473. /**
  474. * Notify external application (if API is enabled) that we are ready to be
  475. * closed.
  476. *
  477. * @returns {void}
  478. */
  479. notifyReadyToClose() {
  480. this._sendEvent({ name: 'video-ready-to-close' });
  481. }
  482. /**
  483. * Notify external application (if API is enabled) that a suspend event in host computer.
  484. *
  485. * @returns {void}
  486. */
  487. notifySuspendDetected() {
  488. this._sendEvent({ name: 'suspend-detected' });
  489. }
  490. /**
  491. * Notify external application (if API is enabled) for audio muted status
  492. * changed.
  493. *
  494. * @param {boolean} muted - The new muted status.
  495. * @returns {void}
  496. */
  497. notifyAudioMutedStatusChanged(muted: boolean) {
  498. this._sendEvent({
  499. name: 'audio-mute-status-changed',
  500. muted
  501. });
  502. }
  503. /**
  504. * Notify external application (if API is enabled) for video muted status
  505. * changed.
  506. *
  507. * @param {boolean} muted - The new muted status.
  508. * @returns {void}
  509. */
  510. notifyVideoMutedStatusChanged(muted: boolean) {
  511. this._sendEvent({
  512. name: 'video-mute-status-changed',
  513. muted
  514. });
  515. }
  516. /**
  517. * Notify external application (if API is enabled) for audio availability
  518. * changed.
  519. *
  520. * @param {boolean} available - True if available and false otherwise.
  521. * @returns {void}
  522. */
  523. notifyAudioAvailabilityChanged(available: boolean) {
  524. audioAvailable = available;
  525. this._sendEvent({
  526. name: 'audio-availability-changed',
  527. available
  528. });
  529. }
  530. /**
  531. * Notify external application (if API is enabled) for video available
  532. * status changed.
  533. *
  534. * @param {boolean} available - True if available and false otherwise.
  535. * @returns {void}
  536. */
  537. notifyVideoAvailabilityChanged(available: boolean) {
  538. videoAvailable = available;
  539. this._sendEvent({
  540. name: 'video-availability-changed',
  541. available
  542. });
  543. }
  544. /**
  545. * Notify external application (if API is enabled) that the on stage
  546. * participant has changed.
  547. *
  548. * @param {string} id - User id of the new on stage participant.
  549. * @returns {void}
  550. */
  551. notifyOnStageParticipantChanged(id: string) {
  552. this._sendEvent({
  553. name: 'on-stage-participant-changed',
  554. id
  555. });
  556. }
  557. /**
  558. * Notify external application of an unexpected camera-related error having
  559. * occurred.
  560. *
  561. * @param {string} type - The type of the camera error.
  562. * @param {string} message - Additional information about the error.
  563. * @returns {void}
  564. */
  565. notifyOnCameraError(type: string, message: string) {
  566. this._sendEvent({
  567. name: 'camera-error',
  568. type,
  569. message
  570. });
  571. }
  572. /**
  573. * Notify external application of an unexpected mic-related error having
  574. * occurred.
  575. *
  576. * @param {string} type - The type of the mic error.
  577. * @param {string} message - Additional information about the error.
  578. * @returns {void}
  579. */
  580. notifyOnMicError(type: string, message: string) {
  581. this._sendEvent({
  582. name: 'mic-error',
  583. type,
  584. message
  585. });
  586. }
  587. /**
  588. * Notify external application (if API is enabled) that conference feedback
  589. * has been submitted. Intended to be used in conjunction with the
  590. * submit-feedback command to get notified if feedback was submitted.
  591. *
  592. * @param {string} error - A failure message, if any.
  593. * @returns {void}
  594. */
  595. notifyFeedbackSubmitted(error: string) {
  596. this._sendEvent({
  597. name: 'feedback-submitted',
  598. error
  599. });
  600. }
  601. /**
  602. * Notify external application (if API is enabled) that the feedback prompt
  603. * has been displayed.
  604. *
  605. * @returns {void}
  606. */
  607. notifyFeedbackPromptDisplayed() {
  608. this._sendEvent({ name: 'feedback-prompt-displayed' });
  609. }
  610. /**
  611. * Notify external application (if API is enabled) that the display
  612. * configuration of the filmstrip has been changed.
  613. *
  614. * @param {boolean} visible - Whether or not the filmstrip has been set to
  615. * be displayed or hidden.
  616. * @returns {void}
  617. */
  618. notifyFilmstripDisplayChanged(visible: boolean) {
  619. this._sendEvent({
  620. name: 'filmstrip-display-changed',
  621. visible
  622. });
  623. }
  624. /**
  625. * Notify external application of a participant, remote or local, being
  626. * removed from the conference by another participant.
  627. *
  628. * @param {string} kicked - The ID of the participant removed from the
  629. * conference.
  630. * @param {string} kicker - The ID of the participant that removed the
  631. * other participant.
  632. * @returns {void}
  633. */
  634. notifyKickedOut(kicked: Object, kicker: Object) {
  635. this._sendEvent({
  636. name: 'participant-kicked-out',
  637. kicked,
  638. kicker
  639. });
  640. }
  641. /**
  642. * Notify external application of the current meeting requiring a password
  643. * to join.
  644. *
  645. * @returns {void}
  646. */
  647. notifyOnPasswordRequired() {
  648. this._sendEvent({ name: 'password-required' });
  649. }
  650. /**
  651. * Notify external application (if API is enabled) that the screen sharing
  652. * has been turned on/off.
  653. *
  654. * @param {boolean} on - True if screen sharing is enabled.
  655. * @param {Object} details - Additional information about the screen
  656. * sharing.
  657. * @param {string} details.sourceType - Type of device or window the screen
  658. * share is capturing.
  659. * @returns {void}
  660. */
  661. notifyScreenSharingStatusChanged(on: boolean, details: Object) {
  662. this._sendEvent({
  663. name: 'screen-sharing-status-changed',
  664. on,
  665. details
  666. });
  667. }
  668. /**
  669. * Notify external application (if API is enabled) that the dominant speaker
  670. * has been turned on/off.
  671. *
  672. * @param {string} id - Id of the dominant participant.
  673. * @returns {void}
  674. */
  675. notifyDominantSpeakerChanged(id: string) {
  676. this._sendEvent({
  677. name: 'dominant-speaker-changed',
  678. id
  679. });
  680. }
  681. /**
  682. * Notify external application (if API is enabled) that the conference
  683. * changed their subject.
  684. *
  685. * @param {string} subject - Conference subject.
  686. * @returns {void}
  687. */
  688. notifySubjectChanged(subject: string) {
  689. this._sendEvent({
  690. name: 'subject-change',
  691. subject
  692. });
  693. }
  694. /**
  695. * Notify external application (if API is enabled) that tile view has been
  696. * entered or exited.
  697. *
  698. * @param {string} enabled - True if tile view is currently displayed, false
  699. * otherwise.
  700. * @returns {void}
  701. */
  702. notifyTileViewChanged(enabled: boolean) {
  703. this._sendEvent({
  704. name: 'tile-view-changed',
  705. enabled
  706. });
  707. }
  708. /**
  709. * Disposes the allocated resources.
  710. *
  711. * @returns {void}
  712. */
  713. dispose() {
  714. if (this._enabled) {
  715. this._enabled = false;
  716. APP.conference.removeListener(
  717. JitsiMeetConferenceEvents.DESKTOP_SHARING_ENABLED_CHANGED,
  718. onDesktopSharingEnabledChanged);
  719. }
  720. }
  721. }
  722. export default new API();