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

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