blob: 22fb9f7748d9dbca6f9f5d40b7d5489c5a744fa1 [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 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);
Jakob Ivarsson609b0472020-10-19 09:19:34 +020033 DecisionLogic(NetEqController::Config config,
34 std::unique_ptr<DelayManager> delay_manager,
35 std::unique_ptr<BufferLevelFilter> buffer_level_filter);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000036
Ivo Creusen53a31f72019-10-24 15:20:39 +020037 ~DecisionLogic() override;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000038
Byoungchan Lee604fd2f2022-01-21 09:49:39 +090039 DecisionLogic(const DecisionLogic&) = delete;
40 DecisionLogic& operator=(const DecisionLogic&) = delete;
41
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000042 // Resets object to a clean state.
Ivo Creusen53a31f72019-10-24 15:20:39 +020043 void Reset() override;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000044
45 // Resets parts of the state. Typically done when switching codecs.
Ivo Creusen53a31f72019-10-24 15:20:39 +020046 void SoftReset() override;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000047
48 // Sets the sample rate and the output block size.
Ivo Creusen53a31f72019-10-24 15:20:39 +020049 void SetSampleRate(int fs_hz, size_t output_size_samples) override;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000050
Ivo Creusen53a31f72019-10-24 15:20:39 +020051 // Given info about the latest received packet, and current jitter buffer
Artem Titovd00ce742021-07-28 20:00:17 +020052 // status, returns the operation. `target_timestamp` and `expand_mutefactor`
53 // are provided for reference. `last_packet_samples` is the number of samples
ossu7a377612016-10-18 04:06:13 -070054 // obtained from the last decoded frame. If there is a packet available, it
Artem Titovd00ce742021-07-28 20:00:17 +020055 // should be supplied in `packet`; otherwise it should be NULL. The mode
ossu7a377612016-10-18 04:06:13 -070056 // resulting from the last call to NetEqImpl::GetAudio is supplied in
Artem Titovd00ce742021-07-28 20:00:17 +020057 // `last_mode`. If there is a DTMF event to play, `play_dtmf` should be set to
58 // true. The output variable `reset_decoder` will be set to true if a reset is
ossu7a377612016-10-18 04:06:13 -070059 // required; otherwise it is left unchanged (i.e., it can remain true if it
Ivo Creusen53a31f72019-10-24 15:20:39 +020060 // was true before the call).
Ivo Creusen3ce44a32019-10-31 14:38:11 +010061 NetEq::Operation GetDecision(const NetEqController::NetEqStatus& status,
62 bool* reset_decoder) override;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000063
Artem Titovd00ce742021-07-28 20:00:17 +020064 // These methods test the `cng_state_` for different conditions.
Ivo Creusen53a31f72019-10-24 15:20:39 +020065 bool CngRfc3389On() const override { return cng_state_ == kCngRfc3389On; }
66 bool CngOff() const override { return cng_state_ == kCngOff; }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000067
Artem Titovd00ce742021-07-28 20:00:17 +020068 // Resets the `cng_state_` to kCngOff.
Ivo Creusen53a31f72019-10-24 15:20:39 +020069 void SetCngOff() override { cng_state_ = kCngOff; }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000070
Jakob Ivarssonca101e62022-04-04 21:42:55 +020071 void ExpandDecision(NetEq::Operation operation) override {}
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000072
Artem Titovd00ce742021-07-28 20:00:17 +020073 // Adds `value` to `sample_memory_`.
Ivo Creusen53a31f72019-10-24 15:20:39 +020074 void AddSampleMemory(int32_t value) override { sample_memory_ += value; }
75
Jakob Ivarsson80fb9782020-10-09 13:41:06 +020076 int TargetLevelMs() const override { return delay_manager_->TargetDelayMs(); }
Ivo Creusen53a31f72019-10-24 15:20:39 +020077
Ivo Creusena2b31c32020-10-14 17:54:22 +020078 absl::optional<int> PacketArrived(int fs_hz,
Ivo Creusen53a31f72019-10-24 15:20:39 +020079 bool should_update_stats,
Ivo Creusena2b31c32020-10-14 17:54:22 +020080 const PacketArrivedInfo& info) override;
Ivo Creusen53a31f72019-10-24 15:20:39 +020081
Jakob Ivarsson80fb9782020-10-09 13:41:06 +020082 void RegisterEmptyPacket() override {}
Ivo Creusen53a31f72019-10-24 15:20:39 +020083
Jakob Ivarssonca101e62022-04-04 21:42:55 +020084 void NotifyMutedState() override;
Ivo Creusen43546862020-10-06 17:29:09 +020085
Ivo Creusen53a31f72019-10-24 15:20:39 +020086 bool SetMaximumDelay(int delay_ms) override {
87 return delay_manager_->SetMaximumDelay(delay_ms);
88 }
89 bool SetMinimumDelay(int delay_ms) override {
90 return delay_manager_->SetMinimumDelay(delay_ms);
91 }
92 bool SetBaseMinimumDelay(int delay_ms) override {
93 return delay_manager_->SetBaseMinimumDelay(delay_ms);
94 }
95 int GetBaseMinimumDelay() const override {
96 return delay_manager_->GetBaseMinimumDelay();
97 }
Jakob Ivarssonbd5874a2020-01-07 17:07:40 +010098 bool PeakFound() const override { return false; }
Ivo Creusen53a31f72019-10-24 15:20:39 +020099
Ivo Creusenca585bb2019-11-04 16:40:04 +0100100 int GetFilteredBufferLevel() const override {
Jakob Ivarsson609b0472020-10-19 09:19:34 +0200101 return buffer_level_filter_->filtered_current_level();
Ivo Creusen53a31f72019-10-24 15:20:39 +0200102 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000103
104 // Accessors and mutators.
Ivo Creusen53a31f72019-10-24 15:20:39 +0200105 void set_sample_memory(int32_t value) override { sample_memory_ = value; }
106 size_t noise_fast_forward() const override { return noise_fast_forward_; }
107 size_t packet_length_samples() const override {
108 return packet_length_samples_;
109 }
110 void set_packet_length_samples(size_t value) override {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000111 packet_length_samples_ = value;
112 }
Ivo Creusen53a31f72019-10-24 15:20:39 +0200113 void set_prev_time_scale(bool value) override { prev_time_scale_ = value; }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000114
Henrik Lundin7687ad52018-07-02 10:14:46 +0200115 private:
Henrik Lundin47b17dc2016-05-10 10:20:59 +0200116 // The value 5 sets maximum time-stretch rate to about 100 ms/s.
117 static const int kMinTimescaleInterval = 5;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000118
Yves Gerey665174f2018-06-19 15:03:05 +0200119 enum CngState { kCngOff, kCngRfc3389On, kCngInternalOn };
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000120
Artem Titovd00ce742021-07-28 20:00:17 +0200121 // Updates the `buffer_level_filter_` with the current buffer level
122 // `buffer_size_samples`.
Jakob Ivarsson80fb9782020-10-09 13:41:06 +0200123 void FilterBufferLevel(size_t buffer_size_samples);
Henrik Lundin7687ad52018-07-02 10:14:46 +0200124
Henrik Lundin7687ad52018-07-02 10:14:46 +0200125 // Returns the operation given that the next available packet is a comfort
126 // noise payload (RFC 3389 only, not codec-internal).
Ivo Creusenca585bb2019-11-04 16:40:04 +0100127 virtual NetEq::Operation CngOperation(NetEq::Mode prev_mode,
128 uint32_t target_timestamp,
129 uint32_t available_timestamp,
130 size_t generated_noise_samples);
Henrik Lundin7687ad52018-07-02 10:14:46 +0200131
132 // Returns the operation given that no packets are available (except maybe
Artem Titovd00ce742021-07-28 20:00:17 +0200133 // a DTMF event, flagged by setting `play_dtmf` true).
Ivo Creusenca585bb2019-11-04 16:40:04 +0100134 virtual NetEq::Operation NoPacket(bool play_dtmf);
Henrik Lundin7687ad52018-07-02 10:14:46 +0200135
136 // Returns the operation to do given that the expected packet is available.
Ivo Creusenca585bb2019-11-04 16:40:04 +0100137 virtual NetEq::Operation ExpectedPacketAvailable(NetEq::Mode prev_mode,
138 bool play_dtmf);
Henrik Lundin7687ad52018-07-02 10:14:46 +0200139
140 // Returns the operation to do given that the expected packet is not
141 // available, but a packet further into the future is at hand.
Ivo Creusenca585bb2019-11-04 16:40:04 +0100142 virtual NetEq::Operation FuturePacketAvailable(
143 size_t decoder_frame_length,
144 NetEq::Mode prev_mode,
145 uint32_t target_timestamp,
146 uint32_t available_timestamp,
147 bool play_dtmf,
148 size_t generated_noise_samples,
149 size_t span_samples_in_packet_buffer,
150 size_t num_packets_in_packet_buffer);
Henrik Lundin7687ad52018-07-02 10:14:46 +0200151
152 // Checks if enough time has elapsed since the last successful timescale
153 // operation was done (i.e., accelerate or preemptive expand).
154 bool TimescaleAllowed() const {
155 return !timescale_countdown_ || timescale_countdown_->Finished();
156 }
157
158 // Checks if the current (filtered) buffer level is under the target level.
159 bool UnderTargetLevel() const;
160
Artem Titovd00ce742021-07-28 20:00:17 +0200161 // Checks if `timestamp_leap` is so long into the future that a reset due
Henrik Lundin7687ad52018-07-02 10:14:46 +0200162 // to exceeding kReinitAfterExpands will be done.
163 bool ReinitAfterExpands(uint32_t timestamp_leap) const;
164
165 // Checks if we still have not done enough expands to cover the distance from
166 // the last decoded packet to the next available packet, the distance beeing
Artem Titovd00ce742021-07-28 20:00:17 +0200167 // conveyed in `timestamp_leap`.
Henrik Lundin7687ad52018-07-02 10:14:46 +0200168 bool PacketTooEarly(uint32_t timestamp_leap) const;
169
170 // Checks if num_consecutive_expands_ >= kMaxWaitForPacket.
171 bool MaxWaitForPacket() const;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000172
Ivo Creusen53a31f72019-10-24 15:20:39 +0200173 std::unique_ptr<DelayManager> delay_manager_;
Jakob Ivarsson609b0472020-10-19 09:19:34 +0200174 std::unique_ptr<BufferLevelFilter> buffer_level_filter_;
Henrik Lundin47b17dc2016-05-10 10:20:59 +0200175 const TickTimer* tick_timer_;
Jakob Ivarsson46dda832019-07-03 16:00:30 +0200176 int sample_rate_;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700177 size_t output_size_samples_;
Ivo Creusen53a31f72019-10-24 15:20:39 +0200178 CngState cng_state_ = kCngOff; // Remember if comfort noise is interrupted by
179 // other event (e.g., DTMF).
henrik.lundinb1fb72b2016-05-03 08:18:47 -0700180 size_t noise_fast_forward_ = 0;
Ivo Creusen53a31f72019-10-24 15:20:39 +0200181 size_t packet_length_samples_ = 0;
182 int sample_memory_ = 0;
183 bool prev_time_scale_ = false;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200184 bool disallow_time_stretching_;
Henrik Lundin47b17dc2016-05-10 10:20:59 +0200185 std::unique_ptr<TickTimer::Countdown> timescale_countdown_;
Ivo Creusen53a31f72019-10-24 15:20:39 +0200186 int num_consecutive_expands_ = 0;
187 int time_stretched_cn_samples_ = 0;
Jakob Ivarsson80fb9782020-10-09 13:41:06 +0200188 bool last_pack_cng_or_dtmf_ = true;
Ivo Creusen7b463c52020-11-25 11:32:40 +0100189 bool buffer_flush_ = false;
Jakob Ivarsson46dda832019-07-03 16:00:30 +0200190 FieldTrialConstrained<int> target_level_window_ms_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000191};
192
193} // namespace webrtc
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200194#endif // MODULES_AUDIO_CODING_NETEQ_DECISION_LOGIC_H_