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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. /**
  9. * List of the available commands.
  10. * @type {{
  11. * displayName: inputDisplayNameHandler,
  12. * toggleAudio: toggleAudio,
  13. * toggleVideo: toggleVideo,
  14. * toggleFilmStrip: toggleFilmStrip,
  15. * toggleChat: toggleChat,
  16. * toggleContactList: toggleContactList
  17. * }}
  18. */
  19. let commands = {};
  20. let hashParams = getConfigParamsFromUrl();
  21. /**
  22. * JitsiMeetExternalAPI id - unique for a webpage.
  23. */
  24. let jitsi_meet_external_api_id = hashParams.jitsi_meet_external_api_id;
  25. /**
  26. * Object that will execute sendMessage
  27. */
  28. let target = window.opener ? window.opener : window.parent;
  29. /**
  30. * Postis instance. Used to communicate with the external application.
  31. */
  32. let postis;
  33. /**
  34. * Current status (enabled/disabled) of API.
  35. */
  36. let enabled = false;
  37. function initCommands() {
  38. commands = {
  39. "display-name": APP.UI.inputDisplayNameHandler,
  40. "toggle-audio": APP.conference.toggleAudioMuted.bind(APP.conference),
  41. "toggle-video": APP.conference.toggleVideoMuted.bind(APP.conference),
  42. "toggle-film-strip": APP.UI.toggleFilmStrip,
  43. "toggle-chat": APP.UI.toggleChat,
  44. "toggle-contact-list": APP.UI.toggleContactList,
  45. "toggle-share-screen":
  46. APP.conference.toggleScreenSharing.bind(APP.conference),
  47. "video-hangup": () => APP.conference.hangup()
  48. };
  49. Object.keys(commands).forEach(function (key) {
  50. postis.listen(key, commands[key]);
  51. });
  52. }
  53. /**
  54. * Maps the supported events and their status
  55. * (true it the event is enabled and false if it is disabled)
  56. * @type {{
  57. * incoming-message: boolean,
  58. * outgoing-message: boolean,
  59. * display-name-change: boolean,
  60. * participant-left: boolean,
  61. * participant-joined: boolean,
  62. * video-conference-left: boolean,
  63. * video-conference-joined: boolean
  64. * }}
  65. */
  66. const events = {
  67. "incoming-message": false,
  68. "outgoing-message":false,
  69. "display-name-change": false,
  70. "participant-joined": false,
  71. "participant-left": false,
  72. "video-conference-joined": false,
  73. "video-conference-left": false,
  74. "video-ready-to-close": false
  75. };
  76. /**
  77. * Sends message to the external application.
  78. * @param message {object}
  79. * @param method {string}
  80. * @param params {object} the object that will be sent as JSON string
  81. */
  82. function sendMessage(message) {
  83. if(enabled)
  84. postis.send(message);
  85. }
  86. /**
  87. * Check whether the API should be enabled or not.
  88. * @returns {boolean}
  89. */
  90. function isEnabled () {
  91. return (typeof jitsi_meet_external_api_id === "number");
  92. }
  93. /**
  94. * Checks whether the event is enabled ot not.
  95. * @param name the name of the event.
  96. * @returns {*}
  97. */
  98. function isEventEnabled (name) {
  99. return events[name];
  100. }
  101. /**
  102. * Sends event object to the external application that has been subscribed
  103. * for that event.
  104. * @param name the name event
  105. * @param object data associated with the event
  106. */
  107. function triggerEvent (name, object) {
  108. if(isEventEnabled(name))
  109. sendMessage({method: name, params: object});
  110. }
  111. /**
  112. * Handles system messages. (for example: enable/disable events)
  113. * @param message {object} the message
  114. */
  115. function onSystemMessage(message) {
  116. switch (message.type) {
  117. case "eventStatus":
  118. if(!message.name || !message.value) {
  119. console.warn("Unknown system message format", message);
  120. break;
  121. }
  122. events[message.name] = message.value;
  123. break;
  124. default:
  125. console.warn("Unknown system message type", message);
  126. }
  127. }
  128. export default {
  129. /**
  130. * Initializes the APIConnector. Setups message event listeners that will
  131. * receive information from external applications that embed Jitsi Meet.
  132. * It also sends a message to the external application that APIConnector
  133. * is initialized.
  134. * @param options {object}
  135. * @param forceEnable {boolean} if true the module will be enabled.
  136. * @param enabledEvents {array} array of events that should be enabled.
  137. */
  138. init (options = {}) {
  139. if(!isEnabled() && !options.forceEnable)
  140. return;
  141. enabled = true;
  142. if(options.enabledEvents)
  143. options.enabledEvents.forEach(function (eventName) {
  144. events[eventName] = true;
  145. });
  146. let postisOptions = {
  147. window: target
  148. };
  149. if(typeof jitsi_meet_external_api_id === "number")
  150. postisOptions.scope
  151. = "jitsi_meet_external_api_" + jitsi_meet_external_api_id;
  152. postis = postisInit(postisOptions);
  153. postis.listen("jitsiSystemMessage", onSystemMessage);
  154. initCommands();
  155. },
  156. /**
  157. * Notify external application (if API is enabled) that message was sent.
  158. * @param {string} body message body
  159. */
  160. notifySendingChatMessage (body) {
  161. triggerEvent("outgoing-message", {"message": body});
  162. },
  163. /**
  164. * Notify external application (if API is enabled) that
  165. * message was received.
  166. * @param {string} id user id
  167. * @param {string} nick user nickname
  168. * @param {string} body message body
  169. * @param {number} ts message creation timestamp
  170. */
  171. notifyReceivedChatMessage (id, nick, body, ts) {
  172. if (APP.conference.isLocalId(id)) {
  173. return;
  174. }
  175. triggerEvent(
  176. "incoming-message",
  177. {"from": id, "nick": nick, "message": body, "stamp": ts}
  178. );
  179. },
  180. /**
  181. * Notify external application (if API is enabled) that
  182. * user joined the conference.
  183. * @param {string} id user id
  184. */
  185. notifyUserJoined (id) {
  186. triggerEvent("participant-joined", {id});
  187. },
  188. /**
  189. * Notify external application (if API is enabled) that
  190. * user left the conference.
  191. * @param {string} id user id
  192. */
  193. notifyUserLeft (id) {
  194. triggerEvent("participant-left", {id});
  195. },
  196. /**
  197. * Notify external application (if API is enabled) that
  198. * user changed their nickname.
  199. * @param {string} id user id
  200. * @param {string} displayName user nickname
  201. */
  202. notifyDisplayNameChanged (id, displayName) {
  203. triggerEvent("display-name-change", {id, displayname: displayName});
  204. },
  205. /**
  206. * Notify external application (if API is enabled) that
  207. * user changed their nickname.
  208. * @param {string} id user id
  209. * @param {string} displayName user nickname
  210. */
  211. notifyConferenceJoined (room) {
  212. triggerEvent("video-conference-joined", {roomName: room});
  213. },
  214. /**
  215. * Notify external application (if API is enabled) that
  216. * user changed their nickname.
  217. * @param {string} id user id
  218. * @param {string} displayName user nickname
  219. */
  220. notifyConferenceLeft (room) {
  221. triggerEvent("video-conference-left", {roomName: room});
  222. },
  223. /**
  224. * Notify external application (if API is enabled) that
  225. * we are ready to be closed.
  226. */
  227. notifyReadyToClose () {
  228. triggerEvent("video-ready-to-close", {});
  229. },
  230. /**
  231. * Removes the listeners.
  232. */
  233. dispose: function () {
  234. if(enabled)
  235. postis.destroy();
  236. }
  237. };