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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. "email": APP.conference.changeLocalEmail,
  49. "avatar-url": APP.conference.changeLocalAvatarUrl,
  50. "remote-control-event": event =>
  51. APP.remoteControl.onRemoteControlAPIEvent(event)
  52. };
  53. Object.keys(commands).forEach(function (key) {
  54. postis.listen(key, args => commands[key](...args));
  55. });
  56. }
  57. /**
  58. * Sends message to the external application.
  59. * @param message {object}
  60. * @param method {string}
  61. * @param params {object} the object that will be sent as JSON string
  62. */
  63. function sendMessage(message) {
  64. if(enabled) {
  65. postis.send(message);
  66. }
  67. }
  68. /**
  69. * Check whether the API should be enabled or not.
  70. * @returns {boolean}
  71. */
  72. function shouldBeEnabled () {
  73. return (typeof jitsi_meet_external_api_id === "number");
  74. }
  75. /**
  76. * Sends event object to the external application that has been subscribed
  77. * for that event.
  78. * @param name the name event
  79. * @param object data associated with the event
  80. */
  81. function triggerEvent (name, object) {
  82. if(enabled) {
  83. sendMessage({method: name, params: object});
  84. }
  85. }
  86. class API {
  87. /**
  88. * Constructs new instance
  89. * @constructor
  90. */
  91. constructor() { }
  92. /**
  93. * Initializes the APIConnector. Setups message event listeners that will
  94. * receive information from external applications that embed Jitsi Meet.
  95. * It also sends a message to the external application that APIConnector
  96. * is initialized.
  97. * @param options {object}
  98. * @param forceEnable {boolean} if true the module will be enabled.
  99. */
  100. init (options = {}) {
  101. if(!shouldBeEnabled() && !options.forceEnable)
  102. return;
  103. enabled = true;
  104. if(!postis) {
  105. this._initPostis();
  106. }
  107. }
  108. /**
  109. * initializes postis library.
  110. * @private
  111. */
  112. _initPostis() {
  113. let postisOptions = {
  114. window: target
  115. };
  116. if(typeof jitsi_meet_external_api_id === "number")
  117. postisOptions.scope
  118. = "jitsi_meet_external_api_" + jitsi_meet_external_api_id;
  119. postis = postisInit(postisOptions);
  120. initCommands();
  121. }
  122. /**
  123. * Notify external application (if API is enabled) that message was sent.
  124. * @param {string} body message body
  125. */
  126. notifySendingChatMessage (body) {
  127. triggerEvent("outgoing-message", {"message": body});
  128. }
  129. /**
  130. * Notify external application (if API is enabled) that
  131. * message was received.
  132. * @param {string} id user id
  133. * @param {string} nick user nickname
  134. * @param {string} body message body
  135. * @param {number} ts message creation timestamp
  136. */
  137. notifyReceivedChatMessage (id, nick, body, ts) {
  138. if (APP.conference.isLocalId(id)) {
  139. return;
  140. }
  141. triggerEvent(
  142. "incoming-message",
  143. {"from": id, "nick": nick, "message": body, "stamp": ts}
  144. );
  145. }
  146. /**
  147. * Notify external application (if API is enabled) that
  148. * user joined the conference.
  149. * @param {string} id user id
  150. */
  151. notifyUserJoined (id) {
  152. triggerEvent("participant-joined", {id});
  153. }
  154. /**
  155. * Notify external application (if API is enabled) that
  156. * user left the conference.
  157. * @param {string} id user id
  158. */
  159. notifyUserLeft (id) {
  160. triggerEvent("participant-left", {id});
  161. }
  162. /**
  163. * Notify external application (if API is enabled) that
  164. * user changed their nickname.
  165. * @param {string} id user id
  166. * @param {string} displayName user nickname
  167. */
  168. notifyDisplayNameChanged (id, displayName) {
  169. triggerEvent("display-name-change", {id, displayname: displayName});
  170. }
  171. /**
  172. * Notify external application (if API is enabled) that
  173. * user changed their nickname.
  174. * @param {string} id user id
  175. * @param {string} displayName user nickname
  176. */
  177. notifyConferenceJoined (room) {
  178. triggerEvent("video-conference-joined", {roomName: room});
  179. }
  180. /**
  181. * Notify external application (if API is enabled) that
  182. * user changed their nickname.
  183. * @param {string} id user id
  184. * @param {string} displayName user nickname
  185. */
  186. notifyConferenceLeft (room) {
  187. triggerEvent("video-conference-left", {roomName: room});
  188. }
  189. /**
  190. * Notify external application (if API is enabled) that
  191. * we are ready to be closed.
  192. */
  193. notifyReadyToClose () {
  194. triggerEvent("video-ready-to-close", {});
  195. }
  196. /**
  197. * Sends remote control event.
  198. * @param {RemoteControlEvent} event the remote control event.
  199. */
  200. sendRemoteControlEvent(event) {
  201. sendMessage({method: "remote-control-event", params: event});
  202. }
  203. /**
  204. * Removes the listeners.
  205. */
  206. dispose () {
  207. if(enabled)
  208. postis.destroy();
  209. }
  210. }
  211. export default new API();