You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CallStats.js 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. var CallStats = {
  27. init: function (jingleSession) {
  28. if(!callStatsIntegrationEnabled || callStats !== null) {
  29. return;
  30. }
  31. callStats = new callstats($, io, jsSHA);
  32. this.session = jingleSession;
  33. this.peerconnection = jingleSession.peerconnection.peerconnection;
  34. this.userID = Settings.getCallStatsUserName();
  35. var location = window.location;
  36. this.confID = location.hostname + location.pathname;
  37. //userID is generated or given by the origin server
  38. callStats.initialize(config.callStatsID,
  39. config.callStatsSecret,
  40. this.userID,
  41. initCallback);
  42. var usage = callStats.fabricUsage.multiplex;
  43. callStats.addNewFabric(this.peerconnection,
  44. Strophe.getResourceFromJid(jingleSession.peerjid),
  45. usage,
  46. this.confID,
  47. this.pcCallback.bind(this));
  48. // notify callstats about failures if there were any
  49. if (pendingErrors.length) {
  50. pendingErrors.forEach(function (error) {
  51. this._reportError(error.type, error.error, error.pc);
  52. }, this);
  53. pendingErrors.length = 0;
  54. }
  55. },
  56. /**
  57. * Returns true if the callstats integration is enabled, otherwise returns
  58. * false.
  59. *
  60. * @returns true if the callstats integration is enabled, otherwise returns
  61. * false.
  62. */
  63. isEnabled: function() {
  64. return callStatsIntegrationEnabled;
  65. },
  66. pcCallback: function (err, msg) {
  67. if (!callStats) {
  68. return;
  69. }
  70. console.log("Monitoring status: "+ err + " msg: " + msg);
  71. callStats.sendFabricEvent(this.peerconnection,
  72. callStats.fabricEvent.fabricSetup, this.confID);
  73. },
  74. sendMuteEvent: function (mute, type) {
  75. if (!callStats) {
  76. return;
  77. }
  78. var event = null;
  79. if (type === "video") {
  80. event = (mute? callStats.fabricEvent.videoPause :
  81. callStats.fabricEvent.videoResume);
  82. }
  83. else {
  84. event = (mute? callStats.fabricEvent.audioMute :
  85. callStats.fabricEvent.audioUnmute);
  86. }
  87. callStats.sendFabricEvent(this.peerconnection, event, this.confID);
  88. },
  89. sendTerminateEvent: function () {
  90. if(!callStats) {
  91. return;
  92. }
  93. callStats.sendFabricEvent(this.peerconnection,
  94. callStats.fabricEvent.fabricTerminated, this.confID);
  95. },
  96. sendSetupFailedEvent: function () {
  97. if(!callStats) {
  98. return;
  99. }
  100. callStats.sendFabricEvent(this.peerconnection,
  101. callStats.fabricEvent.fabricSetupFailed, this.confID);
  102. },
  103. /**
  104. * Sends the given feedback through CallStats.
  105. *
  106. * @param overallFeedback an integer between 1 and 5 indicating the
  107. * user feedback
  108. * @param detailedFeedback detailed feedback from the user. Not yet used
  109. */
  110. sendFeedback: function(overallFeedback, detailedFeedback) {
  111. if(!callStats) {
  112. return;
  113. }
  114. var feedbackString = '{"userID":"' + this.userID + '"' +
  115. ', "overall":' + overallFeedback +
  116. ', "comment": "' + detailedFeedback + '"}';
  117. var feedbackJSON = JSON.parse(feedbackString);
  118. callStats.sendUserFeedback(
  119. this.confID, feedbackJSON);
  120. },
  121. /**
  122. * Reports an error to callstats.
  123. *
  124. * @param type the type of the error, which will be one of the wrtcFuncNames
  125. * @param e the error
  126. * @param pc the peerconnection
  127. * @private
  128. */
  129. _reportError: function (type, e, pc) {
  130. if (callStats) {
  131. callStats.reportError(pc, this.confID, type, e);
  132. } else if (callStatsIntegrationEnabled) {
  133. pendingErrors.push({
  134. type: type,
  135. error: e,
  136. pc: pc
  137. });
  138. }
  139. // else just ignore it
  140. },
  141. /**
  142. * Notifies CallStats that getUserMedia failed.
  143. *
  144. * @param {Error} e error to send
  145. */
  146. sendGetUserMediaFailed: function (e) {
  147. this._reportError(wrtcFuncNames.getUserMedia, e, null);
  148. },
  149. /**
  150. * Notifies CallStats that peer connection failed to create offer.
  151. *
  152. * @param {Error} e error to send
  153. * @param {RTCPeerConnection} pc connection on which failure occured.
  154. */
  155. sendCreateOfferFailed: function (e, pc) {
  156. this._reportError(wrtcFuncNames.createOffer, e, pc);
  157. },
  158. /**
  159. * Notifies CallStats that peer connection failed to create answer.
  160. *
  161. * @param {Error} e error to send
  162. * @param {RTCPeerConnection} pc connection on which failure occured.
  163. */
  164. sendCreateAnswerFailed: function (e, pc) {
  165. this._reportError(wrtcFuncNames.createAnswer, e, pc);
  166. },
  167. /**
  168. * Notifies CallStats that peer connection failed to set local description.
  169. *
  170. * @param {Error} e error to send
  171. * @param {RTCPeerConnection} pc connection on which failure occured.
  172. */
  173. sendSetLocalDescFailed: function (e, pc) {
  174. this._reportError(wrtcFuncNames.setLocalDescription, e, pc);
  175. },
  176. /**
  177. * Notifies CallStats that peer connection failed to set remote description.
  178. *
  179. * @param {Error} e error to send
  180. * @param {RTCPeerConnection} pc connection on which failure occured.
  181. */
  182. sendSetRemoteDescFailed: function (e, pc) {
  183. this._reportError(wrtcFuncNames.setRemoteDescription, e, pc);
  184. },
  185. /**
  186. * Notifies CallStats that peer connection failed to add ICE candidate.
  187. *
  188. * @param {Error} e error to send
  189. * @param {RTCPeerConnection} pc connection on which failure occured.
  190. */
  191. sendAddIceCandidateFailed: function (e, pc) {
  192. this._reportError(wrtcFuncNames.addIceCandidate, e, pc);
  193. }
  194. };
  195. module.exports = CallStats;