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