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

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