modified lib-jitsi-meet dev repo
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.

LocalStatsCollector.js 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /* global config */
  2. /**
  3. * Provides statistics for the local stream.
  4. */
  5. var RTCBrowserType = require('../RTC/RTCBrowserType');
  6. var LOCAL_JID = require("../../service/statistics/constants").LOCAL_JID;
  7. /**
  8. * Size of the webaudio analyzer buffer.
  9. * @type {number}
  10. */
  11. var WEBAUDIO_ANALYZER_FFT_SIZE = 2048;
  12. /**
  13. * Value of the webaudio analyzer smoothing time parameter.
  14. * @type {number}
  15. */
  16. var WEBAUDIO_ANALYZER_SMOOTING_TIME = 0.8;
  17. /**
  18. * Converts time domain data array to audio level.
  19. * @param samples the time domain data array.
  20. * @returns {number} the audio level
  21. */
  22. function timeDomainDataToAudioLevel(samples) {
  23. var maxVolume = 0;
  24. var length = samples.length;
  25. for (var i = 0; i < length; i++) {
  26. if (maxVolume < samples[i])
  27. maxVolume = samples[i];
  28. }
  29. return parseFloat(((maxVolume - 127) / 128).toFixed(3));
  30. }
  31. /**
  32. * Animates audio level change
  33. * @param newLevel the new audio level
  34. * @param lastLevel the last audio level
  35. * @returns {Number} the audio level to be set
  36. */
  37. function animateLevel(newLevel, lastLevel) {
  38. var value = 0;
  39. var diff = lastLevel - newLevel;
  40. if(diff > 0.2) {
  41. value = lastLevel - 0.2;
  42. }
  43. else if(diff < -0.4) {
  44. value = lastLevel + 0.4;
  45. }
  46. else {
  47. value = newLevel;
  48. }
  49. return parseFloat(value.toFixed(3));
  50. }
  51. /**
  52. * <tt>LocalStatsCollector</tt> calculates statistics for the local stream.
  53. *
  54. * @param stream the local stream
  55. * @param interval stats refresh interval given in ms.
  56. * @constructor
  57. */
  58. function LocalStatsCollector(stream, interval, statisticsService, eventEmitter) {
  59. window.AudioContext = window.AudioContext || window.webkitAudioContext;
  60. this.stream = stream;
  61. this.intervalId = null;
  62. this.intervalMilis = interval;
  63. this.eventEmitter = eventEmitter;
  64. this.audioLevel = 0;
  65. this.statisticsService = statisticsService;
  66. }
  67. /**
  68. * Starts the collecting the statistics.
  69. */
  70. LocalStatsCollector.prototype.start = function () {
  71. if (!window.AudioContext ||
  72. RTCBrowserType.isTemasysPluginUsed())
  73. return;
  74. var context = new AudioContext();
  75. var analyser = context.createAnalyser();
  76. analyser.smoothingTimeConstant = WEBAUDIO_ANALYZER_SMOOTING_TIME;
  77. analyser.fftSize = WEBAUDIO_ANALYZER_FFT_SIZE;
  78. var source = context.createMediaStreamSource(this.stream);
  79. source.connect(analyser);
  80. var self = this;
  81. this.intervalId = setInterval(
  82. function () {
  83. var array = new Uint8Array(analyser.frequencyBinCount);
  84. analyser.getByteTimeDomainData(array);
  85. var audioLevel = timeDomainDataToAudioLevel(array);
  86. if (audioLevel != self.audioLevel) {
  87. self.audioLevel = animateLevel(audioLevel, self.audioLevel);
  88. self.eventEmitter.emit(
  89. "statistics.audioLevel",
  90. LOCAL_JID,
  91. self.audioLevel);
  92. }
  93. },
  94. this.intervalMilis
  95. );
  96. };
  97. /**
  98. * Stops collecting the statistics.
  99. */
  100. LocalStatsCollector.prototype.stop = function () {
  101. if (this.intervalId) {
  102. clearInterval(this.intervalId);
  103. this.intervalId = null;
  104. }
  105. };
  106. module.exports = LocalStatsCollector;