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

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