您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

API.js 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. }
  105. /**
  106. * initializes postis library.
  107. * @private
  108. */
  109. _initPostis() {
  110. let postisOptions = {
  111. window: target
  112. };
  113. if(typeof jitsi_meet_external_api_id === "number")
  114. postisOptions.scope
  115. = "jitsi_meet_external_api_" + jitsi_meet_external_api_id;
  116. postis = postisInit(postisOptions);
  117. initCommands();
  118. }
  119. /**
  120. * Notify external application (if API is enabled) that message was sent.
  121. * @param {string} body message body
  122. */
  123. notifySendingChatMessage (body) {
  124. triggerEvent("outgoing-message", {"message": body});
  125. }
  126. /**
  127. * Notify external application (if API is enabled) that
  128. * message was received.
  129. * @param {string} id user id
  130. * @param {string} nick user nickname
  131. * @param {string} body message body
  132. * @param {number} ts message creation timestamp
  133. */
  134. notifyReceivedChatMessage (id, nick, body, ts) {
  135. if (APP.conference.isLocalId(id)) {
  136. return;
  137. }
  138. triggerEvent(
  139. "incoming-message",
  140. {"from": id, "nick": nick, "message": body, "stamp": ts}
  141. );
  142. }
  143. /**
  144. * Notify external application (if API is enabled) that
  145. * user joined the conference.
  146. * @param {string} id user id
  147. */
  148. notifyUserJoined (id) {
  149. triggerEvent("participant-joined", {id});
  150. }
  151. /**
  152. * Notify external application (if API is enabled) that
  153. * user left the conference.
  154. * @param {string} id user id
  155. */
  156. notifyUserLeft (id) {
  157. triggerEvent("participant-left", {id});
  158. }
  159. /**
  160. * Notify external application (if API is enabled) that
  161. * user changed their nickname.
  162. * @param {string} id user id
  163. * @param {string} displayName user nickname
  164. */
  165. notifyDisplayNameChanged (id, displayName) {
  166. triggerEvent("display-name-change", {id, displayname: displayName});
  167. }
  168. /**
  169. * Notify external application (if API is enabled) that
  170. * user changed their nickname.
  171. * @param {string} id user id
  172. * @param {string} displayName user nickname
  173. */
  174. notifyConferenceJoined (room) {
  175. triggerEvent("video-conference-joined", {roomName: room});
  176. }
  177. /**
  178. * Notify external application (if API is enabled) that
  179. * user changed their nickname.
  180. * @param {string} id user id
  181. * @param {string} displayName user nickname
  182. */
  183. notifyConferenceLeft (room) {
  184. triggerEvent("video-conference-left", {roomName: room});
  185. }
  186. /**
  187. * Notify external application (if API is enabled) that
  188. * we are ready to be closed.
  189. */
  190. notifyReadyToClose () {
  191. triggerEvent("video-ready-to-close", {});
  192. }
  193. /**
  194. * Sends remote control event.
  195. * @param {object} event the event.
  196. */
  197. sendRemoteControlEvent(event) {
  198. sendMessage({method: "remote-control-event", params: event});
  199. }
  200. /**
  201. * Removes the listeners.
  202. */
  203. dispose () {
  204. if(enabled)
  205. postis.destroy();
  206. }
  207. }
  208. export default new API();