blob: cb6daf062eff928f85fd34ad011e9c5dbdb34d11 [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 Ivarssond723da12021-01-15 17:44:56 +010053 estimate_dtx_delay_("estimate_dtx_delay", true),
54 time_stretch_cn_("time_stretch_cn", true),
Jakob Ivarsson46dda832019-07-03 16:00:30 +020055 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) {
Ivo Creusen7b463c52020-11-25 11:32:40 +0100214 buffer_flush_ = buffer_flush_ || info.buffer_flush;
Ivo Creusena2b31c32020-10-14 17:54:22 +0200215 if (info.is_cng_or_dtmf) {
Jakob Ivarsson80fb9782020-10-09 13:41:06 +0200216 last_pack_cng_or_dtmf_ = true;
217 return absl::nullopt;
Ivo Creusen53a31f72019-10-24 15:20:39 +0200218 }
Jakob Ivarsson80fb9782020-10-09 13:41:06 +0200219 if (!should_update_stats) {
220 return absl::nullopt;
221 }
Ivo Creusena2b31c32020-10-14 17:54:22 +0200222 if (info.packet_length_samples > 0 && fs_hz > 0 &&
223 info.packet_length_samples != packet_length_samples_) {
224 packet_length_samples_ = info.packet_length_samples;
Jakob Ivarsson80fb9782020-10-09 13:41:06 +0200225 delay_manager_->SetPacketAudioLength(packet_length_samples_ * 1000 / fs_hz);
226 }
227 auto relative_delay = delay_manager_->Update(
Ivo Creusena2b31c32020-10-14 17:54:22 +0200228 info.main_timestamp, fs_hz, /*reset=*/last_pack_cng_or_dtmf_);
Jakob Ivarsson80fb9782020-10-09 13:41:06 +0200229 last_pack_cng_or_dtmf_ = false;
Ivo Creusen53a31f72019-10-24 15:20:39 +0200230 return relative_delay;
231}
232
Minyue Li7d204d52019-04-16 11:44:49 +0200233void DecisionLogic::FilterBufferLevel(size_t buffer_size_samples) {
Jakob Ivarsson609b0472020-10-19 09:19:34 +0200234 buffer_level_filter_->SetTargetBufferLevel(delay_manager_->TargetDelayMs());
Henrik Lundin5afa61c2018-07-02 14:53:24 +0200235
Jakob Ivarsson46dda832019-07-03 16:00:30 +0200236 int time_stretched_samples = time_stretched_cn_samples_;
Minyue Li7d204d52019-04-16 11:44:49 +0200237 if (prev_time_scale_) {
Jakob Ivarsson46dda832019-07-03 16:00:30 +0200238 time_stretched_samples += sample_memory_;
Minyue Li7d204d52019-04-16 11:44:49 +0200239 timescale_countdown_ = tick_timer_->GetNewCountdown(kMinTimescaleInterval);
240 }
241
Ivo Creusen7b463c52020-11-25 11:32:40 +0100242 if (buffer_flush_) {
243 buffer_level_filter_->SetFilteredBufferLevel(buffer_size_samples);
244 buffer_flush_ = false;
245 } else {
246 buffer_level_filter_->Update(buffer_size_samples, time_stretched_samples);
247 }
Minyue Li7d204d52019-04-16 11:44:49 +0200248 prev_time_scale_ = false;
Jakob Ivarsson46dda832019-07-03 16:00:30 +0200249 time_stretched_cn_samples_ = 0;
Henrik Lundin5afa61c2018-07-02 14:53:24 +0200250}
251
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100252NetEq::Operation DecisionLogic::CngOperation(NetEq::Mode prev_mode,
253 uint32_t target_timestamp,
254 uint32_t available_timestamp,
255 size_t generated_noise_samples) {
Henrik Lundin7687ad52018-07-02 10:14:46 +0200256 // Signed difference between target and available timestamp.
257 int32_t timestamp_diff = static_cast<int32_t>(
258 static_cast<uint32_t>(generated_noise_samples + target_timestamp) -
259 available_timestamp);
Jakob Ivarsson80fb9782020-10-09 13:41:06 +0200260 int optimal_level_samp =
261 delay_manager_->TargetDelayMs() * sample_rate_ / 1000;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200262 const int64_t excess_waiting_time_samp =
263 -static_cast<int64_t>(timestamp_diff) - optimal_level_samp;
264
265 if (excess_waiting_time_samp > optimal_level_samp / 2) {
266 // The waiting time for this packet will be longer than 1.5
267 // times the wanted buffer delay. Apply fast-forward to cut the
268 // waiting time down to the optimal.
Jakob Ivarsson42b6e2d2019-10-21 11:51:05 +0200269 noise_fast_forward_ = rtc::saturated_cast<size_t>(noise_fast_forward_ +
270 excess_waiting_time_samp);
Henrik Lundin7687ad52018-07-02 10:14:46 +0200271 timestamp_diff =
272 rtc::saturated_cast<int32_t>(timestamp_diff + excess_waiting_time_samp);
273 }
274
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100275 if (timestamp_diff < 0 && prev_mode == NetEq::Mode::kRfc3389Cng) {
Henrik Lundin7687ad52018-07-02 10:14:46 +0200276 // Not time to play this packet yet. Wait another round before using this
277 // packet. Keep on playing CNG from previous CNG parameters.
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100278 return NetEq::Operation::kRfc3389CngNoPacket;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200279 } else {
280 // Otherwise, go for the CNG packet now.
281 noise_fast_forward_ = 0;
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100282 return NetEq::Operation::kRfc3389Cng;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200283 }
284}
285
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100286NetEq::Operation DecisionLogic::NoPacket(bool play_dtmf) {
Henrik Lundin7687ad52018-07-02 10:14:46 +0200287 if (cng_state_ == kCngRfc3389On) {
288 // Keep on playing comfort noise.
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100289 return NetEq::Operation::kRfc3389CngNoPacket;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200290 } else if (cng_state_ == kCngInternalOn) {
291 // Keep on playing codec internal comfort noise.
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100292 return NetEq::Operation::kCodecInternalCng;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200293 } else if (play_dtmf) {
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100294 return NetEq::Operation::kDtmf;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200295 } else {
296 // Nothing to play, do expand.
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100297 return NetEq::Operation::kExpand;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200298 }
299}
300
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100301NetEq::Operation DecisionLogic::ExpectedPacketAvailable(NetEq::Mode prev_mode,
302 bool play_dtmf) {
303 if (!disallow_time_stretching_ && prev_mode != NetEq::Mode::kExpand &&
304 !play_dtmf) {
Jakob Ivarsson80fb9782020-10-09 13:41:06 +0200305 const int samples_per_ms = sample_rate_ / 1000;
306 const int target_level_samples =
307 delay_manager_->TargetDelayMs() * samples_per_ms;
308 const int low_limit =
309 std::max(target_level_samples * 3 / 4,
310 target_level_samples -
311 kDecelerationTargetLevelOffsetMs * samples_per_ms);
312 // |higher_limit| is equal to |target_level|, but should at
313 // least be 20 ms higher than |lower_limit|.
314 const int high_limit =
315 std::max(target_level_samples, low_limit + 20 * samples_per_ms);
316
317 const int buffer_level_samples =
Jakob Ivarsson609b0472020-10-19 09:19:34 +0200318 buffer_level_filter_->filtered_current_level();
Jakob Ivarsson80fb9782020-10-09 13:41:06 +0200319 if (buffer_level_samples >= high_limit << 2)
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100320 return NetEq::Operation::kFastAccelerate;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200321 if (TimescaleAllowed()) {
Jakob Ivarsson80fb9782020-10-09 13:41:06 +0200322 if (buffer_level_samples >= high_limit)
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100323 return NetEq::Operation::kAccelerate;
Jakob Ivarsson80fb9782020-10-09 13:41:06 +0200324 if (buffer_level_samples < low_limit)
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100325 return NetEq::Operation::kPreemptiveExpand;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200326 }
327 }
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100328 return NetEq::Operation::kNormal;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200329}
330
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100331NetEq::Operation DecisionLogic::FuturePacketAvailable(
Henrik Lundin7687ad52018-07-02 10:14:46 +0200332 size_t decoder_frame_length,
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100333 NetEq::Mode prev_mode,
Henrik Lundin7687ad52018-07-02 10:14:46 +0200334 uint32_t target_timestamp,
335 uint32_t available_timestamp,
336 bool play_dtmf,
Ivo Creusen53a31f72019-10-24 15:20:39 +0200337 size_t generated_noise_samples,
338 size_t span_samples_in_packet_buffer,
339 size_t num_packets_in_packet_buffer) {
Henrik Lundin7687ad52018-07-02 10:14:46 +0200340 // Required packet is not available, but a future packet is.
341 // Check if we should continue with an ongoing expand because the new packet
342 // is too far into the future.
343 uint32_t timestamp_leap = available_timestamp - target_timestamp;
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100344 if ((prev_mode == NetEq::Mode::kExpand ||
345 prev_mode == NetEq::Mode::kCodecPlc) &&
Henrik Lundin00eb12a2018-09-05 18:14:52 +0200346 !ReinitAfterExpands(timestamp_leap) && !MaxWaitForPacket() &&
347 PacketTooEarly(timestamp_leap) && UnderTargetLevel()) {
Henrik Lundin7687ad52018-07-02 10:14:46 +0200348 if (play_dtmf) {
349 // Still have DTMF to play, so do not do expand.
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100350 return NetEq::Operation::kDtmf;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200351 } else {
352 // Nothing to play.
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100353 return NetEq::Operation::kExpand;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200354 }
355 }
356
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100357 if (prev_mode == NetEq::Mode::kCodecPlc) {
358 return NetEq::Operation::kNormal;
Henrik Lundin00eb12a2018-09-05 18:14:52 +0200359 }
360
Henrik Lundin7687ad52018-07-02 10:14:46 +0200361 // If previous was comfort noise, then no merge is needed.
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100362 if (prev_mode == NetEq::Mode::kRfc3389Cng ||
363 prev_mode == NetEq::Mode::kCodecInternalCng) {
Jakob Ivarsson46dda832019-07-03 16:00:30 +0200364 size_t cur_size_samples =
365 estimate_dtx_delay_
Jakob Ivarsson80fb9782020-10-09 13:41:06 +0200366 ? span_samples_in_packet_buffer
Ivo Creusen53a31f72019-10-24 15:20:39 +0200367 : num_packets_in_packet_buffer * decoder_frame_length;
Jakob Ivarsson46dda832019-07-03 16:00:30 +0200368 // Target level is in number of packets in Q8.
369 const size_t target_level_samples =
Jakob Ivarsson80fb9782020-10-09 13:41:06 +0200370 delay_manager_->TargetDelayMs() * sample_rate_ / 1000;
Jakob Ivarsson46dda832019-07-03 16:00:30 +0200371 const bool generated_enough_noise =
372 static_cast<uint32_t>(generated_noise_samples + target_timestamp) >=
373 available_timestamp;
374
375 if (time_stretch_cn_) {
376 const size_t target_threshold_samples =
377 target_level_window_ms_ / 2 * (sample_rate_ / 1000);
378 const bool above_target_window =
379 cur_size_samples > target_level_samples + target_threshold_samples;
380 const bool below_target_window =
381 target_level_samples > target_threshold_samples &&
382 cur_size_samples < target_level_samples - target_threshold_samples;
383 // Keep the delay same as before CNG, but make sure that it is within the
384 // target window.
385 if ((generated_enough_noise && !below_target_window) ||
386 above_target_window) {
387 time_stretched_cn_samples_ = timestamp_leap - generated_noise_samples;
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100388 return NetEq::Operation::kNormal;
Jakob Ivarsson46dda832019-07-03 16:00:30 +0200389 }
Henrik Lundin7687ad52018-07-02 10:14:46 +0200390 } else {
Jakob Ivarsson46dda832019-07-03 16:00:30 +0200391 // Keep the same delay as before the CNG, but make sure that the number of
392 // samples in buffer is no higher than 4 times the optimal level.
393 if (generated_enough_noise ||
394 cur_size_samples > target_level_samples * 4) {
395 // Time to play this new packet.
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100396 return NetEq::Operation::kNormal;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200397 }
398 }
Jakob Ivarsson46dda832019-07-03 16:00:30 +0200399
400 // Too early to play this new packet; keep on playing comfort noise.
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100401 if (prev_mode == NetEq::Mode::kRfc3389Cng) {
402 return NetEq::Operation::kRfc3389CngNoPacket;
Jakob Ivarsson46dda832019-07-03 16:00:30 +0200403 }
404 // prevPlayMode == kModeCodecInternalCng.
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100405 return NetEq::Operation::kCodecInternalCng;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200406 }
Jakob Ivarsson46dda832019-07-03 16:00:30 +0200407
Henrik Lundin7687ad52018-07-02 10:14:46 +0200408 // Do not merge unless we have done an expand before.
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100409 if (prev_mode == NetEq::Mode::kExpand) {
410 return NetEq::Operation::kMerge;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200411 } else if (play_dtmf) {
412 // Play DTMF instead of expand.
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100413 return NetEq::Operation::kDtmf;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200414 } else {
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100415 return NetEq::Operation::kExpand;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200416 }
417}
418
419bool DecisionLogic::UnderTargetLevel() const {
Jakob Ivarsson609b0472020-10-19 09:19:34 +0200420 return buffer_level_filter_->filtered_current_level() <
Jakob Ivarsson80fb9782020-10-09 13:41:06 +0200421 delay_manager_->TargetDelayMs() * sample_rate_ / 1000;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200422}
423
424bool DecisionLogic::ReinitAfterExpands(uint32_t timestamp_leap) const {
425 return timestamp_leap >=
426 static_cast<uint32_t>(output_size_samples_ * kReinitAfterExpands);
427}
428
429bool DecisionLogic::PacketTooEarly(uint32_t timestamp_leap) const {
430 return timestamp_leap >
431 static_cast<uint32_t>(output_size_samples_ * num_consecutive_expands_);
432}
433
434bool DecisionLogic::MaxWaitForPacket() const {
435 return num_consecutive_expands_ >= kMaxWaitForPacket;
436}
437
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000438} // namespace webrtc