Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

API.js 17KB

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