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.

CallStats.js 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. /* global $, Strophe, callstats */
  2. var logger = require("jitsi-meet-logger").getLogger(__filename);
  3. var GlobalOnErrorHandler = require("../util/GlobalOnErrorHandler");
  4. var jsSHA = require('jssha');
  5. var io = require('socket.io-client');
  6. /**
  7. * We define enumeration of wrtcFuncNames as we need them before
  8. * callstats is initialized to queue events.
  9. * @const
  10. * @see http://www.callstats.io/api/#enumeration-of-wrtcfuncnames
  11. */
  12. var wrtcFuncNames = {
  13. createOffer: "createOffer",
  14. createAnswer: "createAnswer",
  15. setLocalDescription: "setLocalDescription",
  16. setRemoteDescription: "setRemoteDescription",
  17. addIceCandidate: "addIceCandidate",
  18. getUserMedia: "getUserMedia",
  19. iceConnectionFailure: "iceConnectionFailure",
  20. signalingError: "signalingError",
  21. applicationLog: "applicationLog"
  22. };
  23. /**
  24. * We define enumeration of fabricEvent as we need them before
  25. * callstats is initialized to queue events.
  26. * @const
  27. * @see http://www.callstats.io/api/#enumeration-of-fabricevent
  28. */
  29. var fabricEvent = {
  30. fabricHold:"fabricHold",
  31. fabricResume:"fabricResume",
  32. audioMute:"audioMute",
  33. audioUnmute:"audioUnmute",
  34. videoPause:"videoPause",
  35. videoResume:"videoResume",
  36. fabricUsageEvent:"fabricUsageEvent",
  37. fabricStats:"fabricStats",
  38. fabricTerminated:"fabricTerminated",
  39. screenShareStart:"screenShareStart",
  40. screenShareStop:"screenShareStop",
  41. dominantSpeaker:"dominantSpeaker",
  42. activeDeviceList:"activeDeviceList"
  43. };
  44. var callStats = null;
  45. function initCallback (err, msg) {
  46. logger.log("CallStats Status: err=" + err + " msg=" + msg);
  47. CallStats.initializeInProgress = false;
  48. // there is no lib, nothing to report to
  49. if (err !== 'success') {
  50. CallStats.initializeFailed = true;
  51. return;
  52. }
  53. var ret = callStats.addNewFabric(this.peerconnection,
  54. Strophe.getResourceFromJid(this.session.peerjid),
  55. callStats.fabricUsage.multiplex,
  56. this.confID,
  57. this.pcCallback.bind(this));
  58. var fabricInitialized = (ret.status === 'success');
  59. if(!fabricInitialized) {
  60. CallStats.initializeFailed = true;
  61. console.log("callstats fabric not initilized", ret.message);
  62. return;
  63. }
  64. CallStats.initializeFailed = false;
  65. CallStats.initialized = true;
  66. CallStats.feedbackEnabled = true;
  67. // notify callstats about failures if there were any
  68. if (CallStats.reportsQueue.length) {
  69. CallStats.reportsQueue.forEach(function (report) {
  70. if (report.type === reportType.ERROR) {
  71. var error = report.data;
  72. CallStats._reportError.call(this, error.type, error.error,
  73. error.pc);
  74. }
  75. // if we have and event to report and we failed to add fabric
  76. // this event will not be reported anyway, returning an error
  77. else if (report.type === reportType.EVENT
  78. && fabricInitialized) {
  79. var eventData = report.data;
  80. callStats.sendFabricEvent(
  81. this.peerconnection,
  82. eventData.event,
  83. this.confID,
  84. eventData.eventData);
  85. } else if (report.type === reportType.MST_WITH_USERID) {
  86. var data = report.data;
  87. callStats.associateMstWithUserID(
  88. this.peerconnection,
  89. data.callStatsId,
  90. this.confID,
  91. data.ssrc,
  92. data.usageLabel,
  93. data.containerId
  94. );
  95. }
  96. }, this);
  97. CallStats.reportsQueue.length = 0;
  98. }
  99. }
  100. /**
  101. * Returns a function which invokes f in a try/catch block, logs any exception
  102. * to the console, and then swallows it.
  103. *
  104. * @param f the function to invoke in a try/catch block
  105. * @return a function which invokes f in a try/catch block, logs any exception
  106. * to the console, and then swallows it
  107. */
  108. function _try_catch (f) {
  109. return function () {
  110. try {
  111. f.apply(this, arguments);
  112. } catch (e) {
  113. GlobalOnErrorHandler.callErrorHandler(e);
  114. logger.error(e);
  115. }
  116. };
  117. }
  118. /**
  119. * Creates new CallStats instance that handles all callstats API calls.
  120. * @param peerConnection {JingleSessionPC} the session object
  121. * @param Settings {Settings} the settings instance. Declared in
  122. * /modules/settings/Settings.js
  123. * @param options {object} credentials for callstats.
  124. */
  125. var CallStats = _try_catch(function(jingleSession, Settings, options) {
  126. try{
  127. CallStats.feedbackEnabled = false;
  128. callStats = new callstats($, io, jsSHA); // eslint-disable-line new-cap
  129. this.session = jingleSession;
  130. this.peerconnection = jingleSession.peerconnection.peerconnection;
  131. this.userID = Settings.getCallStatsUserName();
  132. var location = window.location;
  133. // The confID is case sensitive!!!
  134. this.confID = location.hostname + "/" + options.roomName;
  135. this.callStatsID = options.callStatsID;
  136. this.callStatsSecret = options.callStatsSecret;
  137. CallStats.initializeInProgress = true;
  138. //userID is generated or given by the origin server
  139. callStats.initialize(this.callStatsID,
  140. this.callStatsSecret,
  141. this.userID,
  142. initCallback.bind(this));
  143. } catch (e) {
  144. // The callstats.io API failed to initialize (e.g. because its
  145. // download failed to succeed in general or on time). Further
  146. // attempts to utilize it cannot possibly succeed.
  147. GlobalOnErrorHandler.callErrorHandler(e);
  148. callStats = null;
  149. logger.error(e);
  150. }
  151. });
  152. // some errors/events may happen before CallStats init
  153. // in this case we accumulate them in this array
  154. // and send them to callstats on init
  155. CallStats.reportsQueue = [];
  156. /**
  157. * Whether the library was successfully initialized using its initialize method.
  158. * And whether we had successfully called addNewFabric.
  159. * @type {boolean}
  160. */
  161. CallStats.initialized = false;
  162. /**
  163. * Whether we are in progress of initializing.
  164. * @type {boolean}
  165. */
  166. CallStats.initializeInProgress = false;
  167. /**
  168. * Whether we tried to initialize and it failed.
  169. * @type {boolean}
  170. */
  171. CallStats.initializeFailed = false;
  172. /**
  173. * Shows weather sending feedback is enabled or not
  174. * @type {boolean}
  175. */
  176. CallStats.feedbackEnabled = false;
  177. /**
  178. * Checks whether we need to re-initialize callstats and starts the process.
  179. * @private
  180. */
  181. CallStats._checkInitialize = function () {
  182. if (CallStats.initialized || !CallStats.initializeFailed
  183. || !callStats || CallStats.initializeInProgress)
  184. return;
  185. // callstats object created, not initialized and it had previously failed,
  186. // and there is no init in progress, so lets try initialize it again
  187. CallStats.initializeInProgress = true;
  188. callStats.initialize(
  189. callStats.callStatsID,
  190. callStats.callStatsSecret,
  191. callStats.userID,
  192. initCallback.bind(callStats));
  193. };
  194. /**
  195. * Type of pending reports, can be event or an error.
  196. * @type {{ERROR: string, EVENT: string}}
  197. */
  198. var reportType = {
  199. ERROR: "error",
  200. EVENT: "event",
  201. MST_WITH_USERID: "mstWithUserID"
  202. };
  203. CallStats.prototype.pcCallback = _try_catch(function (err, msg) {
  204. if (!callStats) {
  205. return;
  206. }
  207. logger.log("Monitoring status: "+ err + " msg: " + msg);
  208. });
  209. /**
  210. * Lets CallStats module know where is given SSRC rendered by providing renderer
  211. * tag ID.
  212. * If the lib is not initialized yet queue the call for later, when its ready.
  213. * @param ssrc {number} the SSRC of the stream
  214. * @param isLocal {boolean} <tt>true<tt> if this stream is local or
  215. * <tt>false</tt> otherwise.
  216. * @param usageLabel {string} meaningful usage label of this stream like
  217. * 'microphone', 'camera' or 'screen'.
  218. * @param containerId {string} the id of media 'audio' or 'video' tag which
  219. * renders the stream.
  220. */
  221. CallStats.prototype.associateStreamWithVideoTag =
  222. function (ssrc, isLocal, usageLabel, containerId) {
  223. if(!callStats) {
  224. return;
  225. }
  226. // 'focus' is default remote user ID for now
  227. var callStatsId = 'focus';
  228. if (isLocal) {
  229. callStatsId = this.userID;
  230. }
  231. _try_catch(function() {
  232. logger.debug(
  233. "Calling callStats.associateMstWithUserID with:",
  234. this.peerconnection,
  235. callStatsId,
  236. this.confID,
  237. ssrc,
  238. usageLabel,
  239. containerId
  240. );
  241. if(CallStats.initialized) {
  242. callStats.associateMstWithUserID(
  243. this.peerconnection,
  244. callStatsId,
  245. this.confID,
  246. ssrc,
  247. usageLabel,
  248. containerId
  249. );
  250. }
  251. else {
  252. CallStats.reportsQueue.push({
  253. type: reportType.MST_WITH_USERID,
  254. data: {
  255. callStatsId: callStatsId,
  256. ssrc: ssrc,
  257. usageLabel: usageLabel,
  258. containerId: containerId
  259. }
  260. });
  261. CallStats._checkInitialize();
  262. }
  263. }).bind(this)();
  264. };
  265. /**
  266. * Notifies CallStats for mute events
  267. * @param mute {boolean} true for muted and false for not muted
  268. * @param type {String} "audio"/"video"
  269. * @param {CallStats} cs callstats instance related to the event
  270. */
  271. CallStats.sendMuteEvent = _try_catch(function (mute, type, cs) {
  272. var event = null;
  273. if (type === "video") {
  274. event = (mute? fabricEvent.videoPause : fabricEvent.videoResume);
  275. }
  276. else {
  277. event = (mute? fabricEvent.audioMute : fabricEvent.audioUnmute);
  278. }
  279. CallStats._reportEvent.call(cs, event);
  280. });
  281. /**
  282. * Notifies CallStats for screen sharing events
  283. * @param start {boolean} true for starting screen sharing and
  284. * false for not stopping
  285. * @param {CallStats} cs callstats instance related to the event
  286. */
  287. CallStats.sendScreenSharingEvent = _try_catch(function (start, cs) {
  288. CallStats._reportEvent.call(cs,
  289. start? fabricEvent.screenShareStart : fabricEvent.screenShareStop);
  290. });
  291. /**
  292. * Notifies CallStats that we are the new dominant speaker in the conference.
  293. * @param {CallStats} cs callstats instance related to the event
  294. */
  295. CallStats.sendDominantSpeakerEvent = _try_catch(function (cs) {
  296. CallStats._reportEvent.call(cs,
  297. fabricEvent.dominantSpeaker);
  298. });
  299. /**
  300. * Notifies CallStats about active device.
  301. * @param {{deviceList: {String:String}}} list of devices with their data
  302. * @param {CallStats} cs callstats instance related to the event
  303. */
  304. CallStats.sendActiveDeviceListEvent = _try_catch(function (devicesData, cs) {
  305. CallStats._reportEvent.call(cs, fabricEvent.activeDeviceList, devicesData);
  306. });
  307. /**
  308. * Reports an error to callstats.
  309. *
  310. * @param type the type of the error, which will be one of the wrtcFuncNames
  311. * @param e the error
  312. * @param pc the peerconnection
  313. * @param eventData additional data to pass to event
  314. * @private
  315. */
  316. CallStats._reportEvent = function (event, eventData) {
  317. if (CallStats.initialized) {
  318. callStats.sendFabricEvent(
  319. this.peerconnection, event, this.confID, eventData);
  320. } else {
  321. CallStats.reportsQueue.push({
  322. type: reportType.EVENT,
  323. data: {event: event, eventData: eventData}
  324. });
  325. CallStats._checkInitialize();
  326. }
  327. };
  328. /**
  329. * Notifies CallStats for connection setup errors
  330. */
  331. CallStats.prototype.sendTerminateEvent = _try_catch(function () {
  332. if(!CallStats.initialized) {
  333. return;
  334. }
  335. callStats.sendFabricEvent(this.peerconnection,
  336. callStats.fabricEvent.fabricTerminated, this.confID);
  337. });
  338. /**
  339. * Notifies CallStats that audio problems are detected.
  340. *
  341. * @param {Error} e error to send
  342. * @param {CallStats} cs callstats instance related to the error (optional)
  343. */
  344. CallStats.prototype.sendDetectedAudioProblem = _try_catch(function (e) {
  345. CallStats._reportError.call(this, wrtcFuncNames.applicationLog, e,
  346. this.peerconnection);
  347. });
  348. /**
  349. * Notifies CallStats for ice connection failed
  350. * @param {RTCPeerConnection} pc connection on which failure occured.
  351. * @param {CallStats} cs callstats instance related to the error (optional)
  352. */
  353. CallStats.prototype.sendIceConnectionFailedEvent = _try_catch(function (pc, cs){
  354. CallStats._reportError.call(
  355. cs, wrtcFuncNames.iceConnectionFailure, null, pc);
  356. });
  357. /**
  358. * Sends the given feedback through CallStats.
  359. *
  360. * @param overallFeedback an integer between 1 and 5 indicating the
  361. * user feedback
  362. * @param detailedFeedback detailed feedback from the user. Not yet used
  363. */
  364. CallStats.prototype.sendFeedback = _try_catch(
  365. function(overallFeedback, detailedFeedback) {
  366. if(!CallStats.feedbackEnabled) {
  367. return;
  368. }
  369. var feedbackString = '{"userID":"' + this.userID + '"' +
  370. ', "overall":' + overallFeedback +
  371. ', "comment": "' + detailedFeedback + '"}';
  372. var feedbackJSON = JSON.parse(feedbackString);
  373. callStats.sendUserFeedback(this.confID, feedbackJSON);
  374. });
  375. /**
  376. * Reports an error to callstats.
  377. *
  378. * @param type the type of the error, which will be one of the wrtcFuncNames
  379. * @param e the error
  380. * @param pc the peerconnection
  381. * @private
  382. */
  383. CallStats._reportError = function (type, e, pc) {
  384. if(!e) {
  385. logger.warn("No error is passed!");
  386. e = new Error("Unknown error");
  387. }
  388. if (CallStats.initialized) {
  389. callStats.reportError(pc, this.confID, type, e);
  390. } else {
  391. CallStats.reportsQueue.push({
  392. type: reportType.ERROR,
  393. data: { type: type, error: e, pc: pc}
  394. });
  395. CallStats._checkInitialize();
  396. }
  397. // else just ignore it
  398. };
  399. /**
  400. * Notifies CallStats that getUserMedia failed.
  401. *
  402. * @param {Error} e error to send
  403. * @param {CallStats} cs callstats instance related to the error (optional)
  404. */
  405. CallStats.sendGetUserMediaFailed = _try_catch(function (e, cs) {
  406. CallStats._reportError.call(cs, wrtcFuncNames.getUserMedia, e, null);
  407. });
  408. /**
  409. * Notifies CallStats that peer connection failed to create offer.
  410. *
  411. * @param {Error} e error to send
  412. * @param {RTCPeerConnection} pc connection on which failure occured.
  413. * @param {CallStats} cs callstats instance related to the error (optional)
  414. */
  415. CallStats.sendCreateOfferFailed = _try_catch(function (e, pc, cs) {
  416. CallStats._reportError.call(cs, wrtcFuncNames.createOffer, e, pc);
  417. });
  418. /**
  419. * Notifies CallStats that peer connection failed to create answer.
  420. *
  421. * @param {Error} e error to send
  422. * @param {RTCPeerConnection} pc connection on which failure occured.
  423. * @param {CallStats} cs callstats instance related to the error (optional)
  424. */
  425. CallStats.sendCreateAnswerFailed = _try_catch(function (e, pc, cs) {
  426. CallStats._reportError.call(cs, wrtcFuncNames.createAnswer, e, pc);
  427. });
  428. /**
  429. * Notifies CallStats that peer connection failed to set local description.
  430. *
  431. * @param {Error} e error to send
  432. * @param {RTCPeerConnection} pc connection on which failure occured.
  433. * @param {CallStats} cs callstats instance related to the error (optional)
  434. */
  435. CallStats.sendSetLocalDescFailed = _try_catch(function (e, pc, cs) {
  436. CallStats._reportError.call(cs, wrtcFuncNames.setLocalDescription, e, pc);
  437. });
  438. /**
  439. * Notifies CallStats that peer connection failed to set remote description.
  440. *
  441. * @param {Error} e error to send
  442. * @param {RTCPeerConnection} pc connection on which failure occured.
  443. * @param {CallStats} cs callstats instance related to the error (optional)
  444. */
  445. CallStats.sendSetRemoteDescFailed = _try_catch(function (e, pc, cs) {
  446. CallStats._reportError.call(cs, wrtcFuncNames.setRemoteDescription, e, pc);
  447. });
  448. /**
  449. * Notifies CallStats that peer connection failed to add ICE candidate.
  450. *
  451. * @param {Error} e error to send
  452. * @param {RTCPeerConnection} pc connection on which failure occured.
  453. * @param {CallStats} cs callstats instance related to the error (optional)
  454. */
  455. CallStats.sendAddIceCandidateFailed = _try_catch(function (e, pc, cs) {
  456. CallStats._reportError.call(cs, wrtcFuncNames.addIceCandidate, e, pc);
  457. });
  458. /**
  459. * Notifies CallStats that there is a log we want to report.
  460. *
  461. * @param {Error} e error to send or {String} message
  462. * @param {CallStats} cs callstats instance related to the error (optional)
  463. */
  464. CallStats.sendApplicationLog = _try_catch(function (e, cs) {
  465. CallStats._reportError
  466. .call(cs, wrtcFuncNames.applicationLog, e, null);
  467. });
  468. /**
  469. * Clears allocated resources.
  470. */
  471. CallStats.dispose = function () {
  472. // The next line is commented because we need to be able to send feedback
  473. // even after the conference has been destroyed.
  474. // callStats = null;
  475. CallStats.initialized = false;
  476. CallStats.initializeFailed = false;
  477. CallStats.initializeInProgress = false;
  478. };
  479. module.exports = CallStats;