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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  1. /* global $, APP, config, Strophe*/
  2. var Moderator = require("./moderator");
  3. var EventEmitter = require("events");
  4. var Recording = require("./recording");
  5. var SDP = require("./SDP");
  6. var Settings = require("../settings/Settings");
  7. var Pako = require("pako");
  8. var StreamEventTypes = require("../../service/RTC/StreamEventTypes");
  9. var RTCEvents = require("../../service/RTC/RTCEvents");
  10. var XMPPEvents = require("../../service/xmpp/XMPPEvents");
  11. var retry = require('retry');
  12. var eventEmitter = new EventEmitter();
  13. var connection = null;
  14. var authenticatedUser = false;
  15. function connect(jid, password) {
  16. var faultTolerantConnect = retry.operation({
  17. retries: 3
  18. });
  19. // fault tolerant connect
  20. faultTolerantConnect.attempt(function () {
  21. connection = XMPP.createConnection();
  22. Moderator.setConnection(connection);
  23. connection.jingle.pc_constraints = APP.RTC.getPCConstraints();
  24. if (config.useIPv6) {
  25. // https://code.google.com/p/webrtc/issues/detail?id=2828
  26. if (!connection.jingle.pc_constraints.optional)
  27. connection.jingle.pc_constraints.optional = [];
  28. connection.jingle.pc_constraints.optional.push({googIPv6: true});
  29. }
  30. // Include user info in MUC presence
  31. var settings = Settings.getSettings();
  32. if (settings.email) {
  33. connection.emuc.addEmailToPresence(settings.email);
  34. }
  35. if (settings.uid) {
  36. connection.emuc.addUserIdToPresence(settings.uid);
  37. }
  38. if (settings.displayName) {
  39. connection.emuc.addDisplayNameToPresence(settings.displayName);
  40. }
  41. // connection.connect() starts the connection process.
  42. //
  43. // As the connection process proceeds, the user supplied callback will
  44. // be triggered multiple times with status updates. The callback should
  45. // take two arguments - the status code and the error condition.
  46. //
  47. // The status code will be one of the values in the Strophe.Status
  48. // constants. The error condition will be one of the conditions defined
  49. // in RFC 3920 or the condition ‘strophe-parsererror’.
  50. //
  51. // The Parameters wait, hold and route are optional and only relevant
  52. // for BOSH connections. Please see XEP 124 for a more detailed
  53. // explanation of the optional parameters.
  54. //
  55. // Connection status constants for use by the connection handler
  56. // callback.
  57. //
  58. // Status.ERROR - An error has occurred (websockets specific)
  59. // Status.CONNECTING - The connection is currently being made
  60. // Status.CONNFAIL - The connection attempt failed
  61. // Status.AUTHENTICATING - The connection is authenticating
  62. // Status.AUTHFAIL - The authentication attempt failed
  63. // Status.CONNECTED - The connection has succeeded
  64. // Status.DISCONNECTED - The connection has been terminated
  65. // Status.DISCONNECTING - The connection is currently being terminated
  66. // Status.ATTACHED - The connection has been attached
  67. var anonymousConnectionFailed = false;
  68. var connectionFailed = false;
  69. var lastErrorMsg;
  70. connection.connect(jid, password, function (status, msg) {
  71. console.log('Strophe status changed to',
  72. Strophe.getStatusString(status), msg);
  73. if (status === Strophe.Status.CONNECTED) {
  74. if (config.useStunTurn) {
  75. connection.jingle.getStunAndTurnCredentials();
  76. }
  77. console.info("My Jabber ID: " + connection.jid);
  78. if (password)
  79. authenticatedUser = true;
  80. maybeDoJoin();
  81. } else if (status === Strophe.Status.CONNFAIL) {
  82. if (msg === 'x-strophe-bad-non-anon-jid') {
  83. anonymousConnectionFailed = true;
  84. } else {
  85. connectionFailed = true;
  86. }
  87. lastErrorMsg = msg;
  88. } else if (status === Strophe.Status.DISCONNECTED) {
  89. if (anonymousConnectionFailed) {
  90. // prompt user for username and password
  91. XMPP.promptLogin();
  92. } else {
  93. // Strophe already has built-in HTTP/BOSH error handling and
  94. // request retry logic. Requests are resent automatically
  95. // until their error count reaches 5. Strophe.js disconnects
  96. // if the error count is > 5. We are not replicating this
  97. // here.
  98. //
  99. // The "problem" is that failed HTTP/BOSH requests don't
  100. // trigger a callback with a status update, so when a
  101. // callback with status Strophe.Status.DISCONNECTED arrives,
  102. // we can't be sure if it's a graceful disconnect or if it's
  103. // triggered by some HTTP/BOSH error.
  104. //
  105. // But that's a minor issue in Jitsi Meet as we never
  106. // disconnect anyway, not even when the user closes the
  107. // browser window (which is kind of wrong, but the point is
  108. // that we should never ever get disconnected).
  109. //
  110. // On the other hand, failed connections due to XMPP layer
  111. // errors, trigger a callback with status Strophe.Status.CONNFAIL.
  112. //
  113. // Here we implement retry logic for failed connections due
  114. // to XMPP layer errors and we display an error to the user
  115. // if we get disconnected from the XMPP server permanently.
  116. // If the connection failed, retry.
  117. if (connectionFailed &&
  118. faultTolerantConnect.retry("connection-failed")) {
  119. return;
  120. }
  121. // If we failed to connect to the XMPP server, fire an event
  122. // to let all the interested module now about it.
  123. eventEmitter.emit(XMPPEvents.CONNECTION_FAILED,
  124. msg ? msg : lastErrorMsg);
  125. }
  126. } else if (status === Strophe.Status.AUTHFAIL) {
  127. // wrong password or username, prompt user
  128. XMPP.promptLogin();
  129. }
  130. });
  131. });
  132. }
  133. function maybeDoJoin() {
  134. if (connection && connection.connected &&
  135. Strophe.getResourceFromJid(connection.jid) &&
  136. (APP.RTC.localAudio || APP.RTC.localVideo)) {
  137. // .connected is true while connecting?
  138. doJoin();
  139. }
  140. }
  141. function doJoin() {
  142. eventEmitter.emit(XMPPEvents.READY_TO_JOIN);
  143. }
  144. function initStrophePlugins()
  145. {
  146. require("./strophe.emuc")(XMPP, eventEmitter);
  147. require("./strophe.jingle")(XMPP, eventEmitter);
  148. require("./strophe.moderate")(XMPP, eventEmitter);
  149. require("./strophe.util")();
  150. require("./strophe.rayo")();
  151. require("./strophe.logger")();
  152. }
  153. /**
  154. * If given <tt>localStream</tt> is video one this method will advertise it's
  155. * video type in MUC presence.
  156. * @param localStream new or modified <tt>LocalStream</tt>.
  157. */
  158. function broadcastLocalVideoType(localStream) {
  159. if (localStream.videoType)
  160. XMPP.addToPresence('videoType', localStream.videoType);
  161. }
  162. function registerListeners() {
  163. APP.RTC.addStreamListener(
  164. function (localStream) {
  165. maybeDoJoin();
  166. broadcastLocalVideoType(localStream);
  167. },
  168. StreamEventTypes.EVENT_TYPE_LOCAL_CREATED
  169. );
  170. APP.RTC.addStreamListener(
  171. broadcastLocalVideoType,
  172. StreamEventTypes.EVENT_TYPE_LOCAL_CHANGED
  173. );
  174. APP.RTC.addListener(RTCEvents.AVAILABLE_DEVICES_CHANGED, function (devices) {
  175. XMPP.addToPresence("devices", devices);
  176. });
  177. }
  178. var unload = (function () {
  179. var unloaded = false;
  180. return function () {
  181. if (unloaded) { return; }
  182. unloaded = true;
  183. if (connection && connection.connected) {
  184. // ensure signout
  185. $.ajax({
  186. type: 'POST',
  187. url: config.bosh,
  188. async: false,
  189. cache: false,
  190. contentType: 'application/xml',
  191. data: "<body rid='" +
  192. (connection.rid || connection._proto.rid) +
  193. "' xmlns='http://jabber.org/protocol/httpbind' sid='" +
  194. (connection.sid || connection._proto.sid) +
  195. "' type='terminate'>" +
  196. "<presence xmlns='jabber:client' type='unavailable'/>" +
  197. "</body>",
  198. success: function (data) {
  199. console.log('signed out');
  200. console.log(data);
  201. },
  202. error: function (XMLHttpRequest, textStatus, errorThrown) {
  203. console.log('signout error',
  204. textStatus + ' (' + errorThrown + ')');
  205. }
  206. });
  207. }
  208. XMPP.disposeConference(true);
  209. };
  210. })();
  211. function setupEvents() {
  212. // In recent versions of FF the 'beforeunload' event is not fired when the
  213. // window or the tab is closed. It is only fired when we leave the page
  214. // (change URL). If this participant doesn't unload properly, then it
  215. // becomes a ghost for the rest of the participants that stay in the
  216. // conference. Thankfully handling the 'unload' event in addition to the
  217. // 'beforeunload' event seems to guarantee the execution of the 'unload'
  218. // method at least once.
  219. //
  220. // The 'unload' method can safely be run multiple times, it will actually do
  221. // something only the first time that it's run, so we're don't have to worry
  222. // about browsers that fire both events.
  223. $(window).bind('beforeunload', unload);
  224. $(window).bind('unload', unload);
  225. }
  226. var XMPP = {
  227. getConnection: function(){ return connection; },
  228. sessionTerminated: false,
  229. /**
  230. * XMPP connection status
  231. */
  232. Status: Strophe.Status,
  233. /**
  234. * Remembers if we were muted by the focus.
  235. * @type {boolean}
  236. */
  237. forceMuted: false,
  238. start: function () {
  239. setupEvents();
  240. initStrophePlugins();
  241. registerListeners();
  242. Moderator.init(this, eventEmitter);
  243. Recording.init();
  244. var configDomain = config.hosts.anonymousdomain || config.hosts.domain;
  245. // Force authenticated domain if room is appended with '?login=true'
  246. if (config.hosts.anonymousdomain &&
  247. window.location.search.indexOf("login=true") !== -1) {
  248. configDomain = config.hosts.domain;
  249. }
  250. var jid = configDomain || window.location.hostname;
  251. connect(jid, null);
  252. },
  253. createConnection: function () {
  254. var bosh = config.bosh || '/http-bind';
  255. return new Strophe.Connection(bosh);
  256. },
  257. getStatusString: function (status) {
  258. return Strophe.getStatusString(status);
  259. },
  260. promptLogin: function () {
  261. eventEmitter.emit(XMPPEvents.PROMPT_FOR_LOGIN);
  262. },
  263. joinRoom: function(roomName, useNicks, nick) {
  264. var roomjid = roomName;
  265. if (useNicks) {
  266. if (nick) {
  267. roomjid += '/' + nick;
  268. } else {
  269. roomjid += '/' + Strophe.getNodeFromJid(connection.jid);
  270. }
  271. } else {
  272. var tmpJid = Strophe.getNodeFromJid(connection.jid);
  273. if(!authenticatedUser)
  274. tmpJid = tmpJid.substr(0, 8);
  275. roomjid += '/' + tmpJid;
  276. }
  277. connection.emuc.doJoin(roomjid);
  278. },
  279. myJid: function () {
  280. if(!connection)
  281. return null;
  282. return connection.emuc.myroomjid;
  283. },
  284. myResource: function () {
  285. if(!connection || ! connection.emuc.myroomjid)
  286. return null;
  287. return Strophe.getResourceFromJid(connection.emuc.myroomjid);
  288. },
  289. disposeConference: function (onUnload) {
  290. var handler = connection.jingle.activecall;
  291. if (handler && handler.peerconnection) {
  292. // FIXME: probably removing streams is not required and close() should
  293. // be enough
  294. if (APP.RTC.localAudio) {
  295. handler.peerconnection.removeStream(
  296. APP.RTC.localAudio.getOriginalStream(), onUnload);
  297. }
  298. if (APP.RTC.localVideo) {
  299. handler.peerconnection.removeStream(
  300. APP.RTC.localVideo.getOriginalStream(), onUnload);
  301. }
  302. handler.peerconnection.close();
  303. }
  304. eventEmitter.emit(XMPPEvents.DISPOSE_CONFERENCE, onUnload);
  305. connection.jingle.activecall = null;
  306. if (!onUnload) {
  307. this.sessionTerminated = true;
  308. connection.emuc.doLeave();
  309. }
  310. },
  311. addListener: function(type, listener) {
  312. eventEmitter.on(type, listener);
  313. },
  314. removeListener: function (type, listener) {
  315. eventEmitter.removeListener(type, listener);
  316. },
  317. allocateConferenceFocus: function(roomName, callback) {
  318. Moderator.allocateConferenceFocus(roomName, callback);
  319. },
  320. getLoginUrl: function (roomName, callback) {
  321. Moderator.getLoginUrl(roomName, callback);
  322. },
  323. getPopupLoginUrl: function (roomName, callback) {
  324. Moderator.getPopupLoginUrl(roomName, callback);
  325. },
  326. isModerator: function () {
  327. return Moderator.isModerator();
  328. },
  329. isSipGatewayEnabled: function () {
  330. return Moderator.isSipGatewayEnabled();
  331. },
  332. isExternalAuthEnabled: function () {
  333. return Moderator.isExternalAuthEnabled();
  334. },
  335. isConferenceInProgress: function () {
  336. return connection && connection.jingle.activecall &&
  337. connection.jingle.activecall.peerconnection;
  338. },
  339. switchStreams: function (stream, oldStream, callback, isAudio) {
  340. if (this.isConferenceInProgress()) {
  341. // FIXME: will block switchInProgress on true value in case of exception
  342. connection.jingle.activecall.switchStreams(stream, oldStream, callback, isAudio);
  343. } else {
  344. // We are done immediately
  345. console.warn("No conference handler or conference not started yet");
  346. callback();
  347. }
  348. },
  349. sendVideoInfoPresence: function (mute) {
  350. if(!connection)
  351. return;
  352. connection.emuc.addVideoInfoToPresence(mute);
  353. connection.emuc.sendPresence();
  354. },
  355. setVideoMute: function (mute, callback, options) {
  356. if(!connection)
  357. return;
  358. var self = this;
  359. var localCallback = function (mute) {
  360. self.sendVideoInfoPresence(mute);
  361. return callback(mute);
  362. };
  363. if(connection.jingle.activecall)
  364. {
  365. connection.jingle.activecall.setVideoMute(
  366. mute, localCallback, options);
  367. }
  368. else {
  369. localCallback(mute);
  370. }
  371. },
  372. setAudioMute: function (mute, callback) {
  373. if (!(connection && APP.RTC.localAudio)) {
  374. return false;
  375. }
  376. if (this.forceMuted && !mute) {
  377. console.info("Asking focus for unmute");
  378. connection.moderate.setMute(connection.emuc.myroomjid, mute);
  379. // FIXME: wait for result before resetting muted status
  380. this.forceMuted = false;
  381. }
  382. if (mute == APP.RTC.localAudio.isMuted()) {
  383. // Nothing to do
  384. return true;
  385. }
  386. APP.RTC.localAudio.setMute(mute);
  387. this.sendAudioInfoPresence(mute, callback);
  388. return true;
  389. },
  390. sendAudioInfoPresence: function(mute, callback) {
  391. if(connection) {
  392. connection.emuc.addAudioInfoToPresence(mute);
  393. connection.emuc.sendPresence();
  394. }
  395. callback();
  396. return true;
  397. },
  398. toggleRecording: function (tokenEmptyCallback,
  399. recordingStateChangeCallback) {
  400. Recording.toggleRecording(tokenEmptyCallback,
  401. recordingStateChangeCallback, connection);
  402. },
  403. addToPresence: function (name, value, dontSend) {
  404. switch (name) {
  405. case "displayName":
  406. connection.emuc.addDisplayNameToPresence(value);
  407. break;
  408. case "prezi":
  409. connection.emuc.addPreziToPresence(value, 0);
  410. break;
  411. case "preziSlide":
  412. connection.emuc.addCurrentSlideToPresence(value);
  413. break;
  414. case "connectionQuality":
  415. connection.emuc.addConnectionInfoToPresence(value);
  416. break;
  417. case "email":
  418. connection.emuc.addEmailToPresence(value);
  419. break;
  420. case "devices":
  421. connection.emuc.addDevicesToPresence(value);
  422. break;
  423. case "videoType":
  424. connection.emuc.addVideoTypeToPresence(value);
  425. break;
  426. case "startMuted":
  427. if(!Moderator.isModerator())
  428. return;
  429. connection.emuc.addStartMutedToPresence(value[0],
  430. value[1]);
  431. break;
  432. default :
  433. console.log("Unknown tag for presence: " + name);
  434. return;
  435. }
  436. if (!dontSend)
  437. connection.emuc.sendPresence();
  438. },
  439. /**
  440. * Sends 'data' as a log message to the focus. Returns true iff a message
  441. * was sent.
  442. * @param data
  443. * @returns {boolean} true iff a message was sent.
  444. */
  445. sendLogs: function (data) {
  446. if(!connection.emuc.focusMucJid)
  447. return false;
  448. var deflate = true;
  449. var content = JSON.stringify(data);
  450. if (deflate) {
  451. content = String.fromCharCode.apply(null, Pako.deflateRaw(content));
  452. }
  453. content = Base64.encode(content);
  454. // XEP-0337-ish
  455. var message = $msg({to: connection.emuc.focusMucJid, type: 'normal'});
  456. message.c('log', { xmlns: 'urn:xmpp:eventlog',
  457. id: 'PeerConnectionStats'});
  458. message.c('message').t(content).up();
  459. if (deflate) {
  460. message.c('tag', {name: "deflated", value: "true"}).up();
  461. }
  462. message.up();
  463. connection.send(message);
  464. return true;
  465. },
  466. // Gets the logs from strophe.jingle.
  467. getJingleLog: function () {
  468. return connection.jingle ? connection.jingle.getLog() : {};
  469. },
  470. // Gets the logs from strophe.
  471. getXmppLog: function () {
  472. return connection.logger ? connection.logger.log : null;
  473. },
  474. getPrezi: function () {
  475. return connection.emuc.getPrezi(this.myJid());
  476. },
  477. removePreziFromPresence: function () {
  478. connection.emuc.removePreziFromPresence();
  479. connection.emuc.sendPresence();
  480. },
  481. sendChatMessage: function (message, nickname) {
  482. connection.emuc.sendMessage(message, nickname);
  483. },
  484. setSubject: function (topic) {
  485. connection.emuc.setSubject(topic);
  486. },
  487. lockRoom: function (key, onSuccess, onError, onNotSupported) {
  488. connection.emuc.lockRoom(key, onSuccess, onError, onNotSupported);
  489. },
  490. dial: function (to, from, roomName,roomPass) {
  491. connection.rayo.dial(to, from, roomName,roomPass);
  492. },
  493. setMute: function (jid, mute) {
  494. connection.moderate.setMute(jid, mute);
  495. },
  496. eject: function (jid) {
  497. connection.moderate.eject(jid);
  498. },
  499. logout: function (callback) {
  500. Moderator.logout(callback);
  501. },
  502. findJidFromResource: function (resource) {
  503. return connection.emuc.findJidFromResource(resource);
  504. },
  505. getMembers: function () {
  506. return connection.emuc.members;
  507. },
  508. getJidFromSSRC: function (ssrc) {
  509. if (!this.isConferenceInProgress())
  510. return null;
  511. return connection.jingle.activecall.getSsrcOwner(ssrc);
  512. },
  513. // Returns true iff we have joined the MUC.
  514. isMUCJoined: function () {
  515. return connection.emuc.joined;
  516. },
  517. getSessions: function () {
  518. return connection.jingle.sessions;
  519. },
  520. removeStream: function (stream) {
  521. if (!this.isConferenceInProgress())
  522. return;
  523. connection.jingle.activecall.peerconnection.removeStream(stream);
  524. }
  525. };
  526. module.exports = XMPP;