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.

external_api.js 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. const logger = require("jitsi-meet-logger").getLogger(__filename);
  2. import postisInit from "postis";
  3. /**
  4. * The minimum width for the Jitsi Meet frame
  5. * @type {number}
  6. */
  7. const MIN_WIDTH = 790;
  8. /**
  9. * The minimum height for the Jitsi Meet frame
  10. * @type {number}
  11. */
  12. const MIN_HEIGHT = 300;
  13. /**
  14. * Last id of api object
  15. * @type {number}
  16. */
  17. let id = 0;
  18. /**
  19. * Maps the names of the commands expected by the API with the name of the
  20. * commands expected by jitsi-meet
  21. */
  22. const commands = {
  23. "displayName": "display-name",
  24. "toggleAudio": "toggle-audio",
  25. "toggleVideo": "toggle-video",
  26. "toggleFilmStrip": "toggle-film-strip",
  27. "toggleChat": "toggle-chat",
  28. "toggleContactList": "toggle-contact-list",
  29. "toggleShareScreen": "toggle-share-screen",
  30. "hangup": "video-hangup",
  31. "email": "email",
  32. "avatarUrl": "avatar-url"
  33. };
  34. /**
  35. * Maps the names of the events expected by the API with the name of the
  36. * events expected by jitsi-meet
  37. */
  38. const events = {
  39. "incomingMessage": "incoming-message",
  40. "outgoingMessage": "outgoing-message",
  41. "displayNameChange": "display-name-change",
  42. "participantJoined": "participant-joined",
  43. "participantLeft": "participant-left",
  44. "videoConferenceJoined": "video-conference-joined",
  45. "videoConferenceLeft": "video-conference-left",
  46. "readyToClose": "video-ready-to-close"
  47. };
  48. /**
  49. * Sends the passed object to Jitsi Meet
  50. * @param postis {Postis object} the postis instance that is going to be used
  51. * to send the message
  52. * @param object the object to be sent
  53. * - method {sting}
  54. * - params {object}
  55. */
  56. function sendMessage(postis, object) {
  57. postis.send(object);
  58. }
  59. /**
  60. * Adds given number to the numberOfParticipants property of given APIInstance.
  61. * @param {JitsiMeetExternalAPI} APIInstance the instance of the
  62. * JitsiMeetExternalAPI
  63. * @param {int} number - the number of participants to be added to
  64. * numberOfParticipants property (this parameter can be negative number if the
  65. * numberOfParticipants should be decreased).
  66. */
  67. function changeParticipantNumber(APIInstance, number) {
  68. APIInstance.numberOfParticipants += number;
  69. }
  70. /**
  71. * Generates array with URL params based on the passed config object that will
  72. * be used for the Jitsi Meet URL generation.
  73. *
  74. * @param config {object} the config object.
  75. * @returns {Array<string>} the array with URL param strings.
  76. */
  77. function configToURLParamsArray(config) {
  78. const params = [];
  79. for (const key in config) {
  80. try {
  81. params.push(key + '='
  82. + encodeURIComponent(JSON.stringify(config[key])));
  83. } catch (e) {
  84. console.warn(`Error encoding ${key}: ${e}`);
  85. }
  86. }
  87. return params;
  88. }
  89. /**
  90. * The IFrame API interface class.
  91. */
  92. class JitsiMeetExternalAPI {
  93. /**
  94. * Constructs new API instance. Creates iframe element that loads
  95. * Jitsi Meet.
  96. * @param domain the domain name of the server that hosts the conference
  97. * @param room_name the name of the room to join
  98. * @param width width of the iframe
  99. * @param height height of the iframe
  100. * @param parent_node the node that will contain the iframe
  101. * @param configOverwrite object containing configuration options defined in
  102. * config.js to be overridden.
  103. * @param interfaceConfigOverwrite object containing configuration options
  104. * defined in interface_config.js to be overridden.
  105. * @param noSsl if the value is true https won't be used
  106. * @param {string} [jwt] the JWT token if needed by jitsi-meet for
  107. * authentication.
  108. * @constructor
  109. */
  110. constructor(domain, room_name, width, height, parentNode,
  111. configOverwrite, interfaceConfigOverwrite, noSsl, jwt) {
  112. if (!width || width < MIN_WIDTH) {
  113. width = MIN_WIDTH;
  114. }
  115. if (!height || height < MIN_HEIGHT) {
  116. height = MIN_HEIGHT;
  117. }
  118. this.parentNode = null;
  119. if (parentNode) {
  120. this.parentNode = parentNode;
  121. } else {
  122. var scriptTag = document.scripts[document.scripts.length - 1];
  123. this.parentNode = scriptTag.parentNode;
  124. }
  125. this.iframeHolder =
  126. this.parentNode.appendChild(document.createElement("div"));
  127. this.iframeHolder.id = "jitsiConference" + id;
  128. if (width) {
  129. this.iframeHolder.style.width = width + "px";
  130. }
  131. if (height) {
  132. this.iframeHolder.style.height = height + "px";
  133. }
  134. this.frameName = "jitsiConferenceFrame" + id;
  135. this.url = (noSsl) ? "http" : "https" +"://" + domain + "/";
  136. if (room_name) {
  137. this.url += room_name;
  138. }
  139. if (jwt) {
  140. this.url += '?jwt=' + jwt;
  141. }
  142. this.url += "#jitsi_meet_external_api_id=" + id;
  143. const configURLParams = configToURLParamsArray(configOverwrite);
  144. if (configURLParams.length) {
  145. this.url += '&config.' + configURLParams.join('&config.');
  146. }
  147. const interfaceConfigURLParams
  148. = configToURLParamsArray(interfaceConfigOverwrite);
  149. if (interfaceConfigURLParams.length) {
  150. this.url += '&interfaceConfig.'
  151. + interfaceConfigURLParams.join('&interfaceConfig.');
  152. }
  153. this.frame = document.createElement("iframe");
  154. this.frame.src = this.url;
  155. this.frame.name = this.frameName;
  156. this.frame.id = this.frameName;
  157. this.frame.width = "100%";
  158. this.frame.height = "100%";
  159. this.frame.setAttribute("allowFullScreen","true");
  160. this.frame = this.iframeHolder.appendChild(this.frame);
  161. this.postis = postisInit({
  162. window: this.frame.contentWindow,
  163. scope: "jitsi_meet_external_api_" + id
  164. });
  165. this.eventHandlers = {};
  166. // Map<{string} event_name, {boolean} postis_listener_added>
  167. this.postisListeners = {};
  168. this.numberOfParticipants = 1;
  169. this._setupListeners();
  170. id++;
  171. }
  172. /**
  173. * Executes command. The available commands are:
  174. * displayName - sets the display name of the local participant to the value
  175. * passed in the arguments array.
  176. * toggleAudio - mutes / unmutes audio with no arguments
  177. * toggleVideo - mutes / unmutes video with no arguments
  178. * filmStrip - hides / shows the film strip with no arguments
  179. * If the command doesn't require any arguments the parameter should be set
  180. * to empty array or it may be omitted.
  181. * @param name the name of the command
  182. * @param arguments array of arguments
  183. */
  184. executeCommand(name, ...argumentsList) {
  185. if(!(name in commands)) {
  186. logger.error("Not supported command name.");
  187. return;
  188. }
  189. sendMessage(this.postis, {
  190. method: commands[name],
  191. params: argumentsList
  192. });
  193. }
  194. /**
  195. * Executes commands. The available commands are:
  196. * displayName - sets the display name of the local participant to the value
  197. * passed in the arguments array.
  198. * toggleAudio - mutes / unmutes audio. no arguments
  199. * toggleVideo - mutes / unmutes video. no arguments
  200. * filmStrip - hides / shows the film strip. no arguments
  201. * toggleChat - hides / shows chat. no arguments.
  202. * toggleContactList - hides / shows contact list. no arguments.
  203. * toggleShareScreen - starts / stops screen sharing. no arguments.
  204. * @param object the object with commands to be executed. The keys of the
  205. * object are the commands that will be executed and the values are the
  206. * arguments for the command.
  207. */
  208. executeCommands(object) {
  209. for (var key in object) {
  210. this.executeCommand(key, object[key]);
  211. }
  212. }
  213. /**
  214. * Adds event listeners to Meet Jitsi. The object key should be the name of
  215. * the event and value - the listener.
  216. * Currently we support the following
  217. * events:
  218. * incomingMessage - receives event notifications about incoming
  219. * messages. The listener will receive object with the following structure:
  220. * {{
  221. * "from": from,//JID of the user that sent the message
  222. * "nick": nick,//the nickname of the user that sent the message
  223. * "message": txt//the text of the message
  224. * }}
  225. * outgoingMessage - receives event notifications about outgoing
  226. * messages. The listener will receive object with the following structure:
  227. * {{
  228. * "message": txt//the text of the message
  229. * }}
  230. * displayNameChanged - receives event notifications about display name
  231. * change. The listener will receive object with the following structure:
  232. * {{
  233. * jid: jid,//the JID of the participant that changed his display name
  234. * displayname: displayName //the new display name
  235. * }}
  236. * participantJoined - receives event notifications about new participant.
  237. * The listener will receive object with the following structure:
  238. * {{
  239. * jid: jid //the jid of the participant
  240. * }}
  241. * participantLeft - receives event notifications about the participant that
  242. * left the room.
  243. * The listener will receive object with the following structure:
  244. * {{
  245. * jid: jid //the jid of the participant
  246. * }}
  247. * video-conference-joined - receives event notifications about the local
  248. * user has successfully joined the video conference.
  249. * The listener will receive object with the following structure:
  250. * {{
  251. * roomName: room //the room name of the conference
  252. * }}
  253. * video-conference-left - receives event notifications about the local user
  254. * has left the video conference.
  255. * The listener will receive object with the following structure:
  256. * {{
  257. * roomName: room //the room name of the conference
  258. * }}
  259. * readyToClose - all hangup operations are completed and Jitsi Meet is
  260. * ready to be disposed.
  261. * @param object
  262. */
  263. addEventListeners(object) {
  264. for (var i in object) {
  265. this.addEventListener(i, object[i]);
  266. }
  267. }
  268. /**
  269. * Adds event listeners to Meet Jitsi. Currently we support the following
  270. * events:
  271. * incomingMessage - receives event notifications about incoming
  272. * messages. The listener will receive object with the following structure:
  273. * {{
  274. * "from": from,//JID of the user that sent the message
  275. * "nick": nick,//the nickname of the user that sent the message
  276. * "message": txt//the text of the message
  277. * }}
  278. * outgoingMessage - receives event notifications about outgoing
  279. * messages. The listener will receive object with the following structure:
  280. * {{
  281. * "message": txt//the text of the message
  282. * }}
  283. * displayNameChanged - receives event notifications about display name
  284. * change. The listener will receive object with the following structure:
  285. * {{
  286. * jid: jid,//the JID of the participant that changed his display name
  287. * displayname: displayName //the new display name
  288. * }}
  289. * participantJoined - receives event notifications about new participant.
  290. * The listener will receive object with the following structure:
  291. * {{
  292. * jid: jid //the jid of the participant
  293. * }}
  294. * participantLeft - receives event notifications about participant the that
  295. * left the room.
  296. * The listener will receive object with the following structure:
  297. * {{
  298. * jid: jid //the jid of the participant
  299. * }}
  300. * video-conference-joined - receives event notifications fired when the
  301. * local user has joined the video conference.
  302. * The listener will receive object with the following structure:
  303. * {{
  304. * roomName: room //the room name of the conference
  305. * }}
  306. * video-conference-left - receives event notifications fired when the local
  307. * user has joined the video conference.
  308. * The listener will receive object with the following structure:
  309. * {{
  310. * roomName: room //the room name of the conference
  311. * }}
  312. * @param event the name of the event
  313. * @param listener the listener
  314. */
  315. addEventListener(event, listener) {
  316. if (!(event in events)) {
  317. logger.error("Not supported event name.");
  318. return;
  319. }
  320. // We cannot remove listeners from postis that's why we are handling the
  321. // callback that way.
  322. if (!this.postisListeners[event]) {
  323. this.postis.listen(events[event], data => {
  324. if((event in this.eventHandlers) &&
  325. typeof this.eventHandlers[event] === "function")
  326. this.eventHandlers[event].call(null, data);
  327. });
  328. this.postisListeners[event] = true;
  329. }
  330. this.eventHandlers[event] = listener;
  331. }
  332. /**
  333. * Removes event listener.
  334. * @param event the name of the event.
  335. */
  336. removeEventListener(event) {
  337. if(!(event in this.eventHandlers))
  338. {
  339. logger.error("The event " + event + " is not registered.");
  340. return;
  341. }
  342. delete this.eventHandlers[event];
  343. }
  344. /**
  345. * Removes event listeners.
  346. * @param events array with the names of the events.
  347. */
  348. removeEventListeners(events) {
  349. for(var i = 0; i < events.length; i++) {
  350. this.removeEventListener(events[i]);
  351. }
  352. }
  353. /**
  354. * Returns the number of participants in the conference.
  355. * NOTE: the local participant is included.
  356. * @returns {int} the number of participants in the conference.
  357. */
  358. getNumberOfParticipants() {
  359. return this.numberOfParticipants;
  360. }
  361. /**
  362. * Setups listeners that are used internally for JitsiMeetExternalAPI.
  363. */
  364. _setupListeners() {
  365. this.postis.listen("participant-joined",
  366. changeParticipantNumber.bind(null, this, 1));
  367. this.postis.listen("participant-left",
  368. changeParticipantNumber.bind(null, this, -1));
  369. }
  370. /**
  371. * Removes the listeners and removes the Jitsi Meet frame.
  372. */
  373. dispose() {
  374. this.postis.destroy();
  375. var frame = document.getElementById(this.frameName);
  376. if(frame)
  377. frame.src = 'about:blank';
  378. window.setTimeout( () => {
  379. this.iframeHolder.removeChild(this.frame);
  380. this.iframeHolder.parentNode.removeChild(this.iframeHolder);
  381. }, 10);
  382. }
  383. }
  384. module.exports = JitsiMeetExternalAPI;