您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

CallStats.js 11KB

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