Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(555)

Side by Side Diff: webrtc/modules/audio_coding/neteq/nack.cc

Issue 1410073006: ACM: Move NACK functionality inside NetEq (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Add function descriptions Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved. 2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 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 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 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
11 #include "webrtc/modules/audio_coding/main/acm2/nack.h" 11 #include "webrtc/modules/audio_coding/neteq/nack.h"
12 12
13 #include <assert.h> // For assert. 13 #include <assert.h> // For assert.
14 14
15 #include <algorithm> // For std::max. 15 #include <algorithm> // For std::max.
16 16
17 #include "webrtc/modules/interface/module_common_types.h" 17 #include "webrtc/modules/interface/module_common_types.h"
18 #include "webrtc/system_wrappers/interface/logging.h" 18 #include "webrtc/system_wrappers/interface/logging.h"
19 19
20 namespace webrtc { 20 namespace webrtc {
21
22 namespace acm2 {
23
24 namespace { 21 namespace {
25 22
26 const int kDefaultSampleRateKhz = 48; 23 const int kDefaultSampleRateKhz = 48;
27 const int kDefaultPacketSizeMs = 20; 24 const int kDefaultPacketSizeMs = 20;
28 25
29 } // namespace 26 } // namespace
30 27
31 Nack::Nack(int nack_threshold_packets) 28 Nack::Nack(int nack_threshold_packets)
32 : nack_threshold_packets_(nack_threshold_packets), 29 : nack_threshold_packets_(nack_threshold_packets),
33 sequence_num_last_received_rtp_(0), 30 sequence_num_last_received_rtp_(0),
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
82 79
83 UpdateList(sequence_number); 80 UpdateList(sequence_number);
84 81
85 sequence_num_last_received_rtp_ = sequence_number; 82 sequence_num_last_received_rtp_ = sequence_number;
86 timestamp_last_received_rtp_ = timestamp; 83 timestamp_last_received_rtp_ = timestamp;
87 LimitNackListSize(); 84 LimitNackListSize();
88 } 85 }
89 86
90 void Nack::UpdateSamplesPerPacket(uint16_t sequence_number_current_received_rtp, 87 void Nack::UpdateSamplesPerPacket(uint16_t sequence_number_current_received_rtp,
91 uint32_t timestamp_current_received_rtp) { 88 uint32_t timestamp_current_received_rtp) {
92 uint32_t timestamp_increase = timestamp_current_received_rtp - 89 uint32_t timestamp_increase =
93 timestamp_last_received_rtp_; 90 timestamp_current_received_rtp - timestamp_last_received_rtp_;
94 uint16_t sequence_num_increase = sequence_number_current_received_rtp - 91 uint16_t sequence_num_increase =
95 sequence_num_last_received_rtp_; 92 sequence_number_current_received_rtp - sequence_num_last_received_rtp_;
96 93
97 samples_per_packet_ = timestamp_increase / sequence_num_increase; 94 samples_per_packet_ = timestamp_increase / sequence_num_increase;
98 } 95 }
99 96
100 void Nack::UpdateList(uint16_t sequence_number_current_received_rtp) { 97 void Nack::UpdateList(uint16_t sequence_number_current_received_rtp) {
101 // Some of the packets which were considered late, now are considered missing. 98 // Some of the packets which were considered late, now are considered missing.
102 ChangeFromLateToMissing(sequence_number_current_received_rtp); 99 ChangeFromLateToMissing(sequence_number_current_received_rtp);
103 100
104 if (IsNewerSequenceNumber(sequence_number_current_received_rtp, 101 if (IsNewerSequenceNumber(sequence_number_current_received_rtp,
105 sequence_num_last_received_rtp_ + 1)) 102 sequence_num_last_received_rtp_ + 1))
106 AddToList(sequence_number_current_received_rtp); 103 AddToList(sequence_number_current_received_rtp);
107 } 104 }
108 105
109 void Nack::ChangeFromLateToMissing( 106 void Nack::ChangeFromLateToMissing(
110 uint16_t sequence_number_current_received_rtp) { 107 uint16_t sequence_number_current_received_rtp) {
111 NackList::const_iterator lower_bound = nack_list_.lower_bound( 108 NackList::const_iterator lower_bound =
112 static_cast<uint16_t>(sequence_number_current_received_rtp - 109 nack_list_.lower_bound(static_cast<uint16_t>(
113 nack_threshold_packets_)); 110 sequence_number_current_received_rtp - nack_threshold_packets_));
114 111
115 for (NackList::iterator it = nack_list_.begin(); it != lower_bound; ++it) 112 for (NackList::iterator it = nack_list_.begin(); it != lower_bound; ++it)
116 it->second.is_missing = true; 113 it->second.is_missing = true;
117 } 114 }
118 115
119 uint32_t Nack::EstimateTimestamp(uint16_t sequence_num) { 116 uint32_t Nack::EstimateTimestamp(uint16_t sequence_num) {
120 uint16_t sequence_num_diff = sequence_num - sequence_num_last_received_rtp_; 117 uint16_t sequence_num_diff = sequence_num - sequence_num_last_received_rtp_;
121 return sequence_num_diff * samples_per_packet_ + timestamp_last_received_rtp_; 118 return sequence_num_diff * samples_per_packet_ + timestamp_last_received_rtp_;
122 } 119 }
123 120
124 void Nack::AddToList(uint16_t sequence_number_current_received_rtp) { 121 void Nack::AddToList(uint16_t sequence_number_current_received_rtp) {
125 assert(!any_rtp_decoded_ || IsNewerSequenceNumber( 122 assert(!any_rtp_decoded_ ||
126 sequence_number_current_received_rtp, sequence_num_last_decoded_rtp_)); 123 IsNewerSequenceNumber(sequence_number_current_received_rtp,
124 sequence_num_last_decoded_rtp_));
127 125
128 // Packets with sequence numbers older than |upper_bound_missing| are 126 // Packets with sequence numbers older than |upper_bound_missing| are
129 // considered missing, and the rest are considered late. 127 // considered missing, and the rest are considered late.
130 uint16_t upper_bound_missing = sequence_number_current_received_rtp - 128 uint16_t upper_bound_missing =
131 nack_threshold_packets_; 129 sequence_number_current_received_rtp - nack_threshold_packets_;
132 130
133 for (uint16_t n = sequence_num_last_received_rtp_ + 1; 131 for (uint16_t n = sequence_num_last_received_rtp_ + 1;
134 IsNewerSequenceNumber(sequence_number_current_received_rtp, n); ++n) { 132 IsNewerSequenceNumber(sequence_number_current_received_rtp, n); ++n) {
135 bool is_missing = IsNewerSequenceNumber(upper_bound_missing, n); 133 bool is_missing = IsNewerSequenceNumber(upper_bound_missing, n);
136 uint32_t timestamp = EstimateTimestamp(n); 134 uint32_t timestamp = EstimateTimestamp(n);
137 NackElement nack_element(TimeToPlay(timestamp), timestamp, is_missing); 135 NackElement nack_element(TimeToPlay(timestamp), timestamp, is_missing);
138 nack_list_.insert(nack_list_.end(), std::make_pair(n, nack_element)); 136 nack_list_.insert(nack_list_.end(), std::make_pair(n, nack_element));
139 } 137 }
140 } 138 }
141 139
142 void Nack::UpdateEstimatedPlayoutTimeBy10ms() { 140 void Nack::UpdateEstimatedPlayoutTimeBy10ms() {
143 while (!nack_list_.empty() && 141 while (!nack_list_.empty() &&
144 nack_list_.begin()->second.time_to_play_ms <= 10) 142 nack_list_.begin()->second.time_to_play_ms <= 10)
minyue-webrtc 2015/10/27 14:35:08 I know that breaking at ";" can follow the "," rul
hlundin-webrtc 2015/10/28 15:03:36 This is what git cl format gives me.
minyue-webrtc 2015/10/29 10:36:10 Acknowledged.
145 nack_list_.erase(nack_list_.begin()); 143 nack_list_.erase(nack_list_.begin());
146 144
147 for (NackList::iterator it = nack_list_.begin(); it != nack_list_.end(); ++it) 145 for (NackList::iterator it = nack_list_.begin(); it != nack_list_.end(); ++it)
148 it->second.time_to_play_ms -= 10; 146 it->second.time_to_play_ms -= 10;
149 } 147 }
150 148
151 void Nack::UpdateLastDecodedPacket(uint16_t sequence_number, 149 void Nack::UpdateLastDecodedPacket(uint16_t sequence_number,
152 uint32_t timestamp) { 150 uint32_t timestamp) {
153 if (IsNewerSequenceNumber(sequence_number, sequence_num_last_decoded_rtp_) || 151 if (IsNewerSequenceNumber(sequence_number, sequence_num_last_decoded_rtp_) ||
154 !any_rtp_decoded_) { 152 !any_rtp_decoded_) {
155 sequence_num_last_decoded_rtp_ = sequence_number; 153 sequence_num_last_decoded_rtp_ = sequence_number;
156 timestamp_last_decoded_rtp_ = timestamp; 154 timestamp_last_decoded_rtp_ = timestamp;
157 // Packets in the list with sequence numbers less than the 155 // Packets in the list with sequence numbers less than the
158 // sequence number of the decoded RTP should be removed from the lists. 156 // sequence number of the decoded RTP should be removed from the lists.
159 // They will be discarded by the jitter buffer if they arrive. 157 // They will be discarded by the jitter buffer if they arrive.
160 nack_list_.erase(nack_list_.begin(), nack_list_.upper_bound( 158 nack_list_.erase(nack_list_.begin(),
161 sequence_num_last_decoded_rtp_)); 159 nack_list_.upper_bound(sequence_num_last_decoded_rtp_));
162 160
163 // Update estimated time-to-play. 161 // Update estimated time-to-play.
164 for (NackList::iterator it = nack_list_.begin(); it != nack_list_.end(); 162 for (NackList::iterator it = nack_list_.begin(); it != nack_list_.end();
165 ++it) 163 ++it)
166 it->second.time_to_play_ms = TimeToPlay(it->second.estimated_timestamp); 164 it->second.time_to_play_ms = TimeToPlay(it->second.estimated_timestamp);
167 } else { 165 } else {
168 assert(sequence_number == sequence_num_last_decoded_rtp_); 166 assert(sequence_number == sequence_num_last_decoded_rtp_);
169 167
170 // Same sequence number as before. 10 ms is elapsed, update estimations for 168 // Same sequence number as before. 10 ms is elapsed, update estimations for
171 // time-to-play. 169 // time-to-play.
172 UpdateEstimatedPlayoutTimeBy10ms(); 170 UpdateEstimatedPlayoutTimeBy10ms();
173 171
174 // Update timestamp for better estimate of time-to-play, for packets which 172 // Update timestamp for better estimate of time-to-play, for packets which
175 // are added to NACK list later on. 173 // are added to NACK list later on.
(...skipping 22 matching lines...) Expand all
198 int Nack::SetMaxNackListSize(size_t max_nack_list_size) { 196 int Nack::SetMaxNackListSize(size_t max_nack_list_size) {
199 if (max_nack_list_size == 0 || max_nack_list_size > kNackListSizeLimit) 197 if (max_nack_list_size == 0 || max_nack_list_size > kNackListSizeLimit)
200 return -1; 198 return -1;
201 max_nack_list_size_ = max_nack_list_size; 199 max_nack_list_size_ = max_nack_list_size;
202 LimitNackListSize(); 200 LimitNackListSize();
203 return 0; 201 return 0;
204 } 202 }
205 203
206 void Nack::LimitNackListSize() { 204 void Nack::LimitNackListSize() {
207 uint16_t limit = sequence_num_last_received_rtp_ - 205 uint16_t limit = sequence_num_last_received_rtp_ -
208 static_cast<uint16_t>(max_nack_list_size_) - 1; 206 static_cast<uint16_t>(max_nack_list_size_) - 1;
209 nack_list_.erase(nack_list_.begin(), nack_list_.upper_bound(limit)); 207 nack_list_.erase(nack_list_.begin(), nack_list_.upper_bound(limit));
210 } 208 }
211 209
212 int64_t Nack::TimeToPlay(uint32_t timestamp) const { 210 int64_t Nack::TimeToPlay(uint32_t timestamp) const {
213 uint32_t timestamp_increase = timestamp - timestamp_last_decoded_rtp_; 211 uint32_t timestamp_increase = timestamp - timestamp_last_decoded_rtp_;
214 return timestamp_increase / sample_rate_khz_; 212 return timestamp_increase / sample_rate_khz_;
215 } 213 }
216 214
217 // We don't erase elements with time-to-play shorter than round-trip-time. 215 // We don't erase elements with time-to-play shorter than round-trip-time.
218 std::vector<uint16_t> Nack::GetNackList(int64_t round_trip_time_ms) const { 216 std::vector<uint16_t> Nack::GetNackList(int64_t round_trip_time_ms) const {
219 std::vector<uint16_t> sequence_numbers; 217 std::vector<uint16_t> sequence_numbers;
220 for (NackList::const_iterator it = nack_list_.begin(); it != nack_list_.end(); 218 for (NackList::const_iterator it = nack_list_.begin(); it != nack_list_.end();
221 ++it) { 219 ++it) {
222 if (it->second.is_missing && 220 if (it->second.is_missing &&
223 it->second.time_to_play_ms > round_trip_time_ms) 221 it->second.time_to_play_ms > round_trip_time_ms)
224 sequence_numbers.push_back(it->first); 222 sequence_numbers.push_back(it->first);
225 } 223 }
226 return sequence_numbers; 224 return sequence_numbers;
227 } 225 }
228 226
229 } // namespace acm2
230
231 } // namespace webrtc 227 } // namespace webrtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698