xref: /aosp_15_r20/external/webrtc/modules/audio_coding/neteq/packet_arrival_history.cc (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 #include "modules/audio_coding/neteq/packet_arrival_history.h"
12 
13 #include <algorithm>
14 
15 #include "api/neteq/tick_timer.h"
16 #include "modules/include/module_common_types_public.h"
17 
18 namespace webrtc {
19 
PacketArrivalHistory(int window_size_ms)20 PacketArrivalHistory::PacketArrivalHistory(int window_size_ms)
21     : window_size_ms_(window_size_ms) {}
22 
Insert(uint32_t rtp_timestamp,int64_t arrival_time_ms)23 void PacketArrivalHistory::Insert(uint32_t rtp_timestamp,
24                                   int64_t arrival_time_ms) {
25   RTC_DCHECK(sample_rate_khz_ > 0);
26   int64_t unwrapped_rtp_timestamp = timestamp_unwrapper_.Unwrap(rtp_timestamp);
27   if (!newest_rtp_timestamp_ ||
28       unwrapped_rtp_timestamp > *newest_rtp_timestamp_) {
29     newest_rtp_timestamp_ = unwrapped_rtp_timestamp;
30   }
31   history_.emplace_back(unwrapped_rtp_timestamp / sample_rate_khz_,
32                         arrival_time_ms);
33   MaybeUpdateCachedArrivals(history_.back());
34   while (history_.front().rtp_timestamp_ms + window_size_ms_ <
35          unwrapped_rtp_timestamp / sample_rate_khz_) {
36     if (&history_.front() == min_packet_arrival_) {
37       min_packet_arrival_ = nullptr;
38     }
39     if (&history_.front() == max_packet_arrival_) {
40       max_packet_arrival_ = nullptr;
41     }
42     history_.pop_front();
43   }
44   if (!min_packet_arrival_ || !max_packet_arrival_) {
45     for (const PacketArrival& packet : history_) {
46       MaybeUpdateCachedArrivals(packet);
47     }
48   }
49 }
50 
MaybeUpdateCachedArrivals(const PacketArrival & packet_arrival)51 void PacketArrivalHistory::MaybeUpdateCachedArrivals(
52     const PacketArrival& packet_arrival) {
53   if (!min_packet_arrival_ || packet_arrival <= *min_packet_arrival_) {
54     min_packet_arrival_ = &packet_arrival;
55   }
56   if (!max_packet_arrival_ || packet_arrival >= *max_packet_arrival_) {
57     max_packet_arrival_ = &packet_arrival;
58   }
59 }
60 
Reset()61 void PacketArrivalHistory::Reset() {
62   history_.clear();
63   min_packet_arrival_ = nullptr;
64   max_packet_arrival_ = nullptr;
65   timestamp_unwrapper_ = TimestampUnwrapper();
66   newest_rtp_timestamp_ = absl::nullopt;
67 }
68 
GetDelayMs(uint32_t rtp_timestamp,int64_t time_ms) const69 int PacketArrivalHistory::GetDelayMs(uint32_t rtp_timestamp,
70                                      int64_t time_ms) const {
71   RTC_DCHECK(sample_rate_khz_ > 0);
72   int64_t unwrapped_rtp_timestamp_ms =
73       timestamp_unwrapper_.UnwrapWithoutUpdate(rtp_timestamp) /
74       sample_rate_khz_;
75   PacketArrival packet(unwrapped_rtp_timestamp_ms, time_ms);
76   return GetPacketArrivalDelayMs(packet);
77 }
78 
GetMaxDelayMs() const79 int PacketArrivalHistory::GetMaxDelayMs() const {
80   if (!max_packet_arrival_) {
81     return 0;
82   }
83   return GetPacketArrivalDelayMs(*max_packet_arrival_);
84 }
85 
IsNewestRtpTimestamp(uint32_t rtp_timestamp) const86 bool PacketArrivalHistory::IsNewestRtpTimestamp(uint32_t rtp_timestamp) const {
87   if (!newest_rtp_timestamp_) {
88     return false;
89   }
90   int64_t unwrapped_rtp_timestamp =
91       timestamp_unwrapper_.UnwrapWithoutUpdate(rtp_timestamp);
92   return unwrapped_rtp_timestamp == *newest_rtp_timestamp_;
93 }
94 
GetPacketArrivalDelayMs(const PacketArrival & packet_arrival) const95 int PacketArrivalHistory::GetPacketArrivalDelayMs(
96     const PacketArrival& packet_arrival) const {
97   if (!min_packet_arrival_) {
98     return 0;
99   }
100   return std::max(static_cast<int>(packet_arrival.arrival_time_ms -
101                                    min_packet_arrival_->arrival_time_ms -
102                                    (packet_arrival.rtp_timestamp_ms -
103                                     min_packet_arrival_->rtp_timestamp_ms)),
104                   0);
105 }
106 
107 }  // namespace webrtc
108