blob: 3a656a7f43ee2c19d41dd9624837665759e53c75 [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
Jakob Ivarsson01ab7d52022-05-25 21:06:14 +020015#include <cstdint>
Jakob Ivarssonc782cf82022-05-16 15:28:22 +020016#include <memory>
Yves Gerey988cc082018-10-23 12:03:01 +020017#include <string>
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000018
Jakob Ivarsson46dda832019-07-03 16:00:30 +020019#include "absl/types/optional.h"
Jakob Ivarssonc782cf82022-05-16 15:28:22 +020020#include "api/neteq/neteq.h"
21#include "api/neteq/neteq_controller.h"
22#include "modules/audio_coding/neteq/packet_arrival_history.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020023#include "modules/audio_coding/neteq/packet_buffer.h"
Yves Gerey988cc082018-10-23 12:03:01 +020024#include "rtc_base/checks.h"
Jakob Ivarsson46dda832019-07-03 16:00:30 +020025#include "rtc_base/experiments/field_trial_parser.h"
Jakob Ivarssonc782cf82022-05-16 15:28:22 +020026#include "rtc_base/experiments/struct_parameters_parser.h"
Minyue Li7f6417f2018-10-03 21:19:08 +020027#include "rtc_base/logging.h"
Yves Gerey988cc082018-10-23 12:03:01 +020028#include "rtc_base/numerics/safe_conversions.h"
Jakob Ivarsson46dda832019-07-03 16:00:30 +020029#include "system_wrappers/include/field_trial.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000030
Jakob Ivarsson74158ff2021-09-07 14:24:56 +020031namespace webrtc {
32
Minyue Li7f6417f2018-10-03 21:19:08 +020033namespace {
Minyue Li7f6417f2018-10-03 21:19:08 +020034
Jakob Ivarssond3a780b2019-02-28 14:30:21 +010035constexpr int kPostponeDecodingLevel = 50;
Jakob Ivarssonc782cf82022-05-16 15:28:22 +020036constexpr int kTargetLevelWindowMs = 100;
37constexpr int kMaxWaitForPacketTicks = 10;
38// The granularity of delay adjustments (accelerate/preemptive expand) is 15ms,
39// but round up since the clock has a granularity of 10ms.
40constexpr int kDelayAdjustmentGranularityMs = 20;
Minyue Li7f6417f2018-10-03 21:19:08 +020041
Jakob Ivarsson74158ff2021-09-07 14:24:56 +020042std::unique_ptr<DelayManager> CreateDelayManager(
43 const NetEqController::Config& neteq_config) {
44 DelayManager::Config config;
45 config.max_packets_in_buffer = neteq_config.max_packets_in_buffer;
46 config.base_minimum_delay_ms = neteq_config.base_min_delay_ms;
47 config.Log();
48 return std::make_unique<DelayManager>(config, neteq_config.tick_timer);
49}
Minyue Li7f6417f2018-10-03 21:19:08 +020050
Jakob Ivarssonc782cf82022-05-16 15:28:22 +020051bool IsTimestretch(NetEq::Mode mode) {
52 return mode == NetEq::Mode::kAccelerateSuccess ||
53 mode == NetEq::Mode::kAccelerateLowEnergy ||
54 mode == NetEq::Mode::kPreemptiveExpandSuccess ||
55 mode == NetEq::Mode::kPreemptiveExpandLowEnergy;
56}
57
58bool IsCng(NetEq::Mode mode) {
59 return mode == NetEq::Mode::kRfc3389Cng ||
60 mode == NetEq::Mode::kCodecInternalCng;
61}
62
Jakob Ivarssonca101e62022-04-04 21:42:55 +020063bool IsExpand(NetEq::Mode mode) {
64 return mode == NetEq::Mode::kExpand || mode == NetEq::Mode::kCodecPlc;
65}
66
Jakob Ivarsson74158ff2021-09-07 14:24:56 +020067} // namespace
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000068
Jakob Ivarssonc782cf82022-05-16 15:28:22 +020069DecisionLogic::Config::Config() {
70 StructParametersParser::Create(
71 "enable_stable_playout_delay", &enable_stable_playout_delay, //
72 "reinit_after_expands", &reinit_after_expands, //
73 "packet_history_size_ms", &packet_history_size_ms, //
74 "deceleration_target_level_offset_ms",
75 &deceleration_target_level_offset_ms)
76 ->Parse(webrtc::field_trial::FindFullName(
77 "WebRTC-Audio-NetEqDecisionLogicConfig"));
78 RTC_LOG(LS_INFO) << "NetEq decision logic config:"
79 << " enable_stable_playout_delay="
80 << enable_stable_playout_delay
81 << " reinit_after_expands=" << reinit_after_expands
82 << " packet_history_size_ms=" << packet_history_size_ms
83 << " deceleration_target_level_offset_ms="
84 << deceleration_target_level_offset_ms;
85}
86
Ivo Creusen53a31f72019-10-24 15:20:39 +020087DecisionLogic::DecisionLogic(NetEqController::Config config)
Jakob Ivarsson609b0472020-10-19 09:19:34 +020088 : DecisionLogic(config,
Jakob Ivarsson74158ff2021-09-07 14:24:56 +020089 CreateDelayManager(config),
Jakob Ivarsson609b0472020-10-19 09:19:34 +020090 std::make_unique<BufferLevelFilter>()) {}
91
92DecisionLogic::DecisionLogic(
93 NetEqController::Config config,
94 std::unique_ptr<DelayManager> delay_manager,
95 std::unique_ptr<BufferLevelFilter> buffer_level_filter)
96 : delay_manager_(std::move(delay_manager)),
97 buffer_level_filter_(std::move(buffer_level_filter)),
Jakob Ivarssonc782cf82022-05-16 15:28:22 +020098 packet_arrival_history_(config_.packet_history_size_ms),
Ivo Creusen53a31f72019-10-24 15:20:39 +020099 tick_timer_(config.tick_timer),
100 disallow_time_stretching_(!config.allow_time_stretching),
Henrik Lundin47b17dc2016-05-10 10:20:59 +0200101 timescale_countdown_(
Jakob Ivarssonc782cf82022-05-16 15:28:22 +0200102 tick_timer_->GetNewCountdown(kMinTimescaleInterval + 1)) {}
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000103
Henrik Lundin47b17dc2016-05-10 10:20:59 +0200104DecisionLogic::~DecisionLogic() = default;
105
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000106void DecisionLogic::SoftReset() {
107 packet_length_samples_ = 0;
108 sample_memory_ = 0;
109 prev_time_scale_ = false;
Henrik Lundin47b17dc2016-05-10 10:20:59 +0200110 timescale_countdown_ =
111 tick_timer_->GetNewCountdown(kMinTimescaleInterval + 1);
Jakob Ivarsson46dda832019-07-03 16:00:30 +0200112 time_stretched_cn_samples_ = 0;
Ivo Creusen53a31f72019-10-24 15:20:39 +0200113 delay_manager_->Reset();
Jakob Ivarsson609b0472020-10-19 09:19:34 +0200114 buffer_level_filter_->Reset();
Jakob Ivarssonc782cf82022-05-16 15:28:22 +0200115 packet_arrival_history_.Reset();
116 last_playout_delay_ms_ = 0;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000117}
118
Peter Kastingdce40cf2015-08-24 14:52:23 -0700119void DecisionLogic::SetSampleRate(int fs_hz, size_t output_size_samples) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000120 // TODO(hlundin): Change to an enumerator and skip assert.
Mirko Bonadei25ab3222021-07-08 20:08:20 +0200121 RTC_DCHECK(fs_hz == 8000 || fs_hz == 16000 || fs_hz == 32000 ||
122 fs_hz == 48000);
Jakob Ivarssonc782cf82022-05-16 15:28:22 +0200123 sample_rate_khz_ = fs_hz / 1000;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000124 output_size_samples_ = output_size_samples;
Jakob Ivarssonc782cf82022-05-16 15:28:22 +0200125 packet_arrival_history_.set_sample_rate(fs_hz);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000126}
127
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100128NetEq::Operation DecisionLogic::GetDecision(const NetEqStatus& status,
129 bool* reset_decoder) {
ossu61a208b2016-09-20 01:38:00 -0700130 // If last mode was CNG (or Expand, since this could be covering up for
131 // a lost CNG packet), remember that CNG is on. This is needed if comfort
132 // noise is interrupted by DTMF.
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100133 if (status.last_mode == NetEq::Mode::kRfc3389Cng) {
ossu61a208b2016-09-20 01:38:00 -0700134 cng_state_ = kCngRfc3389On;
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100135 } else if (status.last_mode == NetEq::Mode::kCodecInternalCng) {
ossu61a208b2016-09-20 01:38:00 -0700136 cng_state_ = kCngInternalOn;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000137 }
138
Jakob Ivarssonca101e62022-04-04 21:42:55 +0200139 if (IsExpand(status.last_mode)) {
140 ++num_consecutive_expands_;
141 } else {
142 num_consecutive_expands_ = 0;
143 }
144
Jakob Ivarssonc782cf82022-05-16 15:28:22 +0200145 if (!IsExpand(status.last_mode) && !IsCng(status.last_mode)) {
146 last_playout_delay_ms_ = GetPlayoutDelayMs(status);
147 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000148
Jakob Ivarssonc782cf82022-05-16 15:28:22 +0200149 prev_time_scale_ = prev_time_scale_ && IsTimestretch(status.last_mode);
150 if (prev_time_scale_) {
151 timescale_countdown_ = tick_timer_->GetNewCountdown(kMinTimescaleInterval);
152 }
153 if (!IsCng(status.last_mode)) {
Jakob Ivarssone6aabd02022-03-28 16:06:37 +0200154 FilterBufferLevel(status.packet_buffer_info.span_samples);
Minyue Li7d204d52019-04-16 11:44:49 +0200155 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000156
Henrik Lundin7687ad52018-07-02 10:14:46 +0200157 // Guard for errors, to avoid getting stuck in error mode.
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100158 if (status.last_mode == NetEq::Mode::kError) {
Ivo Creusen53a31f72019-10-24 15:20:39 +0200159 if (!status.next_packet) {
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100160 return NetEq::Operation::kExpand;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200161 } else {
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100162 // Use kUndefined to flag for a reset.
163 return NetEq::Operation::kUndefined;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200164 }
165 }
166
Ivo Creusen53a31f72019-10-24 15:20:39 +0200167 if (status.next_packet && status.next_packet->is_cng) {
Jakob Ivarssonc782cf82022-05-16 15:28:22 +0200168 return CngOperation(status);
Henrik Lundin7687ad52018-07-02 10:14:46 +0200169 }
170
171 // Handle the case with no packet at all available (except maybe DTMF).
Ivo Creusen53a31f72019-10-24 15:20:39 +0200172 if (!status.next_packet) {
Jakob Ivarssonc782cf82022-05-16 15:28:22 +0200173 return NoPacket(status);
Henrik Lundin7687ad52018-07-02 10:14:46 +0200174 }
175
176 // If the expand period was very long, reset NetEQ since it is likely that the
177 // sender was restarted.
Jakob Ivarssonc782cf82022-05-16 15:28:22 +0200178 if (num_consecutive_expands_ > config_.reinit_after_expands) {
Henrik Lundin7687ad52018-07-02 10:14:46 +0200179 *reset_decoder = true;
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100180 return NetEq::Operation::kNormal;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200181 }
182
183 // Make sure we don't restart audio too soon after an expansion to avoid
184 // running out of data right away again. We should only wait if there are no
185 // DTX or CNG packets in the buffer (otherwise we should just play out what we
186 // have, since we cannot know the exact duration of DTX or CNG packets), and
187 // if the mute factor is low enough (otherwise the expansion was short enough
188 // to not be noticable).
189 // Note that the MuteFactor is in Q14, so a value of 16384 corresponds to 1.
Jakob Ivarssonc782cf82022-05-16 15:28:22 +0200190 const int target_level_samples = TargetLevelMs() * sample_rate_khz_;
191 if (!config_.enable_stable_playout_delay && IsExpand(status.last_mode) &&
192 status.expand_mutefactor < 16384 / 2 &&
Jakob Ivarssone6aabd02022-03-28 16:06:37 +0200193 status.packet_buffer_info.span_samples <
194 static_cast<size_t>(target_level_samples * kPostponeDecodingLevel /
195 100) &&
Ivo Creusen53a31f72019-10-24 15:20:39 +0200196 !status.packet_buffer_info.dtx_or_cng) {
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100197 return NetEq::Operation::kExpand;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200198 }
199
Jakob Ivarssonc782cf82022-05-16 15:28:22 +0200200 const uint32_t five_seconds_samples =
201 static_cast<uint32_t>(5000 * sample_rate_khz_);
Henrik Lundin7687ad52018-07-02 10:14:46 +0200202 // Check if the required packet is available.
Ivo Creusen53a31f72019-10-24 15:20:39 +0200203 if (status.target_timestamp == status.next_packet->timestamp) {
Jakob Ivarssonc782cf82022-05-16 15:28:22 +0200204 return ExpectedPacketAvailable(status);
Henrik Lundin7687ad52018-07-02 10:14:46 +0200205 }
Jakob Ivarssonc782cf82022-05-16 15:28:22 +0200206 if (!PacketBuffer::IsObsoleteTimestamp(status.next_packet->timestamp,
207 status.target_timestamp,
208 five_seconds_samples)) {
209 return FuturePacketAvailable(status);
210 }
211 // This implies that available_timestamp < target_timestamp, which can
212 // happen when a new stream or codec is received. Signal for a reset.
213 return NetEq::Operation::kUndefined;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200214}
215
Jakob Ivarssonca101e62022-04-04 21:42:55 +0200216void DecisionLogic::NotifyMutedState() {
217 ++num_consecutive_expands_;
Henrik Lundin5afa61c2018-07-02 14:53:24 +0200218}
219
Jakob Ivarssonc782cf82022-05-16 15:28:22 +0200220int DecisionLogic::TargetLevelMs() const {
221 int target_delay_ms = delay_manager_->TargetDelayMs();
222 if (!config_.enable_stable_playout_delay) {
223 target_delay_ms =
224 std::max(target_delay_ms,
225 static_cast<int>(packet_length_samples_ / sample_rate_khz_));
226 }
227 return target_delay_ms;
228}
229
230int DecisionLogic::GetFilteredBufferLevel() const {
231 if (config_.enable_stable_playout_delay) {
232 return last_playout_delay_ms_ * sample_rate_khz_;
233 }
234 return buffer_level_filter_->filtered_current_level();
235}
236
Ivo Creusena2b31c32020-10-14 17:54:22 +0200237absl::optional<int> DecisionLogic::PacketArrived(
238 int fs_hz,
239 bool should_update_stats,
240 const PacketArrivedInfo& info) {
Ivo Creusen7b463c52020-11-25 11:32:40 +0100241 buffer_flush_ = buffer_flush_ || info.buffer_flush;
Jakob Ivarsson01ab7d52022-05-25 21:06:14 +0200242 if (!should_update_stats || info.is_cng_or_dtmf) {
Jakob Ivarsson80fb9782020-10-09 13:41:06 +0200243 return absl::nullopt;
244 }
Ivo Creusena2b31c32020-10-14 17:54:22 +0200245 if (info.packet_length_samples > 0 && fs_hz > 0 &&
246 info.packet_length_samples != packet_length_samples_) {
247 packet_length_samples_ = info.packet_length_samples;
Jakob Ivarsson80fb9782020-10-09 13:41:06 +0200248 delay_manager_->SetPacketAudioLength(packet_length_samples_ * 1000 / fs_hz);
249 }
Jakob Ivarsson01ab7d52022-05-25 21:06:14 +0200250 int64_t time_now_ms = tick_timer_->ticks() * tick_timer_->ms_per_tick();
251 packet_arrival_history_.Insert(info.main_timestamp, time_now_ms);
252 if (packet_arrival_history_.size() < 2) {
253 // No meaningful delay estimate unless at least 2 packets have arrived.
254 return absl::nullopt;
255 }
256 int arrival_delay_ms =
257 packet_arrival_history_.GetDelayMs(info.main_timestamp, time_now_ms);
258 bool reordered =
259 !packet_arrival_history_.IsNewestRtpTimestamp(info.main_timestamp);
260 delay_manager_->Update(arrival_delay_ms, reordered);
261 return arrival_delay_ms;
Ivo Creusen53a31f72019-10-24 15:20:39 +0200262}
263
Minyue Li7d204d52019-04-16 11:44:49 +0200264void DecisionLogic::FilterBufferLevel(size_t buffer_size_samples) {
Jakob Ivarssonc782cf82022-05-16 15:28:22 +0200265 buffer_level_filter_->SetTargetBufferLevel(TargetLevelMs());
Henrik Lundin5afa61c2018-07-02 14:53:24 +0200266
Jakob Ivarsson46dda832019-07-03 16:00:30 +0200267 int time_stretched_samples = time_stretched_cn_samples_;
Minyue Li7d204d52019-04-16 11:44:49 +0200268 if (prev_time_scale_) {
Jakob Ivarsson46dda832019-07-03 16:00:30 +0200269 time_stretched_samples += sample_memory_;
Minyue Li7d204d52019-04-16 11:44:49 +0200270 }
271
Ivo Creusen7b463c52020-11-25 11:32:40 +0100272 if (buffer_flush_) {
273 buffer_level_filter_->SetFilteredBufferLevel(buffer_size_samples);
274 buffer_flush_ = false;
275 } else {
276 buffer_level_filter_->Update(buffer_size_samples, time_stretched_samples);
277 }
Minyue Li7d204d52019-04-16 11:44:49 +0200278 prev_time_scale_ = false;
Jakob Ivarsson46dda832019-07-03 16:00:30 +0200279 time_stretched_cn_samples_ = 0;
Henrik Lundin5afa61c2018-07-02 14:53:24 +0200280}
281
Jakob Ivarssonc782cf82022-05-16 15:28:22 +0200282NetEq::Operation DecisionLogic::CngOperation(
283 NetEqController::NetEqStatus status) {
Henrik Lundin7687ad52018-07-02 10:14:46 +0200284 // Signed difference between target and available timestamp.
285 int32_t timestamp_diff = static_cast<int32_t>(
Jakob Ivarssonc782cf82022-05-16 15:28:22 +0200286 static_cast<uint32_t>(status.generated_noise_samples +
287 status.target_timestamp) -
288 status.next_packet->timestamp);
289 int optimal_level_samp = TargetLevelMs() * sample_rate_khz_;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200290 const int64_t excess_waiting_time_samp =
291 -static_cast<int64_t>(timestamp_diff) - optimal_level_samp;
292
293 if (excess_waiting_time_samp > optimal_level_samp / 2) {
294 // The waiting time for this packet will be longer than 1.5
295 // times the wanted buffer delay. Apply fast-forward to cut the
296 // waiting time down to the optimal.
Jakob Ivarsson42b6e2d2019-10-21 11:51:05 +0200297 noise_fast_forward_ = rtc::saturated_cast<size_t>(noise_fast_forward_ +
298 excess_waiting_time_samp);
Henrik Lundin7687ad52018-07-02 10:14:46 +0200299 timestamp_diff =
300 rtc::saturated_cast<int32_t>(timestamp_diff + excess_waiting_time_samp);
301 }
302
Jakob Ivarssonc782cf82022-05-16 15:28:22 +0200303 if (timestamp_diff < 0 && status.last_mode == NetEq::Mode::kRfc3389Cng) {
Henrik Lundin7687ad52018-07-02 10:14:46 +0200304 // Not time to play this packet yet. Wait another round before using this
305 // packet. Keep on playing CNG from previous CNG parameters.
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100306 return NetEq::Operation::kRfc3389CngNoPacket;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200307 } else {
308 // Otherwise, go for the CNG packet now.
309 noise_fast_forward_ = 0;
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100310 return NetEq::Operation::kRfc3389Cng;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200311 }
312}
313
Jakob Ivarssonc782cf82022-05-16 15:28:22 +0200314NetEq::Operation DecisionLogic::NoPacket(NetEqController::NetEqStatus status) {
Henrik Lundin7687ad52018-07-02 10:14:46 +0200315 if (cng_state_ == kCngRfc3389On) {
316 // Keep on playing comfort noise.
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100317 return NetEq::Operation::kRfc3389CngNoPacket;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200318 } else if (cng_state_ == kCngInternalOn) {
319 // Keep on playing codec internal comfort noise.
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100320 return NetEq::Operation::kCodecInternalCng;
Jakob Ivarssonc782cf82022-05-16 15:28:22 +0200321 } else if (status.play_dtmf) {
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100322 return NetEq::Operation::kDtmf;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200323 } else {
324 // Nothing to play, do expand.
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100325 return NetEq::Operation::kExpand;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200326 }
327}
328
Jakob Ivarssonc782cf82022-05-16 15:28:22 +0200329NetEq::Operation DecisionLogic::ExpectedPacketAvailable(
330 NetEqController::NetEqStatus status) {
331 if (!disallow_time_stretching_ && status.last_mode != NetEq::Mode::kExpand &&
332 !status.play_dtmf) {
333 if (config_.enable_stable_playout_delay) {
334 const int playout_delay_ms = GetPlayoutDelayMs(status);
335 if (playout_delay_ms >= HighThreshold() << 2) {
336 return NetEq::Operation::kFastAccelerate;
337 }
338 if (TimescaleAllowed()) {
339 if (playout_delay_ms >= HighThreshold()) {
340 return NetEq::Operation::kAccelerate;
341 }
342 if (playout_delay_ms < LowThreshold()) {
343 return NetEq::Operation::kPreemptiveExpand;
344 }
345 }
346 } else {
347 const int target_level_samples = TargetLevelMs() * sample_rate_khz_;
348 const int low_limit = std::max(
349 target_level_samples * 3 / 4,
350 target_level_samples -
351 config_.deceleration_target_level_offset_ms * sample_rate_khz_);
352 const int high_limit = std::max(
353 target_level_samples,
354 low_limit + kDelayAdjustmentGranularityMs * sample_rate_khz_);
Jakob Ivarsson80fb9782020-10-09 13:41:06 +0200355
Jakob Ivarssonc782cf82022-05-16 15:28:22 +0200356 const int buffer_level_samples =
357 buffer_level_filter_->filtered_current_level();
358 if (buffer_level_samples >= high_limit << 2)
359 return NetEq::Operation::kFastAccelerate;
360 if (TimescaleAllowed()) {
361 if (buffer_level_samples >= high_limit)
362 return NetEq::Operation::kAccelerate;
363 if (buffer_level_samples < low_limit)
364 return NetEq::Operation::kPreemptiveExpand;
365 }
Henrik Lundin7687ad52018-07-02 10:14:46 +0200366 }
367 }
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100368 return NetEq::Operation::kNormal;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200369}
370
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100371NetEq::Operation DecisionLogic::FuturePacketAvailable(
Jakob Ivarssonc782cf82022-05-16 15:28:22 +0200372 NetEqController::NetEqStatus status) {
Henrik Lundin7687ad52018-07-02 10:14:46 +0200373 // Required packet is not available, but a future packet is.
374 // Check if we should continue with an ongoing expand because the new packet
375 // is too far into the future.
Jakob Ivarssonc782cf82022-05-16 15:28:22 +0200376 if (IsExpand(status.last_mode) && ShouldContinueExpand(status)) {
377 if (status.play_dtmf) {
Henrik Lundin7687ad52018-07-02 10:14:46 +0200378 // Still have DTMF to play, so do not do expand.
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100379 return NetEq::Operation::kDtmf;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200380 } else {
381 // Nothing to play.
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100382 return NetEq::Operation::kExpand;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200383 }
384 }
385
Jakob Ivarssonc782cf82022-05-16 15:28:22 +0200386 if (status.last_mode == NetEq::Mode::kCodecPlc) {
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100387 return NetEq::Operation::kNormal;
Henrik Lundin00eb12a2018-09-05 18:14:52 +0200388 }
389
Henrik Lundin7687ad52018-07-02 10:14:46 +0200390 // If previous was comfort noise, then no merge is needed.
Jakob Ivarssonc782cf82022-05-16 15:28:22 +0200391 if (IsCng(status.last_mode)) {
392 uint32_t timestamp_leap =
393 status.next_packet->timestamp - status.target_timestamp;
Jakob Ivarsson46dda832019-07-03 16:00:30 +0200394 const bool generated_enough_noise =
Jakob Ivarssonc782cf82022-05-16 15:28:22 +0200395 status.generated_noise_samples >= timestamp_leap;
396
397 int playout_delay_ms = GetNextPacketDelayMs(status);
398 const bool above_target_delay = playout_delay_ms > HighThresholdCng();
399 const bool below_target_delay = playout_delay_ms < LowThresholdCng();
Jakob Ivarssone6aabd02022-03-28 16:06:37 +0200400 // Keep the delay same as before CNG, but make sure that it is within the
401 // target window.
Jakob Ivarssonc782cf82022-05-16 15:28:22 +0200402 if ((generated_enough_noise && !below_target_delay) || above_target_delay) {
403 time_stretched_cn_samples_ =
404 timestamp_leap - status.generated_noise_samples;
Jakob Ivarssone6aabd02022-03-28 16:06:37 +0200405 return NetEq::Operation::kNormal;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200406 }
Jakob Ivarsson46dda832019-07-03 16:00:30 +0200407
Jakob Ivarssonc782cf82022-05-16 15:28:22 +0200408 if (status.last_mode == NetEq::Mode::kRfc3389Cng) {
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100409 return NetEq::Operation::kRfc3389CngNoPacket;
Jakob Ivarsson46dda832019-07-03 16:00:30 +0200410 }
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100411 return NetEq::Operation::kCodecInternalCng;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200412 }
Jakob Ivarsson46dda832019-07-03 16:00:30 +0200413
Henrik Lundin7687ad52018-07-02 10:14:46 +0200414 // Do not merge unless we have done an expand before.
Jakob Ivarssonc782cf82022-05-16 15:28:22 +0200415 if (status.last_mode == NetEq::Mode::kExpand) {
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100416 return NetEq::Operation::kMerge;
Jakob Ivarssonc782cf82022-05-16 15:28:22 +0200417 } else if (status.play_dtmf) {
Henrik Lundin7687ad52018-07-02 10:14:46 +0200418 // Play DTMF instead of expand.
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100419 return NetEq::Operation::kDtmf;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200420 } else {
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100421 return NetEq::Operation::kExpand;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200422 }
423}
424
425bool DecisionLogic::UnderTargetLevel() const {
Jakob Ivarsson609b0472020-10-19 09:19:34 +0200426 return buffer_level_filter_->filtered_current_level() <
Jakob Ivarssonc782cf82022-05-16 15:28:22 +0200427 TargetLevelMs() * sample_rate_khz_;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200428}
429
430bool DecisionLogic::ReinitAfterExpands(uint32_t timestamp_leap) const {
Jakob Ivarssonc782cf82022-05-16 15:28:22 +0200431 return timestamp_leap >= static_cast<uint32_t>(output_size_samples_ *
432 config_.reinit_after_expands);
Henrik Lundin7687ad52018-07-02 10:14:46 +0200433}
434
435bool DecisionLogic::PacketTooEarly(uint32_t timestamp_leap) const {
436 return timestamp_leap >
437 static_cast<uint32_t>(output_size_samples_ * num_consecutive_expands_);
438}
439
440bool DecisionLogic::MaxWaitForPacket() const {
Jakob Ivarssonc782cf82022-05-16 15:28:22 +0200441 return num_consecutive_expands_ >= kMaxWaitForPacketTicks;
442}
443
444bool DecisionLogic::ShouldContinueExpand(
445 NetEqController::NetEqStatus status) const {
446 uint32_t timestamp_leap =
447 status.next_packet->timestamp - status.target_timestamp;
448 if (config_.enable_stable_playout_delay) {
449 return GetNextPacketDelayMs(status) < HighThreshold() &&
450 PacketTooEarly(timestamp_leap);
451 }
452 return !ReinitAfterExpands(timestamp_leap) && !MaxWaitForPacket() &&
453 PacketTooEarly(timestamp_leap) && UnderTargetLevel();
454}
455
456int DecisionLogic::GetNextPacketDelayMs(
457 NetEqController::NetEqStatus status) const {
458 if (config_.enable_stable_playout_delay) {
459 return packet_arrival_history_.GetDelayMs(
460 status.next_packet->timestamp,
461 tick_timer_->ticks() * tick_timer_->ms_per_tick());
462 }
463 return status.packet_buffer_info.span_samples / sample_rate_khz_;
464}
465
466int DecisionLogic::GetPlayoutDelayMs(
467 NetEqController::NetEqStatus status) const {
468 uint32_t playout_timestamp =
469 status.target_timestamp - status.sync_buffer_samples;
470 return packet_arrival_history_.GetDelayMs(
471 playout_timestamp, tick_timer_->ticks() * tick_timer_->ms_per_tick());
472}
473
474int DecisionLogic::LowThreshold() const {
475 int target_delay_ms = TargetLevelMs();
476 return std::max(
477 target_delay_ms * 3 / 4,
478 target_delay_ms - config_.deceleration_target_level_offset_ms);
479}
480
481int DecisionLogic::HighThreshold() const {
482 if (config_.enable_stable_playout_delay) {
483 return std::max(TargetLevelMs(), packet_arrival_history_.GetMaxDelayMs()) +
484 kDelayAdjustmentGranularityMs;
485 }
486 return std::max(TargetLevelMs(),
487 LowThreshold() + kDelayAdjustmentGranularityMs);
488}
489
490int DecisionLogic::LowThresholdCng() const {
491 if (config_.enable_stable_playout_delay) {
492 return LowThreshold();
493 }
494 return std::max(0, TargetLevelMs() - kTargetLevelWindowMs / 2);
495}
496
497int DecisionLogic::HighThresholdCng() const {
498 if (config_.enable_stable_playout_delay) {
499 return HighThreshold();
500 }
501 return TargetLevelMs() + kTargetLevelWindowMs / 2;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200502}
503
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000504} // namespace webrtc