henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 1 | /* |
| 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 Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "modules/audio_coding/neteq/decision_logic.h" |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 12 | |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 13 | #include <stdio.h> |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 14 | |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 15 | #include <string> |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 16 | |
Jakob Ivarsson | 46dda83 | 2019-07-03 16:00:30 +0200 | [diff] [blame] | 17 | #include "absl/types/optional.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 18 | #include "modules/audio_coding/neteq/packet_buffer.h" |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 19 | #include "rtc_base/checks.h" |
Jakob Ivarsson | 46dda83 | 2019-07-03 16:00:30 +0200 | [diff] [blame] | 20 | #include "rtc_base/experiments/field_trial_parser.h" |
Minyue Li | 7f6417f | 2018-10-03 21:19:08 +0200 | [diff] [blame] | 21 | #include "rtc_base/logging.h" |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 22 | #include "rtc_base/numerics/safe_conversions.h" |
Jakob Ivarsson | 46dda83 | 2019-07-03 16:00:30 +0200 | [diff] [blame] | 23 | #include "system_wrappers/include/field_trial.h" |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 24 | |
Jakob Ivarsson | 74158ff | 2021-09-07 14:24:56 +0200 | [diff] [blame] | 25 | namespace webrtc { |
| 26 | |
Minyue Li | 7f6417f | 2018-10-03 21:19:08 +0200 | [diff] [blame] | 27 | namespace { |
Minyue Li | 7f6417f | 2018-10-03 21:19:08 +0200 | [diff] [blame] | 28 | |
Jakob Ivarsson | d3a780b | 2019-02-28 14:30:21 +0100 | [diff] [blame] | 29 | constexpr int kPostponeDecodingLevel = 50; |
Jakob Ivarsson | 46dda83 | 2019-07-03 16:00:30 +0200 | [diff] [blame] | 30 | constexpr int kDefaultTargetLevelWindowMs = 100; |
Jakob Ivarsson | 80fb978 | 2020-10-09 13:41:06 +0200 | [diff] [blame] | 31 | constexpr int kDecelerationTargetLevelOffsetMs = 85; |
Minyue Li | 7f6417f | 2018-10-03 21:19:08 +0200 | [diff] [blame] | 32 | |
Jakob Ivarsson | 74158ff | 2021-09-07 14:24:56 +0200 | [diff] [blame] | 33 | std::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 Li | 7f6417f | 2018-10-03 21:19:08 +0200 | [diff] [blame] | 41 | |
Jakob Ivarsson | 74158ff | 2021-09-07 14:24:56 +0200 | [diff] [blame] | 42 | } // namespace |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 43 | |
Ivo Creusen | 53a31f7 | 2019-10-24 15:20:39 +0200 | [diff] [blame] | 44 | DecisionLogic::DecisionLogic(NetEqController::Config config) |
Jakob Ivarsson | 609b047 | 2020-10-19 09:19:34 +0200 | [diff] [blame] | 45 | : DecisionLogic(config, |
Jakob Ivarsson | 74158ff | 2021-09-07 14:24:56 +0200 | [diff] [blame] | 46 | CreateDelayManager(config), |
Jakob Ivarsson | 609b047 | 2020-10-19 09:19:34 +0200 | [diff] [blame] | 47 | std::make_unique<BufferLevelFilter>()) {} |
| 48 | |
| 49 | DecisionLogic::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 Creusen | 53a31f7 | 2019-10-24 15:20:39 +0200 | [diff] [blame] | 55 | tick_timer_(config.tick_timer), |
| 56 | disallow_time_stretching_(!config.allow_time_stretching), |
Henrik Lundin | 47b17dc | 2016-05-10 10:20:59 +0200 | [diff] [blame] | 57 | timescale_countdown_( |
| 58 | tick_timer_->GetNewCountdown(kMinTimescaleInterval + 1)), |
Jakob Ivarsson | 46dda83 | 2019-07-03 16:00:30 +0200 | [diff] [blame] | 59 | target_level_window_ms_("target_level_window", |
| 60 | kDefaultTargetLevelWindowMs, |
| 61 | 0, |
| 62 | absl::nullopt) { |
Jakob Ivarsson | 46dda83 | 2019-07-03 16:00:30 +0200 | [diff] [blame] | 63 | const std::string field_trial_name = |
| 64 | field_trial::FindFullName("WebRTC-Audio-NetEqDecisionLogicSettings"); |
Jakob Ivarsson | e6aabd0 | 2022-03-28 16:06:37 +0200 | [diff] [blame^] | 65 | ParseFieldTrial({&target_level_window_ms_}, field_trial_name); |
Jakob Ivarsson | 46dda83 | 2019-07-03 16:00:30 +0200 | [diff] [blame] | 66 | RTC_LOG(LS_INFO) << "NetEq decision logic settings:" |
Jakob Ivarsson | 46dda83 | 2019-07-03 16:00:30 +0200 | [diff] [blame] | 67 | << " target_level_window_ms=" << target_level_window_ms_; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 68 | } |
| 69 | |
Henrik Lundin | 47b17dc | 2016-05-10 10:20:59 +0200 | [diff] [blame] | 70 | DecisionLogic::~DecisionLogic() = default; |
| 71 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 72 | void DecisionLogic::Reset() { |
| 73 | cng_state_ = kCngOff; |
henrik.lundin | b1fb72b | 2016-05-03 08:18:47 -0700 | [diff] [blame] | 74 | noise_fast_forward_ = 0; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 75 | packet_length_samples_ = 0; |
| 76 | sample_memory_ = 0; |
| 77 | prev_time_scale_ = false; |
Jakob Ivarsson | 80fb978 | 2020-10-09 13:41:06 +0200 | [diff] [blame] | 78 | last_pack_cng_or_dtmf_ = true; |
Henrik Lundin | 47b17dc | 2016-05-10 10:20:59 +0200 | [diff] [blame] | 79 | timescale_countdown_.reset(); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 80 | num_consecutive_expands_ = 0; |
Jakob Ivarsson | 46dda83 | 2019-07-03 16:00:30 +0200 | [diff] [blame] | 81 | time_stretched_cn_samples_ = 0; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | void DecisionLogic::SoftReset() { |
| 85 | packet_length_samples_ = 0; |
| 86 | sample_memory_ = 0; |
| 87 | prev_time_scale_ = false; |
Jakob Ivarsson | 80fb978 | 2020-10-09 13:41:06 +0200 | [diff] [blame] | 88 | last_pack_cng_or_dtmf_ = true; |
Henrik Lundin | 47b17dc | 2016-05-10 10:20:59 +0200 | [diff] [blame] | 89 | timescale_countdown_ = |
| 90 | tick_timer_->GetNewCountdown(kMinTimescaleInterval + 1); |
Jakob Ivarsson | 46dda83 | 2019-07-03 16:00:30 +0200 | [diff] [blame] | 91 | time_stretched_cn_samples_ = 0; |
Ivo Creusen | 53a31f7 | 2019-10-24 15:20:39 +0200 | [diff] [blame] | 92 | delay_manager_->Reset(); |
Jakob Ivarsson | 609b047 | 2020-10-19 09:19:34 +0200 | [diff] [blame] | 93 | buffer_level_filter_->Reset(); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 94 | } |
| 95 | |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 96 | void DecisionLogic::SetSampleRate(int fs_hz, size_t output_size_samples) { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 97 | // TODO(hlundin): Change to an enumerator and skip assert. |
Mirko Bonadei | 25ab322 | 2021-07-08 20:08:20 +0200 | [diff] [blame] | 98 | RTC_DCHECK(fs_hz == 8000 || fs_hz == 16000 || fs_hz == 32000 || |
| 99 | fs_hz == 48000); |
Jakob Ivarsson | 46dda83 | 2019-07-03 16:00:30 +0200 | [diff] [blame] | 100 | sample_rate_ = fs_hz; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 101 | output_size_samples_ = output_size_samples; |
| 102 | } |
| 103 | |
Ivo Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame] | 104 | NetEq::Operation DecisionLogic::GetDecision(const NetEqStatus& status, |
| 105 | bool* reset_decoder) { |
ossu | 61a208b | 2016-09-20 01:38:00 -0700 | [diff] [blame] | 106 | // 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 Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame] | 109 | if (status.last_mode == NetEq::Mode::kRfc3389Cng) { |
ossu | 61a208b | 2016-09-20 01:38:00 -0700 | [diff] [blame] | 110 | cng_state_ = kCngRfc3389On; |
Ivo Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame] | 111 | } else if (status.last_mode == NetEq::Mode::kCodecInternalCng) { |
ossu | 61a208b | 2016-09-20 01:38:00 -0700 | [diff] [blame] | 112 | cng_state_ = kCngInternalOn; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 113 | } |
| 114 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 115 | prev_time_scale_ = |
Ivo Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame] | 116 | 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.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 121 | |
Minyue Li | 7d204d5 | 2019-04-16 11:44:49 +0200 | [diff] [blame] | 122 | // Do not update buffer history if currently playing CNG since it will bias |
| 123 | // the filtered buffer level. |
Ivo Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame] | 124 | if (status.last_mode != NetEq::Mode::kRfc3389Cng && |
Jakob Ivarsson | e6aabd0 | 2022-03-28 16:06:37 +0200 | [diff] [blame^] | 125 | status.last_mode != NetEq::Mode::kCodecInternalCng) { |
| 126 | FilterBufferLevel(status.packet_buffer_info.span_samples); |
Minyue Li | 7d204d5 | 2019-04-16 11:44:49 +0200 | [diff] [blame] | 127 | } |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 128 | |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 129 | // Guard for errors, to avoid getting stuck in error mode. |
Ivo Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame] | 130 | if (status.last_mode == NetEq::Mode::kError) { |
Ivo Creusen | 53a31f7 | 2019-10-24 15:20:39 +0200 | [diff] [blame] | 131 | if (!status.next_packet) { |
Ivo Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame] | 132 | return NetEq::Operation::kExpand; |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 133 | } else { |
Ivo Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame] | 134 | // Use kUndefined to flag for a reset. |
| 135 | return NetEq::Operation::kUndefined; |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 136 | } |
| 137 | } |
| 138 | |
Ivo Creusen | 53a31f7 | 2019-10-24 15:20:39 +0200 | [diff] [blame] | 139 | 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 Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | // Handle the case with no packet at all available (except maybe DTMF). |
Ivo Creusen | 53a31f7 | 2019-10-24 15:20:39 +0200 | [diff] [blame] | 146 | if (!status.next_packet) { |
| 147 | return NoPacket(status.play_dtmf); |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 148 | } |
| 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 Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame] | 154 | return NetEq::Operation::kNormal; |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 155 | } |
| 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 Ivarsson | 80fb978 | 2020-10-09 13:41:06 +0200 | [diff] [blame] | 164 | const int target_level_samples = |
| 165 | delay_manager_->TargetDelayMs() * sample_rate_ / 1000; |
Ivo Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame] | 166 | if ((status.last_mode == NetEq::Mode::kExpand || |
| 167 | status.last_mode == NetEq::Mode::kCodecPlc) && |
Ivo Creusen | 53a31f7 | 2019-10-24 15:20:39 +0200 | [diff] [blame] | 168 | status.expand_mutefactor < 16384 / 2 && |
Jakob Ivarsson | e6aabd0 | 2022-03-28 16:06:37 +0200 | [diff] [blame^] | 169 | status.packet_buffer_info.span_samples < |
| 170 | static_cast<size_t>(target_level_samples * kPostponeDecodingLevel / |
| 171 | 100) && |
Ivo Creusen | 53a31f7 | 2019-10-24 15:20:39 +0200 | [diff] [blame] | 172 | !status.packet_buffer_info.dtx_or_cng) { |
Ivo Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame] | 173 | return NetEq::Operation::kExpand; |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 174 | } |
| 175 | |
Jakob Ivarsson | 46dda83 | 2019-07-03 16:00:30 +0200 | [diff] [blame] | 176 | const uint32_t five_seconds_samples = static_cast<uint32_t>(5 * sample_rate_); |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 177 | // Check if the required packet is available. |
Ivo Creusen | 53a31f7 | 2019-10-24 15:20:39 +0200 | [diff] [blame] | 178 | 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 Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 188 | } 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 Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame] | 191 | return NetEq::Operation::kUndefined; |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 192 | } |
| 193 | } |
| 194 | |
Ivo Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame] | 195 | void DecisionLogic::ExpandDecision(NetEq::Operation operation) { |
| 196 | if (operation == NetEq::Operation::kExpand) { |
Henrik Lundin | 5afa61c | 2018-07-02 14:53:24 +0200 | [diff] [blame] | 197 | num_consecutive_expands_++; |
| 198 | } else { |
| 199 | num_consecutive_expands_ = 0; |
| 200 | } |
| 201 | } |
| 202 | |
Ivo Creusen | a2b31c3 | 2020-10-14 17:54:22 +0200 | [diff] [blame] | 203 | absl::optional<int> DecisionLogic::PacketArrived( |
| 204 | int fs_hz, |
| 205 | bool should_update_stats, |
| 206 | const PacketArrivedInfo& info) { |
Ivo Creusen | 7b463c5 | 2020-11-25 11:32:40 +0100 | [diff] [blame] | 207 | buffer_flush_ = buffer_flush_ || info.buffer_flush; |
Ivo Creusen | a2b31c3 | 2020-10-14 17:54:22 +0200 | [diff] [blame] | 208 | if (info.is_cng_or_dtmf) { |
Jakob Ivarsson | 80fb978 | 2020-10-09 13:41:06 +0200 | [diff] [blame] | 209 | last_pack_cng_or_dtmf_ = true; |
| 210 | return absl::nullopt; |
Ivo Creusen | 53a31f7 | 2019-10-24 15:20:39 +0200 | [diff] [blame] | 211 | } |
Jakob Ivarsson | 80fb978 | 2020-10-09 13:41:06 +0200 | [diff] [blame] | 212 | if (!should_update_stats) { |
| 213 | return absl::nullopt; |
| 214 | } |
Ivo Creusen | a2b31c3 | 2020-10-14 17:54:22 +0200 | [diff] [blame] | 215 | 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 Ivarsson | 80fb978 | 2020-10-09 13:41:06 +0200 | [diff] [blame] | 218 | delay_manager_->SetPacketAudioLength(packet_length_samples_ * 1000 / fs_hz); |
| 219 | } |
| 220 | auto relative_delay = delay_manager_->Update( |
Ivo Creusen | a2b31c3 | 2020-10-14 17:54:22 +0200 | [diff] [blame] | 221 | info.main_timestamp, fs_hz, /*reset=*/last_pack_cng_or_dtmf_); |
Jakob Ivarsson | 80fb978 | 2020-10-09 13:41:06 +0200 | [diff] [blame] | 222 | last_pack_cng_or_dtmf_ = false; |
Ivo Creusen | 53a31f7 | 2019-10-24 15:20:39 +0200 | [diff] [blame] | 223 | return relative_delay; |
| 224 | } |
| 225 | |
Minyue Li | 7d204d5 | 2019-04-16 11:44:49 +0200 | [diff] [blame] | 226 | void DecisionLogic::FilterBufferLevel(size_t buffer_size_samples) { |
Jakob Ivarsson | 609b047 | 2020-10-19 09:19:34 +0200 | [diff] [blame] | 227 | buffer_level_filter_->SetTargetBufferLevel(delay_manager_->TargetDelayMs()); |
Henrik Lundin | 5afa61c | 2018-07-02 14:53:24 +0200 | [diff] [blame] | 228 | |
Jakob Ivarsson | 46dda83 | 2019-07-03 16:00:30 +0200 | [diff] [blame] | 229 | int time_stretched_samples = time_stretched_cn_samples_; |
Minyue Li | 7d204d5 | 2019-04-16 11:44:49 +0200 | [diff] [blame] | 230 | if (prev_time_scale_) { |
Jakob Ivarsson | 46dda83 | 2019-07-03 16:00:30 +0200 | [diff] [blame] | 231 | time_stretched_samples += sample_memory_; |
Minyue Li | 7d204d5 | 2019-04-16 11:44:49 +0200 | [diff] [blame] | 232 | timescale_countdown_ = tick_timer_->GetNewCountdown(kMinTimescaleInterval); |
| 233 | } |
| 234 | |
Ivo Creusen | 7b463c5 | 2020-11-25 11:32:40 +0100 | [diff] [blame] | 235 | 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 Li | 7d204d5 | 2019-04-16 11:44:49 +0200 | [diff] [blame] | 241 | prev_time_scale_ = false; |
Jakob Ivarsson | 46dda83 | 2019-07-03 16:00:30 +0200 | [diff] [blame] | 242 | time_stretched_cn_samples_ = 0; |
Henrik Lundin | 5afa61c | 2018-07-02 14:53:24 +0200 | [diff] [blame] | 243 | } |
| 244 | |
Ivo Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame] | 245 | NetEq::Operation DecisionLogic::CngOperation(NetEq::Mode prev_mode, |
| 246 | uint32_t target_timestamp, |
| 247 | uint32_t available_timestamp, |
| 248 | size_t generated_noise_samples) { |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 249 | // 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 Ivarsson | 80fb978 | 2020-10-09 13:41:06 +0200 | [diff] [blame] | 253 | int optimal_level_samp = |
| 254 | delay_manager_->TargetDelayMs() * sample_rate_ / 1000; |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 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 Ivarsson | 42b6e2d | 2019-10-21 11:51:05 +0200 | [diff] [blame] | 262 | noise_fast_forward_ = rtc::saturated_cast<size_t>(noise_fast_forward_ + |
| 263 | excess_waiting_time_samp); |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 264 | timestamp_diff = |
| 265 | rtc::saturated_cast<int32_t>(timestamp_diff + excess_waiting_time_samp); |
| 266 | } |
| 267 | |
Ivo Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame] | 268 | if (timestamp_diff < 0 && prev_mode == NetEq::Mode::kRfc3389Cng) { |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 269 | // Not time to play this packet yet. Wait another round before using this |
| 270 | // packet. Keep on playing CNG from previous CNG parameters. |
Ivo Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame] | 271 | return NetEq::Operation::kRfc3389CngNoPacket; |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 272 | } else { |
| 273 | // Otherwise, go for the CNG packet now. |
| 274 | noise_fast_forward_ = 0; |
Ivo Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame] | 275 | return NetEq::Operation::kRfc3389Cng; |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 276 | } |
| 277 | } |
| 278 | |
Ivo Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame] | 279 | NetEq::Operation DecisionLogic::NoPacket(bool play_dtmf) { |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 280 | if (cng_state_ == kCngRfc3389On) { |
| 281 | // Keep on playing comfort noise. |
Ivo Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame] | 282 | return NetEq::Operation::kRfc3389CngNoPacket; |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 283 | } else if (cng_state_ == kCngInternalOn) { |
| 284 | // Keep on playing codec internal comfort noise. |
Ivo Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame] | 285 | return NetEq::Operation::kCodecInternalCng; |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 286 | } else if (play_dtmf) { |
Ivo Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame] | 287 | return NetEq::Operation::kDtmf; |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 288 | } else { |
| 289 | // Nothing to play, do expand. |
Ivo Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame] | 290 | return NetEq::Operation::kExpand; |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 291 | } |
| 292 | } |
| 293 | |
Ivo Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame] | 294 | NetEq::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 Ivarsson | 80fb978 | 2020-10-09 13:41:06 +0200 | [diff] [blame] | 298 | 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 Titov | d00ce74 | 2021-07-28 20:00:17 +0200 | [diff] [blame] | 305 | // `higher_limit` is equal to `target_level`, but should at |
| 306 | // least be 20 ms higher than `lower_limit`. |
Jakob Ivarsson | 80fb978 | 2020-10-09 13:41:06 +0200 | [diff] [blame] | 307 | const int high_limit = |
| 308 | std::max(target_level_samples, low_limit + 20 * samples_per_ms); |
| 309 | |
| 310 | const int buffer_level_samples = |
Jakob Ivarsson | 609b047 | 2020-10-19 09:19:34 +0200 | [diff] [blame] | 311 | buffer_level_filter_->filtered_current_level(); |
Jakob Ivarsson | 80fb978 | 2020-10-09 13:41:06 +0200 | [diff] [blame] | 312 | if (buffer_level_samples >= high_limit << 2) |
Ivo Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame] | 313 | return NetEq::Operation::kFastAccelerate; |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 314 | if (TimescaleAllowed()) { |
Jakob Ivarsson | 80fb978 | 2020-10-09 13:41:06 +0200 | [diff] [blame] | 315 | if (buffer_level_samples >= high_limit) |
Ivo Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame] | 316 | return NetEq::Operation::kAccelerate; |
Jakob Ivarsson | 80fb978 | 2020-10-09 13:41:06 +0200 | [diff] [blame] | 317 | if (buffer_level_samples < low_limit) |
Ivo Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame] | 318 | return NetEq::Operation::kPreemptiveExpand; |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 319 | } |
| 320 | } |
Ivo Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame] | 321 | return NetEq::Operation::kNormal; |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 322 | } |
| 323 | |
Ivo Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame] | 324 | NetEq::Operation DecisionLogic::FuturePacketAvailable( |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 325 | size_t decoder_frame_length, |
Ivo Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame] | 326 | NetEq::Mode prev_mode, |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 327 | uint32_t target_timestamp, |
| 328 | uint32_t available_timestamp, |
| 329 | bool play_dtmf, |
Ivo Creusen | 53a31f7 | 2019-10-24 15:20:39 +0200 | [diff] [blame] | 330 | size_t generated_noise_samples, |
| 331 | size_t span_samples_in_packet_buffer, |
| 332 | size_t num_packets_in_packet_buffer) { |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 333 | // 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 Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame] | 337 | if ((prev_mode == NetEq::Mode::kExpand || |
| 338 | prev_mode == NetEq::Mode::kCodecPlc) && |
Henrik Lundin | 00eb12a | 2018-09-05 18:14:52 +0200 | [diff] [blame] | 339 | !ReinitAfterExpands(timestamp_leap) && !MaxWaitForPacket() && |
| 340 | PacketTooEarly(timestamp_leap) && UnderTargetLevel()) { |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 341 | if (play_dtmf) { |
| 342 | // Still have DTMF to play, so do not do expand. |
Ivo Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame] | 343 | return NetEq::Operation::kDtmf; |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 344 | } else { |
| 345 | // Nothing to play. |
Ivo Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame] | 346 | return NetEq::Operation::kExpand; |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 347 | } |
| 348 | } |
| 349 | |
Ivo Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame] | 350 | if (prev_mode == NetEq::Mode::kCodecPlc) { |
| 351 | return NetEq::Operation::kNormal; |
Henrik Lundin | 00eb12a | 2018-09-05 18:14:52 +0200 | [diff] [blame] | 352 | } |
| 353 | |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 354 | // If previous was comfort noise, then no merge is needed. |
Ivo Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame] | 355 | if (prev_mode == NetEq::Mode::kRfc3389Cng || |
| 356 | prev_mode == NetEq::Mode::kCodecInternalCng) { |
Jakob Ivarsson | 46dda83 | 2019-07-03 16:00:30 +0200 | [diff] [blame] | 357 | const size_t target_level_samples = |
Jakob Ivarsson | 80fb978 | 2020-10-09 13:41:06 +0200 | [diff] [blame] | 358 | delay_manager_->TargetDelayMs() * sample_rate_ / 1000; |
Jakob Ivarsson | 46dda83 | 2019-07-03 16:00:30 +0200 | [diff] [blame] | 359 | const bool generated_enough_noise = |
| 360 | static_cast<uint32_t>(generated_noise_samples + target_timestamp) >= |
| 361 | available_timestamp; |
Jakob Ivarsson | e6aabd0 | 2022-03-28 16:06:37 +0200 | [diff] [blame^] | 362 | 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 Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 377 | } |
Jakob Ivarsson | 46dda83 | 2019-07-03 16:00:30 +0200 | [diff] [blame] | 378 | |
| 379 | // Too early to play this new packet; keep on playing comfort noise. |
Ivo Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame] | 380 | if (prev_mode == NetEq::Mode::kRfc3389Cng) { |
| 381 | return NetEq::Operation::kRfc3389CngNoPacket; |
Jakob Ivarsson | 46dda83 | 2019-07-03 16:00:30 +0200 | [diff] [blame] | 382 | } |
| 383 | // prevPlayMode == kModeCodecInternalCng. |
Ivo Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame] | 384 | return NetEq::Operation::kCodecInternalCng; |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 385 | } |
Jakob Ivarsson | 46dda83 | 2019-07-03 16:00:30 +0200 | [diff] [blame] | 386 | |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 387 | // Do not merge unless we have done an expand before. |
Ivo Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame] | 388 | if (prev_mode == NetEq::Mode::kExpand) { |
| 389 | return NetEq::Operation::kMerge; |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 390 | } else if (play_dtmf) { |
| 391 | // Play DTMF instead of expand. |
Ivo Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame] | 392 | return NetEq::Operation::kDtmf; |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 393 | } else { |
Ivo Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame] | 394 | return NetEq::Operation::kExpand; |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 395 | } |
| 396 | } |
| 397 | |
| 398 | bool DecisionLogic::UnderTargetLevel() const { |
Jakob Ivarsson | 609b047 | 2020-10-19 09:19:34 +0200 | [diff] [blame] | 399 | return buffer_level_filter_->filtered_current_level() < |
Jakob Ivarsson | 80fb978 | 2020-10-09 13:41:06 +0200 | [diff] [blame] | 400 | delay_manager_->TargetDelayMs() * sample_rate_ / 1000; |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | bool DecisionLogic::ReinitAfterExpands(uint32_t timestamp_leap) const { |
| 404 | return timestamp_leap >= |
| 405 | static_cast<uint32_t>(output_size_samples_ * kReinitAfterExpands); |
| 406 | } |
| 407 | |
| 408 | bool DecisionLogic::PacketTooEarly(uint32_t timestamp_leap) const { |
| 409 | return timestamp_leap > |
| 410 | static_cast<uint32_t>(output_size_samples_ * num_consecutive_expands_); |
| 411 | } |
| 412 | |
| 413 | bool DecisionLogic::MaxWaitForPacket() const { |
| 414 | return num_consecutive_expands_ >= kMaxWaitForPacket; |
| 415 | } |
| 416 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 417 | } // namespace webrtc |