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

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