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

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