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

JitsiConference.js 36KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093
  1. /* global Strophe, $, Promise */
  2. /* jshint -W101 */
  3. var logger = require("jitsi-meet-logger").getLogger(__filename);
  4. var RTC = require("./modules/RTC/RTC");
  5. var XMPPEvents = require("./service/xmpp/XMPPEvents");
  6. var AuthenticationEvents = require("./service/authentication/AuthenticationEvents");
  7. var RTCEvents = require("./service/RTC/RTCEvents");
  8. var EventEmitter = require("events");
  9. var JitsiConferenceEvents = require("./JitsiConferenceEvents");
  10. var JitsiConferenceErrors = require("./JitsiConferenceErrors");
  11. var JitsiParticipant = require("./JitsiParticipant");
  12. var Statistics = require("./modules/statistics/statistics");
  13. var JitsiDTMFManager = require('./modules/DTMF/JitsiDTMFManager');
  14. var JitsiTrackEvents = require("./JitsiTrackEvents");
  15. var Settings = require("./modules/settings/Settings");
  16. /**
  17. * Creates a JitsiConference object with the given name and properties.
  18. * Note: this constructor is not a part of the public API (objects should be
  19. * created using JitsiConnection.createConference).
  20. * @param options.config properties / settings related to the conference that will be created.
  21. * @param options.name the name of the conference
  22. * @param options.connection the JitsiConnection object for this JitsiConference.
  23. * @constructor
  24. */
  25. function JitsiConference(options) {
  26. if(!options.name || options.name.toLowerCase() !== options.name) {
  27. logger.error("Invalid conference name (no conference name passed or it"
  28. + "contains invalid characters like capital letters)!");
  29. return;
  30. }
  31. this.options = options;
  32. this.connection = this.options.connection;
  33. this.xmpp = this.connection.xmpp;
  34. this.eventEmitter = new EventEmitter();
  35. var confID = this.options.name + '@' + this.xmpp.options.hosts.muc;
  36. this.settings = new Settings(confID);
  37. this.room = this.xmpp.createRoom(this.options.name, this.options.config,
  38. this.settings);
  39. this.room.updateDeviceAvailability(RTC.getDeviceAvailability());
  40. this.rtc = new RTC(this.room, options);
  41. this.statistics = new Statistics({
  42. callStatsID: this.options.config.callStatsID,
  43. callStatsSecret: this.options.config.callStatsSecret,
  44. disableThirdPartyRequests: this.options.config.disableThirdPartyRequests
  45. });
  46. setupListeners(this);
  47. JitsiMeetJS._gumFailedHandler.push(function(error) {
  48. this.statistics.sendGetUserMediaFailed(error);
  49. }.bind(this));
  50. this.participants = {};
  51. this.lastDominantSpeaker = null;
  52. this.dtmfManager = null;
  53. this.somebodySupportsDTMF = false;
  54. this.authEnabled = false;
  55. this.authIdentity;
  56. this.startAudioMuted = false;
  57. this.startVideoMuted = false;
  58. this.startMutedPolicy = {audio: false, video: false};
  59. this.availableDevices = {
  60. audio: undefined,
  61. video: undefined
  62. };
  63. this.isMutedByFocus = false;
  64. }
  65. /**
  66. * Joins the conference.
  67. * @param password {string} the password
  68. */
  69. JitsiConference.prototype.join = function (password) {
  70. if(this.room)
  71. this.room.join(password);
  72. };
  73. /**
  74. * Check if joined to the conference.
  75. */
  76. JitsiConference.prototype.isJoined = function () {
  77. return this.room && this.room.joined;
  78. };
  79. /**
  80. * Leaves the conference.
  81. */
  82. JitsiConference.prototype.leave = function () {
  83. if(this.xmpp && this.room)
  84. this.xmpp.leaveRoom(this.room.roomjid);
  85. this.room = null;
  86. };
  87. /**
  88. * Returns name of this conference.
  89. */
  90. JitsiConference.prototype.getName = function () {
  91. return this.options.name;
  92. };
  93. /**
  94. * Check if authentication is enabled for this conference.
  95. */
  96. JitsiConference.prototype.isAuthEnabled = function () {
  97. return this.authEnabled;
  98. };
  99. /**
  100. * Check if user is logged in.
  101. */
  102. JitsiConference.prototype.isLoggedIn = function () {
  103. return !!this.authIdentity;
  104. };
  105. /**
  106. * Get authorized login.
  107. */
  108. JitsiConference.prototype.getAuthLogin = function () {
  109. return this.authIdentity;
  110. };
  111. /**
  112. * Check if external authentication is enabled for this conference.
  113. */
  114. JitsiConference.prototype.isExternalAuthEnabled = function () {
  115. return this.room && this.room.moderator.isExternalAuthEnabled();
  116. };
  117. /**
  118. * Get url for external authentication.
  119. * @param {boolean} [urlForPopup] if true then return url for login popup,
  120. * else url of login page.
  121. * @returns {Promise}
  122. */
  123. JitsiConference.prototype.getExternalAuthUrl = function (urlForPopup) {
  124. return new Promise(function (resolve, reject) {
  125. if (!this.isExternalAuthEnabled()) {
  126. reject();
  127. return;
  128. }
  129. if (urlForPopup) {
  130. this.room.moderator.getPopupLoginUrl(resolve, reject);
  131. } else {
  132. this.room.moderator.getLoginUrl(resolve, reject);
  133. }
  134. }.bind(this));
  135. };
  136. /**
  137. * Returns the local tracks.
  138. */
  139. JitsiConference.prototype.getLocalTracks = function () {
  140. if (this.rtc) {
  141. return this.rtc.localStreams;
  142. } else {
  143. return [];
  144. }
  145. };
  146. /**
  147. * Attaches a handler for events(For example - "participant joined".) in the conference. All possible event are defined
  148. * in JitsiConferenceEvents.
  149. * @param eventId the event ID.
  150. * @param handler handler for the event.
  151. *
  152. * Note: consider adding eventing functionality by extending an EventEmitter impl, instead of rolling ourselves
  153. */
  154. JitsiConference.prototype.on = function (eventId, handler) {
  155. if(this.eventEmitter)
  156. this.eventEmitter.on(eventId, handler);
  157. };
  158. /**
  159. * Removes event listener
  160. * @param eventId the event ID.
  161. * @param [handler] optional, the specific handler to unbind
  162. *
  163. * Note: consider adding eventing functionality by extending an EventEmitter impl, instead of rolling ourselves
  164. */
  165. JitsiConference.prototype.off = function (eventId, handler) {
  166. if(this.eventEmitter)
  167. this.eventEmitter.removeListener(eventId, handler);
  168. };
  169. // Common aliases for event emitter
  170. JitsiConference.prototype.addEventListener = JitsiConference.prototype.on;
  171. JitsiConference.prototype.removeEventListener = JitsiConference.prototype.off;
  172. /**
  173. * Receives notifications from another participants for commands / custom events
  174. * (send by sendPresenceCommand method).
  175. * @param command {String} the name of the command
  176. * @param handler {Function} handler for the command
  177. */
  178. JitsiConference.prototype.addCommandListener = function (command, handler) {
  179. if(this.room)
  180. this.room.addPresenceListener(command, handler);
  181. };
  182. /**
  183. * Removes command listener
  184. * @param command {String} the name of the command
  185. */
  186. JitsiConference.prototype.removeCommandListener = function (command) {
  187. if(this.room)
  188. this.room.removePresenceListener(command);
  189. };
  190. /**
  191. * Sends text message to the other participants in the conference
  192. * @param message the text message.
  193. */
  194. JitsiConference.prototype.sendTextMessage = function (message) {
  195. if(this.room)
  196. this.room.sendMessage(message);
  197. };
  198. /**
  199. * Send presence command.
  200. * @param name the name of the command.
  201. * @param values Object with keys and values that will be send.
  202. **/
  203. JitsiConference.prototype.sendCommand = function (name, values) {
  204. if(this.room) {
  205. this.room.addToPresence(name, values);
  206. this.room.sendPresence();
  207. }
  208. };
  209. /**
  210. * Send presence command one time.
  211. * @param name the name of the command.
  212. * @param values Object with keys and values that will be send.
  213. **/
  214. JitsiConference.prototype.sendCommandOnce = function (name, values) {
  215. this.sendCommand(name, values);
  216. this.removeCommand(name);
  217. };
  218. /**
  219. * Send presence command.
  220. * @param name the name of the command.
  221. * @param values Object with keys and values that will be send.
  222. * @param persistent if false the command will be sent only one time
  223. **/
  224. JitsiConference.prototype.removeCommand = function (name) {
  225. if(this.room)
  226. this.room.removeFromPresence(name);
  227. };
  228. /**
  229. * Sets the display name for this conference.
  230. * @param name the display name to set
  231. */
  232. JitsiConference.prototype.setDisplayName = function(name) {
  233. if(this.room){
  234. // remove previously set nickname
  235. this.room.removeFromPresence("nick");
  236. this.room.addToPresence("nick", {attributes: {xmlns: 'http://jabber.org/protocol/nick'}, value: name});
  237. this.room.sendPresence();
  238. }
  239. };
  240. /**
  241. * Set new subject for this conference. (available only for moderator)
  242. * @param {string} subject new subject
  243. */
  244. JitsiConference.prototype.setSubject = function (subject) {
  245. if (this.room && this.isModerator()) {
  246. this.room.setSubject(subject);
  247. }
  248. };
  249. /**
  250. * Adds JitsiLocalTrack object to the conference.
  251. * @param track the JitsiLocalTrack object.
  252. */
  253. JitsiConference.prototype.addTrack = function (track) {
  254. this.room.addStream(track.getOriginalStream(), function () {
  255. this.rtc.addLocalStream(track);
  256. if (track.startMuted) {
  257. track.mute();
  258. }
  259. track.muteHandler = this._fireMuteChangeEvent.bind(this, track);
  260. track.stopHandler = this.removeTrack.bind(this, track);
  261. track.audioLevelHandler = this._fireAudioLevelChangeEvent.bind(this);
  262. track.addEventListener(JitsiTrackEvents.TRACK_MUTE_CHANGED,
  263. track.muteHandler);
  264. track.addEventListener(JitsiTrackEvents.TRACK_STOPPED,
  265. track.stopHandler);
  266. track.addEventListener(JitsiTrackEvents.TRACK_AUDIO_LEVEL_CHANGED,
  267. track.audioLevelHandler);
  268. this.eventEmitter.emit(JitsiConferenceEvents.TRACK_ADDED, track);
  269. }.bind(this));
  270. };
  271. /**
  272. * Fires TRACK_AUDIO_LEVEL_CHANGED change conference event.
  273. * @param audioLevel the audio level
  274. */
  275. JitsiConference.prototype._fireAudioLevelChangeEvent = function (audioLevel) {
  276. this.eventEmitter.emit(
  277. JitsiConferenceEvents.TRACK_AUDIO_LEVEL_CHANGED,
  278. this.myUserId(), audioLevel);
  279. };
  280. /**
  281. * Fires TRACK_MUTE_CHANGED change conference event.
  282. * @param track the JitsiTrack object related to the event.
  283. */
  284. JitsiConference.prototype._fireMuteChangeEvent = function (track) {
  285. // check if track was muted by focus and now is unmuted by user
  286. if (this.isMutedByFocus && track.isAudioTrack() && !track.isMuted()) {
  287. this.isMutedByFocus = false;
  288. // unmute local user on server
  289. this.room.muteParticipant(this.room.myroomjid, false);
  290. }
  291. this.eventEmitter.emit(JitsiConferenceEvents.TRACK_MUTE_CHANGED, track);
  292. };
  293. /**
  294. * Removes JitsiLocalTrack object to the conference.
  295. * @param track the JitsiLocalTrack object.
  296. */
  297. JitsiConference.prototype.removeTrack = function (track) {
  298. if(!this.room){
  299. if(this.rtc)
  300. this.rtc.removeLocalStream(track);
  301. return;
  302. }
  303. this.room.removeStream(track.getOriginalStream(), function(){
  304. this.rtc.removeLocalStream(track);
  305. track.removeEventListener(JitsiTrackEvents.TRACK_MUTE_CHANGED, track.muteHandler);
  306. track.removeEventListener(JitsiTrackEvents.TRACK_STOPPED, track.stopHandler);
  307. track.removeEventListener(JitsiTrackEvents.TRACK_AUDIO_LEVEL_CHANGED, track.audioLevelHandler);
  308. this.eventEmitter.emit(JitsiConferenceEvents.TRACK_REMOVED, track);
  309. }.bind(this));
  310. };
  311. /**
  312. * Get role of the local user.
  313. * @returns {string} user role: 'moderator' or 'none'
  314. */
  315. JitsiConference.prototype.getRole = function () {
  316. return this.room.role;
  317. };
  318. /**
  319. * Check if local user is moderator.
  320. * @returns {boolean} true if local user is moderator, false otherwise.
  321. */
  322. JitsiConference.prototype.isModerator = function () {
  323. return this.room.isModerator();
  324. };
  325. /**
  326. * Set password for the room.
  327. * @param {string} password new password for the room.
  328. * @returns {Promise}
  329. */
  330. JitsiConference.prototype.lock = function (password) {
  331. if (!this.isModerator()) {
  332. return Promise.reject();
  333. }
  334. var conference = this;
  335. return new Promise(function (resolve, reject) {
  336. conference.room.lockRoom(password || "", function () {
  337. resolve();
  338. }, function (err) {
  339. reject(err);
  340. }, function () {
  341. reject(JitsiConferenceErrors.PASSWORD_NOT_SUPPORTED);
  342. });
  343. });
  344. };
  345. /**
  346. * Remove password from the room.
  347. * @returns {Promise}
  348. */
  349. JitsiConference.prototype.unlock = function () {
  350. return this.lock();
  351. };
  352. /**
  353. * Elects the participant with the given id to be the selected participant or the speaker.
  354. * @param id the identifier of the participant
  355. */
  356. JitsiConference.prototype.selectParticipant = function(participantId) {
  357. if (this.rtc) {
  358. this.rtc.selectedEndpoint(participantId);
  359. }
  360. };
  361. /**
  362. *
  363. * @param id the identifier of the participant
  364. */
  365. JitsiConference.prototype.pinParticipant = function(participantId) {
  366. if (this.rtc) {
  367. this.rtc.pinEndpoint(participantId);
  368. }
  369. };
  370. /**
  371. * Returns the list of participants for this conference.
  372. * @return Array<JitsiParticipant> a list of participant identifiers containing all conference participants.
  373. */
  374. JitsiConference.prototype.getParticipants = function() {
  375. return Object.keys(this.participants).map(function (key) {
  376. return this.participants[key];
  377. }, this);
  378. };
  379. /**
  380. * @returns {JitsiParticipant} the participant in this conference with the specified id (or
  381. * undefined if there isn't one).
  382. * @param id the id of the participant.
  383. */
  384. JitsiConference.prototype.getParticipantById = function(id) {
  385. return this.participants[id];
  386. };
  387. /**
  388. * Kick participant from this conference.
  389. * @param {string} id id of the participant to kick
  390. */
  391. JitsiConference.prototype.kickParticipant = function (id) {
  392. var participant = this.getParticipantById(id);
  393. if (!participant) {
  394. return;
  395. }
  396. this.room.kick(participant.getJid());
  397. };
  398. /**
  399. * Kick participant from this conference.
  400. * @param {string} id id of the participant to kick
  401. */
  402. JitsiConference.prototype.muteParticipant = function (id) {
  403. var participant = this.getParticipantById(id);
  404. if (!participant) {
  405. return;
  406. }
  407. this.room.muteParticipant(participant.getJid(), true);
  408. };
  409. JitsiConference.prototype.onMemberJoined = function (jid, nick, role) {
  410. var id = Strophe.getResourceFromJid(jid);
  411. if (id === 'focus' || this.myUserId() === id) {
  412. return;
  413. }
  414. var participant = new JitsiParticipant(jid, this, nick);
  415. participant._role = role;
  416. this.participants[id] = participant;
  417. this.eventEmitter.emit(JitsiConferenceEvents.USER_JOINED, id, participant);
  418. this.xmpp.connection.disco.info(
  419. jid, "node", function(iq) {
  420. participant._supportsDTMF = $(iq).find(
  421. '>query>feature[var="urn:xmpp:jingle:dtmf:0"]').length > 0;
  422. this.updateDTMFSupport();
  423. }.bind(this)
  424. );
  425. };
  426. JitsiConference.prototype.onMemberLeft = function (jid) {
  427. var id = Strophe.getResourceFromJid(jid);
  428. if (id === 'focus' || this.myUserId() === id) {
  429. return;
  430. }
  431. var participant = this.participants[id];
  432. delete this.participants[id];
  433. this.eventEmitter.emit(JitsiConferenceEvents.USER_LEFT, id, participant);
  434. };
  435. JitsiConference.prototype.onUserRoleChanged = function (jid, role) {
  436. var id = Strophe.getResourceFromJid(jid);
  437. var participant = this.getParticipantById(id);
  438. if (!participant) {
  439. return;
  440. }
  441. participant._role = role;
  442. this.eventEmitter.emit(JitsiConferenceEvents.USER_ROLE_CHANGED, id, role);
  443. };
  444. JitsiConference.prototype.onDisplayNameChanged = function (jid, displayName) {
  445. var id = Strophe.getResourceFromJid(jid);
  446. var participant = this.getParticipantById(id);
  447. if (!participant) {
  448. return;
  449. }
  450. participant._displayName = displayName;
  451. this.eventEmitter.emit(JitsiConferenceEvents.DISPLAY_NAME_CHANGED, id, displayName);
  452. };
  453. JitsiConference.prototype.onTrackAdded = function (track) {
  454. var id = track.getParticipantId();
  455. var participant = this.getParticipantById(id);
  456. if (!participant) {
  457. return;
  458. }
  459. // add track to JitsiParticipant
  460. participant._tracks.push(track);
  461. var emitter = this.eventEmitter;
  462. track.addEventListener(
  463. JitsiTrackEvents.TRACK_STOPPED,
  464. function () {
  465. // remove track from JitsiParticipant
  466. var pos = participant._tracks.indexOf(track);
  467. if (pos > -1) {
  468. participant._tracks.splice(pos, 1);
  469. }
  470. emitter.emit(JitsiConferenceEvents.TRACK_REMOVED, track);
  471. }
  472. );
  473. track.addEventListener(
  474. JitsiTrackEvents.TRACK_MUTE_CHANGED,
  475. function () {
  476. emitter.emit(JitsiConferenceEvents.TRACK_MUTE_CHANGED, track);
  477. }
  478. );
  479. track.addEventListener(
  480. JitsiTrackEvents.TRACK_AUDIO_LEVEL_CHANGED,
  481. function (audioLevel) {
  482. emitter.emit(JitsiConferenceEvents.TRACK_AUDIO_LEVEL_CHANGED, id, audioLevel);
  483. }
  484. );
  485. this.eventEmitter.emit(JitsiConferenceEvents.TRACK_ADDED, track);
  486. };
  487. JitsiConference.prototype.updateDTMFSupport = function () {
  488. var somebodySupportsDTMF = false;
  489. var participants = this.getParticipants();
  490. // check if at least 1 participant supports DTMF
  491. for (var i = 0; i < participants.length; i += 1) {
  492. if (participants[i].supportsDTMF()) {
  493. somebodySupportsDTMF = true;
  494. break;
  495. }
  496. }
  497. if (somebodySupportsDTMF !== this.somebodySupportsDTMF) {
  498. this.somebodySupportsDTMF = somebodySupportsDTMF;
  499. this.eventEmitter.emit(JitsiConferenceEvents.DTMF_SUPPORT_CHANGED, somebodySupportsDTMF);
  500. }
  501. };
  502. /**
  503. * Allows to check if there is at least one user in the conference
  504. * that supports DTMF.
  505. * @returns {boolean} true if somebody supports DTMF, false otherwise
  506. */
  507. JitsiConference.prototype.isDTMFSupported = function () {
  508. return this.somebodySupportsDTMF;
  509. };
  510. /**
  511. * Returns the local user's ID
  512. * @return {string} local user's ID
  513. */
  514. JitsiConference.prototype.myUserId = function () {
  515. return (this.room && this.room.myroomjid)? Strophe.getResourceFromJid(this.room.myroomjid) : null;
  516. };
  517. JitsiConference.prototype.sendTones = function (tones, duration, pause) {
  518. if (!this.dtmfManager) {
  519. var connection = this.xmpp.connection.jingle.activecall.peerconnection;
  520. if (!connection) {
  521. logger.warn("cannot sendTones: no conneciton");
  522. return;
  523. }
  524. var tracks = this.getLocalTracks().filter(function (track) {
  525. return track.isAudioTrack();
  526. });
  527. if (!tracks.length) {
  528. logger.warn("cannot sendTones: no local audio stream");
  529. return;
  530. }
  531. this.dtmfManager = new JitsiDTMFManager(tracks[0], connection);
  532. }
  533. this.dtmfManager.sendTones(tones, duration, pause);
  534. };
  535. /**
  536. * Returns true if the recording is supproted and false if not.
  537. */
  538. JitsiConference.prototype.isRecordingSupported = function () {
  539. if(this.room)
  540. return this.room.isRecordingSupported();
  541. return false;
  542. };
  543. /**
  544. * Returns null if the recording is not supported, "on" if the recording started
  545. * and "off" if the recording is not started.
  546. */
  547. JitsiConference.prototype.getRecordingState = function () {
  548. if(this.room)
  549. return this.room.getRecordingState();
  550. return "off";
  551. }
  552. /**
  553. * Returns the url of the recorded video.
  554. */
  555. JitsiConference.prototype.getRecordingURL = function () {
  556. if(this.room)
  557. return this.room.getRecordingURL();
  558. return null;
  559. }
  560. /**
  561. * Starts/stops the recording
  562. */
  563. JitsiConference.prototype.toggleRecording = function (options) {
  564. if(this.room)
  565. return this.room.toggleRecording(options, function (status, error) {
  566. this.eventEmitter.emit(
  567. JitsiConferenceEvents.RECORDING_STATE_CHANGED, status, error);
  568. }.bind(this));
  569. this.eventEmitter.emit(
  570. JitsiConferenceEvents.RECORDING_STATE_CHANGED, "error",
  571. new Error("The conference is not created yet!"));
  572. }
  573. /**
  574. * Returns true if the SIP calls are supported and false otherwise
  575. */
  576. JitsiConference.prototype.isSIPCallingSupported = function () {
  577. if(this.room)
  578. return this.room.isSIPCallingSupported();
  579. return false;
  580. }
  581. /**
  582. * Dials a number.
  583. * @param number the number
  584. */
  585. JitsiConference.prototype.dial = function (number) {
  586. if(this.room)
  587. return this.room.dial(number);
  588. return new Promise(function(resolve, reject){
  589. reject(new Error("The conference is not created yet!"))});
  590. }
  591. /**
  592. * Hangup an existing call
  593. */
  594. JitsiConference.prototype.hangup = function () {
  595. if(this.room)
  596. return this.room.hangup();
  597. return new Promise(function(resolve, reject){
  598. reject(new Error("The conference is not created yet!"))});
  599. }
  600. /**
  601. * Returns the phone number for joining the conference.
  602. */
  603. JitsiConference.prototype.getPhoneNumber = function () {
  604. if(this.room)
  605. return this.room.getPhoneNumber();
  606. return null;
  607. }
  608. /**
  609. * Returns the pin for joining the conference with phone.
  610. */
  611. JitsiConference.prototype.getPhonePin = function () {
  612. if(this.room)
  613. return this.room.getPhonePin();
  614. return null;
  615. }
  616. /**
  617. * Returns the connection state for the current room. Its ice connection state
  618. * for its session.
  619. */
  620. JitsiConference.prototype.getConnectionState = function () {
  621. if(this.room)
  622. return this.room.getConnectionState();
  623. return null;
  624. }
  625. /**
  626. * Make all new participants mute their audio/video on join.
  627. * @param policy {Object} object with 2 boolean properties for video and audio:
  628. * @param {boolean} audio if audio should be muted.
  629. * @param {boolean} video if video should be muted.
  630. */
  631. JitsiConference.prototype.setStartMutedPolicy = function (policy) {
  632. if (!this.isModerator()) {
  633. return;
  634. }
  635. this.startMutedPolicy = policy;
  636. this.room.removeFromPresence("startmuted");
  637. this.room.addToPresence("startmuted", {
  638. attributes: {
  639. audio: policy.audio,
  640. video: policy.video,
  641. xmlns: 'http://jitsi.org/jitmeet/start-muted'
  642. }
  643. });
  644. this.room.sendPresence();
  645. };
  646. /**
  647. * Returns current start muted policy
  648. * @returns {Object} with 2 proprties - audio and video.
  649. */
  650. JitsiConference.prototype.getStartMutedPolicy = function () {
  651. return this.startMutedPolicy;
  652. };
  653. /**
  654. * Check if audio is muted on join.
  655. */
  656. JitsiConference.prototype.isStartAudioMuted = function () {
  657. return this.startAudioMuted;
  658. };
  659. /**
  660. * Check if video is muted on join.
  661. */
  662. JitsiConference.prototype.isStartVideoMuted = function () {
  663. return this.startVideoMuted;
  664. };
  665. /**
  666. * Get object with internal logs.
  667. */
  668. JitsiConference.prototype.getLogs = function () {
  669. var data = this.xmpp.getJingleLog();
  670. var metadata = {};
  671. metadata.time = new Date();
  672. metadata.url = window.location.href;
  673. metadata.ua = navigator.userAgent;
  674. var log = this.xmpp.getXmppLog();
  675. if (log) {
  676. metadata.xmpp = log;
  677. }
  678. data.metadata = metadata;
  679. return data;
  680. };
  681. /**
  682. * Sends the given feedback through CallStats if enabled.
  683. *
  684. * @param overallFeedback an integer between 1 and 5 indicating the
  685. * user feedback
  686. * @param detailedFeedback detailed feedback from the user. Not yet used
  687. */
  688. JitsiConference.prototype.sendFeedback =
  689. function(overallFeedback, detailedFeedback){
  690. this.statistics.sendFeedback(overallFeedback, detailedFeedback);
  691. }
  692. /**
  693. * Returns true if the callstats integration is enabled, otherwise returns
  694. * false.
  695. *
  696. * @returns true if the callstats integration is enabled, otherwise returns
  697. * false.
  698. */
  699. JitsiConference.prototype.isCallstatsEnabled = function () {
  700. return this.statistics.isCallstatsEnabled();
  701. }
  702. /**
  703. * Setups the listeners needed for the conference.
  704. * @param conference the conference
  705. */
  706. function setupListeners(conference) {
  707. conference.xmpp.addListener(XMPPEvents.CALL_INCOMING, function (event) {
  708. conference.rtc.onIncommingCall(event);
  709. conference.statistics.startRemoteStats(event.peerconnection);
  710. });
  711. conference.room.addListener(XMPPEvents.REMOTE_STREAM_RECEIVED,
  712. function (data, sid, thessrc) {
  713. var track = conference.rtc.createRemoteStream(data, sid, thessrc);
  714. if (track) {
  715. conference.onTrackAdded(track);
  716. }
  717. }
  718. );
  719. conference.rtc.addListener(RTCEvents.FAKE_VIDEO_TRACK_CREATED,
  720. function (track) {
  721. conference.onTrackAdded(track);
  722. }
  723. );
  724. conference.room.addListener(XMPPEvents.AUDIO_MUTED_BY_FOCUS,
  725. function (value) {
  726. conference.rtc.setAudioMute(value);
  727. conference.isMutedByFocus = true;
  728. }
  729. );
  730. conference.room.addListener(XMPPEvents.SUBJECT_CHANGED, function (subject) {
  731. conference.eventEmitter.emit(JitsiConferenceEvents.SUBJECT_CHANGED, subject);
  732. });
  733. conference.room.addListener(XMPPEvents.MUC_JOINED, function () {
  734. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_JOINED);
  735. });
  736. conference.room.addListener(XMPPEvents.ROOM_JOIN_ERROR, function (pres) {
  737. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.CONNECTION_ERROR, pres);
  738. });
  739. conference.room.addListener(XMPPEvents.ROOM_CONNECT_ERROR, function (pres) {
  740. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.CONNECTION_ERROR, pres);
  741. });
  742. conference.room.addListener(XMPPEvents.PASSWORD_REQUIRED, function (pres) {
  743. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.PASSWORD_REQUIRED, pres);
  744. });
  745. conference.room.addListener(XMPPEvents.AUTHENTICATION_REQUIRED, function () {
  746. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.AUTHENTICATION_REQUIRED);
  747. });
  748. conference.room.addListener(XMPPEvents.BRIDGE_DOWN, function () {
  749. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.VIDEOBRIDGE_NOT_AVAILABLE);
  750. });
  751. conference.room.addListener(XMPPEvents.RESERVATION_ERROR, function (code, msg) {
  752. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.RESERVATION_ERROR, code, msg);
  753. });
  754. conference.room.addListener(XMPPEvents.GRACEFUL_SHUTDOWN, function () {
  755. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.GRACEFUL_SHUTDOWN);
  756. });
  757. conference.room.addListener(XMPPEvents.JINGLE_FATAL_ERROR, function () {
  758. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.JINGLE_FATAL_ERROR);
  759. });
  760. conference.room.addListener(XMPPEvents.MUC_DESTROYED, function (reason) {
  761. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.CONFERENCE_DESTROYED, reason);
  762. });
  763. conference.room.addListener(XMPPEvents.CHAT_ERROR_RECEIVED, function (err, msg) {
  764. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_ERROR, JitsiConferenceErrors.CHAT_ERROR, err, msg);
  765. });
  766. conference.room.addListener(XMPPEvents.FOCUS_DISCONNECTED, function (focus, retrySec) {
  767. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.FOCUS_DISCONNECTED, focus, retrySec);
  768. });
  769. // FIXME
  770. // conference.room.addListener(XMPPEvents.MUC_JOINED, function () {
  771. // conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_LEFT);
  772. // });
  773. conference.room.addListener(XMPPEvents.KICKED, function () {
  774. conference.eventEmitter.emit(JitsiConferenceEvents.KICKED);
  775. });
  776. conference.room.addListener(XMPPEvents.MUC_MEMBER_JOINED, conference.onMemberJoined.bind(conference));
  777. conference.room.addListener(XMPPEvents.MUC_MEMBER_LEFT, conference.onMemberLeft.bind(conference));
  778. conference.room.addListener(XMPPEvents.DISPLAY_NAME_CHANGED, conference.onDisplayNameChanged.bind(conference));
  779. conference.room.addListener(XMPPEvents.LOCAL_ROLE_CHANGED, function (role) {
  780. conference.eventEmitter.emit(JitsiConferenceEvents.USER_ROLE_CHANGED, conference.myUserId(), role);
  781. });
  782. conference.room.addListener(XMPPEvents.MUC_ROLE_CHANGED, conference.onUserRoleChanged.bind(conference));
  783. conference.room.addListener(XMPPEvents.CONNECTION_INTERRUPTED, function () {
  784. conference.eventEmitter.emit(JitsiConferenceEvents.CONNECTION_INTERRUPTED);
  785. });
  786. conference.room.addListener(XMPPEvents.RECORDING_STATE_CHANGED,
  787. function () {
  788. conference.eventEmitter.emit(
  789. JitsiConferenceEvents.RECORDING_STATE_CHANGED);
  790. });
  791. conference.room.addListener(XMPPEvents.PHONE_NUMBER_CHANGED, function () {
  792. conference.eventEmitter.emit(
  793. JitsiConferenceEvents.PHONE_NUMBER_CHANGED);
  794. });
  795. conference.room.addListener(XMPPEvents.CONNECTION_RESTORED, function () {
  796. conference.eventEmitter.emit(JitsiConferenceEvents.CONNECTION_RESTORED);
  797. });
  798. conference.room.addListener(XMPPEvents.CONFERENCE_SETUP_FAILED, function () {
  799. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.SETUP_FAILED);
  800. });
  801. conference.room.addListener(AuthenticationEvents.IDENTITY_UPDATED, function (authEnabled, authIdentity) {
  802. conference.authEnabled = authEnabled;
  803. conference.authIdentity = authIdentity;
  804. });
  805. conference.room.addListener(XMPPEvents.MESSAGE_RECEIVED, function (jid, displayName, txt, myJid, ts) {
  806. var id = Strophe.getResourceFromJid(jid);
  807. conference.eventEmitter.emit(JitsiConferenceEvents.MESSAGE_RECEIVED, id, txt, ts);
  808. });
  809. conference.room.addListener(XMPPEvents.PRESENCE_STATUS, function (jid, status) {
  810. var id = Strophe.getResourceFromJid(jid);
  811. var participant = conference.getParticipantById(id);
  812. if (!participant || participant._status === status) {
  813. return;
  814. }
  815. participant._status = status;
  816. conference.eventEmitter.emit(JitsiConferenceEvents.USER_STATUS_CHANGED, id, status);
  817. });
  818. conference.rtc.addListener(RTCEvents.DOMINANTSPEAKER_CHANGED, function (id) {
  819. if(conference.lastDominantSpeaker !== id && conference.room) {
  820. conference.lastDominantSpeaker = id;
  821. conference.eventEmitter.emit(JitsiConferenceEvents.DOMINANT_SPEAKER_CHANGED, id);
  822. }
  823. });
  824. conference.rtc.addListener(RTCEvents.LASTN_CHANGED, function (oldValue, newValue) {
  825. conference.eventEmitter.emit(JitsiConferenceEvents.IN_LAST_N_CHANGED, oldValue, newValue);
  826. });
  827. conference.rtc.addListener(RTCEvents.LASTN_ENDPOINT_CHANGED,
  828. function (lastNEndpoints, endpointsEnteringLastN) {
  829. conference.eventEmitter.emit(JitsiConferenceEvents.LAST_N_ENDPOINTS_CHANGED,
  830. lastNEndpoints, endpointsEnteringLastN);
  831. });
  832. conference.xmpp.addListener(XMPPEvents.PASSWORD_REQUIRED, function () {
  833. conference.eventEmitter.emit(JitsiConferenceErrors.PASSWORD_REQUIRED);
  834. });
  835. conference.xmpp.addListener(XMPPEvents.START_MUTED_FROM_FOCUS,
  836. function (audioMuted, videoMuted) {
  837. conference.startAudioMuted = audioMuted;
  838. conference.startVideoMuted = videoMuted;
  839. // mute existing local tracks because this is initial mute from
  840. // Jicofo
  841. conference.getLocalTracks().forEach(function (track) {
  842. if (conference.startAudioMuted && track.isAudioTrack()) {
  843. track.mute();
  844. }
  845. if (conference.startVideoMuted && track.isVideoTrack()) {
  846. track.mute();
  847. }
  848. });
  849. conference.eventEmitter.emit(JitsiConferenceEvents.STARTED_MUTED);
  850. });
  851. conference.room.addPresenceListener("startmuted", function (data, from) {
  852. var isModerator = false;
  853. if (conference.myUserId() === from && conference.isModerator()) {
  854. isModerator = true;
  855. } else {
  856. var participant = conference.getParticipantById(from);
  857. if (participant && participant.isModerator()) {
  858. isModerator = true;
  859. }
  860. }
  861. if (!isModerator) {
  862. return;
  863. }
  864. var startAudioMuted = data.attributes.audio === 'true';
  865. var startVideoMuted = data.attributes.video === 'true';
  866. var updated = false;
  867. if (startAudioMuted !== conference.startMutedPolicy.audio) {
  868. conference.startMutedPolicy.audio = startAudioMuted;
  869. updated = true;
  870. }
  871. if (startVideoMuted !== conference.startMutedPolicy.video) {
  872. conference.startMutedPolicy.video = startVideoMuted;
  873. updated = true;
  874. }
  875. if (updated) {
  876. conference.eventEmitter.emit(
  877. JitsiConferenceEvents.START_MUTED_POLICY_CHANGED,
  878. conference.startMutedPolicy
  879. );
  880. }
  881. });
  882. conference.rtc.addListener(RTCEvents.AVAILABLE_DEVICES_CHANGED, function (devices) {
  883. conference.room.updateDeviceAvailability(devices);
  884. });
  885. conference.room.addPresenceListener("devices", function (data, from) {
  886. var isAudioAvailable = false;
  887. var isVideoAvailable = false;
  888. data.children.forEach(function (config) {
  889. if (config.tagName === 'audio') {
  890. isAudioAvailable = config.value === 'true';
  891. }
  892. if (config.tagName === 'video') {
  893. isVideoAvailable = config.value === 'true';
  894. }
  895. });
  896. var availableDevices;
  897. if (conference.myUserId() === from) {
  898. availableDevices = conference.availableDevices;
  899. } else {
  900. var participant = conference.getParticipantById(from);
  901. if (!participant) {
  902. return;
  903. }
  904. availableDevices = participant._availableDevices;
  905. }
  906. var updated = false;
  907. if (availableDevices.audio !== isAudioAvailable) {
  908. updated = true;
  909. availableDevices.audio = isAudioAvailable;
  910. }
  911. if (availableDevices.video !== isVideoAvailable) {
  912. updated = true;
  913. availableDevices.video = isVideoAvailable;
  914. }
  915. if (updated) {
  916. conference.eventEmitter.emit(JitsiConferenceEvents.AVAILABLE_DEVICES_CHANGED, from, availableDevices);
  917. }
  918. });
  919. if(conference.statistics) {
  920. //FIXME: Maybe remove event should not be associated with the conference.
  921. conference.statistics.addAudioLevelListener(function (ssrc, level) {
  922. var userId = null;
  923. var jid = conference.room.getJidBySSRC(ssrc);
  924. if (!jid)
  925. return;
  926. conference.rtc.setAudioLevel(jid, level);
  927. });
  928. conference.xmpp.addListener(XMPPEvents.DISPOSE_CONFERENCE,
  929. function () {
  930. conference.statistics.dispose();
  931. });
  932. conference.room.addListener(XMPPEvents.PEERCONNECTION_READY,
  933. function (session) {
  934. conference.statistics.startCallStats(
  935. session, conference.settings);
  936. });
  937. conference.room.addListener(XMPPEvents.CONFERENCE_SETUP_FAILED,
  938. function () {
  939. conference.statistics.sendSetupFailedEvent();
  940. });
  941. conference.on(JitsiConferenceEvents.TRACK_MUTE_CHANGED,
  942. function (track) {
  943. if(!track.isLocal())
  944. return;
  945. var type = (track.getType() === "audio")? "audio" : "video";
  946. conference.statistics.sendMuteEvent(track.isMuted(), type);
  947. });
  948. conference.room.addListener(XMPPEvents.CREATE_OFFER_FAILED, function (e, pc) {
  949. conference.statistics.sendCreateOfferFailed(e, pc);
  950. });
  951. conference.room.addListener(XMPPEvents.CREATE_ANSWER_FAILED, function (e, pc) {
  952. conference.statistics.sendCreateAnswerFailed(e, pc);
  953. });
  954. conference.room.addListener(XMPPEvents.SET_LOCAL_DESCRIPTION_FAILED,
  955. function (e, pc) {
  956. conference.statistics.sendSetLocalDescFailed(e, pc);
  957. }
  958. );
  959. conference.room.addListener(XMPPEvents.SET_REMOTE_DESCRIPTION_FAILED,
  960. function (e, pc) {
  961. conference.statistics.sendSetRemoteDescFailed(e, pc);
  962. }
  963. );
  964. conference.room.addListener(XMPPEvents.ADD_ICE_CANDIDATE_FAILED,
  965. function (e, pc) {
  966. conference.statistics.sendAddIceCandidateFailed(e, pc);
  967. }
  968. );
  969. }
  970. }
  971. module.exports = JitsiConference;