Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

app.js 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674
  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 URLProcessor from "./modules/config/URLProcessor";
  15. import RoomnameGenerator from './modules/util/RoomnameGenerator';
  16. import CQEvents from './service/connectionquality/CQEvents';
  17. import UIEvents from './service/UI/UIEvents';
  18. import {openConnection} from './modules/connection';
  19. import AuthHandler from './modules/AuthHandler';
  20. import createRoomLocker from './modules/RoomLocker';
  21. const DesktopSharingEventTypes =
  22. require("./service/desktopsharing/DesktopSharingEventTypes");
  23. const ConnectionEvents = JitsiMeetJS.events.connection;
  24. const ConnectionErrors = JitsiMeetJS.errors.connection;
  25. const ConferenceEvents = JitsiMeetJS.events.conference;
  26. const ConferenceErrors = JitsiMeetJS.errors.conference;
  27. const TrackEvents = JitsiMeetJS.events.track;
  28. const TrackErrors = JitsiMeetJS.errors.track;
  29. let localVideo, localAudio;
  30. const Commands = {
  31. CONNECTION_QUALITY: "connectionQuality",
  32. EMAIL: "email",
  33. VIDEO_TYPE: "videoType"
  34. };
  35. function buildRoomName () {
  36. let path = window.location.pathname;
  37. let roomName;
  38. // determinde the room node from the url
  39. // TODO: just the roomnode or the whole bare jid?
  40. if (config.getroomnode && typeof config.getroomnode === 'function') {
  41. // custom function might be responsible for doing the pushstate
  42. roomName = config.getroomnode(path);
  43. } else {
  44. /* fall back to default strategy
  45. * this is making assumptions about how the URL->room mapping happens.
  46. * It currently assumes deployment at root, with a rewrite like the
  47. * following one (for nginx):
  48. location ~ ^/([a-zA-Z0-9]+)$ {
  49. rewrite ^/(.*)$ / break;
  50. }
  51. */
  52. if (path.length > 1) {
  53. roomName = path.substr(1).toLowerCase();
  54. } else {
  55. let word = RoomnameGenerator.generateRoomWithoutSeparator();
  56. roomName = word.toLowerCase();
  57. window.history.pushState(
  58. 'VideoChat', `Room: ${word}`, window.location.pathname + word
  59. );
  60. }
  61. }
  62. return roomName;
  63. }
  64. const APP = {
  65. init () {
  66. let roomName = buildRoomName();
  67. this.conference = {
  68. roomName,
  69. localId: undefined,
  70. isModerator: false,
  71. membersCount: 0,
  72. audioMuted: false,
  73. videoMuted: false,
  74. sipGatewayEnabled: false, //FIXME handle
  75. isLocalId (id) {
  76. return this.localId === id;
  77. },
  78. muteAudio (mute) {
  79. APP.UI.eventEmitter.emit(UIEvents.AUDIO_MUTED, mute);
  80. },
  81. toggleAudioMuted () {
  82. this.muteAudio(!this.audioMuted);
  83. },
  84. muteVideo (mute) {
  85. APP.UI.eventEmitter.emit(UIEvents.VIDEO_MUTED, mute);
  86. },
  87. toggleVideoMuted () {
  88. this.muteVideo(!this.videoMuted);
  89. }
  90. };
  91. this.UI = require("./modules/UI/UI");
  92. this.API = require("./modules/API/API");
  93. this.connectionquality =
  94. require("./modules/connectionquality/connectionquality");
  95. this.statistics = require("./modules/statistics/statistics");
  96. this.desktopsharing =
  97. require("./modules/desktopsharing/desktopsharing");
  98. this.keyboardshortcut =
  99. require("./modules/keyboardshortcut/keyboardshortcut");
  100. this.translation = require("./modules/translation/translation");
  101. this.settings = require("./modules/settings/Settings");
  102. this.configFetch = require("./modules/config/HttpConfigFetch");
  103. }
  104. };
  105. function initConference(localTracks, connection) {
  106. let room = connection.initJitsiConference(APP.conference.roomName, {
  107. openSctp: config.openSctp,
  108. disableAudioLevels: config.disableAudioLevels
  109. });
  110. const addTrack = (track) => {
  111. room.addTrack(track);
  112. if(track.getType() === "audio")
  113. return;
  114. room.removeCommand(Commands.VIDEO_TYPE);
  115. room.sendCommand(Commands.VIDEO_TYPE, {
  116. value: track.videoType,
  117. attributes: {
  118. xmlns: 'http://jitsi.org/jitmeet/video'
  119. }
  120. });
  121. };
  122. APP.conference.localId = room.myUserId();
  123. Object.defineProperty(APP.conference, "membersCount", {
  124. get: function () {
  125. return room.getParticipants().length; // FIXME maybe +1?
  126. }
  127. });
  128. APP.conference.listMembers = function () {
  129. return room.getParticipants();
  130. };
  131. APP.conference.listMembersIds = function () {
  132. return room.getParticipants().map(p => p.getId());
  133. };
  134. /**
  135. * Creates video track (desktop or camera).
  136. * @param type "camera" or "video"
  137. * @param endedHandler onended function
  138. * @returns Promise
  139. */
  140. APP.conference.createVideoTrack = (type, endedHandler) => {
  141. return JitsiMeetJS.createLocalTracks({
  142. devices: [type], resolution: config.resolution
  143. }).then((tracks) => {
  144. tracks[0].on(TrackEvents.TRACK_STOPPED, endedHandler);
  145. return tracks;
  146. });
  147. };
  148. APP.conference.changeLocalVideo = (track, callback) => {
  149. const localCallback = (newTrack) => {
  150. if (newTrack.isLocal() && newTrack === localVideo) {
  151. if(localVideo.isMuted() &&
  152. localVideo.videoType !== track.videoType) {
  153. localVideo.mute();
  154. }
  155. callback();
  156. room.off(ConferenceEvents.TRACK_ADDED, localCallback);
  157. }
  158. };
  159. room.on(ConferenceEvents.TRACK_ADDED, localCallback);
  160. localVideo.stop();
  161. localVideo = track;
  162. addTrack(track);
  163. APP.UI.addLocalStream(track);
  164. };
  165. function getDisplayName(id) {
  166. if (APP.conference.isLocalId(id)) {
  167. return APP.settings.getDisplayName();
  168. }
  169. var participant = room.getParticipantById(id);
  170. if (participant && participant.getDisplayName()) {
  171. return participant.getDisplayName();
  172. }
  173. }
  174. // add local streams when joined to the conference
  175. room.on(ConferenceEvents.CONFERENCE_JOINED, function () {
  176. localTracks.forEach(function (track) {
  177. if(track.getType() === "audio") {
  178. localAudio = track;
  179. }
  180. else if (track.getType() === "video") {
  181. localVideo = track;
  182. }
  183. addTrack(track);
  184. APP.UI.addLocalStream(track);
  185. });
  186. APP.UI.updateAuthInfo(room.isAuthEnabled(), room.getAuthLogin());
  187. });
  188. room.on(ConferenceEvents.USER_JOINED, function (id, user) {
  189. if (APP.conference.isLocalId(id)) {
  190. return;
  191. }
  192. console.error('USER %s connnected', id);
  193. // FIXME email???
  194. APP.UI.addUser(id, user.getDisplayName());
  195. });
  196. room.on(ConferenceEvents.USER_LEFT, function (id, user) {
  197. console.error('USER LEFT', id);
  198. APP.UI.removeUser(id, user.getDisplayName());
  199. });
  200. room.on(ConferenceEvents.USER_ROLE_CHANGED, function (id, role) {
  201. if (APP.conference.isLocalId(id)) {
  202. console.info(`My role changed, new role: ${role}`);
  203. APP.conference.isModerator = room.isModerator();
  204. APP.UI.updateLocalRole(room.isModerator());
  205. } else {
  206. var user = room.getParticipantById(id);
  207. if (user) {
  208. APP.UI.updateUserRole(user);
  209. }
  210. }
  211. });
  212. let roomLocker = createRoomLocker(room);
  213. APP.UI.addListener(UIEvents.ROOM_LOCK_CLICKED, function () {
  214. if (room.isModerator()) {
  215. let promise = roomLocker.isLocked
  216. ? roomLocker.askToUnlock()
  217. : roomLocker.askToLock();
  218. promise.then(function () {
  219. APP.UI.markRoomLocked(roomLocker.isLocked);
  220. });
  221. } else {
  222. roomLocker.notifyModeratorRequired();
  223. }
  224. });
  225. room.on(ConferenceEvents.TRACK_ADDED, function (track) {
  226. if (track.isLocal()) { // skip local tracks
  227. return;
  228. }
  229. console.error(
  230. 'REMOTE %s TRACK', track.getType(), track.getParticipantId()
  231. );
  232. APP.UI.addRemoteStream(track);
  233. });
  234. room.on(ConferenceEvents.TRACK_REMOVED, function (track) {
  235. if (track.isLocal()) { // skip local tracks
  236. return;
  237. }
  238. console.error(
  239. 'REMOTE %s TRACK REMOVED', track.getType(), track.getParticipantId()
  240. );
  241. // FIXME handle
  242. });
  243. room.on(ConferenceEvents.TRACK_MUTE_CHANGED, function (track) {
  244. // FIXME handle mute
  245. });
  246. room.on(ConferenceEvents.TRACK_AUDIO_LEVEL_CHANGED, function (id, lvl) {
  247. APP.UI.setAudioLevel(id, lvl);
  248. });
  249. APP.UI.addListener(UIEvents.AUDIO_MUTED, function (muted) {
  250. // FIXME mute or unmute
  251. APP.UI.setAudioMuted(muted);
  252. APP.conference.audioMuted = muted;
  253. });
  254. APP.UI.addListener(UIEvents.VIDEO_MUTED, function (muted) {
  255. // FIXME mute or unmute
  256. APP.UI.setVideoMuted(muted);
  257. APP.conference.videoMuted = muted;
  258. });
  259. room.on(ConferenceEvents.IN_LAST_N_CHANGED, function (inLastN) {
  260. if (config.muteLocalVideoIfNotInLastN) {
  261. // TODO mute or unmute if required
  262. // mark video on UI
  263. // APP.UI.markVideoMuted(true/false);
  264. }
  265. });
  266. room.on(ConferenceEvents.LAST_N_ENDPOINTS_CHANGED, function (ids) {
  267. APP.UI.handleLastNEndpoints(ids);
  268. });
  269. room.on(ConferenceEvents.ACTIVE_SPEAKER_CHANGED, function (id) {
  270. APP.UI.markDominantSpiker(id);
  271. });
  272. if (!interfaceConfig.filmStripOnly) {
  273. room.on(ConferenceEvents.CONNECTION_INTERRUPTED, function () {
  274. APP.UI.markVideoInterrupted(true);
  275. });
  276. room.on(ConferenceEvents.CONNECTION_RESTORED, function () {
  277. APP.UI.markVideoInterrupted(false);
  278. });
  279. APP.UI.addListener(UIEvents.MESSAGE_CREATED, function (message) {
  280. room.sendTextMessage(message);
  281. });
  282. room.on(ConferenceEvents.MESSAGE_RECEIVED, function (id, text, ts) {
  283. APP.UI.addMessage(id, getDisplayName(id), text, ts);
  284. });
  285. }
  286. APP.connectionquality.addListener(
  287. CQEvents.LOCALSTATS_UPDATED,
  288. function (percent, stats) {
  289. APP.UI.updateLocalStats(percent, stats);
  290. // send local stats to other users
  291. room.sendCommand(Commands.CONNECTION_QUALITY, {
  292. value: APP.connectionquality.convertToMUCStats(stats),
  293. attributes: {
  294. id: room.myUserId()
  295. }
  296. });
  297. }
  298. );
  299. APP.connectionquality.addListener(CQEvents.STOP, function () {
  300. APP.UI.hideStats();
  301. room.removeCommand(Commands.CONNECTION_QUALITY);
  302. });
  303. // listen to remote stats
  304. room.addCommandListener(Commands.CONNECTION_QUALITY, function (data) {
  305. APP.connectionquality.updateRemoteStats(data.attributes.id, data.value);
  306. });
  307. APP.connectionquality.addListener(
  308. CQEvents.REMOTESTATS_UPDATED,
  309. function (id, percent, stats) {
  310. APP.UI.updateRemoteStats(id, percent, stats);
  311. }
  312. );
  313. room.addCommandListener(Commands.VIDEO_TYPE, (data, from) => {
  314. APP.UI.onPeerVideoTypeChanged(from, data.value);
  315. });
  316. // share email with other users
  317. function sendEmail(email) {
  318. room.sendCommand(Commands.EMAIL, {
  319. value: email,
  320. attributes: {
  321. id: room.myUserId()
  322. }
  323. });
  324. }
  325. var email = APP.settings.getEmail();
  326. email && sendEmail(email);
  327. APP.UI.addListener(UIEvents.EMAIL_CHANGED, function (email) {
  328. APP.settings.setEmail(email);
  329. APP.UI.setUserAvatar(room.myUserId(), email);
  330. sendEmail(email);
  331. });
  332. room.addCommandListener(Commands.EMAIL, function (data) {
  333. APP.UI.setUserAvatar(data.attributes.id, data.value);
  334. });
  335. let nick = APP.settings.getDisplayName();
  336. if (config.useNicks && !nick) {
  337. nick = APP.UI.askForNickname();
  338. APP.settings.setDisplayName(nick);
  339. }
  340. if (nick) {
  341. room.setDisplayName(nick);
  342. }
  343. room.on(ConferenceEvents.DISPLAY_NAME_CHANGED, function (id, displayName) {
  344. APP.UI.changeDisplayName(id, displayName);
  345. });
  346. APP.UI.addListener(UIEvents.NICKNAME_CHANGED, function (nickname) {
  347. APP.settings.setDisplayName(nickname);
  348. room.setDisplayName(nickname);
  349. APP.UI.changeDisplayName(APP.conference.localId, nickname);
  350. });
  351. APP.UI.addListener(
  352. UIEvents.START_MUTED_CHANGED,
  353. function (startAudioMuted, startVideoMuted) {
  354. // FIXME start muted
  355. }
  356. );
  357. APP.UI.addListener(UIEvents.USER_INVITED, function (roomUrl) {
  358. APP.UI.inviteParticipants(
  359. roomUrl,
  360. APP.conference.roomName,
  361. roomLocker.password,
  362. APP.settings.getDisplayName()
  363. );
  364. });
  365. // call hangup
  366. APP.UI.addListener(UIEvents.HANGUP, function () {
  367. APP.UI.requestFeedback().then(function () {
  368. connection.disconnect();
  369. if (config.enableWelcomePage) {
  370. setTimeout(function() {
  371. window.localStorage.welcomePageDisabled = false;
  372. window.location.pathname = "/";
  373. }, 3000);
  374. }
  375. });
  376. });
  377. // logout
  378. APP.UI.addListener(UIEvents.LOGOUT, function () {
  379. // FIXME handle logout
  380. // APP.xmpp.logout(function (url) {
  381. // if (url) {
  382. // window.location.href = url;
  383. // } else {
  384. // hangup();
  385. // }
  386. // });
  387. });
  388. APP.UI.addListener(UIEvents.SIP_DIAL, function (sipNumber) {
  389. // FIXME add dial
  390. // APP.xmpp.dial(
  391. // sipNumber,
  392. // 'fromnumber',
  393. // APP.conference.roomName,
  394. // roomLocker.password
  395. // );
  396. });
  397. // Starts or stops the recording for the conference.
  398. APP.UI.addListener(UIEvents.RECORDING_TOGGLE, function (predefinedToken) {
  399. // FIXME recording
  400. // APP.xmpp.toggleRecording(function (callback) {
  401. // if (predefinedToken) {
  402. // callback(predefinedToken);
  403. // return;
  404. // }
  405. // APP.UI.requestRecordingToken().then(callback);
  406. // }, APP.UI.updateRecordingState);
  407. });
  408. APP.UI.addListener(UIEvents.TOPIC_CHANGED, function (topic) {
  409. // FIXME handle topic change
  410. // APP.xmpp.setSubject(topic);
  411. // on SUBJECT_CHANGED UI.setSubject(topic);
  412. });
  413. APP.UI.addListener(UIEvents.USER_KICKED, function (id) {
  414. room.kickParticipant(id);
  415. });
  416. room.on(ConferenceEvents.KICKED, function () {
  417. APP.UI.notifyKicked();
  418. // FIXME close
  419. });
  420. APP.UI.addListener(UIEvents.AUTH_CLICKED, function () {
  421. AuthHandler.authenticate(room);
  422. });
  423. APP.UI.addListener(UIEvents.SELECTED_ENDPOINT, function (id) {
  424. room.selectParticipant(id);
  425. });
  426. room.on(ConferenceEvents.DTMF_SUPPORT_CHANGED, function (isDTMFSupported) {
  427. APP.UI.updateDTMFSupport(isDTMFSupported);
  428. });
  429. $(window).bind('beforeunload', function () {
  430. room.leave();
  431. });
  432. return new Promise(function (resolve, reject) {
  433. room.on(ConferenceEvents.CONFERENCE_JOINED, handleConferenceJoined);
  434. room.on(ConferenceEvents.CONFERENCE_FAILED, onConferenceFailed);
  435. let password;
  436. let reconnectTimeout;
  437. function unsubscribe() {
  438. room.off(
  439. ConferenceEvents.CONFERENCE_JOINED, handleConferenceJoined
  440. );
  441. room.off(
  442. ConferenceEvents.CONFERENCE_FAILED, onConferenceFailed
  443. );
  444. if (reconnectTimeout) {
  445. clearTimeout(reconnectTimeout);
  446. }
  447. AuthHandler.closeAuth();
  448. }
  449. function handleConferenceJoined() {
  450. unsubscribe();
  451. resolve();
  452. }
  453. function handleConferenceFailed(err) {
  454. unsubscribe();
  455. reject(err);
  456. }
  457. function onConferenceFailed(err, msg = '') {
  458. console.error('CONFERENCE FAILED:', err, msg);
  459. switch (err) {
  460. // room is locked by the password
  461. case ConferenceErrors.PASSWORD_REQUIRED:
  462. APP.UI.markRoomLocked(true);
  463. roomLocker.requirePassword().then(function () {
  464. room.join(roomLocker.password);
  465. });
  466. break;
  467. case ConferenceErrors.CONNECTION_ERROR:
  468. APP.UI.notifyConnectionFailed(msg);
  469. break;
  470. // not enough rights to create conference
  471. case ConferenceErrors.AUTHENTICATION_REQUIRED:
  472. // schedule reconnect to check if someone else created the room
  473. reconnectTimeout = setTimeout(function () {
  474. room.join(password);
  475. }, 5000);
  476. // notify user that auth is required
  477. AuthHandler.requireAuth(APP.conference.roomName);
  478. break;
  479. default:
  480. handleConferenceFailed(err);
  481. }
  482. }
  483. room.join(password);
  484. });
  485. }
  486. function createLocalTracks () {
  487. return JitsiMeetJS.createLocalTracks({
  488. devices: ['audio', 'video']
  489. }).catch(function (err) {
  490. console.error('failed to create local tracks', err);
  491. return [];
  492. });
  493. }
  494. function connect() {
  495. return openConnection({retry: true}).catch(function (err) {
  496. if (err === ConnectionErrors.PASSWORD_REQUIRED) {
  497. APP.UI.notifyTokenAuthFailed();
  498. } else {
  499. APP.UI.notifyConnectionFailed(err);
  500. }
  501. throw err;
  502. });
  503. }
  504. function init() {
  505. APP.UI.start();
  506. JitsiMeetJS.setLogLevel(JitsiMeetJS.logLevels.TRACE);
  507. JitsiMeetJS.init(config).then(function () {
  508. return Promise.all([createLocalTracks(), connect()]);
  509. }).then(function ([tracks, connection]) {
  510. console.log('initialized with %s local tracks', tracks.length);
  511. return initConference(tracks, connection);
  512. }).then(function () {
  513. APP.UI.initConference();
  514. APP.UI.addListener(UIEvents.LANG_CHANGED, function (language) {
  515. APP.translation.setLanguage(language);
  516. APP.settings.setLanguage(language);
  517. });
  518. APP.desktopsharing.addListener(
  519. DesktopSharingEventTypes.NEW_STREAM_CREATED,
  520. (stream, callback) => {
  521. APP.conference.changeLocalVideo(stream,
  522. callback);
  523. });
  524. APP.desktopsharing.init(JitsiMeetJS.isDesktopSharingEnabled());
  525. APP.statistics.start();
  526. APP.connectionquality.init();
  527. APP.keyboardshortcut.init();
  528. }).catch(function (err) {
  529. console.error(err);
  530. });
  531. }
  532. /**
  533. * If we have an HTTP endpoint for getting config.json configured we're going to
  534. * read it and override properties from config.js and interfaceConfig.js.
  535. * If there is no endpoint we'll just continue with initialization.
  536. * Keep in mind that if the endpoint has been configured and we fail to obtain
  537. * the config for any reason then the conference won't start and error message
  538. * will be displayed to the user.
  539. */
  540. function obtainConfigAndInit() {
  541. let roomName = APP.conference.roomName;
  542. if (config.configLocation) {
  543. APP.configFetch.obtainConfig(
  544. config.configLocation, roomName,
  545. // Get config result callback
  546. function(success, error) {
  547. if (success) {
  548. console.log("(TIME) configuration fetched:\t",
  549. window.performance.now());
  550. init();
  551. } else {
  552. // Show obtain config error,
  553. // pass the error object for report
  554. APP.UI.messageHandler.openReportDialog(
  555. null, "dialog.connectError", error);
  556. }
  557. });
  558. } else {
  559. require("./modules/config/BoshAddressChoice").chooseAddress(
  560. config, roomName);
  561. init();
  562. }
  563. }
  564. $(document).ready(function () {
  565. console.log("(TIME) document ready:\t", window.performance.now());
  566. URLProcessor.setConfigParametersFromUrl();
  567. APP.init();
  568. APP.translation.init();
  569. if (APP.API.isEnabled()) {
  570. APP.API.init();
  571. }
  572. obtainConfigAndInit();
  573. });
  574. $(window).bind('beforeunload', function () {
  575. if (APP.API.isEnabled()) {
  576. APP.API.dispose();
  577. }
  578. });
  579. module.exports = APP;