henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2012 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 | |
| 11 | #include "webrtc/modules/audio_coding/neteq4/neteq_impl.h" |
| 12 | |
| 13 | #include <assert.h> |
| 14 | #include <memory.h> // memset |
| 15 | |
| 16 | #include <algorithm> |
| 17 | |
| 18 | #include "webrtc/common_audio/signal_processing/include/signal_processing_library.h" |
| 19 | #include "webrtc/modules/audio_coding/neteq4/accelerate.h" |
| 20 | #include "webrtc/modules/audio_coding/neteq4/background_noise.h" |
| 21 | #include "webrtc/modules/audio_coding/neteq4/buffer_level_filter.h" |
| 22 | #include "webrtc/modules/audio_coding/neteq4/comfort_noise.h" |
| 23 | #include "webrtc/modules/audio_coding/neteq4/decision_logic.h" |
| 24 | #include "webrtc/modules/audio_coding/neteq4/decoder_database.h" |
| 25 | #include "webrtc/modules/audio_coding/neteq4/defines.h" |
| 26 | #include "webrtc/modules/audio_coding/neteq4/delay_manager.h" |
| 27 | #include "webrtc/modules/audio_coding/neteq4/delay_peak_detector.h" |
| 28 | #include "webrtc/modules/audio_coding/neteq4/dtmf_buffer.h" |
| 29 | #include "webrtc/modules/audio_coding/neteq4/dtmf_tone_generator.h" |
| 30 | #include "webrtc/modules/audio_coding/neteq4/expand.h" |
| 31 | #include "webrtc/modules/audio_coding/neteq4/interface/audio_decoder.h" |
| 32 | #include "webrtc/modules/audio_coding/neteq4/merge.h" |
| 33 | #include "webrtc/modules/audio_coding/neteq4/normal.h" |
| 34 | #include "webrtc/modules/audio_coding/neteq4/packet_buffer.h" |
| 35 | #include "webrtc/modules/audio_coding/neteq4/packet.h" |
| 36 | #include "webrtc/modules/audio_coding/neteq4/payload_splitter.h" |
| 37 | #include "webrtc/modules/audio_coding/neteq4/post_decode_vad.h" |
| 38 | #include "webrtc/modules/audio_coding/neteq4/preemptive_expand.h" |
| 39 | #include "webrtc/modules/audio_coding/neteq4/sync_buffer.h" |
| 40 | #include "webrtc/modules/audio_coding/neteq4/timestamp_scaler.h" |
| 41 | #include "webrtc/modules/interface/module_common_types.h" |
| 42 | #include "webrtc/system_wrappers/interface/critical_section_wrapper.h" |
| 43 | #include "webrtc/system_wrappers/interface/logging.h" |
| 44 | |
| 45 | // Modify the code to obtain backwards bit-exactness. Once bit-exactness is no |
| 46 | // longer required, this #define should be removed (and the code that it |
| 47 | // enables). |
| 48 | #define LEGACY_BITEXACT |
| 49 | |
| 50 | namespace webrtc { |
| 51 | |
| 52 | NetEqImpl::NetEqImpl(int fs, |
| 53 | BufferLevelFilter* buffer_level_filter, |
| 54 | DecoderDatabase* decoder_database, |
| 55 | DelayManager* delay_manager, |
| 56 | DelayPeakDetector* delay_peak_detector, |
| 57 | DtmfBuffer* dtmf_buffer, |
| 58 | DtmfToneGenerator* dtmf_tone_generator, |
| 59 | PacketBuffer* packet_buffer, |
| 60 | PayloadSplitter* payload_splitter, |
| 61 | TimestampScaler* timestamp_scaler) |
| 62 | : background_noise_(NULL), |
| 63 | buffer_level_filter_(buffer_level_filter), |
| 64 | decoder_database_(decoder_database), |
| 65 | delay_manager_(delay_manager), |
| 66 | delay_peak_detector_(delay_peak_detector), |
| 67 | dtmf_buffer_(dtmf_buffer), |
| 68 | dtmf_tone_generator_(dtmf_tone_generator), |
| 69 | packet_buffer_(packet_buffer), |
| 70 | payload_splitter_(payload_splitter), |
| 71 | timestamp_scaler_(timestamp_scaler), |
| 72 | vad_(new PostDecodeVad()), |
henrik.lundin@webrtc.org | c487c6a | 2013-09-02 07:59:30 +0000 | [diff] [blame] | 73 | algorithm_buffer_(NULL), |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 74 | sync_buffer_(NULL), |
| 75 | expand_(NULL), |
henrik.lundin@webrtc.org | 40d3fc6 | 2013-09-18 12:19:50 +0000 | [diff] [blame] | 76 | normal_(NULL), |
| 77 | merge_(NULL), |
| 78 | accelerate_(NULL), |
| 79 | preemptive_expand_(NULL), |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 80 | comfort_noise_(NULL), |
| 81 | last_mode_(kModeNormal), |
| 82 | mute_factor_array_(NULL), |
| 83 | decoded_buffer_length_(kMaxFrameSize), |
| 84 | decoded_buffer_(new int16_t[decoded_buffer_length_]), |
| 85 | playout_timestamp_(0), |
| 86 | new_codec_(false), |
| 87 | timestamp_(0), |
| 88 | reset_decoder_(false), |
| 89 | current_rtp_payload_type_(0xFF), // Invalid RTP payload type. |
| 90 | current_cng_rtp_payload_type_(0xFF), // Invalid RTP payload type. |
| 91 | ssrc_(0), |
| 92 | first_packet_(true), |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 93 | error_code_(0), |
| 94 | decoder_error_code_(0), |
minyue@webrtc.org | d730177 | 2013-08-29 00:58:14 +0000 | [diff] [blame] | 95 | crit_sect_(CriticalSectionWrapper::CreateCriticalSection()), |
| 96 | decoded_packet_sequence_number_(-1), |
| 97 | decoded_packet_timestamp_(0) { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 98 | if (fs != 8000 && fs != 16000 && fs != 32000 && fs != 48000) { |
| 99 | LOG(LS_ERROR) << "Sample rate " << fs << " Hz not supported. " << |
| 100 | "Changing to 8000 Hz."; |
| 101 | fs = 8000; |
| 102 | } |
| 103 | LOG(LS_INFO) << "Create NetEqImpl object with fs = " << fs << "."; |
| 104 | fs_hz_ = fs; |
| 105 | fs_mult_ = fs / 8000; |
| 106 | output_size_samples_ = kOutputSizeMs * 8 * fs_mult_; |
| 107 | decoder_frame_length_ = 3 * output_size_samples_; |
| 108 | WebRtcSpl_Init(); |
| 109 | decision_logic_.reset(DecisionLogic::Create(fs_hz_, output_size_samples_, |
| 110 | kPlayoutOn, |
| 111 | decoder_database_.get(), |
| 112 | *packet_buffer_.get(), |
| 113 | delay_manager_.get(), |
| 114 | buffer_level_filter_.get())); |
| 115 | SetSampleRateAndChannels(fs, 1); // Default is 1 channel. |
| 116 | } |
| 117 | |
| 118 | NetEqImpl::~NetEqImpl() { |
| 119 | LOG(LS_INFO) << "Deleting NetEqImpl object."; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | int NetEqImpl::InsertPacket(const WebRtcRTPHeader& rtp_header, |
| 123 | const uint8_t* payload, |
| 124 | int length_bytes, |
| 125 | uint32_t receive_timestamp) { |
henrik.lundin@webrtc.org | 0d5da25 | 2013-09-18 21:12:38 +0000 | [diff] [blame] | 126 | CriticalSectionScoped lock(crit_sect_.get()); |
henrik.lundin@webrtc.org | b3e905c | 2013-09-02 09:41:06 +0000 | [diff] [blame] | 127 | NETEQ_LOG_VERBOSE << "InsertPacket: ts=" << rtp_header.header.timestamp << |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 128 | ", sn=" << rtp_header.header.sequenceNumber << |
| 129 | ", pt=" << static_cast<int>(rtp_header.header.payloadType) << |
| 130 | ", ssrc=" << rtp_header.header.ssrc << |
| 131 | ", len=" << length_bytes; |
| 132 | int error = InsertPacketInternal(rtp_header, payload, length_bytes, |
| 133 | receive_timestamp); |
| 134 | if (error != 0) { |
| 135 | LOG_FERR1(LS_WARNING, InsertPacketInternal, error); |
| 136 | error_code_ = error; |
| 137 | return kFail; |
| 138 | } |
| 139 | return kOK; |
| 140 | } |
| 141 | |
| 142 | int NetEqImpl::GetAudio(size_t max_length, int16_t* output_audio, |
| 143 | int* samples_per_channel, int* num_channels, |
| 144 | NetEqOutputType* type) { |
henrik.lundin@webrtc.org | 0d5da25 | 2013-09-18 21:12:38 +0000 | [diff] [blame] | 145 | CriticalSectionScoped lock(crit_sect_.get()); |
henrik.lundin@webrtc.org | b3e905c | 2013-09-02 09:41:06 +0000 | [diff] [blame] | 146 | NETEQ_LOG_VERBOSE << "GetAudio"; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 147 | int error = GetAudioInternal(max_length, output_audio, samples_per_channel, |
| 148 | num_channels); |
henrik.lundin@webrtc.org | b3e905c | 2013-09-02 09:41:06 +0000 | [diff] [blame] | 149 | NETEQ_LOG_VERBOSE << "Produced " << *samples_per_channel << |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 150 | " samples/channel for " << *num_channels << " channel(s)"; |
| 151 | if (error != 0) { |
| 152 | LOG_FERR1(LS_WARNING, GetAudioInternal, error); |
| 153 | error_code_ = error; |
| 154 | return kFail; |
| 155 | } |
| 156 | if (type) { |
| 157 | *type = LastOutputType(); |
| 158 | } |
| 159 | return kOK; |
| 160 | } |
| 161 | |
| 162 | int NetEqImpl::RegisterPayloadType(enum NetEqDecoder codec, |
| 163 | uint8_t rtp_payload_type) { |
henrik.lundin@webrtc.org | 0d5da25 | 2013-09-18 21:12:38 +0000 | [diff] [blame] | 164 | CriticalSectionScoped lock(crit_sect_.get()); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 165 | LOG_API2(static_cast<int>(rtp_payload_type), codec); |
| 166 | int ret = decoder_database_->RegisterPayload(rtp_payload_type, codec); |
| 167 | if (ret != DecoderDatabase::kOK) { |
| 168 | LOG_FERR2(LS_WARNING, RegisterPayload, rtp_payload_type, codec); |
| 169 | switch (ret) { |
| 170 | case DecoderDatabase::kInvalidRtpPayloadType: |
| 171 | error_code_ = kInvalidRtpPayloadType; |
| 172 | break; |
| 173 | case DecoderDatabase::kCodecNotSupported: |
| 174 | error_code_ = kCodecNotSupported; |
| 175 | break; |
| 176 | case DecoderDatabase::kDecoderExists: |
| 177 | error_code_ = kDecoderExists; |
| 178 | break; |
| 179 | default: |
| 180 | error_code_ = kOtherError; |
| 181 | } |
| 182 | return kFail; |
| 183 | } |
| 184 | return kOK; |
| 185 | } |
| 186 | |
| 187 | int NetEqImpl::RegisterExternalDecoder(AudioDecoder* decoder, |
| 188 | enum NetEqDecoder codec, |
| 189 | int sample_rate_hz, |
| 190 | uint8_t rtp_payload_type) { |
henrik.lundin@webrtc.org | 0d5da25 | 2013-09-18 21:12:38 +0000 | [diff] [blame] | 191 | CriticalSectionScoped lock(crit_sect_.get()); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 192 | LOG_API2(static_cast<int>(rtp_payload_type), codec); |
| 193 | if (!decoder) { |
| 194 | LOG(LS_ERROR) << "Cannot register external decoder with NULL pointer"; |
| 195 | assert(false); |
| 196 | return kFail; |
| 197 | } |
| 198 | int ret = decoder_database_->InsertExternal(rtp_payload_type, codec, |
| 199 | sample_rate_hz, decoder); |
| 200 | if (ret != DecoderDatabase::kOK) { |
| 201 | LOG_FERR2(LS_WARNING, InsertExternal, rtp_payload_type, codec); |
| 202 | switch (ret) { |
| 203 | case DecoderDatabase::kInvalidRtpPayloadType: |
| 204 | error_code_ = kInvalidRtpPayloadType; |
| 205 | break; |
| 206 | case DecoderDatabase::kCodecNotSupported: |
| 207 | error_code_ = kCodecNotSupported; |
| 208 | break; |
| 209 | case DecoderDatabase::kDecoderExists: |
| 210 | error_code_ = kDecoderExists; |
| 211 | break; |
| 212 | case DecoderDatabase::kInvalidSampleRate: |
| 213 | error_code_ = kInvalidSampleRate; |
| 214 | break; |
| 215 | case DecoderDatabase::kInvalidPointer: |
| 216 | error_code_ = kInvalidPointer; |
| 217 | break; |
| 218 | default: |
| 219 | error_code_ = kOtherError; |
| 220 | } |
| 221 | return kFail; |
| 222 | } |
| 223 | return kOK; |
| 224 | } |
| 225 | |
| 226 | int NetEqImpl::RemovePayloadType(uint8_t rtp_payload_type) { |
henrik.lundin@webrtc.org | 0d5da25 | 2013-09-18 21:12:38 +0000 | [diff] [blame] | 227 | CriticalSectionScoped lock(crit_sect_.get()); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 228 | LOG_API1(static_cast<int>(rtp_payload_type)); |
| 229 | int ret = decoder_database_->Remove(rtp_payload_type); |
| 230 | if (ret == DecoderDatabase::kOK) { |
| 231 | return kOK; |
| 232 | } else if (ret == DecoderDatabase::kDecoderNotFound) { |
| 233 | error_code_ = kDecoderNotFound; |
| 234 | } else { |
| 235 | error_code_ = kOtherError; |
| 236 | } |
| 237 | LOG_FERR1(LS_WARNING, Remove, rtp_payload_type); |
| 238 | return kFail; |
| 239 | } |
| 240 | |
turaj@webrtc.org | f1efc57 | 2013-08-16 23:44:24 +0000 | [diff] [blame] | 241 | bool NetEqImpl::SetMinimumDelay(int delay_ms) { |
henrik.lundin@webrtc.org | 0d5da25 | 2013-09-18 21:12:38 +0000 | [diff] [blame] | 242 | CriticalSectionScoped lock(crit_sect_.get()); |
turaj@webrtc.org | f1efc57 | 2013-08-16 23:44:24 +0000 | [diff] [blame] | 243 | if (delay_ms >= 0 && delay_ms < 10000) { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 244 | assert(delay_manager_.get()); |
turaj@webrtc.org | f1efc57 | 2013-08-16 23:44:24 +0000 | [diff] [blame] | 245 | return delay_manager_->SetMinimumDelay(delay_ms); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 246 | } |
| 247 | return false; |
| 248 | } |
| 249 | |
turaj@webrtc.org | f1efc57 | 2013-08-16 23:44:24 +0000 | [diff] [blame] | 250 | bool NetEqImpl::SetMaximumDelay(int delay_ms) { |
henrik.lundin@webrtc.org | 0d5da25 | 2013-09-18 21:12:38 +0000 | [diff] [blame] | 251 | CriticalSectionScoped lock(crit_sect_.get()); |
turaj@webrtc.org | f1efc57 | 2013-08-16 23:44:24 +0000 | [diff] [blame] | 252 | if (delay_ms >= 0 && delay_ms < 10000) { |
| 253 | assert(delay_manager_.get()); |
| 254 | return delay_manager_->SetMaximumDelay(delay_ms); |
| 255 | } |
| 256 | return false; |
| 257 | } |
| 258 | |
| 259 | int NetEqImpl::LeastRequiredDelayMs() const { |
henrik.lundin@webrtc.org | 0d5da25 | 2013-09-18 21:12:38 +0000 | [diff] [blame] | 260 | CriticalSectionScoped lock(crit_sect_.get()); |
turaj@webrtc.org | f1efc57 | 2013-08-16 23:44:24 +0000 | [diff] [blame] | 261 | assert(delay_manager_.get()); |
| 262 | return delay_manager_->least_required_delay_ms(); |
| 263 | } |
| 264 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 265 | void NetEqImpl::SetPlayoutMode(NetEqPlayoutMode mode) { |
henrik.lundin@webrtc.org | 0d5da25 | 2013-09-18 21:12:38 +0000 | [diff] [blame] | 266 | CriticalSectionScoped lock(crit_sect_.get()); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 267 | if (!decision_logic_.get() || mode != decision_logic_->playout_mode()) { |
| 268 | // The reset() method calls delete for the old object. |
| 269 | decision_logic_.reset(DecisionLogic::Create(fs_hz_, output_size_samples_, |
| 270 | mode, |
| 271 | decoder_database_.get(), |
| 272 | *packet_buffer_.get(), |
| 273 | delay_manager_.get(), |
| 274 | buffer_level_filter_.get())); |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | NetEqPlayoutMode NetEqImpl::PlayoutMode() const { |
henrik.lundin@webrtc.org | 0d5da25 | 2013-09-18 21:12:38 +0000 | [diff] [blame] | 279 | CriticalSectionScoped lock(crit_sect_.get()); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 280 | assert(decision_logic_.get()); |
| 281 | return decision_logic_->playout_mode(); |
| 282 | } |
| 283 | |
| 284 | int NetEqImpl::NetworkStatistics(NetEqNetworkStatistics* stats) { |
henrik.lundin@webrtc.org | 0d5da25 | 2013-09-18 21:12:38 +0000 | [diff] [blame] | 285 | CriticalSectionScoped lock(crit_sect_.get()); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 286 | assert(decoder_database_.get()); |
| 287 | const int total_samples_in_buffers = packet_buffer_->NumSamplesInBuffer( |
| 288 | decoder_database_.get(), decoder_frame_length_) + |
turaj@webrtc.org | 362a55e | 2013-09-20 16:25:28 +0000 | [diff] [blame] | 289 | static_cast<int>(sync_buffer_->FutureLength()); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 290 | assert(delay_manager_.get()); |
| 291 | assert(decision_logic_.get()); |
| 292 | stats_.GetNetworkStatistics(fs_hz_, total_samples_in_buffers, |
| 293 | decoder_frame_length_, *delay_manager_.get(), |
| 294 | *decision_logic_.get(), stats); |
| 295 | return 0; |
| 296 | } |
| 297 | |
| 298 | void NetEqImpl::WaitingTimes(std::vector<int>* waiting_times) { |
henrik.lundin@webrtc.org | 0d5da25 | 2013-09-18 21:12:38 +0000 | [diff] [blame] | 299 | CriticalSectionScoped lock(crit_sect_.get()); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 300 | stats_.WaitingTimes(waiting_times); |
| 301 | } |
| 302 | |
| 303 | void NetEqImpl::GetRtcpStatistics(RtcpStatistics* stats) { |
henrik.lundin@webrtc.org | 0d5da25 | 2013-09-18 21:12:38 +0000 | [diff] [blame] | 304 | CriticalSectionScoped lock(crit_sect_.get()); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 305 | if (stats) { |
| 306 | rtcp_.GetStatistics(false, stats); |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | void NetEqImpl::GetRtcpStatisticsNoReset(RtcpStatistics* stats) { |
henrik.lundin@webrtc.org | 0d5da25 | 2013-09-18 21:12:38 +0000 | [diff] [blame] | 311 | CriticalSectionScoped lock(crit_sect_.get()); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 312 | if (stats) { |
| 313 | rtcp_.GetStatistics(true, stats); |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | void NetEqImpl::EnableVad() { |
henrik.lundin@webrtc.org | 0d5da25 | 2013-09-18 21:12:38 +0000 | [diff] [blame] | 318 | CriticalSectionScoped lock(crit_sect_.get()); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 319 | assert(vad_.get()); |
| 320 | vad_->Enable(); |
| 321 | } |
| 322 | |
| 323 | void NetEqImpl::DisableVad() { |
henrik.lundin@webrtc.org | 0d5da25 | 2013-09-18 21:12:38 +0000 | [diff] [blame] | 324 | CriticalSectionScoped lock(crit_sect_.get()); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 325 | assert(vad_.get()); |
| 326 | vad_->Disable(); |
| 327 | } |
| 328 | |
| 329 | uint32_t NetEqImpl::PlayoutTimestamp() { |
henrik.lundin@webrtc.org | 0d5da25 | 2013-09-18 21:12:38 +0000 | [diff] [blame] | 330 | CriticalSectionScoped lock(crit_sect_.get()); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 331 | return timestamp_scaler_->ToExternal(playout_timestamp_); |
| 332 | } |
| 333 | |
| 334 | int NetEqImpl::LastError() { |
henrik.lundin@webrtc.org | 0d5da25 | 2013-09-18 21:12:38 +0000 | [diff] [blame] | 335 | CriticalSectionScoped lock(crit_sect_.get()); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 336 | return error_code_; |
| 337 | } |
| 338 | |
| 339 | int NetEqImpl::LastDecoderError() { |
henrik.lundin@webrtc.org | 0d5da25 | 2013-09-18 21:12:38 +0000 | [diff] [blame] | 340 | CriticalSectionScoped lock(crit_sect_.get()); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 341 | return decoder_error_code_; |
| 342 | } |
| 343 | |
| 344 | void NetEqImpl::FlushBuffers() { |
henrik.lundin@webrtc.org | 0d5da25 | 2013-09-18 21:12:38 +0000 | [diff] [blame] | 345 | CriticalSectionScoped lock(crit_sect_.get()); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 346 | LOG_API0(); |
| 347 | packet_buffer_->Flush(); |
henrik.lundin@webrtc.org | 0d5da25 | 2013-09-18 21:12:38 +0000 | [diff] [blame] | 348 | assert(sync_buffer_.get()); |
| 349 | assert(expand_.get()); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 350 | sync_buffer_->Flush(); |
| 351 | sync_buffer_->set_next_index(sync_buffer_->next_index() - |
| 352 | expand_->overlap_length()); |
| 353 | // Set to wait for new codec. |
| 354 | first_packet_ = true; |
| 355 | } |
| 356 | |
turaj@webrtc.org | 3170b57 | 2013-08-30 15:36:53 +0000 | [diff] [blame] | 357 | void NetEqImpl::PacketBufferStatistics(int* current_num_packets, |
| 358 | int* max_num_packets, |
| 359 | int* current_memory_size_bytes, |
| 360 | int* max_memory_size_bytes) const { |
henrik.lundin@webrtc.org | 0d5da25 | 2013-09-18 21:12:38 +0000 | [diff] [blame] | 361 | CriticalSectionScoped lock(crit_sect_.get()); |
turaj@webrtc.org | 3170b57 | 2013-08-30 15:36:53 +0000 | [diff] [blame] | 362 | packet_buffer_->BufferStat(current_num_packets, max_num_packets, |
| 363 | current_memory_size_bytes, max_memory_size_bytes); |
| 364 | } |
| 365 | |
turaj@webrtc.org | ff43c85 | 2013-09-25 00:07:27 +0000 | [diff] [blame] | 366 | int NetEqImpl::DecodedRtpInfo(int* sequence_number, uint32_t* timestamp) const { |
henrik.lundin@webrtc.org | 0d5da25 | 2013-09-18 21:12:38 +0000 | [diff] [blame] | 367 | CriticalSectionScoped lock(crit_sect_.get()); |
minyue@webrtc.org | d730177 | 2013-08-29 00:58:14 +0000 | [diff] [blame] | 368 | if (decoded_packet_sequence_number_ < 0) |
| 369 | return -1; |
| 370 | *sequence_number = decoded_packet_sequence_number_; |
| 371 | *timestamp = decoded_packet_timestamp_; |
| 372 | return 0; |
| 373 | } |
| 374 | |
turaj@webrtc.org | 036b743 | 2013-09-11 18:45:02 +0000 | [diff] [blame] | 375 | int NetEqImpl::InsertSyncPacket(const WebRtcRTPHeader& /* rtp_header */, |
| 376 | uint32_t /* receive_timestamp */) { |
| 377 | return kNotImplemented; |
| 378 | } |
| 379 | |
turaj@webrtc.org | ff43c85 | 2013-09-25 00:07:27 +0000 | [diff] [blame] | 380 | void NetEqImpl::SetBackgroundNoiseMode(NetEqBackgroundNoiseMode mode) { |
| 381 | CriticalSectionScoped lock(crit_sect_.get()); |
| 382 | assert(background_noise_.get()); |
| 383 | background_noise_->set_mode(mode); |
| 384 | } |
turaj@webrtc.org | 036b743 | 2013-09-11 18:45:02 +0000 | [diff] [blame] | 385 | |
| 386 | NetEqBackgroundNoiseMode NetEqImpl::BackgroundNoiseMode() const { |
turaj@webrtc.org | ff43c85 | 2013-09-25 00:07:27 +0000 | [diff] [blame] | 387 | CriticalSectionScoped lock(crit_sect_.get()); |
| 388 | assert(background_noise_.get()); |
| 389 | return background_noise_->mode(); |
turaj@webrtc.org | 036b743 | 2013-09-11 18:45:02 +0000 | [diff] [blame] | 390 | } |
| 391 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 392 | // Methods below this line are private. |
| 393 | |
| 394 | |
| 395 | int NetEqImpl::InsertPacketInternal(const WebRtcRTPHeader& rtp_header, |
| 396 | const uint8_t* payload, |
| 397 | int length_bytes, |
| 398 | uint32_t receive_timestamp) { |
| 399 | if (!payload) { |
| 400 | LOG_F(LS_ERROR) << "payload == NULL"; |
| 401 | return kInvalidPointer; |
| 402 | } |
| 403 | PacketList packet_list; |
| 404 | RTPHeader main_header; |
| 405 | { |
henrik.lundin@webrtc.org | e1d468c | 2013-01-30 07:37:20 +0000 | [diff] [blame] | 406 | // Convert to Packet. |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 407 | // Create |packet| within this separate scope, since it should not be used |
| 408 | // directly once it's been inserted in the packet list. This way, |packet| |
| 409 | // is not defined outside of this block. |
henrik.lundin@webrtc.org | e1d468c | 2013-01-30 07:37:20 +0000 | [diff] [blame] | 410 | Packet* packet = new Packet; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 411 | packet->header.markerBit = false; |
| 412 | packet->header.payloadType = rtp_header.header.payloadType; |
| 413 | packet->header.sequenceNumber = rtp_header.header.sequenceNumber; |
| 414 | packet->header.timestamp = rtp_header.header.timestamp; |
| 415 | packet->header.ssrc = rtp_header.header.ssrc; |
| 416 | packet->header.numCSRCs = 0; |
| 417 | packet->payload_length = length_bytes; |
| 418 | packet->primary = true; |
| 419 | packet->waiting_time = 0; |
| 420 | packet->payload = new uint8_t[packet->payload_length]; |
henrik.lundin@webrtc.org | 73deaad | 2013-01-31 13:32:51 +0000 | [diff] [blame] | 421 | if (!packet->payload) { |
| 422 | LOG_F(LS_ERROR) << "Payload pointer is NULL."; |
| 423 | } |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 424 | assert(payload); // Already checked above. |
| 425 | memcpy(packet->payload, payload, packet->payload_length); |
| 426 | // Insert packet in a packet list. |
| 427 | packet_list.push_back(packet); |
| 428 | // Save main payloads header for later. |
| 429 | memcpy(&main_header, &packet->header, sizeof(main_header)); |
| 430 | } |
| 431 | |
| 432 | // Reinitialize NetEq if it's needed (changed SSRC or first call). |
| 433 | if ((main_header.ssrc != ssrc_) || first_packet_) { |
| 434 | rtcp_.Init(main_header.sequenceNumber); |
| 435 | first_packet_ = false; |
| 436 | |
| 437 | // Flush the packet buffer and DTMF buffer. |
| 438 | packet_buffer_->Flush(); |
| 439 | dtmf_buffer_->Flush(); |
| 440 | |
| 441 | // Store new SSRC. |
| 442 | ssrc_ = main_header.ssrc; |
| 443 | |
turaj@webrtc.org | 4d06db5 | 2013-03-27 18:31:42 +0000 | [diff] [blame] | 444 | // Update audio buffer timestamp. |
| 445 | sync_buffer_->IncreaseEndTimestamp(main_header.timestamp - timestamp_); |
| 446 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 447 | // Update codecs. |
| 448 | timestamp_ = main_header.timestamp; |
| 449 | current_rtp_payload_type_ = main_header.payloadType; |
| 450 | |
| 451 | // Set MCU to update codec on next SignalMCU call. |
| 452 | new_codec_ = true; |
| 453 | |
| 454 | // Reset timestamp scaling. |
| 455 | timestamp_scaler_->Reset(); |
| 456 | } |
| 457 | |
| 458 | // Update RTCP statistics. |
| 459 | rtcp_.Update(main_header, receive_timestamp); |
| 460 | |
| 461 | // Check for RED payload type, and separate payloads into several packets. |
| 462 | if (decoder_database_->IsRed(main_header.payloadType)) { |
| 463 | if (payload_splitter_->SplitRed(&packet_list) != PayloadSplitter::kOK) { |
| 464 | LOG_FERR1(LS_WARNING, SplitRed, packet_list.size()); |
| 465 | PacketBuffer::DeleteAllPackets(&packet_list); |
| 466 | return kRedundancySplitError; |
| 467 | } |
| 468 | // Only accept a few RED payloads of the same type as the main data, |
| 469 | // DTMF events and CNG. |
| 470 | payload_splitter_->CheckRedPayloads(&packet_list, *decoder_database_); |
| 471 | // Update the stored main payload header since the main payload has now |
| 472 | // changed. |
| 473 | memcpy(&main_header, &packet_list.front()->header, sizeof(main_header)); |
| 474 | } |
| 475 | |
| 476 | // Check payload types. |
| 477 | if (decoder_database_->CheckPayloadTypes(packet_list) == |
| 478 | DecoderDatabase::kDecoderNotFound) { |
| 479 | LOG_FERR1(LS_WARNING, CheckPayloadTypes, packet_list.size()); |
| 480 | PacketBuffer::DeleteAllPackets(&packet_list); |
| 481 | return kUnknownRtpPayloadType; |
| 482 | } |
| 483 | |
| 484 | // Scale timestamp to internal domain (only for some codecs). |
| 485 | timestamp_scaler_->ToInternal(&packet_list); |
| 486 | |
| 487 | // Process DTMF payloads. Cycle through the list of packets, and pick out any |
| 488 | // DTMF payloads found. |
| 489 | PacketList::iterator it = packet_list.begin(); |
| 490 | while (it != packet_list.end()) { |
| 491 | Packet* current_packet = (*it); |
| 492 | assert(current_packet); |
| 493 | assert(current_packet->payload); |
| 494 | if (decoder_database_->IsDtmf(current_packet->header.payloadType)) { |
minyue@webrtc.org | 9721db7 | 2013-08-06 05:36:26 +0000 | [diff] [blame] | 495 | DtmfEvent event; |
| 496 | int ret = DtmfBuffer::ParseEvent( |
| 497 | current_packet->header.timestamp, |
| 498 | current_packet->payload, |
| 499 | current_packet->payload_length, |
| 500 | &event); |
| 501 | if (ret != DtmfBuffer::kOK) { |
| 502 | LOG_FERR2(LS_WARNING, ParseEvent, ret, |
| 503 | current_packet->payload_length); |
| 504 | PacketBuffer::DeleteAllPackets(&packet_list); |
| 505 | return kDtmfParsingError; |
| 506 | } |
| 507 | if (dtmf_buffer_->InsertEvent(event) != DtmfBuffer::kOK) { |
| 508 | LOG_FERR0(LS_WARNING, InsertEvent); |
| 509 | PacketBuffer::DeleteAllPackets(&packet_list); |
| 510 | return kDtmfInsertError; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 511 | } |
| 512 | // TODO(hlundin): Let the destructor of Packet handle the payload. |
| 513 | delete [] current_packet->payload; |
| 514 | delete current_packet; |
| 515 | it = packet_list.erase(it); |
| 516 | } else { |
| 517 | ++it; |
| 518 | } |
| 519 | } |
| 520 | |
| 521 | // Split payloads into smaller chunks. This also verifies that all payloads |
| 522 | // are of a known payload type. |
| 523 | int ret = payload_splitter_->SplitAudio(&packet_list, *decoder_database_); |
| 524 | if (ret != PayloadSplitter::kOK) { |
| 525 | LOG_FERR1(LS_WARNING, SplitAudio, packet_list.size()); |
| 526 | PacketBuffer::DeleteAllPackets(&packet_list); |
| 527 | switch (ret) { |
| 528 | case PayloadSplitter::kUnknownPayloadType: |
| 529 | return kUnknownRtpPayloadType; |
| 530 | case PayloadSplitter::kFrameSplitError: |
| 531 | return kFrameSplitError; |
| 532 | default: |
| 533 | return kOtherError; |
| 534 | } |
| 535 | } |
| 536 | |
| 537 | // Update bandwidth estimate. |
| 538 | if (!packet_list.empty()) { |
| 539 | // The list can be empty here if we got nothing but DTMF payloads. |
| 540 | AudioDecoder* decoder = |
| 541 | decoder_database_->GetDecoder(main_header.payloadType); |
| 542 | assert(decoder); // Should always get a valid object, since we have |
| 543 | // already checked that the payload types are known. |
| 544 | decoder->IncomingPacket(packet_list.front()->payload, |
| 545 | packet_list.front()->payload_length, |
| 546 | packet_list.front()->header.sequenceNumber, |
| 547 | packet_list.front()->header.timestamp, |
| 548 | receive_timestamp); |
| 549 | } |
| 550 | |
| 551 | // Insert packets in buffer. |
| 552 | int temp_bufsize = packet_buffer_->NumPacketsInBuffer(); |
| 553 | ret = packet_buffer_->InsertPacketList( |
| 554 | &packet_list, |
| 555 | *decoder_database_, |
| 556 | ¤t_rtp_payload_type_, |
| 557 | ¤t_cng_rtp_payload_type_); |
| 558 | if (ret == PacketBuffer::kFlushed) { |
| 559 | // Reset DSP timestamp etc. if packet buffer flushed. |
| 560 | new_codec_ = true; |
| 561 | LOG_F(LS_WARNING) << "Packet buffer flushed"; |
minyue@webrtc.org | 7bb5436 | 2013-08-06 05:40:57 +0000 | [diff] [blame] | 562 | } else if (ret == PacketBuffer::kOversizePacket) { |
| 563 | LOG_F(LS_WARNING) << "Packet larger than packet buffer"; |
| 564 | return kOversizePacket; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 565 | } else if (ret != PacketBuffer::kOK) { |
| 566 | LOG_FERR1(LS_WARNING, InsertPacketList, packet_list.size()); |
| 567 | PacketBuffer::DeleteAllPackets(&packet_list); |
minyue@webrtc.org | 7bb5436 | 2013-08-06 05:40:57 +0000 | [diff] [blame] | 568 | return kOtherError; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 569 | } |
| 570 | if (current_rtp_payload_type_ != 0xFF) { |
| 571 | const DecoderDatabase::DecoderInfo* dec_info = |
| 572 | decoder_database_->GetDecoderInfo(current_rtp_payload_type_); |
| 573 | if (!dec_info) { |
| 574 | assert(false); // Already checked that the payload type is known. |
| 575 | } |
| 576 | } |
| 577 | |
| 578 | // TODO(hlundin): Move this code to DelayManager class. |
| 579 | const DecoderDatabase::DecoderInfo* dec_info = |
| 580 | decoder_database_->GetDecoderInfo(main_header.payloadType); |
| 581 | assert(dec_info); // Already checked that the payload type is known. |
| 582 | delay_manager_->LastDecoderType(dec_info->codec_type); |
| 583 | if (delay_manager_->last_pack_cng_or_dtmf() == 0) { |
| 584 | // Calculate the total speech length carried in each packet. |
| 585 | temp_bufsize = packet_buffer_->NumPacketsInBuffer() - temp_bufsize; |
| 586 | temp_bufsize *= decoder_frame_length_; |
| 587 | |
| 588 | if ((temp_bufsize > 0) && |
| 589 | (temp_bufsize != decision_logic_->packet_length_samples())) { |
| 590 | decision_logic_->set_packet_length_samples(temp_bufsize); |
| 591 | delay_manager_->SetPacketAudioLength((1000 * temp_bufsize) / fs_hz_); |
| 592 | } |
| 593 | |
| 594 | // Update statistics. |
pbos@webrtc.org | 0946a56 | 2013-04-09 00:28:06 +0000 | [diff] [blame] | 595 | if ((int32_t) (main_header.timestamp - timestamp_) >= 0 && |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 596 | !new_codec_) { |
| 597 | // Only update statistics if incoming packet is not older than last played |
| 598 | // out packet, and if new codec flag is not set. |
| 599 | delay_manager_->Update(main_header.sequenceNumber, main_header.timestamp, |
| 600 | fs_hz_); |
| 601 | } |
| 602 | } else if (delay_manager_->last_pack_cng_or_dtmf() == -1) { |
| 603 | // This is first "normal" packet after CNG or DTMF. |
| 604 | // Reset packet time counter and measure time until next packet, |
| 605 | // but don't update statistics. |
| 606 | delay_manager_->set_last_pack_cng_or_dtmf(0); |
| 607 | delay_manager_->ResetPacketIatCount(); |
| 608 | } |
| 609 | return 0; |
| 610 | } |
| 611 | |
| 612 | int NetEqImpl::GetAudioInternal(size_t max_length, int16_t* output, |
| 613 | int* samples_per_channel, int* num_channels) { |
| 614 | PacketList packet_list; |
| 615 | DtmfEvent dtmf_event; |
| 616 | Operations operation; |
| 617 | bool play_dtmf; |
| 618 | int return_value = GetDecision(&operation, &packet_list, &dtmf_event, |
| 619 | &play_dtmf); |
| 620 | if (return_value != 0) { |
| 621 | LOG_FERR1(LS_WARNING, GetDecision, return_value); |
| 622 | assert(false); |
| 623 | last_mode_ = kModeError; |
| 624 | return return_value; |
| 625 | } |
henrik.lundin@webrtc.org | b3e905c | 2013-09-02 09:41:06 +0000 | [diff] [blame] | 626 | NETEQ_LOG_VERBOSE << "GetDecision returned operation=" << operation << |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 627 | " and " << packet_list.size() << " packet(s)"; |
| 628 | |
| 629 | AudioDecoder::SpeechType speech_type; |
| 630 | int length = 0; |
| 631 | int decode_return_value = Decode(&packet_list, &operation, |
| 632 | &length, &speech_type); |
| 633 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 634 | assert(vad_.get()); |
| 635 | bool sid_frame_available = |
| 636 | (operation == kRfc3389Cng && !packet_list.empty()); |
| 637 | vad_->Update(decoded_buffer_.get(), length, speech_type, |
| 638 | sid_frame_available, fs_hz_); |
| 639 | |
henrik.lundin@webrtc.org | c487c6a | 2013-09-02 07:59:30 +0000 | [diff] [blame] | 640 | algorithm_buffer_->Clear(); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 641 | switch (operation) { |
| 642 | case kNormal: { |
henrik.lundin@webrtc.org | c487c6a | 2013-09-02 07:59:30 +0000 | [diff] [blame] | 643 | DoNormal(decoded_buffer_.get(), length, speech_type, play_dtmf); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 644 | break; |
| 645 | } |
| 646 | case kMerge: { |
henrik.lundin@webrtc.org | c487c6a | 2013-09-02 07:59:30 +0000 | [diff] [blame] | 647 | DoMerge(decoded_buffer_.get(), length, speech_type, play_dtmf); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 648 | break; |
| 649 | } |
| 650 | case kExpand: { |
henrik.lundin@webrtc.org | c487c6a | 2013-09-02 07:59:30 +0000 | [diff] [blame] | 651 | return_value = DoExpand(play_dtmf); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 652 | break; |
| 653 | } |
| 654 | case kAccelerate: { |
| 655 | return_value = DoAccelerate(decoded_buffer_.get(), length, speech_type, |
henrik.lundin@webrtc.org | c487c6a | 2013-09-02 07:59:30 +0000 | [diff] [blame] | 656 | play_dtmf); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 657 | break; |
| 658 | } |
| 659 | case kPreemptiveExpand: { |
| 660 | return_value = DoPreemptiveExpand(decoded_buffer_.get(), length, |
henrik.lundin@webrtc.org | c487c6a | 2013-09-02 07:59:30 +0000 | [diff] [blame] | 661 | speech_type, play_dtmf); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 662 | break; |
| 663 | } |
| 664 | case kRfc3389Cng: |
| 665 | case kRfc3389CngNoPacket: { |
henrik.lundin@webrtc.org | c487c6a | 2013-09-02 07:59:30 +0000 | [diff] [blame] | 666 | return_value = DoRfc3389Cng(&packet_list, play_dtmf); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 667 | break; |
| 668 | } |
| 669 | case kCodecInternalCng: { |
| 670 | // This handles the case when there is no transmission and the decoder |
| 671 | // should produce internal comfort noise. |
| 672 | // TODO(hlundin): Write test for codec-internal CNG. |
henrik.lundin@webrtc.org | c487c6a | 2013-09-02 07:59:30 +0000 | [diff] [blame] | 673 | DoCodecInternalCng(); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 674 | break; |
| 675 | } |
| 676 | case kDtmf: { |
| 677 | // TODO(hlundin): Write test for this. |
henrik.lundin@webrtc.org | c487c6a | 2013-09-02 07:59:30 +0000 | [diff] [blame] | 678 | return_value = DoDtmf(dtmf_event, &play_dtmf); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 679 | break; |
| 680 | } |
| 681 | case kAlternativePlc: { |
| 682 | // TODO(hlundin): Write test for this. |
henrik.lundin@webrtc.org | c487c6a | 2013-09-02 07:59:30 +0000 | [diff] [blame] | 683 | DoAlternativePlc(false); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 684 | break; |
| 685 | } |
| 686 | case kAlternativePlcIncreaseTimestamp: { |
| 687 | // TODO(hlundin): Write test for this. |
henrik.lundin@webrtc.org | c487c6a | 2013-09-02 07:59:30 +0000 | [diff] [blame] | 688 | DoAlternativePlc(true); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 689 | break; |
| 690 | } |
| 691 | case kAudioRepetitionIncreaseTimestamp: { |
| 692 | // TODO(hlundin): Write test for this. |
| 693 | sync_buffer_->IncreaseEndTimestamp(output_size_samples_); |
| 694 | // Skipping break on purpose. Execution should move on into the |
| 695 | // next case. |
| 696 | } |
| 697 | case kAudioRepetition: { |
| 698 | // TODO(hlundin): Write test for this. |
| 699 | // Copy last |output_size_samples_| from |sync_buffer_| to |
| 700 | // |algorithm_buffer|. |
henrik.lundin@webrtc.org | c487c6a | 2013-09-02 07:59:30 +0000 | [diff] [blame] | 701 | algorithm_buffer_->PushBackFromIndex( |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 702 | *sync_buffer_, sync_buffer_->Size() - output_size_samples_); |
| 703 | expand_->Reset(); |
| 704 | break; |
| 705 | } |
| 706 | case kUndefined: { |
| 707 | LOG_F(LS_ERROR) << "Invalid operation kUndefined."; |
| 708 | assert(false); // This should not happen. |
| 709 | last_mode_ = kModeError; |
| 710 | return kInvalidOperation; |
| 711 | } |
| 712 | } // End of switch. |
| 713 | if (return_value < 0) { |
| 714 | return return_value; |
| 715 | } |
| 716 | |
| 717 | if (last_mode_ != kModeRfc3389Cng) { |
| 718 | comfort_noise_->Reset(); |
| 719 | } |
| 720 | |
| 721 | // Copy from |algorithm_buffer| to |sync_buffer_|. |
henrik.lundin@webrtc.org | c487c6a | 2013-09-02 07:59:30 +0000 | [diff] [blame] | 722 | sync_buffer_->PushBack(*algorithm_buffer_); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 723 | |
| 724 | // Extract data from |sync_buffer_| to |output|. |
turaj@webrtc.org | 362a55e | 2013-09-20 16:25:28 +0000 | [diff] [blame] | 725 | size_t num_output_samples_per_channel = output_size_samples_; |
| 726 | size_t num_output_samples = output_size_samples_ * sync_buffer_->Channels(); |
| 727 | if (num_output_samples > max_length) { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 728 | LOG(LS_WARNING) << "Output array is too short. " << max_length << " < " << |
| 729 | output_size_samples_ << " * " << sync_buffer_->Channels(); |
| 730 | num_output_samples = max_length; |
turaj@webrtc.org | 362a55e | 2013-09-20 16:25:28 +0000 | [diff] [blame] | 731 | num_output_samples_per_channel = static_cast<int>( |
| 732 | max_length / sync_buffer_->Channels()); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 733 | } |
turaj@webrtc.org | 362a55e | 2013-09-20 16:25:28 +0000 | [diff] [blame] | 734 | int samples_from_sync = static_cast<int>( |
| 735 | sync_buffer_->GetNextAudioInterleaved(num_output_samples_per_channel, |
| 736 | output)); |
| 737 | *num_channels = static_cast<int>(sync_buffer_->Channels()); |
henrik.lundin@webrtc.org | b3e905c | 2013-09-02 09:41:06 +0000 | [diff] [blame] | 738 | NETEQ_LOG_VERBOSE << "Sync buffer (" << *num_channels << " channel(s)):" << |
henrik.lundin@webrtc.org | c487c6a | 2013-09-02 07:59:30 +0000 | [diff] [blame] | 739 | " insert " << algorithm_buffer_->Size() << " samples, extract " << |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 740 | samples_from_sync << " samples"; |
| 741 | if (samples_from_sync != output_size_samples_) { |
| 742 | LOG_F(LS_ERROR) << "samples_from_sync != output_size_samples_"; |
minyue@webrtc.org | db1cefc | 2013-08-13 01:39:21 +0000 | [diff] [blame] | 743 | // TODO(minyue): treatment of under-run, filling zeros |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 744 | memset(output, 0, num_output_samples * sizeof(int16_t)); |
| 745 | *samples_per_channel = output_size_samples_; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 746 | return kSampleUnderrun; |
| 747 | } |
| 748 | *samples_per_channel = output_size_samples_; |
| 749 | |
| 750 | // Should always have overlap samples left in the |sync_buffer_|. |
| 751 | assert(sync_buffer_->FutureLength() >= expand_->overlap_length()); |
| 752 | |
| 753 | if (play_dtmf) { |
| 754 | return_value = DtmfOverdub(dtmf_event, sync_buffer_->Channels(), output); |
| 755 | } |
| 756 | |
| 757 | // Update the background noise parameters if last operation wrote data |
| 758 | // straight from the decoder to the |sync_buffer_|. That is, none of the |
| 759 | // operations that modify the signal can be followed by a parameter update. |
| 760 | if ((last_mode_ == kModeNormal) || |
| 761 | (last_mode_ == kModeAccelerateFail) || |
| 762 | (last_mode_ == kModePreemptiveExpandFail) || |
| 763 | (last_mode_ == kModeRfc3389Cng) || |
| 764 | (last_mode_ == kModeCodecInternalCng)) { |
| 765 | background_noise_->Update(*sync_buffer_, *vad_.get()); |
| 766 | } |
| 767 | |
| 768 | if (operation == kDtmf) { |
| 769 | // DTMF data was written the end of |sync_buffer_|. |
| 770 | // Update index to end of DTMF data in |sync_buffer_|. |
| 771 | sync_buffer_->set_dtmf_index(sync_buffer_->Size()); |
| 772 | } |
| 773 | |
| 774 | if ((last_mode_ != kModeExpand) && (last_mode_ != kModeRfc3389Cng)) { |
| 775 | // If last operation was neither expand, nor comfort noise, calculate the |
| 776 | // |playout_timestamp_| from the |sync_buffer_|. However, do not update the |
| 777 | // |playout_timestamp_| if it would be moved "backwards". |
| 778 | uint32_t temp_timestamp = sync_buffer_->end_timestamp() - |
turaj@webrtc.org | 362a55e | 2013-09-20 16:25:28 +0000 | [diff] [blame] | 779 | static_cast<uint32_t>(sync_buffer_->FutureLength()); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 780 | if (static_cast<int32_t>(temp_timestamp - playout_timestamp_) > 0) { |
| 781 | playout_timestamp_ = temp_timestamp; |
| 782 | } |
| 783 | } else { |
| 784 | // Use dead reckoning to estimate the |playout_timestamp_|. |
| 785 | playout_timestamp_ += output_size_samples_; |
| 786 | } |
| 787 | |
| 788 | if (decode_return_value) return decode_return_value; |
| 789 | return return_value; |
| 790 | } |
| 791 | |
| 792 | int NetEqImpl::GetDecision(Operations* operation, |
| 793 | PacketList* packet_list, |
| 794 | DtmfEvent* dtmf_event, |
| 795 | bool* play_dtmf) { |
| 796 | // Initialize output variables. |
| 797 | *play_dtmf = false; |
| 798 | *operation = kUndefined; |
| 799 | |
| 800 | // Increment time counters. |
| 801 | packet_buffer_->IncrementWaitingTimes(); |
| 802 | stats_.IncreaseCounter(output_size_samples_, fs_hz_); |
| 803 | |
henrik.lundin@webrtc.org | 0d5da25 | 2013-09-18 21:12:38 +0000 | [diff] [blame] | 804 | assert(sync_buffer_.get()); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 805 | uint32_t end_timestamp = sync_buffer_->end_timestamp(); |
| 806 | if (!new_codec_) { |
| 807 | packet_buffer_->DiscardOldPackets(end_timestamp); |
| 808 | } |
| 809 | const RTPHeader* header = packet_buffer_->NextRtpHeader(); |
| 810 | |
| 811 | if (decision_logic_->CngRfc3389On()) { |
| 812 | // Because of timestamp peculiarities, we have to "manually" disallow using |
| 813 | // a CNG packet with the same timestamp as the one that was last played. |
| 814 | // This can happen when using redundancy and will cause the timing to shift. |
| 815 | while (header && |
| 816 | decoder_database_->IsComfortNoise(header->payloadType) && |
| 817 | end_timestamp >= header->timestamp) { |
| 818 | // Don't use this packet, discard it. |
| 819 | // TODO(hlundin): Write test for this case. |
| 820 | if (packet_buffer_->DiscardNextPacket() != PacketBuffer::kOK) { |
| 821 | assert(false); // Must be ok by design. |
| 822 | } |
| 823 | // Check buffer again. |
| 824 | if (!new_codec_) { |
| 825 | packet_buffer_->DiscardOldPackets(end_timestamp); |
| 826 | } |
| 827 | header = packet_buffer_->NextRtpHeader(); |
| 828 | } |
| 829 | } |
| 830 | |
henrik.lundin@webrtc.org | 0d5da25 | 2013-09-18 21:12:38 +0000 | [diff] [blame] | 831 | assert(expand_.get()); |
turaj@webrtc.org | 362a55e | 2013-09-20 16:25:28 +0000 | [diff] [blame] | 832 | const int samples_left = static_cast<int>(sync_buffer_->FutureLength() - |
| 833 | expand_->overlap_length()); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 834 | if (last_mode_ == kModeAccelerateSuccess || |
| 835 | last_mode_ == kModeAccelerateLowEnergy || |
| 836 | last_mode_ == kModePreemptiveExpandSuccess || |
| 837 | last_mode_ == kModePreemptiveExpandLowEnergy) { |
| 838 | // Subtract (samples_left + output_size_samples_) from sampleMemory. |
| 839 | decision_logic_->AddSampleMemory(-(samples_left + output_size_samples_)); |
| 840 | } |
| 841 | |
| 842 | // Check if it is time to play a DTMF event. |
| 843 | if (dtmf_buffer_->GetEvent(end_timestamp + |
| 844 | decision_logic_->generated_noise_samples(), |
| 845 | dtmf_event)) { |
| 846 | *play_dtmf = true; |
| 847 | } |
| 848 | |
| 849 | // Get instruction. |
henrik.lundin@webrtc.org | 0d5da25 | 2013-09-18 21:12:38 +0000 | [diff] [blame] | 850 | assert(sync_buffer_.get()); |
| 851 | assert(expand_.get()); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 852 | *operation = decision_logic_->GetDecision(*sync_buffer_, |
| 853 | *expand_, |
| 854 | decoder_frame_length_, |
| 855 | header, |
| 856 | last_mode_, |
| 857 | *play_dtmf, |
| 858 | &reset_decoder_); |
| 859 | |
| 860 | // Check if we already have enough samples in the |sync_buffer_|. If so, |
| 861 | // change decision to normal, unless the decision was merge, accelerate, or |
| 862 | // preemptive expand. |
| 863 | if (samples_left >= output_size_samples_ && |
| 864 | *operation != kMerge && |
| 865 | *operation != kAccelerate && |
| 866 | *operation != kPreemptiveExpand) { |
| 867 | *operation = kNormal; |
| 868 | return 0; |
| 869 | } |
| 870 | |
| 871 | decision_logic_->ExpandDecision(*operation == kExpand); |
| 872 | |
| 873 | // Check conditions for reset. |
| 874 | if (new_codec_ || *operation == kUndefined) { |
| 875 | // The only valid reason to get kUndefined is that new_codec_ is set. |
| 876 | assert(new_codec_); |
turaj@webrtc.org | 4d06db5 | 2013-03-27 18:31:42 +0000 | [diff] [blame] | 877 | if (*play_dtmf && !header) { |
| 878 | timestamp_ = dtmf_event->timestamp; |
| 879 | } else { |
| 880 | assert(header); |
| 881 | if (!header) { |
| 882 | LOG_F(LS_ERROR) << "Packet missing where it shouldn't."; |
| 883 | return -1; |
| 884 | } |
| 885 | timestamp_ = header->timestamp; |
| 886 | if (*operation == kRfc3389CngNoPacket |
| 887 | #ifndef LEGACY_BITEXACT |
| 888 | // Without this check, it can happen that a non-CNG packet is sent to |
| 889 | // the CNG decoder as if it was a SID frame. This is clearly a bug, |
| 890 | // but is kept for now to maintain bit-exactness with the test |
| 891 | // vectors. |
| 892 | && decoder_database_->IsComfortNoise(header->payloadType) |
| 893 | #endif |
| 894 | ) { |
| 895 | // Change decision to CNG packet, since we do have a CNG packet, but it |
| 896 | // was considered too early to use. Now, use it anyway. |
| 897 | *operation = kRfc3389Cng; |
| 898 | } else if (*operation != kRfc3389Cng) { |
| 899 | *operation = kNormal; |
| 900 | } |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 901 | } |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 902 | // Adjust |sync_buffer_| timestamp before setting |end_timestamp| to the |
| 903 | // new value. |
| 904 | sync_buffer_->IncreaseEndTimestamp(timestamp_ - end_timestamp); |
turaj@webrtc.org | 4d06db5 | 2013-03-27 18:31:42 +0000 | [diff] [blame] | 905 | end_timestamp = timestamp_; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 906 | new_codec_ = false; |
| 907 | decision_logic_->SoftReset(); |
| 908 | buffer_level_filter_->Reset(); |
| 909 | delay_manager_->Reset(); |
| 910 | stats_.ResetMcu(); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 911 | } |
| 912 | |
| 913 | int required_samples = output_size_samples_; |
| 914 | const int samples_10_ms = 80 * fs_mult_; |
| 915 | const int samples_20_ms = 2 * samples_10_ms; |
| 916 | const int samples_30_ms = 3 * samples_10_ms; |
| 917 | |
| 918 | switch (*operation) { |
| 919 | case kExpand: { |
| 920 | timestamp_ = end_timestamp; |
| 921 | return 0; |
| 922 | } |
| 923 | case kRfc3389CngNoPacket: |
| 924 | case kCodecInternalCng: { |
| 925 | return 0; |
| 926 | } |
| 927 | case kDtmf: { |
| 928 | // TODO(hlundin): Write test for this. |
| 929 | // Update timestamp. |
| 930 | timestamp_ = end_timestamp; |
| 931 | if (decision_logic_->generated_noise_samples() > 0 && |
| 932 | last_mode_ != kModeDtmf) { |
| 933 | // Make a jump in timestamp due to the recently played comfort noise. |
| 934 | uint32_t timestamp_jump = decision_logic_->generated_noise_samples(); |
| 935 | sync_buffer_->IncreaseEndTimestamp(timestamp_jump); |
| 936 | timestamp_ += timestamp_jump; |
| 937 | } |
| 938 | decision_logic_->set_generated_noise_samples(0); |
| 939 | return 0; |
| 940 | } |
| 941 | case kAccelerate: { |
| 942 | // In order to do a accelerate we need at least 30 ms of audio data. |
| 943 | if (samples_left >= samples_30_ms) { |
| 944 | // Already have enough data, so we do not need to extract any more. |
| 945 | decision_logic_->set_sample_memory(samples_left); |
| 946 | decision_logic_->set_prev_time_scale(true); |
| 947 | return 0; |
| 948 | } else if (samples_left >= samples_10_ms && |
| 949 | decoder_frame_length_ >= samples_30_ms) { |
| 950 | // Avoid decoding more data as it might overflow the playout buffer. |
| 951 | *operation = kNormal; |
| 952 | return 0; |
| 953 | } else if (samples_left < samples_20_ms && |
| 954 | decoder_frame_length_ < samples_30_ms) { |
| 955 | // Build up decoded data by decoding at least 20 ms of audio data. Do |
| 956 | // not perform accelerate yet, but wait until we only need to do one |
| 957 | // decoding. |
| 958 | required_samples = 2 * output_size_samples_; |
| 959 | *operation = kNormal; |
| 960 | } |
| 961 | // If none of the above is true, we have one of two possible situations: |
| 962 | // (1) 20 ms <= samples_left < 30 ms and decoder_frame_length_ < 30 ms; or |
| 963 | // (2) samples_left < 10 ms and decoder_frame_length_ >= 30 ms. |
| 964 | // In either case, we move on with the accelerate decision, and decode one |
| 965 | // frame now. |
| 966 | break; |
| 967 | } |
| 968 | case kPreemptiveExpand: { |
| 969 | // In order to do a preemptive expand we need at least 30 ms of decoded |
| 970 | // audio data. |
| 971 | if ((samples_left >= samples_30_ms) || |
| 972 | (samples_left >= samples_10_ms && |
| 973 | decoder_frame_length_ >= samples_30_ms)) { |
| 974 | // Already have enough data, so we do not need to extract any more. |
| 975 | // Or, avoid decoding more data as it might overflow the playout buffer. |
| 976 | // Still try preemptive expand, though. |
| 977 | decision_logic_->set_sample_memory(samples_left); |
| 978 | decision_logic_->set_prev_time_scale(true); |
| 979 | return 0; |
| 980 | } |
| 981 | if (samples_left < samples_20_ms && |
| 982 | decoder_frame_length_ < samples_30_ms) { |
| 983 | // Build up decoded data by decoding at least 20 ms of audio data. |
| 984 | // Still try to perform preemptive expand. |
| 985 | required_samples = 2 * output_size_samples_; |
| 986 | } |
| 987 | // Move on with the preemptive expand decision. |
| 988 | break; |
| 989 | } |
| 990 | default: { |
| 991 | // Do nothing. |
| 992 | } |
| 993 | } |
| 994 | |
| 995 | // Get packets from buffer. |
| 996 | int extracted_samples = 0; |
| 997 | if (header && |
| 998 | *operation != kAlternativePlc && |
| 999 | *operation != kAlternativePlcIncreaseTimestamp && |
| 1000 | *operation != kAudioRepetition && |
| 1001 | *operation != kAudioRepetitionIncreaseTimestamp) { |
| 1002 | sync_buffer_->IncreaseEndTimestamp(header->timestamp - end_timestamp); |
| 1003 | if (decision_logic_->CngOff()) { |
| 1004 | // Adjustment of timestamp only corresponds to an actual packet loss |
| 1005 | // if comfort noise is not played. If comfort noise was just played, |
| 1006 | // this adjustment of timestamp is only done to get back in sync with the |
| 1007 | // stream timestamp; no loss to report. |
| 1008 | stats_.LostSamples(header->timestamp - end_timestamp); |
| 1009 | } |
| 1010 | |
| 1011 | if (*operation != kRfc3389Cng) { |
| 1012 | // We are about to decode and use a non-CNG packet. |
| 1013 | decision_logic_->SetCngOff(); |
| 1014 | } |
| 1015 | // Reset CNG timestamp as a new packet will be delivered. |
| 1016 | // (Also if this is a CNG packet, since playedOutTS is updated.) |
| 1017 | decision_logic_->set_generated_noise_samples(0); |
| 1018 | |
| 1019 | extracted_samples = ExtractPackets(required_samples, packet_list); |
| 1020 | if (extracted_samples < 0) { |
| 1021 | LOG_F(LS_WARNING) << "Failed to extract packets from buffer."; |
| 1022 | return kPacketBufferCorruption; |
| 1023 | } |
| 1024 | } |
| 1025 | |
| 1026 | if (*operation == kAccelerate || |
| 1027 | *operation == kPreemptiveExpand) { |
| 1028 | decision_logic_->set_sample_memory(samples_left + extracted_samples); |
| 1029 | decision_logic_->set_prev_time_scale(true); |
| 1030 | } |
| 1031 | |
| 1032 | if (*operation == kAccelerate) { |
| 1033 | // Check that we have enough data (30ms) to do accelerate. |
| 1034 | if (extracted_samples + samples_left < samples_30_ms) { |
| 1035 | // TODO(hlundin): Write test for this. |
| 1036 | // Not enough, do normal operation instead. |
| 1037 | *operation = kNormal; |
| 1038 | } |
| 1039 | } |
| 1040 | |
| 1041 | timestamp_ = end_timestamp; |
| 1042 | return 0; |
| 1043 | } |
| 1044 | |
| 1045 | int NetEqImpl::Decode(PacketList* packet_list, Operations* operation, |
| 1046 | int* decoded_length, |
| 1047 | AudioDecoder::SpeechType* speech_type) { |
| 1048 | *speech_type = AudioDecoder::kSpeech; |
| 1049 | AudioDecoder* decoder = NULL; |
| 1050 | if (!packet_list->empty()) { |
| 1051 | const Packet* packet = packet_list->front(); |
| 1052 | int payload_type = packet->header.payloadType; |
| 1053 | if (!decoder_database_->IsComfortNoise(payload_type)) { |
| 1054 | decoder = decoder_database_->GetDecoder(payload_type); |
| 1055 | assert(decoder); |
| 1056 | if (!decoder) { |
| 1057 | LOG_FERR1(LS_WARNING, GetDecoder, payload_type); |
| 1058 | PacketBuffer::DeleteAllPackets(packet_list); |
| 1059 | return kDecoderNotFound; |
| 1060 | } |
| 1061 | bool decoder_changed; |
| 1062 | decoder_database_->SetActiveDecoder(payload_type, &decoder_changed); |
| 1063 | if (decoder_changed) { |
| 1064 | // We have a new decoder. Re-init some values. |
| 1065 | const DecoderDatabase::DecoderInfo* decoder_info = decoder_database_ |
| 1066 | ->GetDecoderInfo(payload_type); |
| 1067 | assert(decoder_info); |
| 1068 | if (!decoder_info) { |
| 1069 | LOG_FERR1(LS_WARNING, GetDecoderInfo, payload_type); |
| 1070 | PacketBuffer::DeleteAllPackets(packet_list); |
| 1071 | return kDecoderNotFound; |
| 1072 | } |
| 1073 | SetSampleRateAndChannels(decoder_info->fs_hz, decoder->channels()); |
| 1074 | sync_buffer_->set_end_timestamp(timestamp_); |
| 1075 | playout_timestamp_ = timestamp_; |
| 1076 | } |
| 1077 | } |
| 1078 | } |
| 1079 | |
| 1080 | if (reset_decoder_) { |
| 1081 | // TODO(hlundin): Write test for this. |
| 1082 | // Reset decoder. |
| 1083 | if (decoder) { |
| 1084 | decoder->Init(); |
| 1085 | } |
| 1086 | // Reset comfort noise decoder. |
| 1087 | AudioDecoder* cng_decoder = decoder_database_->GetActiveCngDecoder(); |
| 1088 | if (cng_decoder) { |
| 1089 | cng_decoder->Init(); |
| 1090 | } |
| 1091 | reset_decoder_ = false; |
| 1092 | } |
| 1093 | |
| 1094 | #ifdef LEGACY_BITEXACT |
| 1095 | // Due to a bug in old SignalMCU, it could happen that CNG operation was |
| 1096 | // decided, but a speech packet was provided. The speech packet will be used |
| 1097 | // to update the comfort noise decoder, as if it was a SID frame, which is |
| 1098 | // clearly wrong. |
| 1099 | if (*operation == kRfc3389Cng) { |
| 1100 | return 0; |
| 1101 | } |
| 1102 | #endif |
| 1103 | |
| 1104 | *decoded_length = 0; |
| 1105 | // Update codec-internal PLC state. |
| 1106 | if ((*operation == kMerge) && decoder && decoder->HasDecodePlc()) { |
| 1107 | decoder->DecodePlc(1, &decoded_buffer_[*decoded_length]); |
| 1108 | } |
| 1109 | |
| 1110 | int return_value = DecodeLoop(packet_list, operation, decoder, |
| 1111 | decoded_length, speech_type); |
| 1112 | |
| 1113 | if (*decoded_length < 0) { |
| 1114 | // Error returned from the decoder. |
| 1115 | *decoded_length = 0; |
| 1116 | sync_buffer_->IncreaseEndTimestamp(decoder_frame_length_); |
| 1117 | int error_code = 0; |
| 1118 | if (decoder) |
| 1119 | error_code = decoder->ErrorCode(); |
| 1120 | if (error_code != 0) { |
| 1121 | // Got some error code from the decoder. |
| 1122 | decoder_error_code_ = error_code; |
| 1123 | return_value = kDecoderErrorCode; |
| 1124 | } else { |
| 1125 | // Decoder does not implement error codes. Return generic error. |
| 1126 | return_value = kOtherDecoderError; |
| 1127 | } |
| 1128 | LOG_FERR2(LS_WARNING, DecodeLoop, error_code, packet_list->size()); |
| 1129 | *operation = kExpand; // Do expansion to get data instead. |
| 1130 | } |
| 1131 | if (*speech_type != AudioDecoder::kComfortNoise) { |
| 1132 | // Don't increment timestamp if codec returned CNG speech type |
| 1133 | // since in this case, the we will increment the CNGplayedTS counter. |
| 1134 | // Increase with number of samples per channel. |
| 1135 | assert(*decoded_length == 0 || |
| 1136 | (decoder && decoder->channels() == sync_buffer_->Channels())); |
turaj@webrtc.org | 362a55e | 2013-09-20 16:25:28 +0000 | [diff] [blame] | 1137 | sync_buffer_->IncreaseEndTimestamp( |
| 1138 | *decoded_length / static_cast<int>(sync_buffer_->Channels())); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 1139 | } |
| 1140 | return return_value; |
| 1141 | } |
| 1142 | |
| 1143 | int NetEqImpl::DecodeLoop(PacketList* packet_list, Operations* operation, |
| 1144 | AudioDecoder* decoder, int* decoded_length, |
| 1145 | AudioDecoder::SpeechType* speech_type) { |
| 1146 | Packet* packet = NULL; |
| 1147 | if (!packet_list->empty()) { |
| 1148 | packet = packet_list->front(); |
| 1149 | } |
| 1150 | // Do decoding. |
| 1151 | while (packet && |
| 1152 | !decoder_database_->IsComfortNoise(packet->header.payloadType)) { |
| 1153 | assert(decoder); // At this point, we must have a decoder object. |
| 1154 | // The number of channels in the |sync_buffer_| should be the same as the |
| 1155 | // number decoder channels. |
| 1156 | assert(sync_buffer_->Channels() == decoder->channels()); |
| 1157 | assert(decoded_buffer_length_ >= kMaxFrameSize * decoder->channels()); |
| 1158 | assert(*operation == kNormal || *operation == kAccelerate || |
| 1159 | *operation == kMerge || *operation == kPreemptiveExpand); |
| 1160 | packet_list->pop_front(); |
henrik.lundin@webrtc.org | 63464a9 | 2013-01-30 09:41:56 +0000 | [diff] [blame] | 1161 | int payload_length = packet->payload_length; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 1162 | int16_t decode_length; |
| 1163 | if (!packet->primary) { |
| 1164 | // This is a redundant payload; call the special decoder method. |
henrik.lundin@webrtc.org | b3e905c | 2013-09-02 09:41:06 +0000 | [diff] [blame] | 1165 | NETEQ_LOG_VERBOSE << "Decoding packet (redundant):" << |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 1166 | " ts=" << packet->header.timestamp << |
| 1167 | ", sn=" << packet->header.sequenceNumber << |
| 1168 | ", pt=" << static_cast<int>(packet->header.payloadType) << |
| 1169 | ", ssrc=" << packet->header.ssrc << |
| 1170 | ", len=" << packet->payload_length; |
| 1171 | decode_length = decoder->DecodeRedundant( |
| 1172 | packet->payload, packet->payload_length, |
| 1173 | &decoded_buffer_[*decoded_length], speech_type); |
| 1174 | } else { |
henrik.lundin@webrtc.org | b3e905c | 2013-09-02 09:41:06 +0000 | [diff] [blame] | 1175 | NETEQ_LOG_VERBOSE << "Decoding packet: ts=" << packet->header.timestamp << |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 1176 | ", sn=" << packet->header.sequenceNumber << |
| 1177 | ", pt=" << static_cast<int>(packet->header.payloadType) << |
| 1178 | ", ssrc=" << packet->header.ssrc << |
| 1179 | ", len=" << packet->payload_length; |
| 1180 | decode_length = decoder->Decode(packet->payload, |
| 1181 | packet->payload_length, |
| 1182 | &decoded_buffer_[*decoded_length], |
| 1183 | speech_type); |
| 1184 | } |
| 1185 | |
| 1186 | delete[] packet->payload; |
| 1187 | delete packet; |
| 1188 | if (decode_length > 0) { |
| 1189 | *decoded_length += decode_length; |
| 1190 | // Update |decoder_frame_length_| with number of samples per channel. |
turaj@webrtc.org | 362a55e | 2013-09-20 16:25:28 +0000 | [diff] [blame] | 1191 | decoder_frame_length_ = decode_length / |
| 1192 | static_cast<int>(decoder->channels()); |
henrik.lundin@webrtc.org | b3e905c | 2013-09-02 09:41:06 +0000 | [diff] [blame] | 1193 | NETEQ_LOG_VERBOSE << "Decoded " << decode_length << " samples (" << |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 1194 | decoder->channels() << " channel(s) -> " << decoder_frame_length_ << |
| 1195 | " samples per channel)"; |
| 1196 | } else if (decode_length < 0) { |
| 1197 | // Error. |
henrik.lundin@webrtc.org | 63464a9 | 2013-01-30 09:41:56 +0000 | [diff] [blame] | 1198 | LOG_FERR2(LS_WARNING, Decode, decode_length, payload_length); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 1199 | *decoded_length = -1; |
| 1200 | PacketBuffer::DeleteAllPackets(packet_list); |
| 1201 | break; |
| 1202 | } |
| 1203 | if (*decoded_length > static_cast<int>(decoded_buffer_length_)) { |
| 1204 | // Guard against overflow. |
| 1205 | LOG_F(LS_WARNING) << "Decoded too much."; |
| 1206 | PacketBuffer::DeleteAllPackets(packet_list); |
| 1207 | return kDecodedTooMuch; |
| 1208 | } |
| 1209 | if (!packet_list->empty()) { |
| 1210 | packet = packet_list->front(); |
| 1211 | } else { |
| 1212 | packet = NULL; |
| 1213 | } |
| 1214 | } // End of decode loop. |
| 1215 | |
| 1216 | // If the list is not empty at this point, it must hold exactly one CNG |
| 1217 | // packet. |
| 1218 | assert(packet_list->empty() || |
| 1219 | (packet_list->size() == 1 && |
| 1220 | decoder_database_->IsComfortNoise(packet->header.payloadType))); |
| 1221 | return 0; |
| 1222 | } |
| 1223 | |
| 1224 | void NetEqImpl::DoNormal(const int16_t* decoded_buffer, size_t decoded_length, |
henrik.lundin@webrtc.org | c487c6a | 2013-09-02 07:59:30 +0000 | [diff] [blame] | 1225 | AudioDecoder::SpeechType speech_type, bool play_dtmf) { |
henrik.lundin@webrtc.org | 40d3fc6 | 2013-09-18 12:19:50 +0000 | [diff] [blame] | 1226 | assert(normal_.get()); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 1227 | assert(mute_factor_array_.get()); |
henrik.lundin@webrtc.org | 40d3fc6 | 2013-09-18 12:19:50 +0000 | [diff] [blame] | 1228 | normal_->Process(decoded_buffer, decoded_length, last_mode_, |
henrik.lundin@webrtc.org | 0d5da25 | 2013-09-18 21:12:38 +0000 | [diff] [blame] | 1229 | mute_factor_array_.get(), algorithm_buffer_.get()); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 1230 | if (decoded_length != 0) { |
| 1231 | last_mode_ = kModeNormal; |
| 1232 | } |
| 1233 | |
| 1234 | // If last packet was decoded as an inband CNG, set mode to CNG instead. |
| 1235 | if ((speech_type == AudioDecoder::kComfortNoise) |
| 1236 | || ((last_mode_ == kModeCodecInternalCng) |
| 1237 | && (decoded_length == 0))) { |
| 1238 | // TODO(hlundin): Remove second part of || statement above. |
| 1239 | last_mode_ = kModeCodecInternalCng; |
| 1240 | } |
| 1241 | |
| 1242 | if (!play_dtmf) { |
| 1243 | dtmf_tone_generator_->Reset(); |
| 1244 | } |
| 1245 | } |
| 1246 | |
| 1247 | void NetEqImpl::DoMerge(int16_t* decoded_buffer, size_t decoded_length, |
henrik.lundin@webrtc.org | c487c6a | 2013-09-02 07:59:30 +0000 | [diff] [blame] | 1248 | AudioDecoder::SpeechType speech_type, bool play_dtmf) { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 1249 | assert(mute_factor_array_.get()); |
henrik.lundin@webrtc.org | 40d3fc6 | 2013-09-18 12:19:50 +0000 | [diff] [blame] | 1250 | assert(merge_.get()); |
| 1251 | int new_length = merge_->Process(decoded_buffer, decoded_length, |
henrik.lundin@webrtc.org | 0d5da25 | 2013-09-18 21:12:38 +0000 | [diff] [blame] | 1252 | mute_factor_array_.get(), |
| 1253 | algorithm_buffer_.get()); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 1254 | |
| 1255 | // Update in-call and post-call statistics. |
| 1256 | if (expand_->MuteFactor(0) == 0) { |
| 1257 | // Expand generates only noise. |
turaj@webrtc.org | 362a55e | 2013-09-20 16:25:28 +0000 | [diff] [blame] | 1258 | stats_.ExpandedNoiseSamples(new_length - static_cast<int>(decoded_length)); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 1259 | } else { |
| 1260 | // Expansion generates more than only noise. |
turaj@webrtc.org | 362a55e | 2013-09-20 16:25:28 +0000 | [diff] [blame] | 1261 | stats_.ExpandedVoiceSamples(new_length - static_cast<int>(decoded_length)); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 1262 | } |
| 1263 | |
| 1264 | last_mode_ = kModeMerge; |
| 1265 | // If last packet was decoded as an inband CNG, set mode to CNG instead. |
| 1266 | if (speech_type == AudioDecoder::kComfortNoise) { |
| 1267 | last_mode_ = kModeCodecInternalCng; |
| 1268 | } |
| 1269 | expand_->Reset(); |
| 1270 | if (!play_dtmf) { |
| 1271 | dtmf_tone_generator_->Reset(); |
| 1272 | } |
| 1273 | } |
| 1274 | |
henrik.lundin@webrtc.org | c487c6a | 2013-09-02 07:59:30 +0000 | [diff] [blame] | 1275 | int NetEqImpl::DoExpand(bool play_dtmf) { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 1276 | while ((sync_buffer_->FutureLength() - expand_->overlap_length()) < |
| 1277 | static_cast<size_t>(output_size_samples_)) { |
henrik.lundin@webrtc.org | c487c6a | 2013-09-02 07:59:30 +0000 | [diff] [blame] | 1278 | algorithm_buffer_->Clear(); |
henrik.lundin@webrtc.org | 0d5da25 | 2013-09-18 21:12:38 +0000 | [diff] [blame] | 1279 | int return_value = expand_->Process(algorithm_buffer_.get()); |
turaj@webrtc.org | 362a55e | 2013-09-20 16:25:28 +0000 | [diff] [blame] | 1280 | int length = static_cast<int>(algorithm_buffer_->Size()); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 1281 | |
| 1282 | // Update in-call and post-call statistics. |
| 1283 | if (expand_->MuteFactor(0) == 0) { |
| 1284 | // Expand operation generates only noise. |
| 1285 | stats_.ExpandedNoiseSamples(length); |
| 1286 | } else { |
| 1287 | // Expand operation generates more than only noise. |
| 1288 | stats_.ExpandedVoiceSamples(length); |
| 1289 | } |
| 1290 | |
| 1291 | last_mode_ = kModeExpand; |
| 1292 | |
| 1293 | if (return_value < 0) { |
| 1294 | return return_value; |
| 1295 | } |
| 1296 | |
henrik.lundin@webrtc.org | c487c6a | 2013-09-02 07:59:30 +0000 | [diff] [blame] | 1297 | sync_buffer_->PushBack(*algorithm_buffer_); |
| 1298 | algorithm_buffer_->Clear(); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 1299 | } |
| 1300 | if (!play_dtmf) { |
| 1301 | dtmf_tone_generator_->Reset(); |
| 1302 | } |
| 1303 | return 0; |
| 1304 | } |
| 1305 | |
| 1306 | int NetEqImpl::DoAccelerate(int16_t* decoded_buffer, size_t decoded_length, |
| 1307 | AudioDecoder::SpeechType speech_type, |
henrik.lundin@webrtc.org | c487c6a | 2013-09-02 07:59:30 +0000 | [diff] [blame] | 1308 | bool play_dtmf) { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 1309 | const size_t required_samples = 240 * fs_mult_; // Must have 30 ms. |
turaj@webrtc.org | 362a55e | 2013-09-20 16:25:28 +0000 | [diff] [blame] | 1310 | size_t borrowed_samples_per_channel = 0; |
henrik.lundin@webrtc.org | c487c6a | 2013-09-02 07:59:30 +0000 | [diff] [blame] | 1311 | size_t num_channels = algorithm_buffer_->Channels(); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 1312 | size_t decoded_length_per_channel = decoded_length / num_channels; |
| 1313 | if (decoded_length_per_channel < required_samples) { |
| 1314 | // Must move data from the |sync_buffer_| in order to get 30 ms. |
turaj@webrtc.org | 362a55e | 2013-09-20 16:25:28 +0000 | [diff] [blame] | 1315 | borrowed_samples_per_channel = static_cast<int>(required_samples - |
| 1316 | decoded_length_per_channel); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 1317 | memmove(&decoded_buffer[borrowed_samples_per_channel * num_channels], |
| 1318 | decoded_buffer, |
| 1319 | sizeof(int16_t) * decoded_length); |
| 1320 | sync_buffer_->ReadInterleavedFromEnd(borrowed_samples_per_channel, |
| 1321 | decoded_buffer); |
| 1322 | decoded_length = required_samples * num_channels; |
| 1323 | } |
| 1324 | |
| 1325 | int16_t samples_removed; |
henrik.lundin@webrtc.org | 0d5da25 | 2013-09-18 21:12:38 +0000 | [diff] [blame] | 1326 | Accelerate::ReturnCodes return_code = accelerate_->Process( |
| 1327 | decoded_buffer, decoded_length, algorithm_buffer_.get(), |
| 1328 | &samples_removed); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 1329 | stats_.AcceleratedSamples(samples_removed); |
| 1330 | switch (return_code) { |
| 1331 | case Accelerate::kSuccess: |
| 1332 | last_mode_ = kModeAccelerateSuccess; |
| 1333 | break; |
| 1334 | case Accelerate::kSuccessLowEnergy: |
| 1335 | last_mode_ = kModeAccelerateLowEnergy; |
| 1336 | break; |
| 1337 | case Accelerate::kNoStretch: |
| 1338 | last_mode_ = kModeAccelerateFail; |
| 1339 | break; |
| 1340 | case Accelerate::kError: |
| 1341 | // TODO(hlundin): Map to kModeError instead? |
| 1342 | last_mode_ = kModeAccelerateFail; |
| 1343 | return kAccelerateError; |
| 1344 | } |
| 1345 | |
| 1346 | if (borrowed_samples_per_channel > 0) { |
| 1347 | // Copy borrowed samples back to the |sync_buffer_|. |
turaj@webrtc.org | 362a55e | 2013-09-20 16:25:28 +0000 | [diff] [blame] | 1348 | size_t length = algorithm_buffer_->Size(); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 1349 | if (length < borrowed_samples_per_channel) { |
| 1350 | // This destroys the beginning of the buffer, but will not cause any |
| 1351 | // problems. |
henrik.lundin@webrtc.org | c487c6a | 2013-09-02 07:59:30 +0000 | [diff] [blame] | 1352 | sync_buffer_->ReplaceAtIndex(*algorithm_buffer_, |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 1353 | sync_buffer_->Size() - |
| 1354 | borrowed_samples_per_channel); |
| 1355 | sync_buffer_->PushFrontZeros(borrowed_samples_per_channel - length); |
henrik.lundin@webrtc.org | c487c6a | 2013-09-02 07:59:30 +0000 | [diff] [blame] | 1356 | algorithm_buffer_->PopFront(length); |
| 1357 | assert(algorithm_buffer_->Empty()); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 1358 | } else { |
henrik.lundin@webrtc.org | c487c6a | 2013-09-02 07:59:30 +0000 | [diff] [blame] | 1359 | sync_buffer_->ReplaceAtIndex(*algorithm_buffer_, |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 1360 | borrowed_samples_per_channel, |
| 1361 | sync_buffer_->Size() - |
| 1362 | borrowed_samples_per_channel); |
henrik.lundin@webrtc.org | c487c6a | 2013-09-02 07:59:30 +0000 | [diff] [blame] | 1363 | algorithm_buffer_->PopFront(borrowed_samples_per_channel); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 1364 | } |
| 1365 | } |
| 1366 | |
| 1367 | // If last packet was decoded as an inband CNG, set mode to CNG instead. |
| 1368 | if (speech_type == AudioDecoder::kComfortNoise) { |
| 1369 | last_mode_ = kModeCodecInternalCng; |
| 1370 | } |
| 1371 | if (!play_dtmf) { |
| 1372 | dtmf_tone_generator_->Reset(); |
| 1373 | } |
| 1374 | expand_->Reset(); |
| 1375 | return 0; |
| 1376 | } |
| 1377 | |
| 1378 | int NetEqImpl::DoPreemptiveExpand(int16_t* decoded_buffer, |
| 1379 | size_t decoded_length, |
| 1380 | AudioDecoder::SpeechType speech_type, |
henrik.lundin@webrtc.org | c487c6a | 2013-09-02 07:59:30 +0000 | [diff] [blame] | 1381 | bool play_dtmf) { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 1382 | const size_t required_samples = 240 * fs_mult_; // Must have 30 ms. |
henrik.lundin@webrtc.org | c487c6a | 2013-09-02 07:59:30 +0000 | [diff] [blame] | 1383 | size_t num_channels = algorithm_buffer_->Channels(); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 1384 | int borrowed_samples_per_channel = 0; |
| 1385 | int old_borrowed_samples_per_channel = 0; |
| 1386 | size_t decoded_length_per_channel = decoded_length / num_channels; |
| 1387 | if (decoded_length_per_channel < required_samples) { |
| 1388 | // Must move data from the |sync_buffer_| in order to get 30 ms. |
turaj@webrtc.org | 362a55e | 2013-09-20 16:25:28 +0000 | [diff] [blame] | 1389 | borrowed_samples_per_channel = static_cast<int>(required_samples - |
| 1390 | decoded_length_per_channel); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 1391 | // Calculate how many of these were already played out. |
turaj@webrtc.org | 362a55e | 2013-09-20 16:25:28 +0000 | [diff] [blame] | 1392 | old_borrowed_samples_per_channel = static_cast<int>( |
| 1393 | borrowed_samples_per_channel - sync_buffer_->FutureLength()); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 1394 | old_borrowed_samples_per_channel = std::max( |
| 1395 | 0, old_borrowed_samples_per_channel); |
| 1396 | memmove(&decoded_buffer[borrowed_samples_per_channel * num_channels], |
| 1397 | decoded_buffer, |
| 1398 | sizeof(int16_t) * decoded_length); |
| 1399 | sync_buffer_->ReadInterleavedFromEnd(borrowed_samples_per_channel, |
| 1400 | decoded_buffer); |
| 1401 | decoded_length = required_samples * num_channels; |
| 1402 | } |
| 1403 | |
| 1404 | int16_t samples_added; |
henrik.lundin@webrtc.org | 40d3fc6 | 2013-09-18 12:19:50 +0000 | [diff] [blame] | 1405 | PreemptiveExpand::ReturnCodes return_code = preemptive_expand_->Process( |
turaj@webrtc.org | 362a55e | 2013-09-20 16:25:28 +0000 | [diff] [blame] | 1406 | decoded_buffer, static_cast<int>(decoded_length), |
| 1407 | old_borrowed_samples_per_channel, |
henrik.lundin@webrtc.org | 0d5da25 | 2013-09-18 21:12:38 +0000 | [diff] [blame] | 1408 | algorithm_buffer_.get(), &samples_added); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 1409 | stats_.PreemptiveExpandedSamples(samples_added); |
| 1410 | switch (return_code) { |
| 1411 | case PreemptiveExpand::kSuccess: |
| 1412 | last_mode_ = kModePreemptiveExpandSuccess; |
| 1413 | break; |
| 1414 | case PreemptiveExpand::kSuccessLowEnergy: |
| 1415 | last_mode_ = kModePreemptiveExpandLowEnergy; |
| 1416 | break; |
| 1417 | case PreemptiveExpand::kNoStretch: |
| 1418 | last_mode_ = kModePreemptiveExpandFail; |
| 1419 | break; |
| 1420 | case PreemptiveExpand::kError: |
| 1421 | // TODO(hlundin): Map to kModeError instead? |
| 1422 | last_mode_ = kModePreemptiveExpandFail; |
| 1423 | return kPreemptiveExpandError; |
| 1424 | } |
| 1425 | |
| 1426 | if (borrowed_samples_per_channel > 0) { |
| 1427 | // Copy borrowed samples back to the |sync_buffer_|. |
| 1428 | sync_buffer_->ReplaceAtIndex( |
henrik.lundin@webrtc.org | c487c6a | 2013-09-02 07:59:30 +0000 | [diff] [blame] | 1429 | *algorithm_buffer_, borrowed_samples_per_channel, |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 1430 | sync_buffer_->Size() - borrowed_samples_per_channel); |
henrik.lundin@webrtc.org | c487c6a | 2013-09-02 07:59:30 +0000 | [diff] [blame] | 1431 | algorithm_buffer_->PopFront(borrowed_samples_per_channel); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 1432 | } |
| 1433 | |
| 1434 | // If last packet was decoded as an inband CNG, set mode to CNG instead. |
| 1435 | if (speech_type == AudioDecoder::kComfortNoise) { |
| 1436 | last_mode_ = kModeCodecInternalCng; |
| 1437 | } |
| 1438 | if (!play_dtmf) { |
| 1439 | dtmf_tone_generator_->Reset(); |
| 1440 | } |
| 1441 | expand_->Reset(); |
| 1442 | return 0; |
| 1443 | } |
| 1444 | |
henrik.lundin@webrtc.org | c487c6a | 2013-09-02 07:59:30 +0000 | [diff] [blame] | 1445 | int NetEqImpl::DoRfc3389Cng(PacketList* packet_list, bool play_dtmf) { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 1446 | if (!packet_list->empty()) { |
| 1447 | // Must have exactly one SID frame at this point. |
| 1448 | assert(packet_list->size() == 1); |
| 1449 | Packet* packet = packet_list->front(); |
| 1450 | packet_list->pop_front(); |
henrik.lundin@webrtc.org | 73deaad | 2013-01-31 13:32:51 +0000 | [diff] [blame] | 1451 | if (!decoder_database_->IsComfortNoise(packet->header.payloadType)) { |
| 1452 | #ifdef LEGACY_BITEXACT |
| 1453 | // This can happen due to a bug in GetDecision. Change the payload type |
| 1454 | // to a CNG type, and move on. Note that this means that we are in fact |
| 1455 | // sending a non-CNG payload to the comfort noise decoder for decoding. |
| 1456 | // Clearly wrong, but will maintain bit-exactness with legacy. |
| 1457 | if (fs_hz_ == 8000) { |
| 1458 | packet->header.payloadType = |
| 1459 | decoder_database_->GetRtpPayloadType(kDecoderCNGnb); |
| 1460 | } else if (fs_hz_ == 16000) { |
| 1461 | packet->header.payloadType = |
| 1462 | decoder_database_->GetRtpPayloadType(kDecoderCNGwb); |
| 1463 | } else if (fs_hz_ == 32000) { |
| 1464 | packet->header.payloadType = |
| 1465 | decoder_database_->GetRtpPayloadType(kDecoderCNGswb32kHz); |
| 1466 | } else if (fs_hz_ == 48000) { |
| 1467 | packet->header.payloadType = |
| 1468 | decoder_database_->GetRtpPayloadType(kDecoderCNGswb48kHz); |
| 1469 | } |
| 1470 | assert(decoder_database_->IsComfortNoise(packet->header.payloadType)); |
| 1471 | #else |
| 1472 | LOG(LS_ERROR) << "Trying to decode non-CNG payload as CNG."; |
| 1473 | return kOtherError; |
| 1474 | #endif |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 1475 | } |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 1476 | // UpdateParameters() deletes |packet|. |
| 1477 | if (comfort_noise_->UpdateParameters(packet) == |
| 1478 | ComfortNoise::kInternalError) { |
| 1479 | LOG_FERR0(LS_WARNING, UpdateParameters); |
henrik.lundin@webrtc.org | c487c6a | 2013-09-02 07:59:30 +0000 | [diff] [blame] | 1480 | algorithm_buffer_->Zeros(output_size_samples_); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 1481 | return -comfort_noise_->internal_error_code(); |
| 1482 | } |
| 1483 | } |
| 1484 | int cn_return = comfort_noise_->Generate(output_size_samples_, |
henrik.lundin@webrtc.org | 0d5da25 | 2013-09-18 21:12:38 +0000 | [diff] [blame] | 1485 | algorithm_buffer_.get()); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 1486 | expand_->Reset(); |
| 1487 | last_mode_ = kModeRfc3389Cng; |
| 1488 | if (!play_dtmf) { |
| 1489 | dtmf_tone_generator_->Reset(); |
| 1490 | } |
| 1491 | if (cn_return == ComfortNoise::kInternalError) { |
| 1492 | LOG_FERR1(LS_WARNING, comfort_noise_->Generate, cn_return); |
| 1493 | decoder_error_code_ = comfort_noise_->internal_error_code(); |
| 1494 | return kComfortNoiseErrorCode; |
| 1495 | } else if (cn_return == ComfortNoise::kUnknownPayloadType) { |
| 1496 | LOG_FERR1(LS_WARNING, comfort_noise_->Generate, cn_return); |
| 1497 | return kUnknownRtpPayloadType; |
| 1498 | } |
| 1499 | return 0; |
| 1500 | } |
| 1501 | |
henrik.lundin@webrtc.org | c487c6a | 2013-09-02 07:59:30 +0000 | [diff] [blame] | 1502 | void NetEqImpl::DoCodecInternalCng() { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 1503 | int length = 0; |
| 1504 | // TODO(hlundin): Will probably need a longer buffer for multi-channel. |
| 1505 | int16_t decoded_buffer[kMaxFrameSize]; |
| 1506 | AudioDecoder* decoder = decoder_database_->GetActiveDecoder(); |
| 1507 | if (decoder) { |
| 1508 | const uint8_t* dummy_payload = NULL; |
| 1509 | AudioDecoder::SpeechType speech_type; |
| 1510 | length = decoder->Decode(dummy_payload, 0, decoded_buffer, &speech_type); |
| 1511 | } |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 1512 | assert(mute_factor_array_.get()); |
henrik.lundin@webrtc.org | 40d3fc6 | 2013-09-18 12:19:50 +0000 | [diff] [blame] | 1513 | normal_->Process(decoded_buffer, length, last_mode_, mute_factor_array_.get(), |
henrik.lundin@webrtc.org | 0d5da25 | 2013-09-18 21:12:38 +0000 | [diff] [blame] | 1514 | algorithm_buffer_.get()); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 1515 | last_mode_ = kModeCodecInternalCng; |
| 1516 | expand_->Reset(); |
| 1517 | } |
| 1518 | |
henrik.lundin@webrtc.org | c487c6a | 2013-09-02 07:59:30 +0000 | [diff] [blame] | 1519 | int NetEqImpl::DoDtmf(const DtmfEvent& dtmf_event, bool* play_dtmf) { |
turaj@webrtc.org | 4d06db5 | 2013-03-27 18:31:42 +0000 | [diff] [blame] | 1520 | // This block of the code and the block further down, handling |dtmf_switch| |
| 1521 | // are commented out. Otherwise playing out-of-band DTMF would fail in VoE |
| 1522 | // test, DtmfTest.ManualSuccessfullySendsOutOfBandTelephoneEvents. This is |
| 1523 | // equivalent to |dtmf_switch| always be false. |
| 1524 | // |
| 1525 | // See http://webrtc-codereview.appspot.com/1195004/ for discussion |
| 1526 | // On this issue. This change might cause some glitches at the point of |
| 1527 | // switch from audio to DTMF. Issue 1545 is filed to track this. |
| 1528 | // |
| 1529 | // bool dtmf_switch = false; |
| 1530 | // if ((last_mode_ != kModeDtmf) && dtmf_tone_generator_->initialized()) { |
| 1531 | // // Special case; see below. |
| 1532 | // // We must catch this before calling Generate, since |initialized| is |
| 1533 | // // modified in that call. |
| 1534 | // dtmf_switch = true; |
| 1535 | // } |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 1536 | |
| 1537 | int dtmf_return_value = 0; |
| 1538 | if (!dtmf_tone_generator_->initialized()) { |
| 1539 | // Initialize if not already done. |
| 1540 | dtmf_return_value = dtmf_tone_generator_->Init(fs_hz_, dtmf_event.event_no, |
| 1541 | dtmf_event.volume); |
| 1542 | } |
turaj@webrtc.org | 4d06db5 | 2013-03-27 18:31:42 +0000 | [diff] [blame] | 1543 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 1544 | if (dtmf_return_value == 0) { |
| 1545 | // Generate DTMF signal. |
| 1546 | dtmf_return_value = dtmf_tone_generator_->Generate(output_size_samples_, |
henrik.lundin@webrtc.org | 0d5da25 | 2013-09-18 21:12:38 +0000 | [diff] [blame] | 1547 | algorithm_buffer_.get()); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 1548 | } |
turaj@webrtc.org | 4d06db5 | 2013-03-27 18:31:42 +0000 | [diff] [blame] | 1549 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 1550 | if (dtmf_return_value < 0) { |
henrik.lundin@webrtc.org | c487c6a | 2013-09-02 07:59:30 +0000 | [diff] [blame] | 1551 | algorithm_buffer_->Zeros(output_size_samples_); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 1552 | return dtmf_return_value; |
| 1553 | } |
| 1554 | |
turaj@webrtc.org | 4d06db5 | 2013-03-27 18:31:42 +0000 | [diff] [blame] | 1555 | // if (dtmf_switch) { |
| 1556 | // // This is the special case where the previous operation was DTMF |
| 1557 | // // overdub, but the current instruction is "regular" DTMF. We must make |
| 1558 | // // sure that the DTMF does not have any discontinuities. The first DTMF |
| 1559 | // // sample that we generate now must be played out immediately, therefore |
| 1560 | // // it must be copied to the speech buffer. |
| 1561 | // // TODO(hlundin): This code seems incorrect. (Legacy.) Write test and |
| 1562 | // // verify correct operation. |
| 1563 | // assert(false); |
| 1564 | // // Must generate enough data to replace all of the |sync_buffer_| |
| 1565 | // // "future". |
| 1566 | // int required_length = sync_buffer_->FutureLength(); |
| 1567 | // assert(dtmf_tone_generator_->initialized()); |
| 1568 | // dtmf_return_value = dtmf_tone_generator_->Generate(required_length, |
henrik.lundin@webrtc.org | c487c6a | 2013-09-02 07:59:30 +0000 | [diff] [blame] | 1569 | // algorithm_buffer_); |
| 1570 | // assert((size_t) required_length == algorithm_buffer_->Size()); |
turaj@webrtc.org | 4d06db5 | 2013-03-27 18:31:42 +0000 | [diff] [blame] | 1571 | // if (dtmf_return_value < 0) { |
henrik.lundin@webrtc.org | c487c6a | 2013-09-02 07:59:30 +0000 | [diff] [blame] | 1572 | // algorithm_buffer_->Zeros(output_size_samples_); |
turaj@webrtc.org | 4d06db5 | 2013-03-27 18:31:42 +0000 | [diff] [blame] | 1573 | // return dtmf_return_value; |
| 1574 | // } |
| 1575 | // |
| 1576 | // // Overwrite the "future" part of the speech buffer with the new DTMF |
| 1577 | // // data. |
| 1578 | // // TODO(hlundin): It seems that this overwriting has gone lost. |
| 1579 | // // Not adapted for multi-channel yet. |
henrik.lundin@webrtc.org | c487c6a | 2013-09-02 07:59:30 +0000 | [diff] [blame] | 1580 | // assert(algorithm_buffer_->Channels() == 1); |
| 1581 | // if (algorithm_buffer_->Channels() != 1) { |
turaj@webrtc.org | 4d06db5 | 2013-03-27 18:31:42 +0000 | [diff] [blame] | 1582 | // LOG(LS_WARNING) << "DTMF not supported for more than one channel"; |
| 1583 | // return kStereoNotSupported; |
| 1584 | // } |
| 1585 | // // Shuffle the remaining data to the beginning of algorithm buffer. |
henrik.lundin@webrtc.org | c487c6a | 2013-09-02 07:59:30 +0000 | [diff] [blame] | 1586 | // algorithm_buffer_->PopFront(sync_buffer_->FutureLength()); |
turaj@webrtc.org | 4d06db5 | 2013-03-27 18:31:42 +0000 | [diff] [blame] | 1587 | // } |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 1588 | |
| 1589 | sync_buffer_->IncreaseEndTimestamp(output_size_samples_); |
| 1590 | expand_->Reset(); |
| 1591 | last_mode_ = kModeDtmf; |
| 1592 | |
| 1593 | // Set to false because the DTMF is already in the algorithm buffer. |
| 1594 | *play_dtmf = false; |
| 1595 | return 0; |
| 1596 | } |
| 1597 | |
henrik.lundin@webrtc.org | c487c6a | 2013-09-02 07:59:30 +0000 | [diff] [blame] | 1598 | void NetEqImpl::DoAlternativePlc(bool increase_timestamp) { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 1599 | AudioDecoder* decoder = decoder_database_->GetActiveDecoder(); |
| 1600 | int length; |
| 1601 | if (decoder && decoder->HasDecodePlc()) { |
| 1602 | // Use the decoder's packet-loss concealment. |
| 1603 | // TODO(hlundin): Will probably need a longer buffer for multi-channel. |
| 1604 | int16_t decoded_buffer[kMaxFrameSize]; |
| 1605 | length = decoder->DecodePlc(1, decoded_buffer); |
| 1606 | if (length > 0) { |
henrik.lundin@webrtc.org | c487c6a | 2013-09-02 07:59:30 +0000 | [diff] [blame] | 1607 | algorithm_buffer_->PushBackInterleaved(decoded_buffer, length); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 1608 | } else { |
| 1609 | length = 0; |
| 1610 | } |
| 1611 | } else { |
| 1612 | // Do simple zero-stuffing. |
| 1613 | length = output_size_samples_; |
henrik.lundin@webrtc.org | c487c6a | 2013-09-02 07:59:30 +0000 | [diff] [blame] | 1614 | algorithm_buffer_->Zeros(length); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 1615 | // By not advancing the timestamp, NetEq inserts samples. |
| 1616 | stats_.AddZeros(length); |
| 1617 | } |
| 1618 | if (increase_timestamp) { |
| 1619 | sync_buffer_->IncreaseEndTimestamp(length); |
| 1620 | } |
| 1621 | expand_->Reset(); |
| 1622 | } |
| 1623 | |
| 1624 | int NetEqImpl::DtmfOverdub(const DtmfEvent& dtmf_event, size_t num_channels, |
| 1625 | int16_t* output) const { |
| 1626 | size_t out_index = 0; |
| 1627 | int overdub_length = output_size_samples_; // Default value. |
| 1628 | |
| 1629 | if (sync_buffer_->dtmf_index() > sync_buffer_->next_index()) { |
| 1630 | // Special operation for transition from "DTMF only" to "DTMF overdub". |
| 1631 | out_index = std::min( |
| 1632 | sync_buffer_->dtmf_index() - sync_buffer_->next_index(), |
| 1633 | static_cast<size_t>(output_size_samples_)); |
turaj@webrtc.org | 362a55e | 2013-09-20 16:25:28 +0000 | [diff] [blame] | 1634 | overdub_length = output_size_samples_ - static_cast<int>(out_index); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 1635 | } |
| 1636 | |
| 1637 | AudioMultiVector<int16_t> dtmf_output(num_channels); |
| 1638 | int dtmf_return_value = 0; |
| 1639 | if (!dtmf_tone_generator_->initialized()) { |
| 1640 | dtmf_return_value = dtmf_tone_generator_->Init(fs_hz_, dtmf_event.event_no, |
| 1641 | dtmf_event.volume); |
| 1642 | } |
| 1643 | if (dtmf_return_value == 0) { |
| 1644 | dtmf_return_value = dtmf_tone_generator_->Generate(overdub_length, |
| 1645 | &dtmf_output); |
| 1646 | assert((size_t) overdub_length == dtmf_output.Size()); |
| 1647 | } |
| 1648 | dtmf_output.ReadInterleaved(overdub_length, &output[out_index]); |
| 1649 | return dtmf_return_value < 0 ? dtmf_return_value : 0; |
| 1650 | } |
| 1651 | |
| 1652 | int NetEqImpl::ExtractPackets(int required_samples, PacketList* packet_list) { |
| 1653 | bool first_packet = true; |
| 1654 | uint8_t prev_payload_type = 0; |
| 1655 | uint32_t prev_timestamp = 0; |
| 1656 | uint16_t prev_sequence_number = 0; |
| 1657 | bool next_packet_available = false; |
| 1658 | |
henrik.lundin@webrtc.org | e1d468c | 2013-01-30 07:37:20 +0000 | [diff] [blame] | 1659 | const RTPHeader* header = packet_buffer_->NextRtpHeader(); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 1660 | assert(header); |
| 1661 | if (!header) { |
| 1662 | return -1; |
| 1663 | } |
turaj@webrtc.org | 7df9706 | 2013-08-02 18:07:13 +0000 | [diff] [blame] | 1664 | uint32_t first_timestamp = header->timestamp; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 1665 | int extracted_samples = 0; |
| 1666 | |
| 1667 | // Packet extraction loop. |
| 1668 | do { |
| 1669 | timestamp_ = header->timestamp; |
| 1670 | int discard_count = 0; |
henrik.lundin@webrtc.org | e1d468c | 2013-01-30 07:37:20 +0000 | [diff] [blame] | 1671 | Packet* packet = packet_buffer_->GetNextPacket(&discard_count); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 1672 | // |header| may be invalid after the |packet_buffer_| operation. |
| 1673 | header = NULL; |
| 1674 | if (!packet) { |
| 1675 | LOG_FERR1(LS_ERROR, GetNextPacket, discard_count) << |
| 1676 | "Should always be able to extract a packet here"; |
| 1677 | assert(false); // Should always be able to extract a packet here. |
| 1678 | return -1; |
| 1679 | } |
| 1680 | stats_.PacketsDiscarded(discard_count); |
| 1681 | // Store waiting time in ms; packets->waiting_time is in "output blocks". |
| 1682 | stats_.StoreWaitingTime(packet->waiting_time * kOutputSizeMs); |
| 1683 | assert(packet->payload_length > 0); |
| 1684 | packet_list->push_back(packet); // Store packet in list. |
| 1685 | |
| 1686 | if (first_packet) { |
| 1687 | first_packet = false; |
minyue@webrtc.org | d730177 | 2013-08-29 00:58:14 +0000 | [diff] [blame] | 1688 | decoded_packet_sequence_number_ = prev_sequence_number = |
| 1689 | packet->header.sequenceNumber; |
| 1690 | decoded_packet_timestamp_ = prev_timestamp = packet->header.timestamp; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 1691 | prev_payload_type = packet->header.payloadType; |
| 1692 | } |
| 1693 | |
| 1694 | // Store number of extracted samples. |
| 1695 | int packet_duration = 0; |
| 1696 | AudioDecoder* decoder = decoder_database_->GetDecoder( |
| 1697 | packet->header.payloadType); |
| 1698 | if (decoder) { |
| 1699 | packet_duration = decoder->PacketDuration(packet->payload, |
| 1700 | packet->payload_length); |
| 1701 | } else { |
| 1702 | LOG_FERR1(LS_WARNING, GetDecoder, packet->header.payloadType) << |
| 1703 | "Could not find a decoder for a packet about to be extracted."; |
| 1704 | assert(false); |
| 1705 | } |
| 1706 | if (packet_duration <= 0) { |
| 1707 | // Decoder did not return a packet duration. Assume that the packet |
| 1708 | // contains the same number of samples as the previous one. |
| 1709 | packet_duration = decoder_frame_length_; |
| 1710 | } |
| 1711 | extracted_samples = packet->header.timestamp - first_timestamp + |
| 1712 | packet_duration; |
| 1713 | |
| 1714 | // Check what packet is available next. |
| 1715 | header = packet_buffer_->NextRtpHeader(); |
| 1716 | next_packet_available = false; |
| 1717 | if (header && prev_payload_type == header->payloadType) { |
| 1718 | int16_t seq_no_diff = header->sequenceNumber - prev_sequence_number; |
| 1719 | int32_t ts_diff = header->timestamp - prev_timestamp; |
| 1720 | if (seq_no_diff == 1 || |
| 1721 | (seq_no_diff == 0 && ts_diff == decoder_frame_length_)) { |
| 1722 | // The next sequence number is available, or the next part of a packet |
| 1723 | // that was split into pieces upon insertion. |
| 1724 | next_packet_available = true; |
| 1725 | } |
| 1726 | prev_sequence_number = header->sequenceNumber; |
| 1727 | } |
| 1728 | } while (extracted_samples < required_samples && next_packet_available); |
| 1729 | |
| 1730 | return extracted_samples; |
| 1731 | } |
| 1732 | |
| 1733 | void NetEqImpl::SetSampleRateAndChannels(int fs_hz, size_t channels) { |
| 1734 | LOG_API2(fs_hz, channels); |
| 1735 | // TODO(hlundin): Change to an enumerator and skip assert. |
| 1736 | assert(fs_hz == 8000 || fs_hz == 16000 || fs_hz == 32000 || fs_hz == 48000); |
| 1737 | assert(channels > 0); |
| 1738 | |
| 1739 | fs_hz_ = fs_hz; |
| 1740 | fs_mult_ = fs_hz / 8000; |
| 1741 | output_size_samples_ = kOutputSizeMs * 8 * fs_mult_; |
| 1742 | decoder_frame_length_ = 3 * output_size_samples_; // Initialize to 30ms. |
| 1743 | |
| 1744 | last_mode_ = kModeNormal; |
| 1745 | |
| 1746 | // Create a new array of mute factors and set all to 1. |
| 1747 | mute_factor_array_.reset(new int16_t[channels]); |
| 1748 | for (size_t i = 0; i < channels; ++i) { |
| 1749 | mute_factor_array_[i] = 16384; // 1.0 in Q14. |
| 1750 | } |
| 1751 | |
| 1752 | // Reset comfort noise decoder, if there is one active. |
| 1753 | AudioDecoder* cng_decoder = decoder_database_->GetActiveCngDecoder(); |
| 1754 | if (cng_decoder) { |
| 1755 | cng_decoder->Init(); |
| 1756 | } |
| 1757 | |
| 1758 | // Reinit post-decode VAD with new sample rate. |
| 1759 | assert(vad_.get()); // Cannot be NULL here. |
| 1760 | vad_->Init(); |
| 1761 | |
henrik.lundin@webrtc.org | c487c6a | 2013-09-02 07:59:30 +0000 | [diff] [blame] | 1762 | // Delete algorithm buffer and create a new one. |
henrik.lundin@webrtc.org | 0d5da25 | 2013-09-18 21:12:38 +0000 | [diff] [blame] | 1763 | algorithm_buffer_.reset(new AudioMultiVector<int16_t>(channels)); |
henrik.lundin@webrtc.org | c487c6a | 2013-09-02 07:59:30 +0000 | [diff] [blame] | 1764 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 1765 | // Delete sync buffer and create a new one. |
henrik.lundin@webrtc.org | 0d5da25 | 2013-09-18 21:12:38 +0000 | [diff] [blame] | 1766 | sync_buffer_.reset(new SyncBuffer(channels, kSyncBufferSize * fs_mult_)); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 1767 | |
turaj@webrtc.org | ff43c85 | 2013-09-25 00:07:27 +0000 | [diff] [blame] | 1768 | |
| 1769 | // Delete BackgroundNoise object and create a new one, while preserving its |
| 1770 | // mode. |
| 1771 | NetEqBackgroundNoiseMode current_mode = kBgnOn; |
| 1772 | if (background_noise_.get()) |
| 1773 | current_mode = background_noise_->mode(); |
henrik.lundin@webrtc.org | 0d5da25 | 2013-09-18 21:12:38 +0000 | [diff] [blame] | 1774 | background_noise_.reset(new BackgroundNoise(channels)); |
turaj@webrtc.org | ff43c85 | 2013-09-25 00:07:27 +0000 | [diff] [blame] | 1775 | background_noise_->set_mode(current_mode); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 1776 | |
| 1777 | // Reset random vector. |
| 1778 | random_vector_.Reset(); |
| 1779 | |
| 1780 | // Delete Expand object and create a new one. |
henrik.lundin@webrtc.org | 0d5da25 | 2013-09-18 21:12:38 +0000 | [diff] [blame] | 1781 | expand_.reset(new Expand(background_noise_.get(), sync_buffer_.get(), |
| 1782 | &random_vector_, fs_hz, channels)); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 1783 | // Move index so that we create a small set of future samples (all 0). |
| 1784 | sync_buffer_->set_next_index(sync_buffer_->next_index() - |
| 1785 | expand_->overlap_length()); |
| 1786 | |
henrik.lundin@webrtc.org | 40d3fc6 | 2013-09-18 12:19:50 +0000 | [diff] [blame] | 1787 | normal_.reset(new Normal(fs_hz, decoder_database_.get(), *background_noise_, |
henrik.lundin@webrtc.org | 0d5da25 | 2013-09-18 21:12:38 +0000 | [diff] [blame] | 1788 | expand_.get())); |
| 1789 | merge_.reset(new Merge(fs_hz, channels, expand_.get(), sync_buffer_.get())); |
henrik.lundin@webrtc.org | 40d3fc6 | 2013-09-18 12:19:50 +0000 | [diff] [blame] | 1790 | accelerate_.reset(new Accelerate(fs_hz, channels, *background_noise_)); |
| 1791 | preemptive_expand_.reset(new PreemptiveExpand(fs_hz, channels, |
| 1792 | *background_noise_)); |
| 1793 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 1794 | // Delete ComfortNoise object and create a new one. |
henrik.lundin@webrtc.org | 0d5da25 | 2013-09-18 21:12:38 +0000 | [diff] [blame] | 1795 | comfort_noise_.reset(new ComfortNoise(fs_hz, decoder_database_.get(), |
| 1796 | sync_buffer_.get())); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 1797 | |
| 1798 | // Verify that |decoded_buffer_| is long enough. |
| 1799 | if (decoded_buffer_length_ < kMaxFrameSize * channels) { |
| 1800 | // Reallocate to larger size. |
| 1801 | decoded_buffer_length_ = kMaxFrameSize * channels; |
| 1802 | decoded_buffer_.reset(new int16_t[decoded_buffer_length_]); |
| 1803 | } |
| 1804 | |
| 1805 | // Communicate new sample rate and output size to DecisionLogic object. |
| 1806 | assert(decision_logic_.get()); |
| 1807 | decision_logic_->SetSampleRate(fs_hz_, output_size_samples_); |
| 1808 | } |
| 1809 | |
| 1810 | NetEqOutputType NetEqImpl::LastOutputType() { |
| 1811 | assert(vad_.get()); |
henrik.lundin@webrtc.org | 0d5da25 | 2013-09-18 21:12:38 +0000 | [diff] [blame] | 1812 | assert(expand_.get()); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 1813 | if (last_mode_ == kModeCodecInternalCng || last_mode_ == kModeRfc3389Cng) { |
| 1814 | return kOutputCNG; |
| 1815 | } else if (vad_->running() && !vad_->active_speech()) { |
| 1816 | return kOutputVADPassive; |
| 1817 | } else if (last_mode_ == kModeExpand && expand_->MuteFactor(0) == 0) { |
| 1818 | // Expand mode has faded down to background noise only (very long expand). |
| 1819 | return kOutputPLCtoCNG; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 1820 | } else if (last_mode_ == kModeExpand) { |
| 1821 | return kOutputPLC; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 1822 | } else { |
| 1823 | return kOutputNormal; |
| 1824 | } |
| 1825 | } |
| 1826 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 1827 | } // namespace webrtc |