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

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