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

CallStats.js 12KB

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