Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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. let roomName = buildRoomName();
  53. this.conference = {
  54. roomName,
  55. localId: undefined,
  56. isModerator: false,
  57. membersCount: 0,
  58. audioMuted: false,
  59. videoMuted: false,
  60. isLocalId (id) {
  61. return this.localId === id;
  62. },
  63. muteAudio (mute) {
  64. APP.UI.eventEmitter.emit(UIEvents.AUDIO_MUTED, mute);
  65. },
  66. toggleAudioMuted () {
  67. this.muteAudio(!this.audioMuted);
  68. },
  69. muteVideo (mute) {
  70. APP.UI.eventEmitter.emit(UIEvents.VIDEO_MUTED, mute);
  71. },
  72. toggleVideoMuted () {
  73. this.muteVideo(!this.videoMuted);
  74. }
  75. };
  76. this.UI = require("./modules/UI/UI");
  77. this.API = require("./modules/API/API");
  78. this.connectionquality =
  79. require("./modules/connectionquality/connectionquality");
  80. this.statistics = require("./modules/statistics/statistics");
  81. this.desktopsharing =
  82. require("./modules/desktopsharing/desktopsharing");
  83. this.keyboardshortcut =
  84. require("./modules/keyboardshortcut/keyboardshortcut");
  85. this.translation = require("./modules/translation/translation");
  86. this.settings = require("./modules/settings/Settings");
  87. this.configFetch = require("./modules/config/HttpConfigFetch");
  88. }
  89. };
  90. var ConnectionEvents = JitsiMeetJS.events.connection;
  91. var ConnectionErrors = JitsiMeetJS.errors.connection;
  92. function connect() {
  93. var connection = new JitsiMeetJS.JitsiConnection(null, null, {
  94. hosts: config.hosts,
  95. bosh: config.bosh,
  96. clientNode: config.clientNode
  97. });
  98. return new Promise(function (resolve, reject) {
  99. var handlers = {};
  100. var unsubscribe = function () {
  101. Object.keys(handlers).forEach(function (event) {
  102. connection.removeEventListener(event, handlers[event]);
  103. });
  104. };
  105. handlers[ConnectionEvents.CONNECTION_ESTABLISHED] = function () {
  106. console.log('CONNECTED');
  107. unsubscribe();
  108. resolve(connection);
  109. };
  110. var listenForFailure = function (event) {
  111. handlers[event] = function (...args) {
  112. console.error(`CONNECTION FAILED: ${event}`, ...args);
  113. unsubscribe();
  114. reject([event, ...args]);
  115. };
  116. };
  117. listenForFailure(ConnectionEvents.CONNECTION_FAILED);
  118. listenForFailure(ConnectionErrors.PASSWORD_REQUIRED);
  119. listenForFailure(ConnectionErrors.CONNECTION_ERROR);
  120. listenForFailure(ConnectionErrors.OTHER_ERRORS);
  121. // install event listeners
  122. Object.keys(handlers).forEach(function (event) {
  123. connection.addEventListener(event, handlers[event]);
  124. });
  125. connection.connect();
  126. }).catch(function (err) {
  127. if (err[0] === ConnectionErrors.PASSWORD_REQUIRED) {
  128. // FIXME ask for password and try again
  129. return connect();
  130. }
  131. console.error('FAILED TO CONNECT', err);
  132. APP.UI.notifyConnectionFailed(err[1]);
  133. throw new Error(err[0]);
  134. });
  135. }
  136. var ConferenceEvents = JitsiMeetJS.events.conference;
  137. var ConferenceErrors = JitsiMeetJS.errors.conference;
  138. function initConference(localTracks, connection) {
  139. var room = connection.initJitsiConference(APP.conference.roomName, {
  140. openSctp: config.openSctp,
  141. disableAudioLevels: config.disableAudioLevels
  142. });
  143. var users = {};
  144. APP.conference.localId = room.myUserId();
  145. Object.defineProperty(APP.conference, "membersCount", {
  146. get: function () {
  147. return Object.keys(users).length; // FIXME maybe +1?
  148. }
  149. });
  150. function getDisplayName(id) {
  151. if (APP.conference.isLocalId(id)) {
  152. return APP.settings.getDisplayName();
  153. }
  154. var user = users[id];
  155. if (user && user.displayName) {
  156. return user.displayName;
  157. }
  158. }
  159. // add local streams when joined to the conference
  160. room.on(ConferenceEvents.CONFERENCE_JOINED, function () {
  161. localTracks.forEach(function (track) {
  162. room.addTrack(track);
  163. APP.UI.addLocalStream(track);
  164. });
  165. });
  166. room.on(ConferenceEvents.USER_JOINED, function (id) {
  167. users[id] = {
  168. displayName: undefined,
  169. tracks: []
  170. };
  171. // FIXME email???
  172. APP.UI.addUser(id);
  173. });
  174. room.on(ConferenceEvents.USER_LEFT, function (id) {
  175. delete users[id];
  176. APP.UI.removeUser(id);
  177. });
  178. room.on(ConferenceEvents.TRACK_MUTE_CHANGED, function (track) {
  179. // FIXME handle mute
  180. });
  181. room.on(ConferenceEvents.TRACK_AUDIO_LEVEL_CHANGED, function (id, lvl) {
  182. APP.UI.setAudioLevel(id, lvl);
  183. });
  184. APP.UI.addListener(UIEvents.AUDIO_MUTED, function (muted) {
  185. // FIXME mute or unmute
  186. APP.UI.setAudioMuted(muted);
  187. APP.conference.audioMuted = muted;
  188. });
  189. APP.UI.addListener(UIEvents.VIDEO_MUTED, function (muted) {
  190. // FIXME mute or unmute
  191. APP.UI.setVideoMuted(muted);
  192. APP.conference.videoMuted = muted;
  193. });
  194. room.on(ConferenceEvents.IN_LAST_N_CHANGED, function (inLastN) {
  195. if (config.muteLocalVideoIfNotInLastN) {
  196. // TODO mute or unmute if required
  197. // mark video on UI
  198. // APP.UI.markVideoMuted(true/false);
  199. }
  200. });
  201. room.on(ConferenceEvents.LAST_N_ENDPOINTS_CHANGED, function (ids) {
  202. APP.UI.handleLastNEndpoints(ids);
  203. });
  204. room.on(ConferenceEvents.ACTIVE_SPEAKER_CHANGED, function (id) {
  205. APP.UI.markDominantSpiker(id);
  206. });
  207. if (!interfaceConfig.filmStripOnly) {
  208. room.on(ConferenceEvents.CONNECTION_INTERRUPTED, function () {
  209. APP.UI.markVideoInterrupted(true);
  210. });
  211. room.on(ConferenceEvents.CONNECTION_RESTORED, function () {
  212. APP.UI.markVideoInterrupted(false);
  213. });
  214. APP.UI.addListener(UIEvents.MESSAGE_CREATED, function (message) {
  215. room.sendTextMessage(message);
  216. });
  217. room.on(ConferenceEvents.MESSAGE_RECEIVED, function (userId, text) {
  218. APP.UI.addMessage(userId, getDisplayName(userId), text, Date.now());
  219. });
  220. }
  221. APP.connectionquality.addListener(
  222. CQEvents.LOCALSTATS_UPDATED,
  223. function (percent, stats) {
  224. APP.UI.updateLocalStats(percent, stats);
  225. // send local stats to other users
  226. room.sendCommand(Commands.CONNECTION_QUALITY, {
  227. value: APP.connectionquality.convertToMUCStats(stats),
  228. attributes: {
  229. id: room.myUserId()
  230. }
  231. });
  232. }
  233. );
  234. APP.connectionquality.addListener(CQEvents.STOP, function () {
  235. APP.UI.hideStats();
  236. room.removeCommand(Commands.CONNECTION_QUALITY);
  237. });
  238. // listen to remote stats
  239. room.addCommandListener(Commands.CONNECTION_QUALITY, function (data) {
  240. APP.connectionquality.updateRemoteStats(data.attributes.id, data.value);
  241. });
  242. APP.connectionquality.addListener(
  243. CQEvents.REMOTESTATS_UPDATED,
  244. function (id, percent, stats) {
  245. APP.UI.updateRemoteStats(id, percent, stats);
  246. }
  247. );
  248. // share email with other users
  249. function sendEmail(email) {
  250. room.sendCommand(Commands.EMAIL, {
  251. value: email,
  252. attributes: {
  253. id: room.myUserId()
  254. }
  255. });
  256. }
  257. var email = APP.settings.getEmail();
  258. email && sendEmail(email);
  259. APP.UI.addListener(UIEvents.EMAIL_CHANGED, function (email) {
  260. APP.settings.setEmail(email);
  261. APP.UI.setUserAvatar(room.myUserId(), email);
  262. sendEmail(email);
  263. });
  264. room.addCommandListener(Commands.EMAIL, function (data) {
  265. APP.UI.setUserAvatar(data.attributes.id, data.value);
  266. });
  267. room.on(ConferenceEvents.DISPLAY_NAME_CHANGED, function (id, displayName) {
  268. APP.UI.changeDisplayName(id, displayName);
  269. });
  270. APP.UI.addListener(UIEvents.NICKNAME_CHANGED, function (nickname) {
  271. APP.settings.setDisplayName(nickname);
  272. room.setDisplayName(nickname);
  273. });
  274. room.on(ConferenceErrors.PASSWORD_REQUIRED, function () {
  275. // FIXME handle
  276. });
  277. room.on(ConferenceErrors.CONNECTION_ERROR, function () {
  278. // FIXME handle
  279. });
  280. APP.UI.addListener(
  281. UIEvents.START_MUTED_CHANGED,
  282. function (startAudioMuted, startVideoMuted) {
  283. // FIXME start muted
  284. }
  285. );
  286. return new Promise(function (resolve, reject) {
  287. room.on(
  288. ConferenceEvents.CONFERENCE_JOINED,
  289. function () {
  290. resolve();
  291. }
  292. );
  293. APP.UI.closeAuthenticationDialog();
  294. if (config.useNicks) {
  295. // FIXME check this
  296. var nick = APP.UI.askForNickname();
  297. }
  298. room.join();
  299. }).catch(function (err) {
  300. if (err[0] === ConferenceErrors.PASSWORD_REQUIRED) {
  301. // FIXME ask for password and try again
  302. return initConference(localTracks, connection);
  303. }
  304. // FIXME else notify that we cannot conenct to the room
  305. throw new Error(err[0]);
  306. });
  307. }
  308. function createLocalTracks () {
  309. return JitsiMeetJS.createLocalTracks({
  310. devices: ['audio', 'video']
  311. }).catch(function (err) {
  312. console.error('failed to create local tracks', err);
  313. return [];
  314. });
  315. }
  316. function init() {
  317. JitsiMeetJS.setLogLevel(JitsiMeetJS.logLevels.TRACE);
  318. JitsiMeetJS.init().then(function () {
  319. return Promise.all([createLocalTracks(), connect()]);
  320. }).then(function ([tracks, connection]) {
  321. console.log('initialized with %s local tracks', tracks.length);
  322. return initConference(tracks, connection);
  323. }).then(function () {
  324. APP.UI.start();
  325. APP.UI.initConference();
  326. APP.UI.addListener(UIEvents.LANG_CHANGED, function (language) {
  327. APP.translation.setLanguage(language);
  328. APP.settings.setLanguage(language);
  329. });
  330. APP.desktopsharing.init();
  331. APP.statistics.start();
  332. APP.connectionquality.init();
  333. APP.keyboardshortcut.init();
  334. });
  335. }
  336. /**
  337. * If we have an HTTP endpoint for getting config.json configured we're going to
  338. * read it and override properties from config.js and interfaceConfig.js.
  339. * If there is no endpoint we'll just continue with initialization.
  340. * Keep in mind that if the endpoint has been configured and we fail to obtain
  341. * the config for any reason then the conference won't start and error message
  342. * will be displayed to the user.
  343. */
  344. function obtainConfigAndInit() {
  345. let roomName = APP.conference.roomName;
  346. if (config.configLocation) {
  347. APP.configFetch.obtainConfig(
  348. config.configLocation, roomName,
  349. // Get config result callback
  350. function(success, error) {
  351. if (success) {
  352. console.log("(TIME) configuration fetched:\t",
  353. window.performance.now());
  354. init();
  355. } else {
  356. // Show obtain config error,
  357. // pass the error object for report
  358. APP.UI.messageHandler.openReportDialog(
  359. null, "dialog.connectError", error);
  360. }
  361. });
  362. } else {
  363. require("./modules/config/BoshAddressChoice").chooseAddress(
  364. config, roomName);
  365. init();
  366. }
  367. }
  368. $(document).ready(function () {
  369. console.log("(TIME) document ready:\t", window.performance.now());
  370. var URLProcessor = require("./modules/config/URLProcessor");
  371. URLProcessor.setConfigParametersFromUrl();
  372. APP.init();
  373. APP.translation.init();
  374. if (APP.API.isEnabled()) {
  375. APP.API.init();
  376. }
  377. obtainConfigAndInit();
  378. });
  379. $(window).bind('beforeunload', function () {
  380. if (APP.API.isEnabled()) {
  381. APP.API.dispose();
  382. }
  383. });
  384. module.exports = APP;