浏览代码

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 年前
父节点
当前提交
65f6027463
共有 2 个文件被更改,包括 23 次插入40 次删除
  1. 6
    9
      modules/RTC/TraceablePeerConnection.js
  2. 17
    31
      modules/statistics/RTPStatsCollector.js

+ 6
- 9
modules/RTC/TraceablePeerConnection.js 查看文件

355
 
355
 
356
     if (this.maxstats) {
356
     if (this.maxstats) {
357
         this.statsinterval = window.setInterval(() => {
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
                     const results = stats.result();
360
                     const results = stats.result();
362
 
361
 
363
                     for (let i = 0; i < results.length; ++i) {
362
                     for (let i = 0; i < results.length; ++i) {
2957
 /**
2956
 /**
2958
  * Obtains call-related stats from the peer connection.
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 查看文件

220
                     }
220
                     }
221
                 } else {
221
                 } else {
222
                     // Interval updates
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
                             this.processAudioLevelReport();
228
                             this.processAudioLevelReport();
235
                             this.baselineAudioLevelsReport = this.currentAudioLevelsReport;
229
                             this.baselineAudioLevelsReport = this.currentAudioLevelsReport;
236
-                        },
237
-                        error => this.errorCallback(error)
238
-                    );
230
+                        })
231
+                        .catch(error => this.errorCallback(error));
239
                 }
232
                 }
240
             },
233
             },
241
             this.audioLevelsIntervalMilis
234
             this.audioLevelsIntervalMilis
244
 
237
 
245
     const processStats = () => {
238
     const processStats = () => {
246
         // Interval updates
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
                 try {
246
                 try {
262
                     this.processStatsReport();
247
                     this.processStatsReport();
263
                 } catch (error) {
248
                 } catch (error) {
265
                     logger.error('Processing of RTP stats failed:', error);
250
                     logger.error('Processing of RTP stats failed:', error);
266
                 }
251
                 }
267
                 this.previousStatsReport = this.currentStatsReport;
252
                 this.previousStatsReport = this.currentStatsReport;
268
-            },
269
-            error => this.errorCallback(error)
270
-        );
253
+            })
254
+            .catch(error => this.errorCallback(error));
271
     };
255
     };
272
 
256
 
273
     processStats();
257
     processStats();
680
             // Get the number of simulcast streams currently enabled from TPC.
664
             // Get the number of simulcast streams currently enabled from TPC.
681
             const numberOfActiveStreams = this.peerconnection.getActiveSimulcastStreams();
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
 

正在加载...
取消
保存