Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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