Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

CallStats.js 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /* global config, $, APP, Strophe, callstats */
  2. var Settings = require('../settings/Settings');
  3. var jsSHA = require('jssha');
  4. var io = require('socket.io-client');
  5. var callStats = null;
  6. /**
  7. * @const
  8. * @see http://www.callstats.io/api/#enumeration-of-wrtcfuncnames
  9. */
  10. var wrtcFuncNames = {
  11. createOffer: "createOffer",
  12. createAnswer: "createAnswer",
  13. setLocalDescription: "setLocalDescription",
  14. setRemoteDescription: "setRemoteDescription",
  15. addIceCandidate: "addIceCandidate",
  16. getUserMedia: "getUserMedia"
  17. };
  18. // some errors may happen before CallStats init
  19. // in this case we accumulate them in this array
  20. // and send them to callstats on init
  21. var pendingErrors = [];
  22. function initCallback (err, msg) {
  23. console.log("CallStats Status: err=" + err + " msg=" + msg);
  24. }
  25. var callStatsIntegrationEnabled = config.callStatsID && config.callStatsSecret;
  26. /**
  27. * Returns a function which invokes f in a try/catch block, logs any exception
  28. * to the console, and then swallows it.
  29. *
  30. * @param f the function to invoke in a try/catch block
  31. * @return a function which invokes f in a try/catch block, logs any exception
  32. * to the console, and then swallows it
  33. */
  34. function _try_catch (f) {
  35. return function () {
  36. try {
  37. f.apply(this, arguments);
  38. } catch (e) {
  39. console.error(e);
  40. }
  41. };
  42. }
  43. var CallStats = {
  44. init: _try_catch(function (jingleSession) {
  45. if(!this.isEnabled() || callStats !== null) {
  46. return;
  47. }
  48. try {
  49. callStats = new callstats($, io, jsSHA);
  50. this.session = jingleSession;
  51. this.peerconnection = jingleSession.peerconnection.peerconnection;
  52. this.userID = Settings.getCallStatsUserName();
  53. var location = window.location;
  54. this.confID = location.hostname + location.pathname;
  55. callStats.initialize(
  56. config.callStatsID, config.callStatsSecret,
  57. this.userID /* generated or given by the origin server */,
  58. initCallback);
  59. var usage = callStats.fabricUsage.multiplex;
  60. callStats.addNewFabric(
  61. this.peerconnection,
  62. Strophe.getResourceFromJid(jingleSession.peerjid),
  63. usage,
  64. this.confID,
  65. this.pcCallback.bind(this));
  66. } catch (e) {
  67. // The callstats.io API failed to initialize (e.g. because its
  68. // download failed to succeed in general or on time). Further
  69. // attempts to utilize it cannot possibly succeed.
  70. callStats = null;
  71. console.error(e);
  72. }
  73. // Notify callstats about pre-init failures if there were any.
  74. if (callStats && pendingErrors.length) {
  75. pendingErrors.forEach(function (error) {
  76. this._reportError(error.type, error.error, error.pc);
  77. }, this);
  78. pendingErrors.length = 0;
  79. }
  80. }),
  81. /**
  82. * Returns true if the callstats integration is enabled, otherwise returns
  83. * false.
  84. *
  85. * @returns true if the callstats integration is enabled, otherwise returns
  86. * false.
  87. */
  88. isEnabled: function() {
  89. return callStatsIntegrationEnabled;
  90. },
  91. pcCallback: _try_catch(function (err, msg) {
  92. if (!callStats) {
  93. return;
  94. }
  95. console.log("Monitoring status: "+ err + " msg: " + msg);
  96. callStats.sendFabricEvent(this.peerconnection,
  97. callStats.fabricEvent.fabricSetup, this.confID);
  98. }),
  99. sendMuteEvent: _try_catch(function (mute, type) {
  100. if (!callStats) {
  101. return;
  102. }
  103. var event = null;
  104. if (type === "video") {
  105. event = (mute? callStats.fabricEvent.videoPause :
  106. callStats.fabricEvent.videoResume);
  107. }
  108. else {
  109. event = (mute? callStats.fabricEvent.audioMute :
  110. callStats.fabricEvent.audioUnmute);
  111. }
  112. callStats.sendFabricEvent(this.peerconnection, event, this.confID);
  113. }),
  114. sendTerminateEvent: _try_catch(function () {
  115. if(!callStats) {
  116. return;
  117. }
  118. callStats.sendFabricEvent(this.peerconnection,
  119. callStats.fabricEvent.fabricTerminated, this.confID);
  120. }),
  121. sendSetupFailedEvent: _try_catch(function () {
  122. if(!callStats) {
  123. return;
  124. }
  125. callStats.sendFabricEvent(this.peerconnection,
  126. callStats.fabricEvent.fabricSetupFailed, this.confID);
  127. }),
  128. /**
  129. * Sends the given feedback through CallStats.
  130. *
  131. * @param overallFeedback an integer between 1 and 5 indicating the
  132. * user feedback
  133. * @param detailedFeedback detailed feedback from the user. Not yet used
  134. */
  135. sendFeedback: _try_catch(function(overallFeedback, detailedFeedback) {
  136. if(!callStats) {
  137. return;
  138. }
  139. var feedbackString = '{"userID":"' + this.userID + '"' +
  140. ', "overall":' + overallFeedback +
  141. ', "comment": "' + detailedFeedback + '"}';
  142. var feedbackJSON = JSON.parse(feedbackString);
  143. callStats.sendUserFeedback(this.confID, feedbackJSON);
  144. }),
  145. /**
  146. * Reports an error to callstats.
  147. *
  148. * @param type the type of the error, which will be one of the wrtcFuncNames
  149. * @param e the error
  150. * @param pc the peerconnection
  151. * @private
  152. */
  153. _reportError: function (type, e, pc) {
  154. if (callStats) {
  155. callStats.reportError(pc, this.confID, type, e);
  156. } else if (this.isEnabled()) {
  157. pendingErrors.push({ type: type, error: e, pc: pc });
  158. }
  159. // else just ignore it
  160. },
  161. /**
  162. * Notifies CallStats that getUserMedia failed.
  163. *
  164. * @param {Error} e error to send
  165. */
  166. sendGetUserMediaFailed: _try_catch(function (e) {
  167. this._reportError(wrtcFuncNames.getUserMedia, e, null);
  168. }),
  169. /**
  170. * Notifies CallStats that peer connection failed to create offer.
  171. *
  172. * @param {Error} e error to send
  173. * @param {RTCPeerConnection} pc connection on which failure occured.
  174. */
  175. sendCreateOfferFailed: _try_catch(function (e, pc) {
  176. this._reportError(wrtcFuncNames.createOffer, e, pc);
  177. }),
  178. /**
  179. * Notifies CallStats that peer connection failed to create answer.
  180. *
  181. * @param {Error} e error to send
  182. * @param {RTCPeerConnection} pc connection on which failure occured.
  183. */
  184. sendCreateAnswerFailed: _try_catch(function (e, pc) {
  185. this._reportError(wrtcFuncNames.createAnswer, e, pc);
  186. }),
  187. /**
  188. * Notifies CallStats that peer connection failed to set local description.
  189. *
  190. * @param {Error} e error to send
  191. * @param {RTCPeerConnection} pc connection on which failure occured.
  192. */
  193. sendSetLocalDescFailed: _try_catch(function (e, pc) {
  194. this._reportError(wrtcFuncNames.setLocalDescription, e, pc);
  195. }),
  196. /**
  197. * Notifies CallStats that peer connection failed to set remote description.
  198. *
  199. * @param {Error} e error to send
  200. * @param {RTCPeerConnection} pc connection on which failure occured.
  201. */
  202. sendSetRemoteDescFailed: _try_catch(function (e, pc) {
  203. this._reportError(wrtcFuncNames.setRemoteDescription, e, pc);
  204. }),
  205. /**
  206. * Notifies CallStats that peer connection failed to add ICE candidate.
  207. *
  208. * @param {Error} e error to send
  209. * @param {RTCPeerConnection} pc connection on which failure occured.
  210. */
  211. sendAddIceCandidateFailed: _try_catch(function (e, pc) {
  212. this._reportError(wrtcFuncNames.addIceCandidate, e, pc);
  213. })
  214. };
  215. module.exports = CallStats;