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

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