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 | #ifndef MODULES_AUDIO_CODING_NETEQ_DECISION_LOGIC_H_ |
| 12 | #define MODULES_AUDIO_CODING_NETEQ_DECISION_LOGIC_H_ |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 13 | |
Ivo Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame] | 14 | #include "api/neteq/neteq.h" |
| 15 | #include "api/neteq/neteq_controller.h" |
| 16 | #include "api/neteq/tick_timer.h" |
Ivo Creusen | 53a31f7 | 2019-10-24 15:20:39 +0200 | [diff] [blame] | 17 | #include "modules/audio_coding/neteq/buffer_level_filter.h" |
Ivo Creusen | 53a31f7 | 2019-10-24 15:20:39 +0200 | [diff] [blame] | 18 | #include "modules/audio_coding/neteq/delay_manager.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 19 | #include "rtc_base/constructor_magic.h" |
Jakob Ivarsson | 46dda83 | 2019-07-03 16:00:30 +0200 | [diff] [blame] | 20 | #include "rtc_base/experiments/field_trial_parser.h" |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 21 | |
| 22 | namespace webrtc { |
| 23 | |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 24 | // This is the class for the decision tree implementation. |
Ivo Creusen | 53a31f7 | 2019-10-24 15:20:39 +0200 | [diff] [blame] | 25 | class DecisionLogic : public NetEqController { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 26 | public: |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 27 | static const int kReinitAfterExpands = 100; |
| 28 | static const int kMaxWaitForPacket = 10; |
| 29 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 30 | // Constructor. |
Ivo Creusen | 53a31f7 | 2019-10-24 15:20:39 +0200 | [diff] [blame] | 31 | DecisionLogic(NetEqController::Config config); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 32 | |
Ivo Creusen | 53a31f7 | 2019-10-24 15:20:39 +0200 | [diff] [blame] | 33 | ~DecisionLogic() override; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 34 | |
| 35 | // Resets object to a clean state. |
Ivo Creusen | 53a31f7 | 2019-10-24 15:20:39 +0200 | [diff] [blame] | 36 | void Reset() override; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 37 | |
| 38 | // Resets parts of the state. Typically done when switching codecs. |
Ivo Creusen | 53a31f7 | 2019-10-24 15:20:39 +0200 | [diff] [blame] | 39 | void SoftReset() override; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 40 | |
| 41 | // Sets the sample rate and the output block size. |
Ivo Creusen | 53a31f7 | 2019-10-24 15:20:39 +0200 | [diff] [blame] | 42 | void SetSampleRate(int fs_hz, size_t output_size_samples) override; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 43 | |
Ivo Creusen | 53a31f7 | 2019-10-24 15:20:39 +0200 | [diff] [blame] | 44 | // Given info about the latest received packet, and current jitter buffer |
| 45 | // status, returns the operation. |target_timestamp| and |expand_mutefactor| |
| 46 | // are provided for reference. |last_packet_samples| is the number of samples |
ossu | 7a37761 | 2016-10-18 04:06:13 -0700 | [diff] [blame] | 47 | // obtained from the last decoded frame. If there is a packet available, it |
Ivo Creusen | 53a31f7 | 2019-10-24 15:20:39 +0200 | [diff] [blame] | 48 | // should be supplied in |packet|; otherwise it should be NULL. The mode |
ossu | 7a37761 | 2016-10-18 04:06:13 -0700 | [diff] [blame] | 49 | // resulting from the last call to NetEqImpl::GetAudio is supplied in |
Ivo Creusen | 53a31f7 | 2019-10-24 15:20:39 +0200 | [diff] [blame] | 50 | // |last_mode|. If there is a DTMF event to play, |play_dtmf| should be set to |
ossu | 7a37761 | 2016-10-18 04:06:13 -0700 | [diff] [blame] | 51 | // true. The output variable |reset_decoder| will be set to true if a reset is |
| 52 | // required; otherwise it is left unchanged (i.e., it can remain true if it |
Ivo Creusen | 53a31f7 | 2019-10-24 15:20:39 +0200 | [diff] [blame] | 53 | // was true before the call). |
Ivo Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame] | 54 | NetEq::Operation GetDecision(const NetEqController::NetEqStatus& status, |
| 55 | bool* reset_decoder) override; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 56 | |
| 57 | // These methods test the |cng_state_| for different conditions. |
Ivo Creusen | 53a31f7 | 2019-10-24 15:20:39 +0200 | [diff] [blame] | 58 | bool CngRfc3389On() const override { return cng_state_ == kCngRfc3389On; } |
| 59 | bool CngOff() const override { return cng_state_ == kCngOff; } |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 60 | |
| 61 | // Resets the |cng_state_| to kCngOff. |
Ivo Creusen | 53a31f7 | 2019-10-24 15:20:39 +0200 | [diff] [blame] | 62 | void SetCngOff() override { cng_state_ = kCngOff; } |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 63 | |
| 64 | // Reports back to DecisionLogic whether the decision to do expand remains or |
| 65 | // not. Note that this is necessary, since an expand decision can be changed |
| 66 | // to kNormal in NetEqImpl::GetDecision if there is still enough data in the |
| 67 | // sync buffer. |
Ivo Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame] | 68 | void ExpandDecision(NetEq::Operation operation) override; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 69 | |
| 70 | // Adds |value| to |sample_memory_|. |
Ivo Creusen | 53a31f7 | 2019-10-24 15:20:39 +0200 | [diff] [blame] | 71 | void AddSampleMemory(int32_t value) override { sample_memory_ += value; } |
| 72 | |
Jakob Ivarsson | 80fb978 | 2020-10-09 13:41:06 +0200 | [diff] [blame] | 73 | int TargetLevelMs() const override { return delay_manager_->TargetDelayMs(); } |
Ivo Creusen | 53a31f7 | 2019-10-24 15:20:39 +0200 | [diff] [blame] | 74 | |
Ivo Creusen | a2b31c3 | 2020-10-14 17:54:22 +0200 | [diff] [blame^] | 75 | absl::optional<int> PacketArrived(int fs_hz, |
Ivo Creusen | 53a31f7 | 2019-10-24 15:20:39 +0200 | [diff] [blame] | 76 | bool should_update_stats, |
Ivo Creusen | a2b31c3 | 2020-10-14 17:54:22 +0200 | [diff] [blame^] | 77 | const PacketArrivedInfo& info) override; |
Ivo Creusen | 53a31f7 | 2019-10-24 15:20:39 +0200 | [diff] [blame] | 78 | |
Jakob Ivarsson | 80fb978 | 2020-10-09 13:41:06 +0200 | [diff] [blame] | 79 | void RegisterEmptyPacket() override {} |
Ivo Creusen | 53a31f7 | 2019-10-24 15:20:39 +0200 | [diff] [blame] | 80 | |
Ivo Creusen | 4354686 | 2020-10-06 17:29:09 +0200 | [diff] [blame] | 81 | void NotifyMutedState() override {} |
| 82 | |
Ivo Creusen | 53a31f7 | 2019-10-24 15:20:39 +0200 | [diff] [blame] | 83 | bool SetMaximumDelay(int delay_ms) override { |
| 84 | return delay_manager_->SetMaximumDelay(delay_ms); |
| 85 | } |
| 86 | bool SetMinimumDelay(int delay_ms) override { |
| 87 | return delay_manager_->SetMinimumDelay(delay_ms); |
| 88 | } |
| 89 | bool SetBaseMinimumDelay(int delay_ms) override { |
| 90 | return delay_manager_->SetBaseMinimumDelay(delay_ms); |
| 91 | } |
| 92 | int GetBaseMinimumDelay() const override { |
| 93 | return delay_manager_->GetBaseMinimumDelay(); |
| 94 | } |
Jakob Ivarsson | bd5874a | 2020-01-07 17:07:40 +0100 | [diff] [blame] | 95 | bool PeakFound() const override { return false; } |
Ivo Creusen | 53a31f7 | 2019-10-24 15:20:39 +0200 | [diff] [blame] | 96 | |
Ivo Creusen | ca585bb | 2019-11-04 16:40:04 +0100 | [diff] [blame] | 97 | int GetFilteredBufferLevel() const override { |
Ivo Creusen | 53a31f7 | 2019-10-24 15:20:39 +0200 | [diff] [blame] | 98 | return buffer_level_filter_.filtered_current_level(); |
| 99 | } |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 100 | |
| 101 | // Accessors and mutators. |
Ivo Creusen | 53a31f7 | 2019-10-24 15:20:39 +0200 | [diff] [blame] | 102 | void set_sample_memory(int32_t value) override { sample_memory_ = value; } |
| 103 | size_t noise_fast_forward() const override { return noise_fast_forward_; } |
| 104 | size_t packet_length_samples() const override { |
| 105 | return packet_length_samples_; |
| 106 | } |
| 107 | void set_packet_length_samples(size_t value) override { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 108 | packet_length_samples_ = value; |
| 109 | } |
Ivo Creusen | 53a31f7 | 2019-10-24 15:20:39 +0200 | [diff] [blame] | 110 | void set_prev_time_scale(bool value) override { prev_time_scale_ = value; } |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 111 | |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 112 | private: |
Henrik Lundin | 47b17dc | 2016-05-10 10:20:59 +0200 | [diff] [blame] | 113 | // The value 5 sets maximum time-stretch rate to about 100 ms/s. |
| 114 | static const int kMinTimescaleInterval = 5; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 115 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 116 | enum CngState { kCngOff, kCngRfc3389On, kCngInternalOn }; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 117 | |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 118 | // Updates the |buffer_level_filter_| with the current buffer level |
Jakob Ivarsson | 80fb978 | 2020-10-09 13:41:06 +0200 | [diff] [blame] | 119 | // |buffer_size_samples|. |
| 120 | void FilterBufferLevel(size_t buffer_size_samples); |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 121 | |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 122 | // Returns the operation given that the next available packet is a comfort |
| 123 | // noise payload (RFC 3389 only, not codec-internal). |
Ivo Creusen | ca585bb | 2019-11-04 16:40:04 +0100 | [diff] [blame] | 124 | virtual NetEq::Operation CngOperation(NetEq::Mode prev_mode, |
| 125 | uint32_t target_timestamp, |
| 126 | uint32_t available_timestamp, |
| 127 | size_t generated_noise_samples); |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 128 | |
| 129 | // Returns the operation given that no packets are available (except maybe |
| 130 | // a DTMF event, flagged by setting |play_dtmf| true). |
Ivo Creusen | ca585bb | 2019-11-04 16:40:04 +0100 | [diff] [blame] | 131 | virtual NetEq::Operation NoPacket(bool play_dtmf); |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 132 | |
| 133 | // Returns the operation to do given that the expected packet is available. |
Ivo Creusen | ca585bb | 2019-11-04 16:40:04 +0100 | [diff] [blame] | 134 | virtual NetEq::Operation ExpectedPacketAvailable(NetEq::Mode prev_mode, |
| 135 | bool play_dtmf); |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 136 | |
| 137 | // Returns the operation to do given that the expected packet is not |
| 138 | // available, but a packet further into the future is at hand. |
Ivo Creusen | ca585bb | 2019-11-04 16:40:04 +0100 | [diff] [blame] | 139 | virtual NetEq::Operation FuturePacketAvailable( |
| 140 | size_t decoder_frame_length, |
| 141 | NetEq::Mode prev_mode, |
| 142 | uint32_t target_timestamp, |
| 143 | uint32_t available_timestamp, |
| 144 | bool play_dtmf, |
| 145 | size_t generated_noise_samples, |
| 146 | size_t span_samples_in_packet_buffer, |
| 147 | size_t num_packets_in_packet_buffer); |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 148 | |
| 149 | // Checks if enough time has elapsed since the last successful timescale |
| 150 | // operation was done (i.e., accelerate or preemptive expand). |
| 151 | bool TimescaleAllowed() const { |
| 152 | return !timescale_countdown_ || timescale_countdown_->Finished(); |
| 153 | } |
| 154 | |
| 155 | // Checks if the current (filtered) buffer level is under the target level. |
| 156 | bool UnderTargetLevel() const; |
| 157 | |
| 158 | // Checks if |timestamp_leap| is so long into the future that a reset due |
| 159 | // to exceeding kReinitAfterExpands will be done. |
| 160 | bool ReinitAfterExpands(uint32_t timestamp_leap) const; |
| 161 | |
| 162 | // Checks if we still have not done enough expands to cover the distance from |
| 163 | // the last decoded packet to the next available packet, the distance beeing |
| 164 | // conveyed in |timestamp_leap|. |
| 165 | bool PacketTooEarly(uint32_t timestamp_leap) const; |
| 166 | |
| 167 | // Checks if num_consecutive_expands_ >= kMaxWaitForPacket. |
| 168 | bool MaxWaitForPacket() const; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 169 | |
Ivo Creusen | 53a31f7 | 2019-10-24 15:20:39 +0200 | [diff] [blame] | 170 | std::unique_ptr<DelayManager> delay_manager_; |
| 171 | BufferLevelFilter buffer_level_filter_; |
Henrik Lundin | 47b17dc | 2016-05-10 10:20:59 +0200 | [diff] [blame] | 172 | const TickTimer* tick_timer_; |
Jakob Ivarsson | 46dda83 | 2019-07-03 16:00:30 +0200 | [diff] [blame] | 173 | int sample_rate_; |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 174 | size_t output_size_samples_; |
Ivo Creusen | 53a31f7 | 2019-10-24 15:20:39 +0200 | [diff] [blame] | 175 | CngState cng_state_ = kCngOff; // Remember if comfort noise is interrupted by |
| 176 | // other event (e.g., DTMF). |
henrik.lundin | b1fb72b | 2016-05-03 08:18:47 -0700 | [diff] [blame] | 177 | size_t noise_fast_forward_ = 0; |
Ivo Creusen | 53a31f7 | 2019-10-24 15:20:39 +0200 | [diff] [blame] | 178 | size_t packet_length_samples_ = 0; |
| 179 | int sample_memory_ = 0; |
| 180 | bool prev_time_scale_ = false; |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 181 | bool disallow_time_stretching_; |
Henrik Lundin | 47b17dc | 2016-05-10 10:20:59 +0200 | [diff] [blame] | 182 | std::unique_ptr<TickTimer::Countdown> timescale_countdown_; |
Ivo Creusen | 53a31f7 | 2019-10-24 15:20:39 +0200 | [diff] [blame] | 183 | int num_consecutive_expands_ = 0; |
| 184 | int time_stretched_cn_samples_ = 0; |
Jakob Ivarsson | 80fb978 | 2020-10-09 13:41:06 +0200 | [diff] [blame] | 185 | bool last_pack_cng_or_dtmf_ = true; |
Jakob Ivarsson | 46dda83 | 2019-07-03 16:00:30 +0200 | [diff] [blame] | 186 | FieldTrialParameter<bool> estimate_dtx_delay_; |
| 187 | FieldTrialParameter<bool> time_stretch_cn_; |
| 188 | FieldTrialConstrained<int> target_level_window_ms_; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 189 | |
henrikg | 3c089d7 | 2015-09-16 05:37:44 -0700 | [diff] [blame] | 190 | RTC_DISALLOW_COPY_AND_ASSIGN(DecisionLogic); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 191 | }; |
| 192 | |
| 193 | } // namespace webrtc |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 194 | #endif // MODULES_AUDIO_CODING_NETEQ_DECISION_LOGIC_H_ |