選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

API.js 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /* global APP, getConfigParamsFromUrl */
  2. const logger = require("jitsi-meet-logger").getLogger(__filename);
  3. /**
  4. * Implements API class that communicates with external api class
  5. * and provides interface to access Jitsi Meet features by external
  6. * applications that embed Jitsi Meet
  7. */
  8. import postisInit from 'postis';
  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. function initCommands() {
  39. commands = {
  40. "display-name": APP.UI.inputDisplayNameHandler,
  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. };
  52. Object.keys(commands).forEach(function (key) {
  53. postis.listen(key, args => commands[key](...args));
  54. });
  55. }
  56. /**
  57. * Maps the supported events and their status
  58. * (true it the event is enabled and false if it is disabled)
  59. * @type {{
  60. * incoming-message: boolean,
  61. * outgoing-message: boolean,
  62. * display-name-change: boolean,
  63. * participant-left: boolean,
  64. * participant-joined: boolean,
  65. * video-conference-left: boolean,
  66. * video-conference-joined: boolean
  67. * }}
  68. */
  69. const events = {
  70. "incoming-message": false,
  71. "outgoing-message":false,
  72. "display-name-change": false,
  73. "participant-joined": false,
  74. "participant-left": false,
  75. "video-conference-joined": false,
  76. "video-conference-left": false,
  77. "video-ready-to-close": false
  78. };
  79. /**
  80. * Sends message to the external application.
  81. * @param message {object}
  82. * @param method {string}
  83. * @param params {object} the object that will be sent as JSON string
  84. */
  85. function sendMessage(message) {
  86. if(enabled)
  87. postis.send(message);
  88. }
  89. /**
  90. * Check whether the API should be enabled or not.
  91. * @returns {boolean}
  92. */
  93. function isEnabled () {
  94. return (typeof jitsi_meet_external_api_id === "number");
  95. }
  96. /**
  97. * Checks whether the event is enabled ot not.
  98. * @param name the name of the event.
  99. * @returns {*}
  100. */
  101. function isEventEnabled (name) {
  102. return events[name];
  103. }
  104. /**
  105. * Sends event object to the external application that has been subscribed
  106. * for that event.
  107. * @param name the name event
  108. * @param object data associated with the event
  109. */
  110. function triggerEvent (name, object) {
  111. if(isEventEnabled(name))
  112. sendMessage({method: name, params: object});
  113. }
  114. /**
  115. * Handles system messages. (for example: enable/disable events)
  116. * @param message {object} the message
  117. */
  118. function onSystemMessage(message) {
  119. switch (message.type) {
  120. case "eventStatus":
  121. if(!message.name || !message.value) {
  122. logger.warn("Unknown system message format", message);
  123. break;
  124. }
  125. events[message.name] = message.value;
  126. break;
  127. default:
  128. logger.warn("Unknown system message type", message);
  129. }
  130. }
  131. export default {
  132. /**
  133. * Initializes the APIConnector. Setups message event listeners that will
  134. * receive information from external applications that embed Jitsi Meet.
  135. * It also sends a message to the external application that APIConnector
  136. * is initialized.
  137. * @param options {object}
  138. * @param forceEnable {boolean} if true the module will be enabled.
  139. * @param enabledEvents {array} array of events that should be enabled.
  140. */
  141. init (options = {}) {
  142. if(!isEnabled() && !options.forceEnable)
  143. return;
  144. enabled = true;
  145. if(options.enabledEvents)
  146. options.enabledEvents.forEach(function (eventName) {
  147. events[eventName] = true;
  148. });
  149. let postisOptions = {
  150. window: target
  151. };
  152. if(typeof jitsi_meet_external_api_id === "number")
  153. postisOptions.scope
  154. = "jitsi_meet_external_api_" + jitsi_meet_external_api_id;
  155. postis = postisInit(postisOptions);
  156. postis.listen("jitsiSystemMessage", onSystemMessage);
  157. initCommands();
  158. },
  159. /**
  160. * Notify external application (if API is enabled) that message was sent.
  161. * @param {string} body message body
  162. */
  163. notifySendingChatMessage (body) {
  164. triggerEvent("outgoing-message", {"message": body});
  165. },
  166. /**
  167. * Notify external application (if API is enabled) that
  168. * message was received.
  169. * @param {string} id user id
  170. * @param {string} nick user nickname
  171. * @param {string} body message body
  172. * @param {number} ts message creation timestamp
  173. */
  174. notifyReceivedChatMessage (id, nick, body, ts) {
  175. if (APP.conference.isLocalId(id)) {
  176. return;
  177. }
  178. triggerEvent(
  179. "incoming-message",
  180. {"from": id, "nick": nick, "message": body, "stamp": ts}
  181. );
  182. },
  183. /**
  184. * Notify external application (if API is enabled) that
  185. * user joined the conference.
  186. * @param {string} id user id
  187. */
  188. notifyUserJoined (id) {
  189. triggerEvent("participant-joined", {id});
  190. },
  191. /**
  192. * Notify external application (if API is enabled) that
  193. * user left the conference.
  194. * @param {string} id user id
  195. */
  196. notifyUserLeft (id) {
  197. triggerEvent("participant-left", {id});
  198. },
  199. /**
  200. * Notify external application (if API is enabled) that
  201. * user changed their nickname.
  202. * @param {string} id user id
  203. * @param {string} displayName user nickname
  204. */
  205. notifyDisplayNameChanged (id, displayName) {
  206. triggerEvent("display-name-change", {id, displayname: displayName});
  207. },
  208. /**
  209. * Notify external application (if API is enabled) that
  210. * user changed their nickname.
  211. * @param {string} id user id
  212. * @param {string} displayName user nickname
  213. */
  214. notifyConferenceJoined (room) {
  215. triggerEvent("video-conference-joined", {roomName: room});
  216. },
  217. /**
  218. * Notify external application (if API is enabled) that
  219. * user changed their nickname.
  220. * @param {string} id user id
  221. * @param {string} displayName user nickname
  222. */
  223. notifyConferenceLeft (room) {
  224. triggerEvent("video-conference-left", {roomName: room});
  225. },
  226. /**
  227. * Notify external application (if API is enabled) that
  228. * we are ready to be closed.
  229. */
  230. notifyReadyToClose () {
  231. triggerEvent("video-ready-to-close", {});
  232. },
  233. /**
  234. * Removes the listeners.
  235. */
  236. dispose: function () {
  237. if(enabled)
  238. postis.destroy();
  239. }
  240. };