blob: 5c746ad96f05cec25f15e7d7fd9c17989456edd0 [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)
36 : delay_peak_detector_(config.tick_timer, config.enable_rtx_handling),
37 delay_manager_(DelayManager::Create(config.max_packets_in_buffer,
38 config.base_min_delay_ms,
39 config.enable_rtx_handling,
40 &delay_peak_detector_,
41 config.tick_timer)),
42 tick_timer_(config.tick_timer),
43 disallow_time_stretching_(!config.allow_time_stretching),
Henrik Lundin47b17dc2016-05-10 10:20:59 +020044 timescale_countdown_(
45 tick_timer_->GetNewCountdown(kMinTimescaleInterval + 1)),
Jakob Ivarsson46dda832019-07-03 16:00:30 +020046 estimate_dtx_delay_("estimate_dtx_delay", false),
47 time_stretch_cn_("time_stretch_cn", false),
48 target_level_window_ms_("target_level_window",
49 kDefaultTargetLevelWindowMs,
50 0,
51 absl::nullopt) {
Jakob Ivarsson46dda832019-07-03 16:00:30 +020052 const std::string field_trial_name =
53 field_trial::FindFullName("WebRTC-Audio-NetEqDecisionLogicSettings");
54 ParseFieldTrial(
55 {&estimate_dtx_delay_, &time_stretch_cn_, &target_level_window_ms_},
56 field_trial_name);
57 RTC_LOG(LS_INFO) << "NetEq decision logic settings:"
58 << " estimate_dtx_delay=" << estimate_dtx_delay_
59 << " time_stretch_cn=" << time_stretch_cn_
60 << " target_level_window_ms=" << target_level_window_ms_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000061}
62
Henrik Lundin47b17dc2016-05-10 10:20:59 +020063DecisionLogic::~DecisionLogic() = default;
64
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000065void DecisionLogic::Reset() {
66 cng_state_ = kCngOff;
henrik.lundinb1fb72b2016-05-03 08:18:47 -070067 noise_fast_forward_ = 0;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000068 packet_length_samples_ = 0;
69 sample_memory_ = 0;
70 prev_time_scale_ = false;
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;
Henrik Lundin47b17dc2016-05-10 10:20:59 +020080 timescale_countdown_ =
81 tick_timer_->GetNewCountdown(kMinTimescaleInterval + 1);
Jakob Ivarsson46dda832019-07-03 16:00:30 +020082 time_stretched_cn_samples_ = 0;
Ivo Creusen53a31f72019-10-24 15:20:39 +020083 delay_manager_->Reset();
84 buffer_level_filter_.Reset();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000085}
86
Peter Kastingdce40cf2015-08-24 14:52:23 -070087void DecisionLogic::SetSampleRate(int fs_hz, size_t output_size_samples) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000088 // TODO(hlundin): Change to an enumerator and skip assert.
Yves Gerey665174f2018-06-19 15:03:05 +020089 assert(fs_hz == 8000 || fs_hz == 16000 || fs_hz == 32000 || fs_hz == 48000);
Jakob Ivarsson46dda832019-07-03 16:00:30 +020090 sample_rate_ = fs_hz;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000091 output_size_samples_ = output_size_samples;
92}
93
Ivo Creusen3ce44a32019-10-31 14:38:11 +010094NetEq::Operation DecisionLogic::GetDecision(const NetEqStatus& status,
95 bool* reset_decoder) {
ossu61a208b2016-09-20 01:38:00 -070096 // If last mode was CNG (or Expand, since this could be covering up for
97 // a lost CNG packet), remember that CNG is on. This is needed if comfort
98 // noise is interrupted by DTMF.
Ivo Creusen3ce44a32019-10-31 14:38:11 +010099 if (status.last_mode == NetEq::Mode::kRfc3389Cng) {
ossu61a208b2016-09-20 01:38:00 -0700100 cng_state_ = kCngRfc3389On;
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100101 } else if (status.last_mode == NetEq::Mode::kCodecInternalCng) {
ossu61a208b2016-09-20 01:38:00 -0700102 cng_state_ = kCngInternalOn;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000103 }
104
Ivo Creusen53a31f72019-10-24 15:20:39 +0200105 size_t cur_size_samples = estimate_dtx_delay_
106 ? status.packet_buffer_info.span_samples
107 : status.packet_buffer_info.num_samples;
Yves Gerey665174f2018-06-19 15:03:05 +0200108 prev_time_scale_ =
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100109 prev_time_scale_ &&
110 (status.last_mode == NetEq::Mode::kAccelerateSuccess ||
111 status.last_mode == NetEq::Mode::kAccelerateLowEnergy ||
112 status.last_mode == NetEq::Mode::kPreemptiveExpandSuccess ||
113 status.last_mode == NetEq::Mode::kPreemptiveExpandLowEnergy);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000114
Minyue Li7d204d52019-04-16 11:44:49 +0200115 // Do not update buffer history if currently playing CNG since it will bias
116 // the filtered buffer level.
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100117 if (status.last_mode != NetEq::Mode::kRfc3389Cng &&
118 status.last_mode != NetEq::Mode::kCodecInternalCng &&
Ivo Creusen53a31f72019-10-24 15:20:39 +0200119 !(status.next_packet && status.next_packet->is_dtx &&
120 !estimate_dtx_delay_)) {
Minyue Li7d204d52019-04-16 11:44:49 +0200121 FilterBufferLevel(cur_size_samples);
122 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000123
Henrik Lundin7687ad52018-07-02 10:14:46 +0200124 // Guard for errors, to avoid getting stuck in error mode.
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100125 if (status.last_mode == NetEq::Mode::kError) {
Ivo Creusen53a31f72019-10-24 15:20:39 +0200126 if (!status.next_packet) {
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100127 return NetEq::Operation::kExpand;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200128 } else {
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100129 // Use kUndefined to flag for a reset.
130 return NetEq::Operation::kUndefined;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200131 }
132 }
133
Ivo Creusen53a31f72019-10-24 15:20:39 +0200134 if (status.next_packet && status.next_packet->is_cng) {
135 return CngOperation(status.last_mode, status.target_timestamp,
136 status.next_packet->timestamp,
137 status.generated_noise_samples);
Henrik Lundin7687ad52018-07-02 10:14:46 +0200138 }
139
140 // Handle the case with no packet at all available (except maybe DTMF).
Ivo Creusen53a31f72019-10-24 15:20:39 +0200141 if (!status.next_packet) {
142 return NoPacket(status.play_dtmf);
Henrik Lundin7687ad52018-07-02 10:14:46 +0200143 }
144
145 // If the expand period was very long, reset NetEQ since it is likely that the
146 // sender was restarted.
147 if (num_consecutive_expands_ > kReinitAfterExpands) {
148 *reset_decoder = true;
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100149 return NetEq::Operation::kNormal;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200150 }
151
152 // Make sure we don't restart audio too soon after an expansion to avoid
153 // running out of data right away again. We should only wait if there are no
154 // DTX or CNG packets in the buffer (otherwise we should just play out what we
155 // have, since we cannot know the exact duration of DTX or CNG packets), and
156 // if the mute factor is low enough (otherwise the expansion was short enough
157 // to not be noticable).
158 // Note that the MuteFactor is in Q14, so a value of 16384 corresponds to 1.
Ivo Creusen53a31f72019-10-24 15:20:39 +0200159 const size_t current_span =
160 estimate_dtx_delay_ ? status.packet_buffer_info.span_samples
161 : status.packet_buffer_info.span_samples_no_dtx;
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100162 if ((status.last_mode == NetEq::Mode::kExpand ||
163 status.last_mode == NetEq::Mode::kCodecPlc) &&
Ivo Creusen53a31f72019-10-24 15:20:39 +0200164 status.expand_mutefactor < 16384 / 2 &&
Jonas Olssona4d87372019-07-05 19:08:33 +0200165 current_span<static_cast<size_t>(delay_manager_->TargetLevel() *
166 packet_length_samples_ *
167 kPostponeDecodingLevel / 100)>> 8 &&
Ivo Creusen53a31f72019-10-24 15:20:39 +0200168 !status.packet_buffer_info.dtx_or_cng) {
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100169 return NetEq::Operation::kExpand;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200170 }
171
Jakob Ivarsson46dda832019-07-03 16:00:30 +0200172 const uint32_t five_seconds_samples = static_cast<uint32_t>(5 * sample_rate_);
Henrik Lundin7687ad52018-07-02 10:14:46 +0200173 // Check if the required packet is available.
Ivo Creusen53a31f72019-10-24 15:20:39 +0200174 if (status.target_timestamp == status.next_packet->timestamp) {
175 return ExpectedPacketAvailable(status.last_mode, status.play_dtmf);
176 } else if (!PacketBuffer::IsObsoleteTimestamp(status.next_packet->timestamp,
177 status.target_timestamp,
178 five_seconds_samples)) {
179 return FuturePacketAvailable(
180 status.last_packet_samples, status.last_mode, status.target_timestamp,
181 status.next_packet->timestamp, status.play_dtmf,
182 status.generated_noise_samples, status.packet_buffer_info.span_samples,
183 status.packet_buffer_info.num_packets);
Henrik Lundin7687ad52018-07-02 10:14:46 +0200184 } else {
185 // This implies that available_timestamp < target_timestamp, which can
186 // happen when a new stream or codec is received. Signal for a reset.
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100187 return NetEq::Operation::kUndefined;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200188 }
189}
190
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100191void DecisionLogic::ExpandDecision(NetEq::Operation operation) {
192 if (operation == NetEq::Operation::kExpand) {
Henrik Lundin5afa61c2018-07-02 14:53:24 +0200193 num_consecutive_expands_++;
194 } else {
195 num_consecutive_expands_ = 0;
196 }
197}
198
Ivo Creusen53a31f72019-10-24 15:20:39 +0200199absl::optional<int> DecisionLogic::PacketArrived(bool last_cng_or_dtmf,
200 size_t packet_length_samples,
201 bool should_update_stats,
202 uint16_t main_sequence_number,
203 uint32_t main_timestamp,
204 int fs_hz) {
205 delay_manager_->LastDecodedWasCngOrDtmf(last_cng_or_dtmf);
206 absl::optional<int> relative_delay;
207 if (delay_manager_->last_pack_cng_or_dtmf() == 0) {
208 // Calculate the total speech length carried in each packet.
209 if (packet_length_samples > 0 &&
210 packet_length_samples != packet_length_samples_) {
211 packet_length_samples_ = packet_length_samples;
212 delay_manager_->SetPacketAudioLength(
213 rtc::dchecked_cast<int>((1000 * packet_length_samples) / fs_hz));
214 }
215
216 // Update statistics.
217 if (should_update_stats) {
218 relative_delay =
219 delay_manager_->Update(main_sequence_number, main_timestamp, fs_hz);
220 }
221 } else if (delay_manager_->last_pack_cng_or_dtmf() == -1) {
222 // This is first "normal" packet after CNG or DTMF.
223 // Reset packet time counter and measure time until next packet,
224 // but don't update statistics.
225 delay_manager_->set_last_pack_cng_or_dtmf(0);
226 delay_manager_->ResetPacketIatCount();
227 }
228 return relative_delay;
229}
230
Minyue Li7d204d52019-04-16 11:44:49 +0200231void DecisionLogic::FilterBufferLevel(size_t buffer_size_samples) {
Ivo Creusen53a31f72019-10-24 15:20:39 +0200232 buffer_level_filter_.SetTargetBufferLevel(
Minyue Li7d204d52019-04-16 11:44:49 +0200233 delay_manager_->base_target_level());
Henrik Lundin5afa61c2018-07-02 14:53:24 +0200234
Jakob Ivarsson46dda832019-07-03 16:00:30 +0200235 int time_stretched_samples = time_stretched_cn_samples_;
Minyue Li7d204d52019-04-16 11:44:49 +0200236 if (prev_time_scale_) {
Jakob Ivarsson46dda832019-07-03 16:00:30 +0200237 time_stretched_samples += sample_memory_;
Minyue Li7d204d52019-04-16 11:44:49 +0200238 timescale_countdown_ = tick_timer_->GetNewCountdown(kMinTimescaleInterval);
239 }
240
Ivo Creusen53a31f72019-10-24 15:20:39 +0200241 buffer_level_filter_.Update(buffer_size_samples, time_stretched_samples);
Minyue Li7d204d52019-04-16 11:44:49 +0200242 prev_time_scale_ = false;
Jakob Ivarsson46dda832019-07-03 16:00:30 +0200243 time_stretched_cn_samples_ = 0;
Henrik Lundin5afa61c2018-07-02 14:53:24 +0200244}
245
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100246NetEq::Operation DecisionLogic::CngOperation(NetEq::Mode prev_mode,
247 uint32_t target_timestamp,
248 uint32_t available_timestamp,
249 size_t generated_noise_samples) {
Henrik Lundin7687ad52018-07-02 10:14:46 +0200250 // Signed difference between target and available timestamp.
251 int32_t timestamp_diff = static_cast<int32_t>(
252 static_cast<uint32_t>(generated_noise_samples + target_timestamp) -
253 available_timestamp);
254 int32_t optimal_level_samp = static_cast<int32_t>(
255 (delay_manager_->TargetLevel() * packet_length_samples_) >> 8);
256 const int64_t excess_waiting_time_samp =
257 -static_cast<int64_t>(timestamp_diff) - optimal_level_samp;
258
259 if (excess_waiting_time_samp > optimal_level_samp / 2) {
260 // The waiting time for this packet will be longer than 1.5
261 // times the wanted buffer delay. Apply fast-forward to cut the
262 // waiting time down to the optimal.
Jakob Ivarsson42b6e2d2019-10-21 11:51:05 +0200263 noise_fast_forward_ = rtc::saturated_cast<size_t>(noise_fast_forward_ +
264 excess_waiting_time_samp);
Henrik Lundin7687ad52018-07-02 10:14:46 +0200265 timestamp_diff =
266 rtc::saturated_cast<int32_t>(timestamp_diff + excess_waiting_time_samp);
267 }
268
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100269 if (timestamp_diff < 0 && prev_mode == NetEq::Mode::kRfc3389Cng) {
Henrik Lundin7687ad52018-07-02 10:14:46 +0200270 // Not time to play this packet yet. Wait another round before using this
271 // packet. Keep on playing CNG from previous CNG parameters.
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100272 return NetEq::Operation::kRfc3389CngNoPacket;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200273 } else {
274 // Otherwise, go for the CNG packet now.
275 noise_fast_forward_ = 0;
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100276 return NetEq::Operation::kRfc3389Cng;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200277 }
278}
279
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100280NetEq::Operation DecisionLogic::NoPacket(bool play_dtmf) {
Henrik Lundin7687ad52018-07-02 10:14:46 +0200281 if (cng_state_ == kCngRfc3389On) {
282 // Keep on playing comfort noise.
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100283 return NetEq::Operation::kRfc3389CngNoPacket;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200284 } else if (cng_state_ == kCngInternalOn) {
285 // Keep on playing codec internal comfort noise.
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100286 return NetEq::Operation::kCodecInternalCng;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200287 } else if (play_dtmf) {
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100288 return NetEq::Operation::kDtmf;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200289 } else {
290 // Nothing to play, do expand.
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100291 return NetEq::Operation::kExpand;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200292 }
293}
294
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100295NetEq::Operation DecisionLogic::ExpectedPacketAvailable(NetEq::Mode prev_mode,
296 bool play_dtmf) {
297 if (!disallow_time_stretching_ && prev_mode != NetEq::Mode::kExpand &&
298 !play_dtmf) {
Jakob Ivarssona36c5912019-06-27 10:12:02 +0200299 // Check criterion for time-stretching. The values are in number of packets
300 // in Q8.
Henrik Lundin7687ad52018-07-02 10:14:46 +0200301 int low_limit, high_limit;
302 delay_manager_->BufferLimits(&low_limit, &high_limit);
Jakob Ivarssona36c5912019-06-27 10:12:02 +0200303 int buffer_level_packets = 0;
304 if (packet_length_samples_ > 0) {
305 buffer_level_packets =
Ivo Creusen53a31f72019-10-24 15:20:39 +0200306 ((1 << 8) * buffer_level_filter_.filtered_current_level()) /
Jakob Ivarssona36c5912019-06-27 10:12:02 +0200307 packet_length_samples_;
308 }
309 if (buffer_level_packets >= high_limit << 2)
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100310 return NetEq::Operation::kFastAccelerate;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200311 if (TimescaleAllowed()) {
Jakob Ivarssona36c5912019-06-27 10:12:02 +0200312 if (buffer_level_packets >= high_limit)
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100313 return NetEq::Operation::kAccelerate;
Jakob Ivarssona36c5912019-06-27 10:12:02 +0200314 if (buffer_level_packets < low_limit)
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100315 return NetEq::Operation::kPreemptiveExpand;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200316 }
317 }
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100318 return NetEq::Operation::kNormal;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200319}
320
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100321NetEq::Operation DecisionLogic::FuturePacketAvailable(
Henrik Lundin7687ad52018-07-02 10:14:46 +0200322 size_t decoder_frame_length,
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100323 NetEq::Mode prev_mode,
Henrik Lundin7687ad52018-07-02 10:14:46 +0200324 uint32_t target_timestamp,
325 uint32_t available_timestamp,
326 bool play_dtmf,
Ivo Creusen53a31f72019-10-24 15:20:39 +0200327 size_t generated_noise_samples,
328 size_t span_samples_in_packet_buffer,
329 size_t num_packets_in_packet_buffer) {
Henrik Lundin7687ad52018-07-02 10:14:46 +0200330 // Required packet is not available, but a future packet is.
331 // Check if we should continue with an ongoing expand because the new packet
332 // is too far into the future.
333 uint32_t timestamp_leap = available_timestamp - target_timestamp;
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100334 if ((prev_mode == NetEq::Mode::kExpand ||
335 prev_mode == NetEq::Mode::kCodecPlc) &&
Henrik Lundin00eb12a2018-09-05 18:14:52 +0200336 !ReinitAfterExpands(timestamp_leap) && !MaxWaitForPacket() &&
337 PacketTooEarly(timestamp_leap) && UnderTargetLevel()) {
Henrik Lundin7687ad52018-07-02 10:14:46 +0200338 if (play_dtmf) {
339 // Still have DTMF to play, so do not do expand.
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100340 return NetEq::Operation::kDtmf;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200341 } else {
342 // Nothing to play.
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100343 return NetEq::Operation::kExpand;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200344 }
345 }
346
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100347 if (prev_mode == NetEq::Mode::kCodecPlc) {
348 return NetEq::Operation::kNormal;
Henrik Lundin00eb12a2018-09-05 18:14:52 +0200349 }
350
Henrik Lundin7687ad52018-07-02 10:14:46 +0200351 // If previous was comfort noise, then no merge is needed.
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100352 if (prev_mode == NetEq::Mode::kRfc3389Cng ||
353 prev_mode == NetEq::Mode::kCodecInternalCng) {
Jakob Ivarsson46dda832019-07-03 16:00:30 +0200354 size_t cur_size_samples =
355 estimate_dtx_delay_
Ivo Creusen53a31f72019-10-24 15:20:39 +0200356 ? cur_size_samples = span_samples_in_packet_buffer
357 : num_packets_in_packet_buffer * decoder_frame_length;
Jakob Ivarsson46dda832019-07-03 16:00:30 +0200358 // Target level is in number of packets in Q8.
359 const size_t target_level_samples =
360 (delay_manager_->TargetLevel() * packet_length_samples_) >> 8;
361 const bool generated_enough_noise =
362 static_cast<uint32_t>(generated_noise_samples + target_timestamp) >=
363 available_timestamp;
364
365 if (time_stretch_cn_) {
366 const size_t target_threshold_samples =
367 target_level_window_ms_ / 2 * (sample_rate_ / 1000);
368 const bool above_target_window =
369 cur_size_samples > target_level_samples + target_threshold_samples;
370 const bool below_target_window =
371 target_level_samples > target_threshold_samples &&
372 cur_size_samples < target_level_samples - target_threshold_samples;
373 // Keep the delay same as before CNG, but make sure that it is within the
374 // target window.
375 if ((generated_enough_noise && !below_target_window) ||
376 above_target_window) {
377 time_stretched_cn_samples_ = timestamp_leap - generated_noise_samples;
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100378 return NetEq::Operation::kNormal;
Jakob Ivarsson46dda832019-07-03 16:00:30 +0200379 }
Henrik Lundin7687ad52018-07-02 10:14:46 +0200380 } else {
Jakob Ivarsson46dda832019-07-03 16:00:30 +0200381 // Keep the same delay as before the CNG, but make sure that the number of
382 // samples in buffer is no higher than 4 times the optimal level.
383 if (generated_enough_noise ||
384 cur_size_samples > target_level_samples * 4) {
385 // Time to play this new packet.
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100386 return NetEq::Operation::kNormal;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200387 }
388 }
Jakob Ivarsson46dda832019-07-03 16:00:30 +0200389
390 // Too early to play this new packet; keep on playing comfort noise.
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100391 if (prev_mode == NetEq::Mode::kRfc3389Cng) {
392 return NetEq::Operation::kRfc3389CngNoPacket;
Jakob Ivarsson46dda832019-07-03 16:00:30 +0200393 }
394 // prevPlayMode == kModeCodecInternalCng.
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100395 return NetEq::Operation::kCodecInternalCng;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200396 }
Jakob Ivarsson46dda832019-07-03 16:00:30 +0200397
Henrik Lundin7687ad52018-07-02 10:14:46 +0200398 // Do not merge unless we have done an expand before.
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100399 if (prev_mode == NetEq::Mode::kExpand) {
400 return NetEq::Operation::kMerge;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200401 } else if (play_dtmf) {
402 // Play DTMF instead of expand.
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100403 return NetEq::Operation::kDtmf;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200404 } else {
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100405 return NetEq::Operation::kExpand;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200406 }
407}
408
409bool DecisionLogic::UnderTargetLevel() const {
Jakob Ivarssona36c5912019-06-27 10:12:02 +0200410 int buffer_level_packets = 0;
411 if (packet_length_samples_ > 0) {
412 buffer_level_packets =
Ivo Creusen53a31f72019-10-24 15:20:39 +0200413 ((1 << 8) * buffer_level_filter_.filtered_current_level()) /
Jakob Ivarssona36c5912019-06-27 10:12:02 +0200414 packet_length_samples_;
415 }
416 return buffer_level_packets <= delay_manager_->TargetLevel();
Henrik Lundin7687ad52018-07-02 10:14:46 +0200417}
418
419bool DecisionLogic::ReinitAfterExpands(uint32_t timestamp_leap) const {
420 return timestamp_leap >=
421 static_cast<uint32_t>(output_size_samples_ * kReinitAfterExpands);
422}
423
424bool DecisionLogic::PacketTooEarly(uint32_t timestamp_leap) const {
425 return timestamp_leap >
426 static_cast<uint32_t>(output_size_samples_ * num_consecutive_expands_);
427}
428
429bool DecisionLogic::MaxWaitForPacket() const {
430 return num_consecutive_expands_ >= kMaxWaitForPacket;
431}
432
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000433} // namespace webrtc