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