blob: 008655d1a2bb930d6f184ca394e2648b31c670fd [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 Lundin47b17dc2016-05-10 10:20:59 +020017#include "webrtc/modules/audio_coding/neteq/tick_timer.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000018#include "webrtc/typedefs.h"
19
20namespace webrtc {
21
22// Forward declarations.
23class BufferLevelFilter;
24class DecoderDatabase;
25class DelayManager;
26class Expand;
27class PacketBuffer;
28class SyncBuffer;
29struct RTPHeader;
30
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
69 // obtained from the last decoded frame. If there is a packet available, the
70 // packet header should be supplied in |packet_header|; otherwise it should
71 // be NULL. The mode resulting form the last call to NetEqImpl::GetAudio is
72 // supplied in |prev_mode|. If there is a DTMF event to play, |play_dtmf|
73 // should be set to true. The output variable |reset_decoder| will be set to
74 // true if a reset is required; otherwise it is left unchanged (i.e., it can
75 // remain true if it was true before the call).
76 // This method end with calling GetDecisionSpecialized to get the actual
77 // return value.
78 Operations GetDecision(const SyncBuffer& sync_buffer,
79 const Expand& expand,
Peter Kastingdce40cf2015-08-24 14:52:23 -070080 size_t decoder_frame_length,
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000081 const RTPHeader* packet_header,
82 Modes prev_mode,
83 bool play_dtmf,
henrik.lundinb1fb72b2016-05-03 08:18:47 -070084 size_t generated_noise_samples,
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000085 bool* reset_decoder);
86
87 // These methods test the |cng_state_| for different conditions.
88 bool CngRfc3389On() const { return cng_state_ == kCngRfc3389On; }
89 bool CngOff() const { return cng_state_ == kCngOff; }
90
91 // Resets the |cng_state_| to kCngOff.
92 void SetCngOff() { cng_state_ = kCngOff; }
93
94 // Reports back to DecisionLogic whether the decision to do expand remains or
95 // not. Note that this is necessary, since an expand decision can be changed
96 // to kNormal in NetEqImpl::GetDecision if there is still enough data in the
97 // sync buffer.
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +000098 virtual void ExpandDecision(Operations operation);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000099
100 // Adds |value| to |sample_memory_|.
101 void AddSampleMemory(int32_t value) {
102 sample_memory_ += value;
103 }
104
105 // Accessors and mutators.
106 void set_sample_memory(int32_t value) { sample_memory_ = value; }
henrik.lundinb1fb72b2016-05-03 08:18:47 -0700107 size_t noise_fast_forward() const { return noise_fast_forward_; }
Peter Kastingdce40cf2015-08-24 14:52:23 -0700108 size_t packet_length_samples() const { return packet_length_samples_; }
109 void set_packet_length_samples(size_t value) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000110 packet_length_samples_ = value;
111 }
112 void set_prev_time_scale(bool value) { prev_time_scale_ = value; }
113 NetEqPlayoutMode playout_mode() const { return playout_mode_; }
114
115 protected:
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
119 enum CngState {
120 kCngOff,
121 kCngRfc3389On,
122 kCngInternalOn
123 };
124
125 // Returns the operation that should be done next. |sync_buffer| and |expand|
126 // are provided for reference. |decoder_frame_length| is the number of samples
127 // obtained from the last decoded frame. If there is a packet available, the
128 // packet header should be supplied in |packet_header|; otherwise it should
129 // be NULL. The mode resulting form the last call to NetEqImpl::GetAudio is
130 // supplied in |prev_mode|. If there is a DTMF event to play, |play_dtmf|
131 // should be set to true. The output variable |reset_decoder| will be set to
132 // true if a reset is required; otherwise it is left unchanged (i.e., it can
133 // remain true if it was true before the call).
134 // Should be implemented by derived classes.
135 virtual Operations GetDecisionSpecialized(const SyncBuffer& sync_buffer,
136 const Expand& expand,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700137 size_t decoder_frame_length,
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000138 const RTPHeader* packet_header,
139 Modes prev_mode,
140 bool play_dtmf,
henrik.lundinb1fb72b2016-05-03 08:18:47 -0700141 bool* reset_decoder,
142 size_t generated_noise_samples) = 0;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000143
144 // Updates the |buffer_level_filter_| with the current buffer level
145 // |buffer_size_packets|.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700146 void FilterBufferLevel(size_t buffer_size_packets, Modes prev_mode);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000147
148 DecoderDatabase* decoder_database_;
149 const PacketBuffer& packet_buffer_;
150 DelayManager* delay_manager_;
151 BufferLevelFilter* buffer_level_filter_;
Henrik Lundin47b17dc2016-05-10 10:20:59 +0200152 const TickTimer* tick_timer_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000153 int fs_mult_;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700154 size_t output_size_samples_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000155 CngState cng_state_; // Remember if comfort noise is interrupted by other
156 // event (e.g., DTMF).
henrik.lundinb1fb72b2016-05-03 08:18:47 -0700157 size_t noise_fast_forward_ = 0;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700158 size_t packet_length_samples_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000159 int sample_memory_;
160 bool prev_time_scale_;
Henrik Lundin47b17dc2016-05-10 10:20:59 +0200161 std::unique_ptr<TickTimer::Countdown> timescale_countdown_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000162 int num_consecutive_expands_;
163 const NetEqPlayoutMode playout_mode_;
164
165 private:
henrikg3c089d72015-09-16 05:37:44 -0700166 RTC_DISALLOW_COPY_AND_ASSIGN(DecisionLogic);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000167};
168
169} // namespace webrtc
henrik.lundin@webrtc.org9c55f0f2014-06-09 08:10:28 +0000170#endif // WEBRTC_MODULES_AUDIO_CODING_NETEQ_DECISION_LOGIC_H_