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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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. * Sends message for event enable/disable status change.
  64. * @param postis {Postis object} the postis instance that is going to be used.
  65. * @param event {string} the name of the event
  66. * @param status {boolean} true - enabled; false - disabled;
  67. */
  68. function changeEventStatus(postis, event, status) {
  69. if(!(event in events)) {
  70. logger.error("Not supported event name.");
  71. return;
  72. }
  73. sendMessage(postis, {
  74. method: "jitsiSystemMessage",
  75. params: {type: "eventStatus", name: events[event], value: status}
  76. });
  77. }
  78. /**
  79. * Adds given number to the numberOfParticipants property of given APIInstance.
  80. * @param {JitsiMeetExternalAPI} APIInstance the instance of the
  81. * JitsiMeetExternalAPI
  82. * @param {int} number - the number of participants to be added to
  83. * numberOfParticipants property (this parameter can be negative number if the
  84. * numberOfParticipants should be decreased).
  85. */
  86. function changeParticipantNumber(APIInstance, number) {
  87. APIInstance.numberOfParticipants += number;
  88. }
  89. /**
  90. * Constructs new API instance. Creates iframe element that loads
  91. * Jitsi Meet.
  92. * @param domain the domain name of the server that hosts the conference
  93. * @param room_name the name of the room to join
  94. * @param width width of the iframe
  95. * @param height height of the iframe
  96. * @param parent_node the node that will contain the iframe
  97. * @param filmStripOnly if the value is true only the small videos will be
  98. * visible.
  99. * @param noSsl if the value is true https won't be used
  100. * @constructor
  101. */
  102. function JitsiMeetExternalAPI(domain, room_name, width, height, parentNode,
  103. configOverwrite, interfaceConfigOverwrite, noSsl) {
  104. if (!width || width < MIN_WIDTH)
  105. width = MIN_WIDTH;
  106. if (!height || height < MIN_HEIGHT)
  107. height = MIN_HEIGHT;
  108. this.parentNode = null;
  109. if (parentNode) {
  110. this.parentNode = parentNode;
  111. } else {
  112. var scriptTag = document.scripts[document.scripts.length - 1];
  113. this.parentNode = scriptTag.parentNode;
  114. }
  115. this.iframeHolder =
  116. this.parentNode.appendChild(document.createElement("div"));
  117. this.iframeHolder.id = "jitsiConference" + id;
  118. if(width)
  119. this.iframeHolder.style.width = width + "px";
  120. if(height)
  121. this.iframeHolder.style.height = height + "px";
  122. this.frameName = "jitsiConferenceFrame" + id;
  123. this.url = (noSsl) ? "http" : "https" +"://" + domain + "/";
  124. if(room_name)
  125. this.url += room_name;
  126. this.url += "#jitsi_meet_external_api_id=" + id;
  127. var key;
  128. if (configOverwrite) {
  129. for (key in configOverwrite) {
  130. if (!configOverwrite.hasOwnProperty(key) ||
  131. typeof key !== 'string')
  132. continue;
  133. this.url += "&config." + key + "=" + configOverwrite[key];
  134. }
  135. }
  136. if (interfaceConfigOverwrite) {
  137. for (key in interfaceConfigOverwrite) {
  138. if (!interfaceConfigOverwrite.hasOwnProperty(key) ||
  139. typeof key !== 'string')
  140. continue;
  141. this.url += "&interfaceConfig." + key + "=" +
  142. interfaceConfigOverwrite[key];
  143. }
  144. }
  145. this.frame = document.createElement("iframe");
  146. this.frame.src = this.url;
  147. this.frame.name = this.frameName;
  148. this.frame.id = this.frameName;
  149. this.frame.width = "100%";
  150. this.frame.height = "100%";
  151. this.frame.setAttribute("allowFullScreen","true");
  152. this.frame = this.iframeHolder.appendChild(this.frame);
  153. this.postis = postisInit({
  154. window: this.frame.contentWindow,
  155. scope: "jitsi_meet_external_api_" + id
  156. });
  157. this.eventHandlers = {};
  158. this.numberOfParticipants = 1;
  159. this._setupListeners();
  160. id++;
  161. }
  162. /**
  163. * Executes command. The available commands are:
  164. * displayName - sets the display name of the local participant to the value
  165. * passed in the arguments array.
  166. * toggleAudio - mutes / unmutes audio with no arguments
  167. * toggleVideo - mutes / unmutes video with no arguments
  168. * filmStrip - hides / shows the film strip with no arguments
  169. * If the command doesn't require any arguments the parameter should be set
  170. * to empty array or it may be omitted.
  171. * @param name the name of the command
  172. * @param arguments array of arguments
  173. */
  174. JitsiMeetExternalAPI.prototype.executeCommand
  175. = function(name, ...argumentsList) {
  176. if(!(name in commands)) {
  177. logger.error("Not supported command name.");
  178. return;
  179. }
  180. sendMessage(this.postis, {method: commands[name], params: argumentsList});
  181. };
  182. /**
  183. * Executes commands. The available commands are:
  184. * displayName - sets the display name of the local participant to the value
  185. * passed in the arguments array.
  186. * toggleAudio - mutes / unmutes audio. no arguments
  187. * toggleVideo - mutes / unmutes video. no arguments
  188. * filmStrip - hides / shows the film strip. no arguments
  189. * toggleChat - hides / shows chat. no arguments.
  190. * toggleContactList - hides / shows contact list. no arguments.
  191. * toggleShareScreen - starts / stops screen sharing. no arguments.
  192. * @param object the object with commands to be executed. The keys of the
  193. * object are the commands that will be executed and the values are the
  194. * arguments for the command.
  195. */
  196. JitsiMeetExternalAPI.prototype.executeCommands = function(object) {
  197. for(var key in object)
  198. this.executeCommand(key, object[key]);
  199. };
  200. /**
  201. * Adds event listeners to Meet Jitsi. The object key should be the name of
  202. * the event and value - the listener.
  203. * Currently we support the following
  204. * events:
  205. * incomingMessage - receives event notifications about incoming
  206. * messages. The listener will receive object with the following structure:
  207. * {{
  208. * "from": from,//JID of the user that sent the message
  209. * "nick": nick,//the nickname of the user that sent the message
  210. * "message": txt//the text of the message
  211. * }}
  212. * outgoingMessage - receives event notifications about outgoing
  213. * messages. The listener will receive object with the following structure:
  214. * {{
  215. * "message": txt//the text of the message
  216. * }}
  217. * displayNameChanged - receives event notifications about display name
  218. * change. The listener will receive object with the following structure:
  219. * {{
  220. * jid: jid,//the JID of the participant that changed his display name
  221. * displayname: displayName //the new display name
  222. * }}
  223. * participantJoined - receives event notifications about new participant.
  224. * The listener will receive object with the following structure:
  225. * {{
  226. * jid: jid //the jid of the participant
  227. * }}
  228. * participantLeft - receives event notifications about the participant that
  229. * left the room.
  230. * The listener will receive object with the following structure:
  231. * {{
  232. * jid: jid //the jid of the participant
  233. * }}
  234. * video-conference-joined - receives event notifications about the local user
  235. * has successfully joined the video conference.
  236. * The listener will receive object with the following structure:
  237. * {{
  238. * roomName: room //the room name of the conference
  239. * }}
  240. * video-conference-left - receives event notifications about the local user
  241. * has left the video conference.
  242. * The listener will receive object with the following structure:
  243. * {{
  244. * roomName: room //the room name of the conference
  245. * }}
  246. * readyToClose - all hangup operations are completed and Jitsi Meet is ready
  247. * to be disposed.
  248. * @param object
  249. */
  250. JitsiMeetExternalAPI.prototype.addEventListeners = function(object) {
  251. for(var i in object)
  252. this.addEventListener(i, object[i]);
  253. };
  254. /**
  255. * Adds event listeners to Meet Jitsi. Currently we support the following
  256. * events:
  257. * incomingMessage - receives event notifications about incoming
  258. * messages. The listener will receive object with the following structure:
  259. * {{
  260. * "from": from,//JID of the user that sent the message
  261. * "nick": nick,//the nickname of the user that sent the message
  262. * "message": txt//the text of the message
  263. * }}
  264. * outgoingMessage - receives event notifications about outgoing
  265. * messages. The listener will receive object with the following structure:
  266. * {{
  267. * "message": txt//the text of the message
  268. * }}
  269. * displayNameChanged - receives event notifications about display name
  270. * change. The listener will receive object with the following structure:
  271. * {{
  272. * jid: jid,//the JID of the participant that changed his display name
  273. * displayname: displayName //the new display name
  274. * }}
  275. * participantJoined - receives event notifications about new participant.
  276. * The listener will receive object with the following structure:
  277. * {{
  278. * jid: jid //the jid of the participant
  279. * }}
  280. * participantLeft - receives event notifications about participant the that
  281. * left the room.
  282. * The listener will receive object with the following structure:
  283. * {{
  284. * jid: jid //the jid of the participant
  285. * }}
  286. * video-conference-joined - receives event notifications fired when the local
  287. * user has joined the video conference.
  288. * The listener will receive object with the following structure:
  289. * {{
  290. * roomName: room //the room name of the conference
  291. * }}
  292. * video-conference-left - receives event notifications fired when the local
  293. * user has joined the video conference.
  294. * The listener will receive object with the following structure:
  295. * {{
  296. * roomName: room //the room name of the conference
  297. * }}
  298. * @param event the name of the event
  299. * @param listener the listener
  300. */
  301. JitsiMeetExternalAPI.prototype.addEventListener = function(event, listener) {
  302. if(!(event in events)) {
  303. logger.error("Not supported event name.");
  304. return;
  305. }
  306. // We cannot remove listeners from postis that's why we are handling the
  307. // callback that way.
  308. if(!(event in this.eventHandlers))
  309. this.postis.listen(events[event], function(data) {
  310. if((event in this.eventHandlers) &&
  311. typeof this.eventHandlers[event] === "function")
  312. this.eventHandlers[event].call(null, data);
  313. }.bind(this));
  314. this.eventHandlers[event] = listener;
  315. changeEventStatus(this.postis, event, true);
  316. };
  317. /**
  318. * Removes event listener.
  319. * @param event the name of the event.
  320. */
  321. JitsiMeetExternalAPI.prototype.removeEventListener = function(event) {
  322. if(!(event in this.eventHandlers))
  323. {
  324. logger.error("The event " + event + " is not registered.");
  325. return;
  326. }
  327. delete this.eventHandlers[event];
  328. changeEventStatus(this.postis, event, false);
  329. };
  330. /**
  331. * Removes event listeners.
  332. * @param events array with the names of the events.
  333. */
  334. JitsiMeetExternalAPI.prototype.removeEventListeners = function(events) {
  335. for(var i = 0; i < events.length; i++)
  336. this.removeEventListener(events[i]);
  337. };
  338. /**
  339. * Returns the number of participants in the conference.
  340. * NOTE: the local participant is included.
  341. * @returns {int} the number of participants in the conference.
  342. */
  343. JitsiMeetExternalAPI.prototype.getNumberOfParticipant = function() {
  344. return this.numberOfParticipants;
  345. };
  346. /**
  347. * Setups listeners that are used internally for JitsiMeetExternalAPI.
  348. */
  349. JitsiMeetExternalAPI.prototype._setupListeners = function() {
  350. this.postis.listen("participant-joined",
  351. changeParticipantNumber.bind(null, this, 1));
  352. this.postis.listen("participant-left",
  353. changeParticipantNumber.bind(null, this, -1));
  354. };
  355. /**
  356. * Removes the listeners and removes the Jitsi Meet frame.
  357. */
  358. JitsiMeetExternalAPI.prototype.dispose = function() {
  359. this.postis.destroy();
  360. var frame = document.getElementById(this.frameName);
  361. if(frame)
  362. frame.src = 'about:blank';
  363. var self = this;
  364. window.setTimeout(function () {
  365. self.iframeHolder.removeChild(self.frame);
  366. self.iframeHolder.parentNode.removeChild(self.iframeHolder);
  367. }, 10);
  368. };
  369. module.exports = JitsiMeetExternalAPI;