• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright (c) 2013 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/nack_tracker.h"
12 
13 #include <assert.h>
14 
15 #include <cstdint>
16 #include <utility>
17 
18 #include "rtc_base/checks.h"
19 
20 namespace webrtc {
21 namespace {
22 
23 const int kDefaultSampleRateKhz = 48;
24 const int kDefaultPacketSizeMs = 20;
25 
26 }  // namespace
27 
NackTracker(int nack_threshold_packets)28 NackTracker::NackTracker(int nack_threshold_packets)
29     : nack_threshold_packets_(nack_threshold_packets),
30       sequence_num_last_received_rtp_(0),
31       timestamp_last_received_rtp_(0),
32       any_rtp_received_(false),
33       sequence_num_last_decoded_rtp_(0),
34       timestamp_last_decoded_rtp_(0),
35       any_rtp_decoded_(false),
36       sample_rate_khz_(kDefaultSampleRateKhz),
37       samples_per_packet_(sample_rate_khz_ * kDefaultPacketSizeMs),
38       max_nack_list_size_(kNackListSizeLimit) {}
39 
40 NackTracker::~NackTracker() = default;
41 
Create(int nack_threshold_packets)42 NackTracker* NackTracker::Create(int nack_threshold_packets) {
43   return new NackTracker(nack_threshold_packets);
44 }
45 
UpdateSampleRate(int sample_rate_hz)46 void NackTracker::UpdateSampleRate(int sample_rate_hz) {
47   assert(sample_rate_hz > 0);
48   sample_rate_khz_ = sample_rate_hz / 1000;
49 }
50 
UpdateLastReceivedPacket(uint16_t sequence_number,uint32_t timestamp)51 void NackTracker::UpdateLastReceivedPacket(uint16_t sequence_number,
52                                            uint32_t timestamp) {
53   // Just record the value of sequence number and timestamp if this is the
54   // first packet.
55   if (!any_rtp_received_) {
56     sequence_num_last_received_rtp_ = sequence_number;
57     timestamp_last_received_rtp_ = timestamp;
58     any_rtp_received_ = true;
59     // If no packet is decoded, to have a reasonable estimate of time-to-play
60     // use the given values.
61     if (!any_rtp_decoded_) {
62       sequence_num_last_decoded_rtp_ = sequence_number;
63       timestamp_last_decoded_rtp_ = timestamp;
64     }
65     return;
66   }
67 
68   if (sequence_number == sequence_num_last_received_rtp_)
69     return;
70 
71   // Received RTP should not be in the list.
72   nack_list_.erase(sequence_number);
73 
74   // If this is an old sequence number, no more action is required, return.
75   if (IsNewerSequenceNumber(sequence_num_last_received_rtp_, sequence_number))
76     return;
77 
78   UpdateSamplesPerPacket(sequence_number, timestamp);
79 
80   UpdateList(sequence_number);
81 
82   sequence_num_last_received_rtp_ = sequence_number;
83   timestamp_last_received_rtp_ = timestamp;
84   LimitNackListSize();
85 }
86 
UpdateSamplesPerPacket(uint16_t sequence_number_current_received_rtp,uint32_t timestamp_current_received_rtp)87 void NackTracker::UpdateSamplesPerPacket(
88     uint16_t sequence_number_current_received_rtp,
89     uint32_t timestamp_current_received_rtp) {
90   uint32_t timestamp_increase =
91       timestamp_current_received_rtp - timestamp_last_received_rtp_;
92   uint16_t sequence_num_increase =
93       sequence_number_current_received_rtp - sequence_num_last_received_rtp_;
94 
95   samples_per_packet_ = timestamp_increase / sequence_num_increase;
96 }
97 
UpdateList(uint16_t sequence_number_current_received_rtp)98 void NackTracker::UpdateList(uint16_t sequence_number_current_received_rtp) {
99   // Some of the packets which were considered late, now are considered missing.
100   ChangeFromLateToMissing(sequence_number_current_received_rtp);
101 
102   if (IsNewerSequenceNumber(sequence_number_current_received_rtp,
103                             sequence_num_last_received_rtp_ + 1))
104     AddToList(sequence_number_current_received_rtp);
105 }
106 
ChangeFromLateToMissing(uint16_t sequence_number_current_received_rtp)107 void NackTracker::ChangeFromLateToMissing(
108     uint16_t sequence_number_current_received_rtp) {
109   NackList::const_iterator lower_bound =
110       nack_list_.lower_bound(static_cast<uint16_t>(
111           sequence_number_current_received_rtp - nack_threshold_packets_));
112 
113   for (NackList::iterator it = nack_list_.begin(); it != lower_bound; ++it)
114     it->second.is_missing = true;
115 }
116 
EstimateTimestamp(uint16_t sequence_num)117 uint32_t NackTracker::EstimateTimestamp(uint16_t sequence_num) {
118   uint16_t sequence_num_diff = sequence_num - sequence_num_last_received_rtp_;
119   return sequence_num_diff * samples_per_packet_ + timestamp_last_received_rtp_;
120 }
121 
AddToList(uint16_t sequence_number_current_received_rtp)122 void NackTracker::AddToList(uint16_t sequence_number_current_received_rtp) {
123   assert(!any_rtp_decoded_ ||
124          IsNewerSequenceNumber(sequence_number_current_received_rtp,
125                                sequence_num_last_decoded_rtp_));
126 
127   // Packets with sequence numbers older than |upper_bound_missing| are
128   // considered missing, and the rest are considered late.
129   uint16_t upper_bound_missing =
130       sequence_number_current_received_rtp - nack_threshold_packets_;
131 
132   for (uint16_t n = sequence_num_last_received_rtp_ + 1;
133        IsNewerSequenceNumber(sequence_number_current_received_rtp, n); ++n) {
134     bool is_missing = IsNewerSequenceNumber(upper_bound_missing, n);
135     uint32_t timestamp = EstimateTimestamp(n);
136     NackElement nack_element(TimeToPlay(timestamp), timestamp, is_missing);
137     nack_list_.insert(nack_list_.end(), std::make_pair(n, nack_element));
138   }
139 }
140 
UpdateEstimatedPlayoutTimeBy10ms()141 void NackTracker::UpdateEstimatedPlayoutTimeBy10ms() {
142   while (!nack_list_.empty() &&
143          nack_list_.begin()->second.time_to_play_ms <= 10)
144     nack_list_.erase(nack_list_.begin());
145 
146   for (NackList::iterator it = nack_list_.begin(); it != nack_list_.end(); ++it)
147     it->second.time_to_play_ms -= 10;
148 }
149 
UpdateLastDecodedPacket(uint16_t sequence_number,uint32_t timestamp)150 void NackTracker::UpdateLastDecodedPacket(uint16_t sequence_number,
151                                           uint32_t timestamp) {
152   if (IsNewerSequenceNumber(sequence_number, sequence_num_last_decoded_rtp_) ||
153       !any_rtp_decoded_) {
154     sequence_num_last_decoded_rtp_ = sequence_number;
155     timestamp_last_decoded_rtp_ = timestamp;
156     // Packets in the list with sequence numbers less than the
157     // sequence number of the decoded RTP should be removed from the lists.
158     // They will be discarded by the jitter buffer if they arrive.
159     nack_list_.erase(nack_list_.begin(),
160                      nack_list_.upper_bound(sequence_num_last_decoded_rtp_));
161 
162     // Update estimated time-to-play.
163     for (NackList::iterator it = nack_list_.begin(); it != nack_list_.end();
164          ++it)
165       it->second.time_to_play_ms = TimeToPlay(it->second.estimated_timestamp);
166   } else {
167     assert(sequence_number == sequence_num_last_decoded_rtp_);
168 
169     // Same sequence number as before. 10 ms is elapsed, update estimations for
170     // time-to-play.
171     UpdateEstimatedPlayoutTimeBy10ms();
172 
173     // Update timestamp for better estimate of time-to-play, for packets which
174     // are added to NACK list later on.
175     timestamp_last_decoded_rtp_ += sample_rate_khz_ * 10;
176   }
177   any_rtp_decoded_ = true;
178 }
179 
GetNackList() const180 NackTracker::NackList NackTracker::GetNackList() const {
181   return nack_list_;
182 }
183 
Reset()184 void NackTracker::Reset() {
185   nack_list_.clear();
186 
187   sequence_num_last_received_rtp_ = 0;
188   timestamp_last_received_rtp_ = 0;
189   any_rtp_received_ = false;
190   sequence_num_last_decoded_rtp_ = 0;
191   timestamp_last_decoded_rtp_ = 0;
192   any_rtp_decoded_ = false;
193   sample_rate_khz_ = kDefaultSampleRateKhz;
194   samples_per_packet_ = sample_rate_khz_ * kDefaultPacketSizeMs;
195 }
196 
SetMaxNackListSize(size_t max_nack_list_size)197 void NackTracker::SetMaxNackListSize(size_t max_nack_list_size) {
198   RTC_CHECK_GT(max_nack_list_size, 0);
199   // Ugly hack to get around the problem of passing static consts by reference.
200   const size_t kNackListSizeLimitLocal = NackTracker::kNackListSizeLimit;
201   RTC_CHECK_LE(max_nack_list_size, kNackListSizeLimitLocal);
202 
203   max_nack_list_size_ = max_nack_list_size;
204   LimitNackListSize();
205 }
206 
LimitNackListSize()207 void NackTracker::LimitNackListSize() {
208   uint16_t limit = sequence_num_last_received_rtp_ -
209                    static_cast<uint16_t>(max_nack_list_size_) - 1;
210   nack_list_.erase(nack_list_.begin(), nack_list_.upper_bound(limit));
211 }
212 
TimeToPlay(uint32_t timestamp) const213 int64_t NackTracker::TimeToPlay(uint32_t timestamp) const {
214   uint32_t timestamp_increase = timestamp - timestamp_last_decoded_rtp_;
215   return timestamp_increase / sample_rate_khz_;
216 }
217 
218 // We don't erase elements with time-to-play shorter than round-trip-time.
GetNackList(int64_t round_trip_time_ms) const219 std::vector<uint16_t> NackTracker::GetNackList(
220     int64_t round_trip_time_ms) const {
221   RTC_DCHECK_GE(round_trip_time_ms, 0);
222   std::vector<uint16_t> sequence_numbers;
223   for (NackList::const_iterator it = nack_list_.begin(); it != nack_list_.end();
224        ++it) {
225     if (it->second.is_missing &&
226         it->second.time_to_play_ms > round_trip_time_ms)
227       sequence_numbers.push_back(it->first);
228   }
229   return sequence_numbers;
230 }
231 
232 }  // namespace webrtc
233