xref: /aosp_15_r20/external/webrtc/modules/audio_coding/neteq/packet_arrival_history.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
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 <cstdint>
15 #include <deque>
16 
17 #include "absl/types/optional.h"
18 #include "api/neteq/tick_timer.h"
19 #include "modules/include/module_common_types_public.h"
20 
21 namespace webrtc {
22 
23 // Stores timing information about previously received packets.
24 // The history has a fixed window size beyond which old data is automatically
25 // pruned.
26 class PacketArrivalHistory {
27  public:
28   explicit PacketArrivalHistory(int window_size_ms);
29 
30   // Insert packet with `rtp_timestamp` and `arrival_time_ms` into the history.
31   void Insert(uint32_t rtp_timestamp, int64_t arrival_time_ms);
32 
33   // The delay for `rtp_timestamp` at `time_ms` is calculated as
34   // `(time_ms - p.arrival_time_ms) - (rtp_timestamp - p.rtp_timestamp)`
35   // where `p` is chosen as the packet arrival in the history that maximizes the
36   // delay.
37   int GetDelayMs(uint32_t rtp_timestamp, int64_t time_ms) const;
38 
39   // Get the maximum packet arrival delay observed in the history.
40   int GetMaxDelayMs() const;
41 
42   bool IsNewestRtpTimestamp(uint32_t rtp_timestamp) const;
43 
44   void Reset();
45 
set_sample_rate(int sample_rate)46   void set_sample_rate(int sample_rate) {
47     sample_rate_khz_ = sample_rate / 1000;
48   }
49 
size()50   size_t size() const { return history_.size(); }
51 
52  private:
53   struct PacketArrival {
PacketArrivalPacketArrival54     PacketArrival(int64_t rtp_timestamp_ms, int64_t arrival_time_ms)
55         : rtp_timestamp_ms(rtp_timestamp_ms),
56           arrival_time_ms(arrival_time_ms) {}
57     int64_t rtp_timestamp_ms;
58     int64_t arrival_time_ms;
59     bool operator<=(const PacketArrival& other) const {
60       return arrival_time_ms - rtp_timestamp_ms <=
61              other.arrival_time_ms - other.rtp_timestamp_ms;
62     }
63     bool operator>=(const PacketArrival& other) const {
64       return arrival_time_ms - rtp_timestamp_ms >=
65              other.arrival_time_ms - other.rtp_timestamp_ms;
66     }
67   };
68   std::deque<PacketArrival> history_;
69   int GetPacketArrivalDelayMs(const PacketArrival& packet_arrival) const;
70   // Updates `min_packet_arrival_` and `max_packet_arrival_`.
71   void MaybeUpdateCachedArrivals(const PacketArrival& packet);
72   const PacketArrival* min_packet_arrival_ = nullptr;
73   const PacketArrival* max_packet_arrival_ = nullptr;
74   const int window_size_ms_;
75   TimestampUnwrapper timestamp_unwrapper_;
76   absl::optional<int64_t> newest_rtp_timestamp_;
77   int sample_rate_khz_ = 0;
78 };
79 
80 }  // namespace webrtc
81 
82 #endif  // MODULES_AUDIO_CODING_NETEQ_PACKET_ARRIVAL_HISTORY_H_
83