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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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. applicationError: "applicationError"
  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. };
  43. var callStats = null;
  44. function initCallback (err, msg) {
  45. logger.log("CallStats Status: err=" + err + " msg=" + msg);
  46. // there is no lib, nothing to report to
  47. if (err !== 'success')
  48. return;
  49. // notify callstats about failures if there were any
  50. if (CallStats.reportsQueue.length) {
  51. CallStats.reportsQueue.forEach(function (report) {
  52. if (report.type === reportType.ERROR)
  53. {
  54. var error = report.data;
  55. CallStats._reportError.call(this, error.type, error.error,
  56. error.pc);
  57. }
  58. else if (report.type === reportType.EVENT)
  59. {
  60. var data = report.data;
  61. callStats.sendFabricEvent(
  62. this.peerconnection, data.event, this.confID);
  63. }
  64. }, this);
  65. CallStats.reportsQueue.length = 0;
  66. }
  67. }
  68. /**
  69. * Returns a function which invokes f in a try/catch block, logs any exception
  70. * to the console, and then swallows it.
  71. *
  72. * @param f the function to invoke in a try/catch block
  73. * @return a function which invokes f in a try/catch block, logs any exception
  74. * to the console, and then swallows it
  75. */
  76. function _try_catch (f) {
  77. return function () {
  78. try {
  79. f.apply(this, arguments);
  80. } catch (e) {
  81. GlobalOnErrorHandler.callErrorHandler(e);
  82. logger.error(e);
  83. }
  84. };
  85. }
  86. /**
  87. * Creates new CallStats instance that handles all callstats API calls.
  88. * @param peerConnection {JingleSessionPC} the session object
  89. * @param Settings {Settings} the settings instance. Declared in
  90. * /modules/settings/Settings.js
  91. * @param options {object} credentials for callstats.
  92. */
  93. var CallStats = _try_catch(function(jingleSession, Settings, options) {
  94. try{
  95. //check weather that should work with more than 1 peerconnection
  96. if(!callStats) {
  97. callStats = new callstats($, io, jsSHA);
  98. } else {
  99. return;
  100. }
  101. this.session = jingleSession;
  102. this.peerconnection = jingleSession.peerconnection.peerconnection;
  103. this.userID = Settings.getCallStatsUserName();
  104. var location = window.location;
  105. // The confID is case sensitive!!!
  106. this.confID = location.hostname + "/" + options.roomName;
  107. //userID is generated or given by the origin server
  108. callStats.initialize(options.callStatsID,
  109. options.callStatsSecret,
  110. this.userID,
  111. initCallback.bind(this));
  112. callStats.addNewFabric(this.peerconnection,
  113. Strophe.getResourceFromJid(jingleSession.peerjid),
  114. callStats.fabricUsage.multiplex,
  115. this.confID,
  116. this.pcCallback.bind(this));
  117. } catch (e) {
  118. // The callstats.io API failed to initialize (e.g. because its
  119. // download failed to succeed in general or on time). Further
  120. // attempts to utilize it cannot possibly succeed.
  121. GlobalOnErrorHandler.callErrorHandler(e);
  122. callStats = null;
  123. logger.error(e);
  124. }
  125. });
  126. // some errors/events may happen before CallStats init
  127. // in this case we accumulate them in this array
  128. // and send them to callstats on init
  129. CallStats.reportsQueue = [];
  130. /**
  131. * Type of pending reports, can be event or an error.
  132. * @type {{ERROR: string, EVENT: string}}
  133. */
  134. var reportType = {
  135. ERROR: "error",
  136. EVENT: "event"
  137. };
  138. CallStats.prototype.pcCallback = _try_catch(function (err, msg) {
  139. if (!callStats) {
  140. return;
  141. }
  142. logger.log("Monitoring status: "+ err + " msg: " + msg);
  143. });
  144. /**
  145. * Lets CallStats module know where is given SSRC rendered by providing renderer
  146. * tag ID.
  147. * @param ssrc {number} the SSRC of the stream
  148. * @param isLocal {boolean} <tt>true<tt> if this stream is local or
  149. * <tt>false</tt> otherwise.
  150. * @param usageLabel {string} meaningful usage label of this stream like
  151. * 'microphone', 'camera' or 'screen'.
  152. * @param containerId {string} the id of media 'audio' or 'video' tag which
  153. * renders the stream.
  154. */
  155. CallStats.prototype.associateStreamWithVideoTag =
  156. function (ssrc, isLocal, usageLabel, containerId) {
  157. if(!callStats) {
  158. return;
  159. }
  160. // 'focus' is default remote user ID for now
  161. var callStatsId = 'focus';
  162. if (isLocal) {
  163. callStatsId = this.userID;
  164. }
  165. _try_catch(function() {
  166. logger.debug(
  167. "Calling callStats.associateMstWithUserID with:",
  168. this.peerconnection,
  169. callStatsId,
  170. this.confID,
  171. ssrc,
  172. usageLabel,
  173. containerId
  174. );
  175. callStats.associateMstWithUserID(
  176. this.peerconnection,
  177. callStatsId,
  178. this.confID,
  179. ssrc,
  180. usageLabel,
  181. containerId
  182. );
  183. }).bind(this)();
  184. };
  185. /**
  186. * Notifies CallStats for mute events
  187. * @param mute {boolean} true for muted and false for not muted
  188. * @param type {String} "audio"/"video"
  189. */
  190. CallStats.sendMuteEvent = _try_catch(function (mute, type, cs) {
  191. var event = null;
  192. if (type === "video") {
  193. event = (mute? fabricEvent.videoPause : fabricEvent.videoResume);
  194. }
  195. else {
  196. event = (mute? fabricEvent.audioMute : fabricEvent.audioUnmute);
  197. }
  198. CallStats._reportEvent.call(cs, event);
  199. });
  200. /**
  201. * Notifies CallStats for screen sharing events
  202. * @param start {boolean} true for starting screen sharing and
  203. * false for not stopping
  204. */
  205. CallStats.sendScreenSharingEvent = _try_catch(function (start, cs) {
  206. CallStats._reportEvent.call(cs,
  207. start? fabricEvent.screenShareStart : fabricEvent.screenShareStop);
  208. });
  209. /**
  210. * Notifies CallStats that we are the new dominant speaker in the conference.
  211. */
  212. CallStats.sendDominantSpeakerEvent = _try_catch(function (cs) {
  213. CallStats._reportEvent.call(cs,
  214. fabricEvent.dominantSpeaker);
  215. });
  216. /**
  217. * Reports an error to callstats.
  218. *
  219. * @param type the type of the error, which will be one of the wrtcFuncNames
  220. * @param e the error
  221. * @param pc the peerconnection
  222. * @private
  223. */
  224. CallStats._reportEvent = function (event) {
  225. if (callStats) {
  226. callStats.sendFabricEvent(this.peerconnection, event, this.confID);
  227. } else {
  228. CallStats.reportsQueue.push({
  229. type: reportType.EVENT,
  230. data: {event: event}
  231. });
  232. }
  233. };
  234. /**
  235. * Notifies CallStats for connection setup errors
  236. */
  237. CallStats.prototype.sendTerminateEvent = _try_catch(function () {
  238. if(!callStats) {
  239. return;
  240. }
  241. callStats.sendFabricEvent(this.peerconnection,
  242. callStats.fabricEvent.fabricTerminated, this.confID);
  243. });
  244. /**
  245. * Notifies CallStats that audio problems are detected.
  246. *
  247. * @param {Error} e error to send
  248. * @param {CallStats} cs callstats instance related to the error (optional)
  249. */
  250. CallStats.prototype.sendDetectedAudioProblem = _try_catch(function (e) {
  251. CallStats._reportError.call(this, wrtcFuncNames.signalingError, e,
  252. this.peerconnection);
  253. });
  254. /**
  255. * Notifies CallStats for ice connection failed
  256. * @param {RTCPeerConnection} pc connection on which failure occured.
  257. * @param {CallStats} cs callstats instance related to the error (optional)
  258. */
  259. CallStats.prototype.sendIceConnectionFailedEvent = _try_catch(function (pc, cs){
  260. CallStats._reportError.call(
  261. cs, wrtcFuncNames.iceConnectionFailure, null, pc);
  262. });
  263. /**
  264. * Sends the given feedback through CallStats.
  265. *
  266. * @param overallFeedback an integer between 1 and 5 indicating the
  267. * user feedback
  268. * @param detailedFeedback detailed feedback from the user. Not yet used
  269. */
  270. CallStats.prototype.sendFeedback = _try_catch(
  271. function(overallFeedback, detailedFeedback) {
  272. if(!callStats) {
  273. return;
  274. }
  275. var feedbackString = '{"userID":"' + this.userID + '"' +
  276. ', "overall":' + overallFeedback +
  277. ', "comment": "' + detailedFeedback + '"}';
  278. var feedbackJSON = JSON.parse(feedbackString);
  279. callStats.sendUserFeedback(this.confID, feedbackJSON);
  280. });
  281. /**
  282. * Reports an error to callstats.
  283. *
  284. * @param type the type of the error, which will be one of the wrtcFuncNames
  285. * @param e the error
  286. * @param pc the peerconnection
  287. * @private
  288. */
  289. CallStats._reportError = function (type, e, pc) {
  290. if(!e) {
  291. logger.warn("No error is passed!");
  292. e = new Error("Unknown error");
  293. }
  294. if (callStats) {
  295. callStats.reportError(pc, this.confID, type, e);
  296. } else {
  297. CallStats.reportsQueue.push({
  298. type: reportType.ERROR,
  299. data: { type: type, error: e, pc: pc}
  300. });
  301. }
  302. // else just ignore it
  303. };
  304. /**
  305. * Notifies CallStats that getUserMedia failed.
  306. *
  307. * @param {Error} e error to send
  308. * @param {CallStats} cs callstats instance related to the error (optional)
  309. */
  310. CallStats.sendGetUserMediaFailed = _try_catch(function (e, cs) {
  311. CallStats._reportError.call(cs, wrtcFuncNames.getUserMedia, e, null);
  312. });
  313. /**
  314. * Notifies CallStats that peer connection failed to create offer.
  315. *
  316. * @param {Error} e error to send
  317. * @param {RTCPeerConnection} pc connection on which failure occured.
  318. * @param {CallStats} cs callstats instance related to the error (optional)
  319. */
  320. CallStats.sendCreateOfferFailed = _try_catch(function (e, pc, cs) {
  321. CallStats._reportError.call(cs, wrtcFuncNames.createOffer, e, pc);
  322. });
  323. /**
  324. * Notifies CallStats that peer connection failed to create answer.
  325. *
  326. * @param {Error} e error to send
  327. * @param {RTCPeerConnection} pc connection on which failure occured.
  328. * @param {CallStats} cs callstats instance related to the error (optional)
  329. */
  330. CallStats.sendCreateAnswerFailed = _try_catch(function (e, pc, cs) {
  331. CallStats._reportError.call(cs, wrtcFuncNames.createAnswer, e, pc);
  332. });
  333. /**
  334. * Notifies CallStats that peer connection failed to set local description.
  335. *
  336. * @param {Error} e error to send
  337. * @param {RTCPeerConnection} pc connection on which failure occured.
  338. * @param {CallStats} cs callstats instance related to the error (optional)
  339. */
  340. CallStats.sendSetLocalDescFailed = _try_catch(function (e, pc, cs) {
  341. CallStats._reportError.call(cs, wrtcFuncNames.setLocalDescription, e, pc);
  342. });
  343. /**
  344. * Notifies CallStats that peer connection failed to set remote description.
  345. *
  346. * @param {Error} e error to send
  347. * @param {RTCPeerConnection} pc connection on which failure occured.
  348. * @param {CallStats} cs callstats instance related to the error (optional)
  349. */
  350. CallStats.sendSetRemoteDescFailed = _try_catch(function (e, pc, cs) {
  351. CallStats._reportError.call(cs, wrtcFuncNames.setRemoteDescription, e, pc);
  352. });
  353. /**
  354. * Notifies CallStats that peer connection failed to add ICE candidate.
  355. *
  356. * @param {Error} e error to send
  357. * @param {RTCPeerConnection} pc connection on which failure occured.
  358. * @param {CallStats} cs callstats instance related to the error (optional)
  359. */
  360. CallStats.sendAddIceCandidateFailed = _try_catch(function (e, pc, cs) {
  361. CallStats._reportError.call(cs, wrtcFuncNames.addIceCandidate, e, pc);
  362. });
  363. /**
  364. * Notifies CallStats that there is an unhandled error on the page.
  365. *
  366. * @param {Error} e error to send
  367. * @param {RTCPeerConnection} pc connection on which failure occured.
  368. * @param {CallStats} cs callstats instance related to the error (optional)
  369. */
  370. CallStats.sendUnhandledError = _try_catch(function (e, cs) {
  371. CallStats._reportError
  372. .call(cs, wrtcFuncNames.applicationError, e, null);
  373. });
  374. module.exports = CallStats;