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