Просмотр исходного кода

fix(RTPStatsCollector) don't skip processing the first report

Some values, like codecs, are available right away, we don't need to
compute differences.
tags/v0.0.2
Saúl Ibarra Corretgé 2 лет назад
Родитель
Сommit
d34162a57c
1 измененных файлов: 26 добавлений и 24 удалений
  1. 26
    24
      modules/statistics/RTPStatsCollector.js

+ 26
- 24
modules/statistics/RTPStatsCollector.js Просмотреть файл

@@ -494,12 +494,11 @@ StatsCollector.prototype._calculateBitrate = function(now, before, fieldName) {
494 494
  * Stats processing for spec-compliant RTCPeerConnection#getStats.
495 495
  */
496 496
 StatsCollector.prototype.processStatsReport = function() {
497
-    if (!this.previousStatsReport) {
498
-        return;
499
-    }
500 497
     const byteSentStats = {};
501 498
 
502 499
     this.currentStatsReport.forEach(now => {
500
+        const before = this.previousStatsReport ? this.previousStatsReport.get(now.id) : null;
501
+
503 502
         // RTCIceCandidatePairStats - https://w3c.github.io/webrtc-stats/#candidatepair-dict*
504 503
         if (now.type === 'candidate-pair' && now.nominated && now.state === 'succeeded') {
505 504
             const availableIncomingBitrate = now.availableIncomingBitrate;
@@ -556,10 +555,9 @@ StatsCollector.prototype.processStatsReport = function() {
556 555
         // RTCSentRtpStreamStats
557 556
         // https://w3c.github.io/webrtc-stats/#sentrtpstats-dict*
558 557
         } else if (now.type === 'inbound-rtp' || now.type === 'outbound-rtp') {
559
-            const before = this.previousStatsReport.get(now.id);
560 558
             const ssrc = this.getNonNegativeValue(now.ssrc);
561 559
 
562
-            if (!before || !ssrc) {
560
+            if (!ssrc) {
563 561
                 return;
564 562
             }
565 563
 
@@ -584,18 +582,20 @@ StatsCollector.prototype.processStatsReport = function() {
584 582
                 packetsNow = 0;
585 583
             }
586 584
 
587
-            const packetsBefore = this.getNonNegativeValue(before[key]);
588
-            const packetsDiff = Math.max(0, packetsNow - packetsBefore);
585
+            if (before) {
586
+                const packetsBefore = this.getNonNegativeValue(before[key]);
587
+                const packetsDiff = Math.max(0, packetsNow - packetsBefore);
589 588
 
590
-            const packetsLostNow = this.getNonNegativeValue(now.packetsLost);
591
-            const packetsLostBefore = this.getNonNegativeValue(before.packetsLost);
592
-            const packetsLostDiff = Math.max(0, packetsLostNow - packetsLostBefore);
589
+                const packetsLostNow = this.getNonNegativeValue(now.packetsLost);
590
+                const packetsLostBefore = this.getNonNegativeValue(before.packetsLost);
591
+                const packetsLostDiff = Math.max(0, packetsLostNow - packetsLostBefore);
593 592
 
594
-            ssrcStats.setLoss({
595
-                packetsTotal: packetsDiff + packetsLostDiff,
596
-                packetsLost: packetsLostDiff,
597
-                isDownloadStream
598
-            });
593
+                ssrcStats.setLoss({
594
+                    packetsTotal: packetsDiff + packetsLostDiff,
595
+                    packetsLost: packetsLostDiff,
596
+                    isDownloadStream
597
+                });
598
+            }
599 599
 
600 600
             // Get the resolution and framerate for only remote video sources here. For the local video sources,
601 601
             // 'track' stats will be used since they have the updated resolution based on the simulcast streams
@@ -614,11 +614,13 @@ StatsCollector.prototype.processStatsReport = function() {
614 614
                 }
615 615
                 ssrcStats.setFramerate(Math.round(frameRate || 0));
616 616
 
617
-                ssrcStats.addBitrate({
618
-                    'download': this._calculateBitrate(now, before, 'bytesReceived'),
619
-                    'upload': 0
620
-                });
621
-            } else {
617
+                if (before) {
618
+                    ssrcStats.addBitrate({
619
+                        'download': this._calculateBitrate(now, before, 'bytesReceived'),
620
+                        'upload': 0
621
+                    });
622
+                }
623
+            } else if (before) {
622 624
                 byteSentStats[ssrc] = this.getNonNegativeValue(now.bytesSent);
623 625
                 ssrcStats.addBitrate({
624 626
                     'download': 0,
@@ -673,8 +675,6 @@ StatsCollector.prototype.processStatsReport = function() {
673 675
             let frameRate = now.framesPerSecond;
674 676
 
675 677
             if (!frameRate) {
676
-                const before = this.previousStatsReport.get(now.id);
677
-
678 678
                 if (before) {
679 679
                     const timeMs = now.timestamp - before.timestamp;
680 680
 
@@ -699,7 +699,10 @@ StatsCollector.prototype.processStatsReport = function() {
699 699
         }
700 700
     });
701 701
 
702
-    this.eventEmitter.emit(StatisticsEvents.BYTE_SENT_STATS, this.peerconnection, byteSentStats);
702
+    if (Object.keys(byteSentStats).length) {
703
+        this.eventEmitter.emit(StatisticsEvents.BYTE_SENT_STATS, this.peerconnection, byteSentStats);
704
+    }
705
+
703 706
     this._processAndEmitReport();
704 707
 };
705 708
 
@@ -740,4 +743,3 @@ StatsCollector.prototype.processAudioLevelReport = function() {
740 743
         }
741 744
     });
742 745
 };
743
-

Загрузка…
Отмена
Сохранить