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

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