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

external_api.js 12KB

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