| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 #ifndef WEBRTC_MODULES_AUDIO_CODING_NETEQ_DECISION_LOGIC_H_ | 11 #ifndef WEBRTC_MODULES_AUDIO_CODING_NETEQ_DECISION_LOGIC_H_ |
| 12 #define WEBRTC_MODULES_AUDIO_CODING_NETEQ_DECISION_LOGIC_H_ | 12 #define WEBRTC_MODULES_AUDIO_CODING_NETEQ_DECISION_LOGIC_H_ |
| 13 | 13 |
| 14 #include "webrtc/base/constructormagic.h" | 14 #include "webrtc/base/constructormagic.h" |
| 15 #include "webrtc/modules/audio_coding/neteq/defines.h" | 15 #include "webrtc/modules/audio_coding/neteq/defines.h" |
| 16 #include "webrtc/modules/audio_coding/neteq/include/neteq.h" | 16 #include "webrtc/modules/audio_coding/neteq/include/neteq.h" |
| 17 #include "webrtc/modules/audio_coding/neteq/tick_timer.h" |
| 17 #include "webrtc/typedefs.h" | 18 #include "webrtc/typedefs.h" |
| 18 | 19 |
| 19 namespace webrtc { | 20 namespace webrtc { |
| 20 | 21 |
| 21 // Forward declarations. | 22 // Forward declarations. |
| 22 class BufferLevelFilter; | 23 class BufferLevelFilter; |
| 23 class DecoderDatabase; | 24 class DecoderDatabase; |
| 24 class DelayManager; | 25 class DelayManager; |
| 25 class Expand; | 26 class Expand; |
| 26 class PacketBuffer; | 27 class PacketBuffer; |
| 27 class SyncBuffer; | 28 class SyncBuffer; |
| 28 struct RTPHeader; | 29 struct RTPHeader; |
| 29 | 30 |
| 30 // This is the base class for the decision tree implementations. Derived classes | 31 // This is the base class for the decision tree implementations. Derived classes |
| 31 // must implement the method GetDecisionSpecialized(). | 32 // must implement the method GetDecisionSpecialized(). |
| 32 class DecisionLogic { | 33 class DecisionLogic { |
| 33 public: | 34 public: |
| 34 // Static factory function which creates different types of objects depending | 35 // Static factory function which creates different types of objects depending |
| 35 // on the |playout_mode|. | 36 // on the |playout_mode|. |
| 36 static DecisionLogic* Create(int fs_hz, | 37 static DecisionLogic* Create(int fs_hz, |
| 37 size_t output_size_samples, | 38 size_t output_size_samples, |
| 38 NetEqPlayoutMode playout_mode, | 39 NetEqPlayoutMode playout_mode, |
| 39 DecoderDatabase* decoder_database, | 40 DecoderDatabase* decoder_database, |
| 40 const PacketBuffer& packet_buffer, | 41 const PacketBuffer& packet_buffer, |
| 41 DelayManager* delay_manager, | 42 DelayManager* delay_manager, |
| 42 BufferLevelFilter* buffer_level_filter); | 43 BufferLevelFilter* buffer_level_filter, |
| 44 const TickTimer& tick_timer); |
| 43 | 45 |
| 44 // Constructor. | 46 // Constructor. |
| 45 DecisionLogic(int fs_hz, | 47 DecisionLogic(int fs_hz, |
| 46 size_t output_size_samples, | 48 size_t output_size_samples, |
| 47 NetEqPlayoutMode playout_mode, | 49 NetEqPlayoutMode playout_mode, |
| 48 DecoderDatabase* decoder_database, | 50 DecoderDatabase* decoder_database, |
| 49 const PacketBuffer& packet_buffer, | 51 const PacketBuffer& packet_buffer, |
| 50 DelayManager* delay_manager, | 52 DelayManager* delay_manager, |
| 51 BufferLevelFilter* buffer_level_filter); | 53 BufferLevelFilter* buffer_level_filter, |
| 54 const TickTimer& tick_timer); |
| 52 | 55 |
| 53 // Destructor. | 56 virtual ~DecisionLogic(); |
| 54 virtual ~DecisionLogic() {} | |
| 55 | 57 |
| 56 // Resets object to a clean state. | 58 // Resets object to a clean state. |
| 57 void Reset(); | 59 void Reset(); |
| 58 | 60 |
| 59 // Resets parts of the state. Typically done when switching codecs. | 61 // Resets parts of the state. Typically done when switching codecs. |
| 60 void SoftReset(); | 62 void SoftReset(); |
| 61 | 63 |
| 62 // Sets the sample rate and the output block size. | 64 // Sets the sample rate and the output block size. |
| 63 void SetSampleRate(int fs_hz, size_t output_size_samples); | 65 void SetSampleRate(int fs_hz, size_t output_size_samples); |
| 64 | 66 |
| 65 // Returns the operation that should be done next. |sync_buffer| and |expand| | 67 // 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 | 68 // 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 | 69 // 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 | 70 // 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 | 71 // 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| | 72 // 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 | 73 // 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 | 74 // 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). | 75 // remain true if it was true before the call). |
| 74 // This method end with calling GetDecisionSpecialized to get the actual | 76 // This method end with calling GetDecisionSpecialized to get the actual |
| 75 // return value. | 77 // return value. |
| 76 Operations GetDecision(const SyncBuffer& sync_buffer, | 78 Operations GetDecision(const SyncBuffer& sync_buffer, |
| 77 const Expand& expand, | 79 const Expand& expand, |
| 78 size_t decoder_frame_length, | 80 size_t decoder_frame_length, |
| 79 const RTPHeader* packet_header, | 81 const RTPHeader* packet_header, |
| 80 Modes prev_mode, | 82 Modes prev_mode, |
| 81 bool play_dtmf, | 83 bool play_dtmf, |
| 84 size_t noise_samples_played, |
| 82 bool* reset_decoder); | 85 bool* reset_decoder); |
| 83 | 86 |
| 84 // These methods test the |cng_state_| for different conditions. | 87 // These methods test the |cng_state_| for different conditions. |
| 85 bool CngRfc3389On() const { return cng_state_ == kCngRfc3389On; } | 88 bool CngRfc3389On() const { return cng_state_ == kCngRfc3389On; } |
| 86 bool CngOff() const { return cng_state_ == kCngOff; } | 89 bool CngOff() const { return cng_state_ == kCngOff; } |
| 87 | 90 |
| 88 // Resets the |cng_state_| to kCngOff. | 91 // Resets the |cng_state_| to kCngOff. |
| 89 void SetCngOff() { cng_state_ = kCngOff; } | 92 void SetCngOff() { cng_state_ = kCngOff; } |
| 90 | 93 |
| 91 // Reports back to DecisionLogic whether the decision to do expand remains or | 94 // Reports back to DecisionLogic whether the decision to do expand remains or |
| 92 // not. Note that this is necessary, since an expand decision can be changed | 95 // not. Note that this is necessary, since an expand decision can be changed |
| 93 // to kNormal in NetEqImpl::GetDecision if there is still enough data in the | 96 // to kNormal in NetEqImpl::GetDecision if there is still enough data in the |
| 94 // sync buffer. | 97 // sync buffer. |
| 95 virtual void ExpandDecision(Operations operation); | 98 virtual void ExpandDecision(Operations operation); |
| 96 | 99 |
| 97 // Adds |value| to |sample_memory_|. | 100 // Adds |value| to |sample_memory_|. |
| 98 void AddSampleMemory(int32_t value) { | 101 void AddSampleMemory(int32_t value) { |
| 99 sample_memory_ += value; | 102 sample_memory_ += value; |
| 100 } | 103 } |
| 101 | 104 |
| 102 // Accessors and mutators. | 105 // Accessors and mutators. |
| 103 void set_sample_memory(int32_t value) { sample_memory_ = value; } | 106 void set_sample_memory(int32_t value) { sample_memory_ = value; } |
| 104 size_t generated_noise_samples() const { return generated_noise_samples_; } | 107 size_t noise_fast_forward() const { return noise_fast_forward_; } |
| 105 void set_generated_noise_samples(size_t value) { | |
| 106 generated_noise_samples_ = value; | |
| 107 } | |
| 108 size_t packet_length_samples() const { return packet_length_samples_; } | 108 size_t packet_length_samples() const { return packet_length_samples_; } |
| 109 void set_packet_length_samples(size_t value) { | 109 void set_packet_length_samples(size_t value) { |
| 110 packet_length_samples_ = value; | 110 packet_length_samples_ = value; |
| 111 } | 111 } |
| 112 void set_prev_time_scale(bool value) { prev_time_scale_ = value; } | 112 void set_prev_time_scale(bool value) { prev_time_scale_ = value; } |
| 113 NetEqPlayoutMode playout_mode() const { return playout_mode_; } | 113 NetEqPlayoutMode playout_mode() const { return playout_mode_; } |
| 114 | 114 |
| 115 protected: | 115 protected: |
| 116 // The value 6 sets maximum time-stretch rate to about 100 ms/s. | 116 // The value 6 sets maximum time-stretch rate to about 100 ms/s. |
| 117 static const int kMinTimescaleInterval = 6; | 117 static const int kMinTimescaleInterval = 5; |
| 118 | 118 |
| 119 enum CngState { | 119 enum CngState { |
| 120 kCngOff, | 120 kCngOff, |
| 121 kCngRfc3389On, | 121 kCngRfc3389On, |
| 122 kCngInternalOn | 122 kCngInternalOn |
| 123 }; | 123 }; |
| 124 | 124 |
| 125 // Returns the operation that should be done next. |sync_buffer| and |expand| | 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 | 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 | 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 | 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 | 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| | 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 | 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 | 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). | 133 // remain true if it was true before the call). |
| 134 // Should be implemented by derived classes. | 134 // Should be implemented by derived classes. |
| 135 virtual Operations GetDecisionSpecialized(const SyncBuffer& sync_buffer, | 135 virtual Operations GetDecisionSpecialized(const SyncBuffer& sync_buffer, |
| 136 const Expand& expand, | 136 const Expand& expand, |
| 137 size_t decoder_frame_length, | 137 size_t decoder_frame_length, |
| 138 const RTPHeader* packet_header, | 138 const RTPHeader* packet_header, |
| 139 Modes prev_mode, | 139 Modes prev_mode, |
| 140 bool play_dtmf, | 140 bool play_dtmf, |
| 141 bool* reset_decoder) = 0; | 141 bool* reset_decoder, |
| 142 size_t noise_samples_played) = 0; |
| 142 | 143 |
| 143 // Updates the |buffer_level_filter_| with the current buffer level | 144 // Updates the |buffer_level_filter_| with the current buffer level |
| 144 // |buffer_size_packets|. | 145 // |buffer_size_packets|. |
| 145 void FilterBufferLevel(size_t buffer_size_packets, Modes prev_mode); | 146 void FilterBufferLevel(size_t buffer_size_packets, Modes prev_mode); |
| 146 | 147 |
| 147 DecoderDatabase* decoder_database_; | 148 DecoderDatabase* decoder_database_; |
| 148 const PacketBuffer& packet_buffer_; | 149 const PacketBuffer& packet_buffer_; |
| 149 DelayManager* delay_manager_; | 150 DelayManager* delay_manager_; |
| 150 BufferLevelFilter* buffer_level_filter_; | 151 BufferLevelFilter* buffer_level_filter_; |
| 152 const TickTimer& tick_timer_; |
| 151 int fs_mult_; | 153 int fs_mult_; |
| 152 size_t output_size_samples_; | 154 size_t output_size_samples_; |
| 153 CngState cng_state_; // Remember if comfort noise is interrupted by other | 155 CngState cng_state_; // Remember if comfort noise is interrupted by other |
| 154 // event (e.g., DTMF). | 156 // event (e.g., DTMF). |
| 155 size_t generated_noise_samples_; | 157 size_t noise_fast_forward_ = 0; |
| 156 size_t packet_length_samples_; | 158 size_t packet_length_samples_; |
| 157 int sample_memory_; | 159 int sample_memory_; |
| 158 bool prev_time_scale_; | 160 bool prev_time_scale_; |
| 159 int timescale_hold_off_; | 161 std::unique_ptr<TickTimer::Countdown> timescale_countdown_; |
| 160 int num_consecutive_expands_; | 162 int num_consecutive_expands_; |
| 161 const NetEqPlayoutMode playout_mode_; | 163 const NetEqPlayoutMode playout_mode_; |
| 162 | 164 |
| 163 private: | 165 private: |
| 164 RTC_DISALLOW_COPY_AND_ASSIGN(DecisionLogic); | 166 RTC_DISALLOW_COPY_AND_ASSIGN(DecisionLogic); |
| 165 }; | 167 }; |
| 166 | 168 |
| 167 } // namespace webrtc | 169 } // namespace webrtc |
| 168 #endif // WEBRTC_MODULES_AUDIO_CODING_NETEQ_DECISION_LOGIC_H_ | 170 #endif // WEBRTC_MODULES_AUDIO_CODING_NETEQ_DECISION_LOGIC_H_ |
| OLD | NEW |