blob: 30463fcc49c361bb50e90ed6d3d3b0be2f370867 [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
Yves Gerey988cc082018-10-23 12:03:01 +020013#include <stdio.h>
Jonas Olssona4d87372019-07-05 19:08:33 +020014
Yves Gerey988cc082018-10-23 12:03:01 +020015#include <string>
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000016
Jakob Ivarsson46dda832019-07-03 16:00:30 +020017#include "absl/types/optional.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "modules/audio_coding/neteq/packet_buffer.h"
Yves Gerey988cc082018-10-23 12:03:01 +020019#include "rtc_base/checks.h"
Jakob Ivarsson46dda832019-07-03 16:00:30 +020020#include "rtc_base/experiments/field_trial_parser.h"
Minyue Li7f6417f2018-10-03 21:19:08 +020021#include "rtc_base/logging.h"
Yves Gerey988cc082018-10-23 12:03:01 +020022#include "rtc_base/numerics/safe_conversions.h"
Jakob Ivarsson46dda832019-07-03 16:00:30 +020023#include "system_wrappers/include/field_trial.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000024
Jakob Ivarsson74158ff2021-09-07 14:24:56 +020025namespace webrtc {
26
Minyue Li7f6417f2018-10-03 21:19:08 +020027namespace {
Minyue Li7f6417f2018-10-03 21:19:08 +020028
Jakob Ivarssond3a780b2019-02-28 14:30:21 +010029constexpr int kPostponeDecodingLevel = 50;
Jakob Ivarsson46dda832019-07-03 16:00:30 +020030constexpr int kDefaultTargetLevelWindowMs = 100;
Jakob Ivarsson80fb9782020-10-09 13:41:06 +020031constexpr int kDecelerationTargetLevelOffsetMs = 85;
Minyue Li7f6417f2018-10-03 21:19:08 +020032
Jakob Ivarsson74158ff2021-09-07 14:24:56 +020033std::unique_ptr<DelayManager> CreateDelayManager(
34 const NetEqController::Config& neteq_config) {
35 DelayManager::Config config;
36 config.max_packets_in_buffer = neteq_config.max_packets_in_buffer;
37 config.base_minimum_delay_ms = neteq_config.base_min_delay_ms;
38 config.Log();
39 return std::make_unique<DelayManager>(config, neteq_config.tick_timer);
40}
Minyue Li7f6417f2018-10-03 21:19:08 +020041
Jakob Ivarsson74158ff2021-09-07 14:24:56 +020042} // namespace
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000043
Ivo Creusen53a31f72019-10-24 15:20:39 +020044DecisionLogic::DecisionLogic(NetEqController::Config config)
Jakob Ivarsson609b0472020-10-19 09:19:34 +020045 : DecisionLogic(config,
Jakob Ivarsson74158ff2021-09-07 14:24:56 +020046 CreateDelayManager(config),
Jakob Ivarsson609b0472020-10-19 09:19:34 +020047 std::make_unique<BufferLevelFilter>()) {}
48
49DecisionLogic::DecisionLogic(
50 NetEqController::Config config,
51 std::unique_ptr<DelayManager> delay_manager,
52 std::unique_ptr<BufferLevelFilter> buffer_level_filter)
53 : delay_manager_(std::move(delay_manager)),
54 buffer_level_filter_(std::move(buffer_level_filter)),
Ivo Creusen53a31f72019-10-24 15:20:39 +020055 tick_timer_(config.tick_timer),
56 disallow_time_stretching_(!config.allow_time_stretching),
Henrik Lundin47b17dc2016-05-10 10:20:59 +020057 timescale_countdown_(
58 tick_timer_->GetNewCountdown(kMinTimescaleInterval + 1)),
Jakob Ivarssond723da12021-01-15 17:44:56 +010059 estimate_dtx_delay_("estimate_dtx_delay", true),
60 time_stretch_cn_("time_stretch_cn", true),
Jakob Ivarsson46dda832019-07-03 16:00:30 +020061 target_level_window_ms_("target_level_window",
62 kDefaultTargetLevelWindowMs,
63 0,
64 absl::nullopt) {
Jakob Ivarsson46dda832019-07-03 16:00:30 +020065 const std::string field_trial_name =
66 field_trial::FindFullName("WebRTC-Audio-NetEqDecisionLogicSettings");
67 ParseFieldTrial(
68 {&estimate_dtx_delay_, &time_stretch_cn_, &target_level_window_ms_},
69 field_trial_name);
70 RTC_LOG(LS_INFO) << "NetEq decision logic settings:"
Jonas Olssonb2b20312020-01-14 12:11:31 +010071 " estimate_dtx_delay="
72 << estimate_dtx_delay_
Jakob Ivarsson46dda832019-07-03 16:00:30 +020073 << " time_stretch_cn=" << time_stretch_cn_
74 << " target_level_window_ms=" << target_level_window_ms_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000075}
76
Henrik Lundin47b17dc2016-05-10 10:20:59 +020077DecisionLogic::~DecisionLogic() = default;
78
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000079void DecisionLogic::Reset() {
80 cng_state_ = kCngOff;
henrik.lundinb1fb72b2016-05-03 08:18:47 -070081 noise_fast_forward_ = 0;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000082 packet_length_samples_ = 0;
83 sample_memory_ = 0;
84 prev_time_scale_ = false;
Jakob Ivarsson80fb9782020-10-09 13:41:06 +020085 last_pack_cng_or_dtmf_ = true;
Henrik Lundin47b17dc2016-05-10 10:20:59 +020086 timescale_countdown_.reset();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000087 num_consecutive_expands_ = 0;
Jakob Ivarsson46dda832019-07-03 16:00:30 +020088 time_stretched_cn_samples_ = 0;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000089}
90
91void DecisionLogic::SoftReset() {
92 packet_length_samples_ = 0;
93 sample_memory_ = 0;
94 prev_time_scale_ = false;
Jakob Ivarsson80fb9782020-10-09 13:41:06 +020095 last_pack_cng_or_dtmf_ = true;
Henrik Lundin47b17dc2016-05-10 10:20:59 +020096 timescale_countdown_ =
97 tick_timer_->GetNewCountdown(kMinTimescaleInterval + 1);
Jakob Ivarsson46dda832019-07-03 16:00:30 +020098 time_stretched_cn_samples_ = 0;
Ivo Creusen53a31f72019-10-24 15:20:39 +020099 delay_manager_->Reset();
Jakob Ivarsson609b0472020-10-19 09:19:34 +0200100 buffer_level_filter_->Reset();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000101}
102
Peter Kastingdce40cf2015-08-24 14:52:23 -0700103void DecisionLogic::SetSampleRate(int fs_hz, size_t output_size_samples) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000104 // TODO(hlundin): Change to an enumerator and skip assert.
Mirko Bonadei25ab3222021-07-08 20:08:20 +0200105 RTC_DCHECK(fs_hz == 8000 || fs_hz == 16000 || fs_hz == 32000 ||
106 fs_hz == 48000);
Jakob Ivarsson46dda832019-07-03 16:00:30 +0200107 sample_rate_ = fs_hz;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000108 output_size_samples_ = output_size_samples;
109}
110
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100111NetEq::Operation DecisionLogic::GetDecision(const NetEqStatus& status,
112 bool* reset_decoder) {
ossu61a208b2016-09-20 01:38:00 -0700113 // If last mode was CNG (or Expand, since this could be covering up for
114 // a lost CNG packet), remember that CNG is on. This is needed if comfort
115 // noise is interrupted by DTMF.
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100116 if (status.last_mode == NetEq::Mode::kRfc3389Cng) {
ossu61a208b2016-09-20 01:38:00 -0700117 cng_state_ = kCngRfc3389On;
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100118 } else if (status.last_mode == NetEq::Mode::kCodecInternalCng) {
ossu61a208b2016-09-20 01:38:00 -0700119 cng_state_ = kCngInternalOn;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000120 }
121
Ivo Creusen53a31f72019-10-24 15:20:39 +0200122 size_t cur_size_samples = estimate_dtx_delay_
123 ? status.packet_buffer_info.span_samples
124 : status.packet_buffer_info.num_samples;
Yves Gerey665174f2018-06-19 15:03:05 +0200125 prev_time_scale_ =
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100126 prev_time_scale_ &&
127 (status.last_mode == NetEq::Mode::kAccelerateSuccess ||
128 status.last_mode == NetEq::Mode::kAccelerateLowEnergy ||
129 status.last_mode == NetEq::Mode::kPreemptiveExpandSuccess ||
130 status.last_mode == NetEq::Mode::kPreemptiveExpandLowEnergy);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000131
Minyue Li7d204d52019-04-16 11:44:49 +0200132 // Do not update buffer history if currently playing CNG since it will bias
133 // the filtered buffer level.
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100134 if (status.last_mode != NetEq::Mode::kRfc3389Cng &&
135 status.last_mode != NetEq::Mode::kCodecInternalCng &&
Ivo Creusen53a31f72019-10-24 15:20:39 +0200136 !(status.next_packet && status.next_packet->is_dtx &&
137 !estimate_dtx_delay_)) {
Minyue Li7d204d52019-04-16 11:44:49 +0200138 FilterBufferLevel(cur_size_samples);
139 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000140
Henrik Lundin7687ad52018-07-02 10:14:46 +0200141 // Guard for errors, to avoid getting stuck in error mode.
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100142 if (status.last_mode == NetEq::Mode::kError) {
Ivo Creusen53a31f72019-10-24 15:20:39 +0200143 if (!status.next_packet) {
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100144 return NetEq::Operation::kExpand;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200145 } else {
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100146 // Use kUndefined to flag for a reset.
147 return NetEq::Operation::kUndefined;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200148 }
149 }
150
Ivo Creusen53a31f72019-10-24 15:20:39 +0200151 if (status.next_packet && status.next_packet->is_cng) {
152 return CngOperation(status.last_mode, status.target_timestamp,
153 status.next_packet->timestamp,
154 status.generated_noise_samples);
Henrik Lundin7687ad52018-07-02 10:14:46 +0200155 }
156
157 // Handle the case with no packet at all available (except maybe DTMF).
Ivo Creusen53a31f72019-10-24 15:20:39 +0200158 if (!status.next_packet) {
159 return NoPacket(status.play_dtmf);
Henrik Lundin7687ad52018-07-02 10:14:46 +0200160 }
161
162 // If the expand period was very long, reset NetEQ since it is likely that the
163 // sender was restarted.
164 if (num_consecutive_expands_ > kReinitAfterExpands) {
165 *reset_decoder = true;
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100166 return NetEq::Operation::kNormal;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200167 }
168
169 // Make sure we don't restart audio too soon after an expansion to avoid
170 // running out of data right away again. We should only wait if there are no
171 // DTX or CNG packets in the buffer (otherwise we should just play out what we
172 // have, since we cannot know the exact duration of DTX or CNG packets), and
173 // if the mute factor is low enough (otherwise the expansion was short enough
174 // to not be noticable).
175 // Note that the MuteFactor is in Q14, so a value of 16384 corresponds to 1.
Ivo Creusen53a31f72019-10-24 15:20:39 +0200176 const size_t current_span =
177 estimate_dtx_delay_ ? status.packet_buffer_info.span_samples
178 : status.packet_buffer_info.span_samples_no_dtx;
Jakob Ivarsson80fb9782020-10-09 13:41:06 +0200179 const int target_level_samples =
180 delay_manager_->TargetDelayMs() * sample_rate_ / 1000;
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100181 if ((status.last_mode == NetEq::Mode::kExpand ||
182 status.last_mode == NetEq::Mode::kCodecPlc) &&
Ivo Creusen53a31f72019-10-24 15:20:39 +0200183 status.expand_mutefactor < 16384 / 2 &&
Jakob Ivarsson80fb9782020-10-09 13:41:06 +0200184 current_span < static_cast<size_t>(target_level_samples *
185 kPostponeDecodingLevel / 100) &&
Ivo Creusen53a31f72019-10-24 15:20:39 +0200186 !status.packet_buffer_info.dtx_or_cng) {
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100187 return NetEq::Operation::kExpand;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200188 }
189
Jakob Ivarsson46dda832019-07-03 16:00:30 +0200190 const uint32_t five_seconds_samples = static_cast<uint32_t>(5 * sample_rate_);
Henrik Lundin7687ad52018-07-02 10:14:46 +0200191 // Check if the required packet is available.
Ivo Creusen53a31f72019-10-24 15:20:39 +0200192 if (status.target_timestamp == status.next_packet->timestamp) {
193 return ExpectedPacketAvailable(status.last_mode, status.play_dtmf);
194 } else if (!PacketBuffer::IsObsoleteTimestamp(status.next_packet->timestamp,
195 status.target_timestamp,
196 five_seconds_samples)) {
197 return FuturePacketAvailable(
198 status.last_packet_samples, status.last_mode, status.target_timestamp,
199 status.next_packet->timestamp, status.play_dtmf,
200 status.generated_noise_samples, status.packet_buffer_info.span_samples,
201 status.packet_buffer_info.num_packets);
Henrik Lundin7687ad52018-07-02 10:14:46 +0200202 } else {
203 // This implies that available_timestamp < target_timestamp, which can
204 // happen when a new stream or codec is received. Signal for a reset.
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100205 return NetEq::Operation::kUndefined;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200206 }
207}
208
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100209void DecisionLogic::ExpandDecision(NetEq::Operation operation) {
210 if (operation == NetEq::Operation::kExpand) {
Henrik Lundin5afa61c2018-07-02 14:53:24 +0200211 num_consecutive_expands_++;
212 } else {
213 num_consecutive_expands_ = 0;
214 }
215}
216
Ivo Creusena2b31c32020-10-14 17:54:22 +0200217absl::optional<int> DecisionLogic::PacketArrived(
218 int fs_hz,
219 bool should_update_stats,
220 const PacketArrivedInfo& info) {
Ivo Creusen7b463c52020-11-25 11:32:40 +0100221 buffer_flush_ = buffer_flush_ || info.buffer_flush;
Ivo Creusena2b31c32020-10-14 17:54:22 +0200222 if (info.is_cng_or_dtmf) {
Jakob Ivarsson80fb9782020-10-09 13:41:06 +0200223 last_pack_cng_or_dtmf_ = true;
224 return absl::nullopt;
Ivo Creusen53a31f72019-10-24 15:20:39 +0200225 }
Jakob Ivarsson80fb9782020-10-09 13:41:06 +0200226 if (!should_update_stats) {
227 return absl::nullopt;
228 }
Ivo Creusena2b31c32020-10-14 17:54:22 +0200229 if (info.packet_length_samples > 0 && fs_hz > 0 &&
230 info.packet_length_samples != packet_length_samples_) {
231 packet_length_samples_ = info.packet_length_samples;
Jakob Ivarsson80fb9782020-10-09 13:41:06 +0200232 delay_manager_->SetPacketAudioLength(packet_length_samples_ * 1000 / fs_hz);
233 }
234 auto relative_delay = delay_manager_->Update(
Ivo Creusena2b31c32020-10-14 17:54:22 +0200235 info.main_timestamp, fs_hz, /*reset=*/last_pack_cng_or_dtmf_);
Jakob Ivarsson80fb9782020-10-09 13:41:06 +0200236 last_pack_cng_or_dtmf_ = false;
Ivo Creusen53a31f72019-10-24 15:20:39 +0200237 return relative_delay;
238}
239
Minyue Li7d204d52019-04-16 11:44:49 +0200240void DecisionLogic::FilterBufferLevel(size_t buffer_size_samples) {
Jakob Ivarsson609b0472020-10-19 09:19:34 +0200241 buffer_level_filter_->SetTargetBufferLevel(delay_manager_->TargetDelayMs());
Henrik Lundin5afa61c2018-07-02 14:53:24 +0200242
Jakob Ivarsson46dda832019-07-03 16:00:30 +0200243 int time_stretched_samples = time_stretched_cn_samples_;
Minyue Li7d204d52019-04-16 11:44:49 +0200244 if (prev_time_scale_) {
Jakob Ivarsson46dda832019-07-03 16:00:30 +0200245 time_stretched_samples += sample_memory_;
Minyue Li7d204d52019-04-16 11:44:49 +0200246 timescale_countdown_ = tick_timer_->GetNewCountdown(kMinTimescaleInterval);
247 }
248
Ivo Creusen7b463c52020-11-25 11:32:40 +0100249 if (buffer_flush_) {
250 buffer_level_filter_->SetFilteredBufferLevel(buffer_size_samples);
251 buffer_flush_ = false;
252 } else {
253 buffer_level_filter_->Update(buffer_size_samples, time_stretched_samples);
254 }
Minyue Li7d204d52019-04-16 11:44:49 +0200255 prev_time_scale_ = false;
Jakob Ivarsson46dda832019-07-03 16:00:30 +0200256 time_stretched_cn_samples_ = 0;
Henrik Lundin5afa61c2018-07-02 14:53:24 +0200257}
258
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100259NetEq::Operation DecisionLogic::CngOperation(NetEq::Mode prev_mode,
260 uint32_t target_timestamp,
261 uint32_t available_timestamp,
262 size_t generated_noise_samples) {
Henrik Lundin7687ad52018-07-02 10:14:46 +0200263 // Signed difference between target and available timestamp.
264 int32_t timestamp_diff = static_cast<int32_t>(
265 static_cast<uint32_t>(generated_noise_samples + target_timestamp) -
266 available_timestamp);
Jakob Ivarsson80fb9782020-10-09 13:41:06 +0200267 int optimal_level_samp =
268 delay_manager_->TargetDelayMs() * sample_rate_ / 1000;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200269 const int64_t excess_waiting_time_samp =
270 -static_cast<int64_t>(timestamp_diff) - optimal_level_samp;
271
272 if (excess_waiting_time_samp > optimal_level_samp / 2) {
273 // The waiting time for this packet will be longer than 1.5
274 // times the wanted buffer delay. Apply fast-forward to cut the
275 // waiting time down to the optimal.
Jakob Ivarsson42b6e2d2019-10-21 11:51:05 +0200276 noise_fast_forward_ = rtc::saturated_cast<size_t>(noise_fast_forward_ +
277 excess_waiting_time_samp);
Henrik Lundin7687ad52018-07-02 10:14:46 +0200278 timestamp_diff =
279 rtc::saturated_cast<int32_t>(timestamp_diff + excess_waiting_time_samp);
280 }
281
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100282 if (timestamp_diff < 0 && prev_mode == NetEq::Mode::kRfc3389Cng) {
Henrik Lundin7687ad52018-07-02 10:14:46 +0200283 // Not time to play this packet yet. Wait another round before using this
284 // packet. Keep on playing CNG from previous CNG parameters.
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100285 return NetEq::Operation::kRfc3389CngNoPacket;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200286 } else {
287 // Otherwise, go for the CNG packet now.
288 noise_fast_forward_ = 0;
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100289 return NetEq::Operation::kRfc3389Cng;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200290 }
291}
292
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100293NetEq::Operation DecisionLogic::NoPacket(bool play_dtmf) {
Henrik Lundin7687ad52018-07-02 10:14:46 +0200294 if (cng_state_ == kCngRfc3389On) {
295 // Keep on playing comfort noise.
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100296 return NetEq::Operation::kRfc3389CngNoPacket;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200297 } else if (cng_state_ == kCngInternalOn) {
298 // Keep on playing codec internal comfort noise.
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100299 return NetEq::Operation::kCodecInternalCng;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200300 } else if (play_dtmf) {
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100301 return NetEq::Operation::kDtmf;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200302 } else {
303 // Nothing to play, do expand.
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100304 return NetEq::Operation::kExpand;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200305 }
306}
307
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100308NetEq::Operation DecisionLogic::ExpectedPacketAvailable(NetEq::Mode prev_mode,
309 bool play_dtmf) {
310 if (!disallow_time_stretching_ && prev_mode != NetEq::Mode::kExpand &&
311 !play_dtmf) {
Jakob Ivarsson80fb9782020-10-09 13:41:06 +0200312 const int samples_per_ms = sample_rate_ / 1000;
313 const int target_level_samples =
314 delay_manager_->TargetDelayMs() * samples_per_ms;
315 const int low_limit =
316 std::max(target_level_samples * 3 / 4,
317 target_level_samples -
318 kDecelerationTargetLevelOffsetMs * samples_per_ms);
Artem Titovd00ce742021-07-28 20:00:17 +0200319 // `higher_limit` is equal to `target_level`, but should at
320 // least be 20 ms higher than `lower_limit`.
Jakob Ivarsson80fb9782020-10-09 13:41:06 +0200321 const int high_limit =
322 std::max(target_level_samples, low_limit + 20 * samples_per_ms);
323
324 const int buffer_level_samples =
Jakob Ivarsson609b0472020-10-19 09:19:34 +0200325 buffer_level_filter_->filtered_current_level();
Jakob Ivarsson80fb9782020-10-09 13:41:06 +0200326 if (buffer_level_samples >= high_limit << 2)
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100327 return NetEq::Operation::kFastAccelerate;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200328 if (TimescaleAllowed()) {
Jakob Ivarsson80fb9782020-10-09 13:41:06 +0200329 if (buffer_level_samples >= high_limit)
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100330 return NetEq::Operation::kAccelerate;
Jakob Ivarsson80fb9782020-10-09 13:41:06 +0200331 if (buffer_level_samples < low_limit)
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100332 return NetEq::Operation::kPreemptiveExpand;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200333 }
334 }
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100335 return NetEq::Operation::kNormal;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200336}
337
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100338NetEq::Operation DecisionLogic::FuturePacketAvailable(
Henrik Lundin7687ad52018-07-02 10:14:46 +0200339 size_t decoder_frame_length,
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100340 NetEq::Mode prev_mode,
Henrik Lundin7687ad52018-07-02 10:14:46 +0200341 uint32_t target_timestamp,
342 uint32_t available_timestamp,
343 bool play_dtmf,
Ivo Creusen53a31f72019-10-24 15:20:39 +0200344 size_t generated_noise_samples,
345 size_t span_samples_in_packet_buffer,
346 size_t num_packets_in_packet_buffer) {
Henrik Lundin7687ad52018-07-02 10:14:46 +0200347 // Required packet is not available, but a future packet is.
348 // Check if we should continue with an ongoing expand because the new packet
349 // is too far into the future.
350 uint32_t timestamp_leap = available_timestamp - target_timestamp;
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100351 if ((prev_mode == NetEq::Mode::kExpand ||
352 prev_mode == NetEq::Mode::kCodecPlc) &&
Henrik Lundin00eb12a2018-09-05 18:14:52 +0200353 !ReinitAfterExpands(timestamp_leap) && !MaxWaitForPacket() &&
354 PacketTooEarly(timestamp_leap) && UnderTargetLevel()) {
Henrik Lundin7687ad52018-07-02 10:14:46 +0200355 if (play_dtmf) {
356 // Still have DTMF to play, so do not do expand.
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100357 return NetEq::Operation::kDtmf;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200358 } else {
359 // Nothing to play.
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100360 return NetEq::Operation::kExpand;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200361 }
362 }
363
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100364 if (prev_mode == NetEq::Mode::kCodecPlc) {
365 return NetEq::Operation::kNormal;
Henrik Lundin00eb12a2018-09-05 18:14:52 +0200366 }
367
Henrik Lundin7687ad52018-07-02 10:14:46 +0200368 // If previous was comfort noise, then no merge is needed.
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100369 if (prev_mode == NetEq::Mode::kRfc3389Cng ||
370 prev_mode == NetEq::Mode::kCodecInternalCng) {
Jakob Ivarsson46dda832019-07-03 16:00:30 +0200371 size_t cur_size_samples =
372 estimate_dtx_delay_
Jakob Ivarsson80fb9782020-10-09 13:41:06 +0200373 ? span_samples_in_packet_buffer
Ivo Creusen53a31f72019-10-24 15:20:39 +0200374 : num_packets_in_packet_buffer * decoder_frame_length;
Jakob Ivarsson46dda832019-07-03 16:00:30 +0200375 // Target level is in number of packets in Q8.
376 const size_t target_level_samples =
Jakob Ivarsson80fb9782020-10-09 13:41:06 +0200377 delay_manager_->TargetDelayMs() * sample_rate_ / 1000;
Jakob Ivarsson46dda832019-07-03 16:00:30 +0200378 const bool generated_enough_noise =
379 static_cast<uint32_t>(generated_noise_samples + target_timestamp) >=
380 available_timestamp;
381
382 if (time_stretch_cn_) {
383 const size_t target_threshold_samples =
384 target_level_window_ms_ / 2 * (sample_rate_ / 1000);
385 const bool above_target_window =
386 cur_size_samples > target_level_samples + target_threshold_samples;
387 const bool below_target_window =
388 target_level_samples > target_threshold_samples &&
389 cur_size_samples < target_level_samples - target_threshold_samples;
390 // Keep the delay same as before CNG, but make sure that it is within the
391 // target window.
392 if ((generated_enough_noise && !below_target_window) ||
393 above_target_window) {
394 time_stretched_cn_samples_ = timestamp_leap - generated_noise_samples;
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100395 return NetEq::Operation::kNormal;
Jakob Ivarsson46dda832019-07-03 16:00:30 +0200396 }
Henrik Lundin7687ad52018-07-02 10:14:46 +0200397 } else {
Jakob Ivarsson46dda832019-07-03 16:00:30 +0200398 // Keep the same delay as before the CNG, but make sure that the number of
399 // samples in buffer is no higher than 4 times the optimal level.
400 if (generated_enough_noise ||
401 cur_size_samples > target_level_samples * 4) {
402 // Time to play this new packet.
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100403 return NetEq::Operation::kNormal;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200404 }
405 }
Jakob Ivarsson46dda832019-07-03 16:00:30 +0200406
407 // Too early to play this new packet; keep on playing comfort noise.
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100408 if (prev_mode == NetEq::Mode::kRfc3389Cng) {
409 return NetEq::Operation::kRfc3389CngNoPacket;
Jakob Ivarsson46dda832019-07-03 16:00:30 +0200410 }
411 // prevPlayMode == kModeCodecInternalCng.
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100412 return NetEq::Operation::kCodecInternalCng;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200413 }
Jakob Ivarsson46dda832019-07-03 16:00:30 +0200414
Henrik Lundin7687ad52018-07-02 10:14:46 +0200415 // Do not merge unless we have done an expand before.
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100416 if (prev_mode == NetEq::Mode::kExpand) {
417 return NetEq::Operation::kMerge;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200418 } else if (play_dtmf) {
419 // Play DTMF instead of expand.
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100420 return NetEq::Operation::kDtmf;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200421 } else {
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100422 return NetEq::Operation::kExpand;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200423 }
424}
425
426bool DecisionLogic::UnderTargetLevel() const {
Jakob Ivarsson609b0472020-10-19 09:19:34 +0200427 return buffer_level_filter_->filtered_current_level() <
Jakob Ivarsson80fb9782020-10-09 13:41:06 +0200428 delay_manager_->TargetDelayMs() * sample_rate_ / 1000;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200429}
430
431bool DecisionLogic::ReinitAfterExpands(uint32_t timestamp_leap) const {
432 return timestamp_leap >=
433 static_cast<uint32_t>(output_size_samples_ * kReinitAfterExpands);
434}
435
436bool DecisionLogic::PacketTooEarly(uint32_t timestamp_leap) const {
437 return timestamp_leap >
438 static_cast<uint32_t>(output_size_samples_ * num_consecutive_expands_);
439}
440
441bool DecisionLogic::MaxWaitForPacket() const {
442 return num_consecutive_expands_ >= kMaxWaitForPacket;
443}
444
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000445} // namespace webrtc