blob: d70272988120196ac8a97c8032961df48c98e33f [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.
Mirko Bonadei25ab3222021-07-08 20:08:20 +020099 RTC_DCHECK(fs_hz == 8000 || fs_hz == 16000 || fs_hz == 32000 ||
100 fs_hz == 48000);
Jakob Ivarsson46dda832019-07-03 16:00:30 +0200101 sample_rate_ = fs_hz;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000102 output_size_samples_ = output_size_samples;
103}
104
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100105NetEq::Operation DecisionLogic::GetDecision(const NetEqStatus& status,
106 bool* reset_decoder) {
ossu61a208b2016-09-20 01:38:00 -0700107 // If last mode was CNG (or Expand, since this could be covering up for
108 // a lost CNG packet), remember that CNG is on. This is needed if comfort
109 // noise is interrupted by DTMF.
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100110 if (status.last_mode == NetEq::Mode::kRfc3389Cng) {
ossu61a208b2016-09-20 01:38:00 -0700111 cng_state_ = kCngRfc3389On;
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100112 } else if (status.last_mode == NetEq::Mode::kCodecInternalCng) {
ossu61a208b2016-09-20 01:38:00 -0700113 cng_state_ = kCngInternalOn;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000114 }
115
Ivo Creusen53a31f72019-10-24 15:20:39 +0200116 size_t cur_size_samples = estimate_dtx_delay_
117 ? status.packet_buffer_info.span_samples
118 : status.packet_buffer_info.num_samples;
Yves Gerey665174f2018-06-19 15:03:05 +0200119 prev_time_scale_ =
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100120 prev_time_scale_ &&
121 (status.last_mode == NetEq::Mode::kAccelerateSuccess ||
122 status.last_mode == NetEq::Mode::kAccelerateLowEnergy ||
123 status.last_mode == NetEq::Mode::kPreemptiveExpandSuccess ||
124 status.last_mode == NetEq::Mode::kPreemptiveExpandLowEnergy);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000125
Minyue Li7d204d52019-04-16 11:44:49 +0200126 // Do not update buffer history if currently playing CNG since it will bias
127 // the filtered buffer level.
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100128 if (status.last_mode != NetEq::Mode::kRfc3389Cng &&
129 status.last_mode != NetEq::Mode::kCodecInternalCng &&
Ivo Creusen53a31f72019-10-24 15:20:39 +0200130 !(status.next_packet && status.next_packet->is_dtx &&
131 !estimate_dtx_delay_)) {
Minyue Li7d204d52019-04-16 11:44:49 +0200132 FilterBufferLevel(cur_size_samples);
133 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000134
Henrik Lundin7687ad52018-07-02 10:14:46 +0200135 // Guard for errors, to avoid getting stuck in error mode.
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100136 if (status.last_mode == NetEq::Mode::kError) {
Ivo Creusen53a31f72019-10-24 15:20:39 +0200137 if (!status.next_packet) {
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100138 return NetEq::Operation::kExpand;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200139 } else {
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100140 // Use kUndefined to flag for a reset.
141 return NetEq::Operation::kUndefined;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200142 }
143 }
144
Ivo Creusen53a31f72019-10-24 15:20:39 +0200145 if (status.next_packet && status.next_packet->is_cng) {
146 return CngOperation(status.last_mode, status.target_timestamp,
147 status.next_packet->timestamp,
148 status.generated_noise_samples);
Henrik Lundin7687ad52018-07-02 10:14:46 +0200149 }
150
151 // Handle the case with no packet at all available (except maybe DTMF).
Ivo Creusen53a31f72019-10-24 15:20:39 +0200152 if (!status.next_packet) {
153 return NoPacket(status.play_dtmf);
Henrik Lundin7687ad52018-07-02 10:14:46 +0200154 }
155
156 // If the expand period was very long, reset NetEQ since it is likely that the
157 // sender was restarted.
158 if (num_consecutive_expands_ > kReinitAfterExpands) {
159 *reset_decoder = true;
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100160 return NetEq::Operation::kNormal;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200161 }
162
163 // Make sure we don't restart audio too soon after an expansion to avoid
164 // running out of data right away again. We should only wait if there are no
165 // DTX or CNG packets in the buffer (otherwise we should just play out what we
166 // have, since we cannot know the exact duration of DTX or CNG packets), and
167 // if the mute factor is low enough (otherwise the expansion was short enough
168 // to not be noticable).
169 // Note that the MuteFactor is in Q14, so a value of 16384 corresponds to 1.
Ivo Creusen53a31f72019-10-24 15:20:39 +0200170 const size_t current_span =
171 estimate_dtx_delay_ ? status.packet_buffer_info.span_samples
172 : status.packet_buffer_info.span_samples_no_dtx;
Jakob Ivarsson80fb9782020-10-09 13:41:06 +0200173 const int target_level_samples =
174 delay_manager_->TargetDelayMs() * sample_rate_ / 1000;
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100175 if ((status.last_mode == NetEq::Mode::kExpand ||
176 status.last_mode == NetEq::Mode::kCodecPlc) &&
Ivo Creusen53a31f72019-10-24 15:20:39 +0200177 status.expand_mutefactor < 16384 / 2 &&
Jakob Ivarsson80fb9782020-10-09 13:41:06 +0200178 current_span < static_cast<size_t>(target_level_samples *
179 kPostponeDecodingLevel / 100) &&
Ivo Creusen53a31f72019-10-24 15:20:39 +0200180 !status.packet_buffer_info.dtx_or_cng) {
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100181 return NetEq::Operation::kExpand;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200182 }
183
Jakob Ivarsson46dda832019-07-03 16:00:30 +0200184 const uint32_t five_seconds_samples = static_cast<uint32_t>(5 * sample_rate_);
Henrik Lundin7687ad52018-07-02 10:14:46 +0200185 // Check if the required packet is available.
Ivo Creusen53a31f72019-10-24 15:20:39 +0200186 if (status.target_timestamp == status.next_packet->timestamp) {
187 return ExpectedPacketAvailable(status.last_mode, status.play_dtmf);
188 } else if (!PacketBuffer::IsObsoleteTimestamp(status.next_packet->timestamp,
189 status.target_timestamp,
190 five_seconds_samples)) {
191 return FuturePacketAvailable(
192 status.last_packet_samples, status.last_mode, status.target_timestamp,
193 status.next_packet->timestamp, status.play_dtmf,
194 status.generated_noise_samples, status.packet_buffer_info.span_samples,
195 status.packet_buffer_info.num_packets);
Henrik Lundin7687ad52018-07-02 10:14:46 +0200196 } else {
197 // This implies that available_timestamp < target_timestamp, which can
198 // happen when a new stream or codec is received. Signal for a reset.
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100199 return NetEq::Operation::kUndefined;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200200 }
201}
202
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100203void DecisionLogic::ExpandDecision(NetEq::Operation operation) {
204 if (operation == NetEq::Operation::kExpand) {
Henrik Lundin5afa61c2018-07-02 14:53:24 +0200205 num_consecutive_expands_++;
206 } else {
207 num_consecutive_expands_ = 0;
208 }
209}
210
Ivo Creusena2b31c32020-10-14 17:54:22 +0200211absl::optional<int> DecisionLogic::PacketArrived(
212 int fs_hz,
213 bool should_update_stats,
214 const PacketArrivedInfo& info) {
Ivo Creusen7b463c52020-11-25 11:32:40 +0100215 buffer_flush_ = buffer_flush_ || info.buffer_flush;
Ivo Creusena2b31c32020-10-14 17:54:22 +0200216 if (info.is_cng_or_dtmf) {
Jakob Ivarsson80fb9782020-10-09 13:41:06 +0200217 last_pack_cng_or_dtmf_ = true;
218 return absl::nullopt;
Ivo Creusen53a31f72019-10-24 15:20:39 +0200219 }
Jakob Ivarsson80fb9782020-10-09 13:41:06 +0200220 if (!should_update_stats) {
221 return absl::nullopt;
222 }
Ivo Creusena2b31c32020-10-14 17:54:22 +0200223 if (info.packet_length_samples > 0 && fs_hz > 0 &&
224 info.packet_length_samples != packet_length_samples_) {
225 packet_length_samples_ = info.packet_length_samples;
Jakob Ivarsson80fb9782020-10-09 13:41:06 +0200226 delay_manager_->SetPacketAudioLength(packet_length_samples_ * 1000 / fs_hz);
227 }
228 auto relative_delay = delay_manager_->Update(
Ivo Creusena2b31c32020-10-14 17:54:22 +0200229 info.main_timestamp, fs_hz, /*reset=*/last_pack_cng_or_dtmf_);
Jakob Ivarsson80fb9782020-10-09 13:41:06 +0200230 last_pack_cng_or_dtmf_ = false;
Ivo Creusen53a31f72019-10-24 15:20:39 +0200231 return relative_delay;
232}
233
Minyue Li7d204d52019-04-16 11:44:49 +0200234void DecisionLogic::FilterBufferLevel(size_t buffer_size_samples) {
Jakob Ivarsson609b0472020-10-19 09:19:34 +0200235 buffer_level_filter_->SetTargetBufferLevel(delay_manager_->TargetDelayMs());
Henrik Lundin5afa61c2018-07-02 14:53:24 +0200236
Jakob Ivarsson46dda832019-07-03 16:00:30 +0200237 int time_stretched_samples = time_stretched_cn_samples_;
Minyue Li7d204d52019-04-16 11:44:49 +0200238 if (prev_time_scale_) {
Jakob Ivarsson46dda832019-07-03 16:00:30 +0200239 time_stretched_samples += sample_memory_;
Minyue Li7d204d52019-04-16 11:44:49 +0200240 timescale_countdown_ = tick_timer_->GetNewCountdown(kMinTimescaleInterval);
241 }
242
Ivo Creusen7b463c52020-11-25 11:32:40 +0100243 if (buffer_flush_) {
244 buffer_level_filter_->SetFilteredBufferLevel(buffer_size_samples);
245 buffer_flush_ = false;
246 } else {
247 buffer_level_filter_->Update(buffer_size_samples, time_stretched_samples);
248 }
Minyue Li7d204d52019-04-16 11:44:49 +0200249 prev_time_scale_ = false;
Jakob Ivarsson46dda832019-07-03 16:00:30 +0200250 time_stretched_cn_samples_ = 0;
Henrik Lundin5afa61c2018-07-02 14:53:24 +0200251}
252
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100253NetEq::Operation DecisionLogic::CngOperation(NetEq::Mode prev_mode,
254 uint32_t target_timestamp,
255 uint32_t available_timestamp,
256 size_t generated_noise_samples) {
Henrik Lundin7687ad52018-07-02 10:14:46 +0200257 // Signed difference between target and available timestamp.
258 int32_t timestamp_diff = static_cast<int32_t>(
259 static_cast<uint32_t>(generated_noise_samples + target_timestamp) -
260 available_timestamp);
Jakob Ivarsson80fb9782020-10-09 13:41:06 +0200261 int optimal_level_samp =
262 delay_manager_->TargetDelayMs() * sample_rate_ / 1000;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200263 const int64_t excess_waiting_time_samp =
264 -static_cast<int64_t>(timestamp_diff) - optimal_level_samp;
265
266 if (excess_waiting_time_samp > optimal_level_samp / 2) {
267 // The waiting time for this packet will be longer than 1.5
268 // times the wanted buffer delay. Apply fast-forward to cut the
269 // waiting time down to the optimal.
Jakob Ivarsson42b6e2d2019-10-21 11:51:05 +0200270 noise_fast_forward_ = rtc::saturated_cast<size_t>(noise_fast_forward_ +
271 excess_waiting_time_samp);
Henrik Lundin7687ad52018-07-02 10:14:46 +0200272 timestamp_diff =
273 rtc::saturated_cast<int32_t>(timestamp_diff + excess_waiting_time_samp);
274 }
275
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100276 if (timestamp_diff < 0 && prev_mode == NetEq::Mode::kRfc3389Cng) {
Henrik Lundin7687ad52018-07-02 10:14:46 +0200277 // Not time to play this packet yet. Wait another round before using this
278 // packet. Keep on playing CNG from previous CNG parameters.
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100279 return NetEq::Operation::kRfc3389CngNoPacket;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200280 } else {
281 // Otherwise, go for the CNG packet now.
282 noise_fast_forward_ = 0;
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100283 return NetEq::Operation::kRfc3389Cng;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200284 }
285}
286
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100287NetEq::Operation DecisionLogic::NoPacket(bool play_dtmf) {
Henrik Lundin7687ad52018-07-02 10:14:46 +0200288 if (cng_state_ == kCngRfc3389On) {
289 // Keep on playing comfort noise.
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100290 return NetEq::Operation::kRfc3389CngNoPacket;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200291 } else if (cng_state_ == kCngInternalOn) {
292 // Keep on playing codec internal comfort noise.
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100293 return NetEq::Operation::kCodecInternalCng;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200294 } else if (play_dtmf) {
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100295 return NetEq::Operation::kDtmf;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200296 } else {
297 // Nothing to play, do expand.
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100298 return NetEq::Operation::kExpand;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200299 }
300}
301
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100302NetEq::Operation DecisionLogic::ExpectedPacketAvailable(NetEq::Mode prev_mode,
303 bool play_dtmf) {
304 if (!disallow_time_stretching_ && prev_mode != NetEq::Mode::kExpand &&
305 !play_dtmf) {
Jakob Ivarsson80fb9782020-10-09 13:41:06 +0200306 const int samples_per_ms = sample_rate_ / 1000;
307 const int target_level_samples =
308 delay_manager_->TargetDelayMs() * samples_per_ms;
309 const int low_limit =
310 std::max(target_level_samples * 3 / 4,
311 target_level_samples -
312 kDecelerationTargetLevelOffsetMs * samples_per_ms);
313 // |higher_limit| is equal to |target_level|, but should at
314 // least be 20 ms higher than |lower_limit|.
315 const int high_limit =
316 std::max(target_level_samples, low_limit + 20 * samples_per_ms);
317
318 const int buffer_level_samples =
Jakob Ivarsson609b0472020-10-19 09:19:34 +0200319 buffer_level_filter_->filtered_current_level();
Jakob Ivarsson80fb9782020-10-09 13:41:06 +0200320 if (buffer_level_samples >= high_limit << 2)
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100321 return NetEq::Operation::kFastAccelerate;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200322 if (TimescaleAllowed()) {
Jakob Ivarsson80fb9782020-10-09 13:41:06 +0200323 if (buffer_level_samples >= high_limit)
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100324 return NetEq::Operation::kAccelerate;
Jakob Ivarsson80fb9782020-10-09 13:41:06 +0200325 if (buffer_level_samples < low_limit)
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100326 return NetEq::Operation::kPreemptiveExpand;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200327 }
328 }
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100329 return NetEq::Operation::kNormal;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200330}
331
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100332NetEq::Operation DecisionLogic::FuturePacketAvailable(
Henrik Lundin7687ad52018-07-02 10:14:46 +0200333 size_t decoder_frame_length,
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100334 NetEq::Mode prev_mode,
Henrik Lundin7687ad52018-07-02 10:14:46 +0200335 uint32_t target_timestamp,
336 uint32_t available_timestamp,
337 bool play_dtmf,
Ivo Creusen53a31f72019-10-24 15:20:39 +0200338 size_t generated_noise_samples,
339 size_t span_samples_in_packet_buffer,
340 size_t num_packets_in_packet_buffer) {
Henrik Lundin7687ad52018-07-02 10:14:46 +0200341 // Required packet is not available, but a future packet is.
342 // Check if we should continue with an ongoing expand because the new packet
343 // is too far into the future.
344 uint32_t timestamp_leap = available_timestamp - target_timestamp;
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100345 if ((prev_mode == NetEq::Mode::kExpand ||
346 prev_mode == NetEq::Mode::kCodecPlc) &&
Henrik Lundin00eb12a2018-09-05 18:14:52 +0200347 !ReinitAfterExpands(timestamp_leap) && !MaxWaitForPacket() &&
348 PacketTooEarly(timestamp_leap) && UnderTargetLevel()) {
Henrik Lundin7687ad52018-07-02 10:14:46 +0200349 if (play_dtmf) {
350 // Still have DTMF to play, so do not do expand.
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100351 return NetEq::Operation::kDtmf;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200352 } else {
353 // Nothing to play.
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100354 return NetEq::Operation::kExpand;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200355 }
356 }
357
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100358 if (prev_mode == NetEq::Mode::kCodecPlc) {
359 return NetEq::Operation::kNormal;
Henrik Lundin00eb12a2018-09-05 18:14:52 +0200360 }
361
Henrik Lundin7687ad52018-07-02 10:14:46 +0200362 // If previous was comfort noise, then no merge is needed.
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100363 if (prev_mode == NetEq::Mode::kRfc3389Cng ||
364 prev_mode == NetEq::Mode::kCodecInternalCng) {
Jakob Ivarsson46dda832019-07-03 16:00:30 +0200365 size_t cur_size_samples =
366 estimate_dtx_delay_
Jakob Ivarsson80fb9782020-10-09 13:41:06 +0200367 ? span_samples_in_packet_buffer
Ivo Creusen53a31f72019-10-24 15:20:39 +0200368 : num_packets_in_packet_buffer * decoder_frame_length;
Jakob Ivarsson46dda832019-07-03 16:00:30 +0200369 // Target level is in number of packets in Q8.
370 const size_t target_level_samples =
Jakob Ivarsson80fb9782020-10-09 13:41:06 +0200371 delay_manager_->TargetDelayMs() * sample_rate_ / 1000;
Jakob Ivarsson46dda832019-07-03 16:00:30 +0200372 const bool generated_enough_noise =
373 static_cast<uint32_t>(generated_noise_samples + target_timestamp) >=
374 available_timestamp;
375
376 if (time_stretch_cn_) {
377 const size_t target_threshold_samples =
378 target_level_window_ms_ / 2 * (sample_rate_ / 1000);
379 const bool above_target_window =
380 cur_size_samples > target_level_samples + target_threshold_samples;
381 const bool below_target_window =
382 target_level_samples > target_threshold_samples &&
383 cur_size_samples < target_level_samples - target_threshold_samples;
384 // Keep the delay same as before CNG, but make sure that it is within the
385 // target window.
386 if ((generated_enough_noise && !below_target_window) ||
387 above_target_window) {
388 time_stretched_cn_samples_ = timestamp_leap - generated_noise_samples;
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100389 return NetEq::Operation::kNormal;
Jakob Ivarsson46dda832019-07-03 16:00:30 +0200390 }
Henrik Lundin7687ad52018-07-02 10:14:46 +0200391 } else {
Jakob Ivarsson46dda832019-07-03 16:00:30 +0200392 // Keep the same delay as before the CNG, but make sure that the number of
393 // samples in buffer is no higher than 4 times the optimal level.
394 if (generated_enough_noise ||
395 cur_size_samples > target_level_samples * 4) {
396 // Time to play this new packet.
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100397 return NetEq::Operation::kNormal;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200398 }
399 }
Jakob Ivarsson46dda832019-07-03 16:00:30 +0200400
401 // Too early to play this new packet; keep on playing comfort noise.
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100402 if (prev_mode == NetEq::Mode::kRfc3389Cng) {
403 return NetEq::Operation::kRfc3389CngNoPacket;
Jakob Ivarsson46dda832019-07-03 16:00:30 +0200404 }
405 // prevPlayMode == kModeCodecInternalCng.
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100406 return NetEq::Operation::kCodecInternalCng;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200407 }
Jakob Ivarsson46dda832019-07-03 16:00:30 +0200408
Henrik Lundin7687ad52018-07-02 10:14:46 +0200409 // Do not merge unless we have done an expand before.
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100410 if (prev_mode == NetEq::Mode::kExpand) {
411 return NetEq::Operation::kMerge;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200412 } else if (play_dtmf) {
413 // Play DTMF instead of expand.
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100414 return NetEq::Operation::kDtmf;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200415 } else {
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100416 return NetEq::Operation::kExpand;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200417 }
418}
419
420bool DecisionLogic::UnderTargetLevel() const {
Jakob Ivarsson609b0472020-10-19 09:19:34 +0200421 return buffer_level_filter_->filtered_current_level() <
Jakob Ivarsson80fb9782020-10-09 13:41:06 +0200422 delay_manager_->TargetDelayMs() * sample_rate_ / 1000;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200423}
424
425bool DecisionLogic::ReinitAfterExpands(uint32_t timestamp_leap) const {
426 return timestamp_leap >=
427 static_cast<uint32_t>(output_size_samples_ * kReinitAfterExpands);
428}
429
430bool DecisionLogic::PacketTooEarly(uint32_t timestamp_leap) const {
431 return timestamp_leap >
432 static_cast<uint32_t>(output_size_samples_ * num_consecutive_expands_);
433}
434
435bool DecisionLogic::MaxWaitForPacket() const {
436 return num_consecutive_expands_ >= kMaxWaitForPacket;
437}
438
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000439} // namespace webrtc