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

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