Browse Source

fix(stats): Return promise for getStats.

Switch to returning a Promise for getStats. Reset frame rate stat to 0 when video is suspended as a result of endpoint falling out of last-n.
dev1
Jaya Allamsetty 4 years ago
parent
commit
65f6027463
2 changed files with 23 additions and 40 deletions
  1. 6
    9
      modules/RTC/TraceablePeerConnection.js
  2. 17
    31
      modules/statistics/RTPStatsCollector.js

+ 6
- 9
modules/RTC/TraceablePeerConnection.js View File

@@ -355,9 +355,8 @@ export default function TraceablePeerConnection(
355 355
 
356 356
     if (this.maxstats) {
357 357
         this.statsinterval = window.setInterval(() => {
358
-            this.getStats(stats => {
359
-                if (stats.result
360
-                    && typeof stats.result === 'function') {
358
+            this.getStats().then(stats => {
359
+                if (typeof stats?.result === 'function') {
361 360
                     const results = stats.result();
362 361
 
363 362
                     for (let i = 0; i < results.length; ++i) {
@@ -2957,13 +2956,11 @@ TraceablePeerConnection.prototype.getActiveSimulcastStreams = function() {
2957 2956
 /**
2958 2957
  * Obtains call-related stats from the peer connection.
2959 2958
  *
2960
- * @param {Function} callback - The function to invoke after successfully obtaining stats.
2961
- * @param {Function} errback - The function to invoke after failing to obtain stats.
2962
- * @returns {void}
2959
+ * @returns {Promise<Object>} Promise which resolves with data providing statistics about
2960
+ * the peerconnection.
2963 2961
  */
2964
-TraceablePeerConnection.prototype.getStats = function(callback, errback) {
2965
-    // eslint-disable-next-line no-empty-function
2966
-    this.peerconnection.getStats().then(callback, errback || (() => {}));
2962
+TraceablePeerConnection.prototype.getStats = function() {
2963
+    return this.peerconnection.getStats();
2967 2964
 };
2968 2965
 
2969 2966
 /**

+ 17
- 31
modules/statistics/RTPStatsCollector.js View File

@@ -220,22 +220,15 @@ StatsCollector.prototype.start = function(startAudioLevelStats) {
220 220
                     }
221 221
                 } else {
222 222
                     // Interval updates
223
-                    this.peerconnection.getStats(
224
-                        report => {
225
-                            let results = null;
226
-
227
-                            if (!report || !report.result
228
-                                || typeof report.result !== 'function') {
229
-                                results = report;
230
-                            } else {
231
-                                results = report.result();
232
-                            }
233
-                            this.currentAudioLevelsReport = results;
223
+                    this.peerconnection.getStats()
224
+                        .then(report => {
225
+                            this.currentAudioLevelsReport = typeof report?.result === 'function'
226
+                                ? report.result()
227
+                                : report;
234 228
                             this.processAudioLevelReport();
235 229
                             this.baselineAudioLevelsReport = this.currentAudioLevelsReport;
236
-                        },
237
-                        error => this.errorCallback(error)
238
-                    );
230
+                        })
231
+                        .catch(error => this.errorCallback(error));
239 232
                 }
240 233
             },
241 234
             this.audioLevelsIntervalMilis
@@ -244,20 +237,12 @@ StatsCollector.prototype.start = function(startAudioLevelStats) {
244 237
 
245 238
     const processStats = () => {
246 239
         // Interval updates
247
-        this.peerconnection.getStats(
248
-            report => {
249
-                let results = null;
250
-
251
-                if (!report || !report.result
252
-                    || typeof report.result !== 'function') {
253
-                    // firefox
254
-                    results = report;
255
-                } else {
256
-                    // chrome
257
-                    results = report.result();
258
-                }
240
+        this.peerconnection.getStats()
241
+            .then(report => {
242
+                this.currentStatsReport = typeof report?.result === 'function'
243
+                    ? report.result()
244
+                    : report;
259 245
 
260
-                this.currentStatsReport = results;
261 246
                 try {
262 247
                     this.processStatsReport();
263 248
                 } catch (error) {
@@ -265,9 +250,8 @@ StatsCollector.prototype.start = function(startAudioLevelStats) {
265 250
                     logger.error('Processing of RTP stats failed:', error);
266 251
                 }
267 252
                 this.previousStatsReport = this.currentStatsReport;
268
-            },
269
-            error => this.errorCallback(error)
270
-        );
253
+            })
254
+            .catch(error => this.errorCallback(error));
271 255
     };
272 256
 
273 257
     processStats();
@@ -680,7 +664,9 @@ StatsCollector.prototype.processStatsReport = function() {
680 664
             // Get the number of simulcast streams currently enabled from TPC.
681 665
             const numberOfActiveStreams = this.peerconnection.getActiveSimulcastStreams();
682 666
 
683
-            ssrcStats.setFramerate(Math.round((frameRate / numberOfActiveStreams) || 0));
667
+            // Reset frame rate to 0 when video is suspended as a result of endpoint falling out of last-n.
668
+            frameRate = numberOfActiveStreams ? Math.round(frameRate / numberOfActiveStreams) : 0;
669
+            ssrcStats.setFramerate(frameRate);
684 670
         }
685 671
     });
686 672
 

Loading…
Cancel
Save