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