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

statistics.js 25KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829
  1. import EventEmitter from 'events';
  2. import * as JitsiConferenceEvents from '../../JitsiConferenceEvents';
  3. import JitsiTrackError from '../../JitsiTrackError';
  4. import { FEEDBACK } from '../../service/statistics/AnalyticsEvents';
  5. import * as StatisticsEvents from '../../service/statistics/Events';
  6. import browser from '../browser';
  7. import ScriptUtil from '../util/ScriptUtil';
  8. import analytics from './AnalyticsAdapter';
  9. import CallStats from './CallStats';
  10. import LocalStats from './LocalStatsCollector';
  11. import { PerformanceObserverStats } from './PerformanceObserverStats';
  12. import RTPStats from './RTPStatsCollector';
  13. import { CALLSTATS_SCRIPT_URL } from './constants';
  14. const logger = require('jitsi-meet-logger').getLogger(__filename);
  15. /**
  16. * Stores all active {@link Statistics} instances.
  17. * @type {Set<Statistics>}
  18. */
  19. let _instances;
  20. /**
  21. * True if callstats API is loaded
  22. */
  23. let isCallstatsLoaded = false;
  24. /**
  25. * Since callstats.io is a third party, we cannot guarantee the quality of their
  26. * service. More specifically, their server may take noticeably long time to
  27. * respond. Consequently, it is in our best interest (in the sense that the
  28. * intergration of callstats.io is pretty important to us but not enough to
  29. * allow it to prevent people from joining a conference) to (1) start
  30. * downloading their API as soon as possible and (2) do the downloading
  31. * asynchronously.
  32. *
  33. * @param {StatisticsOptions} options - Options to use for downloading and
  34. * initializing callstats backend.
  35. */
  36. function loadCallStatsAPI(options) {
  37. if (!isCallstatsLoaded) {
  38. ScriptUtil.loadScript(
  39. options.customScriptUrl || CALLSTATS_SCRIPT_URL,
  40. /* async */ true,
  41. /* prepend */ true,
  42. /* relativeURL */ undefined,
  43. /* loadCallback */ () => _initCallStatsBackend(options)
  44. );
  45. isCallstatsLoaded = true;
  46. }
  47. }
  48. /**
  49. * Initializes Callstats backend.
  50. *
  51. * @param {StatisticsOptions} options - The options to use for initializing
  52. * callstats backend.
  53. * @private
  54. */
  55. function _initCallStatsBackend(options) {
  56. if (CallStats.isBackendInitialized()) {
  57. return;
  58. }
  59. if (!CallStats.initBackend({
  60. callStatsID: options.callStatsID,
  61. callStatsSecret: options.callStatsSecret,
  62. userName: options.userName,
  63. aliasName: options.aliasName,
  64. applicationName: options.applicationName,
  65. getWiFiStatsMethod: options.getWiFiStatsMethod,
  66. confID: options.confID,
  67. siteID: options.siteID
  68. })) {
  69. logger.error('CallStats Backend initialization failed bad');
  70. }
  71. }
  72. /**
  73. * callstats strips any additional fields from Error except for "name", "stack",
  74. * "message" and "constraintName". So we need to bundle additional information
  75. * from JitsiTrackError into error passed to callstats to preserve valuable
  76. * information about error.
  77. * @param {JitsiTrackError} error
  78. */
  79. function formatJitsiTrackErrorForCallStats(error) {
  80. const err = new Error();
  81. // Just copy original stack from error
  82. err.stack = error.stack;
  83. // Combine name from error's name plus (possibly) name of original GUM error
  84. err.name = (error.name || 'Unknown error') + (error.gum && error.gum.error
  85. && error.gum.error.name ? ` - ${error.gum.error.name}` : '');
  86. // Put all constraints into this field. For constraint failed errors we will
  87. // still know which exactly constraint failed as it will be a part of
  88. // message.
  89. err.constraintName = error.gum && error.gum.constraints
  90. ? JSON.stringify(error.gum.constraints) : '';
  91. // Just copy error's message.
  92. err.message = error.message;
  93. return err;
  94. }
  95. /**
  96. * Init statistic options
  97. * @param options
  98. */
  99. Statistics.init = function(options) {
  100. Statistics.audioLevelsEnabled = !options.disableAudioLevels;
  101. if (typeof options.pcStatsInterval === 'number') {
  102. Statistics.pcStatsInterval = options.pcStatsInterval;
  103. }
  104. if (typeof options.audioLevelsInterval === 'number') {
  105. Statistics.audioLevelsInterval = options.audioLevelsInterval;
  106. }
  107. if (typeof options.longTasksStatsInterval === 'number') {
  108. Statistics.longTasksStatsInterval = options.longTasksStatsInterval;
  109. }
  110. Statistics.disableThirdPartyRequests = options.disableThirdPartyRequests;
  111. };
  112. /**
  113. * The options to configure Statistics.
  114. * @typedef {Object} StatisticsOptions
  115. * @property {string} applicationName - The application name to pass to
  116. * callstats.
  117. * @property {string} aliasName - The alias name to use when initializing callstats.
  118. * @property {string} userName - The user name to use when initializing callstats.
  119. * @property {string} confID - The callstats conference ID to use.
  120. * @property {string} callStatsID - Callstats credentials - the id.
  121. * @property {string} callStatsSecret - Callstats credentials - the secret.
  122. * @property {string} customScriptUrl - A custom lib url to use when downloading
  123. * callstats library.
  124. * @property {string} roomName - The room name we are currently in.
  125. */
  126. /**
  127. *
  128. * @param xmpp
  129. * @param {StatisticsOptions} options - The options to use creating the
  130. * Statistics.
  131. */
  132. export default function Statistics(xmpp, options) {
  133. /**
  134. * {@link RTPStats} mapped by {@link TraceablePeerConnection.id} which
  135. * collect RTP statistics for each peerconnection.
  136. * @type {Map<string, RTPStats}
  137. */
  138. this.rtpStatsMap = new Map();
  139. this.eventEmitter = new EventEmitter();
  140. this.xmpp = xmpp;
  141. this.options = options || {};
  142. this.callStatsIntegrationEnabled
  143. = this.options.callStatsID && this.options.callStatsSecret && this.options.enableCallStats
  144. // Even though AppID and AppSecret may be specified, the integration
  145. // of callstats.io may be disabled because of globally-disallowed
  146. // requests to any third parties.
  147. && (Statistics.disableThirdPartyRequests !== true);
  148. if (this.callStatsIntegrationEnabled) {
  149. this.callStatsApplicationLogsDisabled
  150. = this.options.callStatsApplicationLogsDisabled;
  151. if (browser.isReactNative()) {
  152. _initCallStatsBackend(this.options);
  153. } else {
  154. loadCallStatsAPI(this.options);
  155. }
  156. if (!this.options.confID) {
  157. logger.warn('"confID" is not defined');
  158. }
  159. }
  160. /**
  161. * Stores {@link CallStats} instances for each
  162. * {@link TraceablePeerConnection} (one {@link CallStats} instance serves
  163. * one TPC). The instances are mapped by {@link TraceablePeerConnection.id}.
  164. * @type {Map<number, CallStats>}
  165. */
  166. this.callsStatsInstances = new Map();
  167. Statistics.instances.add(this);
  168. }
  169. Statistics.audioLevelsEnabled = false;
  170. Statistics.audioLevelsInterval = 200;
  171. Statistics.pcStatsInterval = 10000;
  172. Statistics.disableThirdPartyRequests = false;
  173. Statistics.analytics = analytics;
  174. Object.defineProperty(Statistics, 'instances', {
  175. /**
  176. * Returns the Set holding all active {@link Statistics} instances. Lazily
  177. * initializes the Set to allow any Set polyfills to be applied.
  178. * @type {Set<Statistics>}
  179. */
  180. get() {
  181. if (!_instances) {
  182. _instances = new Set();
  183. }
  184. return _instances;
  185. }
  186. });
  187. /**
  188. * Starts collecting RTP stats for given peerconnection.
  189. * @param {TraceablePeerConnection} peerconnection
  190. */
  191. Statistics.prototype.startRemoteStats = function(peerconnection) {
  192. this.stopRemoteStats(peerconnection);
  193. try {
  194. const rtpStats
  195. = new RTPStats(
  196. peerconnection,
  197. Statistics.audioLevelsInterval,
  198. Statistics.pcStatsInterval,
  199. this.eventEmitter);
  200. rtpStats.start(Statistics.audioLevelsEnabled);
  201. this.rtpStatsMap.set(peerconnection.id, rtpStats);
  202. } catch (e) {
  203. logger.error(`Failed to start collecting remote statistics: ${e}`);
  204. }
  205. };
  206. Statistics.localStats = [];
  207. Statistics.startLocalStats = function(stream, callback) {
  208. if (!Statistics.audioLevelsEnabled) {
  209. return;
  210. }
  211. const localStats = new LocalStats(stream, Statistics.audioLevelsInterval,
  212. callback);
  213. this.localStats.push(localStats);
  214. localStats.start();
  215. };
  216. Statistics.prototype.addAudioLevelListener = function(listener) {
  217. if (!Statistics.audioLevelsEnabled) {
  218. return;
  219. }
  220. this.eventEmitter.on(StatisticsEvents.AUDIO_LEVEL, listener);
  221. };
  222. Statistics.prototype.removeAudioLevelListener = function(listener) {
  223. if (!Statistics.audioLevelsEnabled) {
  224. return;
  225. }
  226. this.eventEmitter.removeListener(StatisticsEvents.AUDIO_LEVEL, listener);
  227. };
  228. Statistics.prototype.addBeforeDisposedListener = function(listener) {
  229. this.eventEmitter.on(StatisticsEvents.BEFORE_DISPOSED, listener);
  230. };
  231. Statistics.prototype.removeBeforeDisposedListener = function(listener) {
  232. this.eventEmitter.removeListener(
  233. StatisticsEvents.BEFORE_DISPOSED, listener);
  234. };
  235. Statistics.prototype.addConnectionStatsListener = function(listener) {
  236. this.eventEmitter.on(StatisticsEvents.CONNECTION_STATS, listener);
  237. };
  238. Statistics.prototype.removeConnectionStatsListener = function(listener) {
  239. this.eventEmitter.removeListener(
  240. StatisticsEvents.CONNECTION_STATS,
  241. listener);
  242. };
  243. Statistics.prototype.addByteSentStatsListener = function(listener) {
  244. this.eventEmitter.on(StatisticsEvents.BYTE_SENT_STATS, listener);
  245. };
  246. Statistics.prototype.removeByteSentStatsListener = function(listener) {
  247. this.eventEmitter.removeListener(StatisticsEvents.BYTE_SENT_STATS,
  248. listener);
  249. };
  250. /**
  251. * Add a listener that would be notified on a LONG_TASKS_STATS event.
  252. *
  253. * @param {Function} listener a function that would be called when notified.
  254. * @returns {void}
  255. */
  256. Statistics.prototype.addLongTasksStatsListener = function(listener) {
  257. this.eventEmitter.on(StatisticsEvents.LONG_TASKS_STATS, listener);
  258. };
  259. /**
  260. * Creates an instance of {@link PerformanceObserverStats} and starts the
  261. * observer that records the stats periodically.
  262. *
  263. * @returns {void}
  264. */
  265. Statistics.prototype.attachLongTasksStats = function(conference) {
  266. if (!browser.supportsPerformanceObserver()) {
  267. logger.warn('Performance observer for long tasks not supported by browser!');
  268. return;
  269. }
  270. this.performanceObserverStats = new PerformanceObserverStats(
  271. this.eventEmitter,
  272. Statistics.longTasksStatsInterval);
  273. conference.on(
  274. JitsiConferenceEvents.CONFERENCE_JOINED,
  275. () => this.performanceObserverStats.startObserver());
  276. conference.on(
  277. JitsiConferenceEvents.CONFERENCE_LEFT,
  278. () => this.performanceObserverStats.stopObserver());
  279. };
  280. /**
  281. * Obtains the current value of the LongTasks event statistics.
  282. *
  283. * @returns {Object|null} stats object if the observer has been
  284. * created, null otherwise.
  285. */
  286. Statistics.prototype.getLongTasksStats = function() {
  287. return this.performanceObserverStats
  288. ? this.performanceObserverStats.getLongTasksStats()
  289. : null;
  290. };
  291. /**
  292. * Removes the given listener for the LONG_TASKS_STATS event.
  293. *
  294. * @param {Function} listener the listener we want to remove.
  295. * @returns {void}
  296. */
  297. Statistics.prototype.removeLongTasksStatsListener = function(listener) {
  298. this.eventEmitter.removeListener(StatisticsEvents.LONG_TASKS_STATS, listener);
  299. };
  300. /**
  301. * Updates the list of speakers for which the audio levels are to be calculated. This is needed for the jvb pc only.
  302. *
  303. * @param {Array<string>} speakerList The list of remote endpoint ids.
  304. * @returns {void}
  305. */
  306. Statistics.prototype.setSpeakerList = function(speakerList) {
  307. for (const rtpStats of Array.from(this.rtpStatsMap.values())) {
  308. if (!rtpStats.peerconnection.isP2P) {
  309. rtpStats.setSpeakerList(speakerList);
  310. }
  311. }
  312. };
  313. Statistics.prototype.dispose = function() {
  314. try {
  315. // NOTE Before reading this please see the comment in stopCallStats...
  316. //
  317. // Here we prevent from emitting the event twice in case it will be
  318. // triggered from stopCallStats.
  319. // If the event is triggered from here it means that the logs will not
  320. // be submitted anyway (because there is no CallStats instance), but
  321. // we're doing that for the sake of some kind of consistency.
  322. if (!this.callsStatsInstances.size) {
  323. this.eventEmitter.emit(StatisticsEvents.BEFORE_DISPOSED);
  324. }
  325. for (const callStats of this.callsStatsInstances.values()) {
  326. this.stopCallStats(callStats.tpc);
  327. }
  328. for (const tpcId of this.rtpStatsMap.keys()) {
  329. this._stopRemoteStats(tpcId);
  330. }
  331. if (this.eventEmitter) {
  332. this.eventEmitter.removeAllListeners();
  333. }
  334. } finally {
  335. Statistics.instances.delete(this);
  336. }
  337. };
  338. Statistics.stopLocalStats = function(stream) {
  339. if (!Statistics.audioLevelsEnabled) {
  340. return;
  341. }
  342. for (let i = 0; i < Statistics.localStats.length; i++) {
  343. if (Statistics.localStats[i].stream === stream) {
  344. const localStats = Statistics.localStats.splice(i, 1);
  345. localStats[0].stop();
  346. break;
  347. }
  348. }
  349. };
  350. /**
  351. * Stops remote RTP stats for given peerconnection ID.
  352. * @param {string} tpcId {@link TraceablePeerConnection.id}
  353. * @private
  354. */
  355. Statistics.prototype._stopRemoteStats = function(tpcId) {
  356. const rtpStats = this.rtpStatsMap.get(tpcId);
  357. if (rtpStats) {
  358. rtpStats.stop();
  359. this.rtpStatsMap.delete(tpcId);
  360. }
  361. };
  362. /**
  363. * Stops collecting RTP stats for given peerconnection
  364. * @param {TraceablePeerConnection} tpc
  365. */
  366. Statistics.prototype.stopRemoteStats = function(tpc) {
  367. this._stopRemoteStats(tpc.id);
  368. };
  369. // CALSTATS METHODS
  370. /**
  371. * Initializes the callstats.io API.
  372. * @param {TraceablePeerConnection} tpc the {@link TraceablePeerConnection}
  373. * instance for which CalStats will be started.
  374. * @param {string} remoteUserID
  375. */
  376. Statistics.prototype.startCallStats = function(tpc, remoteUserID) {
  377. if (!this.callStatsIntegrationEnabled) {
  378. return;
  379. } else if (this.callsStatsInstances.has(tpc.id)) {
  380. logger.error('CallStats instance for ${tpc} exists already');
  381. return;
  382. }
  383. logger.info(`Starting CallStats for ${tpc}...`);
  384. const newInstance
  385. = new CallStats(
  386. tpc,
  387. {
  388. confID: this.options.confID,
  389. remoteUserID
  390. });
  391. this.callsStatsInstances.set(tpc.id, newInstance);
  392. };
  393. /**
  394. * Obtains the list of *all* {@link CallStats} instances collected from every
  395. * valid {@link Statistics} instance.
  396. * @return {Set<CallStats>}
  397. * @private
  398. */
  399. Statistics._getAllCallStatsInstances = function() {
  400. const csInstances = new Set();
  401. for (const statistics of Statistics.instances) {
  402. for (const cs of statistics.callsStatsInstances.values()) {
  403. csInstances.add(cs);
  404. }
  405. }
  406. return csInstances;
  407. };
  408. /**
  409. * Removes the callstats.io instances.
  410. */
  411. Statistics.prototype.stopCallStats = function(tpc) {
  412. const callStatsInstance = this.callsStatsInstances.get(tpc.id);
  413. if (callStatsInstance) {
  414. // FIXME the original purpose of adding BEFORE_DISPOSED event was to be
  415. // able to submit the last log batch from jitsi-meet to CallStats. After
  416. // recent changes we dispose the CallStats earlier
  417. // (before Statistics.dispose), so we need to emit this event here to
  418. // give this last chance for final log batch submission.
  419. //
  420. // Eventually there should be a separate module called "log storage"
  421. // which should emit proper events when it's underlying
  422. // CallStats instance is going away.
  423. if (this.callsStatsInstances.size === 1) {
  424. this.eventEmitter.emit(StatisticsEvents.BEFORE_DISPOSED);
  425. }
  426. this.callsStatsInstances.delete(tpc.id);
  427. // The fabric needs to be terminated when being stopped
  428. callStatsInstance.sendTerminateEvent();
  429. }
  430. };
  431. /**
  432. * Returns true if the callstats integration is enabled, otherwise returns
  433. * false.
  434. *
  435. * @returns true if the callstats integration is enabled, otherwise returns
  436. * false.
  437. */
  438. Statistics.prototype.isCallstatsEnabled = function() {
  439. return this.callStatsIntegrationEnabled;
  440. };
  441. /**
  442. * Logs either resume or hold event for the given peer connection.
  443. * @param {TraceablePeerConnection} tpc the connection for which event will be
  444. * reported
  445. * @param {boolean} isResume true for resume or false for hold
  446. */
  447. Statistics.prototype.sendConnectionResumeOrHoldEvent = function(tpc, isResume) {
  448. const instance = this.callsStatsInstances.get(tpc.id);
  449. if (instance) {
  450. instance.sendResumeOrHoldEvent(isResume);
  451. }
  452. };
  453. /**
  454. * Notifies CallStats and analytics (if present) for ice connection failed
  455. * @param {TraceablePeerConnection} tpc connection on which failure occurred.
  456. */
  457. Statistics.prototype.sendIceConnectionFailedEvent = function(tpc) {
  458. const instance = this.callsStatsInstances.get(tpc.id);
  459. if (instance) {
  460. instance.sendIceConnectionFailedEvent();
  461. }
  462. };
  463. /**
  464. * Notifies CallStats for mute events
  465. * @param {TraceablePeerConnection} tpc connection on which failure occurred.
  466. * @param {boolean} muted true for muted and false for not muted
  467. * @param {String} type "audio"/"video"
  468. */
  469. Statistics.prototype.sendMuteEvent = function(tpc, muted, type) {
  470. const instance = tpc && this.callsStatsInstances.get(tpc.id);
  471. CallStats.sendMuteEvent(muted, type, instance);
  472. };
  473. /**
  474. * Notifies CallStats for screen sharing events
  475. * @param start {boolean} true for starting screen sharing and
  476. * false for not stopping
  477. * @param {string|null} ssrc - optional ssrc value, used only when
  478. * starting screen sharing.
  479. */
  480. Statistics.prototype.sendScreenSharingEvent
  481. = function(start, ssrc) {
  482. for (const cs of this.callsStatsInstances.values()) {
  483. cs.sendScreenSharingEvent(start, ssrc);
  484. }
  485. };
  486. /**
  487. * Notifies the statistics module that we are now the dominant speaker of the
  488. * conference.
  489. * @param {String} roomJid - The room jid where the speaker event occurred.
  490. */
  491. Statistics.prototype.sendDominantSpeakerEvent = function(roomJid) {
  492. for (const cs of this.callsStatsInstances.values()) {
  493. cs.sendDominantSpeakerEvent();
  494. }
  495. // xmpp send dominant speaker event
  496. this.xmpp.sendDominantSpeakerEvent(roomJid);
  497. };
  498. /**
  499. * Notifies about active device.
  500. * @param {{deviceList: {String:String}}} devicesData - list of devices with
  501. * their data
  502. */
  503. Statistics.sendActiveDeviceListEvent = function(devicesData) {
  504. const globalSet = Statistics._getAllCallStatsInstances();
  505. if (globalSet.size) {
  506. for (const cs of globalSet) {
  507. CallStats.sendActiveDeviceListEvent(devicesData, cs);
  508. }
  509. } else {
  510. CallStats.sendActiveDeviceListEvent(devicesData, null);
  511. }
  512. };
  513. /* eslint-disable max-params */
  514. /**
  515. * Lets the underlying statistics module know where is given SSRC rendered by
  516. * providing renderer tag ID.
  517. * @param {TraceablePeerConnection} tpc the connection to which the stream
  518. * belongs to
  519. * @param {number} ssrc the SSRC of the stream
  520. * @param {boolean} isLocal
  521. * @param {string} userId
  522. * @param {string} usageLabel meaningful usage label of this stream like
  523. * 'microphone', 'camera' or 'screen'.
  524. * @param {string} containerId the id of media 'audio' or 'video' tag which
  525. * renders the stream.
  526. */
  527. Statistics.prototype.associateStreamWithVideoTag = function(
  528. tpc,
  529. ssrc,
  530. isLocal,
  531. userId,
  532. usageLabel,
  533. containerId) {
  534. const instance = this.callsStatsInstances.get(tpc.id);
  535. if (instance) {
  536. instance.associateStreamWithVideoTag(
  537. ssrc,
  538. isLocal,
  539. userId,
  540. usageLabel,
  541. containerId);
  542. }
  543. };
  544. /* eslint-enable max-params */
  545. /**
  546. * Notifies CallStats that getUserMedia failed.
  547. *
  548. * @param {Error} e error to send
  549. */
  550. Statistics.sendGetUserMediaFailed = function(e) {
  551. const error
  552. = e instanceof JitsiTrackError
  553. ? formatJitsiTrackErrorForCallStats(e) : e;
  554. const globalSet = Statistics._getAllCallStatsInstances();
  555. if (globalSet.size) {
  556. for (const cs of globalSet) {
  557. CallStats.sendGetUserMediaFailed(error, cs);
  558. }
  559. } else {
  560. CallStats.sendGetUserMediaFailed(error, null);
  561. }
  562. };
  563. /**
  564. * Notifies CallStats that peer connection failed to create offer.
  565. *
  566. * @param {Error} e error to send
  567. * @param {TraceablePeerConnection} tpc connection on which failure occurred.
  568. */
  569. Statistics.prototype.sendCreateOfferFailed = function(e, tpc) {
  570. const instance = this.callsStatsInstances.get(tpc.id);
  571. if (instance) {
  572. instance.sendCreateOfferFailed(e);
  573. }
  574. };
  575. /**
  576. * Notifies CallStats that peer connection failed to create answer.
  577. *
  578. * @param {Error} e error to send
  579. * @param {TraceablePeerConnection} tpc connection on which failure occured.
  580. */
  581. Statistics.prototype.sendCreateAnswerFailed = function(e, tpc) {
  582. const instance = this.callsStatsInstances.get(tpc.id);
  583. if (instance) {
  584. instance.sendCreateAnswerFailed(e);
  585. }
  586. };
  587. /**
  588. * Notifies CallStats that peer connection failed to set local description.
  589. *
  590. * @param {Error} e error to send
  591. * @param {TraceablePeerConnection} tpc connection on which failure occurred.
  592. */
  593. Statistics.prototype.sendSetLocalDescFailed = function(e, tpc) {
  594. const instance = this.callsStatsInstances.get(tpc.id);
  595. if (instance) {
  596. instance.sendSetLocalDescFailed(e);
  597. }
  598. };
  599. /**
  600. * Notifies CallStats that peer connection failed to set remote description.
  601. *
  602. * @param {Error} e error to send
  603. * @param {TraceablePeerConnection} tpc connection on which failure occurred.
  604. */
  605. Statistics.prototype.sendSetRemoteDescFailed = function(e, tpc) {
  606. const instance = this.callsStatsInstances.get(tpc.id);
  607. if (instance) {
  608. instance.sendSetRemoteDescFailed(e);
  609. }
  610. };
  611. /**
  612. * Notifies CallStats that peer connection failed to add ICE candidate.
  613. *
  614. * @param {Error} e error to send
  615. * @param {TraceablePeerConnection} tpc connection on which failure occurred.
  616. */
  617. Statistics.prototype.sendAddIceCandidateFailed = function(e, tpc) {
  618. const instance = this.callsStatsInstances.get(tpc.id);
  619. if (instance) {
  620. instance.sendAddIceCandidateFailed(e);
  621. }
  622. };
  623. /**
  624. * Adds to CallStats an application log.
  625. *
  626. * @param {String} m a log message to send or an {Error} object to be reported
  627. */
  628. Statistics.sendLog = function(m) {
  629. const globalSubSet = new Set();
  630. // FIXME we don't want to duplicate logs over P2P instance, but
  631. // here we should go over instances and call this method for each
  632. // unique conference ID rather than selecting the first one.
  633. // We don't have such use case though, so leaving as is for now.
  634. for (const stats of Statistics.instances) {
  635. if (stats.callStatsApplicationLogsDisabled) {
  636. return;
  637. }
  638. if (stats.callsStatsInstances.size) {
  639. globalSubSet.add(stats.callsStatsInstances.values().next().value);
  640. }
  641. }
  642. if (globalSubSet.size) {
  643. for (const csPerStats of globalSubSet) {
  644. CallStats.sendApplicationLog(m, csPerStats);
  645. }
  646. } else {
  647. CallStats.sendApplicationLog(m, null);
  648. }
  649. };
  650. /**
  651. * Sends the given feedback through CallStats.
  652. *
  653. * @param overall an integer between 1 and 5 indicating the user's rating.
  654. * @param comment the comment from the user.
  655. * @returns {Promise} Resolves when callstats feedback has been submitted
  656. * successfully.
  657. */
  658. Statistics.prototype.sendFeedback = function(overall, comment) {
  659. // Statistics.analytics.sendEvent is currently fire and forget, without
  660. // confirmation of successful send.
  661. Statistics.analytics.sendEvent(
  662. FEEDBACK,
  663. {
  664. rating: overall,
  665. comment
  666. });
  667. return CallStats.sendFeedback(this.options.confID, overall, comment);
  668. };
  669. Statistics.LOCAL_JID = require('../../service/statistics/constants').LOCAL_JID;
  670. /**
  671. * Reports global error to CallStats.
  672. *
  673. * @param {Error} error
  674. */
  675. Statistics.reportGlobalError = function(error) {
  676. if (error instanceof JitsiTrackError && error.gum) {
  677. Statistics.sendGetUserMediaFailed(error);
  678. } else {
  679. Statistics.sendLog(error);
  680. }
  681. };
  682. /**
  683. * Sends event to analytics and logs a message to the logger/console. Console
  684. * messages might also be logged to callstats automatically.
  685. *
  686. * @param {string | Object} event the event name, or an object which
  687. * represents the entire event.
  688. * @param {Object} properties properties to attach to the event (if an event
  689. * name as opposed to an event object is provided).
  690. */
  691. Statistics.sendAnalyticsAndLog = function(event, properties = {}) {
  692. if (!event) {
  693. logger.warn('No event or event name given.');
  694. return;
  695. }
  696. let eventToLog;
  697. // Also support an API with a single object as an event.
  698. if (typeof event === 'object') {
  699. eventToLog = event;
  700. } else {
  701. eventToLog = {
  702. name: event,
  703. properties
  704. };
  705. }
  706. logger.log(JSON.stringify(eventToLog));
  707. // We do this last, because it may modify the object which is passed.
  708. this.analytics.sendEvent(event, properties);
  709. };
  710. /**
  711. * Sends event to analytics.
  712. *
  713. * @param {string | Object} eventName the event name, or an object which
  714. * represents the entire event.
  715. * @param {Object} properties properties to attach to the event
  716. */
  717. Statistics.sendAnalytics = function(eventName, properties = {}) {
  718. this.analytics.sendEvent(eventName, properties);
  719. };