您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770
  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) that a suspend event in host computer.
  463. *
  464. * @returns {void}
  465. */
  466. notifySuspendDetected() {
  467. this._sendEvent({ name: 'suspend-detected' });
  468. }
  469. /**
  470. * Notify external application (if API is enabled) for audio muted status
  471. * changed.
  472. *
  473. * @param {boolean} muted - The new muted status.
  474. * @returns {void}
  475. */
  476. notifyAudioMutedStatusChanged(muted: boolean) {
  477. this._sendEvent({
  478. name: 'audio-mute-status-changed',
  479. muted
  480. });
  481. }
  482. /**
  483. * Notify external application (if API is enabled) for video muted status
  484. * changed.
  485. *
  486. * @param {boolean} muted - The new muted status.
  487. * @returns {void}
  488. */
  489. notifyVideoMutedStatusChanged(muted: boolean) {
  490. this._sendEvent({
  491. name: 'video-mute-status-changed',
  492. muted
  493. });
  494. }
  495. /**
  496. * Notify external application (if API is enabled) for audio availability
  497. * changed.
  498. *
  499. * @param {boolean} available - True if available and false otherwise.
  500. * @returns {void}
  501. */
  502. notifyAudioAvailabilityChanged(available: boolean) {
  503. audioAvailable = available;
  504. this._sendEvent({
  505. name: 'audio-availability-changed',
  506. available
  507. });
  508. }
  509. /**
  510. * Notify external application (if API is enabled) for video available
  511. * status changed.
  512. *
  513. * @param {boolean} available - True if available and false otherwise.
  514. * @returns {void}
  515. */
  516. notifyVideoAvailabilityChanged(available: boolean) {
  517. videoAvailable = available;
  518. this._sendEvent({
  519. name: 'video-availability-changed',
  520. available
  521. });
  522. }
  523. /**
  524. * Notify external application (if API is enabled) that the on stage
  525. * participant has changed.
  526. *
  527. * @param {string} id - User id of the new on stage participant.
  528. * @returns {void}
  529. */
  530. notifyOnStageParticipantChanged(id: string) {
  531. this._sendEvent({
  532. name: 'on-stage-participant-changed',
  533. id
  534. });
  535. }
  536. /**
  537. * Notify external application of an unexpected camera-related error having
  538. * occurred.
  539. *
  540. * @param {string} type - The type of the camera error.
  541. * @param {string} message - Additional information about the error.
  542. * @returns {void}
  543. */
  544. notifyOnCameraError(type: string, message: string) {
  545. this._sendEvent({
  546. name: 'camera-error',
  547. type,
  548. message
  549. });
  550. }
  551. /**
  552. * Notify external application of an unexpected mic-related error having
  553. * occurred.
  554. *
  555. * @param {string} type - The type of the mic error.
  556. * @param {string} message - Additional information about the error.
  557. * @returns {void}
  558. */
  559. notifyOnMicError(type: string, message: string) {
  560. this._sendEvent({
  561. name: 'mic-error',
  562. type,
  563. message
  564. });
  565. }
  566. /**
  567. * Notify external application (if API is enabled) that conference feedback
  568. * has been submitted. Intended to be used in conjunction with the
  569. * submit-feedback command to get notified if feedback was submitted.
  570. *
  571. * @param {string} error - A failure message, if any.
  572. * @returns {void}
  573. */
  574. notifyFeedbackSubmitted(error: string) {
  575. this._sendEvent({
  576. name: 'feedback-submitted',
  577. error
  578. });
  579. }
  580. /**
  581. * Notify external application (if API is enabled) that the feedback prompt
  582. * has been displayed.
  583. *
  584. * @returns {void}
  585. */
  586. notifyFeedbackPromptDisplayed() {
  587. this._sendEvent({ name: 'feedback-prompt-displayed' });
  588. }
  589. /**
  590. * Notify external application (if API is enabled) that the display
  591. * configuration of the filmstrip has been changed.
  592. *
  593. * @param {boolean} visible - Whether or not the filmstrip has been set to
  594. * be displayed or hidden.
  595. * @returns {void}
  596. */
  597. notifyFilmstripDisplayChanged(visible: boolean) {
  598. this._sendEvent({
  599. name: 'filmstrip-display-changed',
  600. visible
  601. });
  602. }
  603. /**
  604. * Notify external application of a participant, remote or local, being
  605. * removed from the conference by another participant.
  606. *
  607. * @param {string} kicked - The ID of the participant removed from the
  608. * conference.
  609. * @param {string} kicker - The ID of the participant that removed the
  610. * other participant.
  611. * @returns {void}
  612. */
  613. notifyKickedOut(kicked: Object, kicker: Object) {
  614. this._sendEvent({
  615. name: 'participant-kicked-out',
  616. kicked,
  617. kicker
  618. });
  619. }
  620. /**
  621. * Notify external application of the current meeting requiring a password
  622. * to join.
  623. *
  624. * @returns {void}
  625. */
  626. notifyOnPasswordRequired() {
  627. this._sendEvent({ name: 'password-required' });
  628. }
  629. /**
  630. * Notify external application (if API is enabled) that the screen sharing
  631. * has been turned on/off.
  632. *
  633. * @param {boolean} on - True if screen sharing is enabled.
  634. * @param {Object} details - Additional information about the screen
  635. * sharing.
  636. * @param {string} details.sourceType - Type of device or window the screen
  637. * share is capturing.
  638. * @returns {void}
  639. */
  640. notifyScreenSharingStatusChanged(on: boolean, details: Object) {
  641. this._sendEvent({
  642. name: 'screen-sharing-status-changed',
  643. on,
  644. details
  645. });
  646. }
  647. /**
  648. * Notify external application (if API is enabled) that the dominant speaker
  649. * has been turned on/off.
  650. *
  651. * @param {string} id - Id of the dominant participant.
  652. * @returns {void}
  653. */
  654. notifyDominantSpeakerChanged(id: string) {
  655. this._sendEvent({
  656. name: 'dominant-speaker-changed',
  657. id
  658. });
  659. }
  660. /**
  661. * Notify external application (if API is enabled) that the conference
  662. * changed their subject.
  663. *
  664. * @param {string} subject - Conference subject.
  665. * @returns {void}
  666. */
  667. notifySubjectChanged(subject: string) {
  668. this._sendEvent({
  669. name: 'subject-change',
  670. subject
  671. });
  672. }
  673. /**
  674. * Notify external application (if API is enabled) that tile view has been
  675. * entered or exited.
  676. *
  677. * @param {string} enabled - True if tile view is currently displayed, false
  678. * otherwise.
  679. * @returns {void}
  680. */
  681. notifyTileViewChanged(enabled: boolean) {
  682. this._sendEvent({
  683. name: 'tile-view-changed',
  684. enabled
  685. });
  686. }
  687. /**
  688. * Disposes the allocated resources.
  689. *
  690. * @returns {void}
  691. */
  692. dispose() {
  693. if (this._enabled) {
  694. this._enabled = false;
  695. APP.conference.removeListener(
  696. JitsiMeetConferenceEvents.DESKTOP_SHARING_ENABLED_CHANGED,
  697. onDesktopSharingEnabledChanged);
  698. }
  699. }
  700. }
  701. export default new API();