blob: 4ed3976434c31fa2543399e5fb271b924df2564e [file] [log] [blame]
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001/*
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
50namespace webrtc {
51
52NetEqImpl::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.orgc487c6a2013-09-02 07:59:30 +000073 algorithm_buffer_(NULL),
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000074 sync_buffer_(NULL),
75 expand_(NULL),
henrik.lundin@webrtc.org40d3fc62013-09-18 12:19:50 +000076 normal_(NULL),
77 merge_(NULL),
78 accelerate_(NULL),
79 preemptive_expand_(NULL),
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000080 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.orgd94659d2013-01-29 12:09:21 +000093 error_code_(0),
94 decoder_error_code_(0),
minyue@webrtc.orgd7301772013-08-29 00:58:14 +000095 crit_sect_(CriticalSectionWrapper::CreateCriticalSection()),
96 decoded_packet_sequence_number_(-1),
97 decoded_packet_timestamp_(0) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000098 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
118NetEqImpl::~NetEqImpl() {
119 LOG(LS_INFO) << "Deleting NetEqImpl object.";
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000120}
121
122int NetEqImpl::InsertPacket(const WebRtcRTPHeader& rtp_header,
123 const uint8_t* payload,
124 int length_bytes,
125 uint32_t receive_timestamp) {
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +0000126 CriticalSectionScoped lock(crit_sect_.get());
turaj@webrtc.org0c0fae82013-09-25 17:42:17 +0000127 LOG(LS_VERBOSE) << "InsertPacket: ts=" << rtp_header.header.timestamp <<
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000128 ", 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.org7b75ac62013-09-26 00:27:56 +0000133 receive_timestamp, false);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000134 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.org7b75ac62013-09-26 00:27:56 +0000142int 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.orgd94659d2013-01-29 12:09:21 +0000163int NetEqImpl::GetAudio(size_t max_length, int16_t* output_audio,
164 int* samples_per_channel, int* num_channels,
165 NetEqOutputType* type) {
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +0000166 CriticalSectionScoped lock(crit_sect_.get());
turaj@webrtc.org0c0fae82013-09-25 17:42:17 +0000167 LOG(LS_VERBOSE) << "GetAudio";
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000168 int error = GetAudioInternal(max_length, output_audio, samples_per_channel,
169 num_channels);
turaj@webrtc.org0c0fae82013-09-25 17:42:17 +0000170 LOG(LS_VERBOSE) << "Produced " << *samples_per_channel <<
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000171 " 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
183int NetEqImpl::RegisterPayloadType(enum NetEqDecoder codec,
184 uint8_t rtp_payload_type) {
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +0000185 CriticalSectionScoped lock(crit_sect_.get());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000186 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
208int NetEqImpl::RegisterExternalDecoder(AudioDecoder* decoder,
209 enum NetEqDecoder codec,
210 int sample_rate_hz,
211 uint8_t rtp_payload_type) {
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +0000212 CriticalSectionScoped lock(crit_sect_.get());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000213 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
247int NetEqImpl::RemovePayloadType(uint8_t rtp_payload_type) {
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +0000248 CriticalSectionScoped lock(crit_sect_.get());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000249 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.orgf1efc572013-08-16 23:44:24 +0000262bool NetEqImpl::SetMinimumDelay(int delay_ms) {
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +0000263 CriticalSectionScoped lock(crit_sect_.get());
turaj@webrtc.orgf1efc572013-08-16 23:44:24 +0000264 if (delay_ms >= 0 && delay_ms < 10000) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000265 assert(delay_manager_.get());
turaj@webrtc.orgf1efc572013-08-16 23:44:24 +0000266 return delay_manager_->SetMinimumDelay(delay_ms);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000267 }
268 return false;
269}
270
turaj@webrtc.orgf1efc572013-08-16 23:44:24 +0000271bool NetEqImpl::SetMaximumDelay(int delay_ms) {
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +0000272 CriticalSectionScoped lock(crit_sect_.get());
turaj@webrtc.orgf1efc572013-08-16 23:44:24 +0000273 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
280int NetEqImpl::LeastRequiredDelayMs() const {
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +0000281 CriticalSectionScoped lock(crit_sect_.get());
turaj@webrtc.orgf1efc572013-08-16 23:44:24 +0000282 assert(delay_manager_.get());
283 return delay_manager_->least_required_delay_ms();
284}
285
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000286void NetEqImpl::SetPlayoutMode(NetEqPlayoutMode mode) {
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +0000287 CriticalSectionScoped lock(crit_sect_.get());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000288 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
299NetEqPlayoutMode NetEqImpl::PlayoutMode() const {
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +0000300 CriticalSectionScoped lock(crit_sect_.get());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000301 assert(decision_logic_.get());
302 return decision_logic_->playout_mode();
303}
304
305int NetEqImpl::NetworkStatistics(NetEqNetworkStatistics* stats) {
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +0000306 CriticalSectionScoped lock(crit_sect_.get());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000307 assert(decoder_database_.get());
308 const int total_samples_in_buffers = packet_buffer_->NumSamplesInBuffer(
309 decoder_database_.get(), decoder_frame_length_) +
turaj@webrtc.org362a55e2013-09-20 16:25:28 +0000310 static_cast<int>(sync_buffer_->FutureLength());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000311 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
319void NetEqImpl::WaitingTimes(std::vector<int>* waiting_times) {
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +0000320 CriticalSectionScoped lock(crit_sect_.get());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000321 stats_.WaitingTimes(waiting_times);
322}
323
324void NetEqImpl::GetRtcpStatistics(RtcpStatistics* stats) {
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +0000325 CriticalSectionScoped lock(crit_sect_.get());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000326 if (stats) {
327 rtcp_.GetStatistics(false, stats);
328 }
329}
330
331void NetEqImpl::GetRtcpStatisticsNoReset(RtcpStatistics* stats) {
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +0000332 CriticalSectionScoped lock(crit_sect_.get());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000333 if (stats) {
334 rtcp_.GetStatistics(true, stats);
335 }
336}
337
338void NetEqImpl::EnableVad() {
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +0000339 CriticalSectionScoped lock(crit_sect_.get());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000340 assert(vad_.get());
341 vad_->Enable();
342}
343
344void NetEqImpl::DisableVad() {
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +0000345 CriticalSectionScoped lock(crit_sect_.get());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000346 assert(vad_.get());
347 vad_->Disable();
348}
349
350uint32_t NetEqImpl::PlayoutTimestamp() {
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +0000351 CriticalSectionScoped lock(crit_sect_.get());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000352 return timestamp_scaler_->ToExternal(playout_timestamp_);
353}
354
355int NetEqImpl::LastError() {
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +0000356 CriticalSectionScoped lock(crit_sect_.get());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000357 return error_code_;
358}
359
360int NetEqImpl::LastDecoderError() {
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +0000361 CriticalSectionScoped lock(crit_sect_.get());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000362 return decoder_error_code_;
363}
364
365void NetEqImpl::FlushBuffers() {
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +0000366 CriticalSectionScoped lock(crit_sect_.get());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000367 LOG_API0();
368 packet_buffer_->Flush();
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +0000369 assert(sync_buffer_.get());
370 assert(expand_.get());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000371 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.org3170b572013-08-30 15:36:53 +0000378void 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.org0d5da252013-09-18 21:12:38 +0000382 CriticalSectionScoped lock(crit_sect_.get());
turaj@webrtc.org3170b572013-08-30 15:36:53 +0000383 packet_buffer_->BufferStat(current_num_packets, max_num_packets,
384 current_memory_size_bytes, max_memory_size_bytes);
385}
386
turaj@webrtc.orgff43c852013-09-25 00:07:27 +0000387int NetEqImpl::DecodedRtpInfo(int* sequence_number, uint32_t* timestamp) const {
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +0000388 CriticalSectionScoped lock(crit_sect_.get());
minyue@webrtc.orgd7301772013-08-29 00:58:14 +0000389 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.orgff43c852013-09-25 00:07:27 +0000396void NetEqImpl::SetBackgroundNoiseMode(NetEqBackgroundNoiseMode mode) {
397 CriticalSectionScoped lock(crit_sect_.get());
398 assert(background_noise_.get());
399 background_noise_->set_mode(mode);
400}
turaj@webrtc.org036b7432013-09-11 18:45:02 +0000401
402NetEqBackgroundNoiseMode NetEqImpl::BackgroundNoiseMode() const {
turaj@webrtc.orgff43c852013-09-25 00:07:27 +0000403 CriticalSectionScoped lock(crit_sect_.get());
404 assert(background_noise_.get());
405 return background_noise_->mode();
turaj@webrtc.org036b7432013-09-11 18:45:02 +0000406}
407
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000408// Methods below this line are private.
409
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000410int NetEqImpl::InsertPacketInternal(const WebRtcRTPHeader& rtp_header,
411 const uint8_t* payload,
412 int length_bytes,
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +0000413 uint32_t receive_timestamp,
414 bool is_sync_packet) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000415 if (!payload) {
416 LOG_F(LS_ERROR) << "payload == NULL";
417 return kInvalidPointer;
418 }
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +0000419 // 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.orgd94659d2013-01-29 12:09:21 +0000438 PacketList packet_list;
439 RTPHeader main_header;
440 {
henrik.lundin@webrtc.orge1d468c2013-01-30 07:37:20 +0000441 // Convert to Packet.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000442 // 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.orge1d468c2013-01-30 07:37:20 +0000445 Packet* packet = new Packet;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000446 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.org7b75ac62013-09-26 00:27:56 +0000456 packet->sync_packet = is_sync_packet;
henrik.lundin@webrtc.org73deaad2013-01-31 13:32:51 +0000457 if (!packet->payload) {
458 LOG_F(LS_ERROR) << "Payload pointer is NULL.";
459 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000460 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
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000468 bool update_sample_rate_and_channels = false;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000469 // Reinitialize NetEq if it's needed (changed SSRC or first call).
470 if ((main_header.ssrc != ssrc_) || first_packet_) {
471 rtcp_.Init(main_header.sequenceNumber);
472 first_packet_ = false;
473
474 // Flush the packet buffer and DTMF buffer.
475 packet_buffer_->Flush();
476 dtmf_buffer_->Flush();
477
478 // Store new SSRC.
479 ssrc_ = main_header.ssrc;
480
turaj@webrtc.org4d06db52013-03-27 18:31:42 +0000481 // Update audio buffer timestamp.
482 sync_buffer_->IncreaseEndTimestamp(main_header.timestamp - timestamp_);
483
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000484 // Update codecs.
485 timestamp_ = main_header.timestamp;
486 current_rtp_payload_type_ = main_header.payloadType;
487
488 // Set MCU to update codec on next SignalMCU call.
489 new_codec_ = true;
490
491 // Reset timestamp scaling.
492 timestamp_scaler_->Reset();
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000493
494 // Triger an update of sampling rate and the number of channels.
495 update_sample_rate_and_channels = true;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000496 }
497
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +0000498 // Update RTCP statistics, only for regular packets.
499 if (!is_sync_packet)
500 rtcp_.Update(main_header, receive_timestamp);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000501
502 // Check for RED payload type, and separate payloads into several packets.
503 if (decoder_database_->IsRed(main_header.payloadType)) {
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +0000504 assert(!is_sync_packet); // We had a sanity check for this.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000505 if (payload_splitter_->SplitRed(&packet_list) != PayloadSplitter::kOK) {
506 LOG_FERR1(LS_WARNING, SplitRed, packet_list.size());
507 PacketBuffer::DeleteAllPackets(&packet_list);
508 return kRedundancySplitError;
509 }
510 // Only accept a few RED payloads of the same type as the main data,
511 // DTMF events and CNG.
512 payload_splitter_->CheckRedPayloads(&packet_list, *decoder_database_);
513 // Update the stored main payload header since the main payload has now
514 // changed.
515 memcpy(&main_header, &packet_list.front()->header, sizeof(main_header));
516 }
517
518 // Check payload types.
519 if (decoder_database_->CheckPayloadTypes(packet_list) ==
520 DecoderDatabase::kDecoderNotFound) {
521 LOG_FERR1(LS_WARNING, CheckPayloadTypes, packet_list.size());
522 PacketBuffer::DeleteAllPackets(&packet_list);
523 return kUnknownRtpPayloadType;
524 }
525
526 // Scale timestamp to internal domain (only for some codecs).
527 timestamp_scaler_->ToInternal(&packet_list);
528
529 // Process DTMF payloads. Cycle through the list of packets, and pick out any
530 // DTMF payloads found.
531 PacketList::iterator it = packet_list.begin();
532 while (it != packet_list.end()) {
533 Packet* current_packet = (*it);
534 assert(current_packet);
535 assert(current_packet->payload);
536 if (decoder_database_->IsDtmf(current_packet->header.payloadType)) {
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +0000537 assert(!current_packet->sync_packet); // We had a sanity check for this.
minyue@webrtc.org9721db72013-08-06 05:36:26 +0000538 DtmfEvent event;
539 int ret = DtmfBuffer::ParseEvent(
540 current_packet->header.timestamp,
541 current_packet->payload,
542 current_packet->payload_length,
543 &event);
544 if (ret != DtmfBuffer::kOK) {
545 LOG_FERR2(LS_WARNING, ParseEvent, ret,
546 current_packet->payload_length);
547 PacketBuffer::DeleteAllPackets(&packet_list);
548 return kDtmfParsingError;
549 }
550 if (dtmf_buffer_->InsertEvent(event) != DtmfBuffer::kOK) {
551 LOG_FERR0(LS_WARNING, InsertEvent);
552 PacketBuffer::DeleteAllPackets(&packet_list);
553 return kDtmfInsertError;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000554 }
555 // TODO(hlundin): Let the destructor of Packet handle the payload.
556 delete [] current_packet->payload;
557 delete current_packet;
558 it = packet_list.erase(it);
559 } else {
560 ++it;
561 }
562 }
563
564 // Split payloads into smaller chunks. This also verifies that all payloads
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +0000565 // are of a known payload type. SplitAudio() method is protected against
566 // sync-packets.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000567 int ret = payload_splitter_->SplitAudio(&packet_list, *decoder_database_);
568 if (ret != PayloadSplitter::kOK) {
569 LOG_FERR1(LS_WARNING, SplitAudio, packet_list.size());
570 PacketBuffer::DeleteAllPackets(&packet_list);
571 switch (ret) {
572 case PayloadSplitter::kUnknownPayloadType:
573 return kUnknownRtpPayloadType;
574 case PayloadSplitter::kFrameSplitError:
575 return kFrameSplitError;
576 default:
577 return kOtherError;
578 }
579 }
580
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +0000581 // Update bandwidth estimate, if the packet is not sync-packet.
582 if (!packet_list.empty() && !packet_list.front()->sync_packet) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000583 // The list can be empty here if we got nothing but DTMF payloads.
584 AudioDecoder* decoder =
585 decoder_database_->GetDecoder(main_header.payloadType);
586 assert(decoder); // Should always get a valid object, since we have
587 // already checked that the payload types are known.
588 decoder->IncomingPacket(packet_list.front()->payload,
589 packet_list.front()->payload_length,
590 packet_list.front()->header.sequenceNumber,
591 packet_list.front()->header.timestamp,
592 receive_timestamp);
593 }
594
595 // Insert packets in buffer.
596 int temp_bufsize = packet_buffer_->NumPacketsInBuffer();
597 ret = packet_buffer_->InsertPacketList(
598 &packet_list,
599 *decoder_database_,
600 &current_rtp_payload_type_,
601 &current_cng_rtp_payload_type_);
602 if (ret == PacketBuffer::kFlushed) {
603 // Reset DSP timestamp etc. if packet buffer flushed.
604 new_codec_ = true;
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000605 update_sample_rate_and_channels = true;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000606 LOG_F(LS_WARNING) << "Packet buffer flushed";
minyue@webrtc.org7bb54362013-08-06 05:40:57 +0000607 } else if (ret == PacketBuffer::kOversizePacket) {
608 LOG_F(LS_WARNING) << "Packet larger than packet buffer";
609 return kOversizePacket;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000610 } else if (ret != PacketBuffer::kOK) {
611 LOG_FERR1(LS_WARNING, InsertPacketList, packet_list.size());
612 PacketBuffer::DeleteAllPackets(&packet_list);
minyue@webrtc.org7bb54362013-08-06 05:40:57 +0000613 return kOtherError;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000614 }
615 if (current_rtp_payload_type_ != 0xFF) {
616 const DecoderDatabase::DecoderInfo* dec_info =
617 decoder_database_->GetDecoderInfo(current_rtp_payload_type_);
618 if (!dec_info) {
619 assert(false); // Already checked that the payload type is known.
620 }
621 }
622
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000623 if (update_sample_rate_and_channels && !packet_buffer_->Empty()) {
624 // We do not use |current_rtp_payload_type_| to |set payload_type|, but
625 // get the next RTP header from |packet_buffer_| to obtain the payload type.
626 // The reason for it is the following corner case. If NetEq receives a
627 // CNG packet with a sample rate different than the current CNG then it
628 // flushes its buffer, assuming send codec must have been changed. However,
629 // payload type of the hypothetically new send codec is not known.
630 const RTPHeader* rtp_header = packet_buffer_->NextRtpHeader();
631 assert(rtp_header);
632 int payload_type = rtp_header->payloadType;
633 AudioDecoder* decoder = decoder_database_->GetDecoder(payload_type);
634 assert(decoder); // Payloads are already checked to be valid.
635 const DecoderDatabase::DecoderInfo* decoder_info =
636 decoder_database_->GetDecoderInfo(payload_type);
637 assert(decoder_info);
638 if (decoder_info->fs_hz != fs_hz_ ||
639 decoder->channels() != algorithm_buffer_->Channels())
640 SetSampleRateAndChannels(decoder_info->fs_hz, decoder->channels());
641 }
642
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000643 // TODO(hlundin): Move this code to DelayManager class.
644 const DecoderDatabase::DecoderInfo* dec_info =
645 decoder_database_->GetDecoderInfo(main_header.payloadType);
646 assert(dec_info); // Already checked that the payload type is known.
647 delay_manager_->LastDecoderType(dec_info->codec_type);
648 if (delay_manager_->last_pack_cng_or_dtmf() == 0) {
649 // Calculate the total speech length carried in each packet.
650 temp_bufsize = packet_buffer_->NumPacketsInBuffer() - temp_bufsize;
651 temp_bufsize *= decoder_frame_length_;
652
653 if ((temp_bufsize > 0) &&
654 (temp_bufsize != decision_logic_->packet_length_samples())) {
655 decision_logic_->set_packet_length_samples(temp_bufsize);
656 delay_manager_->SetPacketAudioLength((1000 * temp_bufsize) / fs_hz_);
657 }
658
659 // Update statistics.
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000660 if ((int32_t) (main_header.timestamp - timestamp_) >= 0 &&
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000661 !new_codec_) {
662 // Only update statistics if incoming packet is not older than last played
663 // out packet, and if new codec flag is not set.
664 delay_manager_->Update(main_header.sequenceNumber, main_header.timestamp,
665 fs_hz_);
666 }
667 } else if (delay_manager_->last_pack_cng_or_dtmf() == -1) {
668 // This is first "normal" packet after CNG or DTMF.
669 // Reset packet time counter and measure time until next packet,
670 // but don't update statistics.
671 delay_manager_->set_last_pack_cng_or_dtmf(0);
672 delay_manager_->ResetPacketIatCount();
673 }
674 return 0;
675}
676
677int NetEqImpl::GetAudioInternal(size_t max_length, int16_t* output,
678 int* samples_per_channel, int* num_channels) {
679 PacketList packet_list;
680 DtmfEvent dtmf_event;
681 Operations operation;
682 bool play_dtmf;
683 int return_value = GetDecision(&operation, &packet_list, &dtmf_event,
684 &play_dtmf);
685 if (return_value != 0) {
686 LOG_FERR1(LS_WARNING, GetDecision, return_value);
687 assert(false);
688 last_mode_ = kModeError;
689 return return_value;
690 }
turaj@webrtc.org0c0fae82013-09-25 17:42:17 +0000691 LOG(LS_VERBOSE) << "GetDecision returned operation=" << operation <<
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000692 " and " << packet_list.size() << " packet(s)";
693
694 AudioDecoder::SpeechType speech_type;
695 int length = 0;
696 int decode_return_value = Decode(&packet_list, &operation,
697 &length, &speech_type);
698
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000699 assert(vad_.get());
700 bool sid_frame_available =
701 (operation == kRfc3389Cng && !packet_list.empty());
702 vad_->Update(decoded_buffer_.get(), length, speech_type,
703 sid_frame_available, fs_hz_);
704
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +0000705 algorithm_buffer_->Clear();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000706 switch (operation) {
707 case kNormal: {
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +0000708 DoNormal(decoded_buffer_.get(), length, speech_type, play_dtmf);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000709 break;
710 }
711 case kMerge: {
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +0000712 DoMerge(decoded_buffer_.get(), length, speech_type, play_dtmf);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000713 break;
714 }
715 case kExpand: {
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +0000716 return_value = DoExpand(play_dtmf);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000717 break;
718 }
719 case kAccelerate: {
720 return_value = DoAccelerate(decoded_buffer_.get(), length, speech_type,
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +0000721 play_dtmf);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000722 break;
723 }
724 case kPreemptiveExpand: {
725 return_value = DoPreemptiveExpand(decoded_buffer_.get(), length,
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +0000726 speech_type, play_dtmf);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000727 break;
728 }
729 case kRfc3389Cng:
730 case kRfc3389CngNoPacket: {
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +0000731 return_value = DoRfc3389Cng(&packet_list, play_dtmf);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000732 break;
733 }
734 case kCodecInternalCng: {
735 // This handles the case when there is no transmission and the decoder
736 // should produce internal comfort noise.
737 // TODO(hlundin): Write test for codec-internal CNG.
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +0000738 DoCodecInternalCng();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000739 break;
740 }
741 case kDtmf: {
742 // TODO(hlundin): Write test for this.
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +0000743 return_value = DoDtmf(dtmf_event, &play_dtmf);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000744 break;
745 }
746 case kAlternativePlc: {
747 // TODO(hlundin): Write test for this.
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +0000748 DoAlternativePlc(false);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000749 break;
750 }
751 case kAlternativePlcIncreaseTimestamp: {
752 // TODO(hlundin): Write test for this.
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +0000753 DoAlternativePlc(true);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000754 break;
755 }
756 case kAudioRepetitionIncreaseTimestamp: {
757 // TODO(hlundin): Write test for this.
758 sync_buffer_->IncreaseEndTimestamp(output_size_samples_);
759 // Skipping break on purpose. Execution should move on into the
760 // next case.
761 }
762 case kAudioRepetition: {
763 // TODO(hlundin): Write test for this.
764 // Copy last |output_size_samples_| from |sync_buffer_| to
765 // |algorithm_buffer|.
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +0000766 algorithm_buffer_->PushBackFromIndex(
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000767 *sync_buffer_, sync_buffer_->Size() - output_size_samples_);
768 expand_->Reset();
769 break;
770 }
771 case kUndefined: {
772 LOG_F(LS_ERROR) << "Invalid operation kUndefined.";
773 assert(false); // This should not happen.
774 last_mode_ = kModeError;
775 return kInvalidOperation;
776 }
777 } // End of switch.
778 if (return_value < 0) {
779 return return_value;
780 }
781
782 if (last_mode_ != kModeRfc3389Cng) {
783 comfort_noise_->Reset();
784 }
785
786 // Copy from |algorithm_buffer| to |sync_buffer_|.
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +0000787 sync_buffer_->PushBack(*algorithm_buffer_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000788
789 // Extract data from |sync_buffer_| to |output|.
turaj@webrtc.org362a55e2013-09-20 16:25:28 +0000790 size_t num_output_samples_per_channel = output_size_samples_;
791 size_t num_output_samples = output_size_samples_ * sync_buffer_->Channels();
792 if (num_output_samples > max_length) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000793 LOG(LS_WARNING) << "Output array is too short. " << max_length << " < " <<
794 output_size_samples_ << " * " << sync_buffer_->Channels();
795 num_output_samples = max_length;
turaj@webrtc.org362a55e2013-09-20 16:25:28 +0000796 num_output_samples_per_channel = static_cast<int>(
797 max_length / sync_buffer_->Channels());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000798 }
turaj@webrtc.org362a55e2013-09-20 16:25:28 +0000799 int samples_from_sync = static_cast<int>(
800 sync_buffer_->GetNextAudioInterleaved(num_output_samples_per_channel,
801 output));
802 *num_channels = static_cast<int>(sync_buffer_->Channels());
turaj@webrtc.org0c0fae82013-09-25 17:42:17 +0000803 LOG(LS_VERBOSE) << "Sync buffer (" << *num_channels << " channel(s)):" <<
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +0000804 " insert " << algorithm_buffer_->Size() << " samples, extract " <<
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000805 samples_from_sync << " samples";
806 if (samples_from_sync != output_size_samples_) {
807 LOG_F(LS_ERROR) << "samples_from_sync != output_size_samples_";
minyue@webrtc.orgdb1cefc2013-08-13 01:39:21 +0000808 // TODO(minyue): treatment of under-run, filling zeros
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000809 memset(output, 0, num_output_samples * sizeof(int16_t));
810 *samples_per_channel = output_size_samples_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000811 return kSampleUnderrun;
812 }
813 *samples_per_channel = output_size_samples_;
814
815 // Should always have overlap samples left in the |sync_buffer_|.
816 assert(sync_buffer_->FutureLength() >= expand_->overlap_length());
817
818 if (play_dtmf) {
819 return_value = DtmfOverdub(dtmf_event, sync_buffer_->Channels(), output);
820 }
821
822 // Update the background noise parameters if last operation wrote data
823 // straight from the decoder to the |sync_buffer_|. That is, none of the
824 // operations that modify the signal can be followed by a parameter update.
825 if ((last_mode_ == kModeNormal) ||
826 (last_mode_ == kModeAccelerateFail) ||
827 (last_mode_ == kModePreemptiveExpandFail) ||
828 (last_mode_ == kModeRfc3389Cng) ||
829 (last_mode_ == kModeCodecInternalCng)) {
830 background_noise_->Update(*sync_buffer_, *vad_.get());
831 }
832
833 if (operation == kDtmf) {
834 // DTMF data was written the end of |sync_buffer_|.
835 // Update index to end of DTMF data in |sync_buffer_|.
836 sync_buffer_->set_dtmf_index(sync_buffer_->Size());
837 }
838
839 if ((last_mode_ != kModeExpand) && (last_mode_ != kModeRfc3389Cng)) {
840 // If last operation was neither expand, nor comfort noise, calculate the
841 // |playout_timestamp_| from the |sync_buffer_|. However, do not update the
842 // |playout_timestamp_| if it would be moved "backwards".
843 uint32_t temp_timestamp = sync_buffer_->end_timestamp() -
turaj@webrtc.org362a55e2013-09-20 16:25:28 +0000844 static_cast<uint32_t>(sync_buffer_->FutureLength());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000845 if (static_cast<int32_t>(temp_timestamp - playout_timestamp_) > 0) {
846 playout_timestamp_ = temp_timestamp;
847 }
848 } else {
849 // Use dead reckoning to estimate the |playout_timestamp_|.
850 playout_timestamp_ += output_size_samples_;
851 }
852
853 if (decode_return_value) return decode_return_value;
854 return return_value;
855}
856
857int NetEqImpl::GetDecision(Operations* operation,
858 PacketList* packet_list,
859 DtmfEvent* dtmf_event,
860 bool* play_dtmf) {
861 // Initialize output variables.
862 *play_dtmf = false;
863 *operation = kUndefined;
864
865 // Increment time counters.
866 packet_buffer_->IncrementWaitingTimes();
867 stats_.IncreaseCounter(output_size_samples_, fs_hz_);
868
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +0000869 assert(sync_buffer_.get());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000870 uint32_t end_timestamp = sync_buffer_->end_timestamp();
871 if (!new_codec_) {
872 packet_buffer_->DiscardOldPackets(end_timestamp);
873 }
874 const RTPHeader* header = packet_buffer_->NextRtpHeader();
875
876 if (decision_logic_->CngRfc3389On()) {
877 // Because of timestamp peculiarities, we have to "manually" disallow using
878 // a CNG packet with the same timestamp as the one that was last played.
879 // This can happen when using redundancy and will cause the timing to shift.
880 while (header &&
881 decoder_database_->IsComfortNoise(header->payloadType) &&
882 end_timestamp >= header->timestamp) {
883 // Don't use this packet, discard it.
884 // TODO(hlundin): Write test for this case.
885 if (packet_buffer_->DiscardNextPacket() != PacketBuffer::kOK) {
886 assert(false); // Must be ok by design.
887 }
888 // Check buffer again.
889 if (!new_codec_) {
890 packet_buffer_->DiscardOldPackets(end_timestamp);
891 }
892 header = packet_buffer_->NextRtpHeader();
893 }
894 }
895
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +0000896 assert(expand_.get());
turaj@webrtc.org362a55e2013-09-20 16:25:28 +0000897 const int samples_left = static_cast<int>(sync_buffer_->FutureLength() -
898 expand_->overlap_length());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000899 if (last_mode_ == kModeAccelerateSuccess ||
900 last_mode_ == kModeAccelerateLowEnergy ||
901 last_mode_ == kModePreemptiveExpandSuccess ||
902 last_mode_ == kModePreemptiveExpandLowEnergy) {
903 // Subtract (samples_left + output_size_samples_) from sampleMemory.
904 decision_logic_->AddSampleMemory(-(samples_left + output_size_samples_));
905 }
906
907 // Check if it is time to play a DTMF event.
908 if (dtmf_buffer_->GetEvent(end_timestamp +
909 decision_logic_->generated_noise_samples(),
910 dtmf_event)) {
911 *play_dtmf = true;
912 }
913
914 // Get instruction.
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +0000915 assert(sync_buffer_.get());
916 assert(expand_.get());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000917 *operation = decision_logic_->GetDecision(*sync_buffer_,
918 *expand_,
919 decoder_frame_length_,
920 header,
921 last_mode_,
922 *play_dtmf,
923 &reset_decoder_);
924
925 // Check if we already have enough samples in the |sync_buffer_|. If so,
926 // change decision to normal, unless the decision was merge, accelerate, or
927 // preemptive expand.
928 if (samples_left >= output_size_samples_ &&
929 *operation != kMerge &&
930 *operation != kAccelerate &&
931 *operation != kPreemptiveExpand) {
932 *operation = kNormal;
933 return 0;
934 }
935
936 decision_logic_->ExpandDecision(*operation == kExpand);
937
938 // Check conditions for reset.
939 if (new_codec_ || *operation == kUndefined) {
940 // The only valid reason to get kUndefined is that new_codec_ is set.
941 assert(new_codec_);
turaj@webrtc.org4d06db52013-03-27 18:31:42 +0000942 if (*play_dtmf && !header) {
943 timestamp_ = dtmf_event->timestamp;
944 } else {
945 assert(header);
946 if (!header) {
947 LOG_F(LS_ERROR) << "Packet missing where it shouldn't.";
948 return -1;
949 }
950 timestamp_ = header->timestamp;
951 if (*operation == kRfc3389CngNoPacket
952#ifndef LEGACY_BITEXACT
953 // Without this check, it can happen that a non-CNG packet is sent to
954 // the CNG decoder as if it was a SID frame. This is clearly a bug,
955 // but is kept for now to maintain bit-exactness with the test
956 // vectors.
957 && decoder_database_->IsComfortNoise(header->payloadType)
958#endif
959 ) {
960 // Change decision to CNG packet, since we do have a CNG packet, but it
961 // was considered too early to use. Now, use it anyway.
962 *operation = kRfc3389Cng;
963 } else if (*operation != kRfc3389Cng) {
964 *operation = kNormal;
965 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000966 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000967 // Adjust |sync_buffer_| timestamp before setting |end_timestamp| to the
968 // new value.
969 sync_buffer_->IncreaseEndTimestamp(timestamp_ - end_timestamp);
turaj@webrtc.org4d06db52013-03-27 18:31:42 +0000970 end_timestamp = timestamp_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000971 new_codec_ = false;
972 decision_logic_->SoftReset();
973 buffer_level_filter_->Reset();
974 delay_manager_->Reset();
975 stats_.ResetMcu();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000976 }
977
978 int required_samples = output_size_samples_;
979 const int samples_10_ms = 80 * fs_mult_;
980 const int samples_20_ms = 2 * samples_10_ms;
981 const int samples_30_ms = 3 * samples_10_ms;
982
983 switch (*operation) {
984 case kExpand: {
985 timestamp_ = end_timestamp;
986 return 0;
987 }
988 case kRfc3389CngNoPacket:
989 case kCodecInternalCng: {
990 return 0;
991 }
992 case kDtmf: {
993 // TODO(hlundin): Write test for this.
994 // Update timestamp.
995 timestamp_ = end_timestamp;
996 if (decision_logic_->generated_noise_samples() > 0 &&
997 last_mode_ != kModeDtmf) {
998 // Make a jump in timestamp due to the recently played comfort noise.
999 uint32_t timestamp_jump = decision_logic_->generated_noise_samples();
1000 sync_buffer_->IncreaseEndTimestamp(timestamp_jump);
1001 timestamp_ += timestamp_jump;
1002 }
1003 decision_logic_->set_generated_noise_samples(0);
1004 return 0;
1005 }
1006 case kAccelerate: {
1007 // In order to do a accelerate we need at least 30 ms of audio data.
1008 if (samples_left >= samples_30_ms) {
1009 // Already have enough data, so we do not need to extract any more.
1010 decision_logic_->set_sample_memory(samples_left);
1011 decision_logic_->set_prev_time_scale(true);
1012 return 0;
1013 } else if (samples_left >= samples_10_ms &&
1014 decoder_frame_length_ >= samples_30_ms) {
1015 // Avoid decoding more data as it might overflow the playout buffer.
1016 *operation = kNormal;
1017 return 0;
1018 } else if (samples_left < samples_20_ms &&
1019 decoder_frame_length_ < samples_30_ms) {
1020 // Build up decoded data by decoding at least 20 ms of audio data. Do
1021 // not perform accelerate yet, but wait until we only need to do one
1022 // decoding.
1023 required_samples = 2 * output_size_samples_;
1024 *operation = kNormal;
1025 }
1026 // If none of the above is true, we have one of two possible situations:
1027 // (1) 20 ms <= samples_left < 30 ms and decoder_frame_length_ < 30 ms; or
1028 // (2) samples_left < 10 ms and decoder_frame_length_ >= 30 ms.
1029 // In either case, we move on with the accelerate decision, and decode one
1030 // frame now.
1031 break;
1032 }
1033 case kPreemptiveExpand: {
1034 // In order to do a preemptive expand we need at least 30 ms of decoded
1035 // audio data.
1036 if ((samples_left >= samples_30_ms) ||
1037 (samples_left >= samples_10_ms &&
1038 decoder_frame_length_ >= samples_30_ms)) {
1039 // Already have enough data, so we do not need to extract any more.
1040 // Or, avoid decoding more data as it might overflow the playout buffer.
1041 // Still try preemptive expand, though.
1042 decision_logic_->set_sample_memory(samples_left);
1043 decision_logic_->set_prev_time_scale(true);
1044 return 0;
1045 }
1046 if (samples_left < samples_20_ms &&
1047 decoder_frame_length_ < samples_30_ms) {
1048 // Build up decoded data by decoding at least 20 ms of audio data.
1049 // Still try to perform preemptive expand.
1050 required_samples = 2 * output_size_samples_;
1051 }
1052 // Move on with the preemptive expand decision.
1053 break;
1054 }
1055 default: {
1056 // Do nothing.
1057 }
1058 }
1059
1060 // Get packets from buffer.
1061 int extracted_samples = 0;
1062 if (header &&
1063 *operation != kAlternativePlc &&
1064 *operation != kAlternativePlcIncreaseTimestamp &&
1065 *operation != kAudioRepetition &&
1066 *operation != kAudioRepetitionIncreaseTimestamp) {
1067 sync_buffer_->IncreaseEndTimestamp(header->timestamp - end_timestamp);
1068 if (decision_logic_->CngOff()) {
1069 // Adjustment of timestamp only corresponds to an actual packet loss
1070 // if comfort noise is not played. If comfort noise was just played,
1071 // this adjustment of timestamp is only done to get back in sync with the
1072 // stream timestamp; no loss to report.
1073 stats_.LostSamples(header->timestamp - end_timestamp);
1074 }
1075
1076 if (*operation != kRfc3389Cng) {
1077 // We are about to decode and use a non-CNG packet.
1078 decision_logic_->SetCngOff();
1079 }
1080 // Reset CNG timestamp as a new packet will be delivered.
1081 // (Also if this is a CNG packet, since playedOutTS is updated.)
1082 decision_logic_->set_generated_noise_samples(0);
1083
1084 extracted_samples = ExtractPackets(required_samples, packet_list);
1085 if (extracted_samples < 0) {
1086 LOG_F(LS_WARNING) << "Failed to extract packets from buffer.";
1087 return kPacketBufferCorruption;
1088 }
1089 }
1090
1091 if (*operation == kAccelerate ||
1092 *operation == kPreemptiveExpand) {
1093 decision_logic_->set_sample_memory(samples_left + extracted_samples);
1094 decision_logic_->set_prev_time_scale(true);
1095 }
1096
1097 if (*operation == kAccelerate) {
1098 // Check that we have enough data (30ms) to do accelerate.
1099 if (extracted_samples + samples_left < samples_30_ms) {
1100 // TODO(hlundin): Write test for this.
1101 // Not enough, do normal operation instead.
1102 *operation = kNormal;
1103 }
1104 }
1105
1106 timestamp_ = end_timestamp;
1107 return 0;
1108}
1109
1110int NetEqImpl::Decode(PacketList* packet_list, Operations* operation,
1111 int* decoded_length,
1112 AudioDecoder::SpeechType* speech_type) {
1113 *speech_type = AudioDecoder::kSpeech;
1114 AudioDecoder* decoder = NULL;
1115 if (!packet_list->empty()) {
1116 const Packet* packet = packet_list->front();
1117 int payload_type = packet->header.payloadType;
1118 if (!decoder_database_->IsComfortNoise(payload_type)) {
1119 decoder = decoder_database_->GetDecoder(payload_type);
1120 assert(decoder);
1121 if (!decoder) {
1122 LOG_FERR1(LS_WARNING, GetDecoder, payload_type);
1123 PacketBuffer::DeleteAllPackets(packet_list);
1124 return kDecoderNotFound;
1125 }
1126 bool decoder_changed;
1127 decoder_database_->SetActiveDecoder(payload_type, &decoder_changed);
1128 if (decoder_changed) {
1129 // We have a new decoder. Re-init some values.
1130 const DecoderDatabase::DecoderInfo* decoder_info = decoder_database_
1131 ->GetDecoderInfo(payload_type);
1132 assert(decoder_info);
1133 if (!decoder_info) {
1134 LOG_FERR1(LS_WARNING, GetDecoderInfo, payload_type);
1135 PacketBuffer::DeleteAllPackets(packet_list);
1136 return kDecoderNotFound;
1137 }
turaj@webrtc.orga6101d72013-10-01 22:01:09 +00001138 // We should have correct sampling rate and number of channels. They
1139 // are set when packets are inserted.
1140 if (decoder_info->fs_hz != fs_hz_ ||
1141 decoder->channels() != algorithm_buffer_->Channels()) {
1142 LOG_F(LS_ERROR) << "Sampling rate or number of channels mismatch.";
1143 assert(false);
1144 SetSampleRateAndChannels(decoder_info->fs_hz, decoder->channels());
1145 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001146 sync_buffer_->set_end_timestamp(timestamp_);
1147 playout_timestamp_ = timestamp_;
1148 }
1149 }
1150 }
1151
1152 if (reset_decoder_) {
1153 // TODO(hlundin): Write test for this.
1154 // Reset decoder.
1155 if (decoder) {
1156 decoder->Init();
1157 }
1158 // Reset comfort noise decoder.
1159 AudioDecoder* cng_decoder = decoder_database_->GetActiveCngDecoder();
1160 if (cng_decoder) {
1161 cng_decoder->Init();
1162 }
1163 reset_decoder_ = false;
1164 }
1165
1166#ifdef LEGACY_BITEXACT
1167 // Due to a bug in old SignalMCU, it could happen that CNG operation was
1168 // decided, but a speech packet was provided. The speech packet will be used
1169 // to update the comfort noise decoder, as if it was a SID frame, which is
1170 // clearly wrong.
1171 if (*operation == kRfc3389Cng) {
1172 return 0;
1173 }
1174#endif
1175
1176 *decoded_length = 0;
1177 // Update codec-internal PLC state.
1178 if ((*operation == kMerge) && decoder && decoder->HasDecodePlc()) {
1179 decoder->DecodePlc(1, &decoded_buffer_[*decoded_length]);
1180 }
1181
1182 int return_value = DecodeLoop(packet_list, operation, decoder,
1183 decoded_length, speech_type);
1184
1185 if (*decoded_length < 0) {
1186 // Error returned from the decoder.
1187 *decoded_length = 0;
1188 sync_buffer_->IncreaseEndTimestamp(decoder_frame_length_);
1189 int error_code = 0;
1190 if (decoder)
1191 error_code = decoder->ErrorCode();
1192 if (error_code != 0) {
1193 // Got some error code from the decoder.
1194 decoder_error_code_ = error_code;
1195 return_value = kDecoderErrorCode;
1196 } else {
1197 // Decoder does not implement error codes. Return generic error.
1198 return_value = kOtherDecoderError;
1199 }
1200 LOG_FERR2(LS_WARNING, DecodeLoop, error_code, packet_list->size());
1201 *operation = kExpand; // Do expansion to get data instead.
1202 }
1203 if (*speech_type != AudioDecoder::kComfortNoise) {
1204 // Don't increment timestamp if codec returned CNG speech type
1205 // since in this case, the we will increment the CNGplayedTS counter.
1206 // Increase with number of samples per channel.
1207 assert(*decoded_length == 0 ||
1208 (decoder && decoder->channels() == sync_buffer_->Channels()));
turaj@webrtc.org362a55e2013-09-20 16:25:28 +00001209 sync_buffer_->IncreaseEndTimestamp(
1210 *decoded_length / static_cast<int>(sync_buffer_->Channels()));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001211 }
1212 return return_value;
1213}
1214
1215int NetEqImpl::DecodeLoop(PacketList* packet_list, Operations* operation,
1216 AudioDecoder* decoder, int* decoded_length,
1217 AudioDecoder::SpeechType* speech_type) {
1218 Packet* packet = NULL;
1219 if (!packet_list->empty()) {
1220 packet = packet_list->front();
1221 }
1222 // Do decoding.
1223 while (packet &&
1224 !decoder_database_->IsComfortNoise(packet->header.payloadType)) {
1225 assert(decoder); // At this point, we must have a decoder object.
1226 // The number of channels in the |sync_buffer_| should be the same as the
1227 // number decoder channels.
1228 assert(sync_buffer_->Channels() == decoder->channels());
1229 assert(decoded_buffer_length_ >= kMaxFrameSize * decoder->channels());
1230 assert(*operation == kNormal || *operation == kAccelerate ||
1231 *operation == kMerge || *operation == kPreemptiveExpand);
1232 packet_list->pop_front();
henrik.lundin@webrtc.org63464a92013-01-30 09:41:56 +00001233 int payload_length = packet->payload_length;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001234 int16_t decode_length;
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001235 if (packet->sync_packet) {
1236 // Decode to silence with the same frame size as the last decode.
1237 LOG(LS_VERBOSE) << "Decoding sync-packet: " <<
1238 " ts=" << packet->header.timestamp <<
1239 ", sn=" << packet->header.sequenceNumber <<
1240 ", pt=" << static_cast<int>(packet->header.payloadType) <<
1241 ", ssrc=" << packet->header.ssrc <<
1242 ", len=" << packet->payload_length;
1243 memset(&decoded_buffer_[*decoded_length], 0, decoder_frame_length_ *
1244 decoder->channels() * sizeof(decoded_buffer_[0]));
1245 decode_length = decoder_frame_length_;
1246 } else if (!packet->primary) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001247 // This is a redundant payload; call the special decoder method.
turaj@webrtc.org0c0fae82013-09-25 17:42:17 +00001248 LOG(LS_VERBOSE) << "Decoding packet (redundant):" <<
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001249 " ts=" << packet->header.timestamp <<
1250 ", sn=" << packet->header.sequenceNumber <<
1251 ", pt=" << static_cast<int>(packet->header.payloadType) <<
1252 ", ssrc=" << packet->header.ssrc <<
1253 ", len=" << packet->payload_length;
1254 decode_length = decoder->DecodeRedundant(
1255 packet->payload, packet->payload_length,
1256 &decoded_buffer_[*decoded_length], speech_type);
1257 } else {
turaj@webrtc.org0c0fae82013-09-25 17:42:17 +00001258 LOG(LS_VERBOSE) << "Decoding packet: ts=" << packet->header.timestamp <<
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001259 ", sn=" << packet->header.sequenceNumber <<
1260 ", pt=" << static_cast<int>(packet->header.payloadType) <<
1261 ", ssrc=" << packet->header.ssrc <<
1262 ", len=" << packet->payload_length;
1263 decode_length = decoder->Decode(packet->payload,
1264 packet->payload_length,
1265 &decoded_buffer_[*decoded_length],
1266 speech_type);
1267 }
1268
1269 delete[] packet->payload;
1270 delete packet;
1271 if (decode_length > 0) {
1272 *decoded_length += decode_length;
1273 // Update |decoder_frame_length_| with number of samples per channel.
turaj@webrtc.org362a55e2013-09-20 16:25:28 +00001274 decoder_frame_length_ = decode_length /
1275 static_cast<int>(decoder->channels());
turaj@webrtc.org0c0fae82013-09-25 17:42:17 +00001276 LOG(LS_VERBOSE) << "Decoded " << decode_length << " samples (" <<
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001277 decoder->channels() << " channel(s) -> " << decoder_frame_length_ <<
1278 " samples per channel)";
1279 } else if (decode_length < 0) {
1280 // Error.
henrik.lundin@webrtc.org63464a92013-01-30 09:41:56 +00001281 LOG_FERR2(LS_WARNING, Decode, decode_length, payload_length);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001282 *decoded_length = -1;
1283 PacketBuffer::DeleteAllPackets(packet_list);
1284 break;
1285 }
1286 if (*decoded_length > static_cast<int>(decoded_buffer_length_)) {
1287 // Guard against overflow.
1288 LOG_F(LS_WARNING) << "Decoded too much.";
1289 PacketBuffer::DeleteAllPackets(packet_list);
1290 return kDecodedTooMuch;
1291 }
1292 if (!packet_list->empty()) {
1293 packet = packet_list->front();
1294 } else {
1295 packet = NULL;
1296 }
1297 } // End of decode loop.
1298
1299 // If the list is not empty at this point, it must hold exactly one CNG
1300 // packet.
1301 assert(packet_list->empty() ||
1302 (packet_list->size() == 1 &&
1303 decoder_database_->IsComfortNoise(packet->header.payloadType)));
1304 return 0;
1305}
1306
1307void NetEqImpl::DoNormal(const int16_t* decoded_buffer, size_t decoded_length,
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001308 AudioDecoder::SpeechType speech_type, bool play_dtmf) {
henrik.lundin@webrtc.org40d3fc62013-09-18 12:19:50 +00001309 assert(normal_.get());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001310 assert(mute_factor_array_.get());
henrik.lundin@webrtc.org40d3fc62013-09-18 12:19:50 +00001311 normal_->Process(decoded_buffer, decoded_length, last_mode_,
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +00001312 mute_factor_array_.get(), algorithm_buffer_.get());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001313 if (decoded_length != 0) {
1314 last_mode_ = kModeNormal;
1315 }
1316
1317 // If last packet was decoded as an inband CNG, set mode to CNG instead.
1318 if ((speech_type == AudioDecoder::kComfortNoise)
1319 || ((last_mode_ == kModeCodecInternalCng)
1320 && (decoded_length == 0))) {
1321 // TODO(hlundin): Remove second part of || statement above.
1322 last_mode_ = kModeCodecInternalCng;
1323 }
1324
1325 if (!play_dtmf) {
1326 dtmf_tone_generator_->Reset();
1327 }
1328}
1329
1330void NetEqImpl::DoMerge(int16_t* decoded_buffer, size_t decoded_length,
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001331 AudioDecoder::SpeechType speech_type, bool play_dtmf) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001332 assert(mute_factor_array_.get());
henrik.lundin@webrtc.org40d3fc62013-09-18 12:19:50 +00001333 assert(merge_.get());
1334 int new_length = merge_->Process(decoded_buffer, decoded_length,
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +00001335 mute_factor_array_.get(),
1336 algorithm_buffer_.get());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001337
1338 // Update in-call and post-call statistics.
1339 if (expand_->MuteFactor(0) == 0) {
1340 // Expand generates only noise.
turaj@webrtc.org362a55e2013-09-20 16:25:28 +00001341 stats_.ExpandedNoiseSamples(new_length - static_cast<int>(decoded_length));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001342 } else {
1343 // Expansion generates more than only noise.
turaj@webrtc.org362a55e2013-09-20 16:25:28 +00001344 stats_.ExpandedVoiceSamples(new_length - static_cast<int>(decoded_length));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001345 }
1346
1347 last_mode_ = kModeMerge;
1348 // If last packet was decoded as an inband CNG, set mode to CNG instead.
1349 if (speech_type == AudioDecoder::kComfortNoise) {
1350 last_mode_ = kModeCodecInternalCng;
1351 }
1352 expand_->Reset();
1353 if (!play_dtmf) {
1354 dtmf_tone_generator_->Reset();
1355 }
1356}
1357
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001358int NetEqImpl::DoExpand(bool play_dtmf) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001359 while ((sync_buffer_->FutureLength() - expand_->overlap_length()) <
1360 static_cast<size_t>(output_size_samples_)) {
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001361 algorithm_buffer_->Clear();
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +00001362 int return_value = expand_->Process(algorithm_buffer_.get());
turaj@webrtc.org362a55e2013-09-20 16:25:28 +00001363 int length = static_cast<int>(algorithm_buffer_->Size());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001364
1365 // Update in-call and post-call statistics.
1366 if (expand_->MuteFactor(0) == 0) {
1367 // Expand operation generates only noise.
1368 stats_.ExpandedNoiseSamples(length);
1369 } else {
1370 // Expand operation generates more than only noise.
1371 stats_.ExpandedVoiceSamples(length);
1372 }
1373
1374 last_mode_ = kModeExpand;
1375
1376 if (return_value < 0) {
1377 return return_value;
1378 }
1379
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001380 sync_buffer_->PushBack(*algorithm_buffer_);
1381 algorithm_buffer_->Clear();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001382 }
1383 if (!play_dtmf) {
1384 dtmf_tone_generator_->Reset();
1385 }
1386 return 0;
1387}
1388
1389int NetEqImpl::DoAccelerate(int16_t* decoded_buffer, size_t decoded_length,
1390 AudioDecoder::SpeechType speech_type,
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001391 bool play_dtmf) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001392 const size_t required_samples = 240 * fs_mult_; // Must have 30 ms.
turaj@webrtc.org362a55e2013-09-20 16:25:28 +00001393 size_t borrowed_samples_per_channel = 0;
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001394 size_t num_channels = algorithm_buffer_->Channels();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001395 size_t decoded_length_per_channel = decoded_length / num_channels;
1396 if (decoded_length_per_channel < required_samples) {
1397 // Must move data from the |sync_buffer_| in order to get 30 ms.
turaj@webrtc.org362a55e2013-09-20 16:25:28 +00001398 borrowed_samples_per_channel = static_cast<int>(required_samples -
1399 decoded_length_per_channel);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001400 memmove(&decoded_buffer[borrowed_samples_per_channel * num_channels],
1401 decoded_buffer,
1402 sizeof(int16_t) * decoded_length);
1403 sync_buffer_->ReadInterleavedFromEnd(borrowed_samples_per_channel,
1404 decoded_buffer);
1405 decoded_length = required_samples * num_channels;
1406 }
1407
1408 int16_t samples_removed;
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +00001409 Accelerate::ReturnCodes return_code = accelerate_->Process(
1410 decoded_buffer, decoded_length, algorithm_buffer_.get(),
1411 &samples_removed);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001412 stats_.AcceleratedSamples(samples_removed);
1413 switch (return_code) {
1414 case Accelerate::kSuccess:
1415 last_mode_ = kModeAccelerateSuccess;
1416 break;
1417 case Accelerate::kSuccessLowEnergy:
1418 last_mode_ = kModeAccelerateLowEnergy;
1419 break;
1420 case Accelerate::kNoStretch:
1421 last_mode_ = kModeAccelerateFail;
1422 break;
1423 case Accelerate::kError:
1424 // TODO(hlundin): Map to kModeError instead?
1425 last_mode_ = kModeAccelerateFail;
1426 return kAccelerateError;
1427 }
1428
1429 if (borrowed_samples_per_channel > 0) {
1430 // Copy borrowed samples back to the |sync_buffer_|.
turaj@webrtc.org362a55e2013-09-20 16:25:28 +00001431 size_t length = algorithm_buffer_->Size();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001432 if (length < borrowed_samples_per_channel) {
1433 // This destroys the beginning of the buffer, but will not cause any
1434 // problems.
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001435 sync_buffer_->ReplaceAtIndex(*algorithm_buffer_,
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001436 sync_buffer_->Size() -
1437 borrowed_samples_per_channel);
1438 sync_buffer_->PushFrontZeros(borrowed_samples_per_channel - length);
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001439 algorithm_buffer_->PopFront(length);
1440 assert(algorithm_buffer_->Empty());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001441 } else {
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001442 sync_buffer_->ReplaceAtIndex(*algorithm_buffer_,
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001443 borrowed_samples_per_channel,
1444 sync_buffer_->Size() -
1445 borrowed_samples_per_channel);
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001446 algorithm_buffer_->PopFront(borrowed_samples_per_channel);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001447 }
1448 }
1449
1450 // If last packet was decoded as an inband CNG, set mode to CNG instead.
1451 if (speech_type == AudioDecoder::kComfortNoise) {
1452 last_mode_ = kModeCodecInternalCng;
1453 }
1454 if (!play_dtmf) {
1455 dtmf_tone_generator_->Reset();
1456 }
1457 expand_->Reset();
1458 return 0;
1459}
1460
1461int NetEqImpl::DoPreemptiveExpand(int16_t* decoded_buffer,
1462 size_t decoded_length,
1463 AudioDecoder::SpeechType speech_type,
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001464 bool play_dtmf) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001465 const size_t required_samples = 240 * fs_mult_; // Must have 30 ms.
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001466 size_t num_channels = algorithm_buffer_->Channels();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001467 int borrowed_samples_per_channel = 0;
1468 int old_borrowed_samples_per_channel = 0;
1469 size_t decoded_length_per_channel = decoded_length / num_channels;
1470 if (decoded_length_per_channel < required_samples) {
1471 // Must move data from the |sync_buffer_| in order to get 30 ms.
turaj@webrtc.org362a55e2013-09-20 16:25:28 +00001472 borrowed_samples_per_channel = static_cast<int>(required_samples -
1473 decoded_length_per_channel);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001474 // Calculate how many of these were already played out.
turaj@webrtc.org362a55e2013-09-20 16:25:28 +00001475 old_borrowed_samples_per_channel = static_cast<int>(
1476 borrowed_samples_per_channel - sync_buffer_->FutureLength());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001477 old_borrowed_samples_per_channel = std::max(
1478 0, old_borrowed_samples_per_channel);
1479 memmove(&decoded_buffer[borrowed_samples_per_channel * num_channels],
1480 decoded_buffer,
1481 sizeof(int16_t) * decoded_length);
1482 sync_buffer_->ReadInterleavedFromEnd(borrowed_samples_per_channel,
1483 decoded_buffer);
1484 decoded_length = required_samples * num_channels;
1485 }
1486
1487 int16_t samples_added;
henrik.lundin@webrtc.org40d3fc62013-09-18 12:19:50 +00001488 PreemptiveExpand::ReturnCodes return_code = preemptive_expand_->Process(
turaj@webrtc.org362a55e2013-09-20 16:25:28 +00001489 decoded_buffer, static_cast<int>(decoded_length),
1490 old_borrowed_samples_per_channel,
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +00001491 algorithm_buffer_.get(), &samples_added);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001492 stats_.PreemptiveExpandedSamples(samples_added);
1493 switch (return_code) {
1494 case PreemptiveExpand::kSuccess:
1495 last_mode_ = kModePreemptiveExpandSuccess;
1496 break;
1497 case PreemptiveExpand::kSuccessLowEnergy:
1498 last_mode_ = kModePreemptiveExpandLowEnergy;
1499 break;
1500 case PreemptiveExpand::kNoStretch:
1501 last_mode_ = kModePreemptiveExpandFail;
1502 break;
1503 case PreemptiveExpand::kError:
1504 // TODO(hlundin): Map to kModeError instead?
1505 last_mode_ = kModePreemptiveExpandFail;
1506 return kPreemptiveExpandError;
1507 }
1508
1509 if (borrowed_samples_per_channel > 0) {
1510 // Copy borrowed samples back to the |sync_buffer_|.
1511 sync_buffer_->ReplaceAtIndex(
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001512 *algorithm_buffer_, borrowed_samples_per_channel,
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001513 sync_buffer_->Size() - borrowed_samples_per_channel);
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001514 algorithm_buffer_->PopFront(borrowed_samples_per_channel);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001515 }
1516
1517 // If last packet was decoded as an inband CNG, set mode to CNG instead.
1518 if (speech_type == AudioDecoder::kComfortNoise) {
1519 last_mode_ = kModeCodecInternalCng;
1520 }
1521 if (!play_dtmf) {
1522 dtmf_tone_generator_->Reset();
1523 }
1524 expand_->Reset();
1525 return 0;
1526}
1527
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001528int NetEqImpl::DoRfc3389Cng(PacketList* packet_list, bool play_dtmf) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001529 if (!packet_list->empty()) {
1530 // Must have exactly one SID frame at this point.
1531 assert(packet_list->size() == 1);
1532 Packet* packet = packet_list->front();
1533 packet_list->pop_front();
henrik.lundin@webrtc.org73deaad2013-01-31 13:32:51 +00001534 if (!decoder_database_->IsComfortNoise(packet->header.payloadType)) {
1535#ifdef LEGACY_BITEXACT
1536 // This can happen due to a bug in GetDecision. Change the payload type
1537 // to a CNG type, and move on. Note that this means that we are in fact
1538 // sending a non-CNG payload to the comfort noise decoder for decoding.
1539 // Clearly wrong, but will maintain bit-exactness with legacy.
1540 if (fs_hz_ == 8000) {
1541 packet->header.payloadType =
1542 decoder_database_->GetRtpPayloadType(kDecoderCNGnb);
1543 } else if (fs_hz_ == 16000) {
1544 packet->header.payloadType =
1545 decoder_database_->GetRtpPayloadType(kDecoderCNGwb);
1546 } else if (fs_hz_ == 32000) {
1547 packet->header.payloadType =
1548 decoder_database_->GetRtpPayloadType(kDecoderCNGswb32kHz);
1549 } else if (fs_hz_ == 48000) {
1550 packet->header.payloadType =
1551 decoder_database_->GetRtpPayloadType(kDecoderCNGswb48kHz);
1552 }
1553 assert(decoder_database_->IsComfortNoise(packet->header.payloadType));
1554#else
1555 LOG(LS_ERROR) << "Trying to decode non-CNG payload as CNG.";
1556 return kOtherError;
1557#endif
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001558 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001559 // UpdateParameters() deletes |packet|.
1560 if (comfort_noise_->UpdateParameters(packet) ==
1561 ComfortNoise::kInternalError) {
1562 LOG_FERR0(LS_WARNING, UpdateParameters);
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001563 algorithm_buffer_->Zeros(output_size_samples_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001564 return -comfort_noise_->internal_error_code();
1565 }
1566 }
1567 int cn_return = comfort_noise_->Generate(output_size_samples_,
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +00001568 algorithm_buffer_.get());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001569 expand_->Reset();
1570 last_mode_ = kModeRfc3389Cng;
1571 if (!play_dtmf) {
1572 dtmf_tone_generator_->Reset();
1573 }
1574 if (cn_return == ComfortNoise::kInternalError) {
1575 LOG_FERR1(LS_WARNING, comfort_noise_->Generate, cn_return);
1576 decoder_error_code_ = comfort_noise_->internal_error_code();
1577 return kComfortNoiseErrorCode;
1578 } else if (cn_return == ComfortNoise::kUnknownPayloadType) {
1579 LOG_FERR1(LS_WARNING, comfort_noise_->Generate, cn_return);
1580 return kUnknownRtpPayloadType;
1581 }
1582 return 0;
1583}
1584
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001585void NetEqImpl::DoCodecInternalCng() {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001586 int length = 0;
1587 // TODO(hlundin): Will probably need a longer buffer for multi-channel.
1588 int16_t decoded_buffer[kMaxFrameSize];
1589 AudioDecoder* decoder = decoder_database_->GetActiveDecoder();
1590 if (decoder) {
1591 const uint8_t* dummy_payload = NULL;
1592 AudioDecoder::SpeechType speech_type;
1593 length = decoder->Decode(dummy_payload, 0, decoded_buffer, &speech_type);
1594 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001595 assert(mute_factor_array_.get());
henrik.lundin@webrtc.org40d3fc62013-09-18 12:19:50 +00001596 normal_->Process(decoded_buffer, length, last_mode_, mute_factor_array_.get(),
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +00001597 algorithm_buffer_.get());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001598 last_mode_ = kModeCodecInternalCng;
1599 expand_->Reset();
1600}
1601
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001602int NetEqImpl::DoDtmf(const DtmfEvent& dtmf_event, bool* play_dtmf) {
turaj@webrtc.org4d06db52013-03-27 18:31:42 +00001603 // This block of the code and the block further down, handling |dtmf_switch|
1604 // are commented out. Otherwise playing out-of-band DTMF would fail in VoE
1605 // test, DtmfTest.ManualSuccessfullySendsOutOfBandTelephoneEvents. This is
1606 // equivalent to |dtmf_switch| always be false.
1607 //
1608 // See http://webrtc-codereview.appspot.com/1195004/ for discussion
1609 // On this issue. This change might cause some glitches at the point of
1610 // switch from audio to DTMF. Issue 1545 is filed to track this.
1611 //
1612 // bool dtmf_switch = false;
1613 // if ((last_mode_ != kModeDtmf) && dtmf_tone_generator_->initialized()) {
1614 // // Special case; see below.
1615 // // We must catch this before calling Generate, since |initialized| is
1616 // // modified in that call.
1617 // dtmf_switch = true;
1618 // }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001619
1620 int dtmf_return_value = 0;
1621 if (!dtmf_tone_generator_->initialized()) {
1622 // Initialize if not already done.
1623 dtmf_return_value = dtmf_tone_generator_->Init(fs_hz_, dtmf_event.event_no,
1624 dtmf_event.volume);
1625 }
turaj@webrtc.org4d06db52013-03-27 18:31:42 +00001626
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001627 if (dtmf_return_value == 0) {
1628 // Generate DTMF signal.
1629 dtmf_return_value = dtmf_tone_generator_->Generate(output_size_samples_,
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +00001630 algorithm_buffer_.get());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001631 }
turaj@webrtc.org4d06db52013-03-27 18:31:42 +00001632
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001633 if (dtmf_return_value < 0) {
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001634 algorithm_buffer_->Zeros(output_size_samples_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001635 return dtmf_return_value;
1636 }
1637
turaj@webrtc.org4d06db52013-03-27 18:31:42 +00001638 // if (dtmf_switch) {
1639 // // This is the special case where the previous operation was DTMF
1640 // // overdub, but the current instruction is "regular" DTMF. We must make
1641 // // sure that the DTMF does not have any discontinuities. The first DTMF
1642 // // sample that we generate now must be played out immediately, therefore
1643 // // it must be copied to the speech buffer.
1644 // // TODO(hlundin): This code seems incorrect. (Legacy.) Write test and
1645 // // verify correct operation.
1646 // assert(false);
1647 // // Must generate enough data to replace all of the |sync_buffer_|
1648 // // "future".
1649 // int required_length = sync_buffer_->FutureLength();
1650 // assert(dtmf_tone_generator_->initialized());
1651 // dtmf_return_value = dtmf_tone_generator_->Generate(required_length,
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001652 // algorithm_buffer_);
1653 // assert((size_t) required_length == algorithm_buffer_->Size());
turaj@webrtc.org4d06db52013-03-27 18:31:42 +00001654 // if (dtmf_return_value < 0) {
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001655 // algorithm_buffer_->Zeros(output_size_samples_);
turaj@webrtc.org4d06db52013-03-27 18:31:42 +00001656 // return dtmf_return_value;
1657 // }
1658 //
1659 // // Overwrite the "future" part of the speech buffer with the new DTMF
1660 // // data.
1661 // // TODO(hlundin): It seems that this overwriting has gone lost.
1662 // // Not adapted for multi-channel yet.
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001663 // assert(algorithm_buffer_->Channels() == 1);
1664 // if (algorithm_buffer_->Channels() != 1) {
turaj@webrtc.org4d06db52013-03-27 18:31:42 +00001665 // LOG(LS_WARNING) << "DTMF not supported for more than one channel";
1666 // return kStereoNotSupported;
1667 // }
1668 // // Shuffle the remaining data to the beginning of algorithm buffer.
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001669 // algorithm_buffer_->PopFront(sync_buffer_->FutureLength());
turaj@webrtc.org4d06db52013-03-27 18:31:42 +00001670 // }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001671
1672 sync_buffer_->IncreaseEndTimestamp(output_size_samples_);
1673 expand_->Reset();
1674 last_mode_ = kModeDtmf;
1675
1676 // Set to false because the DTMF is already in the algorithm buffer.
1677 *play_dtmf = false;
1678 return 0;
1679}
1680
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001681void NetEqImpl::DoAlternativePlc(bool increase_timestamp) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001682 AudioDecoder* decoder = decoder_database_->GetActiveDecoder();
1683 int length;
1684 if (decoder && decoder->HasDecodePlc()) {
1685 // Use the decoder's packet-loss concealment.
1686 // TODO(hlundin): Will probably need a longer buffer for multi-channel.
1687 int16_t decoded_buffer[kMaxFrameSize];
1688 length = decoder->DecodePlc(1, decoded_buffer);
1689 if (length > 0) {
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001690 algorithm_buffer_->PushBackInterleaved(decoded_buffer, length);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001691 } else {
1692 length = 0;
1693 }
1694 } else {
1695 // Do simple zero-stuffing.
1696 length = output_size_samples_;
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001697 algorithm_buffer_->Zeros(length);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001698 // By not advancing the timestamp, NetEq inserts samples.
1699 stats_.AddZeros(length);
1700 }
1701 if (increase_timestamp) {
1702 sync_buffer_->IncreaseEndTimestamp(length);
1703 }
1704 expand_->Reset();
1705}
1706
1707int NetEqImpl::DtmfOverdub(const DtmfEvent& dtmf_event, size_t num_channels,
1708 int16_t* output) const {
1709 size_t out_index = 0;
1710 int overdub_length = output_size_samples_; // Default value.
1711
1712 if (sync_buffer_->dtmf_index() > sync_buffer_->next_index()) {
1713 // Special operation for transition from "DTMF only" to "DTMF overdub".
1714 out_index = std::min(
1715 sync_buffer_->dtmf_index() - sync_buffer_->next_index(),
1716 static_cast<size_t>(output_size_samples_));
turaj@webrtc.org362a55e2013-09-20 16:25:28 +00001717 overdub_length = output_size_samples_ - static_cast<int>(out_index);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001718 }
1719
henrik.lundin@webrtc.orgfd11bbf2013-09-30 20:38:44 +00001720 AudioMultiVector dtmf_output(num_channels);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001721 int dtmf_return_value = 0;
1722 if (!dtmf_tone_generator_->initialized()) {
1723 dtmf_return_value = dtmf_tone_generator_->Init(fs_hz_, dtmf_event.event_no,
1724 dtmf_event.volume);
1725 }
1726 if (dtmf_return_value == 0) {
1727 dtmf_return_value = dtmf_tone_generator_->Generate(overdub_length,
1728 &dtmf_output);
1729 assert((size_t) overdub_length == dtmf_output.Size());
1730 }
1731 dtmf_output.ReadInterleaved(overdub_length, &output[out_index]);
1732 return dtmf_return_value < 0 ? dtmf_return_value : 0;
1733}
1734
1735int NetEqImpl::ExtractPackets(int required_samples, PacketList* packet_list) {
1736 bool first_packet = true;
1737 uint8_t prev_payload_type = 0;
1738 uint32_t prev_timestamp = 0;
1739 uint16_t prev_sequence_number = 0;
1740 bool next_packet_available = false;
1741
henrik.lundin@webrtc.orge1d468c2013-01-30 07:37:20 +00001742 const RTPHeader* header = packet_buffer_->NextRtpHeader();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001743 assert(header);
1744 if (!header) {
1745 return -1;
1746 }
turaj@webrtc.org7df97062013-08-02 18:07:13 +00001747 uint32_t first_timestamp = header->timestamp;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001748 int extracted_samples = 0;
1749
1750 // Packet extraction loop.
1751 do {
1752 timestamp_ = header->timestamp;
1753 int discard_count = 0;
henrik.lundin@webrtc.orge1d468c2013-01-30 07:37:20 +00001754 Packet* packet = packet_buffer_->GetNextPacket(&discard_count);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001755 // |header| may be invalid after the |packet_buffer_| operation.
1756 header = NULL;
1757 if (!packet) {
1758 LOG_FERR1(LS_ERROR, GetNextPacket, discard_count) <<
1759 "Should always be able to extract a packet here";
1760 assert(false); // Should always be able to extract a packet here.
1761 return -1;
1762 }
1763 stats_.PacketsDiscarded(discard_count);
1764 // Store waiting time in ms; packets->waiting_time is in "output blocks".
1765 stats_.StoreWaitingTime(packet->waiting_time * kOutputSizeMs);
1766 assert(packet->payload_length > 0);
1767 packet_list->push_back(packet); // Store packet in list.
1768
1769 if (first_packet) {
1770 first_packet = false;
minyue@webrtc.orgd7301772013-08-29 00:58:14 +00001771 decoded_packet_sequence_number_ = prev_sequence_number =
1772 packet->header.sequenceNumber;
1773 decoded_packet_timestamp_ = prev_timestamp = packet->header.timestamp;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001774 prev_payload_type = packet->header.payloadType;
1775 }
1776
1777 // Store number of extracted samples.
1778 int packet_duration = 0;
1779 AudioDecoder* decoder = decoder_database_->GetDecoder(
1780 packet->header.payloadType);
1781 if (decoder) {
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001782 packet_duration = packet->sync_packet ? decoder_frame_length_ :
1783 decoder->PacketDuration(packet->payload, packet->payload_length);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001784 } else {
1785 LOG_FERR1(LS_WARNING, GetDecoder, packet->header.payloadType) <<
1786 "Could not find a decoder for a packet about to be extracted.";
1787 assert(false);
1788 }
1789 if (packet_duration <= 0) {
1790 // Decoder did not return a packet duration. Assume that the packet
1791 // contains the same number of samples as the previous one.
1792 packet_duration = decoder_frame_length_;
1793 }
1794 extracted_samples = packet->header.timestamp - first_timestamp +
1795 packet_duration;
1796
1797 // Check what packet is available next.
1798 header = packet_buffer_->NextRtpHeader();
1799 next_packet_available = false;
1800 if (header && prev_payload_type == header->payloadType) {
1801 int16_t seq_no_diff = header->sequenceNumber - prev_sequence_number;
1802 int32_t ts_diff = header->timestamp - prev_timestamp;
1803 if (seq_no_diff == 1 ||
1804 (seq_no_diff == 0 && ts_diff == decoder_frame_length_)) {
1805 // The next sequence number is available, or the next part of a packet
1806 // that was split into pieces upon insertion.
1807 next_packet_available = true;
1808 }
1809 prev_sequence_number = header->sequenceNumber;
1810 }
1811 } while (extracted_samples < required_samples && next_packet_available);
1812
1813 return extracted_samples;
1814}
1815
1816void NetEqImpl::SetSampleRateAndChannels(int fs_hz, size_t channels) {
1817 LOG_API2(fs_hz, channels);
1818 // TODO(hlundin): Change to an enumerator and skip assert.
1819 assert(fs_hz == 8000 || fs_hz == 16000 || fs_hz == 32000 || fs_hz == 48000);
1820 assert(channels > 0);
1821
1822 fs_hz_ = fs_hz;
1823 fs_mult_ = fs_hz / 8000;
1824 output_size_samples_ = kOutputSizeMs * 8 * fs_mult_;
1825 decoder_frame_length_ = 3 * output_size_samples_; // Initialize to 30ms.
1826
1827 last_mode_ = kModeNormal;
1828
1829 // Create a new array of mute factors and set all to 1.
1830 mute_factor_array_.reset(new int16_t[channels]);
1831 for (size_t i = 0; i < channels; ++i) {
1832 mute_factor_array_[i] = 16384; // 1.0 in Q14.
1833 }
1834
1835 // Reset comfort noise decoder, if there is one active.
1836 AudioDecoder* cng_decoder = decoder_database_->GetActiveCngDecoder();
1837 if (cng_decoder) {
1838 cng_decoder->Init();
1839 }
1840
1841 // Reinit post-decode VAD with new sample rate.
1842 assert(vad_.get()); // Cannot be NULL here.
1843 vad_->Init();
1844
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001845 // Delete algorithm buffer and create a new one.
henrik.lundin@webrtc.orgfd11bbf2013-09-30 20:38:44 +00001846 algorithm_buffer_.reset(new AudioMultiVector(channels));
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001847
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001848 // Delete sync buffer and create a new one.
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +00001849 sync_buffer_.reset(new SyncBuffer(channels, kSyncBufferSize * fs_mult_));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001850
turaj@webrtc.orgff43c852013-09-25 00:07:27 +00001851
1852 // Delete BackgroundNoise object and create a new one, while preserving its
1853 // mode.
1854 NetEqBackgroundNoiseMode current_mode = kBgnOn;
1855 if (background_noise_.get())
1856 current_mode = background_noise_->mode();
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +00001857 background_noise_.reset(new BackgroundNoise(channels));
turaj@webrtc.orgff43c852013-09-25 00:07:27 +00001858 background_noise_->set_mode(current_mode);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001859
1860 // Reset random vector.
1861 random_vector_.Reset();
1862
1863 // Delete Expand object and create a new one.
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +00001864 expand_.reset(new Expand(background_noise_.get(), sync_buffer_.get(),
1865 &random_vector_, fs_hz, channels));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001866 // Move index so that we create a small set of future samples (all 0).
1867 sync_buffer_->set_next_index(sync_buffer_->next_index() -
1868 expand_->overlap_length());
1869
henrik.lundin@webrtc.org40d3fc62013-09-18 12:19:50 +00001870 normal_.reset(new Normal(fs_hz, decoder_database_.get(), *background_noise_,
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +00001871 expand_.get()));
1872 merge_.reset(new Merge(fs_hz, channels, expand_.get(), sync_buffer_.get()));
henrik.lundin@webrtc.org40d3fc62013-09-18 12:19:50 +00001873 accelerate_.reset(new Accelerate(fs_hz, channels, *background_noise_));
1874 preemptive_expand_.reset(new PreemptiveExpand(fs_hz, channels,
1875 *background_noise_));
1876
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001877 // Delete ComfortNoise object and create a new one.
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +00001878 comfort_noise_.reset(new ComfortNoise(fs_hz, decoder_database_.get(),
1879 sync_buffer_.get()));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001880
1881 // Verify that |decoded_buffer_| is long enough.
1882 if (decoded_buffer_length_ < kMaxFrameSize * channels) {
1883 // Reallocate to larger size.
1884 decoded_buffer_length_ = kMaxFrameSize * channels;
1885 decoded_buffer_.reset(new int16_t[decoded_buffer_length_]);
1886 }
1887
1888 // Communicate new sample rate and output size to DecisionLogic object.
1889 assert(decision_logic_.get());
1890 decision_logic_->SetSampleRate(fs_hz_, output_size_samples_);
1891}
1892
1893NetEqOutputType NetEqImpl::LastOutputType() {
1894 assert(vad_.get());
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +00001895 assert(expand_.get());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001896 if (last_mode_ == kModeCodecInternalCng || last_mode_ == kModeRfc3389Cng) {
1897 return kOutputCNG;
1898 } else if (vad_->running() && !vad_->active_speech()) {
1899 return kOutputVADPassive;
1900 } else if (last_mode_ == kModeExpand && expand_->MuteFactor(0) == 0) {
1901 // Expand mode has faded down to background noise only (very long expand).
1902 return kOutputPLCtoCNG;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001903 } else if (last_mode_ == kModeExpand) {
1904 return kOutputPLC;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001905 } else {
1906 return kOutputNormal;
1907 }
1908}
1909
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001910} // namespace webrtc