選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

xmpp.js 22KB

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