選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

API.js 22KB

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