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

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