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.6KB

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