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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  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': {
  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. }
  129. case 'is-audio-muted':
  130. callback(APP.conference.isLocalAudioMuted());
  131. break;
  132. case 'is-video-muted':
  133. callback(APP.conference.isLocalVideoMuted());
  134. break;
  135. case 'is-audio-available':
  136. callback(audioAvailable);
  137. break;
  138. case 'is-video-available':
  139. callback(videoAvailable);
  140. break;
  141. default:
  142. return false;
  143. }
  144. return true;
  145. });
  146. }
  147. /**
  148. * Listens for desktop/screen sharing enabled events and toggles the screen
  149. * sharing if needed.
  150. *
  151. * @param {boolean} enabled - Current screen sharing enabled status.
  152. * @returns {void}
  153. */
  154. function onDesktopSharingEnabledChanged(enabled = false) {
  155. if (enabled && initialScreenSharingState) {
  156. toggleScreenSharing();
  157. }
  158. }
  159. /**
  160. * Check whether the API should be enabled or not.
  161. *
  162. * @returns {boolean}
  163. */
  164. function shouldBeEnabled() {
  165. return (
  166. typeof API_ID === 'number'
  167. // XXX Enable the API when a JSON Web Token (JWT) is specified in
  168. // the location/URL because then it is very likely that the Jitsi
  169. // Meet (Web) app is being used by an external/wrapping (Web) app
  170. // and, consequently, the latter will need to communicate with the
  171. // former. (The described logic is merely a heuristic though.)
  172. || parseJWTFromURLParams());
  173. }
  174. /**
  175. * Executes on toggle-share-screen command.
  176. *
  177. * @returns {void}
  178. */
  179. function toggleScreenSharing() {
  180. if (APP.conference.isDesktopSharingEnabled) {
  181. // eslint-disable-next-line no-empty-function
  182. APP.conference.toggleScreenSharing().catch(() => {});
  183. } else {
  184. initialScreenSharingState = !initialScreenSharingState;
  185. }
  186. }
  187. /**
  188. * Implements API class that communicates with external API class and provides
  189. * interface to access Jitsi Meet features by external applications that embed
  190. * Jitsi Meet.
  191. */
  192. class API {
  193. _enabled: boolean;
  194. /**
  195. * Initializes the API. Setups message event listeners that will receive
  196. * information from external applications that embed Jitsi Meet. It also
  197. * sends a message to the external application that API is initialized.
  198. *
  199. * @param {Object} options - Optional parameters.
  200. * @returns {void}
  201. */
  202. init() {
  203. if (!shouldBeEnabled()) {
  204. return;
  205. }
  206. /**
  207. * Current status (enabled/disabled) of API.
  208. *
  209. * @private
  210. * @type {boolean}
  211. */
  212. this._enabled = true;
  213. APP.conference.addListener(
  214. JitsiMeetConferenceEvents.DESKTOP_SHARING_ENABLED_CHANGED,
  215. onDesktopSharingEnabledChanged);
  216. initCommands();
  217. }
  218. /**
  219. * Notify external application (if API is enabled) that the large video
  220. * visibility changed.
  221. *
  222. * @param {boolean} isHidden - True if the large video is hidden and false
  223. * otherwise.
  224. * @returns {void}
  225. */
  226. notifyLargeVideoVisibilityChanged(isHidden: boolean) {
  227. this._sendEvent({
  228. name: 'large-video-visibility-changed',
  229. isVisible: !isHidden
  230. });
  231. }
  232. /**
  233. * Sends event to the external application.
  234. *
  235. * @param {Object} event - The event to be sent.
  236. * @returns {void}
  237. */
  238. _sendEvent(event: Object = {}) {
  239. if (this._enabled) {
  240. transport.sendEvent(event);
  241. }
  242. }
  243. /**
  244. * Notify external application (if API is enabled) that message was sent.
  245. *
  246. * @param {string} message - Message body.
  247. * @returns {void}
  248. */
  249. notifySendingChatMessage(message: string) {
  250. this._sendEvent({
  251. name: 'outgoing-message',
  252. message
  253. });
  254. }
  255. /**
  256. * Notify external application (if API is enabled) that message was
  257. * received.
  258. *
  259. * @param {Object} options - Object with the message properties.
  260. * @returns {void}
  261. */
  262. notifyReceivedChatMessage(
  263. { body, id, nick, ts }: {
  264. body: *, id: string, nick: string, ts: *
  265. } = {}) {
  266. if (APP.conference.isLocalId(id)) {
  267. return;
  268. }
  269. this._sendEvent({
  270. name: 'incoming-message',
  271. from: id,
  272. message: body,
  273. nick,
  274. stamp: ts
  275. });
  276. }
  277. /**
  278. * Notify external application (if API is enabled) that user joined the
  279. * conference.
  280. *
  281. * @param {string} id - User id.
  282. * @param {Object} props - The display name of the user.
  283. * @returns {void}
  284. */
  285. notifyUserJoined(id: string, props: Object) {
  286. this._sendEvent({
  287. name: 'participant-joined',
  288. id,
  289. ...props
  290. });
  291. }
  292. /**
  293. * Notify external application (if API is enabled) that user left the
  294. * conference.
  295. *
  296. * @param {string} id - User id.
  297. * @returns {void}
  298. */
  299. notifyUserLeft(id: string) {
  300. this._sendEvent({
  301. name: 'participant-left',
  302. id
  303. });
  304. }
  305. /**
  306. * Notify external application (if API is enabled) that user changed their
  307. * avatar.
  308. *
  309. * @param {string} id - User id.
  310. * @param {string} avatarURL - The new avatar URL of the participant.
  311. * @returns {void}
  312. */
  313. notifyAvatarChanged(id: string, avatarURL: string) {
  314. this._sendEvent({
  315. name: 'avatar-changed',
  316. avatarURL,
  317. id
  318. });
  319. }
  320. /**
  321. * Notify external application (if API is enabled) that user changed their
  322. * nickname.
  323. *
  324. * @param {string} id - User id.
  325. * @param {string} displayname - User nickname.
  326. * @param {string} formattedDisplayName - The display name shown in Jitsi
  327. * meet's UI for the user.
  328. * @returns {void}
  329. */
  330. notifyDisplayNameChanged(
  331. id: string,
  332. { displayName, formattedDisplayName }: Object) {
  333. this._sendEvent({
  334. name: 'display-name-change',
  335. displayname: displayName,
  336. formattedDisplayName,
  337. id
  338. });
  339. }
  340. /**
  341. * Notify external application (if API is enabled) that user changed their
  342. * email.
  343. *
  344. * @param {string} id - User id.
  345. * @param {string} email - The new email of the participant.
  346. * @returns {void}
  347. */
  348. notifyEmailChanged(
  349. id: string,
  350. { email }: Object) {
  351. this._sendEvent({
  352. name: 'email-change',
  353. email,
  354. id
  355. });
  356. }
  357. /**
  358. * Notify external application (if API is enabled) that the conference has
  359. * been joined.
  360. *
  361. * @param {string} roomName - The room name.
  362. * @param {string} id - The id of the local user.
  363. * @param {Object} props - The display name and avatar URL of the local
  364. * user.
  365. * @returns {void}
  366. */
  367. notifyConferenceJoined(roomName: string, id: string, props: Object) {
  368. this._sendEvent({
  369. name: 'video-conference-joined',
  370. roomName,
  371. id,
  372. ...props
  373. });
  374. }
  375. /**
  376. * Notify external application (if API is enabled) that user changed their
  377. * nickname.
  378. *
  379. * @param {string} roomName - User id.
  380. * @returns {void}
  381. */
  382. notifyConferenceLeft(roomName: string) {
  383. this._sendEvent({
  384. name: 'video-conference-left',
  385. roomName
  386. });
  387. }
  388. /**
  389. * Notify external application (if API is enabled) that we are ready to be
  390. * closed.
  391. *
  392. * @returns {void}
  393. */
  394. notifyReadyToClose() {
  395. this._sendEvent({ name: 'video-ready-to-close' });
  396. }
  397. /**
  398. * Notify external application (if API is enabled) for audio muted status
  399. * changed.
  400. *
  401. * @param {boolean} muted - The new muted status.
  402. * @returns {void}
  403. */
  404. notifyAudioMutedStatusChanged(muted: boolean) {
  405. this._sendEvent({
  406. name: 'audio-mute-status-changed',
  407. muted
  408. });
  409. }
  410. /**
  411. * Notify external application (if API is enabled) for video muted status
  412. * changed.
  413. *
  414. * @param {boolean} muted - The new muted status.
  415. * @returns {void}
  416. */
  417. notifyVideoMutedStatusChanged(muted: boolean) {
  418. this._sendEvent({
  419. name: 'video-mute-status-changed',
  420. muted
  421. });
  422. }
  423. /**
  424. * Notify external application (if API is enabled) for audio availability
  425. * changed.
  426. *
  427. * @param {boolean} available - True if available and false otherwise.
  428. * @returns {void}
  429. */
  430. notifyAudioAvailabilityChanged(available: boolean) {
  431. audioAvailable = available;
  432. this._sendEvent({
  433. name: 'audio-availability-changed',
  434. available
  435. });
  436. }
  437. /**
  438. * Notify external application (if API is enabled) for video available
  439. * status changed.
  440. *
  441. * @param {boolean} available - True if available and false otherwise.
  442. * @returns {void}
  443. */
  444. notifyVideoAvailabilityChanged(available: boolean) {
  445. videoAvailable = available;
  446. this._sendEvent({
  447. name: 'video-availability-changed',
  448. available
  449. });
  450. }
  451. /**
  452. * Notify external application (if API is enabled) that the on stage
  453. * participant has changed.
  454. *
  455. * @param {string} id - User id of the new on stage participant.
  456. * @returns {void}
  457. */
  458. notifyOnStageParticipantChanged(id: string) {
  459. this._sendEvent({
  460. name: 'on-stage-participant-changed',
  461. id
  462. });
  463. }
  464. /**
  465. * Notify external application (if API is enabled) that conference feedback
  466. * has been submitted. Intended to be used in conjunction with the
  467. * submit-feedback command to get notified if feedback was submitted.
  468. *
  469. * @returns {void}
  470. */
  471. notifyFeedbackSubmitted() {
  472. this._sendEvent({ name: 'feedback-submitted' });
  473. }
  474. /**
  475. * Notify external application (if API is enabled) that the screen sharing
  476. * has been turned on/off.
  477. *
  478. * @param {boolean} on - True if screen sharing is enabled.
  479. * @returns {void}
  480. */
  481. notifyScreenSharingStatusChanged(on: boolean) {
  482. this._sendEvent({
  483. name: 'screen-sharing-status-changed',
  484. on
  485. });
  486. }
  487. /**
  488. * Disposes the allocated resources.
  489. *
  490. * @returns {void}
  491. */
  492. dispose() {
  493. if (this._enabled) {
  494. this._enabled = false;
  495. APP.conference.removeListener(
  496. JitsiMeetConferenceEvents.DESKTOP_SHARING_ENABLED_CHANGED,
  497. onDesktopSharingEnabledChanged);
  498. }
  499. }
  500. }
  501. export default new API();