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