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

CallStats.js 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. /* global $, Strophe, callstats */
  2. var logger = require("jitsi-meet-logger").getLogger(__filename);
  3. var GlobalOnErrorHandler = require("../util/GlobalOnErrorHandler");
  4. var jsSHA = require('jssha');
  5. var io = require('socket.io-client');
  6. /**
  7. * We define enumeration of wrtcFuncNames as we need them before
  8. * callstats is initialized to queue events.
  9. * @const
  10. * @see http://www.callstats.io/api/#enumeration-of-wrtcfuncnames
  11. */
  12. var wrtcFuncNames = {
  13. createOffer: "createOffer",
  14. createAnswer: "createAnswer",
  15. setLocalDescription: "setLocalDescription",
  16. setRemoteDescription: "setRemoteDescription",
  17. addIceCandidate: "addIceCandidate",
  18. getUserMedia: "getUserMedia",
  19. iceConnectionFailure: "iceConnectionFailure",
  20. signalingError: "signalingError",
  21. applicationLog: "applicationLog"
  22. };
  23. /**
  24. * We define enumeration of fabricEvent as we need them before
  25. * callstats is initialized to queue events.
  26. * @const
  27. * @see http://www.callstats.io/api/#enumeration-of-fabricevent
  28. */
  29. var fabricEvent = {
  30. fabricHold:"fabricHold",
  31. fabricResume:"fabricResume",
  32. audioMute:"audioMute",
  33. audioUnmute:"audioUnmute",
  34. videoPause:"videoPause",
  35. videoResume:"videoResume",
  36. fabricUsageEvent:"fabricUsageEvent",
  37. fabricStats:"fabricStats",
  38. fabricTerminated:"fabricTerminated",
  39. screenShareStart:"screenShareStart",
  40. screenShareStop:"screenShareStop",
  41. dominantSpeaker:"dominantSpeaker",
  42. activeDeviceList:"activeDeviceList"
  43. };
  44. var callStats = null;
  45. function initCallback (err, msg) {
  46. logger.log("CallStats Status: err=" + err + " msg=" + msg);
  47. // there is no lib, nothing to report to
  48. if (err !== 'success')
  49. return;
  50. CallStats.initialized = true;
  51. var ret = callStats.addNewFabric(this.peerconnection,
  52. Strophe.getResourceFromJid(this.session.peerjid),
  53. callStats.fabricUsage.multiplex,
  54. this.confID,
  55. this.pcCallback.bind(this));
  56. var fabricInitialized = (ret.status === 'success');
  57. if(!fabricInitialized)
  58. console.log("callstats fabric not initilized", ret.message);
  59. // notify callstats about failures if there were any
  60. if (CallStats.reportsQueue.length) {
  61. CallStats.reportsQueue.forEach(function (report) {
  62. if (report.type === reportType.ERROR) {
  63. var error = report.data;
  64. CallStats._reportError.call(this, error.type, error.error,
  65. error.pc);
  66. }
  67. // if we have and event to report and we failed to add fabric
  68. // this event will not be reported anyway, returning an error
  69. else if (report.type === reportType.EVENT
  70. && fabricInitialized) {
  71. var eventData = report.data;
  72. callStats.sendFabricEvent(
  73. this.peerconnection,
  74. eventData.event,
  75. this.confID,
  76. eventData.eventData);
  77. } else if (report.type === reportType.MST_WITH_USERID) {
  78. var data = report.data;
  79. callStats.associateMstWithUserID(
  80. this.peerconnection,
  81. data.callStatsId,
  82. this.confID,
  83. data.ssrc,
  84. data.usageLabel,
  85. data.containerId
  86. );
  87. }
  88. }, this);
  89. CallStats.reportsQueue.length = 0;
  90. }
  91. }
  92. /**
  93. * Returns a function which invokes f in a try/catch block, logs any exception
  94. * to the console, and then swallows it.
  95. *
  96. * @param f the function to invoke in a try/catch block
  97. * @return a function which invokes f in a try/catch block, logs any exception
  98. * to the console, and then swallows it
  99. */
  100. function _try_catch (f) {
  101. return function () {
  102. try {
  103. f.apply(this, arguments);
  104. } catch (e) {
  105. GlobalOnErrorHandler.callErrorHandler(e);
  106. logger.error(e);
  107. }
  108. };
  109. }
  110. /**
  111. * Creates new CallStats instance that handles all callstats API calls.
  112. * @param peerConnection {JingleSessionPC} the session object
  113. * @param Settings {Settings} the settings instance. Declared in
  114. * /modules/settings/Settings.js
  115. * @param options {object} credentials for callstats.
  116. */
  117. var CallStats = _try_catch(function(jingleSession, Settings, options) {
  118. try{
  119. //check weather that should work with more than 1 peerconnection
  120. if(!callStats) {
  121. callStats = new callstats($, io, jsSHA);
  122. } else {
  123. return;
  124. }
  125. this.session = jingleSession;
  126. this.peerconnection = jingleSession.peerconnection.peerconnection;
  127. this.userID = Settings.getCallStatsUserName();
  128. var location = window.location;
  129. // The confID is case sensitive!!!
  130. this.confID = location.hostname + "/" + options.roomName;
  131. //userID is generated or given by the origin server
  132. callStats.initialize(options.callStatsID,
  133. options.callStatsSecret,
  134. this.userID,
  135. initCallback.bind(this));
  136. } catch (e) {
  137. // The callstats.io API failed to initialize (e.g. because its
  138. // download failed to succeed in general or on time). Further
  139. // attempts to utilize it cannot possibly succeed.
  140. GlobalOnErrorHandler.callErrorHandler(e);
  141. callStats = null;
  142. logger.error(e);
  143. }
  144. });
  145. // some errors/events may happen before CallStats init
  146. // in this case we accumulate them in this array
  147. // and send them to callstats on init
  148. CallStats.reportsQueue = [];
  149. /**
  150. * Whether the library was successfully initialized using its initialize method.
  151. * And whether we had successfully called addNewFabric.
  152. * @type {boolean}
  153. */
  154. CallStats.initialized = false;
  155. /**
  156. * Type of pending reports, can be event or an error.
  157. * @type {{ERROR: string, EVENT: string}}
  158. */
  159. var reportType = {
  160. ERROR: "error",
  161. EVENT: "event",
  162. MST_WITH_USERID: "mstWithUserID"
  163. };
  164. CallStats.prototype.pcCallback = _try_catch(function (err, msg) {
  165. if (!callStats) {
  166. return;
  167. }
  168. logger.log("Monitoring status: "+ err + " msg: " + msg);
  169. });
  170. /**
  171. * Lets CallStats module know where is given SSRC rendered by providing renderer
  172. * tag ID.
  173. * If the lib is not initialized yet queue the call for later, when its ready.
  174. * @param ssrc {number} the SSRC of the stream
  175. * @param isLocal {boolean} <tt>true<tt> if this stream is local or
  176. * <tt>false</tt> otherwise.
  177. * @param usageLabel {string} meaningful usage label of this stream like
  178. * 'microphone', 'camera' or 'screen'.
  179. * @param containerId {string} the id of media 'audio' or 'video' tag which
  180. * renders the stream.
  181. */
  182. CallStats.prototype.associateStreamWithVideoTag =
  183. function (ssrc, isLocal, usageLabel, containerId) {
  184. if(!callStats) {
  185. return;
  186. }
  187. // 'focus' is default remote user ID for now
  188. var callStatsId = 'focus';
  189. if (isLocal) {
  190. callStatsId = this.userID;
  191. }
  192. _try_catch(function() {
  193. logger.debug(
  194. "Calling callStats.associateMstWithUserID with:",
  195. this.peerconnection,
  196. callStatsId,
  197. this.confID,
  198. ssrc,
  199. usageLabel,
  200. containerId
  201. );
  202. if(CallStats.initialized) {
  203. callStats.associateMstWithUserID(
  204. this.peerconnection,
  205. callStatsId,
  206. this.confID,
  207. ssrc,
  208. usageLabel,
  209. containerId
  210. );
  211. }
  212. else {
  213. CallStats.reportsQueue.push({
  214. type: reportType.MST_WITH_USERID,
  215. data: {
  216. callStatsId: callStatsId,
  217. ssrc: ssrc,
  218. usageLabel: usageLabel,
  219. containerId: containerId
  220. }
  221. });
  222. }
  223. }).bind(this)();
  224. };
  225. /**
  226. * Notifies CallStats for mute events
  227. * @param mute {boolean} true for muted and false for not muted
  228. * @param type {String} "audio"/"video"
  229. * @param {CallStats} cs callstats instance related to the event
  230. */
  231. CallStats.sendMuteEvent = _try_catch(function (mute, type, cs) {
  232. var event = null;
  233. if (type === "video") {
  234. event = (mute? fabricEvent.videoPause : fabricEvent.videoResume);
  235. }
  236. else {
  237. event = (mute? fabricEvent.audioMute : fabricEvent.audioUnmute);
  238. }
  239. CallStats._reportEvent.call(cs, event);
  240. });
  241. /**
  242. * Notifies CallStats for screen sharing events
  243. * @param start {boolean} true for starting screen sharing and
  244. * false for not stopping
  245. * @param {CallStats} cs callstats instance related to the event
  246. */
  247. CallStats.sendScreenSharingEvent = _try_catch(function (start, cs) {
  248. CallStats._reportEvent.call(cs,
  249. start? fabricEvent.screenShareStart : fabricEvent.screenShareStop);
  250. });
  251. /**
  252. * Notifies CallStats that we are the new dominant speaker in the conference.
  253. * @param {CallStats} cs callstats instance related to the event
  254. */
  255. CallStats.sendDominantSpeakerEvent = _try_catch(function (cs) {
  256. CallStats._reportEvent.call(cs,
  257. fabricEvent.dominantSpeaker);
  258. });
  259. /**
  260. * Notifies CallStats about active device.
  261. * @param {{deviceList: {String:String}}} list of devices with their data
  262. * @param {CallStats} cs callstats instance related to the event
  263. */
  264. CallStats.sendАctiveDeviceListEvent = _try_catch(function (devicesData, cs) {
  265. CallStats._reportEvent.call(cs, fabricEvent.activeDeviceList, devicesData);
  266. });
  267. /**
  268. * Reports an error to callstats.
  269. *
  270. * @param type the type of the error, which will be one of the wrtcFuncNames
  271. * @param e the error
  272. * @param pc the peerconnection
  273. * @param eventData additional data to pass to event
  274. * @private
  275. */
  276. CallStats._reportEvent = function (event, eventData) {
  277. if (CallStats.initialized) {
  278. callStats.sendFabricEvent(
  279. this.peerconnection, event, this.confID, eventData);
  280. } else {
  281. CallStats.reportsQueue.push({
  282. type: reportType.EVENT,
  283. data: {event: event, eventData: eventData}
  284. });
  285. }
  286. };
  287. /**
  288. * Notifies CallStats for connection setup errors
  289. */
  290. CallStats.prototype.sendTerminateEvent = _try_catch(function () {
  291. if(!CallStats.initialized) {
  292. return;
  293. }
  294. callStats.sendFabricEvent(this.peerconnection,
  295. callStats.fabricEvent.fabricTerminated, this.confID);
  296. });
  297. /**
  298. * Notifies CallStats for ice connection failed
  299. * @param {RTCPeerConnection} pc connection on which failure occured.
  300. * @param {CallStats} cs callstats instance related to the error (optional)
  301. */
  302. CallStats.prototype.sendIceConnectionFailedEvent = _try_catch(function (pc, cs){
  303. CallStats._reportError.call(
  304. cs, wrtcFuncNames.iceConnectionFailure, null, pc);
  305. });
  306. /**
  307. * Sends the given feedback through CallStats.
  308. *
  309. * @param overallFeedback an integer between 1 and 5 indicating the
  310. * user feedback
  311. * @param detailedFeedback detailed feedback from the user. Not yet used
  312. */
  313. CallStats.prototype.sendFeedback = _try_catch(
  314. function(overallFeedback, detailedFeedback) {
  315. if(!CallStats.initialized) {
  316. return;
  317. }
  318. var feedbackString = '{"userID":"' + this.userID + '"' +
  319. ', "overall":' + overallFeedback +
  320. ', "comment": "' + detailedFeedback + '"}';
  321. var feedbackJSON = JSON.parse(feedbackString);
  322. callStats.sendUserFeedback(this.confID, feedbackJSON);
  323. });
  324. /**
  325. * Reports an error to callstats.
  326. *
  327. * @param type the type of the error, which will be one of the wrtcFuncNames
  328. * @param e the error
  329. * @param pc the peerconnection
  330. * @private
  331. */
  332. CallStats._reportError = function (type, e, pc) {
  333. if(!e) {
  334. logger.warn("No error is passed!");
  335. e = new Error("Unknown error");
  336. }
  337. if (CallStats.initialized) {
  338. callStats.reportError(pc, this.confID, type, e);
  339. } else {
  340. CallStats.reportsQueue.push({
  341. type: reportType.ERROR,
  342. data: { type: type, error: e, pc: pc}
  343. });
  344. }
  345. // else just ignore it
  346. };
  347. /**
  348. * Notifies CallStats that getUserMedia failed.
  349. *
  350. * @param {Error} e error to send
  351. * @param {CallStats} cs callstats instance related to the error (optional)
  352. */
  353. CallStats.sendGetUserMediaFailed = _try_catch(function (e, cs) {
  354. CallStats._reportError.call(cs, wrtcFuncNames.getUserMedia, e, null);
  355. });
  356. /**
  357. * Notifies CallStats that peer connection failed to create offer.
  358. *
  359. * @param {Error} e error to send
  360. * @param {RTCPeerConnection} pc connection on which failure occured.
  361. * @param {CallStats} cs callstats instance related to the error (optional)
  362. */
  363. CallStats.sendCreateOfferFailed = _try_catch(function (e, pc, cs) {
  364. CallStats._reportError.call(cs, wrtcFuncNames.createOffer, e, pc);
  365. });
  366. /**
  367. * Notifies CallStats that peer connection failed to create answer.
  368. *
  369. * @param {Error} e error to send
  370. * @param {RTCPeerConnection} pc connection on which failure occured.
  371. * @param {CallStats} cs callstats instance related to the error (optional)
  372. */
  373. CallStats.sendCreateAnswerFailed = _try_catch(function (e, pc, cs) {
  374. CallStats._reportError.call(cs, wrtcFuncNames.createAnswer, e, pc);
  375. });
  376. /**
  377. * Notifies CallStats that peer connection failed to set local description.
  378. *
  379. * @param {Error} e error to send
  380. * @param {RTCPeerConnection} pc connection on which failure occured.
  381. * @param {CallStats} cs callstats instance related to the error (optional)
  382. */
  383. CallStats.sendSetLocalDescFailed = _try_catch(function (e, pc, cs) {
  384. CallStats._reportError.call(cs, wrtcFuncNames.setLocalDescription, e, pc);
  385. });
  386. /**
  387. * Notifies CallStats that peer connection failed to set remote description.
  388. *
  389. * @param {Error} e error to send
  390. * @param {RTCPeerConnection} pc connection on which failure occured.
  391. * @param {CallStats} cs callstats instance related to the error (optional)
  392. */
  393. CallStats.sendSetRemoteDescFailed = _try_catch(function (e, pc, cs) {
  394. CallStats._reportError.call(cs, wrtcFuncNames.setRemoteDescription, e, pc);
  395. });
  396. /**
  397. * Notifies CallStats that peer connection failed to add ICE candidate.
  398. *
  399. * @param {Error} e error to send
  400. * @param {RTCPeerConnection} pc connection on which failure occured.
  401. * @param {CallStats} cs callstats instance related to the error (optional)
  402. */
  403. CallStats.sendAddIceCandidateFailed = _try_catch(function (e, pc, cs) {
  404. CallStats._reportError.call(cs, wrtcFuncNames.addIceCandidate, e, pc);
  405. });
  406. /**
  407. * Notifies CallStats that there is a log we want to report.
  408. *
  409. * @param {Error} e error to send or {String} message
  410. * @param {CallStats} cs callstats instance related to the error (optional)
  411. */
  412. CallStats.sendApplicationLog = _try_catch(function (e, cs) {
  413. CallStats._reportError
  414. .call(cs, wrtcFuncNames.applicationLog, e, null);
  415. });
  416. module.exports = CallStats;