您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

external_api.js 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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. "email": "email",
  35. "avatarUrl": "avatar-url"
  36. };
  37. /**
  38. * Maps the names of the events expected by the API with the name of the
  39. * events expected by jitsi-meet
  40. */
  41. var events = {
  42. "incomingMessage": "incoming-message",
  43. "outgoingMessage": "outgoing-message",
  44. "displayNameChange": "display-name-change",
  45. "participantJoined": "participant-joined",
  46. "participantLeft": "participant-left",
  47. "videoConferenceJoined": "video-conference-joined",
  48. "videoConferenceLeft": "video-conference-left",
  49. "readyToClose": "video-ready-to-close"
  50. };
  51. /**
  52. * Sends the passed object to Jitsi Meet
  53. * @param postis {Postis object} the postis instance that is going to be used
  54. * to send the message
  55. * @param object the object to be sent
  56. * - method {sting}
  57. * - params {object}
  58. */
  59. function sendMessage(postis, object) {
  60. postis.send(object);
  61. }
  62. /**
  63. * Adds given number to the numberOfParticipants property of given APIInstance.
  64. * @param {JitsiMeetExternalAPI} APIInstance the instance of the
  65. * JitsiMeetExternalAPI
  66. * @param {int} number - the number of participants to be added to
  67. * numberOfParticipants property (this parameter can be negative number if the
  68. * numberOfParticipants should be decreased).
  69. */
  70. function changeParticipantNumber(APIInstance, number) {
  71. APIInstance.numberOfParticipants += number;
  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 configOverwrite object containing configuration options defined in
  82. * config.js to be overridden.
  83. * @param interfaceConfigOverwrite object containing configuration options
  84. * defined in interface_config.js to be overridden.
  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. // Map<{string} event_name, {boolean} postis_listener_added>
  145. this.postisListeners = {};
  146. this.numberOfParticipants = 1;
  147. this._setupListeners();
  148. id++;
  149. }
  150. /**
  151. * Executes command. The available commands are:
  152. * displayName - sets the display name of the local participant to the value
  153. * passed in the arguments array.
  154. * toggleAudio - mutes / unmutes audio with no arguments
  155. * toggleVideo - mutes / unmutes video with no arguments
  156. * filmStrip - hides / shows the film strip with no arguments
  157. * If the command doesn't require any arguments the parameter should be set
  158. * to empty array or it may be omitted.
  159. * @param name the name of the command
  160. * @param arguments array of arguments
  161. */
  162. JitsiMeetExternalAPI.prototype.executeCommand
  163. = function(name, ...argumentsList) {
  164. if(!(name in commands)) {
  165. logger.error("Not supported command name.");
  166. return;
  167. }
  168. sendMessage(this.postis, {method: commands[name], params: argumentsList});
  169. };
  170. /**
  171. * Executes commands. The available commands are:
  172. * displayName - sets the display name of the local participant to the value
  173. * passed in the arguments array.
  174. * toggleAudio - mutes / unmutes audio. no arguments
  175. * toggleVideo - mutes / unmutes video. no arguments
  176. * filmStrip - hides / shows the film strip. no arguments
  177. * toggleChat - hides / shows chat. no arguments.
  178. * toggleContactList - hides / shows contact list. no arguments.
  179. * toggleShareScreen - starts / stops screen sharing. no arguments.
  180. * @param object the object with commands to be executed. The keys of the
  181. * object are the commands that will be executed and the values are the
  182. * arguments for the command.
  183. */
  184. JitsiMeetExternalAPI.prototype.executeCommands = function(object) {
  185. for(var key in object)
  186. this.executeCommand(key, object[key]);
  187. };
  188. /**
  189. * Adds event listeners to Meet Jitsi. The object key should be the name of
  190. * the event and value - the listener.
  191. * Currently we support the following
  192. * events:
  193. * incomingMessage - receives event notifications about incoming
  194. * messages. The listener will receive object with the following structure:
  195. * {{
  196. * "from": from,//JID of the user that sent the message
  197. * "nick": nick,//the nickname of the user that sent the message
  198. * "message": txt//the text of the message
  199. * }}
  200. * outgoingMessage - receives event notifications about outgoing
  201. * messages. The listener will receive object with the following structure:
  202. * {{
  203. * "message": txt//the text of the message
  204. * }}
  205. * displayNameChanged - receives event notifications about display name
  206. * change. The listener will receive object with the following structure:
  207. * {{
  208. * jid: jid,//the JID of the participant that changed his display name
  209. * displayname: displayName //the new display name
  210. * }}
  211. * participantJoined - receives event notifications about new participant.
  212. * The listener will receive object with the following structure:
  213. * {{
  214. * jid: jid //the jid of the participant
  215. * }}
  216. * participantLeft - receives event notifications about the participant that
  217. * left the room.
  218. * The listener will receive object with the following structure:
  219. * {{
  220. * jid: jid //the jid of the participant
  221. * }}
  222. * video-conference-joined - receives event notifications about the local user
  223. * has successfully joined the video conference.
  224. * The listener will receive object with the following structure:
  225. * {{
  226. * roomName: room //the room name of the conference
  227. * }}
  228. * video-conference-left - receives event notifications about the local user
  229. * has left the video conference.
  230. * The listener will receive object with the following structure:
  231. * {{
  232. * roomName: room //the room name of the conference
  233. * }}
  234. * readyToClose - all hangup operations are completed and Jitsi Meet is ready
  235. * to be disposed.
  236. * @param object
  237. */
  238. JitsiMeetExternalAPI.prototype.addEventListeners = function(object) {
  239. for(var i in object)
  240. this.addEventListener(i, object[i]);
  241. };
  242. /**
  243. * Adds event listeners to Meet Jitsi. Currently we support the following
  244. * events:
  245. * incomingMessage - receives event notifications about incoming
  246. * messages. The listener will receive object with the following structure:
  247. * {{
  248. * "from": from,//JID of the user that sent the message
  249. * "nick": nick,//the nickname of the user that sent the message
  250. * "message": txt//the text of the message
  251. * }}
  252. * outgoingMessage - receives event notifications about outgoing
  253. * messages. The listener will receive object with the following structure:
  254. * {{
  255. * "message": txt//the text of the message
  256. * }}
  257. * displayNameChanged - receives event notifications about display name
  258. * change. The listener will receive object with the following structure:
  259. * {{
  260. * jid: jid,//the JID of the participant that changed his display name
  261. * displayname: displayName //the new display name
  262. * }}
  263. * participantJoined - receives event notifications about new participant.
  264. * The listener will receive object with the following structure:
  265. * {{
  266. * jid: jid //the jid of the participant
  267. * }}
  268. * participantLeft - receives event notifications about participant the that
  269. * left the room.
  270. * The listener will receive object with the following structure:
  271. * {{
  272. * jid: jid //the jid of the participant
  273. * }}
  274. * video-conference-joined - 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. * video-conference-left - receives event notifications fired when the local
  281. * user has joined the video conference.
  282. * The listener will receive object with the following structure:
  283. * {{
  284. * roomName: room //the room name of the conference
  285. * }}
  286. * @param event the name of the event
  287. * @param listener the listener
  288. */
  289. JitsiMeetExternalAPI.prototype.addEventListener = function(event, listener) {
  290. if(!(event in events)) {
  291. logger.error("Not supported event name.");
  292. return;
  293. }
  294. // We cannot remove listeners from postis that's why we are handling the
  295. // callback that way.
  296. if(!this.postisListeners[event]) {
  297. this.postis.listen(events[event], function(data) {
  298. if((event in this.eventHandlers) &&
  299. typeof this.eventHandlers[event] === "function")
  300. this.eventHandlers[event].call(null, data);
  301. }.bind(this));
  302. this.postisListeners[event] = true;
  303. }
  304. this.eventHandlers[event] = listener;
  305. };
  306. /**
  307. * Removes event listener.
  308. * @param event the name of the event.
  309. */
  310. JitsiMeetExternalAPI.prototype.removeEventListener = function(event) {
  311. if(!(event in this.eventHandlers))
  312. {
  313. logger.error("The event " + event + " is not registered.");
  314. return;
  315. }
  316. delete this.eventHandlers[event];
  317. };
  318. /**
  319. * Removes event listeners.
  320. * @param events array with the names of the events.
  321. */
  322. JitsiMeetExternalAPI.prototype.removeEventListeners = function(events) {
  323. for(var i = 0; i < events.length; i++)
  324. this.removeEventListener(events[i]);
  325. };
  326. /**
  327. * Returns the number of participants in the conference.
  328. * NOTE: the local participant is included.
  329. * @returns {int} the number of participants in the conference.
  330. */
  331. JitsiMeetExternalAPI.prototype.getNumberOfParticipants = function() {
  332. return this.numberOfParticipants;
  333. };
  334. /**
  335. * Setups listeners that are used internally for JitsiMeetExternalAPI.
  336. */
  337. JitsiMeetExternalAPI.prototype._setupListeners = function() {
  338. this.postis.listen("participant-joined",
  339. changeParticipantNumber.bind(null, this, 1));
  340. this.postis.listen("participant-left",
  341. changeParticipantNumber.bind(null, this, -1));
  342. };
  343. /**
  344. * Removes the listeners and removes the Jitsi Meet frame.
  345. */
  346. JitsiMeetExternalAPI.prototype.dispose = function() {
  347. this.postis.destroy();
  348. var frame = document.getElementById(this.frameName);
  349. if(frame)
  350. frame.src = 'about:blank';
  351. var self = this;
  352. window.setTimeout(function () {
  353. self.iframeHolder.removeChild(self.frame);
  354. self.iframeHolder.parentNode.removeChild(self.iframeHolder);
  355. }, 10);
  356. };
  357. module.exports = JitsiMeetExternalAPI;