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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. // @flow
  2. import * as JitsiMeetConferenceEvents from '../../ConferenceEvents';
  3. import { parseJWTFromURLParams } from '../../react/features/base/jwt';
  4. import { getJitsiMeetTransport } from '../transport';
  5. import { API_ID } from './constants';
  6. declare var APP: Object;
  7. /**
  8. * List of the available commands.
  9. */
  10. let commands = {};
  11. /**
  12. * The state of screen sharing(started/stopped) before the screen sharing is
  13. * enabled and initialized.
  14. * NOTE: This flag help us to cache the state and use it if toggle-share-screen
  15. * was received before the initialization.
  16. */
  17. let initialScreenSharingState = false;
  18. /**
  19. * The transport instance used for communication with external apps.
  20. *
  21. * @type {Transport}
  22. */
  23. const transport = getJitsiMeetTransport();
  24. /**
  25. * The current audio availability.
  26. *
  27. * @type {boolean}
  28. */
  29. let audioAvailable = true;
  30. /**
  31. * The current video availability.
  32. *
  33. * @type {boolean}
  34. */
  35. let videoAvailable = true;
  36. /**
  37. * Initializes supported commands.
  38. *
  39. * @returns {void}
  40. */
  41. function initCommands() {
  42. commands = {
  43. 'display-name':
  44. APP.conference.changeLocalDisplayName.bind(APP.conference),
  45. 'toggle-audio': () => {
  46. APP.conference.toggleAudioMuted(false /* no UI */);
  47. },
  48. 'toggle-video': () => {
  49. APP.conference.toggleVideoMuted(false /* no UI */);
  50. },
  51. 'toggle-film-strip': APP.UI.toggleFilmstrip,
  52. 'toggle-chat': APP.UI.toggleChat,
  53. 'toggle-contact-list': APP.UI.toggleContactList,
  54. 'toggle-share-screen': toggleScreenSharing,
  55. 'video-hangup': () => APP.conference.hangup(),
  56. 'email': APP.conference.changeLocalEmail,
  57. 'avatar-url': APP.conference.changeLocalAvatarUrl
  58. };
  59. transport.on('event', ({ data, name }) => {
  60. if (name && commands[name]) {
  61. commands[name](...data);
  62. return true;
  63. }
  64. return false;
  65. });
  66. transport.on('request', ({ name }, callback) => {
  67. switch (name) {
  68. case 'is-audio-muted':
  69. callback(APP.conference.isLocalAudioMuted());
  70. break;
  71. case 'is-video-muted':
  72. callback(APP.conference.isLocalVideoMuted());
  73. break;
  74. case 'is-audio-available':
  75. callback(audioAvailable);
  76. break;
  77. case 'is-video-available':
  78. callback(videoAvailable);
  79. break;
  80. default:
  81. return false;
  82. }
  83. return true;
  84. });
  85. }
  86. /**
  87. * Listens for desktop/screen sharing enabled events and toggles the screen
  88. * sharing if needed.
  89. *
  90. * @param {boolean} enabled - Current screen sharing enabled status.
  91. * @returns {void}
  92. */
  93. function onDesktopSharingEnabledChanged(enabled = false) {
  94. if (enabled && initialScreenSharingState) {
  95. toggleScreenSharing();
  96. }
  97. }
  98. /**
  99. * Check whether the API should be enabled or not.
  100. *
  101. * @returns {boolean}
  102. */
  103. function shouldBeEnabled() {
  104. return (
  105. typeof API_ID === 'number'
  106. // XXX Enable the API when a JSON Web Token (JWT) is specified in
  107. // the location/URL because then it is very likely that the Jitsi
  108. // Meet (Web) app is being used by an external/wrapping (Web) app
  109. // and, consequently, the latter will need to communicate with the
  110. // former. (The described logic is merely a heuristic though.)
  111. || parseJWTFromURLParams());
  112. }
  113. /**
  114. * Executes on toggle-share-screen command.
  115. *
  116. * @returns {void}
  117. */
  118. function toggleScreenSharing() {
  119. if (APP.conference.isDesktopSharingEnabled) {
  120. // eslint-disable-next-line no-empty-function
  121. APP.conference.toggleScreenSharing().catch(() => {});
  122. } else {
  123. initialScreenSharingState = !initialScreenSharingState;
  124. }
  125. }
  126. /**
  127. * Implements API class that communicates with external API class and provides
  128. * interface to access Jitsi Meet features by external applications that embed
  129. * Jitsi Meet.
  130. */
  131. class API {
  132. _enabled: boolean;
  133. /**
  134. * Initializes the API. Setups message event listeners that will receive
  135. * information from external applications that embed Jitsi Meet. It also
  136. * sends a message to the external application that API is initialized.
  137. *
  138. * @param {Object} options - Optional parameters.
  139. * @returns {void}
  140. */
  141. init() {
  142. if (!shouldBeEnabled()) {
  143. return;
  144. }
  145. /**
  146. * Current status (enabled/disabled) of API.
  147. *
  148. * @private
  149. * @type {boolean}
  150. */
  151. this._enabled = true;
  152. APP.conference.addListener(
  153. JitsiMeetConferenceEvents.DESKTOP_SHARING_ENABLED_CHANGED,
  154. onDesktopSharingEnabledChanged);
  155. initCommands();
  156. }
  157. /**
  158. * Sends event to the external application.
  159. *
  160. * @param {Object} event - The event to be sent.
  161. * @returns {void}
  162. */
  163. _sendEvent(event: Object = {}) {
  164. if (this._enabled) {
  165. transport.sendEvent(event);
  166. }
  167. }
  168. /**
  169. * Notify external application (if API is enabled) that message was sent.
  170. *
  171. * @param {string} message - Message body.
  172. * @returns {void}
  173. */
  174. notifySendingChatMessage(message: string) {
  175. this._sendEvent({
  176. name: 'outgoing-message',
  177. message
  178. });
  179. }
  180. /**
  181. * Notify external application (if API is enabled) that message was
  182. * received.
  183. *
  184. * @param {Object} options - Object with the message properties.
  185. * @returns {void}
  186. */
  187. notifyReceivedChatMessage(
  188. { body, id, nick, ts }: {
  189. body: *, id: string, nick: string, ts: *
  190. } = {}) {
  191. if (APP.conference.isLocalId(id)) {
  192. return;
  193. }
  194. this._sendEvent({
  195. name: 'incoming-message',
  196. from: id,
  197. message: body,
  198. nick,
  199. stamp: ts
  200. });
  201. }
  202. /**
  203. * Notify external application (if API is enabled) that user joined the
  204. * conference.
  205. *
  206. * @param {string} id - User id.
  207. * @returns {void}
  208. */
  209. notifyUserJoined(id: string) {
  210. this._sendEvent({
  211. name: 'participant-joined',
  212. id
  213. });
  214. }
  215. /**
  216. * Notify external application (if API is enabled) that user left the
  217. * conference.
  218. *
  219. * @param {string} id - User id.
  220. * @returns {void}
  221. */
  222. notifyUserLeft(id: string) {
  223. this._sendEvent({
  224. name: 'participant-left',
  225. id
  226. });
  227. }
  228. /**
  229. * Notify external application (if API is enabled) that user changed their
  230. * nickname.
  231. *
  232. * @param {string} id - User id.
  233. * @param {string} displayname - User nickname.
  234. * @returns {void}
  235. */
  236. notifyDisplayNameChanged(id: string, displayname: string) {
  237. this._sendEvent({
  238. name: 'display-name-change',
  239. displayname,
  240. id
  241. });
  242. }
  243. /**
  244. * Notify external application (if API is enabled) that the conference has
  245. * been joined.
  246. *
  247. * @param {string} roomName - The room name.
  248. * @returns {void}
  249. */
  250. notifyConferenceJoined(roomName: string) {
  251. this._sendEvent({
  252. name: 'video-conference-joined',
  253. roomName
  254. });
  255. }
  256. /**
  257. * Notify external application (if API is enabled) that user changed their
  258. * nickname.
  259. *
  260. * @param {string} roomName - User id.
  261. * @returns {void}
  262. */
  263. notifyConferenceLeft(roomName: string) {
  264. this._sendEvent({
  265. name: 'video-conference-left',
  266. roomName
  267. });
  268. }
  269. /**
  270. * Notify external application (if API is enabled) that we are ready to be
  271. * closed.
  272. *
  273. * @returns {void}
  274. */
  275. notifyReadyToClose() {
  276. this._sendEvent({ name: 'video-ready-to-close' });
  277. }
  278. /**
  279. * Notify external application (if API is enabled) for audio muted status
  280. * changed.
  281. *
  282. * @param {boolean} muted - The new muted status.
  283. * @returns {void}
  284. */
  285. notifyAudioMutedStatusChanged(muted: boolean) {
  286. this._sendEvent({
  287. name: 'audio-mute-status-changed',
  288. muted
  289. });
  290. }
  291. /**
  292. * Notify external application (if API is enabled) for video muted status
  293. * changed.
  294. *
  295. * @param {boolean} muted - The new muted status.
  296. * @returns {void}
  297. */
  298. notifyVideoMutedStatusChanged(muted: boolean) {
  299. this._sendEvent({
  300. name: 'video-mute-status-changed',
  301. muted
  302. });
  303. }
  304. /**
  305. * Notify external application (if API is enabled) for audio availability
  306. * changed.
  307. *
  308. * @param {boolean} available - True if available and false otherwise.
  309. * @returns {void}
  310. */
  311. notifyAudioAvailabilityChanged(available: boolean) {
  312. audioAvailable = available;
  313. this._sendEvent({
  314. name: 'audio-availability-changed',
  315. available
  316. });
  317. }
  318. /**
  319. * Notify external application (if API is enabled) for video available
  320. * status changed.
  321. *
  322. * @param {boolean} available - True if available and false otherwise.
  323. * @returns {void}
  324. */
  325. notifyVideoAvailabilityChanged(available: boolean) {
  326. videoAvailable = available;
  327. this._sendEvent({
  328. name: 'video-availability-changed',
  329. available
  330. });
  331. }
  332. /**
  333. * Disposes the allocated resources.
  334. *
  335. * @returns {void}
  336. */
  337. dispose() {
  338. if (this._enabled) {
  339. this._enabled = false;
  340. APP.conference.removeListener(
  341. JitsiMeetConferenceEvents.DESKTOP_SHARING_ENABLED_CHANGED,
  342. onDesktopSharingEnabledChanged);
  343. }
  344. }
  345. }
  346. export default new API();