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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. /* global $, JitsiMeetJS, config, interfaceConfig */
  2. /* application specific logic */
  3. import "babel-polyfill";
  4. import "jquery";
  5. import "jquery-ui";
  6. import "strophe";
  7. import "strophe-disco";
  8. import "strophe-caps";
  9. import "tooltip";
  10. import "popover";
  11. import "jQuery-Impromptu";
  12. import "autosize";
  13. window.toastr = require("toastr");
  14. import RoomnameGenerator from './modules/util/RoomnameGenerator';
  15. import CQEvents from './service/connectionquality/CQEvents';
  16. import UIEvents from './service/UI/UIEvents';
  17. const Commands = {
  18. CONNECTION_QUALITY: "connectionQuality",
  19. EMAIL: "email"
  20. };
  21. function buildRoomName () {
  22. let path = window.location.pathname;
  23. let roomName;
  24. // determinde the room node from the url
  25. // TODO: just the roomnode or the whole bare jid?
  26. if (config.getroomnode && typeof config.getroomnode === 'function') {
  27. // custom function might be responsible for doing the pushstate
  28. roomName = config.getroomnode(path);
  29. } else {
  30. /* fall back to default strategy
  31. * this is making assumptions about how the URL->room mapping happens.
  32. * It currently assumes deployment at root, with a rewrite like the
  33. * following one (for nginx):
  34. location ~ ^/([a-zA-Z0-9]+)$ {
  35. rewrite ^/(.*)$ / break;
  36. }
  37. */
  38. if (path.length > 1) {
  39. roomName = path.substr(1).toLowerCase();
  40. } else {
  41. let word = RoomnameGenerator.generateRoomWithoutSeparator();
  42. roomName = word.toLowerCase();
  43. window.history.pushState(
  44. 'VideoChat', 'Room: ' + word, window.location.pathname + word
  45. );
  46. }
  47. }
  48. return roomName;
  49. }
  50. const APP = {
  51. init () {
  52. JitsiMeetJS.init();
  53. JitsiMeetJS.setLogLevel(JitsiMeetJS.logLevels.TRACE);
  54. let roomName = buildRoomName();
  55. this.conference = {
  56. roomName,
  57. localId: undefined,
  58. isModerator: false,
  59. membersCount: 0,
  60. audioMuted: false,
  61. videoMuted: false,
  62. isLocalId (id) {
  63. return this.localId === id;
  64. },
  65. muteAudio (mute) {
  66. APP.UI.eventEmitter.emit(UIEvents.AUDIO_MUTED, mute);
  67. },
  68. toggleAudioMuted () {
  69. this.muteAudio(!this.audioMuted);
  70. },
  71. muteVideo (mute) {
  72. APP.UI.eventEmitter.emit(UIEvents.VIDEO_MUTED, mute);
  73. },
  74. toggleVideoMuted () {
  75. this.muteVideo(!this.videoMuted);
  76. }
  77. };
  78. this.UI = require("./modules/UI/UI");
  79. this.API = require("./modules/API/API");
  80. this.connectionquality =
  81. require("./modules/connectionquality/connectionquality");
  82. this.statistics = require("./modules/statistics/statistics");
  83. this.desktopsharing =
  84. require("./modules/desktopsharing/desktopsharing");
  85. this.keyboardshortcut =
  86. require("./modules/keyboardshortcut/keyboardshortcut");
  87. this.translation = require("./modules/translation/translation");
  88. this.settings = require("./modules/settings/Settings");
  89. this.configFetch = require("./modules/config/HttpConfigFetch");
  90. }
  91. };
  92. var ConnectionEvents = JitsiMeetJS.events.connection;
  93. var ConnectionErrors = JitsiMeetJS.errors.connection;
  94. function connect() {
  95. var connection = new JitsiMeetJS.JitsiConnection(null, null, {
  96. hosts: config.hosts,
  97. bosh: config.bosh,
  98. clientNode: config.clientNode
  99. });
  100. return new Promise(function (resolve, reject) {
  101. var handlers = {};
  102. var unsubscribe = function () {
  103. Object.keys(handlers).forEach(function (event) {
  104. connection.removeEventListener(event, handlers[event]);
  105. });
  106. };
  107. handlers[ConnectionEvents.CONNECTION_ESTABLISHED] = function () {
  108. console.log('CONNECTED');
  109. unsubscribe();
  110. resolve(connection);
  111. };
  112. var listenForFailure = function (event) {
  113. handlers[event] = function (...args) {
  114. console.error(`CONNECTION FAILED: ${event}`, ...args);
  115. unsubscribe();
  116. reject([event, ...args]);
  117. };
  118. };
  119. listenForFailure(ConnectionEvents.CONNECTION_FAILED);
  120. listenForFailure(ConnectionErrors.PASSWORD_REQUIRED);
  121. listenForFailure(ConnectionErrors.CONNECTION_ERROR);
  122. listenForFailure(ConnectionErrors.OTHER_ERRORS);
  123. // install event listeners
  124. Object.keys(handlers).forEach(function (event) {
  125. connection.addEventListener(event, handlers[event]);
  126. });
  127. connection.connect();
  128. }).catch(function (err) {
  129. if (err[0] === ConnectionErrors.PASSWORD_REQUIRED) {
  130. // FIXME ask for password and try again
  131. return connect();
  132. }
  133. console.error('FAILED TO CONNECT', err);
  134. APP.UI.notifyConnectionFailed(err[1]);
  135. throw new Error(err[0]);
  136. });
  137. }
  138. var ConferenceEvents = JitsiMeetJS.events.conference;
  139. var ConferenceErrors = JitsiMeetJS.errors.conference;
  140. function initConference(connection, roomName) {
  141. var room = connection.initJitsiConference(roomName, {
  142. openSctp: config.openSctp,
  143. disableAudioLevels: config.disableAudioLevels
  144. });
  145. var users = {};
  146. var localTracks = [];
  147. APP.conference.localId = room.myUserId();
  148. Object.defineProperty(APP.conference, "membersCount", {
  149. get: function () {
  150. return Object.keys(users).length; // FIXME maybe +1?
  151. }
  152. });
  153. function getDisplayName(id) {
  154. if (APP.conference.isLocalId(id)) {
  155. return APP.settings.getDisplayName();
  156. }
  157. var user = users[id];
  158. if (user && user.displayName) {
  159. return user.displayName;
  160. }
  161. }
  162. room.on(ConferenceEvents.USER_JOINED, function (id) {
  163. users[id] = {
  164. displayName: undefined,
  165. tracks: []
  166. };
  167. // FIXME email???
  168. APP.UI.addUser(id);
  169. });
  170. room.on(ConferenceEvents.USER_LEFT, function (id) {
  171. delete users[id];
  172. APP.UI.removeUser(id);
  173. });
  174. room.on(ConferenceEvents.TRACK_MUTE_CHANGED, function (track) {
  175. // FIXME handle mute
  176. });
  177. room.on(ConferenceEvents.TRACK_AUDIO_LEVEL_CHANGED, function (id, lvl) {
  178. APP.UI.setAudioLevel(id, lvl);
  179. });
  180. APP.UI.addListener(UIEvents.AUDIO_MUTED, function (muted) {
  181. // FIXME mute or unmute
  182. APP.UI.setAudioMuted(muted);
  183. APP.conference.audioMuted = muted;
  184. });
  185. APP.UI.addListener(UIEvents.VIDEO_MUTED, function (muted) {
  186. // FIXME mute or unmute
  187. APP.UI.setVideoMuted(muted);
  188. APP.conference.videoMuted = muted;
  189. });
  190. room.on(ConferenceEvents.IN_LAST_N_CHANGED, function (inLastN) {
  191. if (config.muteLocalVideoIfNotInLastN) {
  192. // TODO mute or unmute if required
  193. // mark video on UI
  194. // APP.UI.markVideoMuted(true/false);
  195. }
  196. });
  197. room.on(ConferenceEvents.LAST_N_ENDPOINTS_CHANGED, function (ids) {
  198. APP.UI.handleLastNEndpoints(ids);
  199. });
  200. room.on(ConferenceEvents.ACTIVE_SPEAKER_CHANGED, function (id) {
  201. APP.UI.markDominantSpiker(id);
  202. });
  203. if (!interfaceConfig.filmStripOnly) {
  204. room.on(ConferenceEvents.CONNECTION_INTERRUPTED, function () {
  205. APP.UI.markVideoInterrupted(true);
  206. });
  207. room.on(ConferenceEvents.CONNECTION_RESTORED, function () {
  208. APP.UI.markVideoInterrupted(false);
  209. });
  210. APP.UI.addListener(UIEvents.MESSAGE_CREATED, function (message) {
  211. room.sendTextMessage(message);
  212. });
  213. room.on(ConferenceEvents.MESSAGE_RECEIVED, function (userId, text) {
  214. APP.UI.addMessage(userId, getDisplayName(userId), text, Date.now());
  215. });
  216. }
  217. APP.connectionquality.addListener(
  218. CQEvents.LOCALSTATS_UPDATED,
  219. function (percent, stats) {
  220. APP.UI.updateLocalStats(percent, stats);
  221. // send local stats to other users
  222. room.sendCommand(Commands.CONNECTION_QUALITY, {
  223. value: APP.connectionquality.convertToMUCStats(stats),
  224. attributes: {
  225. id: room.myUserId()
  226. }
  227. });
  228. }
  229. );
  230. APP.connectionquality.addListener(CQEvents.STOP, function () {
  231. APP.UI.hideStats();
  232. room.removeCommand(Commands.CONNECTION_QUALITY);
  233. });
  234. // listen to remote stats
  235. room.addCommandListener(Commands.CONNECTION_QUALITY, function (data) {
  236. APP.connectionquality.updateRemoteStats(data.attributes.id, data.value);
  237. });
  238. APP.connectionquality.addListener(
  239. CQEvents.REMOTESTATS_UPDATED,
  240. function (id, percent, stats) {
  241. APP.UI.updateRemoteStats(id, percent, stats);
  242. }
  243. );
  244. // share email with other users
  245. function sendEmail(email) {
  246. room.sendCommand(Commands.EMAIL, {
  247. value: email,
  248. attributes: {
  249. id: room.myUserId()
  250. }
  251. });
  252. }
  253. var email = APP.settings.getEmail();
  254. email && sendEmail(email);
  255. APP.UI.addListener(UIEvents.EMAIL_CHANGED, function (email) {
  256. APP.settings.setEmail(email);
  257. APP.UI.setUserAvatar(room.myUserId(), email);
  258. sendEmail(email);
  259. });
  260. room.addCommandListener(Commands.EMAIL, function (data) {
  261. APP.UI.setUserAvatar(data.attributes.id, data.value);
  262. });
  263. room.on(ConferenceEvents.DISPLAY_NAME_CHANGED, function (id, displayName) {
  264. APP.UI.changeDisplayName(id, displayName);
  265. });
  266. APP.UI.addListener(UIEvents.NICKNAME_CHANGED, function (nickname) {
  267. APP.settings.setDisplayName(nickname);
  268. room.setDisplayName(nickname);
  269. });
  270. room.on(ConferenceErrors.PASSWORD_REQUIRED, function () {
  271. // FIXME handle
  272. });
  273. room.on(ConferenceErrors.CONNECTION_ERROR, function () {
  274. // FIXME handle
  275. });
  276. APP.UI.addListener(
  277. UIEvents.START_MUTED_CHANGED,
  278. function (startAudioMuted, startVideoMuted) {
  279. // FIXME start muted
  280. }
  281. );
  282. return new Promise(function (resolve, reject) {
  283. room.on(
  284. ConferenceEvents.CONFERENCE_JOINED,
  285. function () {
  286. resolve();
  287. }
  288. );
  289. APP.UI.closeAuthenticationDialog();
  290. if (config.useNicks) {
  291. // FIXME check this
  292. var nick = APP.UI.askForNickname();
  293. }
  294. room.join();
  295. }).catch(function (err) {
  296. if (err[0] === ConferenceErrors.PASSWORD_REQUIRED) {
  297. // FIXME ask for password and try again
  298. return initConference(connection, roomName);
  299. }
  300. // FIXME else notify that we cannot conenct to the room
  301. throw new Error(err[0]);
  302. });
  303. }
  304. function init() {
  305. connect().then(function (connection) {
  306. return initConference(connection, APP.conference.roomName);
  307. }).then(function () {
  308. APP.UI.start();
  309. APP.UI.initConference();
  310. APP.UI.addListener(UIEvents.LANG_CHANGED, function (language) {
  311. APP.translation.setLanguage(language);
  312. APP.settings.setLanguage(language);
  313. });
  314. APP.desktopsharing.init();
  315. APP.statistics.start();
  316. APP.connectionquality.init();
  317. APP.keyboardshortcut.init();
  318. });
  319. }
  320. /**
  321. * If we have an HTTP endpoint for getting config.json configured we're going to
  322. * read it and override properties from config.js and interfaceConfig.js.
  323. * If there is no endpoint we'll just continue with initialization.
  324. * Keep in mind that if the endpoint has been configured and we fail to obtain
  325. * the config for any reason then the conference won't start and error message
  326. * will be displayed to the user.
  327. */
  328. function obtainConfigAndInit() {
  329. let roomName = APP.conference.roomName;
  330. if (config.configLocation) {
  331. APP.configFetch.obtainConfig(
  332. config.configLocation, roomName,
  333. // Get config result callback
  334. function(success, error) {
  335. if (success) {
  336. console.log("(TIME) configuration fetched:\t",
  337. window.performance.now());
  338. init();
  339. } else {
  340. // Show obtain config error,
  341. // pass the error object for report
  342. APP.UI.messageHandler.openReportDialog(
  343. null, "dialog.connectError", error);
  344. }
  345. });
  346. } else {
  347. require("./modules/config/BoshAddressChoice").chooseAddress(
  348. config, roomName);
  349. init();
  350. }
  351. }
  352. $(document).ready(function () {
  353. console.log("(TIME) document ready:\t", window.performance.now());
  354. var URLProcessor = require("./modules/config/URLProcessor");
  355. URLProcessor.setConfigParametersFromUrl();
  356. APP.init();
  357. APP.translation.init();
  358. if (APP.API.isEnabled()) {
  359. APP.API.init();
  360. }
  361. obtainConfigAndInit();
  362. });
  363. $(window).bind('beforeunload', function () {
  364. if (APP.API.isEnabled()) {
  365. APP.API.dispose();
  366. }
  367. });
  368. module.exports = APP;