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

app.js 18KB

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