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

conference.js 31KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992
  1. /* global $, APP, JitsiMeetJS, config, interfaceConfig */
  2. import {openConnection} from './connection';
  3. //FIXME:
  4. import createRoomLocker from './modules/UI/authentication/RoomLocker';
  5. //FIXME:
  6. import AuthHandler from './modules/UI/authentication/AuthHandler';
  7. import ConnectionQuality from './modules/connectionquality/connectionquality';
  8. import CQEvents from './service/connectionquality/CQEvents';
  9. import UIEvents from './service/UI/UIEvents';
  10. const ConnectionEvents = JitsiMeetJS.events.connection;
  11. const ConnectionErrors = JitsiMeetJS.errors.connection;
  12. const ConferenceEvents = JitsiMeetJS.events.conference;
  13. const ConferenceErrors = JitsiMeetJS.errors.conference;
  14. const TrackEvents = JitsiMeetJS.events.track;
  15. const TrackErrors = JitsiMeetJS.errors.track;
  16. let room, connection, localAudio, localVideo, roomLocker;
  17. /**
  18. * Known custom conference commands.
  19. */
  20. const Commands = {
  21. CONNECTION_QUALITY: "stats",
  22. EMAIL: "email",
  23. ETHERPAD: "etherpad",
  24. PREZI: "prezi",
  25. STOP_PREZI: "stop-prezi"
  26. };
  27. /**
  28. * Open Connection. When authentication failed it shows auth dialog.
  29. * @param roomName the room name to use
  30. * @returns Promise<JitsiConnection>
  31. */
  32. function connect(roomName) {
  33. return openConnection({retry: true, roomName: roomName})
  34. .catch(function (err) {
  35. if (err === ConnectionErrors.PASSWORD_REQUIRED) {
  36. APP.UI.notifyTokenAuthFailed();
  37. } else {
  38. APP.UI.notifyConnectionFailed(err);
  39. }
  40. throw err;
  41. });
  42. }
  43. /**
  44. * Share email with other users.
  45. * @param {string} email new email
  46. */
  47. function sendEmail (email) {
  48. room.sendCommand(Commands.EMAIL, {
  49. value: email,
  50. attributes: {
  51. id: room.myUserId()
  52. }
  53. });
  54. }
  55. /**
  56. * Get user nickname by user id.
  57. * @param {string} id user id
  58. * @returns {string?} user nickname or undefined if user is unknown.
  59. */
  60. function getDisplayName (id) {
  61. if (APP.conference.isLocalId(id)) {
  62. return APP.settings.getDisplayName();
  63. }
  64. let participant = room.getParticipantById(id);
  65. if (participant && participant.getDisplayName()) {
  66. return participant.getDisplayName();
  67. }
  68. }
  69. /**
  70. * Mute or unmute local audio stream if it exists.
  71. * @param {boolean} muted if audio stream should be muted or unmuted.
  72. */
  73. function muteLocalAudio (muted) {
  74. if (!localAudio) {
  75. return;
  76. }
  77. if (muted) {
  78. localAudio.mute();
  79. } else {
  80. localAudio.unmute();
  81. }
  82. }
  83. /**
  84. * Mute or unmute local video stream if it exists.
  85. * @param {boolean} muted if video stream should be muted or unmuted.
  86. */
  87. function muteLocalVideo (muted) {
  88. if (!localVideo) {
  89. return;
  90. }
  91. if (muted) {
  92. localVideo.mute();
  93. } else {
  94. localVideo.unmute();
  95. }
  96. }
  97. /**
  98. * Create local tracks of specified types.
  99. * @param {string[]} devices required track types ('audio', 'video' etc.)
  100. * @returns {Promise<JitsiLocalTrack[]>}
  101. */
  102. function createLocalTracks (...devices) {
  103. return JitsiMeetJS.createLocalTracks({
  104. // copy array to avoid mutations inside library
  105. devices: devices.slice(0),
  106. resolution: config.resolution,
  107. cameraDeviceId: APP.settings.getCameraDeviceId(),
  108. micDeviceId: APP.settings.getMicDeviceId(),
  109. // adds any ff fake device settings if any
  110. firefox_fake_device: config.firefox_fake_device
  111. }).catch(function (err) {
  112. console.error('failed to create local tracks', ...devices, err);
  113. return Promise.reject(err);
  114. });
  115. }
  116. /**
  117. * Create local screen sharing track.
  118. * Shows UI notification if Firefox extension is required.
  119. * @returns {Promise<JitsiLocalTrack[]>}
  120. */
  121. function createDesktopTrack () {
  122. return createLocalTracks('desktop').catch(function (err) {
  123. if (err === TrackErrors.FIREFOX_EXTENSION_NEEDED) {
  124. APP.UI.showExtensionRequiredDialog(
  125. config.desktopSharingFirefoxExtensionURL
  126. );
  127. }
  128. return Promise.reject(err);
  129. });
  130. }
  131. class ConferenceConnector {
  132. constructor(resolve, reject) {
  133. this._resolve = resolve;
  134. this._reject = reject;
  135. this.reconnectTimeout = null;
  136. room.on(ConferenceEvents.CONFERENCE_JOINED,
  137. this._handleConferenceJoined.bind(this));
  138. room.on(ConferenceEvents.CONFERENCE_FAILED,
  139. this._onConferenceFailed.bind(this));
  140. room.on(ConferenceEvents.CONFERENCE_ERROR,
  141. this._onConferenceError.bind(this));
  142. }
  143. _handleConferenceFailed(err, msg) {
  144. this._unsubscribe();
  145. this._reject(err);
  146. }
  147. _onConferenceFailed(err, ...params) {
  148. console.error('CONFERENCE FAILED:', err, ...params);
  149. switch (err) {
  150. // room is locked by the password
  151. case ConferenceErrors.PASSWORD_REQUIRED:
  152. APP.UI.markRoomLocked(true);
  153. roomLocker.requirePassword().then(function () {
  154. room.join(roomLocker.password);
  155. });
  156. break;
  157. case ConferenceErrors.CONNECTION_ERROR:
  158. {
  159. let [msg] = params;
  160. APP.UI.notifyConnectionFailed(msg);
  161. }
  162. break;
  163. case ConferenceErrors.VIDEOBRIDGE_NOT_AVAILABLE:
  164. APP.UI.notifyBridgeDown();
  165. break;
  166. // not enough rights to create conference
  167. case ConferenceErrors.AUTHENTICATION_REQUIRED:
  168. // schedule reconnect to check if someone else created the room
  169. this.reconnectTimeout = setTimeout(function () {
  170. room.join();
  171. }, 5000);
  172. // notify user that auth is required
  173. AuthHandler.requireAuth(room, roomLocker.password);
  174. break;
  175. case ConferenceErrors.RESERVATION_ERROR:
  176. {
  177. let [code, msg] = params;
  178. APP.UI.notifyReservationError(code, msg);
  179. }
  180. break;
  181. case ConferenceErrors.GRACEFUL_SHUTDOWN:
  182. APP.UI.notifyGracefulShutdown();
  183. break;
  184. case ConferenceErrors.JINGLE_FATAL_ERROR:
  185. APP.UI.notifyInternalError();
  186. break;
  187. case ConferenceErrors.CONFERENCE_DESTROYED:
  188. {
  189. let [reason] = params;
  190. APP.UI.hideStats();
  191. APP.UI.notifyConferenceDestroyed(reason);
  192. }
  193. break;
  194. case ConferenceErrors.FOCUS_DISCONNECTED:
  195. {
  196. let [focus, retrySec] = params;
  197. APP.UI.notifyFocusDisconnected(focus, retrySec);
  198. }
  199. break;
  200. case ConferenceErrors.FOCUS_LEFT:
  201. APP.UI.notifyFocusLeft();
  202. break;
  203. default:
  204. this._handleConferenceFailed(err, ...params);
  205. }
  206. }
  207. _onConferenceError(err, ...params) {
  208. console.error('CONFERENCE Error:', err, params);
  209. switch (err) {
  210. case ConferenceErrors.CHAT_ERROR:
  211. {
  212. let [code, msg] = params;
  213. APP.UI.showChatError(code, msg);
  214. }
  215. break;
  216. default:
  217. console.error("Unknown error.");
  218. }
  219. }
  220. _unsubscribe() {
  221. room.off(
  222. ConferenceEvents.CONFERENCE_JOINED, this._handleConferenceJoined);
  223. room.off(
  224. ConferenceEvents.CONFERENCE_FAILED, this._onConferenceFailed);
  225. if (this.reconnectTimeout !== null) {
  226. clearTimeout(this.reconnectTimeout);
  227. }
  228. AuthHandler.closeAuth();
  229. }
  230. _handleConferenceJoined() {
  231. this._unsubscribe();
  232. this._resolve();
  233. }
  234. connect() {
  235. room.join();
  236. }
  237. }
  238. export default {
  239. localId: undefined,
  240. isModerator: false,
  241. audioMuted: false,
  242. videoMuted: false,
  243. isSharingScreen: false,
  244. isDesktopSharingEnabled: false,
  245. /**
  246. * Open new connection and join to the conference.
  247. * @param {object} options
  248. * @param {string} roomName name of the conference
  249. * @returns {Promise}
  250. */
  251. init(options) {
  252. this.roomName = options.roomName;
  253. JitsiMeetJS.setLogLevel(JitsiMeetJS.logLevels.TRACE);
  254. return JitsiMeetJS.init(config).then(() => {
  255. return Promise.all([
  256. // try to retrieve audio and video
  257. createLocalTracks('audio', 'video')
  258. // if failed then try to retrieve only audio
  259. .catch(() => createLocalTracks('audio'))
  260. // if audio also failed then just return empty array
  261. .catch(() => []),
  262. connect(options.roomName)
  263. ]);
  264. }).then(([tracks, con]) => {
  265. console.log('initialized with %s local tracks', tracks.length);
  266. connection = con;
  267. this._createRoom(tracks);
  268. this.isDesktopSharingEnabled =
  269. JitsiMeetJS.isDesktopSharingEnabled();
  270. // update list of available devices
  271. if (JitsiMeetJS.isDeviceListAvailable() &&
  272. JitsiMeetJS.isDeviceChangeAvailable()) {
  273. JitsiMeetJS.enumerateDevices((devices) => {
  274. this.availableDevices = devices;
  275. APP.UI.onAvailableDevicesChanged();
  276. });
  277. }
  278. // XXX The API will take care of disconnecting from the XMPP server
  279. // (and, thus, leaving the room) on unload.
  280. return new Promise((resolve, reject) => {
  281. (new ConferenceConnector(resolve, reject)).connect();
  282. });
  283. });
  284. },
  285. /**
  286. * Check if id is id of the local user.
  287. * @param {string} id id to check
  288. * @returns {boolean}
  289. */
  290. isLocalId (id) {
  291. return this.localId === id;
  292. },
  293. /**
  294. * Simulates toolbar button click for audio mute. Used by shortcuts and API.
  295. * @param mute true for mute and false for unmute.
  296. */
  297. muteAudio (mute) {
  298. muteLocalAudio(mute);
  299. },
  300. /**
  301. * Returns whether local audio is muted or not.
  302. * @returns {boolean}
  303. */
  304. isLocalAudioMuted() {
  305. return this.audioMuted;
  306. },
  307. /**
  308. * Simulates toolbar button click for audio mute. Used by shortcuts and API.
  309. */
  310. toggleAudioMuted () {
  311. this.muteAudio(!this.audioMuted);
  312. },
  313. /**
  314. * Simulates toolbar button click for video mute. Used by shortcuts and API.
  315. * @param mute true for mute and false for unmute.
  316. */
  317. muteVideo (mute) {
  318. muteLocalVideo(mute);
  319. },
  320. /**
  321. * Simulates toolbar button click for video mute. Used by shortcuts and API.
  322. */
  323. toggleVideoMuted () {
  324. this.muteVideo(!this.videoMuted);
  325. },
  326. /**
  327. * Retrieve list of conference participants (without local user).
  328. * @returns {JitsiParticipant[]}
  329. */
  330. listMembers () {
  331. return room.getParticipants();
  332. },
  333. /**
  334. * Retrieve list of ids of conference participants (without local user).
  335. * @returns {string[]}
  336. */
  337. listMembersIds () {
  338. return room.getParticipants().map(p => p.getId());
  339. },
  340. /**
  341. * List of available cameras and microphones.
  342. */
  343. availableDevices: [],
  344. /**
  345. * Check if SIP is supported.
  346. * @returns {boolean}
  347. */
  348. sipGatewayEnabled () {
  349. return room.isSIPCallingSupported();
  350. },
  351. get membersCount () {
  352. return room.getParticipants().length + 1;
  353. },
  354. get startAudioMuted () {
  355. return room && room.getStartMutedPolicy().audio;
  356. },
  357. get startVideoMuted () {
  358. return room && room.getStartMutedPolicy().video;
  359. },
  360. /**
  361. * Returns true if the callstats integration is enabled, otherwise returns
  362. * false.
  363. *
  364. * @returns true if the callstats integration is enabled, otherwise returns
  365. * false.
  366. */
  367. isCallstatsEnabled () {
  368. return room.isCallstatsEnabled();
  369. },
  370. /**
  371. * Sends the given feedback through CallStats if enabled.
  372. *
  373. * @param overallFeedback an integer between 1 and 5 indicating the
  374. * user feedback
  375. * @param detailedFeedback detailed feedback from the user. Not yet used
  376. */
  377. sendFeedback (overallFeedback, detailedFeedback) {
  378. return room.sendFeedback (overallFeedback, detailedFeedback);
  379. },
  380. // used by torture currently
  381. isJoined () {
  382. return this._room
  383. && this._room.isJoined();
  384. },
  385. getConnectionState () {
  386. return this._room
  387. && this._room.getConnectionState();
  388. },
  389. getMyUserId () {
  390. return this._room
  391. && this._room.myUserId();
  392. },
  393. /**
  394. * Will be filled with values only when config.debug is enabled.
  395. * Its used by torture to check audio levels.
  396. */
  397. audioLevelsMap: {},
  398. /**
  399. * Returns the stored audio level (stored only if config.debug is enabled)
  400. * @param id the id for the user audio level to return (the id value is
  401. * returned for the participant using getMyUserId() method)
  402. */
  403. getPeerSSRCAudioLevel (id) {
  404. return this.audioLevelsMap[id];
  405. },
  406. /**
  407. * Will check for number of remote particiapnts that have at least one
  408. * remote track.
  409. * @return {boolean} whether we have enough participants with remote streams
  410. */
  411. checkEnoughParticipants (number) {
  412. var participants = this._room.getParticipants();
  413. var foundParticipants = 0;
  414. for (var i = 0; i < participants.length; i += 1) {
  415. if (participants[i].getTracks().length > 0) {
  416. foundParticipants++;
  417. }
  418. }
  419. return foundParticipants >= number;
  420. },
  421. /**
  422. * Returns the stats.
  423. */
  424. getStats() {
  425. return ConnectionQuality.getStats();
  426. },
  427. // end used by torture
  428. getLogs () {
  429. return room.getLogs();
  430. },
  431. _createRoom (localTracks) {
  432. room = connection.initJitsiConference(APP.conference.roomName,
  433. this._getConferenceOptions());
  434. this.localId = room.myUserId();
  435. localTracks.forEach((track) => {
  436. if (track.isAudioTrack()) {
  437. this.useAudioStream(track);
  438. } else if (track.isVideoTrack()) {
  439. this.useVideoStream(track);
  440. }
  441. });
  442. roomLocker = createRoomLocker(room);
  443. this._room = room; // FIXME do not use this
  444. let email = APP.settings.getEmail();
  445. email && sendEmail(email);
  446. let nick = APP.settings.getDisplayName();
  447. if (config.useNicks && !nick) {
  448. nick = APP.UI.askForNickname();
  449. APP.settings.setDisplayName(nick);
  450. }
  451. nick && room.setDisplayName(nick);
  452. this._setupListeners();
  453. },
  454. _getConferenceOptions() {
  455. let options = config;
  456. if(config.enableRecording) {
  457. options.recordingType = (config.hosts &&
  458. (typeof config.hosts.jirecon != "undefined"))?
  459. "jirecon" : "colibri";
  460. }
  461. return options;
  462. },
  463. /**
  464. * Start using provided video stream.
  465. * Stops previous video stream.
  466. * @param {JitsiLocalTrack} [stream] new stream to use or null
  467. * @returns {Promise}
  468. */
  469. useVideoStream (stream) {
  470. let promise = Promise.resolve();
  471. if (localVideo) {
  472. // this calls room.removeTrack internally
  473. // so we don't need to remove it manually
  474. promise = localVideo.stop();
  475. }
  476. localVideo = stream;
  477. return promise.then(function () {
  478. if (stream) {
  479. return room.addTrack(stream);
  480. }
  481. }).then(() => {
  482. if (stream) {
  483. this.videoMuted = stream.isMuted();
  484. this.isSharingScreen = stream.videoType === 'desktop';
  485. APP.UI.addLocalStream(stream);
  486. } else {
  487. this.videoMuted = false;
  488. this.isSharingScreen = false;
  489. }
  490. APP.UI.setVideoMuted(this.localId, this.videoMuted);
  491. APP.UI.updateDesktopSharingButtons();
  492. });
  493. },
  494. /**
  495. * Start using provided audio stream.
  496. * Stops previous audio stream.
  497. * @param {JitsiLocalTrack} [stream] new stream to use or null
  498. * @returns {Promise}
  499. */
  500. useAudioStream (stream) {
  501. let promise = Promise.resolve();
  502. if (localAudio) {
  503. // this calls room.removeTrack internally
  504. // so we don't need to remove it manually
  505. promise = localAudio.stop();
  506. }
  507. localAudio = stream;
  508. return promise.then(function () {
  509. if (stream) {
  510. return room.addTrack(stream);
  511. }
  512. }).then(() => {
  513. if (stream) {
  514. this.audioMuted = stream.isMuted();
  515. APP.UI.addLocalStream(stream);
  516. } else {
  517. this.audioMuted = false;
  518. }
  519. APP.UI.setAudioMuted(this.localId, this.audioMuted);
  520. });
  521. },
  522. videoSwitchInProgress: false,
  523. toggleScreenSharing (shareScreen = !this.isSharingScreen) {
  524. if (this.videoSwitchInProgress) {
  525. console.warn("Switch in progress.");
  526. return;
  527. }
  528. if (!this.isDesktopSharingEnabled) {
  529. console.warn("Cannot toggle screen sharing: not supported.");
  530. return;
  531. }
  532. this.videoSwitchInProgress = true;
  533. if (shareScreen) {
  534. createDesktopTrack().then(([stream]) => {
  535. stream.on(
  536. TrackEvents.TRACK_STOPPED,
  537. () => {
  538. // if stream was stopped during screensharing session
  539. // then we should switch to video
  540. // otherwise we stopped it because we already switched
  541. // to video, so nothing to do here
  542. if (this.isSharingScreen) {
  543. this.toggleScreenSharing(false);
  544. }
  545. }
  546. );
  547. return this.useVideoStream(stream);
  548. }).then(() => {
  549. this.videoSwitchInProgress = false;
  550. console.log('sharing local desktop');
  551. }).catch((err) => {
  552. this.videoSwitchInProgress = false;
  553. this.toggleScreenSharing(false);
  554. console.error('failed to share local desktop', err);
  555. });
  556. } else {
  557. createLocalTracks('video').then(
  558. ([stream]) => this.useVideoStream(stream)
  559. ).then(() => {
  560. this.videoSwitchInProgress = false;
  561. console.log('sharing local video');
  562. }).catch((err) => {
  563. this.useVideoStream(null);
  564. this.videoSwitchInProgress = false;
  565. console.error('failed to share local video', err);
  566. });
  567. }
  568. },
  569. /**
  570. * Setup interaction between conference and UI.
  571. */
  572. _setupListeners () {
  573. // add local streams when joined to the conference
  574. room.on(ConferenceEvents.CONFERENCE_JOINED, () => {
  575. APP.UI.updateAuthInfo(room.isAuthEnabled(), room.getAuthLogin());
  576. APP.UI.mucJoined();
  577. });
  578. room.on(ConferenceEvents.USER_JOINED, (id, user) => {
  579. console.log('USER %s connnected', id, user);
  580. APP.API.notifyUserJoined(id);
  581. // FIXME email???
  582. APP.UI.addUser(id, user.getDisplayName());
  583. // chek the roles for the new user and reflect them
  584. APP.UI.updateUserRole(user);
  585. });
  586. room.on(ConferenceEvents.USER_LEFT, (id, user) => {
  587. console.log('USER %s LEFT', id, user);
  588. APP.API.notifyUserLeft(id);
  589. APP.UI.removeUser(id, user.getDisplayName());
  590. APP.UI.stopPrezi(id);
  591. });
  592. room.on(ConferenceEvents.USER_ROLE_CHANGED, (id, role) => {
  593. if (this.isLocalId(id)) {
  594. console.info(`My role changed, new role: ${role}`);
  595. this.isModerator = room.isModerator();
  596. APP.UI.updateLocalRole(room.isModerator());
  597. } else {
  598. let user = room.getParticipantById(id);
  599. if (user) {
  600. APP.UI.updateUserRole(user);
  601. }
  602. }
  603. });
  604. room.on(ConferenceEvents.TRACK_ADDED, (track) => {
  605. if(!track || track.isLocal())
  606. return;
  607. track.on(TrackEvents.TRACK_VIDEOTYPE_CHANGED, (type) => {
  608. APP.UI.onPeerVideoTypeChanged(track.getParticipantId(), type);
  609. });
  610. APP.UI.addRemoteStream(track);
  611. });
  612. room.on(ConferenceEvents.TRACK_REMOVED, (track) => {
  613. // FIXME handle
  614. });
  615. room.on(ConferenceEvents.TRACK_MUTE_CHANGED, (track) => {
  616. if(!track)
  617. return;
  618. const handler = (track.getType() === "audio")?
  619. APP.UI.setAudioMuted : APP.UI.setVideoMuted;
  620. let id;
  621. const mute = track.isMuted();
  622. if(track.isLocal()){
  623. id = this.localId;
  624. if(track.getType() === "audio") {
  625. this.audioMuted = mute;
  626. } else {
  627. this.videoMuted = mute;
  628. }
  629. } else {
  630. id = track.getParticipantId();
  631. }
  632. handler(id , mute);
  633. });
  634. room.on(ConferenceEvents.TRACK_AUDIO_LEVEL_CHANGED, (id, lvl) => {
  635. if(this.isLocalId(id) && localAudio && localAudio.isMuted()) {
  636. lvl = 0;
  637. }
  638. if(config.debug)
  639. {
  640. this.audioLevelsMap[id] = lvl;
  641. console.log("AudioLevel:" + id + "/" + lvl);
  642. }
  643. APP.UI.setAudioLevel(id, lvl);
  644. });
  645. room.on(ConferenceEvents.IN_LAST_N_CHANGED, (inLastN) => {
  646. //FIXME
  647. if (config.muteLocalVideoIfNotInLastN) {
  648. // TODO mute or unmute if required
  649. // mark video on UI
  650. // APP.UI.markVideoMuted(true/false);
  651. }
  652. });
  653. room.on(
  654. ConferenceEvents.LAST_N_ENDPOINTS_CHANGED, (ids, enteringIds) => {
  655. APP.UI.handleLastNEndpoints(ids, enteringIds);
  656. });
  657. room.on(ConferenceEvents.DOMINANT_SPEAKER_CHANGED, (id) => {
  658. APP.UI.markDominantSpeaker(id);
  659. });
  660. if (!interfaceConfig.filmStripOnly) {
  661. room.on(ConferenceEvents.CONNECTION_INTERRUPTED, () => {
  662. APP.UI.markVideoInterrupted(true);
  663. });
  664. room.on(ConferenceEvents.CONNECTION_RESTORED, () => {
  665. APP.UI.markVideoInterrupted(false);
  666. });
  667. room.on(ConferenceEvents.MESSAGE_RECEIVED, (id, text, ts) => {
  668. let nick = getDisplayName(id);
  669. APP.API.notifyReceivedChatMessage(id, nick, text, ts);
  670. APP.UI.addMessage(id, nick, text, ts);
  671. });
  672. }
  673. room.on(ConferenceEvents.DISPLAY_NAME_CHANGED, (id, displayName) => {
  674. APP.API.notifyDisplayNameChanged(id, displayName);
  675. APP.UI.changeDisplayName(id, displayName);
  676. });
  677. room.on(ConferenceEvents.RECORDING_STATE_CHANGED, (status, error) => {
  678. if(status == "error") {
  679. console.error(error);
  680. return;
  681. }
  682. APP.UI.updateRecordingState(status);
  683. });
  684. room.on(ConferenceEvents.USER_STATUS_CHANGED, function (id, status) {
  685. APP.UI.updateUserStatus(id, status);
  686. });
  687. room.on(ConferenceEvents.KICKED, () => {
  688. APP.UI.hideStats();
  689. APP.UI.notifyKicked();
  690. // FIXME close
  691. });
  692. room.on(ConferenceEvents.DTMF_SUPPORT_CHANGED, (isDTMFSupported) => {
  693. APP.UI.updateDTMFSupport(isDTMFSupported);
  694. });
  695. room.on(ConferenceEvents.FIREFOX_EXTENSION_NEEDED, function (url) {
  696. APP.UI.notifyFirefoxExtensionRequired(url);
  697. });
  698. APP.UI.addListener(UIEvents.ROOM_LOCK_CLICKED, () => {
  699. if (room.isModerator()) {
  700. let promise = roomLocker.isLocked
  701. ? roomLocker.askToUnlock()
  702. : roomLocker.askToLock();
  703. promise.then(() => {
  704. APP.UI.markRoomLocked(roomLocker.isLocked);
  705. });
  706. } else {
  707. roomLocker.notifyModeratorRequired();
  708. }
  709. });
  710. APP.UI.addListener(UIEvents.AUDIO_MUTED, muteLocalAudio);
  711. APP.UI.addListener(UIEvents.VIDEO_MUTED, muteLocalVideo);
  712. if (!interfaceConfig.filmStripOnly) {
  713. APP.UI.addListener(UIEvents.MESSAGE_CREATED, (message) => {
  714. APP.API.notifySendingChatMessage(message);
  715. room.sendTextMessage(message);
  716. });
  717. }
  718. room.on(ConferenceEvents.CONNECTION_STATS, function (stats) {
  719. ConnectionQuality.updateLocalStats(stats);
  720. });
  721. ConnectionQuality.addListener(
  722. CQEvents.LOCALSTATS_UPDATED,
  723. (percent, stats) => {
  724. APP.UI.updateLocalStats(percent, stats);
  725. // send local stats to other users
  726. room.sendCommandOnce(Commands.CONNECTION_QUALITY, {
  727. children: ConnectionQuality.convertToMUCStats(stats),
  728. attributes: {
  729. xmlns: 'http://jitsi.org/jitmeet/stats'
  730. }
  731. });
  732. }
  733. );
  734. // listen to remote stats
  735. room.addCommandListener(Commands.CONNECTION_QUALITY,(values, from) => {
  736. ConnectionQuality.updateRemoteStats(from, values);
  737. });
  738. ConnectionQuality.addListener(CQEvents.REMOTESTATS_UPDATED,
  739. (id, percent, stats) => {
  740. APP.UI.updateRemoteStats(id, percent, stats);
  741. });
  742. room.addCommandListener(Commands.ETHERPAD, ({value}) => {
  743. APP.UI.initEtherpad(value);
  744. });
  745. room.addCommandListener(Commands.PREZI, ({value, attributes}) => {
  746. APP.UI.showPrezi(attributes.id, value, attributes.slide);
  747. });
  748. room.addCommandListener(Commands.STOP_PREZI, ({attributes}) => {
  749. APP.UI.stopPrezi(attributes.id);
  750. });
  751. APP.UI.addListener(UIEvents.SHARE_PREZI, (url, slide) => {
  752. console.log('Sharing Prezi %s slide %s', url, slide);
  753. room.removeCommand(Commands.PREZI);
  754. room.sendCommand(Commands.PREZI, {
  755. value: url,
  756. attributes: {
  757. id: room.myUserId(),
  758. slide
  759. }
  760. });
  761. });
  762. APP.UI.addListener(UIEvents.STOP_SHARING_PREZI, () => {
  763. room.removeCommand(Commands.PREZI);
  764. room.sendCommandOnce(Commands.STOP_PREZI, {
  765. attributes: {
  766. id: room.myUserId()
  767. }
  768. });
  769. });
  770. APP.UI.addListener(UIEvents.EMAIL_CHANGED, (email) => {
  771. APP.settings.setEmail(email);
  772. APP.UI.setUserAvatar(room.myUserId(), email);
  773. sendEmail(email);
  774. });
  775. room.addCommandListener(Commands.EMAIL, (data) => {
  776. APP.UI.setUserAvatar(data.attributes.id, data.value);
  777. });
  778. APP.UI.addListener(UIEvents.NICKNAME_CHANGED, (nickname) => {
  779. APP.settings.setDisplayName(nickname);
  780. room.setDisplayName(nickname);
  781. APP.UI.changeDisplayName(APP.conference.localId, nickname);
  782. });
  783. APP.UI.addListener(UIEvents.START_MUTED_CHANGED,
  784. (startAudioMuted, startVideoMuted) => {
  785. room.setStartMutedPolicy({audio: startAudioMuted,
  786. video: startVideoMuted});
  787. }
  788. );
  789. room.on(
  790. ConferenceEvents.START_MUTED_POLICY_CHANGED,
  791. (policy) => {
  792. APP.UI.onStartMutedChanged();
  793. }
  794. );
  795. room.on(ConferenceEvents.STARTED_MUTED, () => {
  796. (room.isStartAudioMuted() || room.isStartVideoMuted())
  797. && APP.UI.notifyInitiallyMuted();
  798. });
  799. APP.UI.addListener(UIEvents.USER_INVITED, (roomUrl) => {
  800. APP.UI.inviteParticipants(
  801. roomUrl,
  802. APP.conference.roomName,
  803. roomLocker.password,
  804. APP.settings.getDisplayName()
  805. );
  806. });
  807. room.on(
  808. ConferenceEvents.AVAILABLE_DEVICES_CHANGED, function (id, devices) {
  809. APP.UI.updateDevicesAvailability(id, devices);
  810. }
  811. );
  812. // call hangup
  813. APP.UI.addListener(UIEvents.HANGUP, () => {
  814. APP.UI.requestFeedback().then(() => {
  815. connection.disconnect();
  816. config.enableWelcomePage && setTimeout(() => {
  817. window.localStorage.welcomePageDisabled = false;
  818. window.location.pathname = "/";
  819. }, 3000);
  820. }, (err) => {console.error(err);});
  821. });
  822. // logout
  823. APP.UI.addListener(UIEvents.LOGOUT, () => {
  824. // FIXME handle logout
  825. // APP.xmpp.logout(function (url) {
  826. // if (url) {
  827. // window.location.href = url;
  828. // } else {
  829. // hangup();
  830. // }
  831. // });
  832. });
  833. APP.UI.addListener(UIEvents.SIP_DIAL, (sipNumber) => {
  834. room.dial(sipNumber);
  835. });
  836. // Starts or stops the recording for the conference.
  837. APP.UI.addListener(UIEvents.RECORDING_TOGGLE, (predefinedToken) => {
  838. if (predefinedToken) {
  839. room.toggleRecording({token: predefinedToken});
  840. return;
  841. }
  842. APP.UI.requestRecordingToken().then((token) => {
  843. room.toggleRecording({token: token});
  844. });
  845. });
  846. APP.UI.addListener(UIEvents.SUBJECT_CHANGED, (topic) => {
  847. room.setSubject(topic);
  848. });
  849. room.on(ConferenceEvents.SUBJECT_CHANGED, function (subject) {
  850. APP.UI.setSubject(subject);
  851. });
  852. APP.UI.addListener(UIEvents.USER_KICKED, (id) => {
  853. room.kickParticipant(id);
  854. });
  855. APP.UI.addListener(UIEvents.REMOTE_AUDIO_MUTED, (id) => {
  856. room.muteParticipant(id);
  857. });
  858. APP.UI.addListener(UIEvents.AUTH_CLICKED, () => {
  859. AuthHandler.authenticate(room);
  860. });
  861. APP.UI.addListener(UIEvents.SELECTED_ENDPOINT, (id) => {
  862. room.selectParticipant(id);
  863. });
  864. APP.UI.addListener(UIEvents.PINNED_ENDPOINT, (id) => {
  865. room.pinParticipant(id);
  866. });
  867. APP.UI.addListener(
  868. UIEvents.VIDEO_DEVICE_CHANGED,
  869. (cameraDeviceId) => {
  870. APP.settings.setCameraDeviceId(cameraDeviceId);
  871. createLocalTracks('video').then(([stream]) => {
  872. this.useVideoStream(stream);
  873. console.log('switched local video device');
  874. });
  875. }
  876. );
  877. APP.UI.addListener(
  878. UIEvents.AUDIO_DEVICE_CHANGED,
  879. (micDeviceId) => {
  880. APP.settings.setMicDeviceId(micDeviceId);
  881. createLocalTracks('audio').then(([stream]) => {
  882. this.useAudioStream(stream);
  883. console.log('switched local audio device');
  884. });
  885. }
  886. );
  887. APP.UI.addListener(
  888. UIEvents.TOGGLE_SCREENSHARING, this.toggleScreenSharing.bind(this)
  889. );
  890. }
  891. };