blob: e95c787a0e965f89508dd9ca06b49ee018479d4a [file] [log] [blame]
henrik.lundin@webrtc.org9a400812013-01-29 12:09:21 +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/neteq4/decision_logic_normal.h"
12
13#include <assert.h>
14
15#include <algorithm>
16
17#include "webrtc/modules/audio_coding/neteq4/buffer_level_filter.h"
18#include "webrtc/modules/audio_coding/neteq4/decoder_database.h"
19#include "webrtc/modules/audio_coding/neteq4/delay_manager.h"
20#include "webrtc/modules/audio_coding/neteq4/expand.h"
21#include "webrtc/modules/audio_coding/neteq4/packet_buffer.h"
22#include "webrtc/modules/audio_coding/neteq4/sync_buffer.h"
23#include "webrtc/modules/interface/module_common_types.h"
24
25namespace webrtc {
26
27Operations DecisionLogicNormal::GetDecisionSpecialized(
28 const SyncBuffer& sync_buffer,
29 const Expand& expand,
30 int decoder_frame_length,
31 const RTPHeader* packet_header,
32 Modes prev_mode,
33 bool play_dtmf,
34 bool* reset_decoder) {
35 assert(playout_mode_ == kPlayoutOn || playout_mode_ == kPlayoutStreaming);
36 // Guard for errors, to avoid getting stuck in error mode.
37 if (prev_mode == kModeError) {
38 if (!packet_header) {
39 return kExpand;
40 } else {
41 return kUndefined; // Use kUndefined to flag for a reset.
42 }
43 }
44
45 uint32_t target_timestamp = sync_buffer.end_timestamp();
46 uint32_t available_timestamp = 0;
47 int is_cng_packet = 0;
48 if (packet_header) {
49 available_timestamp = packet_header->timestamp;
50 is_cng_packet =
51 decoder_database_->IsComfortNoise(packet_header->payloadType);
52 }
53
54 if (is_cng_packet) {
55 return CngOperation(prev_mode, target_timestamp, available_timestamp);
56 }
57
58 // Handle the case with no packet at all available (except maybe DTMF).
59 if (!packet_header) {
60 return NoPacket(play_dtmf);
61 }
62
63 // If the expand period was very long, reset NetEQ since it is likely that the
64 // sender was restarted.
65 if (num_consecutive_expands_ > kReinitAfterExpands) {
66 *reset_decoder = true;
67 return kNormal;
68 }
69
70 // Check if the required packet is available.
71 if (target_timestamp == available_timestamp) {
72 return ExpectedPacketAvailable(prev_mode, play_dtmf);
73 } else if (available_timestamp > target_timestamp) {
74 // TODO(hlundin): Consider wrap-around too?
75 return FuturePacketAvailable(sync_buffer, expand, decoder_frame_length,
76 prev_mode, target_timestamp,
77 available_timestamp, play_dtmf);
78 } else {
79 // This implies that available_timestamp < target_timestamp, which can
80 // happen when a new stream or codec is received. Signal for a reset.
81 return kUndefined;
82 }
83}
84
85Operations DecisionLogicNormal::CngOperation(Modes prev_mode,
86 uint32_t target_timestamp,
87 uint32_t available_timestamp) {
88 // Signed difference between target and available timestamp.
89 int32_t timestamp_diff = (generated_noise_samples_ + target_timestamp) -
90 available_timestamp;
91 int32_t optimal_level_samp =
92 (delay_manager_->TargetLevel() * packet_length_samples_) >> 8;
93 int32_t excess_waiting_time_samp = -timestamp_diff - optimal_level_samp;
94
95 if (excess_waiting_time_samp > optimal_level_samp / 2) {
96 // The waiting time for this packet will be longer than 1.5
97 // times the wanted buffer delay. Advance the clock to cut
98 // waiting time down to the optimal.
99 generated_noise_samples_ += excess_waiting_time_samp;
100 timestamp_diff += excess_waiting_time_samp;
101 }
102
103 if (timestamp_diff < 0 && prev_mode == kModeRfc3389Cng) {
104 // Not time to play this packet yet. Wait another round before using this
105 // packet. Keep on playing CNG from previous CNG parameters.
106 return kRfc3389CngNoPacket;
107 } else {
108 // Otherwise, go for the CNG packet now.
109 return kRfc3389Cng;
110 }
111}
112
113Operations DecisionLogicNormal::NoPacket(bool play_dtmf) {
114 if (cng_state_ == kCngRfc3389On) {
115 // Keep on playing comfort noise.
116 return kRfc3389CngNoPacket;
117 } else if (cng_state_ == kCngInternalOn) {
118 // Keep on playing codec internal comfort noise.
119 return kCodecInternalCng;
120 } else if (play_dtmf) {
121 return kDtmf;
122 } else {
123 // Nothing to play, do expand.
124 return kExpand;
125 }
126}
127
128Operations DecisionLogicNormal::ExpectedPacketAvailable(Modes prev_mode,
129 bool play_dtmf) {
130 if (prev_mode != kModeExpand && !play_dtmf) {
131 // Check criterion for time-stretching.
132 int low_limit, high_limit;
133 delay_manager_->BufferLimits(&low_limit, &high_limit);
134 if ((buffer_level_filter_->filtered_current_level() >= high_limit &&
135 TimescaleAllowed()) ||
136 buffer_level_filter_->filtered_current_level() >= high_limit << 2) {
137 // Buffer level higher than limit and time-scaling allowed,
138 // or buffer level really high.
139 return kAccelerate;
140 } else if ((buffer_level_filter_->filtered_current_level() < low_limit)
141 && TimescaleAllowed()) {
142 return kPreemptiveExpand;
143 }
144 }
145 return kNormal;
146}
147
148Operations DecisionLogicNormal::FuturePacketAvailable(
149 const SyncBuffer& sync_buffer,
150 const Expand& expand,
151 int decoder_frame_length,
152 Modes prev_mode,
153 uint32_t target_timestamp,
154 uint32_t available_timestamp,
155 bool play_dtmf) {
156 // Required packet is not available, but a future packet is.
157 // Check if we should continue with an ongoing expand because the new packet
158 // is too far into the future.
159 uint32_t timestamp_leap = available_timestamp - target_timestamp;
160 if ((prev_mode == kModeExpand) &&
161 !ReinitAfterExpands(timestamp_leap) &&
162 !MaxWaitForPacket() &&
163 PacketTooEarly(timestamp_leap) &&
164 UnderTargetLevel()) {
165 if (play_dtmf) {
166 // Still have DTMF to play, so do not do expand.
167 return kDtmf;
168 } else {
169 // Nothing to play.
170 return kExpand;
171 }
172 }
173
174 const int samples_left = sync_buffer.FutureLength() -
175 expand.overlap_length();
176 const int cur_size_samples = samples_left +
177 packet_buffer_.NumPacketsInBuffer() * decoder_frame_length;
178
179 // If previous was comfort noise, then no merge is needed.
180 if (prev_mode == kModeRfc3389Cng ||
181 prev_mode == kModeCodecInternalCng) {
182 // Keep the same delay as before the CNG (or maximum 70 ms in buffer as
183 // safety precaution), but make sure that the number of samples in buffer
184 // is no higher than 4 times the optimal level. (Note that TargetLevel()
185 // is in Q8.)
186 int32_t timestamp_diff = (generated_noise_samples_ + target_timestamp) -
187 available_timestamp;
188 if (timestamp_diff >= 0 ||
189 cur_size_samples >
190 4 * ((delay_manager_->TargetLevel() * packet_length_samples_) >> 8)) {
191 // Time to play this new packet.
192 return kNormal;
193 } else {
194 // Too early to play this new packet; keep on playing comfort noise.
195 if (prev_mode == kModeRfc3389Cng) {
196 return kRfc3389CngNoPacket;
197 } else { // prevPlayMode == kModeCodecInternalCng.
198 return kCodecInternalCng;
199 }
200 }
201 }
202 // Do not merge unless we have done an expand before.
203 // (Convert kAllowMergeWithoutExpand from ms to samples by multiplying with
204 // fs_mult_ * 8 = fs / 1000.)
205 if (prev_mode == kModeExpand ||
206 (decoder_frame_length < output_size_samples_ &&
207 cur_size_samples > kAllowMergeWithoutExpandMs * fs_mult_ * 8)) {
208 return kMerge;
209 } else if (play_dtmf) {
210 // Play DTMF instead of expand.
211 return kDtmf;
212 } else {
213 return kExpand;
214 }
215}
216
217bool DecisionLogicNormal::UnderTargetLevel() const {
218 return buffer_level_filter_->filtered_current_level() <=
219 delay_manager_->TargetLevel();
220}
221
222bool DecisionLogicNormal::ReinitAfterExpands(uint32_t timestamp_leap) const {
223 return timestamp_leap >=
224 static_cast<uint32_t>(output_size_samples_ * kReinitAfterExpands);
225}
226
227bool DecisionLogicNormal::PacketTooEarly(uint32_t timestamp_leap) const {
228 return timestamp_leap >
229 static_cast<uint32_t>(output_size_samples_ * num_consecutive_expands_);
230}
231
232bool DecisionLogicNormal::MaxWaitForPacket() const {
233 return num_consecutive_expands_ >= kMaxWaitForPacket;
234}
235
236} // namespace webrtc