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

API.js 6.4KB

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