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

moderator.js 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. /* global $, $iq, APP, config, messageHandler,
  2. roomName, sessionTerminated, Strophe, Util */
  3. var XMPPEvents = require("../../service/xmpp/XMPPEvents");
  4. var Settings = require("../settings/Settings");
  5. var AuthenticationEvents
  6. = require("../../service/authentication/AuthenticationEvents");
  7. function createExpBackoffTimer(step) {
  8. var count = 1;
  9. return function (reset) {
  10. // Reset call
  11. if (reset) {
  12. count = 1;
  13. return;
  14. }
  15. // Calculate next timeout
  16. var timeout = Math.pow(2, count - 1);
  17. count += 1;
  18. return timeout * step;
  19. };
  20. }
  21. function Moderator(roomName, xmpp, emitter) {
  22. this.roomName = roomName;
  23. this.xmppService = xmpp;
  24. this.getNextTimeout = createExpBackoffTimer(1000);
  25. this.getNextErrorTimeout = createExpBackoffTimer(1000);
  26. // External authentication stuff
  27. this.externalAuthEnabled = false;
  28. this.settings = new Settings(roomName);
  29. // Sip gateway can be enabled by configuring Jigasi host in config.js or
  30. // it will be enabled automatically if focus detects the component through
  31. // service discovery.
  32. this.sipGatewayEnabled = this.xmppService.options.hosts.call_control !== undefined;
  33. this.eventEmitter = emitter;
  34. this.connection = this.xmppService.connection;
  35. this.focusUserJid;
  36. //FIXME:
  37. // Message listener that talks to POPUP window
  38. function listener(event) {
  39. if (event.data && event.data.sessionId) {
  40. if (event.origin !== window.location.origin) {
  41. console.warn("Ignoring sessionId from different origin: " +
  42. event.origin);
  43. return;
  44. }
  45. localStorage.setItem('sessionId', event.data.sessionId);
  46. // After popup is closed we will authenticate
  47. }
  48. }
  49. // Register
  50. if (window.addEventListener) {
  51. window.addEventListener("message", listener, false);
  52. } else {
  53. window.attachEvent("onmessage", listener);
  54. }
  55. }
  56. Moderator.prototype.isExternalAuthEnabled = function () {
  57. return this.externalAuthEnabled;
  58. };
  59. Moderator.prototype.isSipGatewayEnabled = function () {
  60. return this.sipGatewayEnabled;
  61. };
  62. Moderator.prototype.onMucMemberLeft = function (jid) {
  63. console.info("Someone left is it focus ? " + jid);
  64. var resource = Strophe.getResourceFromJid(jid);
  65. if (resource === 'focus' && !this.xmppService.sessionTerminated) {
  66. console.info(
  67. "Focus has left the room - leaving conference");
  68. //hangUp();
  69. // We'd rather reload to have everything re-initialized
  70. //FIXME: show some message before reload
  71. this.eventEmitter.emit(XMPPEvents.FOCUS_LEFT);
  72. }
  73. };
  74. Moderator.prototype.setFocusUserJid = function (focusJid) {
  75. if (!this.focusUserJid) {
  76. this.focusUserJid = focusJid;
  77. console.info("Focus jid set to: " + this.focusUserJid);
  78. }
  79. };
  80. Moderator.prototype.getFocusUserJid = function () {
  81. return this.focusUserJid;
  82. };
  83. Moderator.prototype.getFocusComponent = function () {
  84. // Get focus component address
  85. var focusComponent = this.xmppService.options.hosts.focus;
  86. // If not specified use default: 'focus.domain'
  87. if (!focusComponent) {
  88. focusComponent = 'focus.' + this.xmppService.options.hosts.domain;
  89. }
  90. return focusComponent;
  91. };
  92. Moderator.prototype.createConferenceIq = function () {
  93. // Generate create conference IQ
  94. var elem = $iq({to: this.getFocusComponent(), type: 'set'});
  95. // Session Id used for authentication
  96. var sessionId = localStorage.getItem('sessionId');
  97. var machineUID = this.settings.getSettings().uid;
  98. console.info(
  99. "Session ID: " + sessionId + " machine UID: " + machineUID);
  100. elem.c('conference', {
  101. xmlns: 'http://jitsi.org/protocol/focus',
  102. room: this.roomName,
  103. 'machine-uid': machineUID
  104. });
  105. if (sessionId) {
  106. elem.attrs({ 'session-id': sessionId});
  107. }
  108. if (this.xmppService.options.hosts.bridge !== undefined) {
  109. elem.c(
  110. 'property',
  111. {name: 'bridge',value: this.xmppService.options.hosts.bridge})
  112. .up();
  113. }
  114. // Tell the focus we have Jigasi configured
  115. if (this.xmppService.options.hosts.call_control !== undefined) {
  116. elem.c(
  117. 'property',
  118. {name: 'call_control',value: this.xmppService.options.hosts.call_control})
  119. .up();
  120. }
  121. if (this.xmppService.options.channelLastN !== undefined) {
  122. elem.c(
  123. 'property',
  124. {name: 'channelLastN',value: this.xmppService.options.channelLastN})
  125. .up();
  126. }
  127. if (this.xmppService.options.adaptiveLastN !== undefined) {
  128. elem.c(
  129. 'property',
  130. {name: 'adaptiveLastN',value: this.xmppService.options.adaptiveLastN})
  131. .up();
  132. }
  133. if (this.xmppService.options.adaptiveSimulcast !== undefined) {
  134. elem.c(
  135. 'property',
  136. {name: 'adaptiveSimulcast',value: this.xmppService.options.adaptiveSimulcast})
  137. .up();
  138. }
  139. if (this.xmppService.options.openSctp !== undefined) {
  140. elem.c(
  141. 'property',
  142. {name: 'openSctp',value: this.xmppService.options.openSctp})
  143. .up();
  144. }
  145. if(this.xmppService.options.startAudioMuted !== undefined)
  146. {
  147. elem.c(
  148. 'property',
  149. {name: 'startAudioMuted',value: this.xmppService.options.startAudioMuted})
  150. .up();
  151. }
  152. if(this.xmppService.options.startVideoMuted !== undefined)
  153. {
  154. elem.c(
  155. 'property',
  156. {name: 'startVideoMuted',value: this.xmppService.options.startVideoMuted})
  157. .up();
  158. }
  159. elem.c(
  160. 'property',
  161. {name: 'simulcastMode',value: 'rewriting'})
  162. .up();
  163. elem.up();
  164. return elem;
  165. };
  166. Moderator.prototype.parseSessionId = function (resultIq) {
  167. var sessionId = $(resultIq).find('conference').attr('session-id');
  168. if (sessionId) {
  169. console.info('Received sessionId: ' + sessionId);
  170. localStorage.setItem('sessionId', sessionId);
  171. }
  172. };
  173. Moderator.prototype.parseConfigOptions = function (resultIq) {
  174. this.setFocusUserJid(
  175. $(resultIq).find('conference').attr('focusjid'));
  176. var authenticationEnabled
  177. = $(resultIq).find(
  178. '>conference>property' +
  179. '[name=\'authentication\'][value=\'true\']').length > 0;
  180. console.info("Authentication enabled: " + authenticationEnabled);
  181. this.externalAuthEnabled = $(resultIq).find(
  182. '>conference>property' +
  183. '[name=\'externalAuth\'][value=\'true\']').length > 0;
  184. console.info('External authentication enabled: ' + this.externalAuthEnabled);
  185. if (!this.externalAuthEnabled) {
  186. // We expect to receive sessionId in 'internal' authentication mode
  187. this.parseSessionId(resultIq);
  188. }
  189. var authIdentity = $(resultIq).find('>conference').attr('identity');
  190. this.eventEmitter.emit(AuthenticationEvents.IDENTITY_UPDATED,
  191. authenticationEnabled, authIdentity);
  192. // Check if focus has auto-detected Jigasi component(this will be also
  193. // included if we have passed our host from the config)
  194. if ($(resultIq).find(
  195. '>conference>property' +
  196. '[name=\'sipGatewayEnabled\'][value=\'true\']').length) {
  197. this.sipGatewayEnabled = true;
  198. }
  199. console.info("Sip gateway enabled: " + this.sipGatewayEnabled);
  200. };
  201. // FIXME = we need to show the fact that we're waiting for the focus
  202. // to the user(or that focus is not available)
  203. Moderator.prototype.allocateConferenceFocus = function ( callback) {
  204. // Try to use focus user JID from the config
  205. this.setFocusUserJid(this.xmppService.options.focusUserJid);
  206. // Send create conference IQ
  207. var iq = this.createConferenceIq();
  208. var self = this;
  209. this.connection.sendIQ(
  210. iq,
  211. function (result) {
  212. // Setup config options
  213. self.parseConfigOptions(result);
  214. if ('true' === $(result).find('conference').attr('ready')) {
  215. // Reset both timers
  216. self.getNextTimeout(true);
  217. self.getNextErrorTimeout(true);
  218. // Exec callback
  219. callback();
  220. } else {
  221. var waitMs = self.getNextTimeout();
  222. console.info("Waiting for the focus... " + waitMs);
  223. // Reset error timeout
  224. self.getNextErrorTimeout(true);
  225. window.setTimeout(
  226. function () {
  227. self.allocateConferenceFocus(callback);
  228. }, waitMs);
  229. }
  230. },
  231. function (error) {
  232. // Invalid session ? remove and try again
  233. // without session ID to get a new one
  234. var invalidSession
  235. = $(error).find('>error>session-invalid').length;
  236. if (invalidSession) {
  237. console.info("Session expired! - removing");
  238. localStorage.removeItem("sessionId");
  239. }
  240. if ($(error).find('>error>graceful-shutdown').length) {
  241. self.eventEmitter.emit(XMPPEvents.GRACEFUL_SHUTDOWN);
  242. return;
  243. }
  244. // Check for error returned by the reservation system
  245. var reservationErr = $(error).find('>error>reservation-error');
  246. if (reservationErr.length) {
  247. // Trigger error event
  248. var errorCode = reservationErr.attr('error-code');
  249. var errorMsg;
  250. if ($(error).find('>error>text')) {
  251. errorMsg = $(error).find('>error>text').text();
  252. }
  253. self.eventEmitter.emit(
  254. XMPPEvents.RESERVATION_ERROR, errorCode, errorMsg);
  255. return;
  256. }
  257. // Not authorized to create new room
  258. if ($(error).find('>error>not-authorized').length) {
  259. console.warn("Unauthorized to start the conference", error);
  260. var toDomain
  261. = Strophe.getDomainFromJid(error.getAttribute('to'));
  262. if (toDomain !== this.xmppService.options.hosts.anonymousdomain) {
  263. //FIXME: "is external" should come either from
  264. // the focus or config.js
  265. self.externalAuthEnabled = true;
  266. }
  267. self.eventEmitter.emit(
  268. XMPPEvents.AUTHENTICATION_REQUIRED,
  269. function () {
  270. self.allocateConferenceFocus(
  271. callback);
  272. });
  273. return;
  274. }
  275. var waitMs = self.getNextErrorTimeout();
  276. console.error("Focus error, retry after " + waitMs, error);
  277. // Show message
  278. var focusComponent = self.getFocusComponent();
  279. var retrySec = waitMs / 1000;
  280. //FIXME: message is duplicated ?
  281. // Do not show in case of session invalid
  282. // which means just a retry
  283. if (!invalidSession) {
  284. self.eventEmitter.emit(XMPPEvents.FOCUS_DISCONNECTED,
  285. focusComponent, retrySec);
  286. }
  287. // Reset response timeout
  288. self.getNextTimeout(true);
  289. window.setTimeout(
  290. function () {
  291. self.allocateConferenceFocus(callback);
  292. }, waitMs);
  293. }
  294. );
  295. };
  296. Moderator.prototype.getLoginUrl = function (urlCallback) {
  297. var iq = $iq({to: this.getFocusComponent(), type: 'get'});
  298. iq.c('login-url', {
  299. xmlns: 'http://jitsi.org/protocol/focus',
  300. room: this.roomName,
  301. 'machine-uid': this.settings.getSettings().uid
  302. });
  303. this.connection.sendIQ(
  304. iq,
  305. function (result) {
  306. var url = $(result).find('login-url').attr('url');
  307. url = url = decodeURIComponent(url);
  308. if (url) {
  309. console.info("Got auth url: " + url);
  310. urlCallback(url);
  311. } else {
  312. console.error(
  313. "Failed to get auth url from the focus", result);
  314. }
  315. },
  316. function (error) {
  317. console.error("Get auth url error", error);
  318. }
  319. );
  320. };
  321. Moderator.prototype.getPopupLoginUrl = function (urlCallback) {
  322. var iq = $iq({to: this.getFocusComponent(), type: 'get'});
  323. iq.c('login-url', {
  324. xmlns: 'http://jitsi.org/protocol/focus',
  325. room: this.roomName,
  326. 'machine-uid': this.settings.getSettings().uid,
  327. popup: true
  328. });
  329. this.connection.sendIQ(
  330. iq,
  331. function (result) {
  332. var url = $(result).find('login-url').attr('url');
  333. url = url = decodeURIComponent(url);
  334. if (url) {
  335. console.info("Got POPUP auth url: " + url);
  336. urlCallback(url);
  337. } else {
  338. console.error(
  339. "Failed to get POPUP auth url from the focus", result);
  340. }
  341. },
  342. function (error) {
  343. console.error('Get POPUP auth url error', error);
  344. }
  345. );
  346. };
  347. Moderator.prototype.logout = function (callback) {
  348. var iq = $iq({to: this.getFocusComponent(), type: 'set'});
  349. var sessionId = localStorage.getItem('sessionId');
  350. if (!sessionId) {
  351. callback();
  352. return;
  353. }
  354. iq.c('logout', {
  355. xmlns: 'http://jitsi.org/protocol/focus',
  356. 'session-id': sessionId
  357. });
  358. this.connection.sendIQ(
  359. iq,
  360. function (result) {
  361. var logoutUrl = $(result).find('logout').attr('logout-url');
  362. if (logoutUrl) {
  363. logoutUrl = decodeURIComponent(logoutUrl);
  364. }
  365. console.info("Log out OK, url: " + logoutUrl, result);
  366. localStorage.removeItem('sessionId');
  367. callback(logoutUrl);
  368. },
  369. function (error) {
  370. console.error("Logout error", error);
  371. }
  372. );
  373. };
  374. module.exports = Moderator;