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 | d723da1 | 2021-01-15 17:44:56 +0100 | [diff] [blame] | 59 | estimate_dtx_delay_("estimate_dtx_delay", true), |
| 60 | time_stretch_cn_("time_stretch_cn", true), |
Jakob Ivarsson | 46dda83 | 2019-07-03 16:00:30 +0200 | [diff] [blame] | 61 | target_level_window_ms_("target_level_window", |
| 62 | kDefaultTargetLevelWindowMs, |
| 63 | 0, |
| 64 | absl::nullopt) { |
Jakob Ivarsson | 46dda83 | 2019-07-03 16:00:30 +0200 | [diff] [blame] | 65 | const std::string field_trial_name = |
| 66 | field_trial::FindFullName("WebRTC-Audio-NetEqDecisionLogicSettings"); |
| 67 | ParseFieldTrial( |
| 68 | {&estimate_dtx_delay_, &time_stretch_cn_, &target_level_window_ms_}, |
| 69 | field_trial_name); |
| 70 | RTC_LOG(LS_INFO) << "NetEq decision logic settings:" |
Jonas Olsson | b2b2031 | 2020-01-14 12:11:31 +0100 | [diff] [blame] | 71 | " estimate_dtx_delay=" |
| 72 | << estimate_dtx_delay_ |
Jakob Ivarsson | 46dda83 | 2019-07-03 16:00:30 +0200 | [diff] [blame] | 73 | << " time_stretch_cn=" << time_stretch_cn_ |
| 74 | << " target_level_window_ms=" << target_level_window_ms_; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 75 | } |
| 76 | |
Henrik Lundin | 47b17dc | 2016-05-10 10:20:59 +0200 | [diff] [blame] | 77 | DecisionLogic::~DecisionLogic() = default; |
| 78 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 79 | void DecisionLogic::Reset() { |
| 80 | cng_state_ = kCngOff; |
henrik.lundin | b1fb72b | 2016-05-03 08:18:47 -0700 | [diff] [blame] | 81 | noise_fast_forward_ = 0; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 82 | packet_length_samples_ = 0; |
| 83 | sample_memory_ = 0; |
| 84 | prev_time_scale_ = false; |
Jakob Ivarsson | 80fb978 | 2020-10-09 13:41:06 +0200 | [diff] [blame] | 85 | last_pack_cng_or_dtmf_ = true; |
Henrik Lundin | 47b17dc | 2016-05-10 10:20:59 +0200 | [diff] [blame] | 86 | timescale_countdown_.reset(); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 87 | num_consecutive_expands_ = 0; |
Jakob Ivarsson | 46dda83 | 2019-07-03 16:00:30 +0200 | [diff] [blame] | 88 | time_stretched_cn_samples_ = 0; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | void DecisionLogic::SoftReset() { |
| 92 | packet_length_samples_ = 0; |
| 93 | sample_memory_ = 0; |
| 94 | prev_time_scale_ = false; |
Jakob Ivarsson | 80fb978 | 2020-10-09 13:41:06 +0200 | [diff] [blame] | 95 | last_pack_cng_or_dtmf_ = true; |
Henrik Lundin | 47b17dc | 2016-05-10 10:20:59 +0200 | [diff] [blame] | 96 | timescale_countdown_ = |
| 97 | tick_timer_->GetNewCountdown(kMinTimescaleInterval + 1); |
Jakob Ivarsson | 46dda83 | 2019-07-03 16:00:30 +0200 | [diff] [blame] | 98 | time_stretched_cn_samples_ = 0; |
Ivo Creusen | 53a31f7 | 2019-10-24 15:20:39 +0200 | [diff] [blame] | 99 | delay_manager_->Reset(); |
Jakob Ivarsson | 609b047 | 2020-10-19 09:19:34 +0200 | [diff] [blame] | 100 | buffer_level_filter_->Reset(); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 101 | } |
| 102 | |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 103 | 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] | 104 | // TODO(hlundin): Change to an enumerator and skip assert. |
Mirko Bonadei | 25ab322 | 2021-07-08 20:08:20 +0200 | [diff] [blame] | 105 | RTC_DCHECK(fs_hz == 8000 || fs_hz == 16000 || fs_hz == 32000 || |
| 106 | fs_hz == 48000); |
Jakob Ivarsson | 46dda83 | 2019-07-03 16:00:30 +0200 | [diff] [blame] | 107 | sample_rate_ = fs_hz; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 108 | output_size_samples_ = output_size_samples; |
| 109 | } |
| 110 | |
Ivo Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame] | 111 | NetEq::Operation DecisionLogic::GetDecision(const NetEqStatus& status, |
| 112 | bool* reset_decoder) { |
ossu | 61a208b | 2016-09-20 01:38:00 -0700 | [diff] [blame] | 113 | // If last mode was CNG (or Expand, since this could be covering up for |
| 114 | // a lost CNG packet), remember that CNG is on. This is needed if comfort |
| 115 | // noise is interrupted by DTMF. |
Ivo Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame] | 116 | if (status.last_mode == NetEq::Mode::kRfc3389Cng) { |
ossu | 61a208b | 2016-09-20 01:38:00 -0700 | [diff] [blame] | 117 | cng_state_ = kCngRfc3389On; |
Ivo Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame] | 118 | } else if (status.last_mode == NetEq::Mode::kCodecInternalCng) { |
ossu | 61a208b | 2016-09-20 01:38:00 -0700 | [diff] [blame] | 119 | cng_state_ = kCngInternalOn; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 120 | } |
| 121 | |
Ivo Creusen | 53a31f7 | 2019-10-24 15:20:39 +0200 | [diff] [blame] | 122 | size_t cur_size_samples = estimate_dtx_delay_ |
| 123 | ? status.packet_buffer_info.span_samples |
| 124 | : status.packet_buffer_info.num_samples; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 125 | prev_time_scale_ = |
Ivo Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame] | 126 | prev_time_scale_ && |
| 127 | (status.last_mode == NetEq::Mode::kAccelerateSuccess || |
| 128 | status.last_mode == NetEq::Mode::kAccelerateLowEnergy || |
| 129 | status.last_mode == NetEq::Mode::kPreemptiveExpandSuccess || |
| 130 | status.last_mode == NetEq::Mode::kPreemptiveExpandLowEnergy); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 131 | |
Minyue Li | 7d204d5 | 2019-04-16 11:44:49 +0200 | [diff] [blame] | 132 | // Do not update buffer history if currently playing CNG since it will bias |
| 133 | // the filtered buffer level. |
Ivo Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame] | 134 | if (status.last_mode != NetEq::Mode::kRfc3389Cng && |
| 135 | status.last_mode != NetEq::Mode::kCodecInternalCng && |
Ivo Creusen | 53a31f7 | 2019-10-24 15:20:39 +0200 | [diff] [blame] | 136 | !(status.next_packet && status.next_packet->is_dtx && |
| 137 | !estimate_dtx_delay_)) { |
Minyue Li | 7d204d5 | 2019-04-16 11:44:49 +0200 | [diff] [blame] | 138 | FilterBufferLevel(cur_size_samples); |
| 139 | } |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 140 | |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 141 | // Guard for errors, to avoid getting stuck in error mode. |
Ivo Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame] | 142 | if (status.last_mode == NetEq::Mode::kError) { |
Ivo Creusen | 53a31f7 | 2019-10-24 15:20:39 +0200 | [diff] [blame] | 143 | if (!status.next_packet) { |
Ivo Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame] | 144 | return NetEq::Operation::kExpand; |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 145 | } else { |
Ivo Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame] | 146 | // Use kUndefined to flag for a reset. |
| 147 | return NetEq::Operation::kUndefined; |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 148 | } |
| 149 | } |
| 150 | |
Ivo Creusen | 53a31f7 | 2019-10-24 15:20:39 +0200 | [diff] [blame] | 151 | if (status.next_packet && status.next_packet->is_cng) { |
| 152 | return CngOperation(status.last_mode, status.target_timestamp, |
| 153 | status.next_packet->timestamp, |
| 154 | status.generated_noise_samples); |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | // Handle the case with no packet at all available (except maybe DTMF). |
Ivo Creusen | 53a31f7 | 2019-10-24 15:20:39 +0200 | [diff] [blame] | 158 | if (!status.next_packet) { |
| 159 | return NoPacket(status.play_dtmf); |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | // If the expand period was very long, reset NetEQ since it is likely that the |
| 163 | // sender was restarted. |
| 164 | if (num_consecutive_expands_ > kReinitAfterExpands) { |
| 165 | *reset_decoder = true; |
Ivo Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame] | 166 | return NetEq::Operation::kNormal; |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | // Make sure we don't restart audio too soon after an expansion to avoid |
| 170 | // running out of data right away again. We should only wait if there are no |
| 171 | // DTX or CNG packets in the buffer (otherwise we should just play out what we |
| 172 | // have, since we cannot know the exact duration of DTX or CNG packets), and |
| 173 | // if the mute factor is low enough (otherwise the expansion was short enough |
| 174 | // to not be noticable). |
| 175 | // Note that the MuteFactor is in Q14, so a value of 16384 corresponds to 1. |
Ivo Creusen | 53a31f7 | 2019-10-24 15:20:39 +0200 | [diff] [blame] | 176 | const size_t current_span = |
| 177 | estimate_dtx_delay_ ? status.packet_buffer_info.span_samples |
| 178 | : status.packet_buffer_info.span_samples_no_dtx; |
Jakob Ivarsson | 80fb978 | 2020-10-09 13:41:06 +0200 | [diff] [blame] | 179 | const int target_level_samples = |
| 180 | delay_manager_->TargetDelayMs() * sample_rate_ / 1000; |
Ivo Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame] | 181 | if ((status.last_mode == NetEq::Mode::kExpand || |
| 182 | status.last_mode == NetEq::Mode::kCodecPlc) && |
Ivo Creusen | 53a31f7 | 2019-10-24 15:20:39 +0200 | [diff] [blame] | 183 | status.expand_mutefactor < 16384 / 2 && |
Jakob Ivarsson | 80fb978 | 2020-10-09 13:41:06 +0200 | [diff] [blame] | 184 | current_span < static_cast<size_t>(target_level_samples * |
| 185 | kPostponeDecodingLevel / 100) && |
Ivo Creusen | 53a31f7 | 2019-10-24 15:20:39 +0200 | [diff] [blame] | 186 | !status.packet_buffer_info.dtx_or_cng) { |
Ivo Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame] | 187 | return NetEq::Operation::kExpand; |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 188 | } |
| 189 | |
Jakob Ivarsson | 46dda83 | 2019-07-03 16:00:30 +0200 | [diff] [blame] | 190 | 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] | 191 | // Check if the required packet is available. |
Ivo Creusen | 53a31f7 | 2019-10-24 15:20:39 +0200 | [diff] [blame] | 192 | if (status.target_timestamp == status.next_packet->timestamp) { |
| 193 | return ExpectedPacketAvailable(status.last_mode, status.play_dtmf); |
| 194 | } else if (!PacketBuffer::IsObsoleteTimestamp(status.next_packet->timestamp, |
| 195 | status.target_timestamp, |
| 196 | five_seconds_samples)) { |
| 197 | return FuturePacketAvailable( |
| 198 | status.last_packet_samples, status.last_mode, status.target_timestamp, |
| 199 | status.next_packet->timestamp, status.play_dtmf, |
| 200 | status.generated_noise_samples, status.packet_buffer_info.span_samples, |
| 201 | status.packet_buffer_info.num_packets); |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 202 | } else { |
| 203 | // This implies that available_timestamp < target_timestamp, which can |
| 204 | // 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] | 205 | return NetEq::Operation::kUndefined; |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 206 | } |
| 207 | } |
| 208 | |
Ivo Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame] | 209 | void DecisionLogic::ExpandDecision(NetEq::Operation operation) { |
| 210 | if (operation == NetEq::Operation::kExpand) { |
Henrik Lundin | 5afa61c | 2018-07-02 14:53:24 +0200 | [diff] [blame] | 211 | num_consecutive_expands_++; |
| 212 | } else { |
| 213 | num_consecutive_expands_ = 0; |
| 214 | } |
| 215 | } |
| 216 | |
Ivo Creusen | a2b31c3 | 2020-10-14 17:54:22 +0200 | [diff] [blame] | 217 | absl::optional<int> DecisionLogic::PacketArrived( |
| 218 | int fs_hz, |
| 219 | bool should_update_stats, |
| 220 | const PacketArrivedInfo& info) { |
Ivo Creusen | 7b463c5 | 2020-11-25 11:32:40 +0100 | [diff] [blame] | 221 | buffer_flush_ = buffer_flush_ || info.buffer_flush; |
Ivo Creusen | a2b31c3 | 2020-10-14 17:54:22 +0200 | [diff] [blame] | 222 | if (info.is_cng_or_dtmf) { |
Jakob Ivarsson | 80fb978 | 2020-10-09 13:41:06 +0200 | [diff] [blame] | 223 | last_pack_cng_or_dtmf_ = true; |
| 224 | return absl::nullopt; |
Ivo Creusen | 53a31f7 | 2019-10-24 15:20:39 +0200 | [diff] [blame] | 225 | } |
Jakob Ivarsson | 80fb978 | 2020-10-09 13:41:06 +0200 | [diff] [blame] | 226 | if (!should_update_stats) { |
| 227 | return absl::nullopt; |
| 228 | } |
Ivo Creusen | a2b31c3 | 2020-10-14 17:54:22 +0200 | [diff] [blame] | 229 | if (info.packet_length_samples > 0 && fs_hz > 0 && |
| 230 | info.packet_length_samples != packet_length_samples_) { |
| 231 | packet_length_samples_ = info.packet_length_samples; |
Jakob Ivarsson | 80fb978 | 2020-10-09 13:41:06 +0200 | [diff] [blame] | 232 | delay_manager_->SetPacketAudioLength(packet_length_samples_ * 1000 / fs_hz); |
| 233 | } |
| 234 | auto relative_delay = delay_manager_->Update( |
Ivo Creusen | a2b31c3 | 2020-10-14 17:54:22 +0200 | [diff] [blame] | 235 | info.main_timestamp, fs_hz, /*reset=*/last_pack_cng_or_dtmf_); |
Jakob Ivarsson | 80fb978 | 2020-10-09 13:41:06 +0200 | [diff] [blame] | 236 | last_pack_cng_or_dtmf_ = false; |
Ivo Creusen | 53a31f7 | 2019-10-24 15:20:39 +0200 | [diff] [blame] | 237 | return relative_delay; |
| 238 | } |
| 239 | |
Minyue Li | 7d204d5 | 2019-04-16 11:44:49 +0200 | [diff] [blame] | 240 | void DecisionLogic::FilterBufferLevel(size_t buffer_size_samples) { |
Jakob Ivarsson | 609b047 | 2020-10-19 09:19:34 +0200 | [diff] [blame] | 241 | buffer_level_filter_->SetTargetBufferLevel(delay_manager_->TargetDelayMs()); |
Henrik Lundin | 5afa61c | 2018-07-02 14:53:24 +0200 | [diff] [blame] | 242 | |
Jakob Ivarsson | 46dda83 | 2019-07-03 16:00:30 +0200 | [diff] [blame] | 243 | int time_stretched_samples = time_stretched_cn_samples_; |
Minyue Li | 7d204d5 | 2019-04-16 11:44:49 +0200 | [diff] [blame] | 244 | if (prev_time_scale_) { |
Jakob Ivarsson | 46dda83 | 2019-07-03 16:00:30 +0200 | [diff] [blame] | 245 | time_stretched_samples += sample_memory_; |
Minyue Li | 7d204d5 | 2019-04-16 11:44:49 +0200 | [diff] [blame] | 246 | timescale_countdown_ = tick_timer_->GetNewCountdown(kMinTimescaleInterval); |
| 247 | } |
| 248 | |
Ivo Creusen | 7b463c5 | 2020-11-25 11:32:40 +0100 | [diff] [blame] | 249 | if (buffer_flush_) { |
| 250 | buffer_level_filter_->SetFilteredBufferLevel(buffer_size_samples); |
| 251 | buffer_flush_ = false; |
| 252 | } else { |
| 253 | buffer_level_filter_->Update(buffer_size_samples, time_stretched_samples); |
| 254 | } |
Minyue Li | 7d204d5 | 2019-04-16 11:44:49 +0200 | [diff] [blame] | 255 | prev_time_scale_ = false; |
Jakob Ivarsson | 46dda83 | 2019-07-03 16:00:30 +0200 | [diff] [blame] | 256 | time_stretched_cn_samples_ = 0; |
Henrik Lundin | 5afa61c | 2018-07-02 14:53:24 +0200 | [diff] [blame] | 257 | } |
| 258 | |
Ivo Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame] | 259 | NetEq::Operation DecisionLogic::CngOperation(NetEq::Mode prev_mode, |
| 260 | uint32_t target_timestamp, |
| 261 | uint32_t available_timestamp, |
| 262 | size_t generated_noise_samples) { |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 263 | // Signed difference between target and available timestamp. |
| 264 | int32_t timestamp_diff = static_cast<int32_t>( |
| 265 | static_cast<uint32_t>(generated_noise_samples + target_timestamp) - |
| 266 | available_timestamp); |
Jakob Ivarsson | 80fb978 | 2020-10-09 13:41:06 +0200 | [diff] [blame] | 267 | int optimal_level_samp = |
| 268 | delay_manager_->TargetDelayMs() * sample_rate_ / 1000; |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 269 | const int64_t excess_waiting_time_samp = |
| 270 | -static_cast<int64_t>(timestamp_diff) - optimal_level_samp; |
| 271 | |
| 272 | if (excess_waiting_time_samp > optimal_level_samp / 2) { |
| 273 | // The waiting time for this packet will be longer than 1.5 |
| 274 | // times the wanted buffer delay. Apply fast-forward to cut the |
| 275 | // waiting time down to the optimal. |
Jakob Ivarsson | 42b6e2d | 2019-10-21 11:51:05 +0200 | [diff] [blame] | 276 | noise_fast_forward_ = rtc::saturated_cast<size_t>(noise_fast_forward_ + |
| 277 | excess_waiting_time_samp); |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 278 | timestamp_diff = |
| 279 | rtc::saturated_cast<int32_t>(timestamp_diff + excess_waiting_time_samp); |
| 280 | } |
| 281 | |
Ivo Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame] | 282 | if (timestamp_diff < 0 && prev_mode == NetEq::Mode::kRfc3389Cng) { |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 283 | // Not time to play this packet yet. Wait another round before using this |
| 284 | // packet. Keep on playing CNG from previous CNG parameters. |
Ivo Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame] | 285 | return NetEq::Operation::kRfc3389CngNoPacket; |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 286 | } else { |
| 287 | // Otherwise, go for the CNG packet now. |
| 288 | noise_fast_forward_ = 0; |
Ivo Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame] | 289 | return NetEq::Operation::kRfc3389Cng; |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 290 | } |
| 291 | } |
| 292 | |
Ivo Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame] | 293 | NetEq::Operation DecisionLogic::NoPacket(bool play_dtmf) { |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 294 | if (cng_state_ == kCngRfc3389On) { |
| 295 | // Keep on playing comfort noise. |
Ivo Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame] | 296 | return NetEq::Operation::kRfc3389CngNoPacket; |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 297 | } else if (cng_state_ == kCngInternalOn) { |
| 298 | // Keep on playing codec internal comfort noise. |
Ivo Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame] | 299 | return NetEq::Operation::kCodecInternalCng; |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 300 | } else if (play_dtmf) { |
Ivo Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame] | 301 | return NetEq::Operation::kDtmf; |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 302 | } else { |
| 303 | // Nothing to play, do expand. |
Ivo Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame] | 304 | return NetEq::Operation::kExpand; |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 305 | } |
| 306 | } |
| 307 | |
Ivo Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame] | 308 | NetEq::Operation DecisionLogic::ExpectedPacketAvailable(NetEq::Mode prev_mode, |
| 309 | bool play_dtmf) { |
| 310 | if (!disallow_time_stretching_ && prev_mode != NetEq::Mode::kExpand && |
| 311 | !play_dtmf) { |
Jakob Ivarsson | 80fb978 | 2020-10-09 13:41:06 +0200 | [diff] [blame] | 312 | const int samples_per_ms = sample_rate_ / 1000; |
| 313 | const int target_level_samples = |
| 314 | delay_manager_->TargetDelayMs() * samples_per_ms; |
| 315 | const int low_limit = |
| 316 | std::max(target_level_samples * 3 / 4, |
| 317 | target_level_samples - |
| 318 | kDecelerationTargetLevelOffsetMs * samples_per_ms); |
Artem Titov | d00ce74 | 2021-07-28 20:00:17 +0200 | [diff] [blame] | 319 | // `higher_limit` is equal to `target_level`, but should at |
| 320 | // least be 20 ms higher than `lower_limit`. |
Jakob Ivarsson | 80fb978 | 2020-10-09 13:41:06 +0200 | [diff] [blame] | 321 | const int high_limit = |
| 322 | std::max(target_level_samples, low_limit + 20 * samples_per_ms); |
| 323 | |
| 324 | const int buffer_level_samples = |
Jakob Ivarsson | 609b047 | 2020-10-19 09:19:34 +0200 | [diff] [blame] | 325 | buffer_level_filter_->filtered_current_level(); |
Jakob Ivarsson | 80fb978 | 2020-10-09 13:41:06 +0200 | [diff] [blame] | 326 | if (buffer_level_samples >= high_limit << 2) |
Ivo Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame] | 327 | return NetEq::Operation::kFastAccelerate; |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 328 | if (TimescaleAllowed()) { |
Jakob Ivarsson | 80fb978 | 2020-10-09 13:41:06 +0200 | [diff] [blame] | 329 | if (buffer_level_samples >= high_limit) |
Ivo Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame] | 330 | return NetEq::Operation::kAccelerate; |
Jakob Ivarsson | 80fb978 | 2020-10-09 13:41:06 +0200 | [diff] [blame] | 331 | if (buffer_level_samples < low_limit) |
Ivo Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame] | 332 | return NetEq::Operation::kPreemptiveExpand; |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 333 | } |
| 334 | } |
Ivo Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame] | 335 | return NetEq::Operation::kNormal; |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 336 | } |
| 337 | |
Ivo Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame] | 338 | NetEq::Operation DecisionLogic::FuturePacketAvailable( |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 339 | size_t decoder_frame_length, |
Ivo Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame] | 340 | NetEq::Mode prev_mode, |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 341 | uint32_t target_timestamp, |
| 342 | uint32_t available_timestamp, |
| 343 | bool play_dtmf, |
Ivo Creusen | 53a31f7 | 2019-10-24 15:20:39 +0200 | [diff] [blame] | 344 | size_t generated_noise_samples, |
| 345 | size_t span_samples_in_packet_buffer, |
| 346 | size_t num_packets_in_packet_buffer) { |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 347 | // Required packet is not available, but a future packet is. |
| 348 | // Check if we should continue with an ongoing expand because the new packet |
| 349 | // is too far into the future. |
| 350 | uint32_t timestamp_leap = available_timestamp - target_timestamp; |
Ivo Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame] | 351 | if ((prev_mode == NetEq::Mode::kExpand || |
| 352 | prev_mode == NetEq::Mode::kCodecPlc) && |
Henrik Lundin | 00eb12a | 2018-09-05 18:14:52 +0200 | [diff] [blame] | 353 | !ReinitAfterExpands(timestamp_leap) && !MaxWaitForPacket() && |
| 354 | PacketTooEarly(timestamp_leap) && UnderTargetLevel()) { |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 355 | if (play_dtmf) { |
| 356 | // Still have DTMF to play, so do not do expand. |
Ivo Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame] | 357 | return NetEq::Operation::kDtmf; |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 358 | } else { |
| 359 | // Nothing to play. |
Ivo Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame] | 360 | return NetEq::Operation::kExpand; |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 361 | } |
| 362 | } |
| 363 | |
Ivo Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame] | 364 | if (prev_mode == NetEq::Mode::kCodecPlc) { |
| 365 | return NetEq::Operation::kNormal; |
Henrik Lundin | 00eb12a | 2018-09-05 18:14:52 +0200 | [diff] [blame] | 366 | } |
| 367 | |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 368 | // If previous was comfort noise, then no merge is needed. |
Ivo Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame] | 369 | if (prev_mode == NetEq::Mode::kRfc3389Cng || |
| 370 | prev_mode == NetEq::Mode::kCodecInternalCng) { |
Jakob Ivarsson | 46dda83 | 2019-07-03 16:00:30 +0200 | [diff] [blame] | 371 | size_t cur_size_samples = |
| 372 | estimate_dtx_delay_ |
Jakob Ivarsson | 80fb978 | 2020-10-09 13:41:06 +0200 | [diff] [blame] | 373 | ? span_samples_in_packet_buffer |
Ivo Creusen | 53a31f7 | 2019-10-24 15:20:39 +0200 | [diff] [blame] | 374 | : num_packets_in_packet_buffer * decoder_frame_length; |
Jakob Ivarsson | 46dda83 | 2019-07-03 16:00:30 +0200 | [diff] [blame] | 375 | // Target level is in number of packets in Q8. |
| 376 | const size_t target_level_samples = |
Jakob Ivarsson | 80fb978 | 2020-10-09 13:41:06 +0200 | [diff] [blame] | 377 | delay_manager_->TargetDelayMs() * sample_rate_ / 1000; |
Jakob Ivarsson | 46dda83 | 2019-07-03 16:00:30 +0200 | [diff] [blame] | 378 | const bool generated_enough_noise = |
| 379 | static_cast<uint32_t>(generated_noise_samples + target_timestamp) >= |
| 380 | available_timestamp; |
| 381 | |
| 382 | if (time_stretch_cn_) { |
| 383 | const size_t target_threshold_samples = |
| 384 | target_level_window_ms_ / 2 * (sample_rate_ / 1000); |
| 385 | const bool above_target_window = |
| 386 | cur_size_samples > target_level_samples + target_threshold_samples; |
| 387 | const bool below_target_window = |
| 388 | target_level_samples > target_threshold_samples && |
| 389 | cur_size_samples < target_level_samples - target_threshold_samples; |
| 390 | // Keep the delay same as before CNG, but make sure that it is within the |
| 391 | // target window. |
| 392 | if ((generated_enough_noise && !below_target_window) || |
| 393 | above_target_window) { |
| 394 | time_stretched_cn_samples_ = timestamp_leap - generated_noise_samples; |
Ivo Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame] | 395 | return NetEq::Operation::kNormal; |
Jakob Ivarsson | 46dda83 | 2019-07-03 16:00:30 +0200 | [diff] [blame] | 396 | } |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 397 | } else { |
Jakob Ivarsson | 46dda83 | 2019-07-03 16:00:30 +0200 | [diff] [blame] | 398 | // Keep the same delay as before the CNG, but make sure that the number of |
| 399 | // samples in buffer is no higher than 4 times the optimal level. |
| 400 | if (generated_enough_noise || |
| 401 | cur_size_samples > target_level_samples * 4) { |
| 402 | // Time to play this new packet. |
Ivo Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame] | 403 | return NetEq::Operation::kNormal; |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 404 | } |
| 405 | } |
Jakob Ivarsson | 46dda83 | 2019-07-03 16:00:30 +0200 | [diff] [blame] | 406 | |
| 407 | // Too early to play this new packet; keep on playing comfort noise. |
Ivo Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame] | 408 | if (prev_mode == NetEq::Mode::kRfc3389Cng) { |
| 409 | return NetEq::Operation::kRfc3389CngNoPacket; |
Jakob Ivarsson | 46dda83 | 2019-07-03 16:00:30 +0200 | [diff] [blame] | 410 | } |
| 411 | // prevPlayMode == kModeCodecInternalCng. |
Ivo Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame] | 412 | return NetEq::Operation::kCodecInternalCng; |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 413 | } |
Jakob Ivarsson | 46dda83 | 2019-07-03 16:00:30 +0200 | [diff] [blame] | 414 | |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 415 | // Do not merge unless we have done an expand before. |
Ivo Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame] | 416 | if (prev_mode == NetEq::Mode::kExpand) { |
| 417 | return NetEq::Operation::kMerge; |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 418 | } else if (play_dtmf) { |
| 419 | // Play DTMF instead of expand. |
Ivo Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame] | 420 | return NetEq::Operation::kDtmf; |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 421 | } else { |
Ivo Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame] | 422 | return NetEq::Operation::kExpand; |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 423 | } |
| 424 | } |
| 425 | |
| 426 | bool DecisionLogic::UnderTargetLevel() const { |
Jakob Ivarsson | 609b047 | 2020-10-19 09:19:34 +0200 | [diff] [blame] | 427 | return buffer_level_filter_->filtered_current_level() < |
Jakob Ivarsson | 80fb978 | 2020-10-09 13:41:06 +0200 | [diff] [blame] | 428 | delay_manager_->TargetDelayMs() * sample_rate_ / 1000; |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 429 | } |
| 430 | |
| 431 | bool DecisionLogic::ReinitAfterExpands(uint32_t timestamp_leap) const { |
| 432 | return timestamp_leap >= |
| 433 | static_cast<uint32_t>(output_size_samples_ * kReinitAfterExpands); |
| 434 | } |
| 435 | |
| 436 | bool DecisionLogic::PacketTooEarly(uint32_t timestamp_leap) const { |
| 437 | return timestamp_leap > |
| 438 | static_cast<uint32_t>(output_size_samples_ * num_consecutive_expands_); |
| 439 | } |
| 440 | |
| 441 | bool DecisionLogic::MaxWaitForPacket() const { |
| 442 | return num_consecutive_expands_ >= kMaxWaitForPacket; |
| 443 | } |
| 444 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 445 | } // namespace webrtc |