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

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