Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

CallStats.js 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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. this.confID = options.callStatsConfIDNamespace + '/' + this.session.getName();
  94. //userID is generated or given by the origin server
  95. callStats.initialize(options.callStatsID,
  96. options.callStatsSecret,
  97. this.userID,
  98. initCallback.bind(this));
  99. callStats.addNewFabric(this.peerconnection,
  100. Strophe.getResourceFromJid(jingleSession.peerjid),
  101. callStats.fabricUsage.multiplex,
  102. this.confID,
  103. this.pcCallback.bind(this));
  104. } catch (e) {
  105. // The callstats.io API failed to initialize (e.g. because its
  106. // download failed to succeed in general or on time). Further
  107. // attempts to utilize it cannot possibly succeed.
  108. callStats = null;
  109. logger.error(e);
  110. }
  111. });
  112. // some errors/events may happen before CallStats init
  113. // in this case we accumulate them in this array
  114. // and send them to callstats on init
  115. CallStats.reportsQueue = [];
  116. /**
  117. * Type of pending reports, can be event or an error.
  118. * @type {{ERROR: string, EVENT: string}}
  119. */
  120. var reportType = {
  121. ERROR: "error",
  122. EVENT: "event"
  123. };
  124. CallStats.prototype.pcCallback = _try_catch(function (err, msg) {
  125. if (!callStats) {
  126. return;
  127. }
  128. logger.log("Monitoring status: "+ err + " msg: " + msg);
  129. });
  130. /**
  131. * Lets CallStats module know where is given SSRC rendered by providing renderer
  132. * tag ID.
  133. * @param ssrc {number} the SSRC of the stream
  134. * @param isLocal {boolean} <tt>true<tt> if this stream is local or
  135. * <tt>false</tt> otherwise.
  136. * @param usageLabel {string} meaningful usage label of this stream like
  137. * 'microphone', 'camera' or 'screen'.
  138. * @param containerId {string} the id of media 'audio' or 'video' tag which
  139. * renders the stream.
  140. */
  141. CallStats.prototype.associateStreamWithVideoTag =
  142. function (ssrc, isLocal, usageLabel, containerId) {
  143. if(!callStats) {
  144. return;
  145. }
  146. // 'focus' is default remote user ID for now
  147. var callStatsId = 'focus';
  148. if (isLocal) {
  149. callStatsId = this.userID;
  150. }
  151. _try_catch(function() {
  152. logger.debug(
  153. "Calling callStats.associateMstWithUserID with:",
  154. this.peerconnection,
  155. callStatsId,
  156. this.confID,
  157. ssrc,
  158. usageLabel,
  159. containerId
  160. );
  161. callStats.associateMstWithUserID(
  162. this.peerconnection,
  163. callStatsId,
  164. this.confID,
  165. ssrc,
  166. usageLabel,
  167. containerId
  168. );
  169. }).bind(this)();
  170. };
  171. /**
  172. * Notifies CallStats for mute events
  173. * @param mute {boolean} true for muted and false for not muted
  174. * @param type {String} "audio"/"video"
  175. */
  176. CallStats.sendMuteEvent = _try_catch(function (mute, type, cs) {
  177. var event = null;
  178. if (type === "video") {
  179. event = (mute? fabricEvent.videoPause : fabricEvent.videoResume);
  180. }
  181. else {
  182. event = (mute? fabricEvent.audioMute : fabricEvent.audioUnmute);
  183. }
  184. CallStats._reportEvent.call(cs, event);
  185. });
  186. /**
  187. * Reports an error to callstats.
  188. *
  189. * @param type the type of the error, which will be one of the wrtcFuncNames
  190. * @param e the error
  191. * @param pc the peerconnection
  192. * @private
  193. */
  194. CallStats._reportEvent = function (event) {
  195. if (callStats) {
  196. callStats.sendFabricEvent(this.peerconnection, event, this.confID);
  197. } else {
  198. CallStats.reportsQueue.push({
  199. type: reportType.EVENT,
  200. data: {event: event}
  201. });
  202. }
  203. };
  204. /**
  205. * Notifies CallStats for connection setup errors
  206. */
  207. CallStats.prototype.sendTerminateEvent = _try_catch(function () {
  208. if(!callStats) {
  209. return;
  210. }
  211. callStats.sendFabricEvent(this.peerconnection,
  212. callStats.fabricEvent.fabricTerminated, this.confID);
  213. });
  214. /**
  215. * Notifies CallStats for connection setup errors
  216. */
  217. CallStats.prototype.sendSetupFailedEvent = _try_catch(function () {
  218. if(!callStats) {
  219. return;
  220. }
  221. callStats.sendFabricEvent(this.peerconnection,
  222. callStats.fabricEvent.fabricSetupFailed, this.confID);
  223. });
  224. /**
  225. * Sends the given feedback through CallStats.
  226. *
  227. * @param overallFeedback an integer between 1 and 5 indicating the
  228. * user feedback
  229. * @param detailedFeedback detailed feedback from the user. Not yet used
  230. */
  231. CallStats.prototype.sendFeedback = _try_catch(
  232. function(overallFeedback, detailedFeedback) {
  233. if(!callStats) {
  234. return;
  235. }
  236. var feedbackString = '{"userID":"' + this.userID + '"' +
  237. ', "overall":' + overallFeedback +
  238. ', "comment": "' + detailedFeedback + '"}';
  239. var feedbackJSON = JSON.parse(feedbackString);
  240. callStats.sendUserFeedback(this.confID, feedbackJSON);
  241. });
  242. /**
  243. * Reports an error to callstats.
  244. *
  245. * @param type the type of the error, which will be one of the wrtcFuncNames
  246. * @param e the error
  247. * @param pc the peerconnection
  248. * @private
  249. */
  250. CallStats._reportError = function (type, e, pc) {
  251. if (callStats) {
  252. callStats.reportError(pc, this.confID, type, e);
  253. } else {
  254. CallStats.reportsQueue.push({
  255. type: reportType.ERROR,
  256. data: { type: type, error: e, pc: pc}
  257. });
  258. }
  259. // else just ignore it
  260. };
  261. /**
  262. * Notifies CallStats that getUserMedia failed.
  263. *
  264. * @param {Error} e error to send
  265. * @param {CallStats} cs callstats instance related to the error (optional)
  266. */
  267. CallStats.sendGetUserMediaFailed = _try_catch(function (e, cs) {
  268. CallStats._reportError.call(cs, wrtcFuncNames.getUserMedia, e, null);
  269. });
  270. /**
  271. * Notifies CallStats that peer connection failed to create offer.
  272. *
  273. * @param {Error} e error to send
  274. * @param {RTCPeerConnection} pc connection on which failure occured.
  275. * @param {CallStats} cs callstats instance related to the error (optional)
  276. */
  277. CallStats.sendCreateOfferFailed = _try_catch(function (e, pc, cs) {
  278. CallStats._reportError.call(cs, wrtcFuncNames.createOffer, e, pc);
  279. });
  280. /**
  281. * Notifies CallStats that peer connection failed to create answer.
  282. *
  283. * @param {Error} e error to send
  284. * @param {RTCPeerConnection} pc connection on which failure occured.
  285. * @param {CallStats} cs callstats instance related to the error (optional)
  286. */
  287. CallStats.sendCreateAnswerFailed = _try_catch(function (e, pc, cs) {
  288. CallStats._reportError.call(cs, wrtcFuncNames.createAnswer, e, pc);
  289. });
  290. /**
  291. * Notifies CallStats that peer connection failed to set local description.
  292. *
  293. * @param {Error} e error to send
  294. * @param {RTCPeerConnection} pc connection on which failure occured.
  295. * @param {CallStats} cs callstats instance related to the error (optional)
  296. */
  297. CallStats.sendSetLocalDescFailed = _try_catch(function (e, pc, cs) {
  298. CallStats._reportError.call(cs, wrtcFuncNames.setLocalDescription, e, pc);
  299. });
  300. /**
  301. * Notifies CallStats that peer connection failed to set remote description.
  302. *
  303. * @param {Error} e error to send
  304. * @param {RTCPeerConnection} pc connection on which failure occured.
  305. * @param {CallStats} cs callstats instance related to the error (optional)
  306. */
  307. CallStats.sendSetRemoteDescFailed = _try_catch(function (e, pc, cs) {
  308. CallStats._reportError.call(cs, wrtcFuncNames.setRemoteDescription, e, pc);
  309. });
  310. /**
  311. * Notifies CallStats that peer connection failed to add ICE candidate.
  312. *
  313. * @param {Error} e error to send
  314. * @param {RTCPeerConnection} pc connection on which failure occured.
  315. * @param {CallStats} cs callstats instance related to the error (optional)
  316. */
  317. CallStats.sendAddIceCandidateFailed = _try_catch(function (e, pc, cs) {
  318. CallStats._reportError.call(cs, wrtcFuncNames.addIceCandidate, e, pc);
  319. });
  320. module.exports = CallStats;