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

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