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) |
Jakob Ivarsson | bd5874a | 2020-01-07 17:07:40 +0100 | [diff] [blame] | 36 | : delay_manager_(DelayManager::Create(config.max_packets_in_buffer, |
Ivo Creusen | 53a31f7 | 2019-10-24 15:20:39 +0200 | [diff] [blame] | 37 | config.base_min_delay_ms, |
| 38 | config.enable_rtx_handling, |
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; |
Henrik Lundin | 47b17dc | 2016-05-10 10:20:59 +0200 | [diff] [blame] | 70 | timescale_countdown_.reset(); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 71 | num_consecutive_expands_ = 0; |
Jakob Ivarsson | 46dda83 | 2019-07-03 16:00:30 +0200 | [diff] [blame] | 72 | time_stretched_cn_samples_ = 0; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | void DecisionLogic::SoftReset() { |
| 76 | packet_length_samples_ = 0; |
| 77 | sample_memory_ = 0; |
| 78 | prev_time_scale_ = false; |
Henrik Lundin | 47b17dc | 2016-05-10 10:20:59 +0200 | [diff] [blame] | 79 | timescale_countdown_ = |
| 80 | tick_timer_->GetNewCountdown(kMinTimescaleInterval + 1); |
Jakob Ivarsson | 46dda83 | 2019-07-03 16:00:30 +0200 | [diff] [blame] | 81 | time_stretched_cn_samples_ = 0; |
Ivo Creusen | 53a31f7 | 2019-10-24 15:20:39 +0200 | [diff] [blame] | 82 | delay_manager_->Reset(); |
| 83 | buffer_level_filter_.Reset(); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 84 | } |
| 85 | |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 86 | 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] | 87 | // TODO(hlundin): Change to an enumerator and skip assert. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 88 | 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] | 89 | sample_rate_ = fs_hz; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 90 | output_size_samples_ = output_size_samples; |
| 91 | } |
| 92 | |
Ivo Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame] | 93 | NetEq::Operation DecisionLogic::GetDecision(const NetEqStatus& status, |
| 94 | bool* reset_decoder) { |
ossu | 61a208b | 2016-09-20 01:38:00 -0700 | [diff] [blame] | 95 | // If last mode was CNG (or Expand, since this could be covering up for |
| 96 | // a lost CNG packet), remember that CNG is on. This is needed if comfort |
| 97 | // noise is interrupted by DTMF. |
Ivo Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame] | 98 | if (status.last_mode == NetEq::Mode::kRfc3389Cng) { |
ossu | 61a208b | 2016-09-20 01:38:00 -0700 | [diff] [blame] | 99 | cng_state_ = kCngRfc3389On; |
Ivo Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame] | 100 | } else if (status.last_mode == NetEq::Mode::kCodecInternalCng) { |
ossu | 61a208b | 2016-09-20 01:38:00 -0700 | [diff] [blame] | 101 | cng_state_ = kCngInternalOn; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 102 | } |
| 103 | |
Ivo Creusen | 53a31f7 | 2019-10-24 15:20:39 +0200 | [diff] [blame] | 104 | size_t cur_size_samples = estimate_dtx_delay_ |
| 105 | ? status.packet_buffer_info.span_samples |
| 106 | : status.packet_buffer_info.num_samples; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 107 | prev_time_scale_ = |
Ivo Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame] | 108 | prev_time_scale_ && |
| 109 | (status.last_mode == NetEq::Mode::kAccelerateSuccess || |
| 110 | status.last_mode == NetEq::Mode::kAccelerateLowEnergy || |
| 111 | status.last_mode == NetEq::Mode::kPreemptiveExpandSuccess || |
| 112 | status.last_mode == NetEq::Mode::kPreemptiveExpandLowEnergy); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 113 | |
Minyue Li | 7d204d5 | 2019-04-16 11:44:49 +0200 | [diff] [blame] | 114 | // Do not update buffer history if currently playing CNG since it will bias |
| 115 | // the filtered buffer level. |
Ivo Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame] | 116 | if (status.last_mode != NetEq::Mode::kRfc3389Cng && |
| 117 | status.last_mode != NetEq::Mode::kCodecInternalCng && |
Ivo Creusen | 53a31f7 | 2019-10-24 15:20:39 +0200 | [diff] [blame] | 118 | !(status.next_packet && status.next_packet->is_dtx && |
| 119 | !estimate_dtx_delay_)) { |
Minyue Li | 7d204d5 | 2019-04-16 11:44:49 +0200 | [diff] [blame] | 120 | FilterBufferLevel(cur_size_samples); |
| 121 | } |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 122 | |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 123 | // Guard for errors, to avoid getting stuck in error mode. |
Ivo Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame] | 124 | if (status.last_mode == NetEq::Mode::kError) { |
Ivo Creusen | 53a31f7 | 2019-10-24 15:20:39 +0200 | [diff] [blame] | 125 | if (!status.next_packet) { |
Ivo Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame] | 126 | return NetEq::Operation::kExpand; |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 127 | } else { |
Ivo Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame] | 128 | // Use kUndefined to flag for a reset. |
| 129 | return NetEq::Operation::kUndefined; |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 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; |
Ivo Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame] | 148 | return NetEq::Operation::kNormal; |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 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; |
Ivo Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame] | 161 | if ((status.last_mode == NetEq::Mode::kExpand || |
| 162 | status.last_mode == NetEq::Mode::kCodecPlc) && |
Ivo Creusen | 53a31f7 | 2019-10-24 15:20:39 +0200 | [diff] [blame] | 163 | status.expand_mutefactor < 16384 / 2 && |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 164 | current_span<static_cast<size_t>(delay_manager_->TargetLevel() * |
| 165 | packet_length_samples_ * |
| 166 | kPostponeDecodingLevel / 100)>> 8 && |
Ivo Creusen | 53a31f7 | 2019-10-24 15:20:39 +0200 | [diff] [blame] | 167 | !status.packet_buffer_info.dtx_or_cng) { |
Ivo Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame] | 168 | return NetEq::Operation::kExpand; |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 169 | } |
| 170 | |
Jakob Ivarsson | 46dda83 | 2019-07-03 16:00:30 +0200 | [diff] [blame] | 171 | 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] | 172 | // Check if the required packet is available. |
Ivo Creusen | 53a31f7 | 2019-10-24 15:20:39 +0200 | [diff] [blame] | 173 | if (status.target_timestamp == status.next_packet->timestamp) { |
| 174 | return ExpectedPacketAvailable(status.last_mode, status.play_dtmf); |
| 175 | } else if (!PacketBuffer::IsObsoleteTimestamp(status.next_packet->timestamp, |
| 176 | status.target_timestamp, |
| 177 | five_seconds_samples)) { |
| 178 | return FuturePacketAvailable( |
| 179 | status.last_packet_samples, status.last_mode, status.target_timestamp, |
| 180 | status.next_packet->timestamp, status.play_dtmf, |
| 181 | status.generated_noise_samples, status.packet_buffer_info.span_samples, |
| 182 | status.packet_buffer_info.num_packets); |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 183 | } else { |
| 184 | // This implies that available_timestamp < target_timestamp, which can |
| 185 | // 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] | 186 | return NetEq::Operation::kUndefined; |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 187 | } |
| 188 | } |
| 189 | |
Ivo Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame] | 190 | void DecisionLogic::ExpandDecision(NetEq::Operation operation) { |
| 191 | if (operation == NetEq::Operation::kExpand) { |
Henrik Lundin | 5afa61c | 2018-07-02 14:53:24 +0200 | [diff] [blame] | 192 | num_consecutive_expands_++; |
| 193 | } else { |
| 194 | num_consecutive_expands_ = 0; |
| 195 | } |
| 196 | } |
| 197 | |
Ivo Creusen | 53a31f7 | 2019-10-24 15:20:39 +0200 | [diff] [blame] | 198 | absl::optional<int> DecisionLogic::PacketArrived(bool last_cng_or_dtmf, |
| 199 | size_t packet_length_samples, |
| 200 | bool should_update_stats, |
| 201 | uint16_t main_sequence_number, |
| 202 | uint32_t main_timestamp, |
| 203 | int fs_hz) { |
| 204 | delay_manager_->LastDecodedWasCngOrDtmf(last_cng_or_dtmf); |
| 205 | absl::optional<int> relative_delay; |
| 206 | if (delay_manager_->last_pack_cng_or_dtmf() == 0) { |
| 207 | // Calculate the total speech length carried in each packet. |
| 208 | if (packet_length_samples > 0 && |
| 209 | packet_length_samples != packet_length_samples_) { |
| 210 | packet_length_samples_ = packet_length_samples; |
| 211 | delay_manager_->SetPacketAudioLength( |
| 212 | rtc::dchecked_cast<int>((1000 * packet_length_samples) / fs_hz)); |
| 213 | } |
| 214 | |
| 215 | // Update statistics. |
| 216 | if (should_update_stats) { |
| 217 | relative_delay = |
| 218 | delay_manager_->Update(main_sequence_number, main_timestamp, fs_hz); |
| 219 | } |
| 220 | } else if (delay_manager_->last_pack_cng_or_dtmf() == -1) { |
| 221 | // This is first "normal" packet after CNG or DTMF. |
| 222 | // Reset packet time counter and measure time until next packet, |
| 223 | // but don't update statistics. |
| 224 | delay_manager_->set_last_pack_cng_or_dtmf(0); |
| 225 | delay_manager_->ResetPacketIatCount(); |
| 226 | } |
| 227 | return relative_delay; |
| 228 | } |
| 229 | |
Minyue Li | 7d204d5 | 2019-04-16 11:44:49 +0200 | [diff] [blame] | 230 | void DecisionLogic::FilterBufferLevel(size_t buffer_size_samples) { |
Ivo Creusen | 53a31f7 | 2019-10-24 15:20:39 +0200 | [diff] [blame] | 231 | buffer_level_filter_.SetTargetBufferLevel( |
Minyue Li | 7d204d5 | 2019-04-16 11:44:49 +0200 | [diff] [blame] | 232 | delay_manager_->base_target_level()); |
Henrik Lundin | 5afa61c | 2018-07-02 14:53:24 +0200 | [diff] [blame] | 233 | |
Jakob Ivarsson | 46dda83 | 2019-07-03 16:00:30 +0200 | [diff] [blame] | 234 | int time_stretched_samples = time_stretched_cn_samples_; |
Minyue Li | 7d204d5 | 2019-04-16 11:44:49 +0200 | [diff] [blame] | 235 | if (prev_time_scale_) { |
Jakob Ivarsson | 46dda83 | 2019-07-03 16:00:30 +0200 | [diff] [blame] | 236 | time_stretched_samples += sample_memory_; |
Minyue Li | 7d204d5 | 2019-04-16 11:44:49 +0200 | [diff] [blame] | 237 | timescale_countdown_ = tick_timer_->GetNewCountdown(kMinTimescaleInterval); |
| 238 | } |
| 239 | |
Ivo Creusen | 53a31f7 | 2019-10-24 15:20:39 +0200 | [diff] [blame] | 240 | buffer_level_filter_.Update(buffer_size_samples, time_stretched_samples); |
Minyue Li | 7d204d5 | 2019-04-16 11:44:49 +0200 | [diff] [blame] | 241 | prev_time_scale_ = false; |
Jakob Ivarsson | 46dda83 | 2019-07-03 16:00:30 +0200 | [diff] [blame] | 242 | time_stretched_cn_samples_ = 0; |
Henrik Lundin | 5afa61c | 2018-07-02 14:53:24 +0200 | [diff] [blame] | 243 | } |
| 244 | |
Ivo Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame] | 245 | NetEq::Operation DecisionLogic::CngOperation(NetEq::Mode prev_mode, |
| 246 | uint32_t target_timestamp, |
| 247 | uint32_t available_timestamp, |
| 248 | size_t generated_noise_samples) { |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 249 | // Signed difference between target and available timestamp. |
| 250 | int32_t timestamp_diff = static_cast<int32_t>( |
| 251 | static_cast<uint32_t>(generated_noise_samples + target_timestamp) - |
| 252 | available_timestamp); |
| 253 | int32_t optimal_level_samp = static_cast<int32_t>( |
| 254 | (delay_manager_->TargetLevel() * packet_length_samples_) >> 8); |
| 255 | const int64_t excess_waiting_time_samp = |
| 256 | -static_cast<int64_t>(timestamp_diff) - optimal_level_samp; |
| 257 | |
| 258 | if (excess_waiting_time_samp > optimal_level_samp / 2) { |
| 259 | // The waiting time for this packet will be longer than 1.5 |
| 260 | // times the wanted buffer delay. Apply fast-forward to cut the |
| 261 | // waiting time down to the optimal. |
Jakob Ivarsson | 42b6e2d | 2019-10-21 11:51:05 +0200 | [diff] [blame] | 262 | noise_fast_forward_ = rtc::saturated_cast<size_t>(noise_fast_forward_ + |
| 263 | excess_waiting_time_samp); |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 264 | timestamp_diff = |
| 265 | rtc::saturated_cast<int32_t>(timestamp_diff + excess_waiting_time_samp); |
| 266 | } |
| 267 | |
Ivo Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame] | 268 | if (timestamp_diff < 0 && prev_mode == NetEq::Mode::kRfc3389Cng) { |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 269 | // Not time to play this packet yet. Wait another round before using this |
| 270 | // packet. Keep on playing CNG from previous CNG parameters. |
Ivo Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame] | 271 | return NetEq::Operation::kRfc3389CngNoPacket; |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 272 | } else { |
| 273 | // Otherwise, go for the CNG packet now. |
| 274 | noise_fast_forward_ = 0; |
Ivo Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame] | 275 | return NetEq::Operation::kRfc3389Cng; |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 276 | } |
| 277 | } |
| 278 | |
Ivo Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame] | 279 | NetEq::Operation DecisionLogic::NoPacket(bool play_dtmf) { |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 280 | if (cng_state_ == kCngRfc3389On) { |
| 281 | // Keep on playing comfort noise. |
Ivo Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame] | 282 | return NetEq::Operation::kRfc3389CngNoPacket; |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 283 | } else if (cng_state_ == kCngInternalOn) { |
| 284 | // Keep on playing codec internal comfort noise. |
Ivo Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame] | 285 | return NetEq::Operation::kCodecInternalCng; |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 286 | } else if (play_dtmf) { |
Ivo Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame] | 287 | return NetEq::Operation::kDtmf; |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 288 | } else { |
| 289 | // Nothing to play, do expand. |
Ivo Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame] | 290 | return NetEq::Operation::kExpand; |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 291 | } |
| 292 | } |
| 293 | |
Ivo Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame] | 294 | NetEq::Operation DecisionLogic::ExpectedPacketAvailable(NetEq::Mode prev_mode, |
| 295 | bool play_dtmf) { |
| 296 | if (!disallow_time_stretching_ && prev_mode != NetEq::Mode::kExpand && |
| 297 | !play_dtmf) { |
Jakob Ivarsson | a36c591 | 2019-06-27 10:12:02 +0200 | [diff] [blame] | 298 | // Check criterion for time-stretching. The values are in number of packets |
| 299 | // in Q8. |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 300 | int low_limit, high_limit; |
| 301 | delay_manager_->BufferLimits(&low_limit, &high_limit); |
Jakob Ivarsson | a36c591 | 2019-06-27 10:12:02 +0200 | [diff] [blame] | 302 | int buffer_level_packets = 0; |
| 303 | if (packet_length_samples_ > 0) { |
| 304 | buffer_level_packets = |
Ivo Creusen | 53a31f7 | 2019-10-24 15:20:39 +0200 | [diff] [blame] | 305 | ((1 << 8) * buffer_level_filter_.filtered_current_level()) / |
Jakob Ivarsson | a36c591 | 2019-06-27 10:12:02 +0200 | [diff] [blame] | 306 | packet_length_samples_; |
| 307 | } |
| 308 | if (buffer_level_packets >= high_limit << 2) |
Ivo Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame] | 309 | return NetEq::Operation::kFastAccelerate; |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 310 | if (TimescaleAllowed()) { |
Jakob Ivarsson | a36c591 | 2019-06-27 10:12:02 +0200 | [diff] [blame] | 311 | if (buffer_level_packets >= high_limit) |
Ivo Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame] | 312 | return NetEq::Operation::kAccelerate; |
Jakob Ivarsson | a36c591 | 2019-06-27 10:12:02 +0200 | [diff] [blame] | 313 | if (buffer_level_packets < low_limit) |
Ivo Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame] | 314 | return NetEq::Operation::kPreemptiveExpand; |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 315 | } |
| 316 | } |
Ivo Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame] | 317 | return NetEq::Operation::kNormal; |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 318 | } |
| 319 | |
Ivo Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame] | 320 | NetEq::Operation DecisionLogic::FuturePacketAvailable( |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 321 | size_t decoder_frame_length, |
Ivo Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame] | 322 | NetEq::Mode prev_mode, |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 323 | uint32_t target_timestamp, |
| 324 | uint32_t available_timestamp, |
| 325 | bool play_dtmf, |
Ivo Creusen | 53a31f7 | 2019-10-24 15:20:39 +0200 | [diff] [blame] | 326 | size_t generated_noise_samples, |
| 327 | size_t span_samples_in_packet_buffer, |
| 328 | size_t num_packets_in_packet_buffer) { |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 329 | // Required packet is not available, but a future packet is. |
| 330 | // Check if we should continue with an ongoing expand because the new packet |
| 331 | // is too far into the future. |
| 332 | uint32_t timestamp_leap = available_timestamp - target_timestamp; |
Ivo Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame] | 333 | if ((prev_mode == NetEq::Mode::kExpand || |
| 334 | prev_mode == NetEq::Mode::kCodecPlc) && |
Henrik Lundin | 00eb12a | 2018-09-05 18:14:52 +0200 | [diff] [blame] | 335 | !ReinitAfterExpands(timestamp_leap) && !MaxWaitForPacket() && |
| 336 | PacketTooEarly(timestamp_leap) && UnderTargetLevel()) { |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 337 | if (play_dtmf) { |
| 338 | // Still have DTMF to play, so do not do expand. |
Ivo Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame] | 339 | return NetEq::Operation::kDtmf; |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 340 | } else { |
| 341 | // Nothing to play. |
Ivo Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame] | 342 | return NetEq::Operation::kExpand; |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 343 | } |
| 344 | } |
| 345 | |
Ivo Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame] | 346 | if (prev_mode == NetEq::Mode::kCodecPlc) { |
| 347 | return NetEq::Operation::kNormal; |
Henrik Lundin | 00eb12a | 2018-09-05 18:14:52 +0200 | [diff] [blame] | 348 | } |
| 349 | |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 350 | // If previous was comfort noise, then no merge is needed. |
Ivo Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame] | 351 | if (prev_mode == NetEq::Mode::kRfc3389Cng || |
| 352 | prev_mode == NetEq::Mode::kCodecInternalCng) { |
Jakob Ivarsson | 46dda83 | 2019-07-03 16:00:30 +0200 | [diff] [blame] | 353 | size_t cur_size_samples = |
| 354 | estimate_dtx_delay_ |
Ivo Creusen | 53a31f7 | 2019-10-24 15:20:39 +0200 | [diff] [blame] | 355 | ? cur_size_samples = span_samples_in_packet_buffer |
| 356 | : num_packets_in_packet_buffer * decoder_frame_length; |
Jakob Ivarsson | 46dda83 | 2019-07-03 16:00:30 +0200 | [diff] [blame] | 357 | // Target level is in number of packets in Q8. |
| 358 | const size_t target_level_samples = |
| 359 | (delay_manager_->TargetLevel() * packet_length_samples_) >> 8; |
| 360 | const bool generated_enough_noise = |
| 361 | static_cast<uint32_t>(generated_noise_samples + target_timestamp) >= |
| 362 | available_timestamp; |
| 363 | |
| 364 | if (time_stretch_cn_) { |
| 365 | const size_t target_threshold_samples = |
| 366 | target_level_window_ms_ / 2 * (sample_rate_ / 1000); |
| 367 | const bool above_target_window = |
| 368 | cur_size_samples > target_level_samples + target_threshold_samples; |
| 369 | const bool below_target_window = |
| 370 | target_level_samples > target_threshold_samples && |
| 371 | cur_size_samples < target_level_samples - target_threshold_samples; |
| 372 | // Keep the delay same as before CNG, but make sure that it is within the |
| 373 | // target window. |
| 374 | if ((generated_enough_noise && !below_target_window) || |
| 375 | above_target_window) { |
| 376 | time_stretched_cn_samples_ = timestamp_leap - generated_noise_samples; |
Ivo Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame] | 377 | return NetEq::Operation::kNormal; |
Jakob Ivarsson | 46dda83 | 2019-07-03 16:00:30 +0200 | [diff] [blame] | 378 | } |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 379 | } else { |
Jakob Ivarsson | 46dda83 | 2019-07-03 16:00:30 +0200 | [diff] [blame] | 380 | // Keep the same delay as before the CNG, but make sure that the number of |
| 381 | // samples in buffer is no higher than 4 times the optimal level. |
| 382 | if (generated_enough_noise || |
| 383 | cur_size_samples > target_level_samples * 4) { |
| 384 | // Time to play this new packet. |
Ivo Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame] | 385 | return NetEq::Operation::kNormal; |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 386 | } |
| 387 | } |
Jakob Ivarsson | 46dda83 | 2019-07-03 16:00:30 +0200 | [diff] [blame] | 388 | |
| 389 | // Too early to play this new packet; keep on playing comfort noise. |
Ivo Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame] | 390 | if (prev_mode == NetEq::Mode::kRfc3389Cng) { |
| 391 | return NetEq::Operation::kRfc3389CngNoPacket; |
Jakob Ivarsson | 46dda83 | 2019-07-03 16:00:30 +0200 | [diff] [blame] | 392 | } |
| 393 | // prevPlayMode == kModeCodecInternalCng. |
Ivo Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame] | 394 | return NetEq::Operation::kCodecInternalCng; |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 395 | } |
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 | // Do not merge unless we have done an expand before. |
Ivo Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame] | 398 | if (prev_mode == NetEq::Mode::kExpand) { |
| 399 | return NetEq::Operation::kMerge; |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 400 | } else if (play_dtmf) { |
| 401 | // Play DTMF instead of expand. |
Ivo Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame] | 402 | return NetEq::Operation::kDtmf; |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 403 | } else { |
Ivo Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame] | 404 | return NetEq::Operation::kExpand; |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 405 | } |
| 406 | } |
| 407 | |
| 408 | bool DecisionLogic::UnderTargetLevel() const { |
Jakob Ivarsson | a36c591 | 2019-06-27 10:12:02 +0200 | [diff] [blame] | 409 | int buffer_level_packets = 0; |
| 410 | if (packet_length_samples_ > 0) { |
| 411 | buffer_level_packets = |
Ivo Creusen | 53a31f7 | 2019-10-24 15:20:39 +0200 | [diff] [blame] | 412 | ((1 << 8) * buffer_level_filter_.filtered_current_level()) / |
Jakob Ivarsson | a36c591 | 2019-06-27 10:12:02 +0200 | [diff] [blame] | 413 | packet_length_samples_; |
| 414 | } |
| 415 | return buffer_level_packets <= delay_manager_->TargetLevel(); |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | bool DecisionLogic::ReinitAfterExpands(uint32_t timestamp_leap) const { |
| 419 | return timestamp_leap >= |
| 420 | static_cast<uint32_t>(output_size_samples_ * kReinitAfterExpands); |
| 421 | } |
| 422 | |
| 423 | bool DecisionLogic::PacketTooEarly(uint32_t timestamp_leap) const { |
| 424 | return timestamp_leap > |
| 425 | static_cast<uint32_t>(output_size_samples_ * num_consecutive_expands_); |
| 426 | } |
| 427 | |
| 428 | bool DecisionLogic::MaxWaitForPacket() const { |
| 429 | return num_consecutive_expands_ >= kMaxWaitForPacket; |
| 430 | } |
| 431 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 432 | } // namespace webrtc |