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

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