blob: d23aa744c370d45962da2273124d0fa6a18c2818 [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020014#include "modules/audio_coding/neteq/defines.h"
15#include "modules/audio_coding/neteq/include/neteq.h"
16#include "modules/audio_coding/neteq/tick_timer.h"
17#include "rtc_base/constructormagic.h"
Mirko Bonadei71207422017-09-15 13:58:09 +020018#include "typedefs.h" // NOLINT(build/include)
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000019
20namespace webrtc {
21
22// Forward declarations.
23class BufferLevelFilter;
24class DecoderDatabase;
25class DelayManager;
26class Expand;
27class PacketBuffer;
28class SyncBuffer;
ossu7a377612016-10-18 04:06:13 -070029struct Packet;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000030
31// This is the base class for the decision tree implementations. Derived classes
32// must implement the method GetDecisionSpecialized().
33class DecisionLogic {
34 public:
35 // Static factory function which creates different types of objects depending
36 // on the |playout_mode|.
37 static DecisionLogic* Create(int fs_hz,
Peter Kastingdce40cf2015-08-24 14:52:23 -070038 size_t output_size_samples,
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000039 NetEqPlayoutMode playout_mode,
40 DecoderDatabase* decoder_database,
41 const PacketBuffer& packet_buffer,
42 DelayManager* delay_manager,
Henrik Lundin47b17dc2016-05-10 10:20:59 +020043 BufferLevelFilter* buffer_level_filter,
44 const TickTimer* tick_timer);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000045
46 // Constructor.
47 DecisionLogic(int fs_hz,
Peter Kastingdce40cf2015-08-24 14:52:23 -070048 size_t output_size_samples,
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000049 NetEqPlayoutMode playout_mode,
50 DecoderDatabase* decoder_database,
51 const PacketBuffer& packet_buffer,
52 DelayManager* delay_manager,
Henrik Lundin47b17dc2016-05-10 10:20:59 +020053 BufferLevelFilter* buffer_level_filter,
54 const TickTimer* tick_timer);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000055
Henrik Lundin47b17dc2016-05-10 10:20:59 +020056 virtual ~DecisionLogic();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000057
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 Kastingdce40cf2015-08-24 14:52:23 -070065 void SetSampleRate(int fs_hz, size_t output_size_samples);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000066
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
ossu7a377612016-10-18 04:06:13 -070069 // 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.orgd94659d2013-01-29 12:09:21 +000077 Operations GetDecision(const SyncBuffer& sync_buffer,
78 const Expand& expand,
Peter Kastingdce40cf2015-08-24 14:52:23 -070079 size_t decoder_frame_length,
ossu7a377612016-10-18 04:06:13 -070080 const Packet* next_packet,
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000081 Modes prev_mode,
82 bool play_dtmf,
henrik.lundinb1fb72b2016-05-03 08:18:47 -070083 size_t generated_noise_samples,
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000084 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.
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +000097 virtual void ExpandDecision(Operations operation);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000098
99 // Adds |value| to |sample_memory_|.
Yves Gerey665174f2018-06-19 15:03:05 +0200100 void AddSampleMemory(int32_t value) { sample_memory_ += value; }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000101
102 // Accessors and mutators.
103 void set_sample_memory(int32_t value) { sample_memory_ = value; }
henrik.lundinb1fb72b2016-05-03 08:18:47 -0700104 size_t noise_fast_forward() const { return noise_fast_forward_; }
Peter Kastingdce40cf2015-08-24 14:52:23 -0700105 size_t packet_length_samples() const { return packet_length_samples_; }
106 void set_packet_length_samples(size_t value) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000107 packet_length_samples_ = value;
108 }
109 void set_prev_time_scale(bool value) { prev_time_scale_ = value; }
110 NetEqPlayoutMode playout_mode() const { return playout_mode_; }
111
112 protected:
Henrik Lundin47b17dc2016-05-10 10:20:59 +0200113 // The value 5 sets maximum time-stretch rate to about 100 ms/s.
114 static const int kMinTimescaleInterval = 5;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000115
Yves Gerey665174f2018-06-19 15:03:05 +0200116 enum CngState { kCngOff, kCngRfc3389On, kCngInternalOn };
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000117
118 // Returns the operation that should be done next. |sync_buffer| and |expand|
119 // are provided for reference. |decoder_frame_length| is the number of samples
ossu7a377612016-10-18 04:06:13 -0700120 // obtained from the last decoded frame. If there is a packet available, it
121 // should be supplied in |next_packet|; otherwise it should be NULL. The mode
122 // resulting from the last call to NetEqImpl::GetAudio is supplied in
123 // |prev_mode|. If there is a DTMF event to play, |play_dtmf| should be set to
124 // true. The output variable |reset_decoder| will be set to true if a reset is
125 // required; otherwise it is left unchanged (i.e., it can remain true if it
126 // was true before the call). Should be implemented by derived classes.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000127 virtual Operations GetDecisionSpecialized(const SyncBuffer& sync_buffer,
128 const Expand& expand,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700129 size_t decoder_frame_length,
ossu7a377612016-10-18 04:06:13 -0700130 const Packet* next_packet,
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000131 Modes prev_mode,
132 bool play_dtmf,
henrik.lundinb1fb72b2016-05-03 08:18:47 -0700133 bool* reset_decoder,
Ivo Creusenc7f09ad2018-05-22 13:21:01 +0200134 size_t generated_noise_samples,
135 size_t cur_size_samples) = 0;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000136
137 // Updates the |buffer_level_filter_| with the current buffer level
138 // |buffer_size_packets|.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700139 void FilterBufferLevel(size_t buffer_size_packets, Modes prev_mode);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000140
141 DecoderDatabase* decoder_database_;
142 const PacketBuffer& packet_buffer_;
143 DelayManager* delay_manager_;
144 BufferLevelFilter* buffer_level_filter_;
Henrik Lundin47b17dc2016-05-10 10:20:59 +0200145 const TickTimer* tick_timer_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000146 int fs_mult_;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700147 size_t output_size_samples_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000148 CngState cng_state_; // Remember if comfort noise is interrupted by other
149 // event (e.g., DTMF).
henrik.lundinb1fb72b2016-05-03 08:18:47 -0700150 size_t noise_fast_forward_ = 0;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700151 size_t packet_length_samples_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000152 int sample_memory_;
153 bool prev_time_scale_;
Henrik Lundin47b17dc2016-05-10 10:20:59 +0200154 std::unique_ptr<TickTimer::Countdown> timescale_countdown_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000155 int num_consecutive_expands_;
156 const NetEqPlayoutMode playout_mode_;
157
158 private:
henrikg3c089d72015-09-16 05:37:44 -0700159 RTC_DISALLOW_COPY_AND_ASSIGN(DecisionLogic);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000160};
161
162} // namespace webrtc
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200163#endif // MODULES_AUDIO_CODING_NETEQ_DECISION_LOGIC_H_