您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

API.js 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. };
  35. transport.on('event', ({ data, name }) => {
  36. if (name && commands[name]) {
  37. commands[name](...data);
  38. return true;
  39. }
  40. return false;
  41. });
  42. }
  43. /**
  44. * Listens for desktop/screen sharing enabled events and toggles the screen
  45. * sharing if needed.
  46. *
  47. * @param {boolean} enabled - Current screen sharing enabled status.
  48. * @returns {void}
  49. */
  50. function onDesktopSharingEnabledChanged(enabled = false) {
  51. if (enabled && initialScreenSharingState) {
  52. toggleScreenSharing();
  53. }
  54. }
  55. /**
  56. * Check whether the API should be enabled or not.
  57. *
  58. * @returns {boolean}
  59. */
  60. function shouldBeEnabled() {
  61. return typeof API_ID === 'number';
  62. }
  63. /**
  64. * Executes on toggle-share-screen command.
  65. *
  66. * @returns {void}
  67. */
  68. function toggleScreenSharing() {
  69. if (APP.conference.isDesktopSharingEnabled) {
  70. APP.conference.toggleScreenSharing();
  71. } else {
  72. initialScreenSharingState = !initialScreenSharingState;
  73. }
  74. }
  75. /**
  76. * Implements API class that communicates with external API class and provides
  77. * interface to access Jitsi Meet features by external applications that embed
  78. * Jitsi Meet.
  79. */
  80. class API {
  81. /**
  82. * Initializes the API. Setups message event listeners that will receive
  83. * information from external applications that embed Jitsi Meet. It also
  84. * sends a message to the external application that API is initialized.
  85. *
  86. * @param {Object} options - Optional parameters.
  87. * @param {boolean} options.forceEnable - True to forcefully enable the
  88. * module.
  89. * @returns {void}
  90. */
  91. init({ forceEnable } = {}) {
  92. if (!shouldBeEnabled() && !forceEnable) {
  93. return;
  94. }
  95. /**
  96. * Current status (enabled/disabled) of API.
  97. *
  98. * @private
  99. * @type {boolean}
  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. data,
  118. name
  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({ body, id, nick, ts } = {}) {
  139. if (APP.conference.isLocalId(id)) {
  140. return;
  141. }
  142. this._sendEvent('incoming-message', {
  143. from: id,
  144. message: body,
  145. nick,
  146. stamp: ts
  147. });
  148. }
  149. /**
  150. * Notify external application (if API is enabled) that user joined the
  151. * conference.
  152. *
  153. * @param {string} id - User id.
  154. * @returns {void}
  155. */
  156. notifyUserJoined(id) {
  157. this._sendEvent('participant-joined', { id });
  158. }
  159. /**
  160. * Notify external application (if API is enabled) that user left the
  161. * conference.
  162. *
  163. * @param {string} id - User id.
  164. * @returns {void}
  165. */
  166. notifyUserLeft(id) {
  167. this._sendEvent('participant-left', { id });
  168. }
  169. /**
  170. * Notify external application (if API is enabled) that user changed their
  171. * nickname.
  172. *
  173. * @param {string} id - User id.
  174. * @param {string} displayname - User nickname.
  175. * @returns {void}
  176. */
  177. notifyDisplayNameChanged(id, displayname) {
  178. this._sendEvent('display-name-change', {
  179. displayname,
  180. id
  181. });
  182. }
  183. /**
  184. * Notify external application (if API is enabled) that the conference has
  185. * been joined.
  186. *
  187. * @param {string} roomName - The room name.
  188. * @returns {void}
  189. */
  190. notifyConferenceJoined(roomName) {
  191. this._sendEvent('video-conference-joined', { roomName });
  192. }
  193. /**
  194. * Notify external application (if API is enabled) that user changed their
  195. * nickname.
  196. *
  197. * @param {string} roomName - User id.
  198. * @returns {void}
  199. */
  200. notifyConferenceLeft(roomName) {
  201. this._sendEvent('video-conference-left', { roomName });
  202. }
  203. /**
  204. * Notify external application (if API is enabled) that we are ready to be
  205. * closed.
  206. *
  207. * @returns {void}
  208. */
  209. notifyReadyToClose() {
  210. this._sendEvent('video-ready-to-close', {});
  211. }
  212. /**
  213. * Disposes the allocated resources.
  214. *
  215. * @returns {void}
  216. */
  217. dispose() {
  218. if (this._enabled) {
  219. this._enabled = false;
  220. APP.conference.removeListener(
  221. JitsiMeetConferenceEvents.DESKTOP_SHARING_ENABLED_CHANGED,
  222. onDesktopSharingEnabledChanged);
  223. }
  224. }
  225. }
  226. export default new API();