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.

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