Replacing NetEq discard rate with secondary discarded rate.

NetEq network statistics contains discard rate but has not been used and even not been implemented until recently.

According to w3c.github.io/webrtc-stats/#dom-rtcreceivedrtpstreamstats-packetsdiscarded,
this statistics needs to be replaced with an accumulative stats. Such work will be carried out separately.

Meanwhile, we need to add a rate to reflect rate of discarded redundant packets. See webrtc:8025.

In this CL, we replace the existing discard rate with secondary discarded rate, so as to
1. fulfill the requests on webrtc:8025
2. get ready to implement an accumulative statistics for discarded packets.

BUG: webrtc:7903,webrtc:8025
Change-Id: Idbf143a105db76ca15f0af54848e1448f2a810ec
Reviewed-on: https://chromium-review.googlesource.com/582863
Reviewed-by: Henrik Lundin <henrik.lundin@webrtc.org>
Reviewed-by: Karl Wiberg <kwiberg@webrtc.org>
Commit-Queue: Minyue Li <minyue@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#19495}
diff --git a/webrtc/modules/audio_coding/neteq/packet_buffer.cc b/webrtc/modules/audio_coding/neteq/packet_buffer.cc
index 1e71525..e0e2e9a 100644
--- a/webrtc/modules/audio_coding/neteq/packet_buffer.cc
+++ b/webrtc/modules/audio_coding/neteq/packet_buffer.cc
@@ -48,6 +48,16 @@
   auto* di2 = decoder_database.GetDecoderInfo(pt2);
   return di1 && di2 && di1->SampleRateHz() == di2->SampleRateHz();
 }
+
+void LogPacketDiscarded(int codec_level, StatisticsCalculator* stats) {
+  RTC_CHECK(stats);
+  if (codec_level > 0) {
+    stats->SecondaryPacketsDiscarded(1);
+  } else {
+    stats->PacketsDiscarded(1);
+  }
+}
+
 }  // namespace
 
 PacketBuffer::PacketBuffer(size_t max_number_of_packets,
@@ -99,8 +109,7 @@
   // 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);
+    LogPacketDiscarded(packet.priority.codec_level, stats);
     return return_val;
   }
 
@@ -109,9 +118,8 @@
   // packet.
   PacketList::iterator it = rit.base();
   if (it != buffer_.end() && packet.timestamp == it->timestamp) {
+    LogPacketDiscarded(packet.priority.codec_level, stats);
     it = buffer_.erase(it);
-    RTC_CHECK(stats);
-    stats->PacketsDiscarded(1);
   }
   buffer_.insert(it, std::move(packet));  // Insert the packet at that position.
 
@@ -218,25 +226,24 @@
     return kBufferEmpty;
   }
   // Assert that the packet sanity checks in InsertPacket method works.
-  RTC_DCHECK(!buffer_.front().empty());
+  const Packet& packet = buffer_.front();
+  RTC_DCHECK(!packet.empty());
+  LogPacketDiscarded(packet.priority.codec_level, stats);
   buffer_.pop_front();
-  RTC_CHECK(stats);
-  stats->PacketsDiscarded(1);
   return kOK;
 }
 
 void PacketBuffer::DiscardOldPackets(uint32_t timestamp_limit,
                                      uint32_t horizon_samples,
                                      StatisticsCalculator* stats) {
-  const size_t old_size = buffer_.size();
-  buffer_.remove_if([timestamp_limit, horizon_samples](const Packet& p) {
-    return timestamp_limit != p.timestamp &&
-           IsObsoleteTimestamp(p.timestamp, timestamp_limit, horizon_samples);
+  buffer_.remove_if([timestamp_limit, horizon_samples, stats](const Packet& p) {
+    if (timestamp_limit == p.timestamp ||
+        !IsObsoleteTimestamp(p.timestamp, timestamp_limit, horizon_samples)) {
+      return false;
+    }
+    LogPacketDiscarded(p.priority.codec_level, stats);
+    return true;
   });
-  if (old_size > buffer_.size()) {
-    RTC_CHECK(stats);
-    stats->PacketsDiscarded(old_size - buffer_.size());
-  }
 }
 
 void PacketBuffer::DiscardAllOldPackets(uint32_t timestamp_limit,
@@ -246,20 +253,13 @@
 
 void PacketBuffer::DiscardPacketsWithPayloadType(uint8_t payload_type,
                                                  StatisticsCalculator* stats) {
-  int packets_discarded = 0;
-  for (auto it = buffer_.begin(); it != buffer_.end(); /* */) {
-    const Packet& packet = *it;
-    if (packet.payload_type == payload_type) {
-      it = buffer_.erase(it);
-      ++packets_discarded;
-    } else {
-      ++it;
+  buffer_.remove_if([payload_type, stats](const Packet& p) {
+    if (p.payload_type != payload_type) {
+      return false;
     }
-  }
-  if (packets_discarded > 0) {
-    RTC_CHECK(stats);
-    stats->PacketsDiscarded(packets_discarded);
-  }
+    LogPacketDiscarded(p.priority.codec_level, stats);
+    return true;
+  });
 }
 
 size_t PacketBuffer::NumPacketsInBuffer() const {