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

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