blob: d5a216ea2ac69712ea5621ddec11b4f534d9a23d [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 Ivarsson46dda832019-07-03 16:00:30 +020059 target_level_window_ms_("target_level_window",
60 kDefaultTargetLevelWindowMs,
61 0,
62 absl::nullopt) {
Jakob Ivarsson46dda832019-07-03 16:00:30 +020063 const std::string field_trial_name =
64 field_trial::FindFullName("WebRTC-Audio-NetEqDecisionLogicSettings");
Jakob Ivarssone6aabd02022-03-28 16:06:37 +020065 ParseFieldTrial({&target_level_window_ms_}, field_trial_name);
Jakob Ivarsson46dda832019-07-03 16:00:30 +020066 RTC_LOG(LS_INFO) << "NetEq decision logic settings:"
Jakob Ivarsson46dda832019-07-03 16:00:30 +020067 << " target_level_window_ms=" << target_level_window_ms_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000068}
69
Henrik Lundin47b17dc2016-05-10 10:20:59 +020070DecisionLogic::~DecisionLogic() = default;
71
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000072void DecisionLogic::Reset() {
73 cng_state_ = kCngOff;
henrik.lundinb1fb72b2016-05-03 08:18:47 -070074 noise_fast_forward_ = 0;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000075 packet_length_samples_ = 0;
76 sample_memory_ = 0;
77 prev_time_scale_ = false;
Jakob Ivarsson80fb9782020-10-09 13:41:06 +020078 last_pack_cng_or_dtmf_ = true;
Henrik Lundin47b17dc2016-05-10 10:20:59 +020079 timescale_countdown_.reset();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000080 num_consecutive_expands_ = 0;
Jakob Ivarsson46dda832019-07-03 16:00:30 +020081 time_stretched_cn_samples_ = 0;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000082}
83
84void DecisionLogic::SoftReset() {
85 packet_length_samples_ = 0;
86 sample_memory_ = 0;
87 prev_time_scale_ = false;
Jakob Ivarsson80fb9782020-10-09 13:41:06 +020088 last_pack_cng_or_dtmf_ = true;
Henrik Lundin47b17dc2016-05-10 10:20:59 +020089 timescale_countdown_ =
90 tick_timer_->GetNewCountdown(kMinTimescaleInterval + 1);
Jakob Ivarsson46dda832019-07-03 16:00:30 +020091 time_stretched_cn_samples_ = 0;
Ivo Creusen53a31f72019-10-24 15:20:39 +020092 delay_manager_->Reset();
Jakob Ivarsson609b0472020-10-19 09:19:34 +020093 buffer_level_filter_->Reset();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000094}
95
Peter Kastingdce40cf2015-08-24 14:52:23 -070096void DecisionLogic::SetSampleRate(int fs_hz, size_t output_size_samples) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000097 // TODO(hlundin): Change to an enumerator and skip assert.
Mirko Bonadei25ab3222021-07-08 20:08:20 +020098 RTC_DCHECK(fs_hz == 8000 || fs_hz == 16000 || fs_hz == 32000 ||
99 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
Yves Gerey665174f2018-06-19 15:03:05 +0200115 prev_time_scale_ =
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100116 prev_time_scale_ &&
117 (status.last_mode == NetEq::Mode::kAccelerateSuccess ||
118 status.last_mode == NetEq::Mode::kAccelerateLowEnergy ||
119 status.last_mode == NetEq::Mode::kPreemptiveExpandSuccess ||
120 status.last_mode == NetEq::Mode::kPreemptiveExpandLowEnergy);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000121
Minyue Li7d204d52019-04-16 11:44:49 +0200122 // Do not update buffer history if currently playing CNG since it will bias
123 // the filtered buffer level.
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100124 if (status.last_mode != NetEq::Mode::kRfc3389Cng &&
Jakob Ivarssone6aabd02022-03-28 16:06:37 +0200125 status.last_mode != NetEq::Mode::kCodecInternalCng) {
126 FilterBufferLevel(status.packet_buffer_info.span_samples);
Minyue Li7d204d52019-04-16 11:44:49 +0200127 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000128
Henrik Lundin7687ad52018-07-02 10:14:46 +0200129 // Guard for errors, to avoid getting stuck in error mode.
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100130 if (status.last_mode == NetEq::Mode::kError) {
Ivo Creusen53a31f72019-10-24 15:20:39 +0200131 if (!status.next_packet) {
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100132 return NetEq::Operation::kExpand;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200133 } else {
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100134 // Use kUndefined to flag for a reset.
135 return NetEq::Operation::kUndefined;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200136 }
137 }
138
Ivo Creusen53a31f72019-10-24 15:20:39 +0200139 if (status.next_packet && status.next_packet->is_cng) {
140 return CngOperation(status.last_mode, status.target_timestamp,
141 status.next_packet->timestamp,
142 status.generated_noise_samples);
Henrik Lundin7687ad52018-07-02 10:14:46 +0200143 }
144
145 // Handle the case with no packet at all available (except maybe DTMF).
Ivo Creusen53a31f72019-10-24 15:20:39 +0200146 if (!status.next_packet) {
147 return NoPacket(status.play_dtmf);
Henrik Lundin7687ad52018-07-02 10:14:46 +0200148 }
149
150 // If the expand period was very long, reset NetEQ since it is likely that the
151 // sender was restarted.
152 if (num_consecutive_expands_ > kReinitAfterExpands) {
153 *reset_decoder = true;
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100154 return NetEq::Operation::kNormal;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200155 }
156
157 // Make sure we don't restart audio too soon after an expansion to avoid
158 // running out of data right away again. We should only wait if there are no
159 // DTX or CNG packets in the buffer (otherwise we should just play out what we
160 // have, since we cannot know the exact duration of DTX or CNG packets), and
161 // if the mute factor is low enough (otherwise the expansion was short enough
162 // to not be noticable).
163 // Note that the MuteFactor is in Q14, so a value of 16384 corresponds to 1.
Jakob Ivarsson80fb9782020-10-09 13:41:06 +0200164 const int target_level_samples =
165 delay_manager_->TargetDelayMs() * sample_rate_ / 1000;
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100166 if ((status.last_mode == NetEq::Mode::kExpand ||
167 status.last_mode == NetEq::Mode::kCodecPlc) &&
Ivo Creusen53a31f72019-10-24 15:20:39 +0200168 status.expand_mutefactor < 16384 / 2 &&
Jakob Ivarssone6aabd02022-03-28 16:06:37 +0200169 status.packet_buffer_info.span_samples <
170 static_cast<size_t>(target_level_samples * kPostponeDecodingLevel /
171 100) &&
Ivo Creusen53a31f72019-10-24 15:20:39 +0200172 !status.packet_buffer_info.dtx_or_cng) {
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100173 return NetEq::Operation::kExpand;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200174 }
175
Jakob Ivarsson46dda832019-07-03 16:00:30 +0200176 const uint32_t five_seconds_samples = static_cast<uint32_t>(5 * sample_rate_);
Henrik Lundin7687ad52018-07-02 10:14:46 +0200177 // Check if the required packet is available.
Ivo Creusen53a31f72019-10-24 15:20:39 +0200178 if (status.target_timestamp == status.next_packet->timestamp) {
179 return ExpectedPacketAvailable(status.last_mode, status.play_dtmf);
180 } else if (!PacketBuffer::IsObsoleteTimestamp(status.next_packet->timestamp,
181 status.target_timestamp,
182 five_seconds_samples)) {
183 return FuturePacketAvailable(
184 status.last_packet_samples, status.last_mode, status.target_timestamp,
185 status.next_packet->timestamp, status.play_dtmf,
186 status.generated_noise_samples, status.packet_buffer_info.span_samples,
187 status.packet_buffer_info.num_packets);
Henrik Lundin7687ad52018-07-02 10:14:46 +0200188 } else {
189 // This implies that available_timestamp < target_timestamp, which can
190 // happen when a new stream or codec is received. Signal for a reset.
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100191 return NetEq::Operation::kUndefined;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200192 }
193}
194
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100195void DecisionLogic::ExpandDecision(NetEq::Operation operation) {
196 if (operation == NetEq::Operation::kExpand) {
Henrik Lundin5afa61c2018-07-02 14:53:24 +0200197 num_consecutive_expands_++;
198 } else {
199 num_consecutive_expands_ = 0;
200 }
201}
202
Ivo Creusena2b31c32020-10-14 17:54:22 +0200203absl::optional<int> DecisionLogic::PacketArrived(
204 int fs_hz,
205 bool should_update_stats,
206 const PacketArrivedInfo& info) {
Ivo Creusen7b463c52020-11-25 11:32:40 +0100207 buffer_flush_ = buffer_flush_ || info.buffer_flush;
Ivo Creusena2b31c32020-10-14 17:54:22 +0200208 if (info.is_cng_or_dtmf) {
Jakob Ivarsson80fb9782020-10-09 13:41:06 +0200209 last_pack_cng_or_dtmf_ = true;
210 return absl::nullopt;
Ivo Creusen53a31f72019-10-24 15:20:39 +0200211 }
Jakob Ivarsson80fb9782020-10-09 13:41:06 +0200212 if (!should_update_stats) {
213 return absl::nullopt;
214 }
Ivo Creusena2b31c32020-10-14 17:54:22 +0200215 if (info.packet_length_samples > 0 && fs_hz > 0 &&
216 info.packet_length_samples != packet_length_samples_) {
217 packet_length_samples_ = info.packet_length_samples;
Jakob Ivarsson80fb9782020-10-09 13:41:06 +0200218 delay_manager_->SetPacketAudioLength(packet_length_samples_ * 1000 / fs_hz);
219 }
220 auto relative_delay = delay_manager_->Update(
Ivo Creusena2b31c32020-10-14 17:54:22 +0200221 info.main_timestamp, fs_hz, /*reset=*/last_pack_cng_or_dtmf_);
Jakob Ivarsson80fb9782020-10-09 13:41:06 +0200222 last_pack_cng_or_dtmf_ = false;
Ivo Creusen53a31f72019-10-24 15:20:39 +0200223 return relative_delay;
224}
225
Minyue Li7d204d52019-04-16 11:44:49 +0200226void DecisionLogic::FilterBufferLevel(size_t buffer_size_samples) {
Jakob Ivarsson609b0472020-10-19 09:19:34 +0200227 buffer_level_filter_->SetTargetBufferLevel(delay_manager_->TargetDelayMs());
Henrik Lundin5afa61c2018-07-02 14:53:24 +0200228
Jakob Ivarsson46dda832019-07-03 16:00:30 +0200229 int time_stretched_samples = time_stretched_cn_samples_;
Minyue Li7d204d52019-04-16 11:44:49 +0200230 if (prev_time_scale_) {
Jakob Ivarsson46dda832019-07-03 16:00:30 +0200231 time_stretched_samples += sample_memory_;
Minyue Li7d204d52019-04-16 11:44:49 +0200232 timescale_countdown_ = tick_timer_->GetNewCountdown(kMinTimescaleInterval);
233 }
234
Ivo Creusen7b463c52020-11-25 11:32:40 +0100235 if (buffer_flush_) {
236 buffer_level_filter_->SetFilteredBufferLevel(buffer_size_samples);
237 buffer_flush_ = false;
238 } else {
239 buffer_level_filter_->Update(buffer_size_samples, time_stretched_samples);
240 }
Minyue Li7d204d52019-04-16 11:44:49 +0200241 prev_time_scale_ = false;
Jakob Ivarsson46dda832019-07-03 16:00:30 +0200242 time_stretched_cn_samples_ = 0;
Henrik Lundin5afa61c2018-07-02 14:53:24 +0200243}
244
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100245NetEq::Operation DecisionLogic::CngOperation(NetEq::Mode prev_mode,
246 uint32_t target_timestamp,
247 uint32_t available_timestamp,
248 size_t generated_noise_samples) {
Henrik Lundin7687ad52018-07-02 10:14:46 +0200249 // Signed difference between target and available timestamp.
250 int32_t timestamp_diff = static_cast<int32_t>(
251 static_cast<uint32_t>(generated_noise_samples + target_timestamp) -
252 available_timestamp);
Jakob Ivarsson80fb9782020-10-09 13:41:06 +0200253 int optimal_level_samp =
254 delay_manager_->TargetDelayMs() * sample_rate_ / 1000;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200255 const int64_t excess_waiting_time_samp =
256 -static_cast<int64_t>(timestamp_diff) - optimal_level_samp;
257
258 if (excess_waiting_time_samp > optimal_level_samp / 2) {
259 // The waiting time for this packet will be longer than 1.5
260 // times the wanted buffer delay. Apply fast-forward to cut the
261 // waiting time down to the optimal.
Jakob Ivarsson42b6e2d2019-10-21 11:51:05 +0200262 noise_fast_forward_ = rtc::saturated_cast<size_t>(noise_fast_forward_ +
263 excess_waiting_time_samp);
Henrik Lundin7687ad52018-07-02 10:14:46 +0200264 timestamp_diff =
265 rtc::saturated_cast<int32_t>(timestamp_diff + excess_waiting_time_samp);
266 }
267
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100268 if (timestamp_diff < 0 && prev_mode == NetEq::Mode::kRfc3389Cng) {
Henrik Lundin7687ad52018-07-02 10:14:46 +0200269 // Not time to play this packet yet. Wait another round before using this
270 // packet. Keep on playing CNG from previous CNG parameters.
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100271 return NetEq::Operation::kRfc3389CngNoPacket;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200272 } else {
273 // Otherwise, go for the CNG packet now.
274 noise_fast_forward_ = 0;
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100275 return NetEq::Operation::kRfc3389Cng;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200276 }
277}
278
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100279NetEq::Operation DecisionLogic::NoPacket(bool play_dtmf) {
Henrik Lundin7687ad52018-07-02 10:14:46 +0200280 if (cng_state_ == kCngRfc3389On) {
281 // Keep on playing comfort noise.
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100282 return NetEq::Operation::kRfc3389CngNoPacket;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200283 } else if (cng_state_ == kCngInternalOn) {
284 // Keep on playing codec internal comfort noise.
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100285 return NetEq::Operation::kCodecInternalCng;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200286 } else if (play_dtmf) {
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100287 return NetEq::Operation::kDtmf;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200288 } else {
289 // Nothing to play, do expand.
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100290 return NetEq::Operation::kExpand;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200291 }
292}
293
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100294NetEq::Operation DecisionLogic::ExpectedPacketAvailable(NetEq::Mode prev_mode,
295 bool play_dtmf) {
296 if (!disallow_time_stretching_ && prev_mode != NetEq::Mode::kExpand &&
297 !play_dtmf) {
Jakob Ivarsson80fb9782020-10-09 13:41:06 +0200298 const int samples_per_ms = sample_rate_ / 1000;
299 const int target_level_samples =
300 delay_manager_->TargetDelayMs() * samples_per_ms;
301 const int low_limit =
302 std::max(target_level_samples * 3 / 4,
303 target_level_samples -
304 kDecelerationTargetLevelOffsetMs * samples_per_ms);
Artem Titovd00ce742021-07-28 20:00:17 +0200305 // `higher_limit` is equal to `target_level`, but should at
306 // least be 20 ms higher than `lower_limit`.
Jakob Ivarsson80fb9782020-10-09 13:41:06 +0200307 const int high_limit =
308 std::max(target_level_samples, low_limit + 20 * samples_per_ms);
309
310 const int buffer_level_samples =
Jakob Ivarsson609b0472020-10-19 09:19:34 +0200311 buffer_level_filter_->filtered_current_level();
Jakob Ivarsson80fb9782020-10-09 13:41:06 +0200312 if (buffer_level_samples >= high_limit << 2)
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100313 return NetEq::Operation::kFastAccelerate;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200314 if (TimescaleAllowed()) {
Jakob Ivarsson80fb9782020-10-09 13:41:06 +0200315 if (buffer_level_samples >= high_limit)
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100316 return NetEq::Operation::kAccelerate;
Jakob Ivarsson80fb9782020-10-09 13:41:06 +0200317 if (buffer_level_samples < low_limit)
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100318 return NetEq::Operation::kPreemptiveExpand;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200319 }
320 }
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100321 return NetEq::Operation::kNormal;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200322}
323
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100324NetEq::Operation DecisionLogic::FuturePacketAvailable(
Henrik Lundin7687ad52018-07-02 10:14:46 +0200325 size_t decoder_frame_length,
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100326 NetEq::Mode prev_mode,
Henrik Lundin7687ad52018-07-02 10:14:46 +0200327 uint32_t target_timestamp,
328 uint32_t available_timestamp,
329 bool play_dtmf,
Ivo Creusen53a31f72019-10-24 15:20:39 +0200330 size_t generated_noise_samples,
331 size_t span_samples_in_packet_buffer,
332 size_t num_packets_in_packet_buffer) {
Henrik Lundin7687ad52018-07-02 10:14:46 +0200333 // Required packet is not available, but a future packet is.
334 // Check if we should continue with an ongoing expand because the new packet
335 // is too far into the future.
336 uint32_t timestamp_leap = available_timestamp - target_timestamp;
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100337 if ((prev_mode == NetEq::Mode::kExpand ||
338 prev_mode == NetEq::Mode::kCodecPlc) &&
Henrik Lundin00eb12a2018-09-05 18:14:52 +0200339 !ReinitAfterExpands(timestamp_leap) && !MaxWaitForPacket() &&
340 PacketTooEarly(timestamp_leap) && UnderTargetLevel()) {
Henrik Lundin7687ad52018-07-02 10:14:46 +0200341 if (play_dtmf) {
342 // Still have DTMF to play, so do not do expand.
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100343 return NetEq::Operation::kDtmf;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200344 } else {
345 // Nothing to play.
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100346 return NetEq::Operation::kExpand;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200347 }
348 }
349
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100350 if (prev_mode == NetEq::Mode::kCodecPlc) {
351 return NetEq::Operation::kNormal;
Henrik Lundin00eb12a2018-09-05 18:14:52 +0200352 }
353
Henrik Lundin7687ad52018-07-02 10:14:46 +0200354 // If previous was comfort noise, then no merge is needed.
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100355 if (prev_mode == NetEq::Mode::kRfc3389Cng ||
356 prev_mode == NetEq::Mode::kCodecInternalCng) {
Jakob Ivarsson46dda832019-07-03 16:00:30 +0200357 const size_t target_level_samples =
Jakob Ivarsson80fb9782020-10-09 13:41:06 +0200358 delay_manager_->TargetDelayMs() * sample_rate_ / 1000;
Jakob Ivarsson46dda832019-07-03 16:00:30 +0200359 const bool generated_enough_noise =
360 static_cast<uint32_t>(generated_noise_samples + target_timestamp) >=
361 available_timestamp;
Jakob Ivarssone6aabd02022-03-28 16:06:37 +0200362 const size_t target_threshold_samples =
363 target_level_window_ms_ / 2 * (sample_rate_ / 1000);
364 const bool above_target_window =
365 span_samples_in_packet_buffer >
366 target_level_samples + target_threshold_samples;
367 const bool below_target_window =
368 target_level_samples > target_threshold_samples &&
369 span_samples_in_packet_buffer <
370 target_level_samples - target_threshold_samples;
371 // Keep the delay same as before CNG, but make sure that it is within the
372 // target window.
373 if ((generated_enough_noise && !below_target_window) ||
374 above_target_window) {
375 time_stretched_cn_samples_ = timestamp_leap - generated_noise_samples;
376 return NetEq::Operation::kNormal;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200377 }
Jakob Ivarsson46dda832019-07-03 16:00:30 +0200378
379 // Too early to play this new packet; keep on playing comfort noise.
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100380 if (prev_mode == NetEq::Mode::kRfc3389Cng) {
381 return NetEq::Operation::kRfc3389CngNoPacket;
Jakob Ivarsson46dda832019-07-03 16:00:30 +0200382 }
383 // prevPlayMode == kModeCodecInternalCng.
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100384 return NetEq::Operation::kCodecInternalCng;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200385 }
Jakob Ivarsson46dda832019-07-03 16:00:30 +0200386
Henrik Lundin7687ad52018-07-02 10:14:46 +0200387 // Do not merge unless we have done an expand before.
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100388 if (prev_mode == NetEq::Mode::kExpand) {
389 return NetEq::Operation::kMerge;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200390 } else if (play_dtmf) {
391 // Play DTMF instead of expand.
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100392 return NetEq::Operation::kDtmf;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200393 } else {
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100394 return NetEq::Operation::kExpand;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200395 }
396}
397
398bool DecisionLogic::UnderTargetLevel() const {
Jakob Ivarsson609b0472020-10-19 09:19:34 +0200399 return buffer_level_filter_->filtered_current_level() <
Jakob Ivarsson80fb9782020-10-09 13:41:06 +0200400 delay_manager_->TargetDelayMs() * sample_rate_ / 1000;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200401}
402
403bool DecisionLogic::ReinitAfterExpands(uint32_t timestamp_leap) const {
404 return timestamp_leap >=
405 static_cast<uint32_t>(output_size_samples_ * kReinitAfterExpands);
406}
407
408bool DecisionLogic::PacketTooEarly(uint32_t timestamp_leap) const {
409 return timestamp_leap >
410 static_cast<uint32_t>(output_size_samples_ * num_consecutive_expands_);
411}
412
413bool DecisionLogic::MaxWaitForPacket() const {
414 return num_consecutive_expands_ >= kMaxWaitForPacket;
415}
416
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000417} // namespace webrtc