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