blob: 3b81999dbba042d4991042a203f5de4b2076a39e [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
henrik.lundin@webrtc.org9c55f0f2014-06-09 08:10:28 +000011#include "webrtc/modules/audio_coding/neteq/neteq_impl.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000012
13#include <assert.h>
14#include <memory.h> // memset
15
16#include <algorithm>
17
Henrik Lundind67a2192015-08-03 12:54:37 +020018#include "webrtc/base/logging.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000019#include "webrtc/common_audio/signal_processing/include/signal_processing_library.h"
kwiberg@webrtc.orge04a93b2014-12-09 10:12:53 +000020#include "webrtc/modules/audio_coding/codecs/audio_decoder.h"
henrik.lundin@webrtc.org9c55f0f2014-06-09 08:10:28 +000021#include "webrtc/modules/audio_coding/neteq/accelerate.h"
22#include "webrtc/modules/audio_coding/neteq/background_noise.h"
23#include "webrtc/modules/audio_coding/neteq/buffer_level_filter.h"
24#include "webrtc/modules/audio_coding/neteq/comfort_noise.h"
25#include "webrtc/modules/audio_coding/neteq/decision_logic.h"
26#include "webrtc/modules/audio_coding/neteq/decoder_database.h"
27#include "webrtc/modules/audio_coding/neteq/defines.h"
28#include "webrtc/modules/audio_coding/neteq/delay_manager.h"
29#include "webrtc/modules/audio_coding/neteq/delay_peak_detector.h"
30#include "webrtc/modules/audio_coding/neteq/dtmf_buffer.h"
31#include "webrtc/modules/audio_coding/neteq/dtmf_tone_generator.h"
32#include "webrtc/modules/audio_coding/neteq/expand.h"
henrik.lundin@webrtc.org9c55f0f2014-06-09 08:10:28 +000033#include "webrtc/modules/audio_coding/neteq/merge.h"
34#include "webrtc/modules/audio_coding/neteq/normal.h"
35#include "webrtc/modules/audio_coding/neteq/packet_buffer.h"
36#include "webrtc/modules/audio_coding/neteq/packet.h"
37#include "webrtc/modules/audio_coding/neteq/payload_splitter.h"
38#include "webrtc/modules/audio_coding/neteq/post_decode_vad.h"
39#include "webrtc/modules/audio_coding/neteq/preemptive_expand.h"
40#include "webrtc/modules/audio_coding/neteq/sync_buffer.h"
41#include "webrtc/modules/audio_coding/neteq/timestamp_scaler.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000042#include "webrtc/modules/interface/module_common_types.h"
43#include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000044
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
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +000052NetEqImpl::NetEqImpl(const NetEq::Config& config,
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000053 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,
henrik.lundin@webrtc.orgd9faa462014-01-14 10:18:45 +000061 TimestampScaler* timestamp_scaler,
62 AccelerateFactory* accelerate_factory,
63 ExpandFactory* expand_factory,
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +000064 PreemptiveExpandFactory* preemptive_expand_factory,
65 bool create_components)
henrik.lundin@webrtc.org2f816bb2014-06-05 10:37:13 +000066 : crit_sect_(CriticalSectionWrapper::CreateCriticalSection()),
67 buffer_level_filter_(buffer_level_filter),
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000068 decoder_database_(decoder_database),
69 delay_manager_(delay_manager),
70 delay_peak_detector_(delay_peak_detector),
71 dtmf_buffer_(dtmf_buffer),
72 dtmf_tone_generator_(dtmf_tone_generator),
73 packet_buffer_(packet_buffer),
74 payload_splitter_(payload_splitter),
75 timestamp_scaler_(timestamp_scaler),
76 vad_(new PostDecodeVad()),
henrik.lundin@webrtc.orgd9faa462014-01-14 10:18:45 +000077 expand_factory_(expand_factory),
78 accelerate_factory_(accelerate_factory),
79 preemptive_expand_factory_(preemptive_expand_factory),
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000080 last_mode_(kModeNormal),
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000081 decoded_buffer_length_(kMaxFrameSize),
82 decoded_buffer_(new int16_t[decoded_buffer_length_]),
83 playout_timestamp_(0),
84 new_codec_(false),
85 timestamp_(0),
86 reset_decoder_(false),
87 current_rtp_payload_type_(0xFF), // Invalid RTP payload type.
88 current_cng_rtp_payload_type_(0xFF), // Invalid RTP payload type.
89 ssrc_(0),
90 first_packet_(true),
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000091 error_code_(0),
92 decoder_error_code_(0),
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +000093 background_noise_mode_(config.background_noise_mode),
henrik.lundin@webrtc.org7cbc4f92014-10-07 06:37:39 +000094 playout_mode_(config.playout_mode),
Henrik Lundincf808d22015-05-27 14:33:29 +020095 enable_fast_accelerate_(config.enable_fast_accelerate),
minyue@webrtc.orgd7301772013-08-29 00:58:14 +000096 decoded_packet_sequence_number_(-1),
97 decoded_packet_timestamp_(0) {
Henrik Lundin905495c2015-05-25 16:58:41 +020098 LOG(LS_INFO) << "NetEq config: " << config.ToString();
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +000099 int fs = config.sample_rate_hz;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000100 if (fs != 8000 && fs != 16000 && fs != 32000 && fs != 48000) {
101 LOG(LS_ERROR) << "Sample rate " << fs << " Hz not supported. " <<
102 "Changing to 8000 Hz.";
103 fs = 8000;
104 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000105 fs_hz_ = fs;
106 fs_mult_ = fs / 8000;
107 output_size_samples_ = kOutputSizeMs * 8 * fs_mult_;
108 decoder_frame_length_ = 3 * output_size_samples_;
109 WebRtcSpl_Init();
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +0000110 if (create_components) {
111 SetSampleRateAndChannels(fs, 1); // Default is 1 channel.
112 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000113}
114
Henrik Lundind67a2192015-08-03 12:54:37 +0200115NetEqImpl::~NetEqImpl() = default;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000116
117int NetEqImpl::InsertPacket(const WebRtcRTPHeader& rtp_header,
118 const uint8_t* payload,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000119 size_t length_bytes,
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000120 uint32_t receive_timestamp) {
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +0000121 CriticalSectionScoped lock(crit_sect_.get());
turaj@webrtc.org0c0fae82013-09-25 17:42:17 +0000122 LOG(LS_VERBOSE) << "InsertPacket: ts=" << rtp_header.header.timestamp <<
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000123 ", sn=" << rtp_header.header.sequenceNumber <<
124 ", pt=" << static_cast<int>(rtp_header.header.payloadType) <<
125 ", ssrc=" << rtp_header.header.ssrc <<
126 ", len=" << length_bytes;
127 int error = InsertPacketInternal(rtp_header, payload, length_bytes,
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +0000128 receive_timestamp, false);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000129 if (error != 0) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000130 error_code_ = error;
131 return kFail;
132 }
133 return kOK;
134}
135
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +0000136int NetEqImpl::InsertSyncPacket(const WebRtcRTPHeader& rtp_header,
137 uint32_t receive_timestamp) {
138 CriticalSectionScoped lock(crit_sect_.get());
139 LOG(LS_VERBOSE) << "InsertPacket-Sync: ts="
140 << rtp_header.header.timestamp <<
141 ", sn=" << rtp_header.header.sequenceNumber <<
142 ", pt=" << static_cast<int>(rtp_header.header.payloadType) <<
143 ", ssrc=" << rtp_header.header.ssrc;
144
145 const uint8_t kSyncPayload[] = { 's', 'y', 'n', 'c' };
146 int error = InsertPacketInternal(
147 rtp_header, kSyncPayload, sizeof(kSyncPayload), receive_timestamp, true);
148
henrik.lundin@webrtc.orge7ce4372014-01-09 14:01:55 +0000149 if (error != 0) {
henrik.lundin@webrtc.orge7ce4372014-01-09 14:01:55 +0000150 error_code_ = error;
151 return kFail;
152 }
153 return kOK;
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +0000154}
155
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000156int NetEqImpl::GetAudio(size_t max_length, int16_t* output_audio,
157 int* samples_per_channel, int* num_channels,
158 NetEqOutputType* type) {
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +0000159 CriticalSectionScoped lock(crit_sect_.get());
turaj@webrtc.org0c0fae82013-09-25 17:42:17 +0000160 LOG(LS_VERBOSE) << "GetAudio";
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000161 int error = GetAudioInternal(max_length, output_audio, samples_per_channel,
162 num_channels);
turaj@webrtc.org0c0fae82013-09-25 17:42:17 +0000163 LOG(LS_VERBOSE) << "Produced " << *samples_per_channel <<
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000164 " samples/channel for " << *num_channels << " channel(s)";
165 if (error != 0) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000166 error_code_ = error;
167 return kFail;
168 }
169 if (type) {
170 *type = LastOutputType();
171 }
172 return kOK;
173}
174
175int NetEqImpl::RegisterPayloadType(enum NetEqDecoder codec,
176 uint8_t rtp_payload_type) {
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +0000177 CriticalSectionScoped lock(crit_sect_.get());
Henrik Lundind67a2192015-08-03 12:54:37 +0200178 LOG(LS_VERBOSE) << "RegisterPayloadType "
179 << static_cast<int>(rtp_payload_type) << " " << codec;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000180 int ret = decoder_database_->RegisterPayload(rtp_payload_type, codec);
181 if (ret != DecoderDatabase::kOK) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000182 switch (ret) {
183 case DecoderDatabase::kInvalidRtpPayloadType:
184 error_code_ = kInvalidRtpPayloadType;
185 break;
186 case DecoderDatabase::kCodecNotSupported:
187 error_code_ = kCodecNotSupported;
188 break;
189 case DecoderDatabase::kDecoderExists:
190 error_code_ = kDecoderExists;
191 break;
192 default:
193 error_code_ = kOtherError;
194 }
195 return kFail;
196 }
197 return kOK;
198}
199
200int NetEqImpl::RegisterExternalDecoder(AudioDecoder* decoder,
201 enum NetEqDecoder codec,
Karl Wibergd8399e62015-05-25 14:39:56 +0200202 uint8_t rtp_payload_type,
203 int sample_rate_hz) {
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +0000204 CriticalSectionScoped lock(crit_sect_.get());
Henrik Lundind67a2192015-08-03 12:54:37 +0200205 LOG(LS_VERBOSE) << "RegisterExternalDecoder "
206 << static_cast<int>(rtp_payload_type) << " " << codec;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000207 if (!decoder) {
208 LOG(LS_ERROR) << "Cannot register external decoder with NULL pointer";
209 assert(false);
210 return kFail;
211 }
212 int ret = decoder_database_->InsertExternal(rtp_payload_type, codec,
213 sample_rate_hz, decoder);
214 if (ret != DecoderDatabase::kOK) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000215 switch (ret) {
216 case DecoderDatabase::kInvalidRtpPayloadType:
217 error_code_ = kInvalidRtpPayloadType;
218 break;
219 case DecoderDatabase::kCodecNotSupported:
220 error_code_ = kCodecNotSupported;
221 break;
222 case DecoderDatabase::kDecoderExists:
223 error_code_ = kDecoderExists;
224 break;
225 case DecoderDatabase::kInvalidSampleRate:
226 error_code_ = kInvalidSampleRate;
227 break;
228 case DecoderDatabase::kInvalidPointer:
229 error_code_ = kInvalidPointer;
230 break;
231 default:
232 error_code_ = kOtherError;
233 }
234 return kFail;
235 }
236 return kOK;
237}
238
239int NetEqImpl::RemovePayloadType(uint8_t rtp_payload_type) {
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +0000240 CriticalSectionScoped lock(crit_sect_.get());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000241 int ret = decoder_database_->Remove(rtp_payload_type);
242 if (ret == DecoderDatabase::kOK) {
243 return kOK;
244 } else if (ret == DecoderDatabase::kDecoderNotFound) {
245 error_code_ = kDecoderNotFound;
246 } else {
247 error_code_ = kOtherError;
248 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000249 return kFail;
250}
251
turaj@webrtc.orgf1efc572013-08-16 23:44:24 +0000252bool NetEqImpl::SetMinimumDelay(int delay_ms) {
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +0000253 CriticalSectionScoped lock(crit_sect_.get());
turaj@webrtc.orgf1efc572013-08-16 23:44:24 +0000254 if (delay_ms >= 0 && delay_ms < 10000) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000255 assert(delay_manager_.get());
turaj@webrtc.orgf1efc572013-08-16 23:44:24 +0000256 return delay_manager_->SetMinimumDelay(delay_ms);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000257 }
258 return false;
259}
260
turaj@webrtc.orgf1efc572013-08-16 23:44:24 +0000261bool NetEqImpl::SetMaximumDelay(int delay_ms) {
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +0000262 CriticalSectionScoped lock(crit_sect_.get());
turaj@webrtc.orgf1efc572013-08-16 23:44:24 +0000263 if (delay_ms >= 0 && delay_ms < 10000) {
264 assert(delay_manager_.get());
265 return delay_manager_->SetMaximumDelay(delay_ms);
266 }
267 return false;
268}
269
270int NetEqImpl::LeastRequiredDelayMs() const {
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +0000271 CriticalSectionScoped lock(crit_sect_.get());
turaj@webrtc.orgf1efc572013-08-16 23:44:24 +0000272 assert(delay_manager_.get());
273 return delay_manager_->least_required_delay_ms();
274}
275
Karl Wiberg7f6c4d42015-04-09 15:44:22 +0200276int NetEqImpl::SetTargetDelay() {
277 return kNotImplemented;
278}
279
280int NetEqImpl::TargetDelay() {
281 return kNotImplemented;
282}
283
Henrik Lundin5abd3e12015-06-03 12:58:46 +0200284int NetEqImpl::CurrentDelay() {
285 return kNotImplemented;
Karl Wiberg7f6c4d42015-04-09 15:44:22 +0200286}
287
henrik.lundin@webrtc.org7cbc4f92014-10-07 06:37:39 +0000288// Deprecated.
289// TODO(henrik.lundin) Delete.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000290void NetEqImpl::SetPlayoutMode(NetEqPlayoutMode mode) {
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +0000291 CriticalSectionScoped lock(crit_sect_.get());
henrik.lundin@webrtc.org7cbc4f92014-10-07 06:37:39 +0000292 if (mode != playout_mode_) {
293 playout_mode_ = mode;
294 CreateDecisionLogic();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000295 }
296}
297
henrik.lundin@webrtc.org7cbc4f92014-10-07 06:37:39 +0000298// Deprecated.
299// TODO(henrik.lundin) Delete.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000300NetEqPlayoutMode NetEqImpl::PlayoutMode() const {
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +0000301 CriticalSectionScoped lock(crit_sect_.get());
henrik.lundin@webrtc.org7cbc4f92014-10-07 06:37:39 +0000302 return playout_mode_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000303}
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());
Peter Kasting728d9032015-06-11 14:31:38 -0700308 const int total_samples_in_buffers =
309 packet_buffer_->NumSamplesInBuffer(decoder_database_.get(),
310 decoder_frame_length_) +
311 static_cast<int>(sync_buffer_->FutureLength());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000312 assert(delay_manager_.get());
313 assert(decision_logic_.get());
314 stats_.GetNetworkStatistics(fs_hz_, total_samples_in_buffers,
315 decoder_frame_length_, *delay_manager_.get(),
316 *decision_logic_.get(), stats);
317 return 0;
318}
319
320void NetEqImpl::WaitingTimes(std::vector<int>* waiting_times) {
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +0000321 CriticalSectionScoped lock(crit_sect_.get());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000322 stats_.WaitingTimes(waiting_times);
323}
324
325void NetEqImpl::GetRtcpStatistics(RtcpStatistics* stats) {
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +0000326 CriticalSectionScoped lock(crit_sect_.get());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000327 if (stats) {
328 rtcp_.GetStatistics(false, stats);
329 }
330}
331
332void NetEqImpl::GetRtcpStatisticsNoReset(RtcpStatistics* stats) {
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +0000333 CriticalSectionScoped lock(crit_sect_.get());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000334 if (stats) {
335 rtcp_.GetStatistics(true, stats);
336 }
337}
338
339void NetEqImpl::EnableVad() {
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +0000340 CriticalSectionScoped lock(crit_sect_.get());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000341 assert(vad_.get());
342 vad_->Enable();
343}
344
345void NetEqImpl::DisableVad() {
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +0000346 CriticalSectionScoped lock(crit_sect_.get());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000347 assert(vad_.get());
348 vad_->Disable();
349}
350
wu@webrtc.org94454b72014-06-05 20:34:08 +0000351bool NetEqImpl::GetPlayoutTimestamp(uint32_t* timestamp) {
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +0000352 CriticalSectionScoped lock(crit_sect_.get());
wu@webrtc.org94454b72014-06-05 20:34:08 +0000353 if (first_packet_) {
354 // We don't have a valid RTP timestamp until we have decoded our first
355 // RTP packet.
356 return false;
357 }
358 *timestamp = timestamp_scaler_->ToExternal(playout_timestamp_);
359 return true;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000360}
361
Karl Wiberg7f6c4d42015-04-09 15:44:22 +0200362int NetEqImpl::SetTargetNumberOfChannels() {
363 return kNotImplemented;
364}
365
366int NetEqImpl::SetTargetSampleRate() {
367 return kNotImplemented;
368}
369
henrik.lundin@webrtc.orgb0f4b3d2014-11-04 08:53:10 +0000370int NetEqImpl::LastError() const {
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +0000371 CriticalSectionScoped lock(crit_sect_.get());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000372 return error_code_;
373}
374
375int NetEqImpl::LastDecoderError() {
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +0000376 CriticalSectionScoped lock(crit_sect_.get());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000377 return decoder_error_code_;
378}
379
380void NetEqImpl::FlushBuffers() {
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +0000381 CriticalSectionScoped lock(crit_sect_.get());
Henrik Lundind67a2192015-08-03 12:54:37 +0200382 LOG(LS_VERBOSE) << "FlushBuffers";
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000383 packet_buffer_->Flush();
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +0000384 assert(sync_buffer_.get());
385 assert(expand_.get());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000386 sync_buffer_->Flush();
387 sync_buffer_->set_next_index(sync_buffer_->next_index() -
388 expand_->overlap_length());
389 // Set to wait for new codec.
390 first_packet_ = true;
391}
392
turaj@webrtc.org3170b572013-08-30 15:36:53 +0000393void NetEqImpl::PacketBufferStatistics(int* current_num_packets,
henrik.lundin@webrtc.org116ed1d2014-04-28 08:20:04 +0000394 int* max_num_packets) const {
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +0000395 CriticalSectionScoped lock(crit_sect_.get());
henrik.lundin@webrtc.org116ed1d2014-04-28 08:20:04 +0000396 packet_buffer_->BufferStat(current_num_packets, max_num_packets);
turaj@webrtc.org3170b572013-08-30 15:36:53 +0000397}
398
turaj@webrtc.orgff43c852013-09-25 00:07:27 +0000399int NetEqImpl::DecodedRtpInfo(int* sequence_number, uint32_t* timestamp) const {
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +0000400 CriticalSectionScoped lock(crit_sect_.get());
minyue@webrtc.orgd7301772013-08-29 00:58:14 +0000401 if (decoded_packet_sequence_number_ < 0)
402 return -1;
403 *sequence_number = decoded_packet_sequence_number_;
404 *timestamp = decoded_packet_timestamp_;
405 return 0;
406}
407
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000408const SyncBuffer* NetEqImpl::sync_buffer_for_test() const {
409 CriticalSectionScoped lock(crit_sect_.get());
410 return sync_buffer_.get();
411}
412
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000413// Methods below this line are private.
414
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000415int NetEqImpl::InsertPacketInternal(const WebRtcRTPHeader& rtp_header,
416 const uint8_t* payload,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000417 size_t length_bytes,
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +0000418 uint32_t receive_timestamp,
419 bool is_sync_packet) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000420 if (!payload) {
421 LOG_F(LS_ERROR) << "payload == NULL";
422 return kInvalidPointer;
423 }
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +0000424 // Sanity checks for sync-packets.
425 if (is_sync_packet) {
426 if (decoder_database_->IsDtmf(rtp_header.header.payloadType) ||
427 decoder_database_->IsRed(rtp_header.header.payloadType) ||
428 decoder_database_->IsComfortNoise(rtp_header.header.payloadType)) {
429 LOG_F(LS_ERROR) << "Sync-packet with an unacceptable payload type "
pkasting@chromium.orgd3245462015-02-23 21:28:22 +0000430 << static_cast<int>(rtp_header.header.payloadType);
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +0000431 return kSyncPacketNotAccepted;
432 }
433 if (first_packet_ ||
434 rtp_header.header.payloadType != current_rtp_payload_type_ ||
435 rtp_header.header.ssrc != ssrc_) {
436 // Even if |current_rtp_payload_type_| is 0xFF, sync-packet isn't
437 // accepted.
pkasting@chromium.orgd3245462015-02-23 21:28:22 +0000438 LOG_F(LS_ERROR)
439 << "Changing codec, SSRC or first packet with sync-packet.";
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +0000440 return kSyncPacketNotAccepted;
441 }
442 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000443 PacketList packet_list;
444 RTPHeader main_header;
445 {
henrik.lundin@webrtc.orge1d468c2013-01-30 07:37:20 +0000446 // Convert to Packet.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000447 // Create |packet| within this separate scope, since it should not be used
448 // directly once it's been inserted in the packet list. This way, |packet|
449 // is not defined outside of this block.
henrik.lundin@webrtc.orge1d468c2013-01-30 07:37:20 +0000450 Packet* packet = new Packet;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000451 packet->header.markerBit = false;
452 packet->header.payloadType = rtp_header.header.payloadType;
453 packet->header.sequenceNumber = rtp_header.header.sequenceNumber;
454 packet->header.timestamp = rtp_header.header.timestamp;
455 packet->header.ssrc = rtp_header.header.ssrc;
456 packet->header.numCSRCs = 0;
457 packet->payload_length = length_bytes;
458 packet->primary = true;
459 packet->waiting_time = 0;
460 packet->payload = new uint8_t[packet->payload_length];
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +0000461 packet->sync_packet = is_sync_packet;
henrik.lundin@webrtc.org73deaad2013-01-31 13:32:51 +0000462 if (!packet->payload) {
463 LOG_F(LS_ERROR) << "Payload pointer is NULL.";
464 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000465 assert(payload); // Already checked above.
466 memcpy(packet->payload, payload, packet->payload_length);
467 // Insert packet in a packet list.
468 packet_list.push_back(packet);
469 // Save main payloads header for later.
470 memcpy(&main_header, &packet->header, sizeof(main_header));
471 }
472
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000473 bool update_sample_rate_and_channels = false;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000474 // Reinitialize NetEq if it's needed (changed SSRC or first call).
475 if ((main_header.ssrc != ssrc_) || first_packet_) {
henrik.lundin@webrtc.org6ff3ac12014-11-20 14:14:49 +0000476 // Note: |first_packet_| will be cleared further down in this method, once
477 // the packet has been successfully inserted into the packet buffer.
478
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000479 rtcp_.Init(main_header.sequenceNumber);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000480
481 // Flush the packet buffer and DTMF buffer.
482 packet_buffer_->Flush();
483 dtmf_buffer_->Flush();
484
485 // Store new SSRC.
486 ssrc_ = main_header.ssrc;
487
turaj@webrtc.org4d06db52013-03-27 18:31:42 +0000488 // Update audio buffer timestamp.
489 sync_buffer_->IncreaseEndTimestamp(main_header.timestamp - timestamp_);
490
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000491 // Update codecs.
492 timestamp_ = main_header.timestamp;
493 current_rtp_payload_type_ = main_header.payloadType;
494
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000495 // Reset timestamp scaling.
496 timestamp_scaler_->Reset();
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000497
henrik.lundin@webrtc.org6ff3ac12014-11-20 14:14:49 +0000498 // Trigger an update of sampling rate and the number of channels.
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000499 update_sample_rate_and_channels = true;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000500 }
501
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +0000502 // Update RTCP statistics, only for regular packets.
503 if (!is_sync_packet)
504 rtcp_.Update(main_header, receive_timestamp);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000505
506 // Check for RED payload type, and separate payloads into several packets.
507 if (decoder_database_->IsRed(main_header.payloadType)) {
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +0000508 assert(!is_sync_packet); // We had a sanity check for this.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000509 if (payload_splitter_->SplitRed(&packet_list) != PayloadSplitter::kOK) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000510 PacketBuffer::DeleteAllPackets(&packet_list);
511 return kRedundancySplitError;
512 }
513 // Only accept a few RED payloads of the same type as the main data,
514 // DTMF events and CNG.
515 payload_splitter_->CheckRedPayloads(&packet_list, *decoder_database_);
516 // Update the stored main payload header since the main payload has now
517 // changed.
518 memcpy(&main_header, &packet_list.front()->header, sizeof(main_header));
519 }
520
521 // Check payload types.
522 if (decoder_database_->CheckPayloadTypes(packet_list) ==
523 DecoderDatabase::kDecoderNotFound) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000524 PacketBuffer::DeleteAllPackets(&packet_list);
525 return kUnknownRtpPayloadType;
526 }
527
528 // Scale timestamp to internal domain (only for some codecs).
529 timestamp_scaler_->ToInternal(&packet_list);
530
531 // Process DTMF payloads. Cycle through the list of packets, and pick out any
532 // DTMF payloads found.
533 PacketList::iterator it = packet_list.begin();
534 while (it != packet_list.end()) {
535 Packet* current_packet = (*it);
536 assert(current_packet);
537 assert(current_packet->payload);
538 if (decoder_database_->IsDtmf(current_packet->header.payloadType)) {
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +0000539 assert(!current_packet->sync_packet); // We had a sanity check for this.
minyue@webrtc.org9721db72013-08-06 05:36:26 +0000540 DtmfEvent event;
541 int ret = DtmfBuffer::ParseEvent(
542 current_packet->header.timestamp,
543 current_packet->payload,
544 current_packet->payload_length,
545 &event);
546 if (ret != DtmfBuffer::kOK) {
minyue@webrtc.org9721db72013-08-06 05:36:26 +0000547 PacketBuffer::DeleteAllPackets(&packet_list);
548 return kDtmfParsingError;
549 }
550 if (dtmf_buffer_->InsertEvent(event) != DtmfBuffer::kOK) {
minyue@webrtc.org9721db72013-08-06 05:36:26 +0000551 PacketBuffer::DeleteAllPackets(&packet_list);
552 return kDtmfInsertError;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000553 }
554 // TODO(hlundin): Let the destructor of Packet handle the payload.
555 delete [] current_packet->payload;
556 delete current_packet;
557 it = packet_list.erase(it);
558 } else {
559 ++it;
560 }
561 }
562
minyue@webrtc.org7549ff42014-04-02 15:03:01 +0000563 // Check for FEC in packets, and separate payloads into several packets.
564 int ret = payload_splitter_->SplitFec(&packet_list, decoder_database_.get());
565 if (ret != PayloadSplitter::kOK) {
minyue@webrtc.org7549ff42014-04-02 15:03:01 +0000566 PacketBuffer::DeleteAllPackets(&packet_list);
567 switch (ret) {
568 case PayloadSplitter::kUnknownPayloadType:
569 return kUnknownRtpPayloadType;
570 default:
571 return kOtherError;
572 }
573 }
574
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000575 // Split payloads into smaller chunks. This also verifies that all payloads
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +0000576 // are of a known payload type. SplitAudio() method is protected against
577 // sync-packets.
minyue@webrtc.orgb28bfa72014-03-21 12:07:40 +0000578 ret = payload_splitter_->SplitAudio(&packet_list, *decoder_database_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000579 if (ret != PayloadSplitter::kOK) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000580 PacketBuffer::DeleteAllPackets(&packet_list);
581 switch (ret) {
582 case PayloadSplitter::kUnknownPayloadType:
583 return kUnknownRtpPayloadType;
584 case PayloadSplitter::kFrameSplitError:
585 return kFrameSplitError;
586 default:
587 return kOtherError;
588 }
589 }
590
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +0000591 // Update bandwidth estimate, if the packet is not sync-packet.
592 if (!packet_list.empty() && !packet_list.front()->sync_packet) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000593 // The list can be empty here if we got nothing but DTMF payloads.
594 AudioDecoder* decoder =
595 decoder_database_->GetDecoder(main_header.payloadType);
596 assert(decoder); // Should always get a valid object, since we have
597 // already checked that the payload types are known.
598 decoder->IncomingPacket(packet_list.front()->payload,
599 packet_list.front()->payload_length,
600 packet_list.front()->header.sequenceNumber,
601 packet_list.front()->header.timestamp,
602 receive_timestamp);
603 }
604
605 // Insert packets in buffer.
606 int temp_bufsize = packet_buffer_->NumPacketsInBuffer();
607 ret = packet_buffer_->InsertPacketList(
608 &packet_list,
609 *decoder_database_,
610 &current_rtp_payload_type_,
611 &current_cng_rtp_payload_type_);
612 if (ret == PacketBuffer::kFlushed) {
613 // Reset DSP timestamp etc. if packet buffer flushed.
614 new_codec_ = true;
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000615 update_sample_rate_and_channels = true;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000616 } else if (ret != PacketBuffer::kOK) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000617 PacketBuffer::DeleteAllPackets(&packet_list);
minyue@webrtc.org7bb54362013-08-06 05:40:57 +0000618 return kOtherError;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000619 }
henrik.lundin@webrtc.org6ff3ac12014-11-20 14:14:49 +0000620
621 if (first_packet_) {
622 first_packet_ = false;
623 // Update the codec on the next GetAudio call.
624 new_codec_ = true;
625 }
626
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000627 if (current_rtp_payload_type_ != 0xFF) {
628 const DecoderDatabase::DecoderInfo* dec_info =
629 decoder_database_->GetDecoderInfo(current_rtp_payload_type_);
630 if (!dec_info) {
631 assert(false); // Already checked that the payload type is known.
632 }
633 }
634
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000635 if (update_sample_rate_and_channels && !packet_buffer_->Empty()) {
636 // We do not use |current_rtp_payload_type_| to |set payload_type|, but
637 // get the next RTP header from |packet_buffer_| to obtain the payload type.
638 // The reason for it is the following corner case. If NetEq receives a
639 // CNG packet with a sample rate different than the current CNG then it
640 // flushes its buffer, assuming send codec must have been changed. However,
641 // payload type of the hypothetically new send codec is not known.
642 const RTPHeader* rtp_header = packet_buffer_->NextRtpHeader();
643 assert(rtp_header);
644 int payload_type = rtp_header->payloadType;
645 AudioDecoder* decoder = decoder_database_->GetDecoder(payload_type);
646 assert(decoder); // Payloads are already checked to be valid.
647 const DecoderDatabase::DecoderInfo* decoder_info =
648 decoder_database_->GetDecoderInfo(payload_type);
649 assert(decoder_info);
650 if (decoder_info->fs_hz != fs_hz_ ||
henrik.lundin@webrtc.org6dba1eb2015-03-18 09:47:08 +0000651 decoder->Channels() != algorithm_buffer_->Channels())
652 SetSampleRateAndChannels(decoder_info->fs_hz, decoder->Channels());
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000653 }
654
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000655 // TODO(hlundin): Move this code to DelayManager class.
656 const DecoderDatabase::DecoderInfo* dec_info =
657 decoder_database_->GetDecoderInfo(main_header.payloadType);
658 assert(dec_info); // Already checked that the payload type is known.
659 delay_manager_->LastDecoderType(dec_info->codec_type);
660 if (delay_manager_->last_pack_cng_or_dtmf() == 0) {
661 // Calculate the total speech length carried in each packet.
662 temp_bufsize = packet_buffer_->NumPacketsInBuffer() - temp_bufsize;
663 temp_bufsize *= decoder_frame_length_;
664
665 if ((temp_bufsize > 0) &&
666 (temp_bufsize != decision_logic_->packet_length_samples())) {
667 decision_logic_->set_packet_length_samples(temp_bufsize);
668 delay_manager_->SetPacketAudioLength((1000 * temp_bufsize) / fs_hz_);
669 }
670
671 // Update statistics.
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000672 if ((int32_t) (main_header.timestamp - timestamp_) >= 0 &&
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000673 !new_codec_) {
674 // Only update statistics if incoming packet is not older than last played
675 // out packet, and if new codec flag is not set.
676 delay_manager_->Update(main_header.sequenceNumber, main_header.timestamp,
677 fs_hz_);
678 }
679 } else if (delay_manager_->last_pack_cng_or_dtmf() == -1) {
680 // This is first "normal" packet after CNG or DTMF.
681 // Reset packet time counter and measure time until next packet,
682 // but don't update statistics.
683 delay_manager_->set_last_pack_cng_or_dtmf(0);
684 delay_manager_->ResetPacketIatCount();
685 }
686 return 0;
687}
688
Peter Kasting728d9032015-06-11 14:31:38 -0700689int NetEqImpl::GetAudioInternal(size_t max_length,
690 int16_t* output,
691 int* samples_per_channel,
692 int* num_channels) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000693 PacketList packet_list;
694 DtmfEvent dtmf_event;
695 Operations operation;
696 bool play_dtmf;
697 int return_value = GetDecision(&operation, &packet_list, &dtmf_event,
698 &play_dtmf);
699 if (return_value != 0) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000700 assert(false);
701 last_mode_ = kModeError;
702 return return_value;
703 }
turaj@webrtc.org0c0fae82013-09-25 17:42:17 +0000704 LOG(LS_VERBOSE) << "GetDecision returned operation=" << operation <<
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000705 " and " << packet_list.size() << " packet(s)";
706
707 AudioDecoder::SpeechType speech_type;
708 int length = 0;
709 int decode_return_value = Decode(&packet_list, &operation,
710 &length, &speech_type);
711
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000712 assert(vad_.get());
713 bool sid_frame_available =
714 (operation == kRfc3389Cng && !packet_list.empty());
715 vad_->Update(decoded_buffer_.get(), length, speech_type,
716 sid_frame_available, fs_hz_);
717
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +0000718 algorithm_buffer_->Clear();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000719 switch (operation) {
720 case kNormal: {
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +0000721 DoNormal(decoded_buffer_.get(), length, speech_type, play_dtmf);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000722 break;
723 }
724 case kMerge: {
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +0000725 DoMerge(decoded_buffer_.get(), length, speech_type, play_dtmf);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000726 break;
727 }
728 case kExpand: {
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +0000729 return_value = DoExpand(play_dtmf);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000730 break;
731 }
Henrik Lundincf808d22015-05-27 14:33:29 +0200732 case kAccelerate:
733 case kFastAccelerate: {
734 const bool fast_accelerate =
735 enable_fast_accelerate_ && (operation == kFastAccelerate);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000736 return_value = DoAccelerate(decoded_buffer_.get(), length, speech_type,
Henrik Lundincf808d22015-05-27 14:33:29 +0200737 play_dtmf, fast_accelerate);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000738 break;
739 }
740 case kPreemptiveExpand: {
741 return_value = DoPreemptiveExpand(decoded_buffer_.get(), length,
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +0000742 speech_type, play_dtmf);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000743 break;
744 }
745 case kRfc3389Cng:
746 case kRfc3389CngNoPacket: {
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +0000747 return_value = DoRfc3389Cng(&packet_list, play_dtmf);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000748 break;
749 }
750 case kCodecInternalCng: {
751 // This handles the case when there is no transmission and the decoder
752 // should produce internal comfort noise.
753 // TODO(hlundin): Write test for codec-internal CNG.
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +0000754 DoCodecInternalCng();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000755 break;
756 }
757 case kDtmf: {
758 // TODO(hlundin): Write test for this.
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +0000759 return_value = DoDtmf(dtmf_event, &play_dtmf);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000760 break;
761 }
762 case kAlternativePlc: {
763 // TODO(hlundin): Write test for this.
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +0000764 DoAlternativePlc(false);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000765 break;
766 }
767 case kAlternativePlcIncreaseTimestamp: {
768 // TODO(hlundin): Write test for this.
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +0000769 DoAlternativePlc(true);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000770 break;
771 }
772 case kAudioRepetitionIncreaseTimestamp: {
773 // TODO(hlundin): Write test for this.
Peter Kastingb7e50542015-06-11 12:55:50 -0700774 sync_buffer_->IncreaseEndTimestamp(
775 static_cast<uint32_t>(output_size_samples_));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000776 // Skipping break on purpose. Execution should move on into the
777 // next case.
kjellander@webrtc.org7d2b6a92015-01-28 18:37:58 +0000778 FALLTHROUGH();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000779 }
780 case kAudioRepetition: {
781 // TODO(hlundin): Write test for this.
782 // Copy last |output_size_samples_| from |sync_buffer_| to
783 // |algorithm_buffer|.
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +0000784 algorithm_buffer_->PushBackFromIndex(
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000785 *sync_buffer_, sync_buffer_->Size() - output_size_samples_);
786 expand_->Reset();
787 break;
788 }
789 case kUndefined: {
Henrik Lundind67a2192015-08-03 12:54:37 +0200790 LOG(LS_ERROR) << "Invalid operation kUndefined.";
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000791 assert(false); // This should not happen.
792 last_mode_ = kModeError;
793 return kInvalidOperation;
794 }
795 } // End of switch.
796 if (return_value < 0) {
797 return return_value;
798 }
799
800 if (last_mode_ != kModeRfc3389Cng) {
801 comfort_noise_->Reset();
802 }
803
804 // Copy from |algorithm_buffer| to |sync_buffer_|.
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +0000805 sync_buffer_->PushBack(*algorithm_buffer_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000806
807 // Extract data from |sync_buffer_| to |output|.
turaj@webrtc.org362a55e2013-09-20 16:25:28 +0000808 size_t num_output_samples_per_channel = output_size_samples_;
809 size_t num_output_samples = output_size_samples_ * sync_buffer_->Channels();
810 if (num_output_samples > max_length) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000811 LOG(LS_WARNING) << "Output array is too short. " << max_length << " < " <<
812 output_size_samples_ << " * " << sync_buffer_->Channels();
813 num_output_samples = max_length;
turaj@webrtc.org362a55e2013-09-20 16:25:28 +0000814 num_output_samples_per_channel = static_cast<int>(
815 max_length / sync_buffer_->Channels());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000816 }
Henrik Lundind67a2192015-08-03 12:54:37 +0200817 const int samples_from_sync =
818 static_cast<int>(sync_buffer_->GetNextAudioInterleaved(
819 num_output_samples_per_channel, output));
turaj@webrtc.org362a55e2013-09-20 16:25:28 +0000820 *num_channels = static_cast<int>(sync_buffer_->Channels());
turaj@webrtc.org0c0fae82013-09-25 17:42:17 +0000821 LOG(LS_VERBOSE) << "Sync buffer (" << *num_channels << " channel(s)):" <<
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +0000822 " insert " << algorithm_buffer_->Size() << " samples, extract " <<
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000823 samples_from_sync << " samples";
824 if (samples_from_sync != output_size_samples_) {
Henrik Lundind67a2192015-08-03 12:54:37 +0200825 LOG(LS_ERROR) << "samples_from_sync (" << samples_from_sync
826 << ") != output_size_samples_ (" << output_size_samples_
827 << ")";
minyue@webrtc.orgdb1cefc2013-08-13 01:39:21 +0000828 // TODO(minyue): treatment of under-run, filling zeros
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000829 memset(output, 0, num_output_samples * sizeof(int16_t));
830 *samples_per_channel = output_size_samples_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000831 return kSampleUnderrun;
832 }
833 *samples_per_channel = output_size_samples_;
834
835 // Should always have overlap samples left in the |sync_buffer_|.
836 assert(sync_buffer_->FutureLength() >= expand_->overlap_length());
837
838 if (play_dtmf) {
839 return_value = DtmfOverdub(dtmf_event, sync_buffer_->Channels(), output);
840 }
841
842 // Update the background noise parameters if last operation wrote data
843 // straight from the decoder to the |sync_buffer_|. That is, none of the
844 // operations that modify the signal can be followed by a parameter update.
845 if ((last_mode_ == kModeNormal) ||
846 (last_mode_ == kModeAccelerateFail) ||
847 (last_mode_ == kModePreemptiveExpandFail) ||
848 (last_mode_ == kModeRfc3389Cng) ||
849 (last_mode_ == kModeCodecInternalCng)) {
850 background_noise_->Update(*sync_buffer_, *vad_.get());
851 }
852
853 if (operation == kDtmf) {
854 // DTMF data was written the end of |sync_buffer_|.
855 // Update index to end of DTMF data in |sync_buffer_|.
856 sync_buffer_->set_dtmf_index(sync_buffer_->Size());
857 }
858
henrik.lundin@webrtc.orged865b52014-03-06 10:28:07 +0000859 if (last_mode_ != kModeExpand) {
860 // If last operation was not expand, calculate the |playout_timestamp_| from
861 // the |sync_buffer_|. However, do not update the |playout_timestamp_| if it
862 // would be moved "backwards".
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000863 uint32_t temp_timestamp = sync_buffer_->end_timestamp() -
turaj@webrtc.org362a55e2013-09-20 16:25:28 +0000864 static_cast<uint32_t>(sync_buffer_->FutureLength());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000865 if (static_cast<int32_t>(temp_timestamp - playout_timestamp_) > 0) {
866 playout_timestamp_ = temp_timestamp;
867 }
868 } else {
869 // Use dead reckoning to estimate the |playout_timestamp_|.
Peter Kastingb7e50542015-06-11 12:55:50 -0700870 playout_timestamp_ += static_cast<uint32_t>(output_size_samples_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000871 }
872
873 if (decode_return_value) return decode_return_value;
874 return return_value;
875}
876
877int NetEqImpl::GetDecision(Operations* operation,
878 PacketList* packet_list,
879 DtmfEvent* dtmf_event,
880 bool* play_dtmf) {
881 // Initialize output variables.
882 *play_dtmf = false;
883 *operation = kUndefined;
884
885 // Increment time counters.
886 packet_buffer_->IncrementWaitingTimes();
887 stats_.IncreaseCounter(output_size_samples_, fs_hz_);
888
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +0000889 assert(sync_buffer_.get());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000890 uint32_t end_timestamp = sync_buffer_->end_timestamp();
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000891 if (!new_codec_) {
892 const uint32_t five_seconds_samples = 5 * fs_hz_;
893 packet_buffer_->DiscardOldPackets(end_timestamp, five_seconds_samples);
894 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000895 const RTPHeader* header = packet_buffer_->NextRtpHeader();
896
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +0000897 if (decision_logic_->CngRfc3389On() || last_mode_ == kModeRfc3389Cng) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000898 // Because of timestamp peculiarities, we have to "manually" disallow using
899 // a CNG packet with the same timestamp as the one that was last played.
900 // This can happen when using redundancy and will cause the timing to shift.
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000901 while (header && decoder_database_->IsComfortNoise(header->payloadType) &&
902 (end_timestamp >= header->timestamp ||
903 end_timestamp + decision_logic_->generated_noise_samples() >
904 header->timestamp)) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000905 // Don't use this packet, discard it.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000906 if (packet_buffer_->DiscardNextPacket() != PacketBuffer::kOK) {
907 assert(false); // Must be ok by design.
908 }
909 // Check buffer again.
910 if (!new_codec_) {
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000911 packet_buffer_->DiscardOldPackets(end_timestamp, 5 * fs_hz_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000912 }
913 header = packet_buffer_->NextRtpHeader();
914 }
915 }
916
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +0000917 assert(expand_.get());
turaj@webrtc.org362a55e2013-09-20 16:25:28 +0000918 const int samples_left = static_cast<int>(sync_buffer_->FutureLength() -
919 expand_->overlap_length());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000920 if (last_mode_ == kModeAccelerateSuccess ||
921 last_mode_ == kModeAccelerateLowEnergy ||
922 last_mode_ == kModePreemptiveExpandSuccess ||
923 last_mode_ == kModePreemptiveExpandLowEnergy) {
924 // Subtract (samples_left + output_size_samples_) from sampleMemory.
925 decision_logic_->AddSampleMemory(-(samples_left + output_size_samples_));
926 }
927
928 // Check if it is time to play a DTMF event.
Peter Kastingb7e50542015-06-11 12:55:50 -0700929 if (dtmf_buffer_->GetEvent(
930 static_cast<uint32_t>(
931 end_timestamp + decision_logic_->generated_noise_samples()),
932 dtmf_event)) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000933 *play_dtmf = true;
934 }
935
936 // Get instruction.
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +0000937 assert(sync_buffer_.get());
938 assert(expand_.get());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000939 *operation = decision_logic_->GetDecision(*sync_buffer_,
940 *expand_,
941 decoder_frame_length_,
942 header,
943 last_mode_,
944 *play_dtmf,
945 &reset_decoder_);
946
947 // Check if we already have enough samples in the |sync_buffer_|. If so,
948 // change decision to normal, unless the decision was merge, accelerate, or
949 // preemptive expand.
Henrik Lundincf808d22015-05-27 14:33:29 +0200950 if (samples_left >= output_size_samples_ && *operation != kMerge &&
951 *operation != kAccelerate && *operation != kFastAccelerate &&
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000952 *operation != kPreemptiveExpand) {
953 *operation = kNormal;
954 return 0;
955 }
956
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +0000957 decision_logic_->ExpandDecision(*operation);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000958
959 // Check conditions for reset.
960 if (new_codec_ || *operation == kUndefined) {
961 // The only valid reason to get kUndefined is that new_codec_ is set.
962 assert(new_codec_);
turaj@webrtc.org4d06db52013-03-27 18:31:42 +0000963 if (*play_dtmf && !header) {
964 timestamp_ = dtmf_event->timestamp;
965 } else {
966 assert(header);
967 if (!header) {
Henrik Lundind67a2192015-08-03 12:54:37 +0200968 LOG(LS_ERROR) << "Packet missing where it shouldn't.";
turaj@webrtc.org4d06db52013-03-27 18:31:42 +0000969 return -1;
970 }
971 timestamp_ = header->timestamp;
972 if (*operation == kRfc3389CngNoPacket
973#ifndef LEGACY_BITEXACT
974 // Without this check, it can happen that a non-CNG packet is sent to
975 // the CNG decoder as if it was a SID frame. This is clearly a bug,
976 // but is kept for now to maintain bit-exactness with the test
977 // vectors.
978 && decoder_database_->IsComfortNoise(header->payloadType)
979#endif
980 ) {
981 // Change decision to CNG packet, since we do have a CNG packet, but it
982 // was considered too early to use. Now, use it anyway.
983 *operation = kRfc3389Cng;
984 } else if (*operation != kRfc3389Cng) {
985 *operation = kNormal;
986 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000987 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000988 // Adjust |sync_buffer_| timestamp before setting |end_timestamp| to the
989 // new value.
990 sync_buffer_->IncreaseEndTimestamp(timestamp_ - end_timestamp);
turaj@webrtc.org4d06db52013-03-27 18:31:42 +0000991 end_timestamp = timestamp_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000992 new_codec_ = false;
993 decision_logic_->SoftReset();
994 buffer_level_filter_->Reset();
995 delay_manager_->Reset();
996 stats_.ResetMcu();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000997 }
998
999 int required_samples = output_size_samples_;
1000 const int samples_10_ms = 80 * fs_mult_;
1001 const int samples_20_ms = 2 * samples_10_ms;
1002 const int samples_30_ms = 3 * samples_10_ms;
1003
1004 switch (*operation) {
1005 case kExpand: {
1006 timestamp_ = end_timestamp;
1007 return 0;
1008 }
1009 case kRfc3389CngNoPacket:
1010 case kCodecInternalCng: {
1011 return 0;
1012 }
1013 case kDtmf: {
1014 // TODO(hlundin): Write test for this.
1015 // Update timestamp.
1016 timestamp_ = end_timestamp;
1017 if (decision_logic_->generated_noise_samples() > 0 &&
1018 last_mode_ != kModeDtmf) {
1019 // Make a jump in timestamp due to the recently played comfort noise.
Peter Kastingb7e50542015-06-11 12:55:50 -07001020 uint32_t timestamp_jump =
1021 static_cast<uint32_t>(decision_logic_->generated_noise_samples());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001022 sync_buffer_->IncreaseEndTimestamp(timestamp_jump);
1023 timestamp_ += timestamp_jump;
1024 }
1025 decision_logic_->set_generated_noise_samples(0);
1026 return 0;
1027 }
Henrik Lundincf808d22015-05-27 14:33:29 +02001028 case kAccelerate:
1029 case kFastAccelerate: {
1030 // In order to do an accelerate we need at least 30 ms of audio data.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001031 if (samples_left >= samples_30_ms) {
1032 // Already have enough data, so we do not need to extract any more.
1033 decision_logic_->set_sample_memory(samples_left);
1034 decision_logic_->set_prev_time_scale(true);
1035 return 0;
1036 } else if (samples_left >= samples_10_ms &&
1037 decoder_frame_length_ >= samples_30_ms) {
1038 // Avoid decoding more data as it might overflow the playout buffer.
1039 *operation = kNormal;
1040 return 0;
1041 } else if (samples_left < samples_20_ms &&
1042 decoder_frame_length_ < samples_30_ms) {
1043 // Build up decoded data by decoding at least 20 ms of audio data. Do
1044 // not perform accelerate yet, but wait until we only need to do one
1045 // decoding.
1046 required_samples = 2 * output_size_samples_;
1047 *operation = kNormal;
1048 }
1049 // If none of the above is true, we have one of two possible situations:
1050 // (1) 20 ms <= samples_left < 30 ms and decoder_frame_length_ < 30 ms; or
1051 // (2) samples_left < 10 ms and decoder_frame_length_ >= 30 ms.
1052 // In either case, we move on with the accelerate decision, and decode one
1053 // frame now.
1054 break;
1055 }
1056 case kPreemptiveExpand: {
1057 // In order to do a preemptive expand we need at least 30 ms of decoded
1058 // audio data.
1059 if ((samples_left >= samples_30_ms) ||
1060 (samples_left >= samples_10_ms &&
1061 decoder_frame_length_ >= samples_30_ms)) {
1062 // Already have enough data, so we do not need to extract any more.
1063 // Or, avoid decoding more data as it might overflow the playout buffer.
1064 // Still try preemptive expand, though.
1065 decision_logic_->set_sample_memory(samples_left);
1066 decision_logic_->set_prev_time_scale(true);
1067 return 0;
1068 }
1069 if (samples_left < samples_20_ms &&
1070 decoder_frame_length_ < samples_30_ms) {
1071 // Build up decoded data by decoding at least 20 ms of audio data.
1072 // Still try to perform preemptive expand.
1073 required_samples = 2 * output_size_samples_;
1074 }
1075 // Move on with the preemptive expand decision.
1076 break;
1077 }
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001078 case kMerge: {
1079 required_samples =
1080 std::max(merge_->RequiredFutureSamples(), required_samples);
1081 break;
1082 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001083 default: {
1084 // Do nothing.
1085 }
1086 }
1087
1088 // Get packets from buffer.
1089 int extracted_samples = 0;
1090 if (header &&
1091 *operation != kAlternativePlc &&
1092 *operation != kAlternativePlcIncreaseTimestamp &&
1093 *operation != kAudioRepetition &&
1094 *operation != kAudioRepetitionIncreaseTimestamp) {
1095 sync_buffer_->IncreaseEndTimestamp(header->timestamp - end_timestamp);
1096 if (decision_logic_->CngOff()) {
1097 // Adjustment of timestamp only corresponds to an actual packet loss
1098 // if comfort noise is not played. If comfort noise was just played,
1099 // this adjustment of timestamp is only done to get back in sync with the
1100 // stream timestamp; no loss to report.
1101 stats_.LostSamples(header->timestamp - end_timestamp);
1102 }
1103
1104 if (*operation != kRfc3389Cng) {
1105 // We are about to decode and use a non-CNG packet.
1106 decision_logic_->SetCngOff();
1107 }
1108 // Reset CNG timestamp as a new packet will be delivered.
1109 // (Also if this is a CNG packet, since playedOutTS is updated.)
1110 decision_logic_->set_generated_noise_samples(0);
1111
1112 extracted_samples = ExtractPackets(required_samples, packet_list);
1113 if (extracted_samples < 0) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001114 return kPacketBufferCorruption;
1115 }
1116 }
1117
Henrik Lundincf808d22015-05-27 14:33:29 +02001118 if (*operation == kAccelerate || *operation == kFastAccelerate ||
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001119 *operation == kPreemptiveExpand) {
1120 decision_logic_->set_sample_memory(samples_left + extracted_samples);
1121 decision_logic_->set_prev_time_scale(true);
1122 }
1123
Henrik Lundincf808d22015-05-27 14:33:29 +02001124 if (*operation == kAccelerate || *operation == kFastAccelerate) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001125 // Check that we have enough data (30ms) to do accelerate.
1126 if (extracted_samples + samples_left < samples_30_ms) {
1127 // TODO(hlundin): Write test for this.
1128 // Not enough, do normal operation instead.
1129 *operation = kNormal;
1130 }
1131 }
1132
1133 timestamp_ = end_timestamp;
1134 return 0;
1135}
1136
1137int NetEqImpl::Decode(PacketList* packet_list, Operations* operation,
1138 int* decoded_length,
1139 AudioDecoder::SpeechType* speech_type) {
1140 *speech_type = AudioDecoder::kSpeech;
1141 AudioDecoder* decoder = NULL;
1142 if (!packet_list->empty()) {
1143 const Packet* packet = packet_list->front();
pkasting@chromium.org0e81fdf2015-02-02 23:54:03 +00001144 uint8_t payload_type = packet->header.payloadType;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001145 if (!decoder_database_->IsComfortNoise(payload_type)) {
1146 decoder = decoder_database_->GetDecoder(payload_type);
1147 assert(decoder);
1148 if (!decoder) {
Henrik Lundind67a2192015-08-03 12:54:37 +02001149 LOG(LS_WARNING) << "Unknown payload type "
1150 << static_cast<int>(payload_type);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001151 PacketBuffer::DeleteAllPackets(packet_list);
1152 return kDecoderNotFound;
1153 }
1154 bool decoder_changed;
1155 decoder_database_->SetActiveDecoder(payload_type, &decoder_changed);
1156 if (decoder_changed) {
1157 // We have a new decoder. Re-init some values.
1158 const DecoderDatabase::DecoderInfo* decoder_info = decoder_database_
1159 ->GetDecoderInfo(payload_type);
1160 assert(decoder_info);
1161 if (!decoder_info) {
Henrik Lundind67a2192015-08-03 12:54:37 +02001162 LOG(LS_WARNING) << "Unknown payload type "
1163 << static_cast<int>(payload_type);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001164 PacketBuffer::DeleteAllPackets(packet_list);
1165 return kDecoderNotFound;
1166 }
tina.legrand@webrtc.orgba5a6c32014-03-23 09:58:48 +00001167 // If sampling rate or number of channels has changed, we need to make
1168 // a reset.
turaj@webrtc.orga6101d72013-10-01 22:01:09 +00001169 if (decoder_info->fs_hz != fs_hz_ ||
henrik.lundin@webrtc.org6dba1eb2015-03-18 09:47:08 +00001170 decoder->Channels() != algorithm_buffer_->Channels()) {
tina.legrand@webrtc.orgba5a6c32014-03-23 09:58:48 +00001171 // TODO(tlegrand): Add unittest to cover this event.
henrik.lundin@webrtc.org6dba1eb2015-03-18 09:47:08 +00001172 SetSampleRateAndChannels(decoder_info->fs_hz, decoder->Channels());
turaj@webrtc.orga6101d72013-10-01 22:01:09 +00001173 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001174 sync_buffer_->set_end_timestamp(timestamp_);
1175 playout_timestamp_ = timestamp_;
1176 }
1177 }
1178 }
1179
1180 if (reset_decoder_) {
1181 // TODO(hlundin): Write test for this.
1182 // Reset decoder.
1183 if (decoder) {
1184 decoder->Init();
1185 }
1186 // Reset comfort noise decoder.
1187 AudioDecoder* cng_decoder = decoder_database_->GetActiveCngDecoder();
1188 if (cng_decoder) {
1189 cng_decoder->Init();
1190 }
1191 reset_decoder_ = false;
1192 }
1193
1194#ifdef LEGACY_BITEXACT
1195 // Due to a bug in old SignalMCU, it could happen that CNG operation was
1196 // decided, but a speech packet was provided. The speech packet will be used
1197 // to update the comfort noise decoder, as if it was a SID frame, which is
1198 // clearly wrong.
1199 if (*operation == kRfc3389Cng) {
1200 return 0;
1201 }
1202#endif
1203
1204 *decoded_length = 0;
1205 // Update codec-internal PLC state.
1206 if ((*operation == kMerge) && decoder && decoder->HasDecodePlc()) {
1207 decoder->DecodePlc(1, &decoded_buffer_[*decoded_length]);
1208 }
1209
1210 int return_value = DecodeLoop(packet_list, operation, decoder,
1211 decoded_length, speech_type);
1212
1213 if (*decoded_length < 0) {
1214 // Error returned from the decoder.
1215 *decoded_length = 0;
Peter Kastingb7e50542015-06-11 12:55:50 -07001216 sync_buffer_->IncreaseEndTimestamp(
1217 static_cast<uint32_t>(decoder_frame_length_));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001218 int error_code = 0;
1219 if (decoder)
1220 error_code = decoder->ErrorCode();
1221 if (error_code != 0) {
1222 // Got some error code from the decoder.
1223 decoder_error_code_ = error_code;
1224 return_value = kDecoderErrorCode;
Henrik Lundind67a2192015-08-03 12:54:37 +02001225 LOG(LS_WARNING) << "Decoder returned error code: " << error_code;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001226 } else {
1227 // Decoder does not implement error codes. Return generic error.
1228 return_value = kOtherDecoderError;
Henrik Lundind67a2192015-08-03 12:54:37 +02001229 LOG(LS_WARNING) << "Decoder error (no error code)";
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001230 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001231 *operation = kExpand; // Do expansion to get data instead.
1232 }
1233 if (*speech_type != AudioDecoder::kComfortNoise) {
1234 // Don't increment timestamp if codec returned CNG speech type
1235 // since in this case, the we will increment the CNGplayedTS counter.
1236 // Increase with number of samples per channel.
1237 assert(*decoded_length == 0 ||
henrik.lundin@webrtc.org6dba1eb2015-03-18 09:47:08 +00001238 (decoder && decoder->Channels() == sync_buffer_->Channels()));
turaj@webrtc.org362a55e2013-09-20 16:25:28 +00001239 sync_buffer_->IncreaseEndTimestamp(
1240 *decoded_length / static_cast<int>(sync_buffer_->Channels()));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001241 }
1242 return return_value;
1243}
1244
1245int NetEqImpl::DecodeLoop(PacketList* packet_list, Operations* operation,
1246 AudioDecoder* decoder, int* decoded_length,
1247 AudioDecoder::SpeechType* speech_type) {
1248 Packet* packet = NULL;
1249 if (!packet_list->empty()) {
1250 packet = packet_list->front();
1251 }
1252 // Do decoding.
1253 while (packet &&
1254 !decoder_database_->IsComfortNoise(packet->header.payloadType)) {
1255 assert(decoder); // At this point, we must have a decoder object.
1256 // The number of channels in the |sync_buffer_| should be the same as the
1257 // number decoder channels.
henrik.lundin@webrtc.org6dba1eb2015-03-18 09:47:08 +00001258 assert(sync_buffer_->Channels() == decoder->Channels());
1259 assert(decoded_buffer_length_ >= kMaxFrameSize * decoder->Channels());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001260 assert(*operation == kNormal || *operation == kAccelerate ||
Henrik Lundincf808d22015-05-27 14:33:29 +02001261 *operation == kFastAccelerate || *operation == kMerge ||
1262 *operation == kPreemptiveExpand);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001263 packet_list->pop_front();
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001264 size_t payload_length = packet->payload_length;
Peter Kasting36b7cc32015-06-11 19:57:18 -07001265 int decode_length;
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001266 if (packet->sync_packet) {
1267 // Decode to silence with the same frame size as the last decode.
1268 LOG(LS_VERBOSE) << "Decoding sync-packet: " <<
1269 " ts=" << packet->header.timestamp <<
1270 ", sn=" << packet->header.sequenceNumber <<
1271 ", pt=" << static_cast<int>(packet->header.payloadType) <<
1272 ", ssrc=" << packet->header.ssrc <<
1273 ", len=" << packet->payload_length;
henrik.lundin@webrtc.org6dba1eb2015-03-18 09:47:08 +00001274 memset(&decoded_buffer_[*decoded_length], 0,
1275 decoder_frame_length_ * decoder->Channels() *
1276 sizeof(decoded_buffer_[0]));
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001277 decode_length = decoder_frame_length_;
1278 } else if (!packet->primary) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001279 // This is a redundant payload; call the special decoder method.
turaj@webrtc.org0c0fae82013-09-25 17:42:17 +00001280 LOG(LS_VERBOSE) << "Decoding packet (redundant):" <<
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001281 " ts=" << packet->header.timestamp <<
1282 ", sn=" << packet->header.sequenceNumber <<
1283 ", pt=" << static_cast<int>(packet->header.payloadType) <<
1284 ", ssrc=" << packet->header.ssrc <<
1285 ", len=" << packet->payload_length;
1286 decode_length = decoder->DecodeRedundant(
henrik.lundin@webrtc.org1eda4e32015-02-25 10:02:29 +00001287 packet->payload, packet->payload_length, fs_hz_,
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +00001288 (decoded_buffer_length_ - *decoded_length) * sizeof(int16_t),
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001289 &decoded_buffer_[*decoded_length], speech_type);
1290 } else {
turaj@webrtc.org0c0fae82013-09-25 17:42:17 +00001291 LOG(LS_VERBOSE) << "Decoding packet: ts=" << packet->header.timestamp <<
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001292 ", sn=" << packet->header.sequenceNumber <<
1293 ", pt=" << static_cast<int>(packet->header.payloadType) <<
1294 ", ssrc=" << packet->header.ssrc <<
1295 ", len=" << packet->payload_length;
henrik.lundin@webrtc.org1eda4e32015-02-25 10:02:29 +00001296 decode_length =
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +00001297 decoder->Decode(
1298 packet->payload, packet->payload_length, fs_hz_,
1299 (decoded_buffer_length_ - *decoded_length) * sizeof(int16_t),
1300 &decoded_buffer_[*decoded_length], speech_type);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001301 }
1302
1303 delete[] packet->payload;
1304 delete packet;
turaj@webrtc.org58cd3162013-10-31 15:15:55 +00001305 packet = NULL;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001306 if (decode_length > 0) {
1307 *decoded_length += decode_length;
1308 // Update |decoder_frame_length_| with number of samples per channel.
henrik.lundin@webrtc.org6dba1eb2015-03-18 09:47:08 +00001309 decoder_frame_length_ =
1310 decode_length / static_cast<int>(decoder->Channels());
1311 LOG(LS_VERBOSE) << "Decoded " << decode_length << " samples ("
1312 << decoder->Channels() << " channel(s) -> "
1313 << decoder_frame_length_ << " samples per channel)";
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001314 } else if (decode_length < 0) {
1315 // Error.
Henrik Lundind67a2192015-08-03 12:54:37 +02001316 LOG(LS_WARNING) << "Decode " << decode_length << " " << payload_length;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001317 *decoded_length = -1;
1318 PacketBuffer::DeleteAllPackets(packet_list);
1319 break;
1320 }
1321 if (*decoded_length > static_cast<int>(decoded_buffer_length_)) {
1322 // Guard against overflow.
Henrik Lundind67a2192015-08-03 12:54:37 +02001323 LOG(LS_WARNING) << "Decoded too much.";
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001324 PacketBuffer::DeleteAllPackets(packet_list);
1325 return kDecodedTooMuch;
1326 }
1327 if (!packet_list->empty()) {
1328 packet = packet_list->front();
1329 } else {
1330 packet = NULL;
1331 }
1332 } // End of decode loop.
1333
turaj@webrtc.org58cd3162013-10-31 15:15:55 +00001334 // If the list is not empty at this point, either a decoding error terminated
1335 // the while-loop, or list must hold exactly one CNG packet.
1336 assert(packet_list->empty() || *decoded_length < 0 ||
1337 (packet_list->size() == 1 && packet &&
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001338 decoder_database_->IsComfortNoise(packet->header.payloadType)));
1339 return 0;
1340}
1341
1342void NetEqImpl::DoNormal(const int16_t* decoded_buffer, size_t decoded_length,
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001343 AudioDecoder::SpeechType speech_type, bool play_dtmf) {
henrik.lundin@webrtc.org40d3fc62013-09-18 12:19:50 +00001344 assert(normal_.get());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001345 assert(mute_factor_array_.get());
henrik.lundin@webrtc.org40d3fc62013-09-18 12:19:50 +00001346 normal_->Process(decoded_buffer, decoded_length, last_mode_,
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +00001347 mute_factor_array_.get(), algorithm_buffer_.get());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001348 if (decoded_length != 0) {
1349 last_mode_ = kModeNormal;
1350 }
1351
1352 // If last packet was decoded as an inband CNG, set mode to CNG instead.
1353 if ((speech_type == AudioDecoder::kComfortNoise)
1354 || ((last_mode_ == kModeCodecInternalCng)
1355 && (decoded_length == 0))) {
1356 // TODO(hlundin): Remove second part of || statement above.
1357 last_mode_ = kModeCodecInternalCng;
1358 }
1359
1360 if (!play_dtmf) {
1361 dtmf_tone_generator_->Reset();
1362 }
1363}
1364
1365void NetEqImpl::DoMerge(int16_t* decoded_buffer, size_t decoded_length,
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001366 AudioDecoder::SpeechType speech_type, bool play_dtmf) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001367 assert(mute_factor_array_.get());
henrik.lundin@webrtc.org40d3fc62013-09-18 12:19:50 +00001368 assert(merge_.get());
1369 int new_length = merge_->Process(decoded_buffer, decoded_length,
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +00001370 mute_factor_array_.get(),
1371 algorithm_buffer_.get());
minyue@webrtc.orgc11348b2015-02-10 08:35:38 +00001372 int expand_length_correction = new_length -
1373 static_cast<int>(decoded_length / algorithm_buffer_->Channels());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001374
1375 // Update in-call and post-call statistics.
1376 if (expand_->MuteFactor(0) == 0) {
1377 // Expand generates only noise.
minyue@webrtc.orgc11348b2015-02-10 08:35:38 +00001378 stats_.ExpandedNoiseSamples(expand_length_correction);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001379 } else {
1380 // Expansion generates more than only noise.
minyue@webrtc.orgc11348b2015-02-10 08:35:38 +00001381 stats_.ExpandedVoiceSamples(expand_length_correction);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001382 }
1383
1384 last_mode_ = kModeMerge;
1385 // If last packet was decoded as an inband CNG, set mode to CNG instead.
1386 if (speech_type == AudioDecoder::kComfortNoise) {
1387 last_mode_ = kModeCodecInternalCng;
1388 }
1389 expand_->Reset();
1390 if (!play_dtmf) {
1391 dtmf_tone_generator_->Reset();
1392 }
1393}
1394
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001395int NetEqImpl::DoExpand(bool play_dtmf) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001396 while ((sync_buffer_->FutureLength() - expand_->overlap_length()) <
1397 static_cast<size_t>(output_size_samples_)) {
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001398 algorithm_buffer_->Clear();
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +00001399 int return_value = expand_->Process(algorithm_buffer_.get());
turaj@webrtc.org362a55e2013-09-20 16:25:28 +00001400 int length = static_cast<int>(algorithm_buffer_->Size());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001401
1402 // Update in-call and post-call statistics.
1403 if (expand_->MuteFactor(0) == 0) {
1404 // Expand operation generates only noise.
1405 stats_.ExpandedNoiseSamples(length);
1406 } else {
1407 // Expand operation generates more than only noise.
1408 stats_.ExpandedVoiceSamples(length);
1409 }
1410
1411 last_mode_ = kModeExpand;
1412
1413 if (return_value < 0) {
1414 return return_value;
1415 }
1416
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001417 sync_buffer_->PushBack(*algorithm_buffer_);
1418 algorithm_buffer_->Clear();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001419 }
1420 if (!play_dtmf) {
1421 dtmf_tone_generator_->Reset();
1422 }
1423 return 0;
1424}
1425
Henrik Lundincf808d22015-05-27 14:33:29 +02001426int NetEqImpl::DoAccelerate(int16_t* decoded_buffer,
1427 size_t decoded_length,
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001428 AudioDecoder::SpeechType speech_type,
Henrik Lundincf808d22015-05-27 14:33:29 +02001429 bool play_dtmf,
1430 bool fast_accelerate) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001431 const size_t required_samples = 240 * fs_mult_; // Must have 30 ms.
turaj@webrtc.org362a55e2013-09-20 16:25:28 +00001432 size_t borrowed_samples_per_channel = 0;
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001433 size_t num_channels = algorithm_buffer_->Channels();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001434 size_t decoded_length_per_channel = decoded_length / num_channels;
1435 if (decoded_length_per_channel < required_samples) {
1436 // Must move data from the |sync_buffer_| in order to get 30 ms.
turaj@webrtc.org362a55e2013-09-20 16:25:28 +00001437 borrowed_samples_per_channel = static_cast<int>(required_samples -
1438 decoded_length_per_channel);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001439 memmove(&decoded_buffer[borrowed_samples_per_channel * num_channels],
1440 decoded_buffer,
1441 sizeof(int16_t) * decoded_length);
1442 sync_buffer_->ReadInterleavedFromEnd(borrowed_samples_per_channel,
1443 decoded_buffer);
1444 decoded_length = required_samples * num_channels;
1445 }
1446
1447 int16_t samples_removed;
Henrik Lundincf808d22015-05-27 14:33:29 +02001448 Accelerate::ReturnCodes return_code =
1449 accelerate_->Process(decoded_buffer, decoded_length, fast_accelerate,
1450 algorithm_buffer_.get(), &samples_removed);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001451 stats_.AcceleratedSamples(samples_removed);
1452 switch (return_code) {
1453 case Accelerate::kSuccess:
1454 last_mode_ = kModeAccelerateSuccess;
1455 break;
1456 case Accelerate::kSuccessLowEnergy:
1457 last_mode_ = kModeAccelerateLowEnergy;
1458 break;
1459 case Accelerate::kNoStretch:
1460 last_mode_ = kModeAccelerateFail;
1461 break;
1462 case Accelerate::kError:
1463 // TODO(hlundin): Map to kModeError instead?
1464 last_mode_ = kModeAccelerateFail;
1465 return kAccelerateError;
1466 }
1467
1468 if (borrowed_samples_per_channel > 0) {
1469 // Copy borrowed samples back to the |sync_buffer_|.
turaj@webrtc.org362a55e2013-09-20 16:25:28 +00001470 size_t length = algorithm_buffer_->Size();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001471 if (length < borrowed_samples_per_channel) {
1472 // This destroys the beginning of the buffer, but will not cause any
1473 // problems.
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001474 sync_buffer_->ReplaceAtIndex(*algorithm_buffer_,
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001475 sync_buffer_->Size() -
1476 borrowed_samples_per_channel);
1477 sync_buffer_->PushFrontZeros(borrowed_samples_per_channel - length);
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001478 algorithm_buffer_->PopFront(length);
1479 assert(algorithm_buffer_->Empty());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001480 } else {
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001481 sync_buffer_->ReplaceAtIndex(*algorithm_buffer_,
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001482 borrowed_samples_per_channel,
1483 sync_buffer_->Size() -
1484 borrowed_samples_per_channel);
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001485 algorithm_buffer_->PopFront(borrowed_samples_per_channel);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001486 }
1487 }
1488
1489 // If last packet was decoded as an inband CNG, set mode to CNG instead.
1490 if (speech_type == AudioDecoder::kComfortNoise) {
1491 last_mode_ = kModeCodecInternalCng;
1492 }
1493 if (!play_dtmf) {
1494 dtmf_tone_generator_->Reset();
1495 }
1496 expand_->Reset();
1497 return 0;
1498}
1499
1500int NetEqImpl::DoPreemptiveExpand(int16_t* decoded_buffer,
1501 size_t decoded_length,
1502 AudioDecoder::SpeechType speech_type,
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001503 bool play_dtmf) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001504 const size_t required_samples = 240 * fs_mult_; // Must have 30 ms.
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001505 size_t num_channels = algorithm_buffer_->Channels();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001506 int borrowed_samples_per_channel = 0;
1507 int old_borrowed_samples_per_channel = 0;
1508 size_t decoded_length_per_channel = decoded_length / num_channels;
1509 if (decoded_length_per_channel < required_samples) {
1510 // Must move data from the |sync_buffer_| in order to get 30 ms.
turaj@webrtc.org362a55e2013-09-20 16:25:28 +00001511 borrowed_samples_per_channel = static_cast<int>(required_samples -
1512 decoded_length_per_channel);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001513 // Calculate how many of these were already played out.
Peter Kastingf045e4d2015-06-10 21:15:38 -07001514 const int future_length = static_cast<int>(sync_buffer_->FutureLength());
1515 old_borrowed_samples_per_channel =
1516 (borrowed_samples_per_channel > future_length) ?
1517 (borrowed_samples_per_channel - future_length) : 0;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001518 memmove(&decoded_buffer[borrowed_samples_per_channel * num_channels],
1519 decoded_buffer,
1520 sizeof(int16_t) * decoded_length);
1521 sync_buffer_->ReadInterleavedFromEnd(borrowed_samples_per_channel,
1522 decoded_buffer);
1523 decoded_length = required_samples * num_channels;
1524 }
1525
1526 int16_t samples_added;
henrik.lundin@webrtc.org40d3fc62013-09-18 12:19:50 +00001527 PreemptiveExpand::ReturnCodes return_code = preemptive_expand_->Process(
turaj@webrtc.org362a55e2013-09-20 16:25:28 +00001528 decoded_buffer, static_cast<int>(decoded_length),
1529 old_borrowed_samples_per_channel,
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +00001530 algorithm_buffer_.get(), &samples_added);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001531 stats_.PreemptiveExpandedSamples(samples_added);
1532 switch (return_code) {
1533 case PreemptiveExpand::kSuccess:
1534 last_mode_ = kModePreemptiveExpandSuccess;
1535 break;
1536 case PreemptiveExpand::kSuccessLowEnergy:
1537 last_mode_ = kModePreemptiveExpandLowEnergy;
1538 break;
1539 case PreemptiveExpand::kNoStretch:
1540 last_mode_ = kModePreemptiveExpandFail;
1541 break;
1542 case PreemptiveExpand::kError:
1543 // TODO(hlundin): Map to kModeError instead?
1544 last_mode_ = kModePreemptiveExpandFail;
1545 return kPreemptiveExpandError;
1546 }
1547
1548 if (borrowed_samples_per_channel > 0) {
1549 // Copy borrowed samples back to the |sync_buffer_|.
1550 sync_buffer_->ReplaceAtIndex(
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001551 *algorithm_buffer_, borrowed_samples_per_channel,
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001552 sync_buffer_->Size() - borrowed_samples_per_channel);
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001553 algorithm_buffer_->PopFront(borrowed_samples_per_channel);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001554 }
1555
1556 // If last packet was decoded as an inband CNG, set mode to CNG instead.
1557 if (speech_type == AudioDecoder::kComfortNoise) {
1558 last_mode_ = kModeCodecInternalCng;
1559 }
1560 if (!play_dtmf) {
1561 dtmf_tone_generator_->Reset();
1562 }
1563 expand_->Reset();
1564 return 0;
1565}
1566
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001567int NetEqImpl::DoRfc3389Cng(PacketList* packet_list, bool play_dtmf) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001568 if (!packet_list->empty()) {
1569 // Must have exactly one SID frame at this point.
1570 assert(packet_list->size() == 1);
1571 Packet* packet = packet_list->front();
1572 packet_list->pop_front();
henrik.lundin@webrtc.org73deaad2013-01-31 13:32:51 +00001573 if (!decoder_database_->IsComfortNoise(packet->header.payloadType)) {
1574#ifdef LEGACY_BITEXACT
1575 // This can happen due to a bug in GetDecision. Change the payload type
1576 // to a CNG type, and move on. Note that this means that we are in fact
1577 // sending a non-CNG payload to the comfort noise decoder for decoding.
1578 // Clearly wrong, but will maintain bit-exactness with legacy.
1579 if (fs_hz_ == 8000) {
1580 packet->header.payloadType =
1581 decoder_database_->GetRtpPayloadType(kDecoderCNGnb);
1582 } else if (fs_hz_ == 16000) {
1583 packet->header.payloadType =
1584 decoder_database_->GetRtpPayloadType(kDecoderCNGwb);
1585 } else if (fs_hz_ == 32000) {
1586 packet->header.payloadType =
1587 decoder_database_->GetRtpPayloadType(kDecoderCNGswb32kHz);
1588 } else if (fs_hz_ == 48000) {
1589 packet->header.payloadType =
1590 decoder_database_->GetRtpPayloadType(kDecoderCNGswb48kHz);
1591 }
1592 assert(decoder_database_->IsComfortNoise(packet->header.payloadType));
1593#else
1594 LOG(LS_ERROR) << "Trying to decode non-CNG payload as CNG.";
1595 return kOtherError;
1596#endif
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001597 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001598 // UpdateParameters() deletes |packet|.
1599 if (comfort_noise_->UpdateParameters(packet) ==
1600 ComfortNoise::kInternalError) {
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001601 algorithm_buffer_->Zeros(output_size_samples_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001602 return -comfort_noise_->internal_error_code();
1603 }
1604 }
1605 int cn_return = comfort_noise_->Generate(output_size_samples_,
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +00001606 algorithm_buffer_.get());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001607 expand_->Reset();
1608 last_mode_ = kModeRfc3389Cng;
1609 if (!play_dtmf) {
1610 dtmf_tone_generator_->Reset();
1611 }
1612 if (cn_return == ComfortNoise::kInternalError) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001613 decoder_error_code_ = comfort_noise_->internal_error_code();
1614 return kComfortNoiseErrorCode;
1615 } else if (cn_return == ComfortNoise::kUnknownPayloadType) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001616 return kUnknownRtpPayloadType;
1617 }
1618 return 0;
1619}
1620
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001621void NetEqImpl::DoCodecInternalCng() {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001622 int length = 0;
1623 // TODO(hlundin): Will probably need a longer buffer for multi-channel.
1624 int16_t decoded_buffer[kMaxFrameSize];
1625 AudioDecoder* decoder = decoder_database_->GetActiveDecoder();
1626 if (decoder) {
1627 const uint8_t* dummy_payload = NULL;
1628 AudioDecoder::SpeechType speech_type;
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +00001629 length = decoder->Decode(
1630 dummy_payload, 0, fs_hz_, kMaxFrameSize * sizeof(int16_t),
1631 decoded_buffer, &speech_type);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001632 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001633 assert(mute_factor_array_.get());
henrik.lundin@webrtc.org40d3fc62013-09-18 12:19:50 +00001634 normal_->Process(decoded_buffer, length, last_mode_, mute_factor_array_.get(),
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +00001635 algorithm_buffer_.get());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001636 last_mode_ = kModeCodecInternalCng;
1637 expand_->Reset();
1638}
1639
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001640int NetEqImpl::DoDtmf(const DtmfEvent& dtmf_event, bool* play_dtmf) {
turaj@webrtc.org4d06db52013-03-27 18:31:42 +00001641 // This block of the code and the block further down, handling |dtmf_switch|
1642 // are commented out. Otherwise playing out-of-band DTMF would fail in VoE
1643 // test, DtmfTest.ManualSuccessfullySendsOutOfBandTelephoneEvents. This is
1644 // equivalent to |dtmf_switch| always be false.
1645 //
1646 // See http://webrtc-codereview.appspot.com/1195004/ for discussion
1647 // On this issue. This change might cause some glitches at the point of
1648 // switch from audio to DTMF. Issue 1545 is filed to track this.
1649 //
1650 // bool dtmf_switch = false;
1651 // if ((last_mode_ != kModeDtmf) && dtmf_tone_generator_->initialized()) {
1652 // // Special case; see below.
1653 // // We must catch this before calling Generate, since |initialized| is
1654 // // modified in that call.
1655 // dtmf_switch = true;
1656 // }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001657
1658 int dtmf_return_value = 0;
1659 if (!dtmf_tone_generator_->initialized()) {
1660 // Initialize if not already done.
1661 dtmf_return_value = dtmf_tone_generator_->Init(fs_hz_, dtmf_event.event_no,
1662 dtmf_event.volume);
1663 }
turaj@webrtc.org4d06db52013-03-27 18:31:42 +00001664
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001665 if (dtmf_return_value == 0) {
1666 // Generate DTMF signal.
1667 dtmf_return_value = dtmf_tone_generator_->Generate(output_size_samples_,
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +00001668 algorithm_buffer_.get());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001669 }
turaj@webrtc.org4d06db52013-03-27 18:31:42 +00001670
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001671 if (dtmf_return_value < 0) {
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001672 algorithm_buffer_->Zeros(output_size_samples_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001673 return dtmf_return_value;
1674 }
1675
turaj@webrtc.org4d06db52013-03-27 18:31:42 +00001676 // if (dtmf_switch) {
1677 // // This is the special case where the previous operation was DTMF
1678 // // overdub, but the current instruction is "regular" DTMF. We must make
1679 // // sure that the DTMF does not have any discontinuities. The first DTMF
1680 // // sample that we generate now must be played out immediately, therefore
1681 // // it must be copied to the speech buffer.
1682 // // TODO(hlundin): This code seems incorrect. (Legacy.) Write test and
1683 // // verify correct operation.
1684 // assert(false);
1685 // // Must generate enough data to replace all of the |sync_buffer_|
1686 // // "future".
1687 // int required_length = sync_buffer_->FutureLength();
1688 // assert(dtmf_tone_generator_->initialized());
1689 // dtmf_return_value = dtmf_tone_generator_->Generate(required_length,
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001690 // algorithm_buffer_);
1691 // assert((size_t) required_length == algorithm_buffer_->Size());
turaj@webrtc.org4d06db52013-03-27 18:31:42 +00001692 // if (dtmf_return_value < 0) {
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001693 // algorithm_buffer_->Zeros(output_size_samples_);
turaj@webrtc.org4d06db52013-03-27 18:31:42 +00001694 // return dtmf_return_value;
1695 // }
1696 //
1697 // // Overwrite the "future" part of the speech buffer with the new DTMF
1698 // // data.
1699 // // TODO(hlundin): It seems that this overwriting has gone lost.
1700 // // Not adapted for multi-channel yet.
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001701 // assert(algorithm_buffer_->Channels() == 1);
1702 // if (algorithm_buffer_->Channels() != 1) {
turaj@webrtc.org4d06db52013-03-27 18:31:42 +00001703 // LOG(LS_WARNING) << "DTMF not supported for more than one channel";
1704 // return kStereoNotSupported;
1705 // }
1706 // // Shuffle the remaining data to the beginning of algorithm buffer.
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001707 // algorithm_buffer_->PopFront(sync_buffer_->FutureLength());
turaj@webrtc.org4d06db52013-03-27 18:31:42 +00001708 // }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001709
Peter Kastingb7e50542015-06-11 12:55:50 -07001710 sync_buffer_->IncreaseEndTimestamp(
1711 static_cast<uint32_t>(output_size_samples_));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001712 expand_->Reset();
1713 last_mode_ = kModeDtmf;
1714
1715 // Set to false because the DTMF is already in the algorithm buffer.
1716 *play_dtmf = false;
1717 return 0;
1718}
1719
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001720void NetEqImpl::DoAlternativePlc(bool increase_timestamp) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001721 AudioDecoder* decoder = decoder_database_->GetActiveDecoder();
1722 int length;
1723 if (decoder && decoder->HasDecodePlc()) {
1724 // Use the decoder's packet-loss concealment.
1725 // TODO(hlundin): Will probably need a longer buffer for multi-channel.
1726 int16_t decoded_buffer[kMaxFrameSize];
1727 length = decoder->DecodePlc(1, decoded_buffer);
1728 if (length > 0) {
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001729 algorithm_buffer_->PushBackInterleaved(decoded_buffer, length);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001730 } else {
1731 length = 0;
1732 }
1733 } else {
1734 // Do simple zero-stuffing.
1735 length = output_size_samples_;
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001736 algorithm_buffer_->Zeros(length);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001737 // By not advancing the timestamp, NetEq inserts samples.
1738 stats_.AddZeros(length);
1739 }
1740 if (increase_timestamp) {
Peter Kastingb7e50542015-06-11 12:55:50 -07001741 sync_buffer_->IncreaseEndTimestamp(static_cast<uint32_t>(length));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001742 }
1743 expand_->Reset();
1744}
1745
1746int NetEqImpl::DtmfOverdub(const DtmfEvent& dtmf_event, size_t num_channels,
1747 int16_t* output) const {
1748 size_t out_index = 0;
1749 int overdub_length = output_size_samples_; // Default value.
1750
1751 if (sync_buffer_->dtmf_index() > sync_buffer_->next_index()) {
1752 // Special operation for transition from "DTMF only" to "DTMF overdub".
1753 out_index = std::min(
1754 sync_buffer_->dtmf_index() - sync_buffer_->next_index(),
1755 static_cast<size_t>(output_size_samples_));
turaj@webrtc.org362a55e2013-09-20 16:25:28 +00001756 overdub_length = output_size_samples_ - static_cast<int>(out_index);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001757 }
1758
henrik.lundin@webrtc.orgfd11bbf2013-09-30 20:38:44 +00001759 AudioMultiVector dtmf_output(num_channels);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001760 int dtmf_return_value = 0;
1761 if (!dtmf_tone_generator_->initialized()) {
1762 dtmf_return_value = dtmf_tone_generator_->Init(fs_hz_, dtmf_event.event_no,
1763 dtmf_event.volume);
1764 }
1765 if (dtmf_return_value == 0) {
1766 dtmf_return_value = dtmf_tone_generator_->Generate(overdub_length,
1767 &dtmf_output);
1768 assert((size_t) overdub_length == dtmf_output.Size());
1769 }
1770 dtmf_output.ReadInterleaved(overdub_length, &output[out_index]);
1771 return dtmf_return_value < 0 ? dtmf_return_value : 0;
1772}
1773
1774int NetEqImpl::ExtractPackets(int required_samples, PacketList* packet_list) {
1775 bool first_packet = true;
1776 uint8_t prev_payload_type = 0;
1777 uint32_t prev_timestamp = 0;
1778 uint16_t prev_sequence_number = 0;
1779 bool next_packet_available = false;
1780
henrik.lundin@webrtc.orge1d468c2013-01-30 07:37:20 +00001781 const RTPHeader* header = packet_buffer_->NextRtpHeader();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001782 assert(header);
1783 if (!header) {
Henrik Lundind67a2192015-08-03 12:54:37 +02001784 LOG(LS_ERROR) << "Packet buffer unexpectedly empty.";
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001785 return -1;
1786 }
turaj@webrtc.org7df97062013-08-02 18:07:13 +00001787 uint32_t first_timestamp = header->timestamp;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001788 int extracted_samples = 0;
1789
1790 // Packet extraction loop.
1791 do {
1792 timestamp_ = header->timestamp;
1793 int discard_count = 0;
henrik.lundin@webrtc.orge1d468c2013-01-30 07:37:20 +00001794 Packet* packet = packet_buffer_->GetNextPacket(&discard_count);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001795 // |header| may be invalid after the |packet_buffer_| operation.
1796 header = NULL;
1797 if (!packet) {
Henrik Lundind67a2192015-08-03 12:54:37 +02001798 LOG(LS_ERROR) << "Should always be able to extract a packet here";
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001799 assert(false); // Should always be able to extract a packet here.
1800 return -1;
1801 }
1802 stats_.PacketsDiscarded(discard_count);
1803 // Store waiting time in ms; packets->waiting_time is in "output blocks".
1804 stats_.StoreWaitingTime(packet->waiting_time * kOutputSizeMs);
1805 assert(packet->payload_length > 0);
1806 packet_list->push_back(packet); // Store packet in list.
1807
1808 if (first_packet) {
1809 first_packet = false;
minyue@webrtc.orgd7301772013-08-29 00:58:14 +00001810 decoded_packet_sequence_number_ = prev_sequence_number =
1811 packet->header.sequenceNumber;
1812 decoded_packet_timestamp_ = prev_timestamp = packet->header.timestamp;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001813 prev_payload_type = packet->header.payloadType;
1814 }
1815
1816 // Store number of extracted samples.
1817 int packet_duration = 0;
1818 AudioDecoder* decoder = decoder_database_->GetDecoder(
1819 packet->header.payloadType);
1820 if (decoder) {
minyue@webrtc.orgb28bfa72014-03-21 12:07:40 +00001821 if (packet->sync_packet) {
1822 packet_duration = decoder_frame_length_;
1823 } else {
minyue@webrtc.org2c1bcf22015-02-17 10:17:09 +00001824 if (packet->primary) {
1825 packet_duration = decoder->PacketDuration(packet->payload,
1826 packet->payload_length);
1827 } else {
1828 packet_duration = decoder->
1829 PacketDurationRedundant(packet->payload, packet->payload_length);
1830 stats_.SecondaryDecodedSamples(packet_duration);
1831 }
minyue@webrtc.orgb28bfa72014-03-21 12:07:40 +00001832 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001833 } else {
Henrik Lundind67a2192015-08-03 12:54:37 +02001834 LOG(LS_WARNING) << "Unknown payload type "
1835 << static_cast<int>(packet->header.payloadType);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001836 assert(false);
1837 }
1838 if (packet_duration <= 0) {
1839 // Decoder did not return a packet duration. Assume that the packet
1840 // contains the same number of samples as the previous one.
1841 packet_duration = decoder_frame_length_;
1842 }
1843 extracted_samples = packet->header.timestamp - first_timestamp +
1844 packet_duration;
1845
1846 // Check what packet is available next.
1847 header = packet_buffer_->NextRtpHeader();
1848 next_packet_available = false;
1849 if (header && prev_payload_type == header->payloadType) {
1850 int16_t seq_no_diff = header->sequenceNumber - prev_sequence_number;
1851 int32_t ts_diff = header->timestamp - prev_timestamp;
1852 if (seq_no_diff == 1 ||
1853 (seq_no_diff == 0 && ts_diff == decoder_frame_length_)) {
1854 // The next sequence number is available, or the next part of a packet
1855 // that was split into pieces upon insertion.
1856 next_packet_available = true;
1857 }
1858 prev_sequence_number = header->sequenceNumber;
1859 }
1860 } while (extracted_samples < required_samples && next_packet_available);
1861
henrik.lundin@webrtc.org61217152014-09-22 08:30:07 +00001862 if (extracted_samples > 0) {
1863 // Delete old packets only when we are going to decode something. Otherwise,
1864 // we could end up in the situation where we never decode anything, since
1865 // all incoming packets are considered too old but the buffer will also
1866 // never be flooded and flushed.
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +00001867 packet_buffer_->DiscardAllOldPackets(timestamp_);
henrik.lundin@webrtc.org61217152014-09-22 08:30:07 +00001868 }
1869
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001870 return extracted_samples;
1871}
1872
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001873void NetEqImpl::UpdatePlcComponents(int fs_hz, size_t channels) {
1874 // Delete objects and create new ones.
1875 expand_.reset(expand_factory_->Create(background_noise_.get(),
1876 sync_buffer_.get(), &random_vector_,
1877 fs_hz, channels));
1878 merge_.reset(new Merge(fs_hz, channels, expand_.get(), sync_buffer_.get()));
1879}
1880
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001881void NetEqImpl::SetSampleRateAndChannels(int fs_hz, size_t channels) {
Henrik Lundind67a2192015-08-03 12:54:37 +02001882 LOG(LS_VERBOSE) << "SetSampleRateAndChannels " << fs_hz << " " << channels;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001883 // TODO(hlundin): Change to an enumerator and skip assert.
1884 assert(fs_hz == 8000 || fs_hz == 16000 || fs_hz == 32000 || fs_hz == 48000);
1885 assert(channels > 0);
1886
1887 fs_hz_ = fs_hz;
1888 fs_mult_ = fs_hz / 8000;
1889 output_size_samples_ = kOutputSizeMs * 8 * fs_mult_;
1890 decoder_frame_length_ = 3 * output_size_samples_; // Initialize to 30ms.
1891
1892 last_mode_ = kModeNormal;
1893
1894 // Create a new array of mute factors and set all to 1.
1895 mute_factor_array_.reset(new int16_t[channels]);
1896 for (size_t i = 0; i < channels; ++i) {
1897 mute_factor_array_[i] = 16384; // 1.0 in Q14.
1898 }
1899
1900 // Reset comfort noise decoder, if there is one active.
1901 AudioDecoder* cng_decoder = decoder_database_->GetActiveCngDecoder();
1902 if (cng_decoder) {
1903 cng_decoder->Init();
1904 }
1905
1906 // Reinit post-decode VAD with new sample rate.
1907 assert(vad_.get()); // Cannot be NULL here.
1908 vad_->Init();
1909
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001910 // Delete algorithm buffer and create a new one.
henrik.lundin@webrtc.orgfd11bbf2013-09-30 20:38:44 +00001911 algorithm_buffer_.reset(new AudioMultiVector(channels));
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001912
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001913 // Delete sync buffer and create a new one.
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +00001914 sync_buffer_.reset(new SyncBuffer(channels, kSyncBufferSize * fs_mult_));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001915
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00001916 // Delete BackgroundNoise object and create a new one.
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +00001917 background_noise_.reset(new BackgroundNoise(channels));
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00001918 background_noise_->set_mode(background_noise_mode_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001919
1920 // Reset random vector.
1921 random_vector_.Reset();
1922
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001923 UpdatePlcComponents(fs_hz, channels);
1924
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001925 // Move index so that we create a small set of future samples (all 0).
1926 sync_buffer_->set_next_index(sync_buffer_->next_index() -
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001927 expand_->overlap_length());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001928
henrik.lundin@webrtc.org40d3fc62013-09-18 12:19:50 +00001929 normal_.reset(new Normal(fs_hz, decoder_database_.get(), *background_noise_,
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +00001930 expand_.get()));
henrik.lundin@webrtc.orgd9faa462014-01-14 10:18:45 +00001931 accelerate_.reset(
1932 accelerate_factory_->Create(fs_hz, channels, *background_noise_));
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001933 preemptive_expand_.reset(preemptive_expand_factory_->Create(
1934 fs_hz, channels,
1935 *background_noise_,
1936 static_cast<int>(expand_->overlap_length())));
henrik.lundin@webrtc.org40d3fc62013-09-18 12:19:50 +00001937
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001938 // Delete ComfortNoise object and create a new one.
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +00001939 comfort_noise_.reset(new ComfortNoise(fs_hz, decoder_database_.get(),
1940 sync_buffer_.get()));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001941
1942 // Verify that |decoded_buffer_| is long enough.
1943 if (decoded_buffer_length_ < kMaxFrameSize * channels) {
1944 // Reallocate to larger size.
1945 decoded_buffer_length_ = kMaxFrameSize * channels;
1946 decoded_buffer_.reset(new int16_t[decoded_buffer_length_]);
1947 }
1948
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001949 // Create DecisionLogic if it is not created yet, then communicate new sample
1950 // rate and output size to DecisionLogic object.
1951 if (!decision_logic_.get()) {
henrik.lundin@webrtc.org7cbc4f92014-10-07 06:37:39 +00001952 CreateDecisionLogic();
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001953 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001954 decision_logic_->SetSampleRate(fs_hz_, output_size_samples_);
1955}
1956
1957NetEqOutputType NetEqImpl::LastOutputType() {
1958 assert(vad_.get());
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +00001959 assert(expand_.get());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001960 if (last_mode_ == kModeCodecInternalCng || last_mode_ == kModeRfc3389Cng) {
1961 return kOutputCNG;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001962 } else if (last_mode_ == kModeExpand && expand_->MuteFactor(0) == 0) {
1963 // Expand mode has faded down to background noise only (very long expand).
1964 return kOutputPLCtoCNG;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001965 } else if (last_mode_ == kModeExpand) {
1966 return kOutputPLC;
wu@webrtc.org24301a62013-12-13 19:17:43 +00001967 } else if (vad_->running() && !vad_->active_speech()) {
1968 return kOutputVADPassive;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001969 } else {
1970 return kOutputNormal;
1971 }
1972}
1973
henrik.lundin@webrtc.org7cbc4f92014-10-07 06:37:39 +00001974void NetEqImpl::CreateDecisionLogic() {
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001975 decision_logic_.reset(DecisionLogic::Create(fs_hz_, output_size_samples_,
henrik.lundin@webrtc.org7cbc4f92014-10-07 06:37:39 +00001976 playout_mode_,
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001977 decoder_database_.get(),
1978 *packet_buffer_.get(),
1979 delay_manager_.get(),
1980 buffer_level_filter_.get()));
1981}
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001982} // namespace webrtc