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

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