blob: 9c0ee96824efaf94e503b3dddea73fb91fa105fe [file] [log] [blame]
henrik.lundin@webrtc.orgd94659d2013-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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "modules/audio_coding/neteq/decision_logic.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000012
Henrik Lundin7687ad52018-07-02 10:14:46 +020013#include <assert.h>
Yves Gerey988cc082018-10-23 12:03:01 +020014#include <stdio.h>
Jonas Olssona4d87372019-07-05 19:08:33 +020015
Yves Gerey988cc082018-10-23 12:03:01 +020016#include <string>
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000017
Jakob Ivarsson46dda832019-07-03 16:00:30 +020018#include "absl/types/optional.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "modules/audio_coding/neteq/packet_buffer.h"
Yves Gerey988cc082018-10-23 12:03:01 +020020#include "rtc_base/checks.h"
Jakob Ivarsson46dda832019-07-03 16:00:30 +020021#include "rtc_base/experiments/field_trial_parser.h"
Minyue Li7f6417f2018-10-03 21:19:08 +020022#include "rtc_base/logging.h"
Yves Gerey988cc082018-10-23 12:03:01 +020023#include "rtc_base/numerics/safe_conversions.h"
Jakob Ivarsson46dda832019-07-03 16:00:30 +020024#include "system_wrappers/include/field_trial.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000025
Minyue Li7f6417f2018-10-03 21:19:08 +020026namespace {
Minyue Li7f6417f2018-10-03 21:19:08 +020027
Jakob Ivarssond3a780b2019-02-28 14:30:21 +010028constexpr int kPostponeDecodingLevel = 50;
Jakob Ivarsson46dda832019-07-03 16:00:30 +020029constexpr int kDefaultTargetLevelWindowMs = 100;
Jakob Ivarsson80fb9782020-10-09 13:41:06 +020030constexpr int kDecelerationTargetLevelOffsetMs = 85;
Minyue Li7f6417f2018-10-03 21:19:08 +020031
32} // namespace
33
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000034namespace webrtc {
35
Ivo Creusen53a31f72019-10-24 15:20:39 +020036DecisionLogic::DecisionLogic(NetEqController::Config config)
Jakob Ivarsson609b0472020-10-19 09:19:34 +020037 : DecisionLogic(config,
38 DelayManager::Create(config.max_packets_in_buffer,
39 config.base_min_delay_ms,
40 config.tick_timer),
41 std::make_unique<BufferLevelFilter>()) {}
42
43DecisionLogic::DecisionLogic(
44 NetEqController::Config config,
45 std::unique_ptr<DelayManager> delay_manager,
46 std::unique_ptr<BufferLevelFilter> buffer_level_filter)
47 : delay_manager_(std::move(delay_manager)),
48 buffer_level_filter_(std::move(buffer_level_filter)),
Ivo Creusen53a31f72019-10-24 15:20:39 +020049 tick_timer_(config.tick_timer),
50 disallow_time_stretching_(!config.allow_time_stretching),
Henrik Lundin47b17dc2016-05-10 10:20:59 +020051 timescale_countdown_(
52 tick_timer_->GetNewCountdown(kMinTimescaleInterval + 1)),
Jakob Ivarsson46dda832019-07-03 16:00:30 +020053 estimate_dtx_delay_("estimate_dtx_delay", false),
54 time_stretch_cn_("time_stretch_cn", false),
55 target_level_window_ms_("target_level_window",
56 kDefaultTargetLevelWindowMs,
57 0,
58 absl::nullopt) {
Jakob Ivarsson46dda832019-07-03 16:00:30 +020059 const std::string field_trial_name =
60 field_trial::FindFullName("WebRTC-Audio-NetEqDecisionLogicSettings");
61 ParseFieldTrial(
62 {&estimate_dtx_delay_, &time_stretch_cn_, &target_level_window_ms_},
63 field_trial_name);
64 RTC_LOG(LS_INFO) << "NetEq decision logic settings:"
Jonas Olssonb2b20312020-01-14 12:11:31 +010065 " estimate_dtx_delay="
66 << estimate_dtx_delay_
Jakob Ivarsson46dda832019-07-03 16:00:30 +020067 << " time_stretch_cn=" << time_stretch_cn_
68 << " target_level_window_ms=" << target_level_window_ms_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000069}
70
Henrik Lundin47b17dc2016-05-10 10:20:59 +020071DecisionLogic::~DecisionLogic() = default;
72
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000073void DecisionLogic::Reset() {
74 cng_state_ = kCngOff;
henrik.lundinb1fb72b2016-05-03 08:18:47 -070075 noise_fast_forward_ = 0;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000076 packet_length_samples_ = 0;
77 sample_memory_ = 0;
78 prev_time_scale_ = false;
Jakob Ivarsson80fb9782020-10-09 13:41:06 +020079 last_pack_cng_or_dtmf_ = true;
Henrik Lundin47b17dc2016-05-10 10:20:59 +020080 timescale_countdown_.reset();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000081 num_consecutive_expands_ = 0;
Jakob Ivarsson46dda832019-07-03 16:00:30 +020082 time_stretched_cn_samples_ = 0;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000083}
84
85void DecisionLogic::SoftReset() {
86 packet_length_samples_ = 0;
87 sample_memory_ = 0;
88 prev_time_scale_ = false;
Jakob Ivarsson80fb9782020-10-09 13:41:06 +020089 last_pack_cng_or_dtmf_ = true;
Henrik Lundin47b17dc2016-05-10 10:20:59 +020090 timescale_countdown_ =
91 tick_timer_->GetNewCountdown(kMinTimescaleInterval + 1);
Jakob Ivarsson46dda832019-07-03 16:00:30 +020092 time_stretched_cn_samples_ = 0;
Ivo Creusen53a31f72019-10-24 15:20:39 +020093 delay_manager_->Reset();
Jakob Ivarsson609b0472020-10-19 09:19:34 +020094 buffer_level_filter_->Reset();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000095}
96
Peter Kastingdce40cf2015-08-24 14:52:23 -070097void DecisionLogic::SetSampleRate(int fs_hz, size_t output_size_samples) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000098 // TODO(hlundin): Change to an enumerator and skip assert.
Yves Gerey665174f2018-06-19 15:03:05 +020099 assert(fs_hz == 8000 || fs_hz == 16000 || fs_hz == 32000 || fs_hz == 48000);
Jakob Ivarsson46dda832019-07-03 16:00:30 +0200100 sample_rate_ = fs_hz;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000101 output_size_samples_ = output_size_samples;
102}
103
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100104NetEq::Operation DecisionLogic::GetDecision(const NetEqStatus& status,
105 bool* reset_decoder) {
ossu61a208b2016-09-20 01:38:00 -0700106 // If last mode was CNG (or Expand, since this could be covering up for
107 // a lost CNG packet), remember that CNG is on. This is needed if comfort
108 // noise is interrupted by DTMF.
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100109 if (status.last_mode == NetEq::Mode::kRfc3389Cng) {
ossu61a208b2016-09-20 01:38:00 -0700110 cng_state_ = kCngRfc3389On;
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100111 } else if (status.last_mode == NetEq::Mode::kCodecInternalCng) {
ossu61a208b2016-09-20 01:38:00 -0700112 cng_state_ = kCngInternalOn;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000113 }
114
Ivo Creusen53a31f72019-10-24 15:20:39 +0200115 size_t cur_size_samples = estimate_dtx_delay_
116 ? status.packet_buffer_info.span_samples
117 : status.packet_buffer_info.num_samples;
Yves Gerey665174f2018-06-19 15:03:05 +0200118 prev_time_scale_ =
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100119 prev_time_scale_ &&
120 (status.last_mode == NetEq::Mode::kAccelerateSuccess ||
121 status.last_mode == NetEq::Mode::kAccelerateLowEnergy ||
122 status.last_mode == NetEq::Mode::kPreemptiveExpandSuccess ||
123 status.last_mode == NetEq::Mode::kPreemptiveExpandLowEnergy);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000124
Minyue Li7d204d52019-04-16 11:44:49 +0200125 // Do not update buffer history if currently playing CNG since it will bias
126 // the filtered buffer level.
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100127 if (status.last_mode != NetEq::Mode::kRfc3389Cng &&
128 status.last_mode != NetEq::Mode::kCodecInternalCng &&
Ivo Creusen53a31f72019-10-24 15:20:39 +0200129 !(status.next_packet && status.next_packet->is_dtx &&
130 !estimate_dtx_delay_)) {
Minyue Li7d204d52019-04-16 11:44:49 +0200131 FilterBufferLevel(cur_size_samples);
132 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000133
Henrik Lundin7687ad52018-07-02 10:14:46 +0200134 // Guard for errors, to avoid getting stuck in error mode.
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100135 if (status.last_mode == NetEq::Mode::kError) {
Ivo Creusen53a31f72019-10-24 15:20:39 +0200136 if (!status.next_packet) {
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100137 return NetEq::Operation::kExpand;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200138 } else {
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100139 // Use kUndefined to flag for a reset.
140 return NetEq::Operation::kUndefined;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200141 }
142 }
143
Ivo Creusen53a31f72019-10-24 15:20:39 +0200144 if (status.next_packet && status.next_packet->is_cng) {
145 return CngOperation(status.last_mode, status.target_timestamp,
146 status.next_packet->timestamp,
147 status.generated_noise_samples);
Henrik Lundin7687ad52018-07-02 10:14:46 +0200148 }
149
150 // Handle the case with no packet at all available (except maybe DTMF).
Ivo Creusen53a31f72019-10-24 15:20:39 +0200151 if (!status.next_packet) {
152 return NoPacket(status.play_dtmf);
Henrik Lundin7687ad52018-07-02 10:14:46 +0200153 }
154
155 // If the expand period was very long, reset NetEQ since it is likely that the
156 // sender was restarted.
157 if (num_consecutive_expands_ > kReinitAfterExpands) {
158 *reset_decoder = true;
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100159 return NetEq::Operation::kNormal;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200160 }
161
162 // Make sure we don't restart audio too soon after an expansion to avoid
163 // running out of data right away again. We should only wait if there are no
164 // DTX or CNG packets in the buffer (otherwise we should just play out what we
165 // have, since we cannot know the exact duration of DTX or CNG packets), and
166 // if the mute factor is low enough (otherwise the expansion was short enough
167 // to not be noticable).
168 // Note that the MuteFactor is in Q14, so a value of 16384 corresponds to 1.
Ivo Creusen53a31f72019-10-24 15:20:39 +0200169 const size_t current_span =
170 estimate_dtx_delay_ ? status.packet_buffer_info.span_samples
171 : status.packet_buffer_info.span_samples_no_dtx;
Jakob Ivarsson80fb9782020-10-09 13:41:06 +0200172 const int target_level_samples =
173 delay_manager_->TargetDelayMs() * sample_rate_ / 1000;
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100174 if ((status.last_mode == NetEq::Mode::kExpand ||
175 status.last_mode == NetEq::Mode::kCodecPlc) &&
Ivo Creusen53a31f72019-10-24 15:20:39 +0200176 status.expand_mutefactor < 16384 / 2 &&
Jakob Ivarsson80fb9782020-10-09 13:41:06 +0200177 current_span < static_cast<size_t>(target_level_samples *
178 kPostponeDecodingLevel / 100) &&
Ivo Creusen53a31f72019-10-24 15:20:39 +0200179 !status.packet_buffer_info.dtx_or_cng) {
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100180 return NetEq::Operation::kExpand;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200181 }
182
Jakob Ivarsson46dda832019-07-03 16:00:30 +0200183 const uint32_t five_seconds_samples = static_cast<uint32_t>(5 * sample_rate_);
Henrik Lundin7687ad52018-07-02 10:14:46 +0200184 // Check if the required packet is available.
Ivo Creusen53a31f72019-10-24 15:20:39 +0200185 if (status.target_timestamp == status.next_packet->timestamp) {
186 return ExpectedPacketAvailable(status.last_mode, status.play_dtmf);
187 } else if (!PacketBuffer::IsObsoleteTimestamp(status.next_packet->timestamp,
188 status.target_timestamp,
189 five_seconds_samples)) {
190 return FuturePacketAvailable(
191 status.last_packet_samples, status.last_mode, status.target_timestamp,
192 status.next_packet->timestamp, status.play_dtmf,
193 status.generated_noise_samples, status.packet_buffer_info.span_samples,
194 status.packet_buffer_info.num_packets);
Henrik Lundin7687ad52018-07-02 10:14:46 +0200195 } else {
196 // This implies that available_timestamp < target_timestamp, which can
197 // happen when a new stream or codec is received. Signal for a reset.
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100198 return NetEq::Operation::kUndefined;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200199 }
200}
201
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100202void DecisionLogic::ExpandDecision(NetEq::Operation operation) {
203 if (operation == NetEq::Operation::kExpand) {
Henrik Lundin5afa61c2018-07-02 14:53:24 +0200204 num_consecutive_expands_++;
205 } else {
206 num_consecutive_expands_ = 0;
207 }
208}
209
Ivo Creusena2b31c32020-10-14 17:54:22 +0200210absl::optional<int> DecisionLogic::PacketArrived(
211 int fs_hz,
212 bool should_update_stats,
213 const PacketArrivedInfo& info) {
214 if (info.is_cng_or_dtmf) {
Jakob Ivarsson80fb9782020-10-09 13:41:06 +0200215 last_pack_cng_or_dtmf_ = true;
216 return absl::nullopt;
Ivo Creusen53a31f72019-10-24 15:20:39 +0200217 }
Jakob Ivarsson80fb9782020-10-09 13:41:06 +0200218 if (!should_update_stats) {
219 return absl::nullopt;
220 }
Ivo Creusena2b31c32020-10-14 17:54:22 +0200221 if (info.packet_length_samples > 0 && fs_hz > 0 &&
222 info.packet_length_samples != packet_length_samples_) {
223 packet_length_samples_ = info.packet_length_samples;
Jakob Ivarsson80fb9782020-10-09 13:41:06 +0200224 delay_manager_->SetPacketAudioLength(packet_length_samples_ * 1000 / fs_hz);
225 }
226 auto relative_delay = delay_manager_->Update(
Ivo Creusena2b31c32020-10-14 17:54:22 +0200227 info.main_timestamp, fs_hz, /*reset=*/last_pack_cng_or_dtmf_);
Jakob Ivarsson80fb9782020-10-09 13:41:06 +0200228 last_pack_cng_or_dtmf_ = false;
Ivo Creusen53a31f72019-10-24 15:20:39 +0200229 return relative_delay;
230}
231
Minyue Li7d204d52019-04-16 11:44:49 +0200232void DecisionLogic::FilterBufferLevel(size_t buffer_size_samples) {
Jakob Ivarsson609b0472020-10-19 09:19:34 +0200233 buffer_level_filter_->SetTargetBufferLevel(delay_manager_->TargetDelayMs());
Henrik Lundin5afa61c2018-07-02 14:53:24 +0200234
Jakob Ivarsson46dda832019-07-03 16:00:30 +0200235 int time_stretched_samples = time_stretched_cn_samples_;
Minyue Li7d204d52019-04-16 11:44:49 +0200236 if (prev_time_scale_) {
Jakob Ivarsson46dda832019-07-03 16:00:30 +0200237 time_stretched_samples += sample_memory_;
Minyue Li7d204d52019-04-16 11:44:49 +0200238 timescale_countdown_ = tick_timer_->GetNewCountdown(kMinTimescaleInterval);
239 }
240
Jakob Ivarsson609b0472020-10-19 09:19:34 +0200241 buffer_level_filter_->Update(buffer_size_samples, time_stretched_samples);
Minyue Li7d204d52019-04-16 11:44:49 +0200242 prev_time_scale_ = false;
Jakob Ivarsson46dda832019-07-03 16:00:30 +0200243 time_stretched_cn_samples_ = 0;
Henrik Lundin5afa61c2018-07-02 14:53:24 +0200244}
245
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100246NetEq::Operation DecisionLogic::CngOperation(NetEq::Mode prev_mode,
247 uint32_t target_timestamp,
248 uint32_t available_timestamp,
249 size_t generated_noise_samples) {
Henrik Lundin7687ad52018-07-02 10:14:46 +0200250 // Signed difference between target and available timestamp.
251 int32_t timestamp_diff = static_cast<int32_t>(
252 static_cast<uint32_t>(generated_noise_samples + target_timestamp) -
253 available_timestamp);
Jakob Ivarsson80fb9782020-10-09 13:41:06 +0200254 int optimal_level_samp =
255 delay_manager_->TargetDelayMs() * sample_rate_ / 1000;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200256 const int64_t excess_waiting_time_samp =
257 -static_cast<int64_t>(timestamp_diff) - optimal_level_samp;
258
259 if (excess_waiting_time_samp > optimal_level_samp / 2) {
260 // The waiting time for this packet will be longer than 1.5
261 // times the wanted buffer delay. Apply fast-forward to cut the
262 // waiting time down to the optimal.
Jakob Ivarsson42b6e2d2019-10-21 11:51:05 +0200263 noise_fast_forward_ = rtc::saturated_cast<size_t>(noise_fast_forward_ +
264 excess_waiting_time_samp);
Henrik Lundin7687ad52018-07-02 10:14:46 +0200265 timestamp_diff =
266 rtc::saturated_cast<int32_t>(timestamp_diff + excess_waiting_time_samp);
267 }
268
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100269 if (timestamp_diff < 0 && prev_mode == NetEq::Mode::kRfc3389Cng) {
Henrik Lundin7687ad52018-07-02 10:14:46 +0200270 // Not time to play this packet yet. Wait another round before using this
271 // packet. Keep on playing CNG from previous CNG parameters.
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100272 return NetEq::Operation::kRfc3389CngNoPacket;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200273 } else {
274 // Otherwise, go for the CNG packet now.
275 noise_fast_forward_ = 0;
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100276 return NetEq::Operation::kRfc3389Cng;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200277 }
278}
279
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100280NetEq::Operation DecisionLogic::NoPacket(bool play_dtmf) {
Henrik Lundin7687ad52018-07-02 10:14:46 +0200281 if (cng_state_ == kCngRfc3389On) {
282 // Keep on playing comfort noise.
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100283 return NetEq::Operation::kRfc3389CngNoPacket;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200284 } else if (cng_state_ == kCngInternalOn) {
285 // Keep on playing codec internal comfort noise.
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100286 return NetEq::Operation::kCodecInternalCng;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200287 } else if (play_dtmf) {
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100288 return NetEq::Operation::kDtmf;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200289 } else {
290 // Nothing to play, do expand.
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100291 return NetEq::Operation::kExpand;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200292 }
293}
294
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100295NetEq::Operation DecisionLogic::ExpectedPacketAvailable(NetEq::Mode prev_mode,
296 bool play_dtmf) {
297 if (!disallow_time_stretching_ && prev_mode != NetEq::Mode::kExpand &&
298 !play_dtmf) {
Jakob Ivarsson80fb9782020-10-09 13:41:06 +0200299 const int samples_per_ms = sample_rate_ / 1000;
300 const int target_level_samples =
301 delay_manager_->TargetDelayMs() * samples_per_ms;
302 const int low_limit =
303 std::max(target_level_samples * 3 / 4,
304 target_level_samples -
305 kDecelerationTargetLevelOffsetMs * samples_per_ms);
306 // |higher_limit| is equal to |target_level|, but should at
307 // least be 20 ms higher than |lower_limit|.
308 const int high_limit =
309 std::max(target_level_samples, low_limit + 20 * samples_per_ms);
310
311 const int buffer_level_samples =
Jakob Ivarsson609b0472020-10-19 09:19:34 +0200312 buffer_level_filter_->filtered_current_level();
Jakob Ivarsson80fb9782020-10-09 13:41:06 +0200313 if (buffer_level_samples >= high_limit << 2)
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100314 return NetEq::Operation::kFastAccelerate;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200315 if (TimescaleAllowed()) {
Jakob Ivarsson80fb9782020-10-09 13:41:06 +0200316 if (buffer_level_samples >= high_limit)
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100317 return NetEq::Operation::kAccelerate;
Jakob Ivarsson80fb9782020-10-09 13:41:06 +0200318 if (buffer_level_samples < low_limit)
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100319 return NetEq::Operation::kPreemptiveExpand;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200320 }
321 }
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100322 return NetEq::Operation::kNormal;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200323}
324
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100325NetEq::Operation DecisionLogic::FuturePacketAvailable(
Henrik Lundin7687ad52018-07-02 10:14:46 +0200326 size_t decoder_frame_length,
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100327 NetEq::Mode prev_mode,
Henrik Lundin7687ad52018-07-02 10:14:46 +0200328 uint32_t target_timestamp,
329 uint32_t available_timestamp,
330 bool play_dtmf,
Ivo Creusen53a31f72019-10-24 15:20:39 +0200331 size_t generated_noise_samples,
332 size_t span_samples_in_packet_buffer,
333 size_t num_packets_in_packet_buffer) {
Henrik Lundin7687ad52018-07-02 10:14:46 +0200334 // Required packet is not available, but a future packet is.
335 // Check if we should continue with an ongoing expand because the new packet
336 // is too far into the future.
337 uint32_t timestamp_leap = available_timestamp - target_timestamp;
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100338 if ((prev_mode == NetEq::Mode::kExpand ||
339 prev_mode == NetEq::Mode::kCodecPlc) &&
Henrik Lundin00eb12a2018-09-05 18:14:52 +0200340 !ReinitAfterExpands(timestamp_leap) && !MaxWaitForPacket() &&
341 PacketTooEarly(timestamp_leap) && UnderTargetLevel()) {
Henrik Lundin7687ad52018-07-02 10:14:46 +0200342 if (play_dtmf) {
343 // Still have DTMF to play, so do not do expand.
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100344 return NetEq::Operation::kDtmf;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200345 } else {
346 // Nothing to play.
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100347 return NetEq::Operation::kExpand;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200348 }
349 }
350
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100351 if (prev_mode == NetEq::Mode::kCodecPlc) {
352 return NetEq::Operation::kNormal;
Henrik Lundin00eb12a2018-09-05 18:14:52 +0200353 }
354
Henrik Lundin7687ad52018-07-02 10:14:46 +0200355 // If previous was comfort noise, then no merge is needed.
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100356 if (prev_mode == NetEq::Mode::kRfc3389Cng ||
357 prev_mode == NetEq::Mode::kCodecInternalCng) {
Jakob Ivarsson46dda832019-07-03 16:00:30 +0200358 size_t cur_size_samples =
359 estimate_dtx_delay_
Jakob Ivarsson80fb9782020-10-09 13:41:06 +0200360 ? span_samples_in_packet_buffer
Ivo Creusen53a31f72019-10-24 15:20:39 +0200361 : num_packets_in_packet_buffer * decoder_frame_length;
Jakob Ivarsson46dda832019-07-03 16:00:30 +0200362 // Target level is in number of packets in Q8.
363 const size_t target_level_samples =
Jakob Ivarsson80fb9782020-10-09 13:41:06 +0200364 delay_manager_->TargetDelayMs() * sample_rate_ / 1000;
Jakob Ivarsson46dda832019-07-03 16:00:30 +0200365 const bool generated_enough_noise =
366 static_cast<uint32_t>(generated_noise_samples + target_timestamp) >=
367 available_timestamp;
368
369 if (time_stretch_cn_) {
370 const size_t target_threshold_samples =
371 target_level_window_ms_ / 2 * (sample_rate_ / 1000);
372 const bool above_target_window =
373 cur_size_samples > target_level_samples + target_threshold_samples;
374 const bool below_target_window =
375 target_level_samples > target_threshold_samples &&
376 cur_size_samples < target_level_samples - target_threshold_samples;
377 // Keep the delay same as before CNG, but make sure that it is within the
378 // target window.
379 if ((generated_enough_noise && !below_target_window) ||
380 above_target_window) {
381 time_stretched_cn_samples_ = timestamp_leap - generated_noise_samples;
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100382 return NetEq::Operation::kNormal;
Jakob Ivarsson46dda832019-07-03 16:00:30 +0200383 }
Henrik Lundin7687ad52018-07-02 10:14:46 +0200384 } else {
Jakob Ivarsson46dda832019-07-03 16:00:30 +0200385 // Keep the same delay as before the CNG, but make sure that the number of
386 // samples in buffer is no higher than 4 times the optimal level.
387 if (generated_enough_noise ||
388 cur_size_samples > target_level_samples * 4) {
389 // Time to play this new packet.
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100390 return NetEq::Operation::kNormal;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200391 }
392 }
Jakob Ivarsson46dda832019-07-03 16:00:30 +0200393
394 // Too early to play this new packet; keep on playing comfort noise.
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100395 if (prev_mode == NetEq::Mode::kRfc3389Cng) {
396 return NetEq::Operation::kRfc3389CngNoPacket;
Jakob Ivarsson46dda832019-07-03 16:00:30 +0200397 }
398 // prevPlayMode == kModeCodecInternalCng.
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100399 return NetEq::Operation::kCodecInternalCng;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200400 }
Jakob Ivarsson46dda832019-07-03 16:00:30 +0200401
Henrik Lundin7687ad52018-07-02 10:14:46 +0200402 // Do not merge unless we have done an expand before.
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100403 if (prev_mode == NetEq::Mode::kExpand) {
404 return NetEq::Operation::kMerge;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200405 } else if (play_dtmf) {
406 // Play DTMF instead of expand.
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100407 return NetEq::Operation::kDtmf;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200408 } else {
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100409 return NetEq::Operation::kExpand;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200410 }
411}
412
413bool DecisionLogic::UnderTargetLevel() const {
Jakob Ivarsson609b0472020-10-19 09:19:34 +0200414 return buffer_level_filter_->filtered_current_level() <
Jakob Ivarsson80fb9782020-10-09 13:41:06 +0200415 delay_manager_->TargetDelayMs() * sample_rate_ / 1000;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200416}
417
418bool DecisionLogic::ReinitAfterExpands(uint32_t timestamp_leap) const {
419 return timestamp_leap >=
420 static_cast<uint32_t>(output_size_samples_ * kReinitAfterExpands);
421}
422
423bool DecisionLogic::PacketTooEarly(uint32_t timestamp_leap) const {
424 return timestamp_leap >
425 static_cast<uint32_t>(output_size_samples_ * num_consecutive_expands_);
426}
427
428bool DecisionLogic::MaxWaitForPacket() const {
429 return num_consecutive_expands_ >= kMaxWaitForPacket;
430}
431
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000432} // namespace webrtc