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