blob: fb26b9995f30d4f09a9e68dc37d06606440ba2ca [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 Ivarssonbd5874a2020-01-07 17:07:40 +010037 : delay_manager_(DelayManager::Create(config.max_packets_in_buffer,
Ivo Creusen53a31f72019-10-24 15:20:39 +020038 config.base_min_delay_ms,
Ivo Creusen53a31f72019-10-24 15:20:39 +020039 config.tick_timer)),
40 tick_timer_(config.tick_timer),
41 disallow_time_stretching_(!config.allow_time_stretching),
Henrik Lundin47b17dc2016-05-10 10:20:59 +020042 timescale_countdown_(
43 tick_timer_->GetNewCountdown(kMinTimescaleInterval + 1)),
Jakob Ivarsson46dda832019-07-03 16:00:30 +020044 estimate_dtx_delay_("estimate_dtx_delay", false),
45 time_stretch_cn_("time_stretch_cn", false),
46 target_level_window_ms_("target_level_window",
47 kDefaultTargetLevelWindowMs,
48 0,
49 absl::nullopt) {
Jakob Ivarsson46dda832019-07-03 16:00:30 +020050 const std::string field_trial_name =
51 field_trial::FindFullName("WebRTC-Audio-NetEqDecisionLogicSettings");
52 ParseFieldTrial(
53 {&estimate_dtx_delay_, &time_stretch_cn_, &target_level_window_ms_},
54 field_trial_name);
55 RTC_LOG(LS_INFO) << "NetEq decision logic settings:"
Jonas Olssonb2b20312020-01-14 12:11:31 +010056 " estimate_dtx_delay="
57 << estimate_dtx_delay_
Jakob Ivarsson46dda832019-07-03 16:00:30 +020058 << " time_stretch_cn=" << time_stretch_cn_
59 << " target_level_window_ms=" << target_level_window_ms_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000060}
61
Henrik Lundin47b17dc2016-05-10 10:20:59 +020062DecisionLogic::~DecisionLogic() = default;
63
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000064void DecisionLogic::Reset() {
65 cng_state_ = kCngOff;
henrik.lundinb1fb72b2016-05-03 08:18:47 -070066 noise_fast_forward_ = 0;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000067 packet_length_samples_ = 0;
68 sample_memory_ = 0;
69 prev_time_scale_ = false;
Jakob Ivarsson80fb9782020-10-09 13:41:06 +020070 last_pack_cng_or_dtmf_ = true;
Henrik Lundin47b17dc2016-05-10 10:20:59 +020071 timescale_countdown_.reset();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000072 num_consecutive_expands_ = 0;
Jakob Ivarsson46dda832019-07-03 16:00:30 +020073 time_stretched_cn_samples_ = 0;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000074}
75
76void DecisionLogic::SoftReset() {
77 packet_length_samples_ = 0;
78 sample_memory_ = 0;
79 prev_time_scale_ = false;
Jakob Ivarsson80fb9782020-10-09 13:41:06 +020080 last_pack_cng_or_dtmf_ = true;
Henrik Lundin47b17dc2016-05-10 10:20:59 +020081 timescale_countdown_ =
82 tick_timer_->GetNewCountdown(kMinTimescaleInterval + 1);
Jakob Ivarsson46dda832019-07-03 16:00:30 +020083 time_stretched_cn_samples_ = 0;
Ivo Creusen53a31f72019-10-24 15:20:39 +020084 delay_manager_->Reset();
85 buffer_level_filter_.Reset();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000086}
87
Peter Kastingdce40cf2015-08-24 14:52:23 -070088void DecisionLogic::SetSampleRate(int fs_hz, size_t output_size_samples) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000089 // TODO(hlundin): Change to an enumerator and skip assert.
Yves Gerey665174f2018-06-19 15:03:05 +020090 assert(fs_hz == 8000 || fs_hz == 16000 || fs_hz == 32000 || fs_hz == 48000);
Jakob Ivarsson46dda832019-07-03 16:00:30 +020091 sample_rate_ = fs_hz;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000092 output_size_samples_ = output_size_samples;
93}
94
Ivo Creusen3ce44a32019-10-31 14:38:11 +010095NetEq::Operation DecisionLogic::GetDecision(const NetEqStatus& status,
96 bool* reset_decoder) {
ossu61a208b2016-09-20 01:38:00 -070097 // If last mode was CNG (or Expand, since this could be covering up for
98 // a lost CNG packet), remember that CNG is on. This is needed if comfort
99 // noise is interrupted by DTMF.
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100100 if (status.last_mode == NetEq::Mode::kRfc3389Cng) {
ossu61a208b2016-09-20 01:38:00 -0700101 cng_state_ = kCngRfc3389On;
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100102 } else if (status.last_mode == NetEq::Mode::kCodecInternalCng) {
ossu61a208b2016-09-20 01:38:00 -0700103 cng_state_ = kCngInternalOn;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000104 }
105
Ivo Creusen53a31f72019-10-24 15:20:39 +0200106 size_t cur_size_samples = estimate_dtx_delay_
107 ? status.packet_buffer_info.span_samples
108 : status.packet_buffer_info.num_samples;
Yves Gerey665174f2018-06-19 15:03:05 +0200109 prev_time_scale_ =
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100110 prev_time_scale_ &&
111 (status.last_mode == NetEq::Mode::kAccelerateSuccess ||
112 status.last_mode == NetEq::Mode::kAccelerateLowEnergy ||
113 status.last_mode == NetEq::Mode::kPreemptiveExpandSuccess ||
114 status.last_mode == NetEq::Mode::kPreemptiveExpandLowEnergy);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000115
Minyue Li7d204d52019-04-16 11:44:49 +0200116 // Do not update buffer history if currently playing CNG since it will bias
117 // the filtered buffer level.
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100118 if (status.last_mode != NetEq::Mode::kRfc3389Cng &&
119 status.last_mode != NetEq::Mode::kCodecInternalCng &&
Ivo Creusen53a31f72019-10-24 15:20:39 +0200120 !(status.next_packet && status.next_packet->is_dtx &&
121 !estimate_dtx_delay_)) {
Minyue Li7d204d52019-04-16 11:44:49 +0200122 FilterBufferLevel(cur_size_samples);
123 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000124
Henrik Lundin7687ad52018-07-02 10:14:46 +0200125 // Guard for errors, to avoid getting stuck in error mode.
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100126 if (status.last_mode == NetEq::Mode::kError) {
Ivo Creusen53a31f72019-10-24 15:20:39 +0200127 if (!status.next_packet) {
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100128 return NetEq::Operation::kExpand;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200129 } else {
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100130 // Use kUndefined to flag for a reset.
131 return NetEq::Operation::kUndefined;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200132 }
133 }
134
Ivo Creusen53a31f72019-10-24 15:20:39 +0200135 if (status.next_packet && status.next_packet->is_cng) {
136 return CngOperation(status.last_mode, status.target_timestamp,
137 status.next_packet->timestamp,
138 status.generated_noise_samples);
Henrik Lundin7687ad52018-07-02 10:14:46 +0200139 }
140
141 // Handle the case with no packet at all available (except maybe DTMF).
Ivo Creusen53a31f72019-10-24 15:20:39 +0200142 if (!status.next_packet) {
143 return NoPacket(status.play_dtmf);
Henrik Lundin7687ad52018-07-02 10:14:46 +0200144 }
145
146 // If the expand period was very long, reset NetEQ since it is likely that the
147 // sender was restarted.
148 if (num_consecutive_expands_ > kReinitAfterExpands) {
149 *reset_decoder = true;
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100150 return NetEq::Operation::kNormal;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200151 }
152
153 // Make sure we don't restart audio too soon after an expansion to avoid
154 // running out of data right away again. We should only wait if there are no
155 // DTX or CNG packets in the buffer (otherwise we should just play out what we
156 // have, since we cannot know the exact duration of DTX or CNG packets), and
157 // if the mute factor is low enough (otherwise the expansion was short enough
158 // to not be noticable).
159 // Note that the MuteFactor is in Q14, so a value of 16384 corresponds to 1.
Ivo Creusen53a31f72019-10-24 15:20:39 +0200160 const size_t current_span =
161 estimate_dtx_delay_ ? status.packet_buffer_info.span_samples
162 : status.packet_buffer_info.span_samples_no_dtx;
Jakob Ivarsson80fb9782020-10-09 13:41:06 +0200163 const int target_level_samples =
164 delay_manager_->TargetDelayMs() * sample_rate_ / 1000;
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100165 if ((status.last_mode == NetEq::Mode::kExpand ||
166 status.last_mode == NetEq::Mode::kCodecPlc) &&
Ivo Creusen53a31f72019-10-24 15:20:39 +0200167 status.expand_mutefactor < 16384 / 2 &&
Jakob Ivarsson80fb9782020-10-09 13:41:06 +0200168 current_span < static_cast<size_t>(target_level_samples *
169 kPostponeDecodingLevel / 100) &&
Ivo Creusen53a31f72019-10-24 15:20:39 +0200170 !status.packet_buffer_info.dtx_or_cng) {
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100171 return NetEq::Operation::kExpand;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200172 }
173
Jakob Ivarsson46dda832019-07-03 16:00:30 +0200174 const uint32_t five_seconds_samples = static_cast<uint32_t>(5 * sample_rate_);
Henrik Lundin7687ad52018-07-02 10:14:46 +0200175 // Check if the required packet is available.
Ivo Creusen53a31f72019-10-24 15:20:39 +0200176 if (status.target_timestamp == status.next_packet->timestamp) {
177 return ExpectedPacketAvailable(status.last_mode, status.play_dtmf);
178 } else if (!PacketBuffer::IsObsoleteTimestamp(status.next_packet->timestamp,
179 status.target_timestamp,
180 five_seconds_samples)) {
181 return FuturePacketAvailable(
182 status.last_packet_samples, status.last_mode, status.target_timestamp,
183 status.next_packet->timestamp, status.play_dtmf,
184 status.generated_noise_samples, status.packet_buffer_info.span_samples,
185 status.packet_buffer_info.num_packets);
Henrik Lundin7687ad52018-07-02 10:14:46 +0200186 } else {
187 // This implies that available_timestamp < target_timestamp, which can
188 // happen when a new stream or codec is received. Signal for a reset.
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100189 return NetEq::Operation::kUndefined;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200190 }
191}
192
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100193void DecisionLogic::ExpandDecision(NetEq::Operation operation) {
194 if (operation == NetEq::Operation::kExpand) {
Henrik Lundin5afa61c2018-07-02 14:53:24 +0200195 num_consecutive_expands_++;
196 } else {
197 num_consecutive_expands_ = 0;
198 }
199}
200
Ivo Creusena2b31c32020-10-14 17:54:22 +0200201absl::optional<int> DecisionLogic::PacketArrived(
202 int fs_hz,
203 bool should_update_stats,
204 const PacketArrivedInfo& info) {
205 if (info.is_cng_or_dtmf) {
Jakob Ivarsson80fb9782020-10-09 13:41:06 +0200206 last_pack_cng_or_dtmf_ = true;
207 return absl::nullopt;
Ivo Creusen53a31f72019-10-24 15:20:39 +0200208 }
Jakob Ivarsson80fb9782020-10-09 13:41:06 +0200209 if (!should_update_stats) {
210 return absl::nullopt;
211 }
Ivo Creusena2b31c32020-10-14 17:54:22 +0200212 if (info.packet_length_samples > 0 && fs_hz > 0 &&
213 info.packet_length_samples != packet_length_samples_) {
214 packet_length_samples_ = info.packet_length_samples;
Jakob Ivarsson80fb9782020-10-09 13:41:06 +0200215 delay_manager_->SetPacketAudioLength(packet_length_samples_ * 1000 / fs_hz);
216 }
217 auto relative_delay = delay_manager_->Update(
Ivo Creusena2b31c32020-10-14 17:54:22 +0200218 info.main_timestamp, fs_hz, /*reset=*/last_pack_cng_or_dtmf_);
Jakob Ivarsson80fb9782020-10-09 13:41:06 +0200219 last_pack_cng_or_dtmf_ = false;
Ivo Creusen53a31f72019-10-24 15:20:39 +0200220 return relative_delay;
221}
222
Minyue Li7d204d52019-04-16 11:44:49 +0200223void DecisionLogic::FilterBufferLevel(size_t buffer_size_samples) {
Jakob Ivarsson80fb9782020-10-09 13:41:06 +0200224 buffer_level_filter_.SetTargetBufferLevel(delay_manager_->TargetDelayMs());
Henrik Lundin5afa61c2018-07-02 14:53:24 +0200225
Jakob Ivarsson46dda832019-07-03 16:00:30 +0200226 int time_stretched_samples = time_stretched_cn_samples_;
Minyue Li7d204d52019-04-16 11:44:49 +0200227 if (prev_time_scale_) {
Jakob Ivarsson46dda832019-07-03 16:00:30 +0200228 time_stretched_samples += sample_memory_;
Minyue Li7d204d52019-04-16 11:44:49 +0200229 timescale_countdown_ = tick_timer_->GetNewCountdown(kMinTimescaleInterval);
230 }
231
Ivo Creusen53a31f72019-10-24 15:20:39 +0200232 buffer_level_filter_.Update(buffer_size_samples, time_stretched_samples);
Minyue Li7d204d52019-04-16 11:44:49 +0200233 prev_time_scale_ = false;
Jakob Ivarsson46dda832019-07-03 16:00:30 +0200234 time_stretched_cn_samples_ = 0;
Henrik Lundin5afa61c2018-07-02 14:53:24 +0200235}
236
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100237NetEq::Operation DecisionLogic::CngOperation(NetEq::Mode prev_mode,
238 uint32_t target_timestamp,
239 uint32_t available_timestamp,
240 size_t generated_noise_samples) {
Henrik Lundin7687ad52018-07-02 10:14:46 +0200241 // Signed difference between target and available timestamp.
242 int32_t timestamp_diff = static_cast<int32_t>(
243 static_cast<uint32_t>(generated_noise_samples + target_timestamp) -
244 available_timestamp);
Jakob Ivarsson80fb9782020-10-09 13:41:06 +0200245 int optimal_level_samp =
246 delay_manager_->TargetDelayMs() * sample_rate_ / 1000;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200247 const int64_t excess_waiting_time_samp =
248 -static_cast<int64_t>(timestamp_diff) - optimal_level_samp;
249
250 if (excess_waiting_time_samp > optimal_level_samp / 2) {
251 // The waiting time for this packet will be longer than 1.5
252 // times the wanted buffer delay. Apply fast-forward to cut the
253 // waiting time down to the optimal.
Jakob Ivarsson42b6e2d2019-10-21 11:51:05 +0200254 noise_fast_forward_ = rtc::saturated_cast<size_t>(noise_fast_forward_ +
255 excess_waiting_time_samp);
Henrik Lundin7687ad52018-07-02 10:14:46 +0200256 timestamp_diff =
257 rtc::saturated_cast<int32_t>(timestamp_diff + excess_waiting_time_samp);
258 }
259
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100260 if (timestamp_diff < 0 && prev_mode == NetEq::Mode::kRfc3389Cng) {
Henrik Lundin7687ad52018-07-02 10:14:46 +0200261 // Not time to play this packet yet. Wait another round before using this
262 // packet. Keep on playing CNG from previous CNG parameters.
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100263 return NetEq::Operation::kRfc3389CngNoPacket;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200264 } else {
265 // Otherwise, go for the CNG packet now.
266 noise_fast_forward_ = 0;
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100267 return NetEq::Operation::kRfc3389Cng;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200268 }
269}
270
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100271NetEq::Operation DecisionLogic::NoPacket(bool play_dtmf) {
Henrik Lundin7687ad52018-07-02 10:14:46 +0200272 if (cng_state_ == kCngRfc3389On) {
273 // Keep on playing comfort noise.
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100274 return NetEq::Operation::kRfc3389CngNoPacket;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200275 } else if (cng_state_ == kCngInternalOn) {
276 // Keep on playing codec internal comfort noise.
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100277 return NetEq::Operation::kCodecInternalCng;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200278 } else if (play_dtmf) {
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100279 return NetEq::Operation::kDtmf;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200280 } else {
281 // Nothing to play, do expand.
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100282 return NetEq::Operation::kExpand;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200283 }
284}
285
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100286NetEq::Operation DecisionLogic::ExpectedPacketAvailable(NetEq::Mode prev_mode,
287 bool play_dtmf) {
288 if (!disallow_time_stretching_ && prev_mode != NetEq::Mode::kExpand &&
289 !play_dtmf) {
Jakob Ivarsson80fb9782020-10-09 13:41:06 +0200290 const int samples_per_ms = sample_rate_ / 1000;
291 const int target_level_samples =
292 delay_manager_->TargetDelayMs() * samples_per_ms;
293 const int low_limit =
294 std::max(target_level_samples * 3 / 4,
295 target_level_samples -
296 kDecelerationTargetLevelOffsetMs * samples_per_ms);
297 // |higher_limit| is equal to |target_level|, but should at
298 // least be 20 ms higher than |lower_limit|.
299 const int high_limit =
300 std::max(target_level_samples, low_limit + 20 * samples_per_ms);
301
302 const int buffer_level_samples =
303 buffer_level_filter_.filtered_current_level();
304 if (buffer_level_samples >= high_limit << 2)
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100305 return NetEq::Operation::kFastAccelerate;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200306 if (TimescaleAllowed()) {
Jakob Ivarsson80fb9782020-10-09 13:41:06 +0200307 if (buffer_level_samples >= high_limit)
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100308 return NetEq::Operation::kAccelerate;
Jakob Ivarsson80fb9782020-10-09 13:41:06 +0200309 if (buffer_level_samples < low_limit)
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100310 return NetEq::Operation::kPreemptiveExpand;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200311 }
312 }
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100313 return NetEq::Operation::kNormal;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200314}
315
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100316NetEq::Operation DecisionLogic::FuturePacketAvailable(
Henrik Lundin7687ad52018-07-02 10:14:46 +0200317 size_t decoder_frame_length,
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100318 NetEq::Mode prev_mode,
Henrik Lundin7687ad52018-07-02 10:14:46 +0200319 uint32_t target_timestamp,
320 uint32_t available_timestamp,
321 bool play_dtmf,
Ivo Creusen53a31f72019-10-24 15:20:39 +0200322 size_t generated_noise_samples,
323 size_t span_samples_in_packet_buffer,
324 size_t num_packets_in_packet_buffer) {
Henrik Lundin7687ad52018-07-02 10:14:46 +0200325 // Required packet is not available, but a future packet is.
326 // Check if we should continue with an ongoing expand because the new packet
327 // is too far into the future.
328 uint32_t timestamp_leap = available_timestamp - target_timestamp;
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100329 if ((prev_mode == NetEq::Mode::kExpand ||
330 prev_mode == NetEq::Mode::kCodecPlc) &&
Henrik Lundin00eb12a2018-09-05 18:14:52 +0200331 !ReinitAfterExpands(timestamp_leap) && !MaxWaitForPacket() &&
332 PacketTooEarly(timestamp_leap) && UnderTargetLevel()) {
Henrik Lundin7687ad52018-07-02 10:14:46 +0200333 if (play_dtmf) {
334 // Still have DTMF to play, so do not do expand.
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100335 return NetEq::Operation::kDtmf;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200336 } else {
337 // Nothing to play.
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100338 return NetEq::Operation::kExpand;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200339 }
340 }
341
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100342 if (prev_mode == NetEq::Mode::kCodecPlc) {
343 return NetEq::Operation::kNormal;
Henrik Lundin00eb12a2018-09-05 18:14:52 +0200344 }
345
Henrik Lundin7687ad52018-07-02 10:14:46 +0200346 // If previous was comfort noise, then no merge is needed.
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100347 if (prev_mode == NetEq::Mode::kRfc3389Cng ||
348 prev_mode == NetEq::Mode::kCodecInternalCng) {
Jakob Ivarsson46dda832019-07-03 16:00:30 +0200349 size_t cur_size_samples =
350 estimate_dtx_delay_
Jakob Ivarsson80fb9782020-10-09 13:41:06 +0200351 ? span_samples_in_packet_buffer
Ivo Creusen53a31f72019-10-24 15:20:39 +0200352 : num_packets_in_packet_buffer * decoder_frame_length;
Jakob Ivarsson46dda832019-07-03 16:00:30 +0200353 // Target level is in number of packets in Q8.
354 const size_t target_level_samples =
Jakob Ivarsson80fb9782020-10-09 13:41:06 +0200355 delay_manager_->TargetDelayMs() * sample_rate_ / 1000;
Jakob Ivarsson46dda832019-07-03 16:00:30 +0200356 const bool generated_enough_noise =
357 static_cast<uint32_t>(generated_noise_samples + target_timestamp) >=
358 available_timestamp;
359
360 if (time_stretch_cn_) {
361 const size_t target_threshold_samples =
362 target_level_window_ms_ / 2 * (sample_rate_ / 1000);
363 const bool above_target_window =
364 cur_size_samples > target_level_samples + target_threshold_samples;
365 const bool below_target_window =
366 target_level_samples > target_threshold_samples &&
367 cur_size_samples < target_level_samples - target_threshold_samples;
368 // Keep the delay same as before CNG, but make sure that it is within the
369 // target window.
370 if ((generated_enough_noise && !below_target_window) ||
371 above_target_window) {
372 time_stretched_cn_samples_ = timestamp_leap - generated_noise_samples;
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100373 return NetEq::Operation::kNormal;
Jakob Ivarsson46dda832019-07-03 16:00:30 +0200374 }
Henrik Lundin7687ad52018-07-02 10:14:46 +0200375 } else {
Jakob Ivarsson46dda832019-07-03 16:00:30 +0200376 // Keep the same delay as before the CNG, but make sure that the number of
377 // samples in buffer is no higher than 4 times the optimal level.
378 if (generated_enough_noise ||
379 cur_size_samples > target_level_samples * 4) {
380 // Time to play this new packet.
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100381 return NetEq::Operation::kNormal;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200382 }
383 }
Jakob Ivarsson46dda832019-07-03 16:00:30 +0200384
385 // Too early to play this new packet; keep on playing comfort noise.
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100386 if (prev_mode == NetEq::Mode::kRfc3389Cng) {
387 return NetEq::Operation::kRfc3389CngNoPacket;
Jakob Ivarsson46dda832019-07-03 16:00:30 +0200388 }
389 // prevPlayMode == kModeCodecInternalCng.
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100390 return NetEq::Operation::kCodecInternalCng;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200391 }
Jakob Ivarsson46dda832019-07-03 16:00:30 +0200392
Henrik Lundin7687ad52018-07-02 10:14:46 +0200393 // Do not merge unless we have done an expand before.
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100394 if (prev_mode == NetEq::Mode::kExpand) {
395 return NetEq::Operation::kMerge;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200396 } else if (play_dtmf) {
397 // Play DTMF instead of expand.
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100398 return NetEq::Operation::kDtmf;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200399 } else {
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100400 return NetEq::Operation::kExpand;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200401 }
402}
403
404bool DecisionLogic::UnderTargetLevel() const {
Jakob Ivarsson80fb9782020-10-09 13:41:06 +0200405 return buffer_level_filter_.filtered_current_level() <
406 delay_manager_->TargetDelayMs() * sample_rate_ / 1000;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200407}
408
409bool DecisionLogic::ReinitAfterExpands(uint32_t timestamp_leap) const {
410 return timestamp_leap >=
411 static_cast<uint32_t>(output_size_samples_ * kReinitAfterExpands);
412}
413
414bool DecisionLogic::PacketTooEarly(uint32_t timestamp_leap) const {
415 return timestamp_leap >
416 static_cast<uint32_t>(output_size_samples_ * num_consecutive_expands_);
417}
418
419bool DecisionLogic::MaxWaitForPacket() const {
420 return num_consecutive_expands_ >= kMaxWaitForPacket;
421}
422
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000423} // namespace webrtc