Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

statistics.js 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. /* global require */
  2. import analytics from "./AnalyticsAdapter";
  3. var CallStats = require("./CallStats");
  4. var EventEmitter = require("events");
  5. import JitsiTrackError from "../../JitsiTrackError";
  6. var logger = require("jitsi-meet-logger").getLogger(__filename);
  7. var LocalStats = require("./LocalStatsCollector.js");
  8. var RTPStats = require("./RTPStatsCollector.js");
  9. var ScriptUtil = require('../util/ScriptUtil');
  10. import * as StatisticsEvents from "../../service/statistics/Events";
  11. /**
  12. * True if callstats API is loaded
  13. */
  14. var isCallstatsLoaded = false;
  15. // Since callstats.io is a third party, we cannot guarantee the quality of their
  16. // service. More specifically, their server may take noticeably long time to
  17. // respond. Consequently, it is in our best interest (in the sense that the
  18. // intergration of callstats.io is pretty important to us but not enough to
  19. // allow it to prevent people from joining a conference) to (1) start
  20. // downloading their API as soon as possible and (2) do the downloading
  21. // asynchronously.
  22. function loadCallStatsAPI(customScriptUrl) {
  23. if(!isCallstatsLoaded) {
  24. ScriptUtil.loadScript(
  25. customScriptUrl ? customScriptUrl :
  26. 'https://api.callstats.io/static/callstats-ws.min.js',
  27. /* async */ true,
  28. /* prepend */ true);
  29. isCallstatsLoaded = true;
  30. }
  31. // FIXME At the time of this writing, we hope that the callstats.io API will
  32. // have loaded by the time we needed it (i.e. CallStats.init is invoked).
  33. }
  34. /**
  35. * callstats strips any additional fields from Error except for "name", "stack",
  36. * "message" and "constraintName". So we need to bundle additional information
  37. * from JitsiTrackError into error passed to callstats to preserve valuable
  38. * information about error.
  39. * @param {JitsiTrackError} error
  40. */
  41. function formatJitsiTrackErrorForCallStats(error) {
  42. var err = new Error();
  43. // Just copy original stack from error
  44. err.stack = error.stack;
  45. // Combine name from error's name plus (possibly) name of original GUM error
  46. err.name = (error.name || "Unknown error") + (error.gum && error.gum.error
  47. && error.gum.error.name ? " - " + error.gum.error.name : "");
  48. // Put all constraints into this field. For constraint failed errors we will
  49. // still know which exactly constraint failed as it will be a part of
  50. // message.
  51. err.constraintName = error.gum && error.gum.constraints
  52. ? JSON.stringify(error.gum.constraints) : "";
  53. // Just copy error's message.
  54. err.message = error.message;
  55. return err;
  56. }
  57. /**
  58. * Init statistic options
  59. * @param options
  60. */
  61. Statistics.init = function (options) {
  62. Statistics.audioLevelsEnabled = !options.disableAudioLevels;
  63. if(typeof options.audioLevelsInterval === 'number') {
  64. Statistics.audioLevelsInterval = options.audioLevelsInterval;
  65. }
  66. Statistics.disableThirdPartyRequests = options.disableThirdPartyRequests;
  67. };
  68. function Statistics(xmpp, options) {
  69. this.rtpStats = null;
  70. this.eventEmitter = new EventEmitter();
  71. this.xmpp = xmpp;
  72. this.options = options || {};
  73. this.callStatsIntegrationEnabled
  74. = this.options.callStatsID && this.options.callStatsSecret
  75. // Even though AppID and AppSecret may be specified, the integration
  76. // of callstats.io may be disabled because of globally-disallowed
  77. // requests to any third parties.
  78. && (Statistics.disableThirdPartyRequests !== true);
  79. if(this.callStatsIntegrationEnabled)
  80. loadCallStatsAPI(this.options.callStatsCustomScriptUrl);
  81. this.callStats = null;
  82. // Flag indicates whether or not the CallStats have been started for this
  83. // Statistics instance
  84. this.callStatsStarted = false;
  85. }
  86. Statistics.audioLevelsEnabled = false;
  87. Statistics.audioLevelsInterval = 200;
  88. Statistics.disableThirdPartyRequests = false;
  89. Statistics.analytics = analytics;
  90. /**
  91. * Array of callstats instances. Used to call Statistics static methods and
  92. * send stats to all cs instances.
  93. */
  94. Statistics.callsStatsInstances = [];
  95. Statistics.prototype.startRemoteStats = function (peerconnection) {
  96. this.stopRemoteStats();
  97. try {
  98. this.rtpStats
  99. = new RTPStats(peerconnection,
  100. Statistics.audioLevelsInterval, 2000, this.eventEmitter);
  101. this.rtpStats.start(Statistics.audioLevelsEnabled);
  102. } catch (e) {
  103. this.rtpStats = null;
  104. logger.error('Failed to start collecting remote statistics: ' + e);
  105. }
  106. };
  107. Statistics.localStats = [];
  108. Statistics.startLocalStats = function (stream, callback) {
  109. if(!Statistics.audioLevelsEnabled)
  110. return;
  111. var localStats = new LocalStats(stream, Statistics.audioLevelsInterval,
  112. callback);
  113. this.localStats.push(localStats);
  114. localStats.start();
  115. };
  116. Statistics.prototype.addAudioLevelListener = function(listener) {
  117. if(!Statistics.audioLevelsEnabled)
  118. return;
  119. this.eventEmitter.on(StatisticsEvents.AUDIO_LEVEL, listener);
  120. };
  121. Statistics.prototype.removeAudioLevelListener = function(listener) {
  122. if(!Statistics.audioLevelsEnabled)
  123. return;
  124. this.eventEmitter.removeListener(StatisticsEvents.AUDIO_LEVEL, listener);
  125. };
  126. Statistics.prototype.addBeforeDisposedListener = function (listener) {
  127. this.eventEmitter.on(StatisticsEvents.BEFORE_DISPOSED, listener);
  128. };
  129. Statistics.prototype.removeBeforeDisposedListener = function (listener) {
  130. this.eventEmitter.removeListener(
  131. StatisticsEvents.BEFORE_DISPOSED, listener);
  132. };
  133. Statistics.prototype.addConnectionStatsListener = function (listener) {
  134. this.eventEmitter.on(StatisticsEvents.CONNECTION_STATS, listener);
  135. };
  136. Statistics.prototype.removeConnectionStatsListener = function (listener) {
  137. this.eventEmitter.removeListener(StatisticsEvents.CONNECTION_STATS, listener);
  138. };
  139. Statistics.prototype.addByteSentStatsListener = function (listener) {
  140. this.eventEmitter.on(StatisticsEvents.BYTE_SENT_STATS, listener);
  141. };
  142. Statistics.prototype.removeByteSentStatsListener = function (listener) {
  143. this.eventEmitter.removeListener(StatisticsEvents.BYTE_SENT_STATS,
  144. listener);
  145. };
  146. Statistics.prototype.dispose = function () {
  147. if (this.eventEmitter) {
  148. this.eventEmitter.emit(StatisticsEvents.BEFORE_DISPOSED);
  149. }
  150. this.stopCallStats();
  151. this.stopRemoteStats();
  152. if(this.eventEmitter)
  153. this.eventEmitter.removeAllListeners();
  154. };
  155. Statistics.stopLocalStats = function (stream) {
  156. if(!Statistics.audioLevelsEnabled)
  157. return;
  158. for(var i = 0; i < Statistics.localStats.length; i++)
  159. if(Statistics.localStats[i].stream === stream){
  160. var localStats = Statistics.localStats.splice(i, 1);
  161. localStats[0].stop();
  162. break;
  163. }
  164. };
  165. Statistics.prototype.stopRemoteStats = function () {
  166. if (!this.rtpStats) {
  167. return;
  168. }
  169. this.rtpStats.stop();
  170. this.rtpStats = null;
  171. };
  172. //CALSTATS METHODS
  173. /**
  174. * Initializes the callstats.io API.
  175. * @param peerConnection {JingleSessionPC} the session object
  176. */
  177. Statistics.prototype.startCallStats = function (session) {
  178. if(this.callStatsIntegrationEnabled && !this.callStatsStarted) {
  179. // Here we overwrite the previous instance, but it must be bound to
  180. // the new PeerConnection
  181. this.callstats = new CallStats(session, this.options);
  182. Statistics.callsStatsInstances.push(this.callstats);
  183. this.callStatsStarted = true;
  184. }
  185. };
  186. /**
  187. * Removes the callstats.io instances.
  188. */
  189. Statistics.prototype.stopCallStats = function () {
  190. if(this.callStatsStarted) {
  191. var index = Statistics.callsStatsInstances.indexOf(this.callstats);
  192. if(index > -1)
  193. Statistics.callsStatsInstances.splice(index, 1);
  194. // The next line is commented because we need to be able to send
  195. // feedback even after the conference has been destroyed.
  196. // this.callstats = null;
  197. CallStats.dispose();
  198. this.callStatsStarted = false;
  199. }
  200. };
  201. /**
  202. * Returns true if the callstats integration is enabled, otherwise returns
  203. * false.
  204. *
  205. * @returns true if the callstats integration is enabled, otherwise returns
  206. * false.
  207. */
  208. Statistics.prototype.isCallstatsEnabled = function () {
  209. return this.callStatsIntegrationEnabled;
  210. };
  211. /**
  212. * Notifies CallStats and analytics(if present) for ice connection failed
  213. * @param {RTCPeerConnection} pc connection on which failure occured.
  214. */
  215. Statistics.prototype.sendIceConnectionFailedEvent = function (pc) {
  216. if(this.callstats)
  217. this.callstats.sendIceConnectionFailedEvent(pc, this.callstats);
  218. Statistics.analytics.sendEvent('connection.ice_failed');
  219. };
  220. /**
  221. * Notifies CallStats for mute events
  222. * @param mute {boolean} true for muted and false for not muted
  223. * @param type {String} "audio"/"video"
  224. */
  225. Statistics.prototype.sendMuteEvent = function (muted, type) {
  226. if(this.callstats)
  227. CallStats.sendMuteEvent(muted, type, this.callstats);
  228. };
  229. /**
  230. * Notifies CallStats for screen sharing events
  231. * @param start {boolean} true for starting screen sharing and
  232. * false for not stopping
  233. */
  234. Statistics.prototype.sendScreenSharingEvent = function (start) {
  235. if(this.callstats)
  236. CallStats.sendScreenSharingEvent(start, this.callstats);
  237. };
  238. /**
  239. * Notifies the statistics module that we are now the dominant speaker of the
  240. * conference.
  241. */
  242. Statistics.prototype.sendDominantSpeakerEvent = function () {
  243. if(this.callstats)
  244. CallStats.sendDominantSpeakerEvent(this.callstats);
  245. };
  246. /**
  247. * Notifies about active device.
  248. * @param {{deviceList: {String:String}}} devicesData - list of devices with
  249. * their data
  250. */
  251. Statistics.sendActiveDeviceListEvent = function (devicesData) {
  252. if (Statistics.callsStatsInstances.length) {
  253. Statistics.callsStatsInstances.forEach(function (cs) {
  254. CallStats.sendActiveDeviceListEvent(devicesData, cs);
  255. });
  256. } else {
  257. CallStats.sendActiveDeviceListEvent(devicesData, null);
  258. }
  259. };
  260. /**
  261. * Lets the underlying statistics module know where is given SSRC rendered by
  262. * providing renderer tag ID.
  263. * @param ssrc {number} the SSRC of the stream
  264. * @param isLocal {boolean} <tt>true<tt> if this stream is local or
  265. * <tt>false</tt> otherwise.
  266. * @param usageLabel {string} meaningful usage label of this stream like
  267. * 'microphone', 'camera' or 'screen'.
  268. * @param containerId {string} the id of media 'audio' or 'video' tag which
  269. * renders the stream.
  270. */
  271. Statistics.prototype.associateStreamWithVideoTag =
  272. function (ssrc, isLocal, usageLabel, containerId) {
  273. if(this.callstats) {
  274. this.callstats.associateStreamWithVideoTag(
  275. ssrc, isLocal, usageLabel, containerId);
  276. }
  277. };
  278. /**
  279. * Notifies CallStats that getUserMedia failed.
  280. *
  281. * @param {Error} e error to send
  282. */
  283. Statistics.sendGetUserMediaFailed = function (e) {
  284. if (Statistics.callsStatsInstances.length) {
  285. Statistics.callsStatsInstances.forEach(function (cs) {
  286. CallStats.sendGetUserMediaFailed(
  287. e instanceof JitsiTrackError
  288. ? formatJitsiTrackErrorForCallStats(e)
  289. : e,
  290. cs);
  291. });
  292. } else {
  293. CallStats.sendGetUserMediaFailed(
  294. e instanceof JitsiTrackError
  295. ? formatJitsiTrackErrorForCallStats(e)
  296. : e,
  297. null);
  298. }
  299. };
  300. /**
  301. * Notifies CallStats that peer connection failed to create offer.
  302. *
  303. * @param {Error} e error to send
  304. * @param {RTCPeerConnection} pc connection on which failure occured.
  305. */
  306. Statistics.prototype.sendCreateOfferFailed = function (e, pc) {
  307. if(this.callstats)
  308. CallStats.sendCreateOfferFailed(e, pc, this.callstats);
  309. };
  310. /**
  311. * Notifies CallStats that peer connection failed to create answer.
  312. *
  313. * @param {Error} e error to send
  314. * @param {RTCPeerConnection} pc connection on which failure occured.
  315. */
  316. Statistics.prototype.sendCreateAnswerFailed = function (e, pc) {
  317. if(this.callstats)
  318. CallStats.sendCreateAnswerFailed(e, pc, this.callstats);
  319. };
  320. /**
  321. * Notifies CallStats that peer connection failed to set local description.
  322. *
  323. * @param {Error} e error to send
  324. * @param {RTCPeerConnection} pc connection on which failure occured.
  325. */
  326. Statistics.prototype.sendSetLocalDescFailed = function (e, pc) {
  327. if(this.callstats)
  328. CallStats.sendSetLocalDescFailed(e, pc, this.callstats);
  329. };
  330. /**
  331. * Notifies CallStats that peer connection failed to set remote description.
  332. *
  333. * @param {Error} e error to send
  334. * @param {RTCPeerConnection} pc connection on which failure occured.
  335. */
  336. Statistics.prototype.sendSetRemoteDescFailed = function (e, pc) {
  337. if(this.callstats)
  338. CallStats.sendSetRemoteDescFailed(e, pc, this.callstats);
  339. };
  340. /**
  341. * Notifies CallStats that peer connection failed to add ICE candidate.
  342. *
  343. * @param {Error} e error to send
  344. * @param {RTCPeerConnection} pc connection on which failure occured.
  345. */
  346. Statistics.prototype.sendAddIceCandidateFailed = function (e, pc) {
  347. if(this.callstats)
  348. CallStats.sendAddIceCandidateFailed(e, pc, this.callstats);
  349. };
  350. /**
  351. * Adds to CallStats an application log.
  352. *
  353. * @param {String} a log message to send or an {Error} object to be reported
  354. */
  355. Statistics.sendLog = function (m) {
  356. if (Statistics.callsStatsInstances.length) {
  357. Statistics.callsStatsInstances.forEach(function (cs) {
  358. CallStats.sendApplicationLog(m, cs);
  359. });
  360. } else {
  361. CallStats.sendApplicationLog(m, null);
  362. }
  363. };
  364. /**
  365. * Sends the given feedback through CallStats.
  366. *
  367. * @param overall an integer between 1 and 5 indicating the user feedback
  368. * @param detailed detailed feedback from the user. Not yet used
  369. */
  370. Statistics.prototype.sendFeedback = function(overall, detailed) {
  371. if(this.callstats)
  372. this.callstats.sendFeedback(overall, detailed);
  373. Statistics.analytics.sendEvent("feedback.rating",
  374. {value: overall, detailed: detailed});
  375. };
  376. Statistics.LOCAL_JID = require("../../service/statistics/constants").LOCAL_JID;
  377. /**
  378. * Reports global error to CallStats.
  379. *
  380. * @param {Error} error
  381. */
  382. Statistics.reportGlobalError = function (error) {
  383. if (error instanceof JitsiTrackError && error.gum) {
  384. Statistics.sendGetUserMediaFailed(error);
  385. } else {
  386. Statistics.sendLog(error);
  387. }
  388. };
  389. /**
  390. * Sends event to analytics and callstats.
  391. * @param {string} eventName the event name.
  392. * @param {Object} data the data to be sent.
  393. */
  394. Statistics.sendEventToAll = function (eventName, data) {
  395. this.analytics.sendEvent(eventName, data);
  396. Statistics.sendLog(JSON.stringify({name: eventName, data}));
  397. };
  398. module.exports = Statistics;