1 /* 2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. 3 * 4 * Use of this source code is governed by a BSD-style license 5 * that can be found in the LICENSE file in the root of the source 6 * tree. An additional intellectual property rights grant can be found 7 * in the file PATENTS. All contributing project authors may 8 * be found in the AUTHORS file in the root of the source tree. 9 */ 10 11 #include "webrtc/modules/rtp_rtcp/source/packet_loss_stats.h" 12 13 #include <vector> 14 15 #include "webrtc/base/checks.h" 16 17 // After this many packets are added, adding additional packets will cause the 18 // oldest packets to be pruned from the buffer. 19 static const int kBufferSize = 100; 20 21 namespace webrtc { 22 23 PacketLossStats::PacketLossStats() 24 : single_loss_historic_count_(0), 25 multiple_loss_historic_event_count_(0), 26 multiple_loss_historic_packet_count_(0) { 27 } 28 29 void PacketLossStats::AddLostPacket(uint16_t sequence_number) { 30 // Detect sequence number wrap around. 31 if (!lost_packets_buffer_.empty() && 32 static_cast<int>(*(lost_packets_buffer_.rbegin())) - sequence_number 33 > 0x8000) { 34 // The buffer contains large numbers and this is a small number. 35 lost_packets_wrapped_buffer_.insert(sequence_number); 36 } else { 37 lost_packets_buffer_.insert(sequence_number); 38 } 39 if (lost_packets_wrapped_buffer_.size() + lost_packets_buffer_.size() 40 > kBufferSize || (!lost_packets_wrapped_buffer_.empty() && 41 *(lost_packets_wrapped_buffer_.rbegin()) > 0x4000)) { 42 PruneBuffer(); 43 } 44 } 45 46 int PacketLossStats::GetSingleLossCount() const { 47 int single_loss_count, unused1, unused2; 48 ComputeLossCounts(&single_loss_count, &unused1, &unused2); 49 return single_loss_count; 50 } 51 52 int PacketLossStats::GetMultipleLossEventCount() const { 53 int event_count, unused1, unused2; 54 ComputeLossCounts(&unused1, &event_count, &unused2); 55 return event_count; 56 } 57 58 int PacketLossStats::GetMultipleLossPacketCount() const { 59 int packet_count, unused1, unused2; 60 ComputeLossCounts(&unused1, &unused2, &packet_count); 61 return packet_count; 62 } 63 64 void PacketLossStats::ComputeLossCounts( 65 int* out_single_loss_count, 66 int* out_multiple_loss_event_count, 67 int* out_multiple_loss_packet_count) const { 68 *out_single_loss_count = single_loss_historic_count_; 69 *out_multiple_loss_event_count = multiple_loss_historic_event_count_; 70 *out_multiple_loss_packet_count = multiple_loss_historic_packet_count_; 71 if (lost_packets_buffer_.empty()) { 72 RTC_DCHECK(lost_packets_wrapped_buffer_.empty()); 73 return; 74 } 75 uint16_t last_num = 0; 76 int sequential_count = 0; 77 std::vector<const std::set<uint16_t>*> buffers; 78 buffers.push_back(&lost_packets_buffer_); 79 buffers.push_back(&lost_packets_wrapped_buffer_); 80 for (auto buffer : buffers) { 81 for (auto it = buffer->begin(); it != buffer->end(); ++it) { 82 uint16_t current_num = *it; 83 if (sequential_count > 0 && current_num != ((last_num + 1) & 0xFFFF)) { 84 if (sequential_count == 1) { 85 (*out_single_loss_count)++; 86 } else { 87 (*out_multiple_loss_event_count)++; 88 *out_multiple_loss_packet_count += sequential_count; 89 } 90 sequential_count = 0; 91 } 92 sequential_count++; 93 last_num = current_num; 94 } 95 } 96 if (sequential_count == 1) { 97 (*out_single_loss_count)++; 98 } else if (sequential_count > 1) { 99 (*out_multiple_loss_event_count)++; 100 *out_multiple_loss_packet_count += sequential_count; 101 } 102 } 103 104 void PacketLossStats::PruneBuffer() { 105 // Remove the oldest lost packet and any contiguous packets and move them 106 // into the historic counts. 107 auto it = lost_packets_buffer_.begin(); 108 uint16_t last_removed = 0; 109 int remove_count = 0; 110 // Count adjacent packets and continue counting if it is wrap around by 111 // swapping in the wrapped buffer and letting our value wrap as well. 112 while (remove_count == 0 || (!lost_packets_buffer_.empty() && 113 *it == ((last_removed + 1) & 0xFFFF))) { 114 last_removed = *it; 115 remove_count++; 116 auto to_erase = it++; 117 lost_packets_buffer_.erase(to_erase); 118 if (lost_packets_buffer_.empty()) { 119 lost_packets_buffer_.swap(lost_packets_wrapped_buffer_); 120 it = lost_packets_buffer_.begin(); 121 } 122 } 123 if (remove_count > 1) { 124 multiple_loss_historic_event_count_++; 125 multiple_loss_historic_packet_count_ += remove_count; 126 } else { 127 single_loss_historic_count_++; 128 } 129 // Continue pruning if the wrapped buffer is beyond a threshold and there are 130 // things left in the pre-wrapped buffer. 131 if (!lost_packets_wrapped_buffer_.empty() && 132 *(lost_packets_wrapped_buffer_.rbegin()) > 0x4000) { 133 PruneBuffer(); 134 } 135 } 136 137 } // namespace webrtc 138