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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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. * Notifies CallStats for mute events
  134. * @param mute {boolean} true for muted and false for not muted
  135. * @param type {String} "audio"/"video"
  136. */
  137. CallStats.sendMuteEvent = _try_catch(function (mute, type, cs) {
  138. var event = null;
  139. if (type === "video") {
  140. event = (mute? fabricEvent.videoPause : fabricEvent.videoResume);
  141. }
  142. else {
  143. event = (mute? fabricEvent.audioMute : fabricEvent.audioUnmute);
  144. }
  145. CallStats._reportEvent.call(cs, event);
  146. });
  147. /**
  148. * Reports an error to callstats.
  149. *
  150. * @param type the type of the error, which will be one of the wrtcFuncNames
  151. * @param e the error
  152. * @param pc the peerconnection
  153. * @private
  154. */
  155. CallStats._reportEvent = function (event) {
  156. if (callStats) {
  157. callStats.sendFabricEvent(this.peerconnection, event, this.confID);
  158. } else {
  159. CallStats.reportsQueue.push({
  160. type: reportType.EVENT,
  161. data: {event: event}
  162. });
  163. }
  164. };
  165. /**
  166. * Notifies CallStats for connection setup errors
  167. */
  168. CallStats.prototype.sendTerminateEvent = _try_catch(function () {
  169. if(!callStats) {
  170. return;
  171. }
  172. callStats.sendFabricEvent(this.peerconnection,
  173. callStats.fabricEvent.fabricTerminated, this.confID);
  174. });
  175. /**
  176. * Notifies CallStats for connection setup errors
  177. */
  178. CallStats.prototype.sendSetupFailedEvent = _try_catch(function () {
  179. if(!callStats) {
  180. return;
  181. }
  182. callStats.sendFabricEvent(this.peerconnection,
  183. callStats.fabricEvent.fabricSetupFailed, this.confID);
  184. });
  185. /**
  186. * Sends the given feedback through CallStats.
  187. *
  188. * @param overallFeedback an integer between 1 and 5 indicating the
  189. * user feedback
  190. * @param detailedFeedback detailed feedback from the user. Not yet used
  191. */
  192. CallStats.prototype.sendFeedback = _try_catch(
  193. function(overallFeedback, detailedFeedback) {
  194. if(!callStats) {
  195. return;
  196. }
  197. var feedbackString = '{"userID":"' + this.userID + '"' +
  198. ', "overall":' + overallFeedback +
  199. ', "comment": "' + detailedFeedback + '"}';
  200. var feedbackJSON = JSON.parse(feedbackString);
  201. callStats.sendUserFeedback(this.confID, feedbackJSON);
  202. });
  203. /**
  204. * Reports an error to callstats.
  205. *
  206. * @param type the type of the error, which will be one of the wrtcFuncNames
  207. * @param e the error
  208. * @param pc the peerconnection
  209. * @private
  210. */
  211. CallStats._reportError = function (type, e, pc) {
  212. if (callStats) {
  213. callStats.reportError(pc, this.confID, type, e);
  214. } else {
  215. CallStats.reportsQueue.push({
  216. type: reportType.ERROR,
  217. data: { type: type, error: e, pc: pc}
  218. });
  219. }
  220. // else just ignore it
  221. };
  222. /**
  223. * Notifies CallStats that getUserMedia failed.
  224. *
  225. * @param {Error} e error to send
  226. * @param {CallStats} cs callstats instance related to the error (optional)
  227. */
  228. CallStats.sendGetUserMediaFailed = _try_catch(function (e, cs) {
  229. CallStats._reportError.call(cs, wrtcFuncNames.getUserMedia, e, null);
  230. });
  231. /**
  232. * Notifies CallStats that peer connection failed to create offer.
  233. *
  234. * @param {Error} e error to send
  235. * @param {RTCPeerConnection} pc connection on which failure occured.
  236. * @param {CallStats} cs callstats instance related to the error (optional)
  237. */
  238. CallStats.sendCreateOfferFailed = _try_catch(function (e, pc, cs) {
  239. CallStats._reportError.call(cs, wrtcFuncNames.createOffer, e, pc);
  240. });
  241. /**
  242. * Notifies CallStats that peer connection failed to create answer.
  243. *
  244. * @param {Error} e error to send
  245. * @param {RTCPeerConnection} pc connection on which failure occured.
  246. * @param {CallStats} cs callstats instance related to the error (optional)
  247. */
  248. CallStats.sendCreateAnswerFailed = _try_catch(function (e, pc, cs) {
  249. CallStats._reportError.call(cs, wrtcFuncNames.createAnswer, e, pc);
  250. });
  251. /**
  252. * Notifies CallStats that peer connection failed to set local description.
  253. *
  254. * @param {Error} e error to send
  255. * @param {RTCPeerConnection} pc connection on which failure occured.
  256. * @param {CallStats} cs callstats instance related to the error (optional)
  257. */
  258. CallStats.sendSetLocalDescFailed = _try_catch(function (e, pc, cs) {
  259. CallStats._reportError.call(cs, wrtcFuncNames.setLocalDescription, e, pc);
  260. });
  261. /**
  262. * Notifies CallStats that peer connection failed to set remote description.
  263. *
  264. * @param {Error} e error to send
  265. * @param {RTCPeerConnection} pc connection on which failure occured.
  266. * @param {CallStats} cs callstats instance related to the error (optional)
  267. */
  268. CallStats.sendSetRemoteDescFailed = _try_catch(function (e, pc, cs) {
  269. CallStats._reportError.call(cs, wrtcFuncNames.setRemoteDescription, e, pc);
  270. });
  271. /**
  272. * Notifies CallStats that peer connection failed to add ICE candidate.
  273. *
  274. * @param {Error} e error to send
  275. * @param {RTCPeerConnection} pc connection on which failure occured.
  276. * @param {CallStats} cs callstats instance related to the error (optional)
  277. */
  278. CallStats.sendAddIceCandidateFailed = _try_catch(function (e, pc, cs) {
  279. CallStats._reportError.call(cs, wrtcFuncNames.addIceCandidate, e, pc);
  280. });
  281. module.exports = CallStats;