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 | |
Jakob Ivarsson | 609b047 | 2020-10-19 09:19:34 +0200 | [diff] [blame] | 14 | #include <memory> |
| 15 | |
Ivo Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame] | 16 | #include "api/neteq/neteq.h" |
| 17 | #include "api/neteq/neteq_controller.h" |
| 18 | #include "api/neteq/tick_timer.h" |
Ivo Creusen | 53a31f7 | 2019-10-24 15:20:39 +0200 | [diff] [blame] | 19 | #include "modules/audio_coding/neteq/buffer_level_filter.h" |
Ivo Creusen | 53a31f7 | 2019-10-24 15:20:39 +0200 | [diff] [blame] | 20 | #include "modules/audio_coding/neteq/delay_manager.h" |
Jakob Ivarsson | c782cf8 | 2022-05-16 15:28:22 +0200 | [diff] [blame] | 21 | #include "modules/audio_coding/neteq/packet_arrival_history.h" |
Jakob Ivarsson | 46dda83 | 2019-07-03 16:00:30 +0200 | [diff] [blame] | 22 | #include "rtc_base/experiments/field_trial_parser.h" |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 23 | |
| 24 | namespace webrtc { |
| 25 | |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 26 | // This is the class for the decision tree implementation. |
Ivo Creusen | 53a31f7 | 2019-10-24 15:20:39 +0200 | [diff] [blame] | 27 | class DecisionLogic : public NetEqController { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 28 | public: |
Ivo Creusen | 53a31f7 | 2019-10-24 15:20:39 +0200 | [diff] [blame] | 29 | DecisionLogic(NetEqController::Config config); |
Jakob Ivarsson | 609b047 | 2020-10-19 09:19:34 +0200 | [diff] [blame] | 30 | DecisionLogic(NetEqController::Config config, |
| 31 | std::unique_ptr<DelayManager> delay_manager, |
| 32 | std::unique_ptr<BufferLevelFilter> buffer_level_filter); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 33 | |
Ivo Creusen | 53a31f7 | 2019-10-24 15:20:39 +0200 | [diff] [blame] | 34 | ~DecisionLogic() override; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 35 | |
Byoungchan Lee | 604fd2f | 2022-01-21 09:49:39 +0900 | [diff] [blame] | 36 | DecisionLogic(const DecisionLogic&) = delete; |
| 37 | DecisionLogic& operator=(const DecisionLogic&) = delete; |
| 38 | |
Jakob Ivarsson | c782cf8 | 2022-05-16 15:28:22 +0200 | [diff] [blame] | 39 | // Not used. |
| 40 | void Reset() override {} |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 41 | |
| 42 | // Resets parts of the state. Typically done when switching codecs. |
Ivo Creusen | 53a31f7 | 2019-10-24 15:20:39 +0200 | [diff] [blame] | 43 | void SoftReset() override; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 44 | |
| 45 | // Sets the sample rate and the output block size. |
Ivo Creusen | 53a31f7 | 2019-10-24 15:20:39 +0200 | [diff] [blame] | 46 | 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] | 47 | |
Ivo Creusen | 53a31f7 | 2019-10-24 15:20:39 +0200 | [diff] [blame] | 48 | // Given info about the latest received packet, and current jitter buffer |
Artem Titov | d00ce74 | 2021-07-28 20:00:17 +0200 | [diff] [blame] | 49 | // status, returns the operation. `target_timestamp` and `expand_mutefactor` |
| 50 | // are provided for reference. `last_packet_samples` is the number of samples |
ossu | 7a37761 | 2016-10-18 04:06:13 -0700 | [diff] [blame] | 51 | // obtained from the last decoded frame. If there is a packet available, it |
Artem Titov | d00ce74 | 2021-07-28 20:00:17 +0200 | [diff] [blame] | 52 | // should be supplied in `packet`; otherwise it should be NULL. The mode |
ossu | 7a37761 | 2016-10-18 04:06:13 -0700 | [diff] [blame] | 53 | // resulting from the last call to NetEqImpl::GetAudio is supplied in |
Artem Titov | d00ce74 | 2021-07-28 20:00:17 +0200 | [diff] [blame] | 54 | // `last_mode`. If there is a DTMF event to play, `play_dtmf` should be set to |
| 55 | // true. The output variable `reset_decoder` will be set to true if a reset is |
ossu | 7a37761 | 2016-10-18 04:06:13 -0700 | [diff] [blame] | 56 | // 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] | 57 | // was true before the call). |
Ivo Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame] | 58 | NetEq::Operation GetDecision(const NetEqController::NetEqStatus& status, |
| 59 | bool* reset_decoder) override; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 60 | |
Artem Titov | d00ce74 | 2021-07-28 20:00:17 +0200 | [diff] [blame] | 61 | // These methods test the `cng_state_` for different conditions. |
Ivo Creusen | 53a31f7 | 2019-10-24 15:20:39 +0200 | [diff] [blame] | 62 | bool CngRfc3389On() const override { return cng_state_ == kCngRfc3389On; } |
| 63 | bool CngOff() const override { return cng_state_ == kCngOff; } |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 64 | |
Artem Titov | d00ce74 | 2021-07-28 20:00:17 +0200 | [diff] [blame] | 65 | // Resets the `cng_state_` to kCngOff. |
Ivo Creusen | 53a31f7 | 2019-10-24 15:20:39 +0200 | [diff] [blame] | 66 | void SetCngOff() override { cng_state_ = kCngOff; } |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 67 | |
Jakob Ivarsson | ca101e6 | 2022-04-04 21:42:55 +0200 | [diff] [blame] | 68 | void ExpandDecision(NetEq::Operation operation) override {} |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 69 | |
Artem Titov | d00ce74 | 2021-07-28 20:00:17 +0200 | [diff] [blame] | 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 | c782cf8 | 2022-05-16 15:28:22 +0200 | [diff] [blame] | 73 | int TargetLevelMs() const override; |
Ivo Creusen | 53a31f7 | 2019-10-24 15:20:39 +0200 | [diff] [blame] | 74 | |
Ivo Creusen | 1a84b56 | 2022-07-19 16:33:10 +0200 | [diff] [blame] | 75 | int UnlimitedTargetLevelMs() const override; |
| 76 | |
Ivo Creusen | a2b31c3 | 2020-10-14 17:54:22 +0200 | [diff] [blame] | 77 | absl::optional<int> PacketArrived(int fs_hz, |
Ivo Creusen | 53a31f7 | 2019-10-24 15:20:39 +0200 | [diff] [blame] | 78 | bool should_update_stats, |
Ivo Creusen | a2b31c3 | 2020-10-14 17:54:22 +0200 | [diff] [blame] | 79 | const PacketArrivedInfo& info) override; |
Ivo Creusen | 53a31f7 | 2019-10-24 15:20:39 +0200 | [diff] [blame] | 80 | |
Jakob Ivarsson | 80fb978 | 2020-10-09 13:41:06 +0200 | [diff] [blame] | 81 | void RegisterEmptyPacket() override {} |
Ivo Creusen | 53a31f7 | 2019-10-24 15:20:39 +0200 | [diff] [blame] | 82 | |
Jakob Ivarsson | ca101e6 | 2022-04-04 21:42:55 +0200 | [diff] [blame] | 83 | void NotifyMutedState() override; |
Ivo Creusen | 4354686 | 2020-10-06 17:29:09 +0200 | [diff] [blame] | 84 | |
Ivo Creusen | 53a31f7 | 2019-10-24 15:20:39 +0200 | [diff] [blame] | 85 | bool SetMaximumDelay(int delay_ms) override { |
| 86 | return delay_manager_->SetMaximumDelay(delay_ms); |
| 87 | } |
| 88 | bool SetMinimumDelay(int delay_ms) override { |
| 89 | return delay_manager_->SetMinimumDelay(delay_ms); |
| 90 | } |
| 91 | bool SetBaseMinimumDelay(int delay_ms) override { |
| 92 | return delay_manager_->SetBaseMinimumDelay(delay_ms); |
| 93 | } |
| 94 | int GetBaseMinimumDelay() const override { |
| 95 | return delay_manager_->GetBaseMinimumDelay(); |
| 96 | } |
Jakob Ivarsson | bd5874a | 2020-01-07 17:07:40 +0100 | [diff] [blame] | 97 | bool PeakFound() const override { return false; } |
Ivo Creusen | 53a31f7 | 2019-10-24 15:20:39 +0200 | [diff] [blame] | 98 | |
Jakob Ivarsson | c782cf8 | 2022-05-16 15:28:22 +0200 | [diff] [blame] | 99 | int GetFilteredBufferLevel() const override; |
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 | |
Artem Titov | d00ce74 | 2021-07-28 20:00:17 +0200 | [diff] [blame] | 118 | // Updates the `buffer_level_filter_` with the current buffer level |
| 119 | // `buffer_size_samples`. |
Jakob Ivarsson | 80fb978 | 2020-10-09 13:41:06 +0200 | [diff] [blame] | 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). |
Jakob Ivarsson | c782cf8 | 2022-05-16 15:28:22 +0200 | [diff] [blame] | 124 | virtual NetEq::Operation CngOperation(NetEqController::NetEqStatus status); |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 125 | |
| 126 | // Returns the operation given that no packets are available (except maybe |
Artem Titov | d00ce74 | 2021-07-28 20:00:17 +0200 | [diff] [blame] | 127 | // a DTMF event, flagged by setting `play_dtmf` true). |
Jakob Ivarsson | c782cf8 | 2022-05-16 15:28:22 +0200 | [diff] [blame] | 128 | virtual NetEq::Operation NoPacket(NetEqController::NetEqStatus status); |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 129 | |
| 130 | // Returns the operation to do given that the expected packet is available. |
Jakob Ivarsson | c782cf8 | 2022-05-16 15:28:22 +0200 | [diff] [blame] | 131 | virtual NetEq::Operation ExpectedPacketAvailable( |
| 132 | NetEqController::NetEqStatus status); |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 133 | |
| 134 | // Returns the operation to do given that the expected packet is not |
| 135 | // available, but a packet further into the future is at hand. |
Ivo Creusen | ca585bb | 2019-11-04 16:40:04 +0100 | [diff] [blame] | 136 | virtual NetEq::Operation FuturePacketAvailable( |
Jakob Ivarsson | c782cf8 | 2022-05-16 15:28:22 +0200 | [diff] [blame] | 137 | NetEqController::NetEqStatus status); |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 138 | |
| 139 | // Checks if enough time has elapsed since the last successful timescale |
| 140 | // operation was done (i.e., accelerate or preemptive expand). |
| 141 | bool TimescaleAllowed() const { |
| 142 | return !timescale_countdown_ || timescale_countdown_->Finished(); |
| 143 | } |
| 144 | |
| 145 | // Checks if the current (filtered) buffer level is under the target level. |
| 146 | bool UnderTargetLevel() const; |
| 147 | |
Artem Titov | d00ce74 | 2021-07-28 20:00:17 +0200 | [diff] [blame] | 148 | // Checks if `timestamp_leap` is so long into the future that a reset due |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 149 | // to exceeding kReinitAfterExpands will be done. |
| 150 | bool ReinitAfterExpands(uint32_t timestamp_leap) const; |
| 151 | |
| 152 | // Checks if we still have not done enough expands to cover the distance from |
| 153 | // the last decoded packet to the next available packet, the distance beeing |
Artem Titov | d00ce74 | 2021-07-28 20:00:17 +0200 | [diff] [blame] | 154 | // conveyed in `timestamp_leap`. |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 155 | bool PacketTooEarly(uint32_t timestamp_leap) const; |
| 156 | |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 157 | bool MaxWaitForPacket() const; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 158 | |
Jakob Ivarsson | c782cf8 | 2022-05-16 15:28:22 +0200 | [diff] [blame] | 159 | bool ShouldContinueExpand(NetEqController::NetEqStatus status) const; |
| 160 | |
| 161 | int GetNextPacketDelayMs(NetEqController::NetEqStatus status) const; |
| 162 | int GetPlayoutDelayMs(NetEqController::NetEqStatus status) const; |
| 163 | |
| 164 | int LowThreshold() const; |
| 165 | int HighThreshold() const; |
| 166 | int LowThresholdCng() const; |
| 167 | int HighThresholdCng() const; |
| 168 | |
| 169 | // Runtime configurable options through field trial |
| 170 | // WebRTC-Audio-NetEqDecisionLogicConfig. |
| 171 | struct Config { |
| 172 | Config(); |
| 173 | |
| 174 | bool enable_stable_playout_delay = false; |
| 175 | int reinit_after_expands = 100; |
| 176 | int deceleration_target_level_offset_ms = 85; |
| 177 | int packet_history_size_ms = 2000; |
| 178 | }; |
| 179 | Config config_; |
Ivo Creusen | 53a31f7 | 2019-10-24 15:20:39 +0200 | [diff] [blame] | 180 | std::unique_ptr<DelayManager> delay_manager_; |
Jakob Ivarsson | 609b047 | 2020-10-19 09:19:34 +0200 | [diff] [blame] | 181 | std::unique_ptr<BufferLevelFilter> buffer_level_filter_; |
Jakob Ivarsson | c782cf8 | 2022-05-16 15:28:22 +0200 | [diff] [blame] | 182 | PacketArrivalHistory packet_arrival_history_; |
Henrik Lundin | 47b17dc | 2016-05-10 10:20:59 +0200 | [diff] [blame] | 183 | const TickTimer* tick_timer_; |
Jakob Ivarsson | c782cf8 | 2022-05-16 15:28:22 +0200 | [diff] [blame] | 184 | int sample_rate_khz_; |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 185 | size_t output_size_samples_; |
Ivo Creusen | 53a31f7 | 2019-10-24 15:20:39 +0200 | [diff] [blame] | 186 | CngState cng_state_ = kCngOff; // Remember if comfort noise is interrupted by |
| 187 | // other event (e.g., DTMF). |
henrik.lundin | b1fb72b | 2016-05-03 08:18:47 -0700 | [diff] [blame] | 188 | size_t noise_fast_forward_ = 0; |
Ivo Creusen | 53a31f7 | 2019-10-24 15:20:39 +0200 | [diff] [blame] | 189 | size_t packet_length_samples_ = 0; |
| 190 | int sample_memory_ = 0; |
| 191 | bool prev_time_scale_ = false; |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 192 | bool disallow_time_stretching_; |
Henrik Lundin | 47b17dc | 2016-05-10 10:20:59 +0200 | [diff] [blame] | 193 | std::unique_ptr<TickTimer::Countdown> timescale_countdown_; |
Ivo Creusen | 53a31f7 | 2019-10-24 15:20:39 +0200 | [diff] [blame] | 194 | int num_consecutive_expands_ = 0; |
| 195 | int time_stretched_cn_samples_ = 0; |
Ivo Creusen | 7b463c5 | 2020-11-25 11:32:40 +0100 | [diff] [blame] | 196 | bool buffer_flush_ = false; |
Jakob Ivarsson | c782cf8 | 2022-05-16 15:28:22 +0200 | [diff] [blame] | 197 | int last_playout_delay_ms_ = 0; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 198 | }; |
| 199 | |
| 200 | } // namespace webrtc |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 201 | #endif // MODULES_AUDIO_CODING_NETEQ_DECISION_LOGIC_H_ |