blob: 8e1ffaf9feb03841d7c34aac551d6351379eec92 [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;
Minyue Li7f6417f2018-10-03 21:19:08 +020030
31} // namespace
32
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000033namespace webrtc {
34
Ivo Creusen53a31f72019-10-24 15:20:39 +020035DecisionLogic::DecisionLogic(NetEqController::Config config)
Jakob Ivarssonbd5874a2020-01-07 17:07:40 +010036 : delay_manager_(DelayManager::Create(config.max_packets_in_buffer,
Ivo Creusen53a31f72019-10-24 15:20:39 +020037 config.base_min_delay_ms,
38 config.enable_rtx_handling,
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;
Henrik Lundin47b17dc2016-05-10 10:20:59 +020070 timescale_countdown_.reset();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000071 num_consecutive_expands_ = 0;
Jakob Ivarsson46dda832019-07-03 16:00:30 +020072 time_stretched_cn_samples_ = 0;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000073}
74
75void DecisionLogic::SoftReset() {
76 packet_length_samples_ = 0;
77 sample_memory_ = 0;
78 prev_time_scale_ = false;
Henrik Lundin47b17dc2016-05-10 10:20:59 +020079 timescale_countdown_ =
80 tick_timer_->GetNewCountdown(kMinTimescaleInterval + 1);
Jakob Ivarsson46dda832019-07-03 16:00:30 +020081 time_stretched_cn_samples_ = 0;
Ivo Creusen53a31f72019-10-24 15:20:39 +020082 delay_manager_->Reset();
83 buffer_level_filter_.Reset();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000084}
85
Peter Kastingdce40cf2015-08-24 14:52:23 -070086void DecisionLogic::SetSampleRate(int fs_hz, size_t output_size_samples) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000087 // TODO(hlundin): Change to an enumerator and skip assert.
Yves Gerey665174f2018-06-19 15:03:05 +020088 assert(fs_hz == 8000 || fs_hz == 16000 || fs_hz == 32000 || fs_hz == 48000);
Jakob Ivarsson46dda832019-07-03 16:00:30 +020089 sample_rate_ = fs_hz;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000090 output_size_samples_ = output_size_samples;
91}
92
Ivo Creusen3ce44a32019-10-31 14:38:11 +010093NetEq::Operation DecisionLogic::GetDecision(const NetEqStatus& status,
94 bool* reset_decoder) {
ossu61a208b2016-09-20 01:38:00 -070095 // If last mode was CNG (or Expand, since this could be covering up for
96 // a lost CNG packet), remember that CNG is on. This is needed if comfort
97 // noise is interrupted by DTMF.
Ivo Creusen3ce44a32019-10-31 14:38:11 +010098 if (status.last_mode == NetEq::Mode::kRfc3389Cng) {
ossu61a208b2016-09-20 01:38:00 -070099 cng_state_ = kCngRfc3389On;
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100100 } else if (status.last_mode == NetEq::Mode::kCodecInternalCng) {
ossu61a208b2016-09-20 01:38:00 -0700101 cng_state_ = kCngInternalOn;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000102 }
103
Ivo Creusen53a31f72019-10-24 15:20:39 +0200104 size_t cur_size_samples = estimate_dtx_delay_
105 ? status.packet_buffer_info.span_samples
106 : status.packet_buffer_info.num_samples;
Yves Gerey665174f2018-06-19 15:03:05 +0200107 prev_time_scale_ =
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100108 prev_time_scale_ &&
109 (status.last_mode == NetEq::Mode::kAccelerateSuccess ||
110 status.last_mode == NetEq::Mode::kAccelerateLowEnergy ||
111 status.last_mode == NetEq::Mode::kPreemptiveExpandSuccess ||
112 status.last_mode == NetEq::Mode::kPreemptiveExpandLowEnergy);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000113
Minyue Li7d204d52019-04-16 11:44:49 +0200114 // Do not update buffer history if currently playing CNG since it will bias
115 // the filtered buffer level.
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100116 if (status.last_mode != NetEq::Mode::kRfc3389Cng &&
117 status.last_mode != NetEq::Mode::kCodecInternalCng &&
Ivo Creusen53a31f72019-10-24 15:20:39 +0200118 !(status.next_packet && status.next_packet->is_dtx &&
119 !estimate_dtx_delay_)) {
Minyue Li7d204d52019-04-16 11:44:49 +0200120 FilterBufferLevel(cur_size_samples);
121 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000122
Henrik Lundin7687ad52018-07-02 10:14:46 +0200123 // Guard for errors, to avoid getting stuck in error mode.
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100124 if (status.last_mode == NetEq::Mode::kError) {
Ivo Creusen53a31f72019-10-24 15:20:39 +0200125 if (!status.next_packet) {
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100126 return NetEq::Operation::kExpand;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200127 } else {
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100128 // Use kUndefined to flag for a reset.
129 return NetEq::Operation::kUndefined;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200130 }
131 }
132
Ivo Creusen53a31f72019-10-24 15:20:39 +0200133 if (status.next_packet && status.next_packet->is_cng) {
134 return CngOperation(status.last_mode, status.target_timestamp,
135 status.next_packet->timestamp,
136 status.generated_noise_samples);
Henrik Lundin7687ad52018-07-02 10:14:46 +0200137 }
138
139 // Handle the case with no packet at all available (except maybe DTMF).
Ivo Creusen53a31f72019-10-24 15:20:39 +0200140 if (!status.next_packet) {
141 return NoPacket(status.play_dtmf);
Henrik Lundin7687ad52018-07-02 10:14:46 +0200142 }
143
144 // If the expand period was very long, reset NetEQ since it is likely that the
145 // sender was restarted.
146 if (num_consecutive_expands_ > kReinitAfterExpands) {
147 *reset_decoder = true;
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100148 return NetEq::Operation::kNormal;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200149 }
150
151 // Make sure we don't restart audio too soon after an expansion to avoid
152 // running out of data right away again. We should only wait if there are no
153 // DTX or CNG packets in the buffer (otherwise we should just play out what we
154 // have, since we cannot know the exact duration of DTX or CNG packets), and
155 // if the mute factor is low enough (otherwise the expansion was short enough
156 // to not be noticable).
157 // Note that the MuteFactor is in Q14, so a value of 16384 corresponds to 1.
Ivo Creusen53a31f72019-10-24 15:20:39 +0200158 const size_t current_span =
159 estimate_dtx_delay_ ? status.packet_buffer_info.span_samples
160 : status.packet_buffer_info.span_samples_no_dtx;
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100161 if ((status.last_mode == NetEq::Mode::kExpand ||
162 status.last_mode == NetEq::Mode::kCodecPlc) &&
Ivo Creusen53a31f72019-10-24 15:20:39 +0200163 status.expand_mutefactor < 16384 / 2 &&
Jonas Olssona4d87372019-07-05 19:08:33 +0200164 current_span<static_cast<size_t>(delay_manager_->TargetLevel() *
165 packet_length_samples_ *
166 kPostponeDecodingLevel / 100)>> 8 &&
Ivo Creusen53a31f72019-10-24 15:20:39 +0200167 !status.packet_buffer_info.dtx_or_cng) {
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100168 return NetEq::Operation::kExpand;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200169 }
170
Jakob Ivarsson46dda832019-07-03 16:00:30 +0200171 const uint32_t five_seconds_samples = static_cast<uint32_t>(5 * sample_rate_);
Henrik Lundin7687ad52018-07-02 10:14:46 +0200172 // Check if the required packet is available.
Ivo Creusen53a31f72019-10-24 15:20:39 +0200173 if (status.target_timestamp == status.next_packet->timestamp) {
174 return ExpectedPacketAvailable(status.last_mode, status.play_dtmf);
175 } else if (!PacketBuffer::IsObsoleteTimestamp(status.next_packet->timestamp,
176 status.target_timestamp,
177 five_seconds_samples)) {
178 return FuturePacketAvailable(
179 status.last_packet_samples, status.last_mode, status.target_timestamp,
180 status.next_packet->timestamp, status.play_dtmf,
181 status.generated_noise_samples, status.packet_buffer_info.span_samples,
182 status.packet_buffer_info.num_packets);
Henrik Lundin7687ad52018-07-02 10:14:46 +0200183 } else {
184 // This implies that available_timestamp < target_timestamp, which can
185 // happen when a new stream or codec is received. Signal for a reset.
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100186 return NetEq::Operation::kUndefined;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200187 }
188}
189
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100190void DecisionLogic::ExpandDecision(NetEq::Operation operation) {
191 if (operation == NetEq::Operation::kExpand) {
Henrik Lundin5afa61c2018-07-02 14:53:24 +0200192 num_consecutive_expands_++;
193 } else {
194 num_consecutive_expands_ = 0;
195 }
196}
197
Ivo Creusen53a31f72019-10-24 15:20:39 +0200198absl::optional<int> DecisionLogic::PacketArrived(bool last_cng_or_dtmf,
199 size_t packet_length_samples,
200 bool should_update_stats,
201 uint16_t main_sequence_number,
202 uint32_t main_timestamp,
203 int fs_hz) {
204 delay_manager_->LastDecodedWasCngOrDtmf(last_cng_or_dtmf);
205 absl::optional<int> relative_delay;
206 if (delay_manager_->last_pack_cng_or_dtmf() == 0) {
207 // Calculate the total speech length carried in each packet.
208 if (packet_length_samples > 0 &&
209 packet_length_samples != packet_length_samples_) {
210 packet_length_samples_ = packet_length_samples;
211 delay_manager_->SetPacketAudioLength(
212 rtc::dchecked_cast<int>((1000 * packet_length_samples) / fs_hz));
213 }
214
215 // Update statistics.
216 if (should_update_stats) {
217 relative_delay =
218 delay_manager_->Update(main_sequence_number, main_timestamp, fs_hz);
219 }
220 } else if (delay_manager_->last_pack_cng_or_dtmf() == -1) {
221 // This is first "normal" packet after CNG or DTMF.
222 // Reset packet time counter and measure time until next packet,
223 // but don't update statistics.
224 delay_manager_->set_last_pack_cng_or_dtmf(0);
225 delay_manager_->ResetPacketIatCount();
226 }
227 return relative_delay;
228}
229
Minyue Li7d204d52019-04-16 11:44:49 +0200230void DecisionLogic::FilterBufferLevel(size_t buffer_size_samples) {
Ivo Creusen53a31f72019-10-24 15:20:39 +0200231 buffer_level_filter_.SetTargetBufferLevel(
Minyue Li7d204d52019-04-16 11:44:49 +0200232 delay_manager_->base_target_level());
Henrik Lundin5afa61c2018-07-02 14:53:24 +0200233
Jakob Ivarsson46dda832019-07-03 16:00:30 +0200234 int time_stretched_samples = time_stretched_cn_samples_;
Minyue Li7d204d52019-04-16 11:44:49 +0200235 if (prev_time_scale_) {
Jakob Ivarsson46dda832019-07-03 16:00:30 +0200236 time_stretched_samples += sample_memory_;
Minyue Li7d204d52019-04-16 11:44:49 +0200237 timescale_countdown_ = tick_timer_->GetNewCountdown(kMinTimescaleInterval);
238 }
239
Ivo Creusen53a31f72019-10-24 15:20:39 +0200240 buffer_level_filter_.Update(buffer_size_samples, time_stretched_samples);
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);
253 int32_t optimal_level_samp = static_cast<int32_t>(
254 (delay_manager_->TargetLevel() * packet_length_samples_) >> 8);
255 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 Ivarssona36c5912019-06-27 10:12:02 +0200298 // Check criterion for time-stretching. The values are in number of packets
299 // in Q8.
Henrik Lundin7687ad52018-07-02 10:14:46 +0200300 int low_limit, high_limit;
301 delay_manager_->BufferLimits(&low_limit, &high_limit);
Jakob Ivarssona36c5912019-06-27 10:12:02 +0200302 int buffer_level_packets = 0;
303 if (packet_length_samples_ > 0) {
304 buffer_level_packets =
Ivo Creusen53a31f72019-10-24 15:20:39 +0200305 ((1 << 8) * buffer_level_filter_.filtered_current_level()) /
Jakob Ivarssona36c5912019-06-27 10:12:02 +0200306 packet_length_samples_;
307 }
308 if (buffer_level_packets >= high_limit << 2)
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100309 return NetEq::Operation::kFastAccelerate;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200310 if (TimescaleAllowed()) {
Jakob Ivarssona36c5912019-06-27 10:12:02 +0200311 if (buffer_level_packets >= high_limit)
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100312 return NetEq::Operation::kAccelerate;
Jakob Ivarssona36c5912019-06-27 10:12:02 +0200313 if (buffer_level_packets < low_limit)
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100314 return NetEq::Operation::kPreemptiveExpand;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200315 }
316 }
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100317 return NetEq::Operation::kNormal;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200318}
319
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100320NetEq::Operation DecisionLogic::FuturePacketAvailable(
Henrik Lundin7687ad52018-07-02 10:14:46 +0200321 size_t decoder_frame_length,
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100322 NetEq::Mode prev_mode,
Henrik Lundin7687ad52018-07-02 10:14:46 +0200323 uint32_t target_timestamp,
324 uint32_t available_timestamp,
325 bool play_dtmf,
Ivo Creusen53a31f72019-10-24 15:20:39 +0200326 size_t generated_noise_samples,
327 size_t span_samples_in_packet_buffer,
328 size_t num_packets_in_packet_buffer) {
Henrik Lundin7687ad52018-07-02 10:14:46 +0200329 // Required packet is not available, but a future packet is.
330 // Check if we should continue with an ongoing expand because the new packet
331 // is too far into the future.
332 uint32_t timestamp_leap = available_timestamp - target_timestamp;
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100333 if ((prev_mode == NetEq::Mode::kExpand ||
334 prev_mode == NetEq::Mode::kCodecPlc) &&
Henrik Lundin00eb12a2018-09-05 18:14:52 +0200335 !ReinitAfterExpands(timestamp_leap) && !MaxWaitForPacket() &&
336 PacketTooEarly(timestamp_leap) && UnderTargetLevel()) {
Henrik Lundin7687ad52018-07-02 10:14:46 +0200337 if (play_dtmf) {
338 // Still have DTMF to play, so do not do expand.
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100339 return NetEq::Operation::kDtmf;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200340 } else {
341 // Nothing to play.
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100342 return NetEq::Operation::kExpand;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200343 }
344 }
345
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100346 if (prev_mode == NetEq::Mode::kCodecPlc) {
347 return NetEq::Operation::kNormal;
Henrik Lundin00eb12a2018-09-05 18:14:52 +0200348 }
349
Henrik Lundin7687ad52018-07-02 10:14:46 +0200350 // If previous was comfort noise, then no merge is needed.
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100351 if (prev_mode == NetEq::Mode::kRfc3389Cng ||
352 prev_mode == NetEq::Mode::kCodecInternalCng) {
Jakob Ivarsson46dda832019-07-03 16:00:30 +0200353 size_t cur_size_samples =
354 estimate_dtx_delay_
Ivo Creusen53a31f72019-10-24 15:20:39 +0200355 ? cur_size_samples = span_samples_in_packet_buffer
356 : num_packets_in_packet_buffer * decoder_frame_length;
Jakob Ivarsson46dda832019-07-03 16:00:30 +0200357 // Target level is in number of packets in Q8.
358 const size_t target_level_samples =
359 (delay_manager_->TargetLevel() * packet_length_samples_) >> 8;
360 const bool generated_enough_noise =
361 static_cast<uint32_t>(generated_noise_samples + target_timestamp) >=
362 available_timestamp;
363
364 if (time_stretch_cn_) {
365 const size_t target_threshold_samples =
366 target_level_window_ms_ / 2 * (sample_rate_ / 1000);
367 const bool above_target_window =
368 cur_size_samples > target_level_samples + target_threshold_samples;
369 const bool below_target_window =
370 target_level_samples > target_threshold_samples &&
371 cur_size_samples < target_level_samples - target_threshold_samples;
372 // Keep the delay same as before CNG, but make sure that it is within the
373 // target window.
374 if ((generated_enough_noise && !below_target_window) ||
375 above_target_window) {
376 time_stretched_cn_samples_ = timestamp_leap - generated_noise_samples;
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100377 return NetEq::Operation::kNormal;
Jakob Ivarsson46dda832019-07-03 16:00:30 +0200378 }
Henrik Lundin7687ad52018-07-02 10:14:46 +0200379 } else {
Jakob Ivarsson46dda832019-07-03 16:00:30 +0200380 // Keep the same delay as before the CNG, but make sure that the number of
381 // samples in buffer is no higher than 4 times the optimal level.
382 if (generated_enough_noise ||
383 cur_size_samples > target_level_samples * 4) {
384 // Time to play this new packet.
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100385 return NetEq::Operation::kNormal;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200386 }
387 }
Jakob Ivarsson46dda832019-07-03 16:00:30 +0200388
389 // Too early to play this new packet; keep on playing comfort noise.
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100390 if (prev_mode == NetEq::Mode::kRfc3389Cng) {
391 return NetEq::Operation::kRfc3389CngNoPacket;
Jakob Ivarsson46dda832019-07-03 16:00:30 +0200392 }
393 // prevPlayMode == kModeCodecInternalCng.
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100394 return NetEq::Operation::kCodecInternalCng;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200395 }
Jakob Ivarsson46dda832019-07-03 16:00:30 +0200396
Henrik Lundin7687ad52018-07-02 10:14:46 +0200397 // Do not merge unless we have done an expand before.
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100398 if (prev_mode == NetEq::Mode::kExpand) {
399 return NetEq::Operation::kMerge;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200400 } else if (play_dtmf) {
401 // Play DTMF instead of expand.
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100402 return NetEq::Operation::kDtmf;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200403 } else {
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100404 return NetEq::Operation::kExpand;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200405 }
406}
407
408bool DecisionLogic::UnderTargetLevel() const {
Jakob Ivarssona36c5912019-06-27 10:12:02 +0200409 int buffer_level_packets = 0;
410 if (packet_length_samples_ > 0) {
411 buffer_level_packets =
Ivo Creusen53a31f72019-10-24 15:20:39 +0200412 ((1 << 8) * buffer_level_filter_.filtered_current_level()) /
Jakob Ivarssona36c5912019-06-27 10:12:02 +0200413 packet_length_samples_;
414 }
415 return buffer_level_packets <= delay_manager_->TargetLevel();
Henrik Lundin7687ad52018-07-02 10:14:46 +0200416}
417
418bool DecisionLogic::ReinitAfterExpands(uint32_t timestamp_leap) const {
419 return timestamp_leap >=
420 static_cast<uint32_t>(output_size_samples_ * kReinitAfterExpands);
421}
422
423bool DecisionLogic::PacketTooEarly(uint32_t timestamp_leap) const {
424 return timestamp_leap >
425 static_cast<uint32_t>(output_size_samples_ * num_consecutive_expands_);
426}
427
428bool DecisionLogic::MaxWaitForPacket() const {
429 return num_consecutive_expands_ >= kMaxWaitForPacket;
430}
431
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000432} // namespace webrtc