blob: 899f930862ca488943f14d9e0af3ade98ad8d20e [file] [log] [blame]
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001/*
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 Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef MODULES_AUDIO_CODING_NETEQ_DECISION_LOGIC_H_
12#define MODULES_AUDIO_CODING_NETEQ_DECISION_LOGIC_H_
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000013
Ivo Creusen3ce44a32019-10-31 14:38:11 +010014#include "api/neteq/neteq.h"
15#include "api/neteq/neteq_controller.h"
16#include "api/neteq/tick_timer.h"
Ivo Creusen53a31f72019-10-24 15:20:39 +020017#include "modules/audio_coding/neteq/buffer_level_filter.h"
Ivo Creusen53a31f72019-10-24 15:20:39 +020018#include "modules/audio_coding/neteq/delay_manager.h"
19#include "modules/audio_coding/neteq/delay_peak_detector.h"
Steve Anton10542f22019-01-11 09:11:00 -080020#include "rtc_base/constructor_magic.h"
Jakob Ivarsson46dda832019-07-03 16:00:30 +020021#include "rtc_base/experiments/field_trial_parser.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000022
23namespace webrtc {
24
Henrik Lundin7687ad52018-07-02 10:14:46 +020025// This is the class for the decision tree implementation.
Ivo Creusen53a31f72019-10-24 15:20:39 +020026class DecisionLogic : public NetEqController {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000027 public:
Henrik Lundin7687ad52018-07-02 10:14:46 +020028 static const int kReinitAfterExpands = 100;
29 static const int kMaxWaitForPacket = 10;
30
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000031 // Constructor.
Ivo Creusen53a31f72019-10-24 15:20:39 +020032 DecisionLogic(NetEqController::Config config);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000033
Ivo Creusen53a31f72019-10-24 15:20:39 +020034 ~DecisionLogic() override;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000035
36 // Resets object to a clean state.
Ivo Creusen53a31f72019-10-24 15:20:39 +020037 void Reset() override;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000038
39 // Resets parts of the state. Typically done when switching codecs.
Ivo Creusen53a31f72019-10-24 15:20:39 +020040 void SoftReset() override;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000041
42 // Sets the sample rate and the output block size.
Ivo Creusen53a31f72019-10-24 15:20:39 +020043 void SetSampleRate(int fs_hz, size_t output_size_samples) override;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000044
Ivo Creusen53a31f72019-10-24 15:20:39 +020045 // Given info about the latest received packet, and current jitter buffer
46 // status, returns the operation. |target_timestamp| and |expand_mutefactor|
47 // are provided for reference. |last_packet_samples| is the number of samples
ossu7a377612016-10-18 04:06:13 -070048 // obtained from the last decoded frame. If there is a packet available, it
Ivo Creusen53a31f72019-10-24 15:20:39 +020049 // should be supplied in |packet|; otherwise it should be NULL. The mode
ossu7a377612016-10-18 04:06:13 -070050 // resulting from the last call to NetEqImpl::GetAudio is supplied in
Ivo Creusen53a31f72019-10-24 15:20:39 +020051 // |last_mode|. If there is a DTMF event to play, |play_dtmf| should be set to
ossu7a377612016-10-18 04:06:13 -070052 // true. The output variable |reset_decoder| will be set to true if a reset is
53 // required; otherwise it is left unchanged (i.e., it can remain true if it
Ivo Creusen53a31f72019-10-24 15:20:39 +020054 // was true before the call).
Ivo Creusen3ce44a32019-10-31 14:38:11 +010055 NetEq::Operation GetDecision(const NetEqController::NetEqStatus& status,
56 bool* reset_decoder) override;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000057
58 // These methods test the |cng_state_| for different conditions.
Ivo Creusen53a31f72019-10-24 15:20:39 +020059 bool CngRfc3389On() const override { return cng_state_ == kCngRfc3389On; }
60 bool CngOff() const override { return cng_state_ == kCngOff; }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000061
62 // Resets the |cng_state_| to kCngOff.
Ivo Creusen53a31f72019-10-24 15:20:39 +020063 void SetCngOff() override { cng_state_ = kCngOff; }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000064
65 // Reports back to DecisionLogic whether the decision to do expand remains or
66 // not. Note that this is necessary, since an expand decision can be changed
67 // to kNormal in NetEqImpl::GetDecision if there is still enough data in the
68 // sync buffer.
Ivo Creusen3ce44a32019-10-31 14:38:11 +010069 void ExpandDecision(NetEq::Operation operation) override;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000070
71 // Adds |value| to |sample_memory_|.
Ivo Creusen53a31f72019-10-24 15:20:39 +020072 void AddSampleMemory(int32_t value) override { sample_memory_ += value; }
73
74 int TargetLevelMs() override {
75 return ((delay_manager_->TargetLevel() * packet_length_samples_) >> 8) /
76 rtc::CheckedDivExact(sample_rate_, 1000);
77 }
78
79 absl::optional<int> PacketArrived(bool last_cng_or_dtmf,
80 size_t packet_length_samples,
81 bool should_update_stats,
82 uint16_t main_sequence_number,
83 uint32_t main_timestamp,
84 int fs_hz) override;
85
86 void RegisterEmptyPacket() override { delay_manager_->RegisterEmptyPacket(); }
87
88 bool SetMaximumDelay(int delay_ms) override {
89 return delay_manager_->SetMaximumDelay(delay_ms);
90 }
91 bool SetMinimumDelay(int delay_ms) override {
92 return delay_manager_->SetMinimumDelay(delay_ms);
93 }
94 bool SetBaseMinimumDelay(int delay_ms) override {
95 return delay_manager_->SetBaseMinimumDelay(delay_ms);
96 }
97 int GetBaseMinimumDelay() const override {
98 return delay_manager_->GetBaseMinimumDelay();
99 }
100 bool PeakFound() const override { return delay_manager_->PeakFound(); }
101
Ivo Creusenca585bb2019-11-04 16:40:04 +0100102 int GetFilteredBufferLevel() const override {
Ivo Creusen53a31f72019-10-24 15:20:39 +0200103 return buffer_level_filter_.filtered_current_level();
104 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000105
106 // Accessors and mutators.
Ivo Creusen53a31f72019-10-24 15:20:39 +0200107 void set_sample_memory(int32_t value) override { sample_memory_ = value; }
108 size_t noise_fast_forward() const override { return noise_fast_forward_; }
109 size_t packet_length_samples() const override {
110 return packet_length_samples_;
111 }
112 void set_packet_length_samples(size_t value) override {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000113 packet_length_samples_ = value;
114 }
Ivo Creusen53a31f72019-10-24 15:20:39 +0200115 void set_prev_time_scale(bool value) override { prev_time_scale_ = value; }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000116
Henrik Lundin7687ad52018-07-02 10:14:46 +0200117 private:
Henrik Lundin47b17dc2016-05-10 10:20:59 +0200118 // The value 5 sets maximum time-stretch rate to about 100 ms/s.
119 static const int kMinTimescaleInterval = 5;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000120
Yves Gerey665174f2018-06-19 15:03:05 +0200121 enum CngState { kCngOff, kCngRfc3389On, kCngInternalOn };
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000122
Henrik Lundin7687ad52018-07-02 10:14:46 +0200123 // Updates the |buffer_level_filter_| with the current buffer level
124 // |buffer_size_packets|.
Minyue Li7d204d52019-04-16 11:44:49 +0200125 void FilterBufferLevel(size_t buffer_size_packets);
Henrik Lundin7687ad52018-07-02 10:14:46 +0200126
Henrik Lundin7687ad52018-07-02 10:14:46 +0200127 // Returns the operation given that the next available packet is a comfort
128 // noise payload (RFC 3389 only, not codec-internal).
Ivo Creusenca585bb2019-11-04 16:40:04 +0100129 virtual NetEq::Operation CngOperation(NetEq::Mode prev_mode,
130 uint32_t target_timestamp,
131 uint32_t available_timestamp,
132 size_t generated_noise_samples);
Henrik Lundin7687ad52018-07-02 10:14:46 +0200133
134 // Returns the operation given that no packets are available (except maybe
135 // a DTMF event, flagged by setting |play_dtmf| true).
Ivo Creusenca585bb2019-11-04 16:40:04 +0100136 virtual NetEq::Operation NoPacket(bool play_dtmf);
Henrik Lundin7687ad52018-07-02 10:14:46 +0200137
138 // Returns the operation to do given that the expected packet is available.
Ivo Creusenca585bb2019-11-04 16:40:04 +0100139 virtual NetEq::Operation ExpectedPacketAvailable(NetEq::Mode prev_mode,
140 bool play_dtmf);
Henrik Lundin7687ad52018-07-02 10:14:46 +0200141
142 // Returns the operation to do given that the expected packet is not
143 // available, but a packet further into the future is at hand.
Ivo Creusenca585bb2019-11-04 16:40:04 +0100144 virtual NetEq::Operation FuturePacketAvailable(
145 size_t decoder_frame_length,
146 NetEq::Mode prev_mode,
147 uint32_t target_timestamp,
148 uint32_t available_timestamp,
149 bool play_dtmf,
150 size_t generated_noise_samples,
151 size_t span_samples_in_packet_buffer,
152 size_t num_packets_in_packet_buffer);
Henrik Lundin7687ad52018-07-02 10:14:46 +0200153
154 // Checks if enough time has elapsed since the last successful timescale
155 // operation was done (i.e., accelerate or preemptive expand).
156 bool TimescaleAllowed() const {
157 return !timescale_countdown_ || timescale_countdown_->Finished();
158 }
159
160 // Checks if the current (filtered) buffer level is under the target level.
161 bool UnderTargetLevel() const;
162
163 // Checks if |timestamp_leap| is so long into the future that a reset due
164 // to exceeding kReinitAfterExpands will be done.
165 bool ReinitAfterExpands(uint32_t timestamp_leap) const;
166
167 // Checks if we still have not done enough expands to cover the distance from
168 // the last decoded packet to the next available packet, the distance beeing
169 // conveyed in |timestamp_leap|.
170 bool PacketTooEarly(uint32_t timestamp_leap) const;
171
172 // Checks if num_consecutive_expands_ >= kMaxWaitForPacket.
173 bool MaxWaitForPacket() const;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000174
Ivo Creusen53a31f72019-10-24 15:20:39 +0200175 DelayPeakDetector delay_peak_detector_;
176 std::unique_ptr<DelayManager> delay_manager_;
177 BufferLevelFilter buffer_level_filter_;
Henrik Lundin47b17dc2016-05-10 10:20:59 +0200178 const TickTimer* tick_timer_;
Jakob Ivarsson46dda832019-07-03 16:00:30 +0200179 int sample_rate_;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700180 size_t output_size_samples_;
Ivo Creusen53a31f72019-10-24 15:20:39 +0200181 CngState cng_state_ = kCngOff; // Remember if comfort noise is interrupted by
182 // other event (e.g., DTMF).
henrik.lundinb1fb72b2016-05-03 08:18:47 -0700183 size_t noise_fast_forward_ = 0;
Ivo Creusen53a31f72019-10-24 15:20:39 +0200184 size_t packet_length_samples_ = 0;
185 int sample_memory_ = 0;
186 bool prev_time_scale_ = false;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200187 bool disallow_time_stretching_;
Henrik Lundin47b17dc2016-05-10 10:20:59 +0200188 std::unique_ptr<TickTimer::Countdown> timescale_countdown_;
Ivo Creusen53a31f72019-10-24 15:20:39 +0200189 int num_consecutive_expands_ = 0;
190 int time_stretched_cn_samples_ = 0;
Jakob Ivarsson46dda832019-07-03 16:00:30 +0200191 FieldTrialParameter<bool> estimate_dtx_delay_;
192 FieldTrialParameter<bool> time_stretch_cn_;
193 FieldTrialConstrained<int> target_level_window_ms_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000194
henrikg3c089d72015-09-16 05:37:44 -0700195 RTC_DISALLOW_COPY_AND_ASSIGN(DecisionLogic);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000196};
197
198} // namespace webrtc
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200199#endif // MODULES_AUDIO_CODING_NETEQ_DECISION_LOGIC_H_