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

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