You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

conference.js 27KB

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