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