2013-01-29 12:09:21 +00:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
|
|
|
|
|
*
|
|
|
|
|
* Use of this source code is governed by a BSD-style license
|
|
|
|
|
* that can be found in the LICENSE file in the root of the source
|
|
|
|
|
* tree. An additional intellectual property rights grant can be found
|
|
|
|
|
* in the file PATENTS. All contributing project authors may
|
|
|
|
|
* be found in the AUTHORS file in the root of the source tree.
|
|
|
|
|
*/
|
|
|
|
|
|
2017-09-15 06:47:31 +02:00
|
|
|
#ifndef MODULES_AUDIO_CODING_NETEQ_DECISION_LOGIC_H_
|
|
|
|
|
#define MODULES_AUDIO_CODING_NETEQ_DECISION_LOGIC_H_
|
2013-01-29 12:09:21 +00:00
|
|
|
|
2020-10-19 09:19:34 +02:00
|
|
|
#include <memory>
|
|
|
|
|
|
2019-10-31 14:38:11 +01:00
|
|
|
#include "api/neteq/neteq.h"
|
|
|
|
|
#include "api/neteq/neteq_controller.h"
|
|
|
|
|
#include "api/neteq/tick_timer.h"
|
2019-10-24 15:20:39 +02:00
|
|
|
#include "modules/audio_coding/neteq/buffer_level_filter.h"
|
|
|
|
|
#include "modules/audio_coding/neteq/delay_manager.h"
|
2019-07-03 16:00:30 +02:00
|
|
|
#include "rtc_base/experiments/field_trial_parser.h"
|
2013-01-29 12:09:21 +00:00
|
|
|
|
|
|
|
|
namespace webrtc {
|
|
|
|
|
|
2018-07-02 10:14:46 +02:00
|
|
|
// This is the class for the decision tree implementation.
|
2019-10-24 15:20:39 +02:00
|
|
|
class DecisionLogic : public NetEqController {
|
2013-01-29 12:09:21 +00:00
|
|
|
public:
|
2018-07-02 10:14:46 +02:00
|
|
|
static const int kReinitAfterExpands = 100;
|
|
|
|
|
static const int kMaxWaitForPacket = 10;
|
|
|
|
|
|
2013-01-29 12:09:21 +00:00
|
|
|
// Constructor.
|
2019-10-24 15:20:39 +02:00
|
|
|
DecisionLogic(NetEqController::Config config);
|
2020-10-19 09:19:34 +02:00
|
|
|
DecisionLogic(NetEqController::Config config,
|
|
|
|
|
std::unique_ptr<DelayManager> delay_manager,
|
|
|
|
|
std::unique_ptr<BufferLevelFilter> buffer_level_filter);
|
2013-01-29 12:09:21 +00:00
|
|
|
|
2019-10-24 15:20:39 +02:00
|
|
|
~DecisionLogic() override;
|
2013-01-29 12:09:21 +00:00
|
|
|
|
2022-01-21 09:49:39 +09:00
|
|
|
DecisionLogic(const DecisionLogic&) = delete;
|
|
|
|
|
DecisionLogic& operator=(const DecisionLogic&) = delete;
|
|
|
|
|
|
2013-01-29 12:09:21 +00:00
|
|
|
// Resets object to a clean state.
|
2019-10-24 15:20:39 +02:00
|
|
|
void Reset() override;
|
2013-01-29 12:09:21 +00:00
|
|
|
|
|
|
|
|
// Resets parts of the state. Typically done when switching codecs.
|
2019-10-24 15:20:39 +02:00
|
|
|
void SoftReset() override;
|
2013-01-29 12:09:21 +00:00
|
|
|
|
|
|
|
|
// Sets the sample rate and the output block size.
|
2019-10-24 15:20:39 +02:00
|
|
|
void SetSampleRate(int fs_hz, size_t output_size_samples) override;
|
2013-01-29 12:09:21 +00:00
|
|
|
|
2019-10-24 15:20:39 +02:00
|
|
|
// Given info about the latest received packet, and current jitter buffer
|
2021-07-28 20:00:17 +02:00
|
|
|
// status, returns the operation. `target_timestamp` and `expand_mutefactor`
|
|
|
|
|
// are provided for reference. `last_packet_samples` is the number of samples
|
2016-10-18 04:06:13 -07:00
|
|
|
// obtained from the last decoded frame. If there is a packet available, it
|
2021-07-28 20:00:17 +02:00
|
|
|
// should be supplied in `packet`; otherwise it should be NULL. The mode
|
2016-10-18 04:06:13 -07:00
|
|
|
// resulting from the last call to NetEqImpl::GetAudio is supplied in
|
2021-07-28 20:00:17 +02:00
|
|
|
// `last_mode`. If there is a DTMF event to play, `play_dtmf` should be set to
|
|
|
|
|
// true. The output variable `reset_decoder` will be set to true if a reset is
|
2016-10-18 04:06:13 -07:00
|
|
|
// required; otherwise it is left unchanged (i.e., it can remain true if it
|
2019-10-24 15:20:39 +02:00
|
|
|
// was true before the call).
|
2019-10-31 14:38:11 +01:00
|
|
|
NetEq::Operation GetDecision(const NetEqController::NetEqStatus& status,
|
|
|
|
|
bool* reset_decoder) override;
|
2013-01-29 12:09:21 +00:00
|
|
|
|
2021-07-28 20:00:17 +02:00
|
|
|
// These methods test the `cng_state_` for different conditions.
|
2019-10-24 15:20:39 +02:00
|
|
|
bool CngRfc3389On() const override { return cng_state_ == kCngRfc3389On; }
|
|
|
|
|
bool CngOff() const override { return cng_state_ == kCngOff; }
|
2013-01-29 12:09:21 +00:00
|
|
|
|
2021-07-28 20:00:17 +02:00
|
|
|
// Resets the `cng_state_` to kCngOff.
|
2019-10-24 15:20:39 +02:00
|
|
|
void SetCngOff() override { cng_state_ = kCngOff; }
|
2013-01-29 12:09:21 +00:00
|
|
|
|
|
|
|
|
// Reports back to DecisionLogic whether the decision to do expand remains or
|
|
|
|
|
// not. Note that this is necessary, since an expand decision can be changed
|
|
|
|
|
// to kNormal in NetEqImpl::GetDecision if there is still enough data in the
|
|
|
|
|
// sync buffer.
|
2019-10-31 14:38:11 +01:00
|
|
|
void ExpandDecision(NetEq::Operation operation) override;
|
2013-01-29 12:09:21 +00:00
|
|
|
|
2021-07-28 20:00:17 +02:00
|
|
|
// Adds `value` to `sample_memory_`.
|
2019-10-24 15:20:39 +02:00
|
|
|
void AddSampleMemory(int32_t value) override { sample_memory_ += value; }
|
|
|
|
|
|
2020-10-09 13:41:06 +02:00
|
|
|
int TargetLevelMs() const override { return delay_manager_->TargetDelayMs(); }
|
2019-10-24 15:20:39 +02:00
|
|
|
|
2020-10-14 17:54:22 +02:00
|
|
|
absl::optional<int> PacketArrived(int fs_hz,
|
2019-10-24 15:20:39 +02:00
|
|
|
bool should_update_stats,
|
2020-10-14 17:54:22 +02:00
|
|
|
const PacketArrivedInfo& info) override;
|
2019-10-24 15:20:39 +02:00
|
|
|
|
2020-10-09 13:41:06 +02:00
|
|
|
void RegisterEmptyPacket() override {}
|
2019-10-24 15:20:39 +02:00
|
|
|
|
2020-10-06 17:29:09 +02:00
|
|
|
void NotifyMutedState() override {}
|
|
|
|
|
|
2019-10-24 15:20:39 +02:00
|
|
|
bool SetMaximumDelay(int delay_ms) override {
|
|
|
|
|
return delay_manager_->SetMaximumDelay(delay_ms);
|
|
|
|
|
}
|
|
|
|
|
bool SetMinimumDelay(int delay_ms) override {
|
|
|
|
|
return delay_manager_->SetMinimumDelay(delay_ms);
|
|
|
|
|
}
|
|
|
|
|
bool SetBaseMinimumDelay(int delay_ms) override {
|
|
|
|
|
return delay_manager_->SetBaseMinimumDelay(delay_ms);
|
|
|
|
|
}
|
|
|
|
|
int GetBaseMinimumDelay() const override {
|
|
|
|
|
return delay_manager_->GetBaseMinimumDelay();
|
|
|
|
|
}
|
2020-01-07 17:07:40 +01:00
|
|
|
bool PeakFound() const override { return false; }
|
2019-10-24 15:20:39 +02:00
|
|
|
|
2019-11-04 16:40:04 +01:00
|
|
|
int GetFilteredBufferLevel() const override {
|
2020-10-19 09:19:34 +02:00
|
|
|
return buffer_level_filter_->filtered_current_level();
|
2019-10-24 15:20:39 +02:00
|
|
|
}
|
2013-01-29 12:09:21 +00:00
|
|
|
|
|
|
|
|
// Accessors and mutators.
|
2019-10-24 15:20:39 +02:00
|
|
|
void set_sample_memory(int32_t value) override { sample_memory_ = value; }
|
|
|
|
|
size_t noise_fast_forward() const override { return noise_fast_forward_; }
|
|
|
|
|
size_t packet_length_samples() const override {
|
|
|
|
|
return packet_length_samples_;
|
|
|
|
|
}
|
|
|
|
|
void set_packet_length_samples(size_t value) override {
|
2013-01-29 12:09:21 +00:00
|
|
|
packet_length_samples_ = value;
|
|
|
|
|
}
|
2019-10-24 15:20:39 +02:00
|
|
|
void set_prev_time_scale(bool value) override { prev_time_scale_ = value; }
|
2013-01-29 12:09:21 +00:00
|
|
|
|
2018-07-02 10:14:46 +02:00
|
|
|
private:
|
2016-05-10 10:20:59 +02:00
|
|
|
// The value 5 sets maximum time-stretch rate to about 100 ms/s.
|
|
|
|
|
static const int kMinTimescaleInterval = 5;
|
2013-01-29 12:09:21 +00:00
|
|
|
|
|
|
|
|
enum CngState { kCngOff, kCngRfc3389On, kCngInternalOn };
|
|
|
|
|
|
2021-07-28 20:00:17 +02:00
|
|
|
// Updates the `buffer_level_filter_` with the current buffer level
|
|
|
|
|
// `buffer_size_samples`.
|
2020-10-09 13:41:06 +02:00
|
|
|
void FilterBufferLevel(size_t buffer_size_samples);
|
2018-07-02 10:14:46 +02:00
|
|
|
|
|
|
|
|
// Returns the operation given that the next available packet is a comfort
|
|
|
|
|
// noise payload (RFC 3389 only, not codec-internal).
|
2019-11-04 16:40:04 +01:00
|
|
|
virtual NetEq::Operation CngOperation(NetEq::Mode prev_mode,
|
|
|
|
|
uint32_t target_timestamp,
|
|
|
|
|
uint32_t available_timestamp,
|
|
|
|
|
size_t generated_noise_samples);
|
2018-07-02 10:14:46 +02:00
|
|
|
|
|
|
|
|
// Returns the operation given that no packets are available (except maybe
|
2021-07-28 20:00:17 +02:00
|
|
|
// a DTMF event, flagged by setting `play_dtmf` true).
|
2019-11-04 16:40:04 +01:00
|
|
|
virtual NetEq::Operation NoPacket(bool play_dtmf);
|
2018-07-02 10:14:46 +02:00
|
|
|
|
|
|
|
|
// Returns the operation to do given that the expected packet is available.
|
2019-11-04 16:40:04 +01:00
|
|
|
virtual NetEq::Operation ExpectedPacketAvailable(NetEq::Mode prev_mode,
|
|
|
|
|
bool play_dtmf);
|
2018-07-02 10:14:46 +02:00
|
|
|
|
|
|
|
|
// Returns the operation to do given that the expected packet is not
|
|
|
|
|
// available, but a packet further into the future is at hand.
|
2019-11-04 16:40:04 +01:00
|
|
|
virtual NetEq::Operation FuturePacketAvailable(
|
|
|
|
|
size_t decoder_frame_length,
|
|
|
|
|
NetEq::Mode prev_mode,
|
|
|
|
|
uint32_t target_timestamp,
|
|
|
|
|
uint32_t available_timestamp,
|
|
|
|
|
bool play_dtmf,
|
|
|
|
|
size_t generated_noise_samples,
|
|
|
|
|
size_t span_samples_in_packet_buffer,
|
|
|
|
|
size_t num_packets_in_packet_buffer);
|
2018-07-02 10:14:46 +02:00
|
|
|
|
|
|
|
|
// Checks if enough time has elapsed since the last successful timescale
|
|
|
|
|
// operation was done (i.e., accelerate or preemptive expand).
|
|
|
|
|
bool TimescaleAllowed() const {
|
|
|
|
|
return !timescale_countdown_ || timescale_countdown_->Finished();
|
|
|
|
|
}
|
2013-01-29 12:09:21 +00:00
|
|
|
|
2018-07-02 10:14:46 +02:00
|
|
|
// Checks if the current (filtered) buffer level is under the target level.
|
|
|
|
|
bool UnderTargetLevel() const;
|
|
|
|
|
|
2021-07-28 20:00:17 +02:00
|
|
|
// Checks if `timestamp_leap` is so long into the future that a reset due
|
2018-07-02 10:14:46 +02:00
|
|
|
// to exceeding kReinitAfterExpands will be done.
|
|
|
|
|
bool ReinitAfterExpands(uint32_t timestamp_leap) const;
|
|
|
|
|
|
|
|
|
|
// Checks if we still have not done enough expands to cover the distance from
|
|
|
|
|
// the last decoded packet to the next available packet, the distance beeing
|
2021-07-28 20:00:17 +02:00
|
|
|
// conveyed in `timestamp_leap`.
|
2018-07-02 10:14:46 +02:00
|
|
|
bool PacketTooEarly(uint32_t timestamp_leap) const;
|
|
|
|
|
|
|
|
|
|
// Checks if num_consecutive_expands_ >= kMaxWaitForPacket.
|
|
|
|
|
bool MaxWaitForPacket() const;
|
2013-01-29 12:09:21 +00:00
|
|
|
|
2019-10-24 15:20:39 +02:00
|
|
|
std::unique_ptr<DelayManager> delay_manager_;
|
2020-10-19 09:19:34 +02:00
|
|
|
std::unique_ptr<BufferLevelFilter> buffer_level_filter_;
|
2016-05-10 10:20:59 +02:00
|
|
|
const TickTimer* tick_timer_;
|
2019-07-03 16:00:30 +02:00
|
|
|
int sample_rate_;
|
Update a ton of audio code to use size_t more correctly and in general reduce
use of int16_t/uint16_t.
This is the upshot of a recommendation by henrik.lundin and kwiberg on an original small change ( https://webrtc-codereview.appspot.com/42569004/#ps1 ) to stop using int16_t just because values could fit in it, and is similar in nature to a previous "mass change to use size_t more" ( https://webrtc-codereview.appspot.com/23129004/ ) which also needed to be split up for review but to land all at once, since, like adding "const", such changes tend to cause a lot of transitive effects.
This was be reviewed and approved in pieces:
https://codereview.webrtc.org/1224093003
https://codereview.webrtc.org/1224123002
https://codereview.webrtc.org/1224163002
https://codereview.webrtc.org/1225133003
https://codereview.webrtc.org/1225173002
https://codereview.webrtc.org/1227163003
https://codereview.webrtc.org/1227203003
https://codereview.webrtc.org/1227213002
https://codereview.webrtc.org/1227893002
https://codereview.webrtc.org/1228793004
https://codereview.webrtc.org/1228803003
https://codereview.webrtc.org/1228823002
https://codereview.webrtc.org/1228823003
https://codereview.webrtc.org/1228843002
https://codereview.webrtc.org/1230693002
https://codereview.webrtc.org/1231713002
The change is being landed as TBR to all the folks who reviewed the above.
BUG=chromium:81439
TEST=none
R=andrew@webrtc.org, pbos@webrtc.org
TBR=aluebs, andrew, asapersson, henrika, hlundin, jan.skoglund, kwiberg, minyue, pbos, pthatcher
Review URL: https://codereview.webrtc.org/1230503003 .
Cr-Commit-Position: refs/heads/master@{#9768}
2015-08-24 14:52:23 -07:00
|
|
|
size_t output_size_samples_;
|
2019-10-24 15:20:39 +02:00
|
|
|
CngState cng_state_ = kCngOff; // Remember if comfort noise is interrupted by
|
|
|
|
|
// other event (e.g., DTMF).
|
2016-05-03 08:18:47 -07:00
|
|
|
size_t noise_fast_forward_ = 0;
|
2019-10-24 15:20:39 +02:00
|
|
|
size_t packet_length_samples_ = 0;
|
|
|
|
|
int sample_memory_ = 0;
|
|
|
|
|
bool prev_time_scale_ = false;
|
2018-07-02 10:14:46 +02:00
|
|
|
bool disallow_time_stretching_;
|
2016-05-10 10:20:59 +02:00
|
|
|
std::unique_ptr<TickTimer::Countdown> timescale_countdown_;
|
2019-10-24 15:20:39 +02:00
|
|
|
int num_consecutive_expands_ = 0;
|
|
|
|
|
int time_stretched_cn_samples_ = 0;
|
2020-10-09 13:41:06 +02:00
|
|
|
bool last_pack_cng_or_dtmf_ = true;
|
2020-11-25 11:32:40 +01:00
|
|
|
bool buffer_flush_ = false;
|
2019-07-03 16:00:30 +02:00
|
|
|
FieldTrialParameter<bool> estimate_dtx_delay_;
|
|
|
|
|
FieldTrialParameter<bool> time_stretch_cn_;
|
|
|
|
|
FieldTrialConstrained<int> target_level_window_ms_;
|
2013-01-29 12:09:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // namespace webrtc
|
2017-09-15 06:47:31 +02:00
|
|
|
#endif // MODULES_AUDIO_CODING_NETEQ_DECISION_LOGIC_H_
|