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

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