packet_arrival_history.h (4122B)
1 /* 2 * Copyright (c) 2022 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 #ifndef MODULES_AUDIO_CODING_NETEQ_PACKET_ARRIVAL_HISTORY_H_ 12 #define MODULES_AUDIO_CODING_NETEQ_PACKET_ARRIVAL_HISTORY_H_ 13 14 #include <cstddef> 15 #include <cstdint> 16 #include <deque> 17 #include <map> 18 19 #include "api/neteq/tick_timer.h" 20 #include "rtc_base/numerics/sequence_number_unwrapper.h" 21 22 namespace webrtc { 23 24 // Stores timing information about previously received packets. 25 // The history has a fixed window size beyond which old data is automatically 26 // pruned. 27 class PacketArrivalHistory { 28 public: 29 explicit PacketArrivalHistory(const TickTimer* tick_timer, 30 int window_size_ms); 31 virtual ~PacketArrivalHistory() = default; 32 33 // Insert packet with `rtp_timestamp` into the history. Returns true if the 34 // packet was inserted, false if the timestamp is too old or if the timestamp 35 // already exists. 36 bool Insert(uint32_t rtp_timestamp, int packet_length_samples); 37 38 // The delay for `rtp_timestamp` at time `now` is calculated as 39 // `(now - p.arrival_timestamp) - (rtp_timestamp - p.rtp_timestamp)` where `p` 40 // is chosen as the packet arrival in the history that maximizes the delay. 41 virtual int GetDelayMs(uint32_t rtp_timestamp) const; 42 43 // Get the maximum packet arrival delay observed in the history, excluding 44 // reordered packets. 45 virtual int GetMaxDelayMs() const; 46 47 bool IsNewestRtpTimestamp(uint32_t rtp_timestamp) const; 48 49 void Reset(); 50 51 void set_sample_rate(int sample_rate) { 52 sample_rate_khz_ = sample_rate / 1000; 53 } 54 55 size_t size() const { return history_.size(); } 56 57 private: 58 struct PacketArrival { 59 PacketArrival(int64_t rtp_timestamp, 60 int64_t arrival_timestamp, 61 int length_samples) 62 : rtp_timestamp(rtp_timestamp), 63 arrival_timestamp(arrival_timestamp), 64 length_samples(length_samples) {} 65 PacketArrival() = default; 66 int64_t rtp_timestamp; 67 int64_t arrival_timestamp; 68 int length_samples; 69 bool operator==(const PacketArrival& other) const { 70 return rtp_timestamp == other.rtp_timestamp && 71 arrival_timestamp == other.arrival_timestamp && 72 length_samples == other.length_samples; 73 } 74 bool operator!=(const PacketArrival& other) const { 75 return !(*this == other); 76 } 77 bool operator<=(const PacketArrival& other) const { 78 return arrival_timestamp - rtp_timestamp <= 79 other.arrival_timestamp - other.rtp_timestamp; 80 } 81 bool operator>=(const PacketArrival& other) const { 82 return arrival_timestamp - rtp_timestamp >= 83 other.arrival_timestamp - other.rtp_timestamp; 84 } 85 bool contains(const PacketArrival& other) const { 86 return rtp_timestamp <= other.rtp_timestamp && 87 rtp_timestamp + length_samples >= 88 other.rtp_timestamp + other.length_samples; 89 } 90 }; 91 int GetPacketArrivalDelayMs(const PacketArrival& packet_arrival) const; 92 // Checks if the packet is older than the window size. 93 bool IsObsolete(const PacketArrival& packet_arrival) const; 94 // Check if the packet exists or fully overlaps with a packet in the history. 95 bool Contains(const PacketArrival& packet_arrival) const; 96 const TickTimer* tick_timer_; 97 const int window_size_ms_; 98 int sample_rate_khz_ = 0; 99 RtpTimestampUnwrapper timestamp_unwrapper_; 100 // Packet history ordered by rtp timestamp. 101 std::map<int64_t, PacketArrival> history_; 102 // Tracks min/max packet arrivals in `history_` in ascending/descending order. 103 // Reordered packets are excluded. 104 std::deque<PacketArrival> min_packet_arrivals_; 105 std::deque<PacketArrival> max_packet_arrivals_; 106 }; 107 108 } // namespace webrtc 109 110 #endif // MODULES_AUDIO_CODING_NETEQ_PACKET_ARRIVAL_HISTORY_H_