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.0KB

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