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 26KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801
  1. /* global callstats */
  2. import browser from '../browser';
  3. import GlobalOnErrorHandler from '../util/GlobalOnErrorHandler';
  4. const logger = require('@jitsi/logger').getLogger(__filename);
  5. /**
  6. * We define enumeration of wrtcFuncNames as we need them before
  7. * callstats is initialized to queue events.
  8. * @const
  9. * @see http://www.callstats.io/api/#enumeration-of-wrtcfuncnames
  10. */
  11. const wrtcFuncNames = {
  12. createOffer: 'createOffer',
  13. createAnswer: 'createAnswer',
  14. setLocalDescription: 'setLocalDescription',
  15. setRemoteDescription: 'setRemoteDescription',
  16. addIceCandidate: 'addIceCandidate',
  17. getUserMedia: 'getUserMedia',
  18. iceConnectionFailure: 'iceConnectionFailure',
  19. signalingError: 'signalingError',
  20. applicationLog: 'applicationLog'
  21. };
  22. /**
  23. * We define enumeration of fabricEvent as we need them before
  24. * callstats is initialized to queue events.
  25. * @const
  26. * @see http://www.callstats.io/api/#enumeration-of-fabricevent
  27. */
  28. const fabricEvent = {
  29. fabricHold: 'fabricHold',
  30. fabricResume: 'fabricResume',
  31. audioMute: 'audioMute',
  32. audioUnmute: 'audioUnmute',
  33. videoPause: 'videoPause',
  34. videoResume: 'videoResume',
  35. fabricUsageEvent: 'fabricUsageEvent',
  36. fabricStats: 'fabricStats',
  37. fabricTerminated: 'fabricTerminated',
  38. screenShareStart: 'screenShareStart',
  39. screenShareStop: 'screenShareStop',
  40. dominantSpeaker: 'dominantSpeaker',
  41. activeDeviceList: 'activeDeviceList'
  42. };
  43. /**
  44. * The user id to report to callstats as destination.
  45. * @type {string}
  46. */
  47. const DEFAULT_REMOTE_USER = 'jitsi';
  48. /**
  49. * Type of pending reports, can be event or an error.
  50. * @type {{ERROR: string, EVENT: string}}
  51. */
  52. const reportType = {
  53. ERROR: 'error',
  54. EVENT: 'event',
  55. MST_WITH_USERID: 'mstWithUserID'
  56. };
  57. /**
  58. * Set of currently existing {@link CallStats} instances.
  59. * @type {Set<CallStats>}
  60. */
  61. let _fabrics;
  62. /**
  63. * An instance of this class is a wrapper for the CallStats API fabric. A fabric
  64. * reports one peer connection to the CallStats backend and is allocated with
  65. * {@link callstats.addNewFabric}. It has a bunch of instance methods for
  66. * reporting various events. A fabric is considered disposed when
  67. * {@link CallStats.sendTerminateEvent} is executed.
  68. *
  69. * Currently only one backend instance can be created ever and it's done using
  70. * {@link CallStats.initBackend}. At the time of this writing there is no way to
  71. * explicitly shutdown the backend, but it's supposed to close it's connection
  72. * automatically, after all fabrics have been terminated.
  73. */
  74. export default class CallStats {
  75. /**
  76. * A callback passed to {@link callstats.addNewFabric}.
  77. * @param {string} error 'success' means ok
  78. * @param {string} msg some more details
  79. * @private
  80. */
  81. static _addNewFabricCallback(error, msg) {
  82. if (CallStats.backend && error !== 'success') {
  83. logger.error(`Monitoring status: ${error} msg: ${msg}`);
  84. }
  85. }
  86. /**
  87. * Callback passed to {@link callstats.initialize} (backend initialization)
  88. * @param {string} error 'success' means ok
  89. * @param {String} msg
  90. * @private
  91. */
  92. static _initCallback(error, msg) {
  93. logger.log(`CallStats Status: err=${error} msg=${msg}`);
  94. // there is no lib, nothing to report to
  95. if (error !== 'success') {
  96. return;
  97. }
  98. CallStats.backendInitialized = true;
  99. // I hate that
  100. let atLeastOneFabric = false;
  101. let defaultInstance = null;
  102. for (const callStatsInstance of CallStats.fabrics.values()) {
  103. if (!callStatsInstance.hasFabric) {
  104. logger.debug('addNewFabric - initCallback');
  105. if (callStatsInstance._addNewFabric()) {
  106. atLeastOneFabric = true;
  107. if (!defaultInstance) {
  108. defaultInstance = callStatsInstance;
  109. }
  110. }
  111. }
  112. }
  113. if (!atLeastOneFabric) {
  114. return;
  115. }
  116. CallStats._emptyReportQueue(defaultInstance);
  117. }
  118. /**
  119. * Empties report queue.
  120. *
  121. * @param {CallStats} csInstance - The callstats instance.
  122. * @private
  123. */
  124. static _emptyReportQueue(csInstance) {
  125. // There is no conference ID nor a PeerConnection available when some of
  126. // the events are scheduled on the reportsQueue, so those will be
  127. // reported on the first initialized fabric.
  128. const defaultConfID = csInstance.confID;
  129. const defaultPC = csInstance.peerconnection;
  130. // notify callstats about failures if there were any
  131. for (const report of CallStats.reportsQueue) {
  132. if (report.type === reportType.ERROR) {
  133. const errorData = report.data;
  134. CallStats._reportError(
  135. csInstance,
  136. errorData.type,
  137. errorData.error,
  138. errorData.pc || defaultPC);
  139. } else if (report.type === reportType.EVENT) {
  140. // if we have and event to report and we failed to add
  141. // fabric this event will not be reported anyway, returning
  142. // an error
  143. const eventData = report.data;
  144. CallStats.backend.sendFabricEvent(
  145. report.pc || defaultPC,
  146. eventData.event,
  147. defaultConfID,
  148. eventData.eventData);
  149. } else if (report.type === reportType.MST_WITH_USERID) {
  150. const data = report.data;
  151. CallStats.backend.associateMstWithUserID(
  152. report.pc || defaultPC,
  153. data.callStatsId,
  154. defaultConfID,
  155. data.ssrc,
  156. data.usageLabel,
  157. data.containerId
  158. );
  159. }
  160. }
  161. CallStats.reportsQueue.length = 0;
  162. }
  163. /* eslint-disable max-params */
  164. /**
  165. * Reports an error to callstats.
  166. *
  167. * @param {CallStats} [cs]
  168. * @param type the type of the error, which will be one of the wrtcFuncNames
  169. * @param error the error
  170. * @param pc the peerconnection
  171. * @private
  172. */
  173. static _reportError(cs, type, error, pc) {
  174. let _error = error;
  175. if (!_error) {
  176. logger.warn('No error is passed!');
  177. _error = new Error('Unknown error');
  178. }
  179. if (CallStats.backendInitialized && cs) {
  180. CallStats.backend.reportError(pc, cs.confID, type, _error);
  181. } else {
  182. CallStats.reportsQueue.push({
  183. type: reportType.ERROR,
  184. data: {
  185. error: _error,
  186. pc,
  187. type
  188. }
  189. });
  190. }
  191. // else just ignore it
  192. }
  193. /* eslint-enable max-params */
  194. /**
  195. * Reports an error to callstats.
  196. *
  197. * @param {CallStats} cs
  198. * @param event the type of the event, which will be one of the fabricEvent
  199. * @param eventData additional data to pass to event
  200. * @private
  201. */
  202. static _reportEvent(cs, event, eventData) {
  203. const pc = cs && cs.peerconnection;
  204. const confID = cs && cs.confID;
  205. if (CallStats.backendInitialized && cs) {
  206. CallStats.backend.sendFabricEvent(pc, event, confID, eventData);
  207. } else {
  208. CallStats.reportsQueue.push({
  209. confID,
  210. pc,
  211. type: reportType.EVENT,
  212. data: { event,
  213. eventData }
  214. });
  215. }
  216. }
  217. /**
  218. * Wraps some of the CallStats API method and logs their calls with
  219. * arguments on the debug logging level. Also wraps some of the backend
  220. * methods execution into try catch blocks to not crash the app in case
  221. * there is a problem with the backend itself.
  222. * @param {callstats} theBackend
  223. * @private
  224. */
  225. static _traceAndCatchBackendCalls(theBackend) {
  226. const tryCatchMethods = [
  227. 'associateMstWithUserID',
  228. 'sendFabricEvent',
  229. 'sendUserFeedback'
  230. // 'reportError', - this one needs special handling - see code below
  231. ];
  232. for (const methodName of tryCatchMethods) {
  233. const originalMethod = theBackend[methodName];
  234. theBackend[methodName] = function(...theArguments) {
  235. try {
  236. return originalMethod.apply(theBackend, theArguments);
  237. } catch (e) {
  238. GlobalOnErrorHandler.callErrorHandler(e);
  239. }
  240. };
  241. }
  242. const debugMethods = [
  243. 'associateMstWithUserID',
  244. 'sendFabricEvent',
  245. 'sendUserFeedback'
  246. // 'reportError', - this one needs special handling - see code below
  247. ];
  248. for (const methodName of debugMethods) {
  249. const originalMethod = theBackend[methodName];
  250. theBackend[methodName] = function(...theArguments) {
  251. logger.debug(methodName, theArguments);
  252. originalMethod.apply(theBackend, theArguments);
  253. };
  254. }
  255. const originalReportError = theBackend.reportError;
  256. /* eslint-disable max-params */
  257. theBackend.reportError = function(pc, cs, type, ...args) {
  258. // Logs from the logger are submitted on the applicationLog event
  259. // "type". Logging the arguments on the logger will create endless
  260. // loop, because it will put all the logs to the logger queue again.
  261. if (type === wrtcFuncNames.applicationLog) {
  262. // NOTE otherArguments are not logged to the console on purpose
  263. // to not log the whole log batch
  264. // FIXME check the current logging level (currently not exposed
  265. // by the logger implementation)
  266. // NOTE it is not safe to log whole objects on react-native as
  267. // those contain too many circular references and may crash
  268. // the app.
  269. if (!browser.isReactNative()) {
  270. console && console.debug('reportError', pc, cs, type);
  271. }
  272. } else {
  273. logger.debug('reportError', pc, cs, type, ...args);
  274. }
  275. try {
  276. originalReportError.call(theBackend, pc, cs, type, ...args);
  277. } catch (exception) {
  278. if (type === wrtcFuncNames.applicationLog) {
  279. console && console.error('reportError', exception);
  280. } else {
  281. GlobalOnErrorHandler.callErrorHandler(exception);
  282. }
  283. }
  284. };
  285. /* eslint-enable max-params */
  286. }
  287. /**
  288. * Returns the Set with the currently existing {@link CallStats} instances.
  289. * Lazily initializes the Set to allow any Set polyfills to be applied.
  290. * @type {Set<CallStats>}
  291. */
  292. static get fabrics() {
  293. if (!_fabrics) {
  294. _fabrics = new Set();
  295. }
  296. return _fabrics;
  297. }
  298. /**
  299. * Initializes the CallStats backend. Should be called only if
  300. * {@link CallStats.isBackendInitialized} returns <tt>false</tt>.
  301. * @param {object} options
  302. * @param {String} options.callStatsID CallStats credentials - ID
  303. * @param {String} options.callStatsSecret CallStats credentials - secret
  304. * @param {string} options.aliasName the <tt>aliasName</tt> part of
  305. * the <tt>userID</tt> aka endpoint ID, see CallStats docs for more info.
  306. * @param {string} options.userName the <tt>userName</tt> part of
  307. * the <tt>userID</tt> aka display name, see CallStats docs for more info.
  308. * @param {object} options.configParams the set of parameters
  309. * to enable/disable certain features in the library. See CallStats docs for more info.
  310. *
  311. */
  312. static initBackend(options) {
  313. if (CallStats.backend) {
  314. throw new Error('CallStats backend has been initialized already!');
  315. }
  316. try {
  317. const CallStatsBackend = callstats;
  318. CallStats.backend = new CallStatsBackend();
  319. CallStats._traceAndCatchBackendCalls(CallStats.backend);
  320. CallStats.userID = {
  321. aliasName: options.aliasName,
  322. userName: options.userName
  323. };
  324. CallStats.callStatsID = options.callStatsID;
  325. CallStats.callStatsSecret = options.callStatsSecret;
  326. const configParams = { ...options.configParams };
  327. if (options.applicationName) {
  328. configParams.applicationVersion = `${options.applicationName} (${browser.getName()})`;
  329. }
  330. if (options.confID) {
  331. // we first check is there a tenant in the confID
  332. const match = options.confID.match(/.*\/(.*)\/.*/);
  333. // if there is no tenant, we will just set '/'
  334. configParams.siteID = options.siteID || (match && match[1]) || '/';
  335. }
  336. // userID is generated or given by the origin server
  337. CallStats.backend.initialize(
  338. CallStats.callStatsID,
  339. CallStats.callStatsSecret,
  340. CallStats.userID,
  341. CallStats._initCallback,
  342. undefined,
  343. configParams);
  344. const getWiFiStatsMethod = options.getWiFiStatsMethod;
  345. if (getWiFiStatsMethod) {
  346. CallStats.backend.attachWifiStatsHandler(getWiFiStatsMethod);
  347. getWiFiStatsMethod().then(result => {
  348. if (result) {
  349. logger.info('Reported wifi addresses:'
  350. , JSON.parse(result).addresses);
  351. }
  352. })
  353. .catch(() => {});// eslint-disable-line no-empty-function
  354. }
  355. return true;
  356. } catch (e) {
  357. // The callstats.io API failed to initialize (e.g. because its
  358. // download did not succeed in general or on time). Further attempts
  359. // to utilize it cannot possibly succeed.
  360. GlobalOnErrorHandler.callErrorHandler(e);
  361. CallStats.backend = null;
  362. logger.error(e);
  363. return false;
  364. }
  365. }
  366. /**
  367. * Checks if the CallStats backend has been created. It does not mean that
  368. * it has been initialized, but only that the API instance has been
  369. * allocated successfully.
  370. * @return {boolean} <tt>true</tt> if backend exists or <tt>false</tt>
  371. * otherwise
  372. */
  373. static isBackendInitialized() {
  374. return Boolean(CallStats.backend);
  375. }
  376. /**
  377. * Notifies CallStats about active device.
  378. * @param {{deviceList: {String:String}}} devicesData list of devices with
  379. * their data
  380. * @param {CallStats} cs callstats instance related to the event
  381. */
  382. static sendActiveDeviceListEvent(devicesData, cs) {
  383. CallStats._reportEvent(cs, fabricEvent.activeDeviceList, devicesData);
  384. }
  385. /**
  386. * Notifies CallStats that there is a log we want to report.
  387. *
  388. * @param {Error} e error to send or {String} message
  389. * @param {CallStats} cs callstats instance related to the error (optional)
  390. */
  391. static sendApplicationLog(e, cs) {
  392. try {
  393. CallStats._reportError(
  394. cs,
  395. wrtcFuncNames.applicationLog,
  396. e,
  397. cs && cs.peerconnection);
  398. } catch (error) {
  399. // If sendApplicationLog fails it should not be printed to
  400. // the logger, because it will try to push the logs again
  401. // (through sendApplicationLog) and an endless loop is created.
  402. if (console && (typeof console.error === 'function')) {
  403. // FIXME send analytics event as well
  404. console.error('sendApplicationLog failed', error);
  405. }
  406. }
  407. }
  408. /**
  409. * Sends the given feedback through CallStats.
  410. *
  411. * @param {string} conferenceID the conference ID for which the feedback
  412. * will be reported.
  413. * @param overall an integer between 1 and 5 indicating the
  414. * user feedback
  415. * @param comment detailed feedback from the user.
  416. */
  417. static sendFeedback(conferenceID, overall, comment) {
  418. return new Promise((resolve, reject) => {
  419. if (CallStats.backend) {
  420. CallStats.backend.sendUserFeedback(
  421. conferenceID,
  422. {
  423. userID: CallStats.userID,
  424. overall,
  425. comment
  426. },
  427. (status, message) => {
  428. if (status === 'success') {
  429. resolve(message);
  430. } else {
  431. reject(message);
  432. }
  433. });
  434. } else {
  435. const reason = 'Failed to submit feedback to CallStats - no backend';
  436. logger.error(reason);
  437. reject(reason);
  438. }
  439. });
  440. }
  441. /**
  442. * Notifies CallStats that getUserMedia failed.
  443. *
  444. * @param {Error} e error to send
  445. * @param {CallStats} cs callstats instance related to the error (optional)
  446. */
  447. static sendGetUserMediaFailed(e, cs) {
  448. CallStats._reportError(cs, wrtcFuncNames.getUserMedia, e, null);
  449. }
  450. /**
  451. * Notifies CallStats for mute events
  452. * @param mute {boolean} true for muted and false for not muted
  453. * @param type {String} "audio"/"video"
  454. * @param {CallStats} cs callstats instance related to the event
  455. */
  456. static sendMuteEvent(mute, type, cs) {
  457. let event;
  458. if (type === 'video') {
  459. event = mute ? fabricEvent.videoPause : fabricEvent.videoResume;
  460. } else {
  461. event = mute ? fabricEvent.audioMute : fabricEvent.audioUnmute;
  462. }
  463. CallStats._reportEvent(cs, event);
  464. }
  465. /**
  466. * Creates new CallStats instance that handles all callstats API calls for
  467. * given {@link TraceablePeerConnection}. Each instance is meant to handle
  468. * one CallStats fabric added with 'addFabric' API method for the
  469. * {@link TraceablePeerConnection} instance passed in the constructor.
  470. * @param {TraceablePeerConnection} tpc
  471. * @param {Object} options
  472. * @param {string} options.confID the conference ID that wil be used to
  473. * report the session.
  474. * @param {string} [options.remoteUserID='jitsi'] the remote user ID to
  475. * which given <tt>tpc</tt> is connected.
  476. */
  477. constructor(tpc, options) {
  478. this.confID = options.confID;
  479. this.tpc = tpc;
  480. this.peerconnection = tpc.peerconnection;
  481. this.remoteUserID = options.remoteUserID || DEFAULT_REMOTE_USER;
  482. this.hasFabric = false;
  483. CallStats.fabrics.add(this);
  484. if (CallStats.backendInitialized) {
  485. this._addNewFabric();
  486. // if this is the first fabric let's try to empty the
  487. // report queue. Reports all events that we recorded between
  488. // backend initialization and receiving the first fabric
  489. if (CallStats.fabrics.size === 1) {
  490. CallStats._emptyReportQueue(this);
  491. }
  492. }
  493. }
  494. /**
  495. * Initializes CallStats fabric by calling "addNewFabric" for
  496. * the peer connection associated with this instance.
  497. * @return {boolean} true if the call was successful or false otherwise.
  498. */
  499. _addNewFabric() {
  500. logger.info('addNewFabric', this.remoteUserID);
  501. try {
  502. const fabricAttributes = {
  503. remoteEndpointType:
  504. this.tpc.isP2P
  505. ? CallStats.backend.endpointType.peer
  506. : CallStats.backend.endpointType.server
  507. };
  508. const ret
  509. = CallStats.backend.addNewFabric(
  510. this.peerconnection,
  511. this.remoteUserID,
  512. CallStats.backend.fabricUsage.multiplex,
  513. this.confID,
  514. fabricAttributes,
  515. CallStats._addNewFabricCallback);
  516. this.hasFabric = true;
  517. const success = ret.status === 'success';
  518. if (!success) {
  519. logger.error('callstats fabric not initilized', ret.message);
  520. }
  521. return success;
  522. } catch (error) {
  523. GlobalOnErrorHandler.callErrorHandler(error);
  524. return false;
  525. }
  526. }
  527. /* eslint-disable max-params */
  528. /**
  529. * Lets CallStats module know where is given SSRC rendered by providing
  530. * renderer tag ID.
  531. * If the lib is not initialized yet queue the call for later, when it's
  532. * ready.
  533. * @param {number} ssrc the SSRC of the stream
  534. * @param {boolean} isLocal indicates whether this the stream is local
  535. * @param {string|null} streamEndpointId if the stream is not local the it
  536. * needs to contain the stream owner's ID
  537. * @param {string} usageLabel meaningful usage label of this stream like
  538. * 'microphone', 'camera' or 'screen'.
  539. * @param {string} containerId the id of media 'audio' or 'video' tag which
  540. * renders the stream.
  541. */
  542. associateStreamWithVideoTag(
  543. ssrc,
  544. isLocal,
  545. streamEndpointId,
  546. usageLabel,
  547. containerId) {
  548. if (!CallStats.backend) {
  549. return;
  550. }
  551. const callStatsId = isLocal ? CallStats.userID : streamEndpointId;
  552. if (CallStats.backendInitialized) {
  553. CallStats.backend.associateMstWithUserID(
  554. this.peerconnection,
  555. callStatsId,
  556. this.confID,
  557. ssrc,
  558. usageLabel,
  559. containerId);
  560. } else {
  561. CallStats.reportsQueue.push({
  562. type: reportType.MST_WITH_USERID,
  563. pc: this.peerconnection,
  564. data: {
  565. callStatsId,
  566. containerId,
  567. ssrc,
  568. usageLabel
  569. }
  570. });
  571. }
  572. }
  573. /* eslint-enable max-params */
  574. /**
  575. * Notifies CallStats that we are the new dominant speaker in the
  576. * conference.
  577. */
  578. sendDominantSpeakerEvent() {
  579. CallStats._reportEvent(this, fabricEvent.dominantSpeaker);
  580. }
  581. /**
  582. * Notifies CallStats that the fabric for the underlying peerconnection was
  583. * closed and no evens should be reported, after this call.
  584. */
  585. sendTerminateEvent() {
  586. if (CallStats.backendInitialized) {
  587. CallStats.backend.sendFabricEvent(
  588. this.peerconnection,
  589. CallStats.backend.fabricEvent.fabricTerminated,
  590. this.confID);
  591. }
  592. CallStats.fabrics.delete(this);
  593. }
  594. /**
  595. * Notifies CallStats for ice connection failed
  596. */
  597. sendIceConnectionFailedEvent() {
  598. CallStats._reportError(
  599. this,
  600. wrtcFuncNames.iceConnectionFailure,
  601. null,
  602. this.peerconnection);
  603. }
  604. /**
  605. * Notifies CallStats that peer connection failed to create offer.
  606. *
  607. * @param {Error} e error to send
  608. */
  609. sendCreateOfferFailed(e) {
  610. CallStats._reportError(
  611. this, wrtcFuncNames.createOffer, e, this.peerconnection);
  612. }
  613. /**
  614. * Notifies CallStats that peer connection failed to create answer.
  615. *
  616. * @param {Error} e error to send
  617. */
  618. sendCreateAnswerFailed(e) {
  619. CallStats._reportError(
  620. this, wrtcFuncNames.createAnswer, e, this.peerconnection);
  621. }
  622. /**
  623. * Sends either resume or hold event for the fabric associated with
  624. * the underlying peerconnection.
  625. * @param {boolean} isResume true to resume or false to hold
  626. */
  627. sendResumeOrHoldEvent(isResume) {
  628. CallStats._reportEvent(
  629. this,
  630. isResume ? fabricEvent.fabricResume : fabricEvent.fabricHold);
  631. }
  632. /**
  633. * Notifies CallStats for screen sharing events
  634. * @param {boolean} start true for starting screen sharing and
  635. * false for not stopping
  636. * @param {string|null} ssrc - optional ssrc value, used only when
  637. * starting screen sharing.
  638. */
  639. sendScreenSharingEvent(start, ssrc) {
  640. let eventData;
  641. if (ssrc) {
  642. eventData = { ssrc };
  643. }
  644. CallStats._reportEvent(
  645. this,
  646. start ? fabricEvent.screenShareStart : fabricEvent.screenShareStop,
  647. eventData);
  648. }
  649. /**
  650. * Notifies CallStats that peer connection failed to set local description.
  651. *
  652. * @param {Error} e error to send
  653. */
  654. sendSetLocalDescFailed(e) {
  655. CallStats._reportError(
  656. this, wrtcFuncNames.setLocalDescription, e, this.peerconnection);
  657. }
  658. /**
  659. * Notifies CallStats that peer connection failed to set remote description.
  660. *
  661. * @param {Error} e error to send
  662. */
  663. sendSetRemoteDescFailed(e) {
  664. CallStats._reportError(
  665. this, wrtcFuncNames.setRemoteDescription, e, this.peerconnection);
  666. }
  667. /**
  668. * Notifies CallStats that peer connection failed to add ICE candidate.
  669. *
  670. * @param {Error} e error to send
  671. */
  672. sendAddIceCandidateFailed(e) {
  673. CallStats._reportError(
  674. this, wrtcFuncNames.addIceCandidate, e, this.peerconnection);
  675. }
  676. }
  677. /**
  678. * The CallStats API backend instance
  679. * @type {callstats}
  680. */
  681. CallStats.backend = null;
  682. // some errors/events may happen before CallStats init
  683. // in this case we accumulate them in this array
  684. // and send them to callstats on init
  685. CallStats.reportsQueue = [];
  686. /**
  687. * Whether the library was successfully initialized(the backend) using its
  688. * initialize method.
  689. * @type {boolean}
  690. */
  691. CallStats.backendInitialized = false;
  692. /**
  693. * Part of the CallStats credentials - application ID
  694. * @type {string}
  695. */
  696. CallStats.callStatsID = null;
  697. /**
  698. * Part of the CallStats credentials - application secret
  699. * @type {string}
  700. */
  701. CallStats.callStatsSecret = null;
  702. /**
  703. * Local CallStats user ID structure. Can be set only once when
  704. * {@link backend} is initialized, so it's static for the time being.
  705. * See CallStats API for more info:
  706. * https://www.callstats.io/api/#userid
  707. * @type {object}
  708. */
  709. CallStats.userID = null;