turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 +0000 | [diff] [blame] | 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "modules/audio_coding/neteq/nack_tracker.h" |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 +0000 | [diff] [blame] | 12 | |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 13 | #include <assert.h> |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 14 | |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 15 | #include <cstdint> |
| 16 | #include <utility> |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 +0000 | [diff] [blame] | 17 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 18 | #include "rtc_base/checks.h" |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 +0000 | [diff] [blame] | 19 | |
| 20 | namespace webrtc { |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 +0000 | [diff] [blame] | 21 | namespace { |
| 22 | |
| 23 | const int kDefaultSampleRateKhz = 48; |
| 24 | const int kDefaultPacketSizeMs = 20; |
| 25 | |
| 26 | } // namespace |
| 27 | |
henrik.lundin | 9195186 | 2016-06-08 06:43:41 -0700 | [diff] [blame] | 28 | NackTracker::NackTracker(int nack_threshold_packets) |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 +0000 | [diff] [blame] | 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 | |
henrik.lundin | 9195186 | 2016-06-08 06:43:41 -0700 | [diff] [blame] | 40 | NackTracker::~NackTracker() = default; |
Karl Wiberg | 2519c45 | 2015-04-07 16:12:57 +0200 | [diff] [blame] | 41 | |
henrik.lundin | 9195186 | 2016-06-08 06:43:41 -0700 | [diff] [blame] | 42 | NackTracker* NackTracker::Create(int nack_threshold_packets) { |
| 43 | return new NackTracker(nack_threshold_packets); |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 +0000 | [diff] [blame] | 44 | } |
| 45 | |
henrik.lundin | 9195186 | 2016-06-08 06:43:41 -0700 | [diff] [blame] | 46 | void NackTracker::UpdateSampleRate(int sample_rate_hz) { |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 +0000 | [diff] [blame] | 47 | assert(sample_rate_hz > 0); |
| 48 | sample_rate_khz_ = sample_rate_hz / 1000; |
| 49 | } |
| 50 | |
henrik.lundin | 9195186 | 2016-06-08 06:43:41 -0700 | [diff] [blame] | 51 | void NackTracker::UpdateLastReceivedPacket(uint16_t sequence_number, |
| 52 | uint32_t timestamp) { |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 +0000 | [diff] [blame] | 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 | |
henrik.lundin | 9195186 | 2016-06-08 06:43:41 -0700 | [diff] [blame] | 87 | void NackTracker::UpdateSamplesPerPacket( |
| 88 | uint16_t sequence_number_current_received_rtp, |
| 89 | uint32_t timestamp_current_received_rtp) { |
henrik.lundin | 48ed930 | 2015-10-29 05:36:24 -0700 | [diff] [blame] | 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_; |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 +0000 | [diff] [blame] | 94 | |
| 95 | samples_per_packet_ = timestamp_increase / sequence_num_increase; |
| 96 | } |
| 97 | |
henrik.lundin | 9195186 | 2016-06-08 06:43:41 -0700 | [diff] [blame] | 98 | void NackTracker::UpdateList(uint16_t sequence_number_current_received_rtp) { |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 +0000 | [diff] [blame] | 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 | |
henrik.lundin | 9195186 | 2016-06-08 06:43:41 -0700 | [diff] [blame] | 107 | void NackTracker::ChangeFromLateToMissing( |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 +0000 | [diff] [blame] | 108 | uint16_t sequence_number_current_received_rtp) { |
henrik.lundin | 48ed930 | 2015-10-29 05:36:24 -0700 | [diff] [blame] | 109 | NackList::const_iterator lower_bound = |
| 110 | nack_list_.lower_bound(static_cast<uint16_t>( |
| 111 | sequence_number_current_received_rtp - nack_threshold_packets_)); |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 +0000 | [diff] [blame] | 112 | |
| 113 | for (NackList::iterator it = nack_list_.begin(); it != lower_bound; ++it) |
| 114 | it->second.is_missing = true; |
| 115 | } |
| 116 | |
henrik.lundin | 9195186 | 2016-06-08 06:43:41 -0700 | [diff] [blame] | 117 | uint32_t NackTracker::EstimateTimestamp(uint16_t sequence_num) { |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 +0000 | [diff] [blame] | 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 | |
henrik.lundin | 9195186 | 2016-06-08 06:43:41 -0700 | [diff] [blame] | 122 | void NackTracker::AddToList(uint16_t sequence_number_current_received_rtp) { |
henrik.lundin | 48ed930 | 2015-10-29 05:36:24 -0700 | [diff] [blame] | 123 | assert(!any_rtp_decoded_ || |
| 124 | IsNewerSequenceNumber(sequence_number_current_received_rtp, |
| 125 | sequence_num_last_decoded_rtp_)); |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 +0000 | [diff] [blame] | 126 | |
| 127 | // Packets with sequence numbers older than |upper_bound_missing| are |
| 128 | // considered missing, and the rest are considered late. |
henrik.lundin | 48ed930 | 2015-10-29 05:36:24 -0700 | [diff] [blame] | 129 | uint16_t upper_bound_missing = |
| 130 | sequence_number_current_received_rtp - nack_threshold_packets_; |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 +0000 | [diff] [blame] | 131 | |
| 132 | for (uint16_t n = sequence_num_last_received_rtp_ + 1; |
henrik.lundin | 48ed930 | 2015-10-29 05:36:24 -0700 | [diff] [blame] | 133 | IsNewerSequenceNumber(sequence_number_current_received_rtp, n); ++n) { |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 +0000 | [diff] [blame] | 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 | |
henrik.lundin | 9195186 | 2016-06-08 06:43:41 -0700 | [diff] [blame] | 141 | void NackTracker::UpdateEstimatedPlayoutTimeBy10ms() { |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 +0000 | [diff] [blame] | 142 | while (!nack_list_.empty() && |
henrik.lundin | 48ed930 | 2015-10-29 05:36:24 -0700 | [diff] [blame] | 143 | nack_list_.begin()->second.time_to_play_ms <= 10) |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 +0000 | [diff] [blame] | 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 | |
henrik.lundin | 9195186 | 2016-06-08 06:43:41 -0700 | [diff] [blame] | 150 | void NackTracker::UpdateLastDecodedPacket(uint16_t sequence_number, |
| 151 | uint32_t timestamp) { |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 +0000 | [diff] [blame] | 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. |
henrik.lundin | 48ed930 | 2015-10-29 05:36:24 -0700 | [diff] [blame] | 159 | nack_list_.erase(nack_list_.begin(), |
| 160 | nack_list_.upper_bound(sequence_num_last_decoded_rtp_)); |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 +0000 | [diff] [blame] | 161 | |
| 162 | // Update estimated time-to-play. |
| 163 | for (NackList::iterator it = nack_list_.begin(); it != nack_list_.end(); |
henrik.lundin | 48ed930 | 2015-10-29 05:36:24 -0700 | [diff] [blame] | 164 | ++it) |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 +0000 | [diff] [blame] | 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 | |
henrik.lundin | 9195186 | 2016-06-08 06:43:41 -0700 | [diff] [blame] | 180 | NackTracker::NackList NackTracker::GetNackList() const { |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 +0000 | [diff] [blame] | 181 | return nack_list_; |
| 182 | } |
| 183 | |
henrik.lundin | 9195186 | 2016-06-08 06:43:41 -0700 | [diff] [blame] | 184 | void NackTracker::Reset() { |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 +0000 | [diff] [blame] | 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 | |
henrik.lundin | 9195186 | 2016-06-08 06:43:41 -0700 | [diff] [blame] | 197 | void NackTracker::SetMaxNackListSize(size_t max_nack_list_size) { |
kwiberg | af476c7 | 2016-11-28 15:21:39 -0800 | [diff] [blame] | 198 | RTC_CHECK_GT(max_nack_list_size, 0); |
henrik.lundin | 48ed930 | 2015-10-29 05:36:24 -0700 | [diff] [blame] | 199 | // Ugly hack to get around the problem of passing static consts by reference. |
henrik.lundin | 9195186 | 2016-06-08 06:43:41 -0700 | [diff] [blame] | 200 | const size_t kNackListSizeLimitLocal = NackTracker::kNackListSizeLimit; |
henrik.lundin | 48ed930 | 2015-10-29 05:36:24 -0700 | [diff] [blame] | 201 | RTC_CHECK_LE(max_nack_list_size, kNackListSizeLimitLocal); |
| 202 | |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 +0000 | [diff] [blame] | 203 | max_nack_list_size_ = max_nack_list_size; |
| 204 | LimitNackListSize(); |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 +0000 | [diff] [blame] | 205 | } |
| 206 | |
henrik.lundin | 9195186 | 2016-06-08 06:43:41 -0700 | [diff] [blame] | 207 | void NackTracker::LimitNackListSize() { |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 +0000 | [diff] [blame] | 208 | uint16_t limit = sequence_num_last_received_rtp_ - |
henrik.lundin | 48ed930 | 2015-10-29 05:36:24 -0700 | [diff] [blame] | 209 | static_cast<uint16_t>(max_nack_list_size_) - 1; |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 +0000 | [diff] [blame] | 210 | nack_list_.erase(nack_list_.begin(), nack_list_.upper_bound(limit)); |
| 211 | } |
| 212 | |
henrik.lundin | 9195186 | 2016-06-08 06:43:41 -0700 | [diff] [blame] | 213 | int64_t NackTracker::TimeToPlay(uint32_t timestamp) const { |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 +0000 | [diff] [blame] | 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. |
henrik.lundin | 9195186 | 2016-06-08 06:43:41 -0700 | [diff] [blame] | 219 | std::vector<uint16_t> NackTracker::GetNackList( |
| 220 | int64_t round_trip_time_ms) const { |
henrik.lundin | 48ed930 | 2015-10-29 05:36:24 -0700 | [diff] [blame] | 221 | RTC_DCHECK_GE(round_trip_time_ms, 0); |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 +0000 | [diff] [blame] | 222 | std::vector<uint16_t> sequence_numbers; |
| 223 | for (NackList::const_iterator it = nack_list_.begin(); it != nack_list_.end(); |
henrik.lundin | 48ed930 | 2015-10-29 05:36:24 -0700 | [diff] [blame] | 224 | ++it) { |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 +0000 | [diff] [blame] | 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 | |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 +0000 | [diff] [blame] | 232 | } // namespace webrtc |