blob: a0f590e884c05fc09376e4d9ca2a99c1ff8f60f1 [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
Jakob Ivarsson609b0472020-10-19 09:19:34 +020014#include <memory>
15
Ivo Creusen3ce44a32019-10-31 14:38:11 +010016#include "api/neteq/neteq.h"
17#include "api/neteq/neteq_controller.h"
18#include "api/neteq/tick_timer.h"
Ivo Creusen53a31f72019-10-24 15:20:39 +020019#include "modules/audio_coding/neteq/buffer_level_filter.h"
Ivo Creusen53a31f72019-10-24 15:20:39 +020020#include "modules/audio_coding/neteq/delay_manager.h"
Jakob Ivarssonc782cf82022-05-16 15:28:22 +020021#include "modules/audio_coding/neteq/packet_arrival_history.h"
Jakob Ivarsson46dda832019-07-03 16:00:30 +020022#include "rtc_base/experiments/field_trial_parser.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000023
24namespace webrtc {
25
Henrik Lundin7687ad52018-07-02 10:14:46 +020026// This is the class for the decision tree implementation.
Ivo Creusen53a31f72019-10-24 15:20:39 +020027class DecisionLogic : public NetEqController {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000028 public:
Ivo Creusen53a31f72019-10-24 15:20:39 +020029 DecisionLogic(NetEqController::Config config);
Jakob Ivarsson609b0472020-10-19 09:19:34 +020030 DecisionLogic(NetEqController::Config config,
31 std::unique_ptr<DelayManager> delay_manager,
32 std::unique_ptr<BufferLevelFilter> buffer_level_filter);
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
Byoungchan Lee604fd2f2022-01-21 09:49:39 +090036 DecisionLogic(const DecisionLogic&) = delete;
37 DecisionLogic& operator=(const DecisionLogic&) = delete;
38
Jakob Ivarssonc782cf82022-05-16 15:28:22 +020039 // Not used.
40 void Reset() override {}
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000041
42 // Resets parts of the state. Typically done when switching codecs.
Ivo Creusen53a31f72019-10-24 15:20:39 +020043 void SoftReset() override;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000044
45 // Sets the sample rate and the output block size.
Ivo Creusen53a31f72019-10-24 15:20:39 +020046 void SetSampleRate(int fs_hz, size_t output_size_samples) override;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000047
Ivo Creusen53a31f72019-10-24 15:20:39 +020048 // Given info about the latest received packet, and current jitter buffer
Artem Titovd00ce742021-07-28 20:00:17 +020049 // status, returns the operation. `target_timestamp` and `expand_mutefactor`
50 // are provided for reference. `last_packet_samples` is the number of samples
ossu7a377612016-10-18 04:06:13 -070051 // obtained from the last decoded frame. If there is a packet available, it
Artem Titovd00ce742021-07-28 20:00:17 +020052 // should be supplied in `packet`; otherwise it should be NULL. The mode
ossu7a377612016-10-18 04:06:13 -070053 // resulting from the last call to NetEqImpl::GetAudio is supplied in
Artem Titovd00ce742021-07-28 20:00:17 +020054 // `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
ossu7a377612016-10-18 04:06:13 -070056 // required; otherwise it is left unchanged (i.e., it can remain true if it
Ivo Creusen53a31f72019-10-24 15:20:39 +020057 // was true before the call).
Ivo Creusen3ce44a32019-10-31 14:38:11 +010058 NetEq::Operation GetDecision(const NetEqController::NetEqStatus& status,
59 bool* reset_decoder) override;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000060
Artem Titovd00ce742021-07-28 20:00:17 +020061 // These methods test the `cng_state_` for different conditions.
Ivo Creusen53a31f72019-10-24 15:20:39 +020062 bool CngRfc3389On() const override { return cng_state_ == kCngRfc3389On; }
63 bool CngOff() const override { return cng_state_ == kCngOff; }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000064
Artem Titovd00ce742021-07-28 20:00:17 +020065 // Resets the `cng_state_` to kCngOff.
Ivo Creusen53a31f72019-10-24 15:20:39 +020066 void SetCngOff() override { cng_state_ = kCngOff; }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000067
Jakob Ivarssonca101e62022-04-04 21:42:55 +020068 void ExpandDecision(NetEq::Operation operation) override {}
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000069
Artem Titovd00ce742021-07-28 20:00:17 +020070 // Adds `value` to `sample_memory_`.
Ivo Creusen53a31f72019-10-24 15:20:39 +020071 void AddSampleMemory(int32_t value) override { sample_memory_ += value; }
72
Jakob Ivarssonc782cf82022-05-16 15:28:22 +020073 int TargetLevelMs() const override;
Ivo Creusen53a31f72019-10-24 15:20:39 +020074
Ivo Creusena2b31c32020-10-14 17:54:22 +020075 absl::optional<int> PacketArrived(int fs_hz,
Ivo Creusen53a31f72019-10-24 15:20:39 +020076 bool should_update_stats,
Ivo Creusena2b31c32020-10-14 17:54:22 +020077 const PacketArrivedInfo& info) override;
Ivo Creusen53a31f72019-10-24 15:20:39 +020078
Jakob Ivarsson80fb9782020-10-09 13:41:06 +020079 void RegisterEmptyPacket() override {}
Ivo Creusen53a31f72019-10-24 15:20:39 +020080
Jakob Ivarssonca101e62022-04-04 21:42:55 +020081 void NotifyMutedState() override;
Ivo Creusen43546862020-10-06 17:29:09 +020082
Ivo Creusen53a31f72019-10-24 15:20:39 +020083 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 Ivarssonbd5874a2020-01-07 17:07:40 +010095 bool PeakFound() const override { return false; }
Ivo Creusen53a31f72019-10-24 15:20:39 +020096
Jakob Ivarssonc782cf82022-05-16 15:28:22 +020097 int GetFilteredBufferLevel() const override;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000098
99 // Accessors and mutators.
Ivo Creusen53a31f72019-10-24 15:20:39 +0200100 void set_sample_memory(int32_t value) override { sample_memory_ = value; }
101 size_t noise_fast_forward() const override { return noise_fast_forward_; }
102 size_t packet_length_samples() const override {
103 return packet_length_samples_;
104 }
105 void set_packet_length_samples(size_t value) override {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000106 packet_length_samples_ = value;
107 }
Ivo Creusen53a31f72019-10-24 15:20:39 +0200108 void set_prev_time_scale(bool value) override { prev_time_scale_ = value; }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000109
Henrik Lundin7687ad52018-07-02 10:14:46 +0200110 private:
Henrik Lundin47b17dc2016-05-10 10:20:59 +0200111 // The value 5 sets maximum time-stretch rate to about 100 ms/s.
112 static const int kMinTimescaleInterval = 5;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000113
Yves Gerey665174f2018-06-19 15:03:05 +0200114 enum CngState { kCngOff, kCngRfc3389On, kCngInternalOn };
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000115
Artem Titovd00ce742021-07-28 20:00:17 +0200116 // Updates the `buffer_level_filter_` with the current buffer level
117 // `buffer_size_samples`.
Jakob Ivarsson80fb9782020-10-09 13:41:06 +0200118 void FilterBufferLevel(size_t buffer_size_samples);
Henrik Lundin7687ad52018-07-02 10:14:46 +0200119
Henrik Lundin7687ad52018-07-02 10:14:46 +0200120 // Returns the operation given that the next available packet is a comfort
121 // noise payload (RFC 3389 only, not codec-internal).
Jakob Ivarssonc782cf82022-05-16 15:28:22 +0200122 virtual NetEq::Operation CngOperation(NetEqController::NetEqStatus status);
Henrik Lundin7687ad52018-07-02 10:14:46 +0200123
124 // Returns the operation given that no packets are available (except maybe
Artem Titovd00ce742021-07-28 20:00:17 +0200125 // a DTMF event, flagged by setting `play_dtmf` true).
Jakob Ivarssonc782cf82022-05-16 15:28:22 +0200126 virtual NetEq::Operation NoPacket(NetEqController::NetEqStatus status);
Henrik Lundin7687ad52018-07-02 10:14:46 +0200127
128 // Returns the operation to do given that the expected packet is available.
Jakob Ivarssonc782cf82022-05-16 15:28:22 +0200129 virtual NetEq::Operation ExpectedPacketAvailable(
130 NetEqController::NetEqStatus status);
Henrik Lundin7687ad52018-07-02 10:14:46 +0200131
132 // Returns the operation to do given that the expected packet is not
133 // available, but a packet further into the future is at hand.
Ivo Creusenca585bb2019-11-04 16:40:04 +0100134 virtual NetEq::Operation FuturePacketAvailable(
Jakob Ivarssonc782cf82022-05-16 15:28:22 +0200135 NetEqController::NetEqStatus status);
Henrik Lundin7687ad52018-07-02 10:14:46 +0200136
137 // Checks if enough time has elapsed since the last successful timescale
138 // operation was done (i.e., accelerate or preemptive expand).
139 bool TimescaleAllowed() const {
140 return !timescale_countdown_ || timescale_countdown_->Finished();
141 }
142
143 // Checks if the current (filtered) buffer level is under the target level.
144 bool UnderTargetLevel() const;
145
Artem Titovd00ce742021-07-28 20:00:17 +0200146 // Checks if `timestamp_leap` is so long into the future that a reset due
Henrik Lundin7687ad52018-07-02 10:14:46 +0200147 // to exceeding kReinitAfterExpands will be done.
148 bool ReinitAfterExpands(uint32_t timestamp_leap) const;
149
150 // Checks if we still have not done enough expands to cover the distance from
151 // the last decoded packet to the next available packet, the distance beeing
Artem Titovd00ce742021-07-28 20:00:17 +0200152 // conveyed in `timestamp_leap`.
Henrik Lundin7687ad52018-07-02 10:14:46 +0200153 bool PacketTooEarly(uint32_t timestamp_leap) const;
154
Henrik Lundin7687ad52018-07-02 10:14:46 +0200155 bool MaxWaitForPacket() const;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000156
Jakob Ivarssonc782cf82022-05-16 15:28:22 +0200157 bool ShouldContinueExpand(NetEqController::NetEqStatus status) const;
158
159 int GetNextPacketDelayMs(NetEqController::NetEqStatus status) const;
160 int GetPlayoutDelayMs(NetEqController::NetEqStatus status) const;
161
162 int LowThreshold() const;
163 int HighThreshold() const;
164 int LowThresholdCng() const;
165 int HighThresholdCng() const;
166
167 // Runtime configurable options through field trial
168 // WebRTC-Audio-NetEqDecisionLogicConfig.
169 struct Config {
170 Config();
171
172 bool enable_stable_playout_delay = false;
173 int reinit_after_expands = 100;
174 int deceleration_target_level_offset_ms = 85;
175 int packet_history_size_ms = 2000;
176 };
177 Config config_;
Ivo Creusen53a31f72019-10-24 15:20:39 +0200178 std::unique_ptr<DelayManager> delay_manager_;
Jakob Ivarsson609b0472020-10-19 09:19:34 +0200179 std::unique_ptr<BufferLevelFilter> buffer_level_filter_;
Jakob Ivarssonc782cf82022-05-16 15:28:22 +0200180 PacketArrivalHistory packet_arrival_history_;
Henrik Lundin47b17dc2016-05-10 10:20:59 +0200181 const TickTimer* tick_timer_;
Jakob Ivarssonc782cf82022-05-16 15:28:22 +0200182 int sample_rate_khz_;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700183 size_t output_size_samples_;
Ivo Creusen53a31f72019-10-24 15:20:39 +0200184 CngState cng_state_ = kCngOff; // Remember if comfort noise is interrupted by
185 // other event (e.g., DTMF).
henrik.lundinb1fb72b2016-05-03 08:18:47 -0700186 size_t noise_fast_forward_ = 0;
Ivo Creusen53a31f72019-10-24 15:20:39 +0200187 size_t packet_length_samples_ = 0;
188 int sample_memory_ = 0;
189 bool prev_time_scale_ = false;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200190 bool disallow_time_stretching_;
Henrik Lundin47b17dc2016-05-10 10:20:59 +0200191 std::unique_ptr<TickTimer::Countdown> timescale_countdown_;
Ivo Creusen53a31f72019-10-24 15:20:39 +0200192 int num_consecutive_expands_ = 0;
193 int time_stretched_cn_samples_ = 0;
Jakob Ivarsson80fb9782020-10-09 13:41:06 +0200194 bool last_pack_cng_or_dtmf_ = true;
Ivo Creusen7b463c52020-11-25 11:32:40 +0100195 bool buffer_flush_ = false;
Jakob Ivarssonc782cf82022-05-16 15:28:22 +0200196 int last_playout_delay_ms_ = 0;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000197};
198
199} // namespace webrtc
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200200#endif // MODULES_AUDIO_CODING_NETEQ_DECISION_LOGIC_H_