Correct the calculation of discard rate.

Bug: webrtc:7903
Change-Id: Ib5d6fd882a994dd542b616e5fe1c75710346dd31
Reviewed-on: https://chromium-review.googlesource.com/575057
Commit-Queue: Minyue Li <minyue@webrtc.org>
Reviewed-by: Oskar Sundbom <ossu@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#19101}
diff --git a/webrtc/modules/audio_coding/neteq/packet_buffer.cc b/webrtc/modules/audio_coding/neteq/packet_buffer.cc
index efb3505..1e71525 100644
--- a/webrtc/modules/audio_coding/neteq/packet_buffer.cc
+++ b/webrtc/modules/audio_coding/neteq/packet_buffer.cc
@@ -68,7 +68,7 @@
   return buffer_.empty();
 }
 
-int PacketBuffer::InsertPacket(Packet&& packet) {
+int PacketBuffer::InsertPacket(Packet&& packet, StatisticsCalculator* stats) {
   if (packet.empty()) {
     LOG(LS_WARNING) << "InsertPacket invalid packet";
     return kInvalidPacket;
@@ -99,6 +99,8 @@
   // timestamp as |rit|, which has a higher priority, do not insert the new
   // packet to list.
   if (rit != buffer_.rend() && packet.timestamp == rit->timestamp) {
+    RTC_CHECK(stats);
+    stats->PacketsDiscarded(1);
     return return_val;
   }
 
@@ -108,6 +110,8 @@
   PacketList::iterator it = rit.base();
   if (it != buffer_.end() && packet.timestamp == it->timestamp) {
     it = buffer_.erase(it);
+    RTC_CHECK(stats);
+    stats->PacketsDiscarded(1);
   }
   buffer_.insert(it, std::move(packet));  // Insert the packet at that position.
 
@@ -118,7 +122,9 @@
     PacketList* packet_list,
     const DecoderDatabase& decoder_database,
     rtc::Optional<uint8_t>* current_rtp_payload_type,
-    rtc::Optional<uint8_t>* current_cng_rtp_payload_type) {
+    rtc::Optional<uint8_t>* current_cng_rtp_payload_type,
+    StatisticsCalculator* stats) {
+  RTC_DCHECK(stats);
   bool flushed = false;
   for (auto& packet : *packet_list) {
     if (decoder_database.IsComfortNoise(packet.payload_type)) {
@@ -145,7 +151,7 @@
       }
       *current_rtp_payload_type = rtc::Optional<uint8_t>(packet.payload_type);
     }
-    int return_val = InsertPacket(std::move(packet));
+    int return_val = InsertPacket(std::move(packet), stats);
     if (return_val == kFlushed) {
       // The buffer flushed, but this is not an error. We can still continue.
       flushed = true;
@@ -214,6 +220,7 @@
   // Assert that the packet sanity checks in InsertPacket method works.
   RTC_DCHECK(!buffer_.front().empty());
   buffer_.pop_front();
+  RTC_CHECK(stats);
   stats->PacketsDiscarded(1);
   return kOK;
 }
@@ -227,6 +234,7 @@
            IsObsoleteTimestamp(p.timestamp, timestamp_limit, horizon_samples);
   });
   if (old_size > buffer_.size()) {
+    RTC_CHECK(stats);
     stats->PacketsDiscarded(old_size - buffer_.size());
   }
 }
@@ -248,8 +256,10 @@
       ++it;
     }
   }
-  if (packets_discarded > 0)
+  if (packets_discarded > 0) {
+    RTC_CHECK(stats);
     stats->PacketsDiscarded(packets_discarded);
+  }
 }
 
 size_t PacketBuffer::NumPacketsInBuffer() const {