blob: 18f424aa0984fda2f5f9e4833106b28d7eb32e79 [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
henrik.lundin@webrtc.org9c55f0f2014-06-09 08:10:28 +000011#ifndef WEBRTC_MODULES_AUDIO_CODING_NETEQ_DECISION_LOGIC_H_
12#define WEBRTC_MODULES_AUDIO_CODING_NETEQ_DECISION_LOGIC_H_
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000013
henrike@webrtc.org88fbb2d2014-05-21 21:18:46 +000014#include "webrtc/base/constructormagic.h"
henrik.lundin@webrtc.org9c55f0f2014-06-09 08:10:28 +000015#include "webrtc/modules/audio_coding/neteq/defines.h"
Henrik Kjellander74640892015-10-29 11:31:02 +010016#include "webrtc/modules/audio_coding/neteq/include/neteq.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000017#include "webrtc/typedefs.h"
18
19namespace webrtc {
20
21// Forward declarations.
22class BufferLevelFilter;
23class DecoderDatabase;
24class DelayManager;
25class Expand;
26class PacketBuffer;
27class SyncBuffer;
28struct RTPHeader;
29
30// This is the base class for the decision tree implementations. Derived classes
31// must implement the method GetDecisionSpecialized().
32class DecisionLogic {
33 public:
34 // Static factory function which creates different types of objects depending
35 // on the |playout_mode|.
36 static DecisionLogic* Create(int fs_hz,
Peter Kastingdce40cf2015-08-24 14:52:23 -070037 size_t output_size_samples,
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000038 NetEqPlayoutMode playout_mode,
39 DecoderDatabase* decoder_database,
40 const PacketBuffer& packet_buffer,
41 DelayManager* delay_manager,
42 BufferLevelFilter* buffer_level_filter);
43
44 // Constructor.
45 DecisionLogic(int fs_hz,
Peter Kastingdce40cf2015-08-24 14:52:23 -070046 size_t output_size_samples,
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000047 NetEqPlayoutMode playout_mode,
48 DecoderDatabase* decoder_database,
49 const PacketBuffer& packet_buffer,
50 DelayManager* delay_manager,
51 BufferLevelFilter* buffer_level_filter);
52
53 // Destructor.
54 virtual ~DecisionLogic() {}
55
56 // Resets object to a clean state.
57 void Reset();
58
59 // Resets parts of the state. Typically done when switching codecs.
60 void SoftReset();
61
62 // Sets the sample rate and the output block size.
Peter Kastingdce40cf2015-08-24 14:52:23 -070063 void SetSampleRate(int fs_hz, size_t output_size_samples);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000064
65 // Returns the operation that should be done next. |sync_buffer| and |expand|
66 // are provided for reference. |decoder_frame_length| is the number of samples
67 // obtained from the last decoded frame. If there is a packet available, the
68 // packet header should be supplied in |packet_header|; otherwise it should
69 // be NULL. The mode resulting form the last call to NetEqImpl::GetAudio is
70 // supplied in |prev_mode|. If there is a DTMF event to play, |play_dtmf|
71 // should be set to true. The output variable |reset_decoder| will be set to
72 // true if a reset is required; otherwise it is left unchanged (i.e., it can
73 // remain true if it was true before the call).
74 // This method end with calling GetDecisionSpecialized to get the actual
75 // return value.
76 Operations GetDecision(const SyncBuffer& sync_buffer,
77 const Expand& expand,
Peter Kastingdce40cf2015-08-24 14:52:23 -070078 size_t decoder_frame_length,
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000079 const RTPHeader* packet_header,
80 Modes prev_mode,
81 bool play_dtmf,
henrik.lundinb1fb72b2016-05-03 08:18:47 -070082 size_t generated_noise_samples,
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000083 bool* reset_decoder);
84
85 // These methods test the |cng_state_| for different conditions.
86 bool CngRfc3389On() const { return cng_state_ == kCngRfc3389On; }
87 bool CngOff() const { return cng_state_ == kCngOff; }
88
89 // Resets the |cng_state_| to kCngOff.
90 void SetCngOff() { cng_state_ = kCngOff; }
91
92 // Reports back to DecisionLogic whether the decision to do expand remains or
93 // not. Note that this is necessary, since an expand decision can be changed
94 // to kNormal in NetEqImpl::GetDecision if there is still enough data in the
95 // sync buffer.
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +000096 virtual void ExpandDecision(Operations operation);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000097
98 // Adds |value| to |sample_memory_|.
99 void AddSampleMemory(int32_t value) {
100 sample_memory_ += value;
101 }
102
103 // Accessors and mutators.
104 void set_sample_memory(int32_t value) { sample_memory_ = value; }
henrik.lundinb1fb72b2016-05-03 08:18:47 -0700105 size_t noise_fast_forward() const { return noise_fast_forward_; }
Peter Kastingdce40cf2015-08-24 14:52:23 -0700106 size_t packet_length_samples() const { return packet_length_samples_; }
107 void set_packet_length_samples(size_t value) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000108 packet_length_samples_ = value;
109 }
110 void set_prev_time_scale(bool value) { prev_time_scale_ = value; }
111 NetEqPlayoutMode playout_mode() const { return playout_mode_; }
112
113 protected:
114 // The value 6 sets maximum time-stretch rate to about 100 ms/s.
115 static const int kMinTimescaleInterval = 6;
116
117 enum CngState {
118 kCngOff,
119 kCngRfc3389On,
120 kCngInternalOn
121 };
122
123 // Returns the operation that should be done next. |sync_buffer| and |expand|
124 // are provided for reference. |decoder_frame_length| is the number of samples
125 // obtained from the last decoded frame. If there is a packet available, the
126 // packet header should be supplied in |packet_header|; otherwise it should
127 // be NULL. The mode resulting form the last call to NetEqImpl::GetAudio is
128 // supplied in |prev_mode|. If there is a DTMF event to play, |play_dtmf|
129 // should be set to true. The output variable |reset_decoder| will be set to
130 // true if a reset is required; otherwise it is left unchanged (i.e., it can
131 // remain true if it was true before the call).
132 // Should be implemented by derived classes.
133 virtual Operations GetDecisionSpecialized(const SyncBuffer& sync_buffer,
134 const Expand& expand,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700135 size_t decoder_frame_length,
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000136 const RTPHeader* packet_header,
137 Modes prev_mode,
138 bool play_dtmf,
henrik.lundinb1fb72b2016-05-03 08:18:47 -0700139 bool* reset_decoder,
140 size_t generated_noise_samples) = 0;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000141
142 // Updates the |buffer_level_filter_| with the current buffer level
143 // |buffer_size_packets|.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700144 void FilterBufferLevel(size_t buffer_size_packets, Modes prev_mode);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000145
146 DecoderDatabase* decoder_database_;
147 const PacketBuffer& packet_buffer_;
148 DelayManager* delay_manager_;
149 BufferLevelFilter* buffer_level_filter_;
150 int fs_mult_;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700151 size_t output_size_samples_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000152 CngState cng_state_; // Remember if comfort noise is interrupted by other
153 // event (e.g., DTMF).
henrik.lundinb1fb72b2016-05-03 08:18:47 -0700154 size_t noise_fast_forward_ = 0;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700155 size_t packet_length_samples_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000156 int sample_memory_;
157 bool prev_time_scale_;
158 int timescale_hold_off_;
159 int num_consecutive_expands_;
160 const NetEqPlayoutMode playout_mode_;
161
162 private:
henrikg3c089d72015-09-16 05:37:44 -0700163 RTC_DISALLOW_COPY_AND_ASSIGN(DecisionLogic);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000164};
165
166} // namespace webrtc
henrik.lundin@webrtc.org9c55f0f2014-06-09 08:10:28 +0000167#endif // WEBRTC_MODULES_AUDIO_CODING_NETEQ_DECISION_LOGIC_H_