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

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