NetEq: Rectify the implementation of PacketBuffer::DiscardOldPackets

The implementation of this method did not follow the description in
the method comment. It was supposed to delete all packets in a range
[A, B], but if at least one packet in the buffer had a timestamp lower
than A, then no packets at all were discarded. This is now fixed.

BUG=webrtc:7937

Review-Url: https://codereview.webrtc.org/2969123003
Cr-Commit-Position: refs/heads/master@{#18903}
diff --git a/webrtc/modules/audio_coding/neteq/packet_buffer.cc b/webrtc/modules/audio_coding/neteq/packet_buffer.cc
index cea6b3d..4c8c462 100644
--- a/webrtc/modules/audio_coding/neteq/packet_buffer.cc
+++ b/webrtc/modules/audio_coding/neteq/packet_buffer.cc
@@ -221,15 +221,13 @@
 void PacketBuffer::DiscardOldPackets(uint32_t timestamp_limit,
                                      uint32_t horizon_samples,
                                      StatisticsCalculator* stats) {
-  // TODO(minyue): the following implementation is wrong. It won't discard
-  // old packets if the buffer_.front() is newer than timestamp_limit -
-  // horizon_samples. https://bugs.chromium.org/p/webrtc/issues/detail?id=7937
-  while (!Empty() && timestamp_limit != buffer_.front().timestamp &&
-         IsObsoleteTimestamp(buffer_.front().timestamp, timestamp_limit,
-                             horizon_samples)) {
-    if (DiscardNextPacket(stats) != kOK) {
-      assert(false);  // Must be ok by design.
-    }
+  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);
+  });
+  if (old_size > buffer_.size()) {
+    stats->PacketsDiscarded(old_size - buffer_.size());
   }
 }