blob: 6598a790c5236b833fe2ee1815e0524397a66046 [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
18#include "webrtc/common_audio/signal_processing/include/signal_processing_library.h"
kwiberg@webrtc.orge04a93b2014-12-09 10:12:53 +000019#include "webrtc/modules/audio_coding/codecs/audio_decoder.h"
henrik.lundin@webrtc.org9c55f0f2014-06-09 08:10:28 +000020#include "webrtc/modules/audio_coding/neteq/accelerate.h"
21#include "webrtc/modules/audio_coding/neteq/background_noise.h"
22#include "webrtc/modules/audio_coding/neteq/buffer_level_filter.h"
23#include "webrtc/modules/audio_coding/neteq/comfort_noise.h"
24#include "webrtc/modules/audio_coding/neteq/decision_logic.h"
25#include "webrtc/modules/audio_coding/neteq/decoder_database.h"
26#include "webrtc/modules/audio_coding/neteq/defines.h"
27#include "webrtc/modules/audio_coding/neteq/delay_manager.h"
28#include "webrtc/modules/audio_coding/neteq/delay_peak_detector.h"
29#include "webrtc/modules/audio_coding/neteq/dtmf_buffer.h"
30#include "webrtc/modules/audio_coding/neteq/dtmf_tone_generator.h"
31#include "webrtc/modules/audio_coding/neteq/expand.h"
henrik.lundin@webrtc.org9c55f0f2014-06-09 08:10:28 +000032#include "webrtc/modules/audio_coding/neteq/merge.h"
33#include "webrtc/modules/audio_coding/neteq/normal.h"
34#include "webrtc/modules/audio_coding/neteq/packet_buffer.h"
35#include "webrtc/modules/audio_coding/neteq/packet.h"
36#include "webrtc/modules/audio_coding/neteq/payload_splitter.h"
37#include "webrtc/modules/audio_coding/neteq/post_decode_vad.h"
38#include "webrtc/modules/audio_coding/neteq/preemptive_expand.h"
39#include "webrtc/modules/audio_coding/neteq/sync_buffer.h"
40#include "webrtc/modules/audio_coding/neteq/timestamp_scaler.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000041#include "webrtc/modules/interface/module_common_types.h"
42#include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
43#include "webrtc/system_wrappers/interface/logging.h"
44
45// Modify the code to obtain backwards bit-exactness. Once bit-exactness is no
46// longer required, this #define should be removed (and the code that it
47// enables).
48#define LEGACY_BITEXACT
49
50namespace webrtc {
51
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 }
andrew@webrtc.org0569d932014-04-09 17:48:48 +0000105 LOG(LS_VERBOSE) << "Create NetEqImpl object with fs = " << fs << ".";
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000106 fs_hz_ = fs;
107 fs_mult_ = fs / 8000;
108 output_size_samples_ = kOutputSizeMs * 8 * fs_mult_;
109 decoder_frame_length_ = 3 * output_size_samples_;
110 WebRtcSpl_Init();
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +0000111 if (create_components) {
112 SetSampleRateAndChannels(fs, 1); // Default is 1 channel.
113 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000114}
115
116NetEqImpl::~NetEqImpl() {
117 LOG(LS_INFO) << "Deleting NetEqImpl object.";
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000118}
119
120int NetEqImpl::InsertPacket(const WebRtcRTPHeader& rtp_header,
121 const uint8_t* payload,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000122 size_t length_bytes,
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000123 uint32_t receive_timestamp) {
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +0000124 CriticalSectionScoped lock(crit_sect_.get());
turaj@webrtc.org0c0fae82013-09-25 17:42:17 +0000125 LOG(LS_VERBOSE) << "InsertPacket: ts=" << rtp_header.header.timestamp <<
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000126 ", sn=" << rtp_header.header.sequenceNumber <<
127 ", pt=" << static_cast<int>(rtp_header.header.payloadType) <<
128 ", ssrc=" << rtp_header.header.ssrc <<
129 ", len=" << length_bytes;
130 int error = InsertPacketInternal(rtp_header, payload, length_bytes,
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +0000131 receive_timestamp, false);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000132 if (error != 0) {
133 LOG_FERR1(LS_WARNING, InsertPacketInternal, error);
134 error_code_ = error;
135 return kFail;
136 }
137 return kOK;
138}
139
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +0000140int NetEqImpl::InsertSyncPacket(const WebRtcRTPHeader& rtp_header,
141 uint32_t receive_timestamp) {
142 CriticalSectionScoped lock(crit_sect_.get());
143 LOG(LS_VERBOSE) << "InsertPacket-Sync: ts="
144 << rtp_header.header.timestamp <<
145 ", sn=" << rtp_header.header.sequenceNumber <<
146 ", pt=" << static_cast<int>(rtp_header.header.payloadType) <<
147 ", ssrc=" << rtp_header.header.ssrc;
148
149 const uint8_t kSyncPayload[] = { 's', 'y', 'n', 'c' };
150 int error = InsertPacketInternal(
151 rtp_header, kSyncPayload, sizeof(kSyncPayload), receive_timestamp, true);
152
henrik.lundin@webrtc.orge7ce4372014-01-09 14:01:55 +0000153 if (error != 0) {
154 LOG_FERR1(LS_WARNING, InsertPacketInternal, error);
155 error_code_ = error;
156 return kFail;
157 }
158 return kOK;
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +0000159}
160
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000161int NetEqImpl::GetAudio(size_t max_length, int16_t* output_audio,
162 int* samples_per_channel, int* num_channels,
163 NetEqOutputType* type) {
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +0000164 CriticalSectionScoped lock(crit_sect_.get());
turaj@webrtc.org0c0fae82013-09-25 17:42:17 +0000165 LOG(LS_VERBOSE) << "GetAudio";
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000166 int error = GetAudioInternal(max_length, output_audio, samples_per_channel,
167 num_channels);
turaj@webrtc.org0c0fae82013-09-25 17:42:17 +0000168 LOG(LS_VERBOSE) << "Produced " << *samples_per_channel <<
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000169 " samples/channel for " << *num_channels << " channel(s)";
170 if (error != 0) {
171 LOG_FERR1(LS_WARNING, GetAudioInternal, error);
172 error_code_ = error;
173 return kFail;
174 }
175 if (type) {
176 *type = LastOutputType();
177 }
178 return kOK;
179}
180
181int NetEqImpl::RegisterPayloadType(enum NetEqDecoder codec,
182 uint8_t rtp_payload_type) {
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +0000183 CriticalSectionScoped lock(crit_sect_.get());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000184 LOG_API2(static_cast<int>(rtp_payload_type), codec);
185 int ret = decoder_database_->RegisterPayload(rtp_payload_type, codec);
186 if (ret != DecoderDatabase::kOK) {
pkasting@chromium.org026b8922015-01-30 19:53:42 +0000187 LOG_FERR2(LS_WARNING, RegisterPayload, static_cast<int>(rtp_payload_type),
188 codec);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000189 switch (ret) {
190 case DecoderDatabase::kInvalidRtpPayloadType:
191 error_code_ = kInvalidRtpPayloadType;
192 break;
193 case DecoderDatabase::kCodecNotSupported:
194 error_code_ = kCodecNotSupported;
195 break;
196 case DecoderDatabase::kDecoderExists:
197 error_code_ = kDecoderExists;
198 break;
199 default:
200 error_code_ = kOtherError;
201 }
202 return kFail;
203 }
204 return kOK;
205}
206
207int NetEqImpl::RegisterExternalDecoder(AudioDecoder* decoder,
208 enum NetEqDecoder codec,
Karl Wibergd8399e62015-05-25 14:39:56 +0200209 uint8_t rtp_payload_type,
210 int sample_rate_hz) {
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +0000211 CriticalSectionScoped lock(crit_sect_.get());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000212 LOG_API2(static_cast<int>(rtp_payload_type), codec);
213 if (!decoder) {
214 LOG(LS_ERROR) << "Cannot register external decoder with NULL pointer";
215 assert(false);
216 return kFail;
217 }
218 int ret = decoder_database_->InsertExternal(rtp_payload_type, codec,
219 sample_rate_hz, decoder);
220 if (ret != DecoderDatabase::kOK) {
pkasting@chromium.org026b8922015-01-30 19:53:42 +0000221 LOG_FERR2(LS_WARNING, InsertExternal, static_cast<int>(rtp_payload_type),
222 codec);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000223 switch (ret) {
224 case DecoderDatabase::kInvalidRtpPayloadType:
225 error_code_ = kInvalidRtpPayloadType;
226 break;
227 case DecoderDatabase::kCodecNotSupported:
228 error_code_ = kCodecNotSupported;
229 break;
230 case DecoderDatabase::kDecoderExists:
231 error_code_ = kDecoderExists;
232 break;
233 case DecoderDatabase::kInvalidSampleRate:
234 error_code_ = kInvalidSampleRate;
235 break;
236 case DecoderDatabase::kInvalidPointer:
237 error_code_ = kInvalidPointer;
238 break;
239 default:
240 error_code_ = kOtherError;
241 }
242 return kFail;
243 }
244 return kOK;
245}
246
247int NetEqImpl::RemovePayloadType(uint8_t rtp_payload_type) {
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +0000248 CriticalSectionScoped lock(crit_sect_.get());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000249 LOG_API1(static_cast<int>(rtp_payload_type));
250 int ret = decoder_database_->Remove(rtp_payload_type);
251 if (ret == DecoderDatabase::kOK) {
252 return kOK;
253 } else if (ret == DecoderDatabase::kDecoderNotFound) {
254 error_code_ = kDecoderNotFound;
255 } else {
256 error_code_ = kOtherError;
257 }
pkasting@chromium.org026b8922015-01-30 19:53:42 +0000258 LOG_FERR1(LS_WARNING, Remove, static_cast<int>(rtp_payload_type));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000259 return kFail;
260}
261
turaj@webrtc.orgf1efc572013-08-16 23:44:24 +0000262bool NetEqImpl::SetMinimumDelay(int delay_ms) {
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +0000263 CriticalSectionScoped lock(crit_sect_.get());
turaj@webrtc.orgf1efc572013-08-16 23:44:24 +0000264 if (delay_ms >= 0 && delay_ms < 10000) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000265 assert(delay_manager_.get());
turaj@webrtc.orgf1efc572013-08-16 23:44:24 +0000266 return delay_manager_->SetMinimumDelay(delay_ms);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000267 }
268 return false;
269}
270
turaj@webrtc.orgf1efc572013-08-16 23:44:24 +0000271bool NetEqImpl::SetMaximumDelay(int delay_ms) {
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +0000272 CriticalSectionScoped lock(crit_sect_.get());
turaj@webrtc.orgf1efc572013-08-16 23:44:24 +0000273 if (delay_ms >= 0 && delay_ms < 10000) {
274 assert(delay_manager_.get());
275 return delay_manager_->SetMaximumDelay(delay_ms);
276 }
277 return false;
278}
279
280int NetEqImpl::LeastRequiredDelayMs() const {
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +0000281 CriticalSectionScoped lock(crit_sect_.get());
turaj@webrtc.orgf1efc572013-08-16 23:44:24 +0000282 assert(delay_manager_.get());
283 return delay_manager_->least_required_delay_ms();
284}
285
Karl Wiberg7f6c4d42015-04-09 15:44:22 +0200286int NetEqImpl::SetTargetDelay() {
287 return kNotImplemented;
288}
289
290int NetEqImpl::TargetDelay() {
291 return kNotImplemented;
292}
293
Henrik Lundin5abd3e12015-06-03 12:58:46 +0200294int NetEqImpl::CurrentDelay() {
295 return kNotImplemented;
Karl Wiberg7f6c4d42015-04-09 15:44:22 +0200296}
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 +0000300void NetEqImpl::SetPlayoutMode(NetEqPlayoutMode mode) {
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 if (mode != playout_mode_) {
303 playout_mode_ = mode;
304 CreateDecisionLogic();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000305 }
306}
307
henrik.lundin@webrtc.org7cbc4f92014-10-07 06:37:39 +0000308// Deprecated.
309// TODO(henrik.lundin) Delete.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000310NetEqPlayoutMode NetEqImpl::PlayoutMode() const {
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +0000311 CriticalSectionScoped lock(crit_sect_.get());
henrik.lundin@webrtc.org7cbc4f92014-10-07 06:37:39 +0000312 return playout_mode_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000313}
314
315int NetEqImpl::NetworkStatistics(NetEqNetworkStatistics* stats) {
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +0000316 CriticalSectionScoped lock(crit_sect_.get());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000317 assert(decoder_database_.get());
Peter Kasting728d9032015-06-11 14:31:38 -0700318 const int total_samples_in_buffers =
319 packet_buffer_->NumSamplesInBuffer(decoder_database_.get(),
320 decoder_frame_length_) +
321 static_cast<int>(sync_buffer_->FutureLength());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000322 assert(delay_manager_.get());
323 assert(decision_logic_.get());
324 stats_.GetNetworkStatistics(fs_hz_, total_samples_in_buffers,
325 decoder_frame_length_, *delay_manager_.get(),
326 *decision_logic_.get(), stats);
327 return 0;
328}
329
330void NetEqImpl::WaitingTimes(std::vector<int>* waiting_times) {
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +0000331 CriticalSectionScoped lock(crit_sect_.get());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000332 stats_.WaitingTimes(waiting_times);
333}
334
335void NetEqImpl::GetRtcpStatistics(RtcpStatistics* stats) {
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +0000336 CriticalSectionScoped lock(crit_sect_.get());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000337 if (stats) {
338 rtcp_.GetStatistics(false, stats);
339 }
340}
341
342void NetEqImpl::GetRtcpStatisticsNoReset(RtcpStatistics* stats) {
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +0000343 CriticalSectionScoped lock(crit_sect_.get());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000344 if (stats) {
345 rtcp_.GetStatistics(true, stats);
346 }
347}
348
349void NetEqImpl::EnableVad() {
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +0000350 CriticalSectionScoped lock(crit_sect_.get());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000351 assert(vad_.get());
352 vad_->Enable();
353}
354
355void NetEqImpl::DisableVad() {
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +0000356 CriticalSectionScoped lock(crit_sect_.get());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000357 assert(vad_.get());
358 vad_->Disable();
359}
360
wu@webrtc.org94454b72014-06-05 20:34:08 +0000361bool NetEqImpl::GetPlayoutTimestamp(uint32_t* timestamp) {
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +0000362 CriticalSectionScoped lock(crit_sect_.get());
wu@webrtc.org94454b72014-06-05 20:34:08 +0000363 if (first_packet_) {
364 // We don't have a valid RTP timestamp until we have decoded our first
365 // RTP packet.
366 return false;
367 }
368 *timestamp = timestamp_scaler_->ToExternal(playout_timestamp_);
369 return true;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000370}
371
Karl Wiberg7f6c4d42015-04-09 15:44:22 +0200372int NetEqImpl::SetTargetNumberOfChannels() {
373 return kNotImplemented;
374}
375
376int NetEqImpl::SetTargetSampleRate() {
377 return kNotImplemented;
378}
379
henrik.lundin@webrtc.orgb0f4b3d2014-11-04 08:53:10 +0000380int NetEqImpl::LastError() const {
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +0000381 CriticalSectionScoped lock(crit_sect_.get());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000382 return error_code_;
383}
384
385int NetEqImpl::LastDecoderError() {
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +0000386 CriticalSectionScoped lock(crit_sect_.get());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000387 return decoder_error_code_;
388}
389
390void NetEqImpl::FlushBuffers() {
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +0000391 CriticalSectionScoped lock(crit_sect_.get());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000392 LOG_API0();
393 packet_buffer_->Flush();
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +0000394 assert(sync_buffer_.get());
395 assert(expand_.get());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000396 sync_buffer_->Flush();
397 sync_buffer_->set_next_index(sync_buffer_->next_index() -
398 expand_->overlap_length());
399 // Set to wait for new codec.
400 first_packet_ = true;
401}
402
turaj@webrtc.org3170b572013-08-30 15:36:53 +0000403void NetEqImpl::PacketBufferStatistics(int* current_num_packets,
henrik.lundin@webrtc.org116ed1d2014-04-28 08:20:04 +0000404 int* max_num_packets) const {
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +0000405 CriticalSectionScoped lock(crit_sect_.get());
henrik.lundin@webrtc.org116ed1d2014-04-28 08:20:04 +0000406 packet_buffer_->BufferStat(current_num_packets, max_num_packets);
turaj@webrtc.org3170b572013-08-30 15:36:53 +0000407}
408
turaj@webrtc.orgff43c852013-09-25 00:07:27 +0000409int NetEqImpl::DecodedRtpInfo(int* sequence_number, uint32_t* timestamp) const {
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +0000410 CriticalSectionScoped lock(crit_sect_.get());
minyue@webrtc.orgd7301772013-08-29 00:58:14 +0000411 if (decoded_packet_sequence_number_ < 0)
412 return -1;
413 *sequence_number = decoded_packet_sequence_number_;
414 *timestamp = decoded_packet_timestamp_;
415 return 0;
416}
417
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000418const SyncBuffer* NetEqImpl::sync_buffer_for_test() const {
419 CriticalSectionScoped lock(crit_sect_.get());
420 return sync_buffer_.get();
421}
422
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000423// Methods below this line are private.
424
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000425int NetEqImpl::InsertPacketInternal(const WebRtcRTPHeader& rtp_header,
426 const uint8_t* payload,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000427 size_t length_bytes,
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +0000428 uint32_t receive_timestamp,
429 bool is_sync_packet) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000430 if (!payload) {
431 LOG_F(LS_ERROR) << "payload == NULL";
432 return kInvalidPointer;
433 }
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +0000434 // Sanity checks for sync-packets.
435 if (is_sync_packet) {
436 if (decoder_database_->IsDtmf(rtp_header.header.payloadType) ||
437 decoder_database_->IsRed(rtp_header.header.payloadType) ||
438 decoder_database_->IsComfortNoise(rtp_header.header.payloadType)) {
439 LOG_F(LS_ERROR) << "Sync-packet with an unacceptable payload type "
pkasting@chromium.orgd3245462015-02-23 21:28:22 +0000440 << static_cast<int>(rtp_header.header.payloadType);
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +0000441 return kSyncPacketNotAccepted;
442 }
443 if (first_packet_ ||
444 rtp_header.header.payloadType != current_rtp_payload_type_ ||
445 rtp_header.header.ssrc != ssrc_) {
446 // Even if |current_rtp_payload_type_| is 0xFF, sync-packet isn't
447 // accepted.
pkasting@chromium.orgd3245462015-02-23 21:28:22 +0000448 LOG_F(LS_ERROR)
449 << "Changing codec, SSRC or first packet with sync-packet.";
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +0000450 return kSyncPacketNotAccepted;
451 }
452 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000453 PacketList packet_list;
454 RTPHeader main_header;
455 {
henrik.lundin@webrtc.orge1d468c2013-01-30 07:37:20 +0000456 // Convert to Packet.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000457 // Create |packet| within this separate scope, since it should not be used
458 // directly once it's been inserted in the packet list. This way, |packet|
459 // is not defined outside of this block.
henrik.lundin@webrtc.orge1d468c2013-01-30 07:37:20 +0000460 Packet* packet = new Packet;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000461 packet->header.markerBit = false;
462 packet->header.payloadType = rtp_header.header.payloadType;
463 packet->header.sequenceNumber = rtp_header.header.sequenceNumber;
464 packet->header.timestamp = rtp_header.header.timestamp;
465 packet->header.ssrc = rtp_header.header.ssrc;
466 packet->header.numCSRCs = 0;
467 packet->payload_length = length_bytes;
468 packet->primary = true;
469 packet->waiting_time = 0;
470 packet->payload = new uint8_t[packet->payload_length];
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +0000471 packet->sync_packet = is_sync_packet;
henrik.lundin@webrtc.org73deaad2013-01-31 13:32:51 +0000472 if (!packet->payload) {
473 LOG_F(LS_ERROR) << "Payload pointer is NULL.";
474 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000475 assert(payload); // Already checked above.
476 memcpy(packet->payload, payload, packet->payload_length);
477 // Insert packet in a packet list.
478 packet_list.push_back(packet);
479 // Save main payloads header for later.
480 memcpy(&main_header, &packet->header, sizeof(main_header));
481 }
482
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000483 bool update_sample_rate_and_channels = false;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000484 // Reinitialize NetEq if it's needed (changed SSRC or first call).
485 if ((main_header.ssrc != ssrc_) || first_packet_) {
henrik.lundin@webrtc.org6ff3ac12014-11-20 14:14:49 +0000486 // Note: |first_packet_| will be cleared further down in this method, once
487 // the packet has been successfully inserted into the packet buffer.
488
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000489 rtcp_.Init(main_header.sequenceNumber);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000490
491 // Flush the packet buffer and DTMF buffer.
492 packet_buffer_->Flush();
493 dtmf_buffer_->Flush();
494
495 // Store new SSRC.
496 ssrc_ = main_header.ssrc;
497
turaj@webrtc.org4d06db52013-03-27 18:31:42 +0000498 // Update audio buffer timestamp.
499 sync_buffer_->IncreaseEndTimestamp(main_header.timestamp - timestamp_);
500
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000501 // Update codecs.
502 timestamp_ = main_header.timestamp;
503 current_rtp_payload_type_ = main_header.payloadType;
504
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000505 // Reset timestamp scaling.
506 timestamp_scaler_->Reset();
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000507
henrik.lundin@webrtc.org6ff3ac12014-11-20 14:14:49 +0000508 // Trigger an update of sampling rate and the number of channels.
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000509 update_sample_rate_and_channels = true;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000510 }
511
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +0000512 // Update RTCP statistics, only for regular packets.
513 if (!is_sync_packet)
514 rtcp_.Update(main_header, receive_timestamp);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000515
516 // Check for RED payload type, and separate payloads into several packets.
517 if (decoder_database_->IsRed(main_header.payloadType)) {
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +0000518 assert(!is_sync_packet); // We had a sanity check for this.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000519 if (payload_splitter_->SplitRed(&packet_list) != PayloadSplitter::kOK) {
520 LOG_FERR1(LS_WARNING, SplitRed, packet_list.size());
521 PacketBuffer::DeleteAllPackets(&packet_list);
522 return kRedundancySplitError;
523 }
524 // Only accept a few RED payloads of the same type as the main data,
525 // DTMF events and CNG.
526 payload_splitter_->CheckRedPayloads(&packet_list, *decoder_database_);
527 // Update the stored main payload header since the main payload has now
528 // changed.
529 memcpy(&main_header, &packet_list.front()->header, sizeof(main_header));
530 }
531
532 // Check payload types.
533 if (decoder_database_->CheckPayloadTypes(packet_list) ==
534 DecoderDatabase::kDecoderNotFound) {
535 LOG_FERR1(LS_WARNING, CheckPayloadTypes, packet_list.size());
536 PacketBuffer::DeleteAllPackets(&packet_list);
537 return kUnknownRtpPayloadType;
538 }
539
540 // Scale timestamp to internal domain (only for some codecs).
541 timestamp_scaler_->ToInternal(&packet_list);
542
543 // Process DTMF payloads. Cycle through the list of packets, and pick out any
544 // DTMF payloads found.
545 PacketList::iterator it = packet_list.begin();
546 while (it != packet_list.end()) {
547 Packet* current_packet = (*it);
548 assert(current_packet);
549 assert(current_packet->payload);
550 if (decoder_database_->IsDtmf(current_packet->header.payloadType)) {
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +0000551 assert(!current_packet->sync_packet); // We had a sanity check for this.
minyue@webrtc.org9721db72013-08-06 05:36:26 +0000552 DtmfEvent event;
553 int ret = DtmfBuffer::ParseEvent(
554 current_packet->header.timestamp,
555 current_packet->payload,
556 current_packet->payload_length,
557 &event);
558 if (ret != DtmfBuffer::kOK) {
559 LOG_FERR2(LS_WARNING, ParseEvent, ret,
560 current_packet->payload_length);
561 PacketBuffer::DeleteAllPackets(&packet_list);
562 return kDtmfParsingError;
563 }
564 if (dtmf_buffer_->InsertEvent(event) != DtmfBuffer::kOK) {
565 LOG_FERR0(LS_WARNING, InsertEvent);
566 PacketBuffer::DeleteAllPackets(&packet_list);
567 return kDtmfInsertError;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000568 }
569 // TODO(hlundin): Let the destructor of Packet handle the payload.
570 delete [] current_packet->payload;
571 delete current_packet;
572 it = packet_list.erase(it);
573 } else {
574 ++it;
575 }
576 }
577
minyue@webrtc.org7549ff42014-04-02 15:03:01 +0000578 // Check for FEC in packets, and separate payloads into several packets.
579 int ret = payload_splitter_->SplitFec(&packet_list, decoder_database_.get());
580 if (ret != PayloadSplitter::kOK) {
581 LOG_FERR1(LS_WARNING, SplitFec, packet_list.size());
582 PacketBuffer::DeleteAllPackets(&packet_list);
583 switch (ret) {
584 case PayloadSplitter::kUnknownPayloadType:
585 return kUnknownRtpPayloadType;
586 default:
587 return kOtherError;
588 }
589 }
590
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000591 // Split payloads into smaller chunks. This also verifies that all payloads
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +0000592 // are of a known payload type. SplitAudio() method is protected against
593 // sync-packets.
minyue@webrtc.orgb28bfa72014-03-21 12:07:40 +0000594 ret = payload_splitter_->SplitAudio(&packet_list, *decoder_database_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000595 if (ret != PayloadSplitter::kOK) {
596 LOG_FERR1(LS_WARNING, SplitAudio, packet_list.size());
597 PacketBuffer::DeleteAllPackets(&packet_list);
598 switch (ret) {
599 case PayloadSplitter::kUnknownPayloadType:
600 return kUnknownRtpPayloadType;
601 case PayloadSplitter::kFrameSplitError:
602 return kFrameSplitError;
603 default:
604 return kOtherError;
605 }
606 }
607
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +0000608 // Update bandwidth estimate, if the packet is not sync-packet.
609 if (!packet_list.empty() && !packet_list.front()->sync_packet) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000610 // The list can be empty here if we got nothing but DTMF payloads.
611 AudioDecoder* decoder =
612 decoder_database_->GetDecoder(main_header.payloadType);
613 assert(decoder); // Should always get a valid object, since we have
614 // already checked that the payload types are known.
615 decoder->IncomingPacket(packet_list.front()->payload,
616 packet_list.front()->payload_length,
617 packet_list.front()->header.sequenceNumber,
618 packet_list.front()->header.timestamp,
619 receive_timestamp);
620 }
621
622 // Insert packets in buffer.
623 int temp_bufsize = packet_buffer_->NumPacketsInBuffer();
624 ret = packet_buffer_->InsertPacketList(
625 &packet_list,
626 *decoder_database_,
627 &current_rtp_payload_type_,
628 &current_cng_rtp_payload_type_);
629 if (ret == PacketBuffer::kFlushed) {
630 // Reset DSP timestamp etc. if packet buffer flushed.
631 new_codec_ = true;
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000632 update_sample_rate_and_channels = true;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000633 LOG_F(LS_WARNING) << "Packet buffer flushed";
634 } else if (ret != PacketBuffer::kOK) {
635 LOG_FERR1(LS_WARNING, InsertPacketList, packet_list.size());
636 PacketBuffer::DeleteAllPackets(&packet_list);
minyue@webrtc.org7bb54362013-08-06 05:40:57 +0000637 return kOtherError;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000638 }
henrik.lundin@webrtc.org6ff3ac12014-11-20 14:14:49 +0000639
640 if (first_packet_) {
641 first_packet_ = false;
642 // Update the codec on the next GetAudio call.
643 new_codec_ = true;
644 }
645
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000646 if (current_rtp_payload_type_ != 0xFF) {
647 const DecoderDatabase::DecoderInfo* dec_info =
648 decoder_database_->GetDecoderInfo(current_rtp_payload_type_);
649 if (!dec_info) {
650 assert(false); // Already checked that the payload type is known.
651 }
652 }
653
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000654 if (update_sample_rate_and_channels && !packet_buffer_->Empty()) {
655 // We do not use |current_rtp_payload_type_| to |set payload_type|, but
656 // get the next RTP header from |packet_buffer_| to obtain the payload type.
657 // The reason for it is the following corner case. If NetEq receives a
658 // CNG packet with a sample rate different than the current CNG then it
659 // flushes its buffer, assuming send codec must have been changed. However,
660 // payload type of the hypothetically new send codec is not known.
661 const RTPHeader* rtp_header = packet_buffer_->NextRtpHeader();
662 assert(rtp_header);
663 int payload_type = rtp_header->payloadType;
664 AudioDecoder* decoder = decoder_database_->GetDecoder(payload_type);
665 assert(decoder); // Payloads are already checked to be valid.
666 const DecoderDatabase::DecoderInfo* decoder_info =
667 decoder_database_->GetDecoderInfo(payload_type);
668 assert(decoder_info);
669 if (decoder_info->fs_hz != fs_hz_ ||
henrik.lundin@webrtc.org6dba1eb2015-03-18 09:47:08 +0000670 decoder->Channels() != algorithm_buffer_->Channels())
671 SetSampleRateAndChannels(decoder_info->fs_hz, decoder->Channels());
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000672 }
673
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000674 // TODO(hlundin): Move this code to DelayManager class.
675 const DecoderDatabase::DecoderInfo* dec_info =
676 decoder_database_->GetDecoderInfo(main_header.payloadType);
677 assert(dec_info); // Already checked that the payload type is known.
678 delay_manager_->LastDecoderType(dec_info->codec_type);
679 if (delay_manager_->last_pack_cng_or_dtmf() == 0) {
680 // Calculate the total speech length carried in each packet.
681 temp_bufsize = packet_buffer_->NumPacketsInBuffer() - temp_bufsize;
682 temp_bufsize *= decoder_frame_length_;
683
684 if ((temp_bufsize > 0) &&
685 (temp_bufsize != decision_logic_->packet_length_samples())) {
686 decision_logic_->set_packet_length_samples(temp_bufsize);
687 delay_manager_->SetPacketAudioLength((1000 * temp_bufsize) / fs_hz_);
688 }
689
690 // Update statistics.
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000691 if ((int32_t) (main_header.timestamp - timestamp_) >= 0 &&
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000692 !new_codec_) {
693 // Only update statistics if incoming packet is not older than last played
694 // out packet, and if new codec flag is not set.
695 delay_manager_->Update(main_header.sequenceNumber, main_header.timestamp,
696 fs_hz_);
697 }
698 } else if (delay_manager_->last_pack_cng_or_dtmf() == -1) {
699 // This is first "normal" packet after CNG or DTMF.
700 // Reset packet time counter and measure time until next packet,
701 // but don't update statistics.
702 delay_manager_->set_last_pack_cng_or_dtmf(0);
703 delay_manager_->ResetPacketIatCount();
704 }
705 return 0;
706}
707
Peter Kasting728d9032015-06-11 14:31:38 -0700708int NetEqImpl::GetAudioInternal(size_t max_length,
709 int16_t* output,
710 int* samples_per_channel,
711 int* num_channels) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000712 PacketList packet_list;
713 DtmfEvent dtmf_event;
714 Operations operation;
715 bool play_dtmf;
716 int return_value = GetDecision(&operation, &packet_list, &dtmf_event,
717 &play_dtmf);
718 if (return_value != 0) {
719 LOG_FERR1(LS_WARNING, GetDecision, return_value);
720 assert(false);
721 last_mode_ = kModeError;
722 return return_value;
723 }
turaj@webrtc.org0c0fae82013-09-25 17:42:17 +0000724 LOG(LS_VERBOSE) << "GetDecision returned operation=" << operation <<
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000725 " and " << packet_list.size() << " packet(s)";
726
727 AudioDecoder::SpeechType speech_type;
728 int length = 0;
729 int decode_return_value = Decode(&packet_list, &operation,
730 &length, &speech_type);
731
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000732 assert(vad_.get());
733 bool sid_frame_available =
734 (operation == kRfc3389Cng && !packet_list.empty());
735 vad_->Update(decoded_buffer_.get(), length, speech_type,
736 sid_frame_available, fs_hz_);
737
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +0000738 algorithm_buffer_->Clear();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000739 switch (operation) {
740 case kNormal: {
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +0000741 DoNormal(decoded_buffer_.get(), length, speech_type, play_dtmf);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000742 break;
743 }
744 case kMerge: {
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +0000745 DoMerge(decoded_buffer_.get(), length, speech_type, play_dtmf);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000746 break;
747 }
748 case kExpand: {
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +0000749 return_value = DoExpand(play_dtmf);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000750 break;
751 }
Henrik Lundincf808d22015-05-27 14:33:29 +0200752 case kAccelerate:
753 case kFastAccelerate: {
754 const bool fast_accelerate =
755 enable_fast_accelerate_ && (operation == kFastAccelerate);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000756 return_value = DoAccelerate(decoded_buffer_.get(), length, speech_type,
Henrik Lundincf808d22015-05-27 14:33:29 +0200757 play_dtmf, fast_accelerate);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000758 break;
759 }
760 case kPreemptiveExpand: {
761 return_value = DoPreemptiveExpand(decoded_buffer_.get(), length,
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +0000762 speech_type, play_dtmf);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000763 break;
764 }
765 case kRfc3389Cng:
766 case kRfc3389CngNoPacket: {
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +0000767 return_value = DoRfc3389Cng(&packet_list, play_dtmf);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000768 break;
769 }
770 case kCodecInternalCng: {
771 // This handles the case when there is no transmission and the decoder
772 // should produce internal comfort noise.
773 // TODO(hlundin): Write test for codec-internal CNG.
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +0000774 DoCodecInternalCng();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000775 break;
776 }
777 case kDtmf: {
778 // TODO(hlundin): Write test for this.
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +0000779 return_value = DoDtmf(dtmf_event, &play_dtmf);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000780 break;
781 }
782 case kAlternativePlc: {
783 // TODO(hlundin): Write test for this.
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +0000784 DoAlternativePlc(false);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000785 break;
786 }
787 case kAlternativePlcIncreaseTimestamp: {
788 // TODO(hlundin): Write test for this.
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +0000789 DoAlternativePlc(true);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000790 break;
791 }
792 case kAudioRepetitionIncreaseTimestamp: {
793 // TODO(hlundin): Write test for this.
Peter Kastingb7e50542015-06-11 12:55:50 -0700794 sync_buffer_->IncreaseEndTimestamp(
795 static_cast<uint32_t>(output_size_samples_));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000796 // Skipping break on purpose. Execution should move on into the
797 // next case.
kjellander@webrtc.org7d2b6a92015-01-28 18:37:58 +0000798 FALLTHROUGH();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000799 }
800 case kAudioRepetition: {
801 // TODO(hlundin): Write test for this.
802 // Copy last |output_size_samples_| from |sync_buffer_| to
803 // |algorithm_buffer|.
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +0000804 algorithm_buffer_->PushBackFromIndex(
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000805 *sync_buffer_, sync_buffer_->Size() - output_size_samples_);
806 expand_->Reset();
807 break;
808 }
809 case kUndefined: {
810 LOG_F(LS_ERROR) << "Invalid operation kUndefined.";
811 assert(false); // This should not happen.
812 last_mode_ = kModeError;
813 return kInvalidOperation;
814 }
815 } // End of switch.
816 if (return_value < 0) {
817 return return_value;
818 }
819
820 if (last_mode_ != kModeRfc3389Cng) {
821 comfort_noise_->Reset();
822 }
823
824 // Copy from |algorithm_buffer| to |sync_buffer_|.
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +0000825 sync_buffer_->PushBack(*algorithm_buffer_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000826
827 // Extract data from |sync_buffer_| to |output|.
turaj@webrtc.org362a55e2013-09-20 16:25:28 +0000828 size_t num_output_samples_per_channel = output_size_samples_;
829 size_t num_output_samples = output_size_samples_ * sync_buffer_->Channels();
830 if (num_output_samples > max_length) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000831 LOG(LS_WARNING) << "Output array is too short. " << max_length << " < " <<
832 output_size_samples_ << " * " << sync_buffer_->Channels();
833 num_output_samples = max_length;
turaj@webrtc.org362a55e2013-09-20 16:25:28 +0000834 num_output_samples_per_channel = static_cast<int>(
835 max_length / sync_buffer_->Channels());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000836 }
turaj@webrtc.org362a55e2013-09-20 16:25:28 +0000837 int samples_from_sync = static_cast<int>(
838 sync_buffer_->GetNextAudioInterleaved(num_output_samples_per_channel,
839 output));
840 *num_channels = static_cast<int>(sync_buffer_->Channels());
turaj@webrtc.org0c0fae82013-09-25 17:42:17 +0000841 LOG(LS_VERBOSE) << "Sync buffer (" << *num_channels << " channel(s)):" <<
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +0000842 " insert " << algorithm_buffer_->Size() << " samples, extract " <<
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000843 samples_from_sync << " samples";
844 if (samples_from_sync != output_size_samples_) {
845 LOG_F(LS_ERROR) << "samples_from_sync != output_size_samples_";
minyue@webrtc.orgdb1cefc2013-08-13 01:39:21 +0000846 // TODO(minyue): treatment of under-run, filling zeros
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000847 memset(output, 0, num_output_samples * sizeof(int16_t));
848 *samples_per_channel = output_size_samples_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000849 return kSampleUnderrun;
850 }
851 *samples_per_channel = output_size_samples_;
852
853 // Should always have overlap samples left in the |sync_buffer_|.
854 assert(sync_buffer_->FutureLength() >= expand_->overlap_length());
855
856 if (play_dtmf) {
857 return_value = DtmfOverdub(dtmf_event, sync_buffer_->Channels(), output);
858 }
859
860 // Update the background noise parameters if last operation wrote data
861 // straight from the decoder to the |sync_buffer_|. That is, none of the
862 // operations that modify the signal can be followed by a parameter update.
863 if ((last_mode_ == kModeNormal) ||
864 (last_mode_ == kModeAccelerateFail) ||
865 (last_mode_ == kModePreemptiveExpandFail) ||
866 (last_mode_ == kModeRfc3389Cng) ||
867 (last_mode_ == kModeCodecInternalCng)) {
868 background_noise_->Update(*sync_buffer_, *vad_.get());
869 }
870
871 if (operation == kDtmf) {
872 // DTMF data was written the end of |sync_buffer_|.
873 // Update index to end of DTMF data in |sync_buffer_|.
874 sync_buffer_->set_dtmf_index(sync_buffer_->Size());
875 }
876
henrik.lundin@webrtc.orged865b52014-03-06 10:28:07 +0000877 if (last_mode_ != kModeExpand) {
878 // If last operation was not expand, calculate the |playout_timestamp_| from
879 // the |sync_buffer_|. However, do not update the |playout_timestamp_| if it
880 // would be moved "backwards".
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000881 uint32_t temp_timestamp = sync_buffer_->end_timestamp() -
turaj@webrtc.org362a55e2013-09-20 16:25:28 +0000882 static_cast<uint32_t>(sync_buffer_->FutureLength());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000883 if (static_cast<int32_t>(temp_timestamp - playout_timestamp_) > 0) {
884 playout_timestamp_ = temp_timestamp;
885 }
886 } else {
887 // Use dead reckoning to estimate the |playout_timestamp_|.
Peter Kastingb7e50542015-06-11 12:55:50 -0700888 playout_timestamp_ += static_cast<uint32_t>(output_size_samples_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000889 }
890
891 if (decode_return_value) return decode_return_value;
892 return return_value;
893}
894
895int NetEqImpl::GetDecision(Operations* operation,
896 PacketList* packet_list,
897 DtmfEvent* dtmf_event,
898 bool* play_dtmf) {
899 // Initialize output variables.
900 *play_dtmf = false;
901 *operation = kUndefined;
902
903 // Increment time counters.
904 packet_buffer_->IncrementWaitingTimes();
905 stats_.IncreaseCounter(output_size_samples_, fs_hz_);
906
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +0000907 assert(sync_buffer_.get());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000908 uint32_t end_timestamp = sync_buffer_->end_timestamp();
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000909 if (!new_codec_) {
910 const uint32_t five_seconds_samples = 5 * fs_hz_;
911 packet_buffer_->DiscardOldPackets(end_timestamp, five_seconds_samples);
912 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000913 const RTPHeader* header = packet_buffer_->NextRtpHeader();
914
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +0000915 if (decision_logic_->CngRfc3389On() || last_mode_ == kModeRfc3389Cng) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000916 // Because of timestamp peculiarities, we have to "manually" disallow using
917 // a CNG packet with the same timestamp as the one that was last played.
918 // This can happen when using redundancy and will cause the timing to shift.
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000919 while (header && decoder_database_->IsComfortNoise(header->payloadType) &&
920 (end_timestamp >= header->timestamp ||
921 end_timestamp + decision_logic_->generated_noise_samples() >
922 header->timestamp)) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000923 // Don't use this packet, discard it.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000924 if (packet_buffer_->DiscardNextPacket() != PacketBuffer::kOK) {
925 assert(false); // Must be ok by design.
926 }
927 // Check buffer again.
928 if (!new_codec_) {
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000929 packet_buffer_->DiscardOldPackets(end_timestamp, 5 * fs_hz_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000930 }
931 header = packet_buffer_->NextRtpHeader();
932 }
933 }
934
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +0000935 assert(expand_.get());
turaj@webrtc.org362a55e2013-09-20 16:25:28 +0000936 const int samples_left = static_cast<int>(sync_buffer_->FutureLength() -
937 expand_->overlap_length());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000938 if (last_mode_ == kModeAccelerateSuccess ||
939 last_mode_ == kModeAccelerateLowEnergy ||
940 last_mode_ == kModePreemptiveExpandSuccess ||
941 last_mode_ == kModePreemptiveExpandLowEnergy) {
942 // Subtract (samples_left + output_size_samples_) from sampleMemory.
943 decision_logic_->AddSampleMemory(-(samples_left + output_size_samples_));
944 }
945
946 // Check if it is time to play a DTMF event.
Peter Kastingb7e50542015-06-11 12:55:50 -0700947 if (dtmf_buffer_->GetEvent(
948 static_cast<uint32_t>(
949 end_timestamp + decision_logic_->generated_noise_samples()),
950 dtmf_event)) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000951 *play_dtmf = true;
952 }
953
954 // Get instruction.
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +0000955 assert(sync_buffer_.get());
956 assert(expand_.get());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000957 *operation = decision_logic_->GetDecision(*sync_buffer_,
958 *expand_,
959 decoder_frame_length_,
960 header,
961 last_mode_,
962 *play_dtmf,
963 &reset_decoder_);
964
965 // Check if we already have enough samples in the |sync_buffer_|. If so,
966 // change decision to normal, unless the decision was merge, accelerate, or
967 // preemptive expand.
Henrik Lundincf808d22015-05-27 14:33:29 +0200968 if (samples_left >= output_size_samples_ && *operation != kMerge &&
969 *operation != kAccelerate && *operation != kFastAccelerate &&
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000970 *operation != kPreemptiveExpand) {
971 *operation = kNormal;
972 return 0;
973 }
974
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +0000975 decision_logic_->ExpandDecision(*operation);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000976
977 // Check conditions for reset.
978 if (new_codec_ || *operation == kUndefined) {
979 // The only valid reason to get kUndefined is that new_codec_ is set.
980 assert(new_codec_);
turaj@webrtc.org4d06db52013-03-27 18:31:42 +0000981 if (*play_dtmf && !header) {
982 timestamp_ = dtmf_event->timestamp;
983 } else {
984 assert(header);
985 if (!header) {
986 LOG_F(LS_ERROR) << "Packet missing where it shouldn't.";
987 return -1;
988 }
989 timestamp_ = header->timestamp;
990 if (*operation == kRfc3389CngNoPacket
991#ifndef LEGACY_BITEXACT
992 // Without this check, it can happen that a non-CNG packet is sent to
993 // the CNG decoder as if it was a SID frame. This is clearly a bug,
994 // but is kept for now to maintain bit-exactness with the test
995 // vectors.
996 && decoder_database_->IsComfortNoise(header->payloadType)
997#endif
998 ) {
999 // Change decision to CNG packet, since we do have a CNG packet, but it
1000 // was considered too early to use. Now, use it anyway.
1001 *operation = kRfc3389Cng;
1002 } else if (*operation != kRfc3389Cng) {
1003 *operation = kNormal;
1004 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001005 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001006 // Adjust |sync_buffer_| timestamp before setting |end_timestamp| to the
1007 // new value.
1008 sync_buffer_->IncreaseEndTimestamp(timestamp_ - end_timestamp);
turaj@webrtc.org4d06db52013-03-27 18:31:42 +00001009 end_timestamp = timestamp_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001010 new_codec_ = false;
1011 decision_logic_->SoftReset();
1012 buffer_level_filter_->Reset();
1013 delay_manager_->Reset();
1014 stats_.ResetMcu();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001015 }
1016
1017 int required_samples = output_size_samples_;
1018 const int samples_10_ms = 80 * fs_mult_;
1019 const int samples_20_ms = 2 * samples_10_ms;
1020 const int samples_30_ms = 3 * samples_10_ms;
1021
1022 switch (*operation) {
1023 case kExpand: {
1024 timestamp_ = end_timestamp;
1025 return 0;
1026 }
1027 case kRfc3389CngNoPacket:
1028 case kCodecInternalCng: {
1029 return 0;
1030 }
1031 case kDtmf: {
1032 // TODO(hlundin): Write test for this.
1033 // Update timestamp.
1034 timestamp_ = end_timestamp;
1035 if (decision_logic_->generated_noise_samples() > 0 &&
1036 last_mode_ != kModeDtmf) {
1037 // Make a jump in timestamp due to the recently played comfort noise.
Peter Kastingb7e50542015-06-11 12:55:50 -07001038 uint32_t timestamp_jump =
1039 static_cast<uint32_t>(decision_logic_->generated_noise_samples());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001040 sync_buffer_->IncreaseEndTimestamp(timestamp_jump);
1041 timestamp_ += timestamp_jump;
1042 }
1043 decision_logic_->set_generated_noise_samples(0);
1044 return 0;
1045 }
Henrik Lundincf808d22015-05-27 14:33:29 +02001046 case kAccelerate:
1047 case kFastAccelerate: {
1048 // In order to do an accelerate we need at least 30 ms of audio data.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001049 if (samples_left >= samples_30_ms) {
1050 // Already have enough data, so we do not need to extract any more.
1051 decision_logic_->set_sample_memory(samples_left);
1052 decision_logic_->set_prev_time_scale(true);
1053 return 0;
1054 } else if (samples_left >= samples_10_ms &&
1055 decoder_frame_length_ >= samples_30_ms) {
1056 // Avoid decoding more data as it might overflow the playout buffer.
1057 *operation = kNormal;
1058 return 0;
1059 } else if (samples_left < samples_20_ms &&
1060 decoder_frame_length_ < samples_30_ms) {
1061 // Build up decoded data by decoding at least 20 ms of audio data. Do
1062 // not perform accelerate yet, but wait until we only need to do one
1063 // decoding.
1064 required_samples = 2 * output_size_samples_;
1065 *operation = kNormal;
1066 }
1067 // If none of the above is true, we have one of two possible situations:
1068 // (1) 20 ms <= samples_left < 30 ms and decoder_frame_length_ < 30 ms; or
1069 // (2) samples_left < 10 ms and decoder_frame_length_ >= 30 ms.
1070 // In either case, we move on with the accelerate decision, and decode one
1071 // frame now.
1072 break;
1073 }
1074 case kPreemptiveExpand: {
1075 // In order to do a preemptive expand we need at least 30 ms of decoded
1076 // audio data.
1077 if ((samples_left >= samples_30_ms) ||
1078 (samples_left >= samples_10_ms &&
1079 decoder_frame_length_ >= samples_30_ms)) {
1080 // Already have enough data, so we do not need to extract any more.
1081 // Or, avoid decoding more data as it might overflow the playout buffer.
1082 // Still try preemptive expand, though.
1083 decision_logic_->set_sample_memory(samples_left);
1084 decision_logic_->set_prev_time_scale(true);
1085 return 0;
1086 }
1087 if (samples_left < samples_20_ms &&
1088 decoder_frame_length_ < samples_30_ms) {
1089 // Build up decoded data by decoding at least 20 ms of audio data.
1090 // Still try to perform preemptive expand.
1091 required_samples = 2 * output_size_samples_;
1092 }
1093 // Move on with the preemptive expand decision.
1094 break;
1095 }
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001096 case kMerge: {
1097 required_samples =
1098 std::max(merge_->RequiredFutureSamples(), required_samples);
1099 break;
1100 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001101 default: {
1102 // Do nothing.
1103 }
1104 }
1105
1106 // Get packets from buffer.
1107 int extracted_samples = 0;
1108 if (header &&
1109 *operation != kAlternativePlc &&
1110 *operation != kAlternativePlcIncreaseTimestamp &&
1111 *operation != kAudioRepetition &&
1112 *operation != kAudioRepetitionIncreaseTimestamp) {
1113 sync_buffer_->IncreaseEndTimestamp(header->timestamp - end_timestamp);
1114 if (decision_logic_->CngOff()) {
1115 // Adjustment of timestamp only corresponds to an actual packet loss
1116 // if comfort noise is not played. If comfort noise was just played,
1117 // this adjustment of timestamp is only done to get back in sync with the
1118 // stream timestamp; no loss to report.
1119 stats_.LostSamples(header->timestamp - end_timestamp);
1120 }
1121
1122 if (*operation != kRfc3389Cng) {
1123 // We are about to decode and use a non-CNG packet.
1124 decision_logic_->SetCngOff();
1125 }
1126 // Reset CNG timestamp as a new packet will be delivered.
1127 // (Also if this is a CNG packet, since playedOutTS is updated.)
1128 decision_logic_->set_generated_noise_samples(0);
1129
1130 extracted_samples = ExtractPackets(required_samples, packet_list);
1131 if (extracted_samples < 0) {
1132 LOG_F(LS_WARNING) << "Failed to extract packets from buffer.";
1133 return kPacketBufferCorruption;
1134 }
1135 }
1136
Henrik Lundincf808d22015-05-27 14:33:29 +02001137 if (*operation == kAccelerate || *operation == kFastAccelerate ||
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001138 *operation == kPreemptiveExpand) {
1139 decision_logic_->set_sample_memory(samples_left + extracted_samples);
1140 decision_logic_->set_prev_time_scale(true);
1141 }
1142
Henrik Lundincf808d22015-05-27 14:33:29 +02001143 if (*operation == kAccelerate || *operation == kFastAccelerate) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001144 // Check that we have enough data (30ms) to do accelerate.
1145 if (extracted_samples + samples_left < samples_30_ms) {
1146 // TODO(hlundin): Write test for this.
1147 // Not enough, do normal operation instead.
1148 *operation = kNormal;
1149 }
1150 }
1151
1152 timestamp_ = end_timestamp;
1153 return 0;
1154}
1155
1156int NetEqImpl::Decode(PacketList* packet_list, Operations* operation,
1157 int* decoded_length,
1158 AudioDecoder::SpeechType* speech_type) {
1159 *speech_type = AudioDecoder::kSpeech;
1160 AudioDecoder* decoder = NULL;
1161 if (!packet_list->empty()) {
1162 const Packet* packet = packet_list->front();
pkasting@chromium.org0e81fdf2015-02-02 23:54:03 +00001163 uint8_t payload_type = packet->header.payloadType;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001164 if (!decoder_database_->IsComfortNoise(payload_type)) {
1165 decoder = decoder_database_->GetDecoder(payload_type);
1166 assert(decoder);
1167 if (!decoder) {
pkasting@chromium.org0e81fdf2015-02-02 23:54:03 +00001168 LOG_FERR1(LS_WARNING, GetDecoder, static_cast<int>(payload_type));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001169 PacketBuffer::DeleteAllPackets(packet_list);
1170 return kDecoderNotFound;
1171 }
1172 bool decoder_changed;
1173 decoder_database_->SetActiveDecoder(payload_type, &decoder_changed);
1174 if (decoder_changed) {
1175 // We have a new decoder. Re-init some values.
1176 const DecoderDatabase::DecoderInfo* decoder_info = decoder_database_
1177 ->GetDecoderInfo(payload_type);
1178 assert(decoder_info);
1179 if (!decoder_info) {
pkasting@chromium.org0e81fdf2015-02-02 23:54:03 +00001180 LOG_FERR1(LS_WARNING, GetDecoderInfo, static_cast<int>(payload_type));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001181 PacketBuffer::DeleteAllPackets(packet_list);
1182 return kDecoderNotFound;
1183 }
tina.legrand@webrtc.orgba5a6c32014-03-23 09:58:48 +00001184 // If sampling rate or number of channels has changed, we need to make
1185 // a reset.
turaj@webrtc.orga6101d72013-10-01 22:01:09 +00001186 if (decoder_info->fs_hz != fs_hz_ ||
henrik.lundin@webrtc.org6dba1eb2015-03-18 09:47:08 +00001187 decoder->Channels() != algorithm_buffer_->Channels()) {
tina.legrand@webrtc.orgba5a6c32014-03-23 09:58:48 +00001188 // TODO(tlegrand): Add unittest to cover this event.
henrik.lundin@webrtc.org6dba1eb2015-03-18 09:47:08 +00001189 SetSampleRateAndChannels(decoder_info->fs_hz, decoder->Channels());
turaj@webrtc.orga6101d72013-10-01 22:01:09 +00001190 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001191 sync_buffer_->set_end_timestamp(timestamp_);
1192 playout_timestamp_ = timestamp_;
1193 }
1194 }
1195 }
1196
1197 if (reset_decoder_) {
1198 // TODO(hlundin): Write test for this.
1199 // Reset decoder.
1200 if (decoder) {
1201 decoder->Init();
1202 }
1203 // Reset comfort noise decoder.
1204 AudioDecoder* cng_decoder = decoder_database_->GetActiveCngDecoder();
1205 if (cng_decoder) {
1206 cng_decoder->Init();
1207 }
1208 reset_decoder_ = false;
1209 }
1210
1211#ifdef LEGACY_BITEXACT
1212 // Due to a bug in old SignalMCU, it could happen that CNG operation was
1213 // decided, but a speech packet was provided. The speech packet will be used
1214 // to update the comfort noise decoder, as if it was a SID frame, which is
1215 // clearly wrong.
1216 if (*operation == kRfc3389Cng) {
1217 return 0;
1218 }
1219#endif
1220
1221 *decoded_length = 0;
1222 // Update codec-internal PLC state.
1223 if ((*operation == kMerge) && decoder && decoder->HasDecodePlc()) {
1224 decoder->DecodePlc(1, &decoded_buffer_[*decoded_length]);
1225 }
1226
1227 int return_value = DecodeLoop(packet_list, operation, decoder,
1228 decoded_length, speech_type);
1229
1230 if (*decoded_length < 0) {
1231 // Error returned from the decoder.
1232 *decoded_length = 0;
Peter Kastingb7e50542015-06-11 12:55:50 -07001233 sync_buffer_->IncreaseEndTimestamp(
1234 static_cast<uint32_t>(decoder_frame_length_));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001235 int error_code = 0;
1236 if (decoder)
1237 error_code = decoder->ErrorCode();
1238 if (error_code != 0) {
1239 // Got some error code from the decoder.
1240 decoder_error_code_ = error_code;
1241 return_value = kDecoderErrorCode;
1242 } else {
1243 // Decoder does not implement error codes. Return generic error.
1244 return_value = kOtherDecoderError;
1245 }
1246 LOG_FERR2(LS_WARNING, DecodeLoop, error_code, packet_list->size());
1247 *operation = kExpand; // Do expansion to get data instead.
1248 }
1249 if (*speech_type != AudioDecoder::kComfortNoise) {
1250 // Don't increment timestamp if codec returned CNG speech type
1251 // since in this case, the we will increment the CNGplayedTS counter.
1252 // Increase with number of samples per channel.
1253 assert(*decoded_length == 0 ||
henrik.lundin@webrtc.org6dba1eb2015-03-18 09:47:08 +00001254 (decoder && decoder->Channels() == sync_buffer_->Channels()));
turaj@webrtc.org362a55e2013-09-20 16:25:28 +00001255 sync_buffer_->IncreaseEndTimestamp(
1256 *decoded_length / static_cast<int>(sync_buffer_->Channels()));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001257 }
1258 return return_value;
1259}
1260
1261int NetEqImpl::DecodeLoop(PacketList* packet_list, Operations* operation,
1262 AudioDecoder* decoder, int* decoded_length,
1263 AudioDecoder::SpeechType* speech_type) {
1264 Packet* packet = NULL;
1265 if (!packet_list->empty()) {
1266 packet = packet_list->front();
1267 }
1268 // Do decoding.
1269 while (packet &&
1270 !decoder_database_->IsComfortNoise(packet->header.payloadType)) {
1271 assert(decoder); // At this point, we must have a decoder object.
1272 // The number of channels in the |sync_buffer_| should be the same as the
1273 // number decoder channels.
henrik.lundin@webrtc.org6dba1eb2015-03-18 09:47:08 +00001274 assert(sync_buffer_->Channels() == decoder->Channels());
1275 assert(decoded_buffer_length_ >= kMaxFrameSize * decoder->Channels());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001276 assert(*operation == kNormal || *operation == kAccelerate ||
Henrik Lundincf808d22015-05-27 14:33:29 +02001277 *operation == kFastAccelerate || *operation == kMerge ||
1278 *operation == kPreemptiveExpand);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001279 packet_list->pop_front();
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001280 size_t payload_length = packet->payload_length;
Peter Kasting36b7cc32015-06-11 19:57:18 -07001281 int decode_length;
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001282 if (packet->sync_packet) {
1283 // Decode to silence with the same frame size as the last decode.
1284 LOG(LS_VERBOSE) << "Decoding sync-packet: " <<
1285 " ts=" << packet->header.timestamp <<
1286 ", sn=" << packet->header.sequenceNumber <<
1287 ", pt=" << static_cast<int>(packet->header.payloadType) <<
1288 ", ssrc=" << packet->header.ssrc <<
1289 ", len=" << packet->payload_length;
henrik.lundin@webrtc.org6dba1eb2015-03-18 09:47:08 +00001290 memset(&decoded_buffer_[*decoded_length], 0,
1291 decoder_frame_length_ * decoder->Channels() *
1292 sizeof(decoded_buffer_[0]));
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001293 decode_length = decoder_frame_length_;
1294 } else if (!packet->primary) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001295 // This is a redundant payload; call the special decoder method.
turaj@webrtc.org0c0fae82013-09-25 17:42:17 +00001296 LOG(LS_VERBOSE) << "Decoding packet (redundant):" <<
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001297 " ts=" << packet->header.timestamp <<
1298 ", sn=" << packet->header.sequenceNumber <<
1299 ", pt=" << static_cast<int>(packet->header.payloadType) <<
1300 ", ssrc=" << packet->header.ssrc <<
1301 ", len=" << packet->payload_length;
1302 decode_length = decoder->DecodeRedundant(
henrik.lundin@webrtc.org1eda4e32015-02-25 10:02:29 +00001303 packet->payload, packet->payload_length, fs_hz_,
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +00001304 (decoded_buffer_length_ - *decoded_length) * sizeof(int16_t),
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001305 &decoded_buffer_[*decoded_length], speech_type);
1306 } else {
turaj@webrtc.org0c0fae82013-09-25 17:42:17 +00001307 LOG(LS_VERBOSE) << "Decoding packet: ts=" << packet->header.timestamp <<
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001308 ", sn=" << packet->header.sequenceNumber <<
1309 ", pt=" << static_cast<int>(packet->header.payloadType) <<
1310 ", ssrc=" << packet->header.ssrc <<
1311 ", len=" << packet->payload_length;
henrik.lundin@webrtc.org1eda4e32015-02-25 10:02:29 +00001312 decode_length =
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +00001313 decoder->Decode(
1314 packet->payload, packet->payload_length, fs_hz_,
1315 (decoded_buffer_length_ - *decoded_length) * sizeof(int16_t),
1316 &decoded_buffer_[*decoded_length], speech_type);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001317 }
1318
1319 delete[] packet->payload;
1320 delete packet;
turaj@webrtc.org58cd3162013-10-31 15:15:55 +00001321 packet = NULL;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001322 if (decode_length > 0) {
1323 *decoded_length += decode_length;
1324 // Update |decoder_frame_length_| with number of samples per channel.
henrik.lundin@webrtc.org6dba1eb2015-03-18 09:47:08 +00001325 decoder_frame_length_ =
1326 decode_length / static_cast<int>(decoder->Channels());
1327 LOG(LS_VERBOSE) << "Decoded " << decode_length << " samples ("
1328 << decoder->Channels() << " channel(s) -> "
1329 << decoder_frame_length_ << " samples per channel)";
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001330 } else if (decode_length < 0) {
1331 // Error.
henrik.lundin@webrtc.org63464a92013-01-30 09:41:56 +00001332 LOG_FERR2(LS_WARNING, Decode, decode_length, payload_length);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001333 *decoded_length = -1;
1334 PacketBuffer::DeleteAllPackets(packet_list);
1335 break;
1336 }
1337 if (*decoded_length > static_cast<int>(decoded_buffer_length_)) {
1338 // Guard against overflow.
1339 LOG_F(LS_WARNING) << "Decoded too much.";
1340 PacketBuffer::DeleteAllPackets(packet_list);
1341 return kDecodedTooMuch;
1342 }
1343 if (!packet_list->empty()) {
1344 packet = packet_list->front();
1345 } else {
1346 packet = NULL;
1347 }
1348 } // End of decode loop.
1349
turaj@webrtc.org58cd3162013-10-31 15:15:55 +00001350 // If the list is not empty at this point, either a decoding error terminated
1351 // the while-loop, or list must hold exactly one CNG packet.
1352 assert(packet_list->empty() || *decoded_length < 0 ||
1353 (packet_list->size() == 1 && packet &&
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001354 decoder_database_->IsComfortNoise(packet->header.payloadType)));
1355 return 0;
1356}
1357
1358void NetEqImpl::DoNormal(const int16_t* decoded_buffer, size_t decoded_length,
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001359 AudioDecoder::SpeechType speech_type, bool play_dtmf) {
henrik.lundin@webrtc.org40d3fc62013-09-18 12:19:50 +00001360 assert(normal_.get());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001361 assert(mute_factor_array_.get());
henrik.lundin@webrtc.org40d3fc62013-09-18 12:19:50 +00001362 normal_->Process(decoded_buffer, decoded_length, last_mode_,
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +00001363 mute_factor_array_.get(), algorithm_buffer_.get());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001364 if (decoded_length != 0) {
1365 last_mode_ = kModeNormal;
1366 }
1367
1368 // If last packet was decoded as an inband CNG, set mode to CNG instead.
1369 if ((speech_type == AudioDecoder::kComfortNoise)
1370 || ((last_mode_ == kModeCodecInternalCng)
1371 && (decoded_length == 0))) {
1372 // TODO(hlundin): Remove second part of || statement above.
1373 last_mode_ = kModeCodecInternalCng;
1374 }
1375
1376 if (!play_dtmf) {
1377 dtmf_tone_generator_->Reset();
1378 }
1379}
1380
1381void NetEqImpl::DoMerge(int16_t* decoded_buffer, size_t decoded_length,
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001382 AudioDecoder::SpeechType speech_type, bool play_dtmf) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001383 assert(mute_factor_array_.get());
henrik.lundin@webrtc.org40d3fc62013-09-18 12:19:50 +00001384 assert(merge_.get());
1385 int new_length = merge_->Process(decoded_buffer, decoded_length,
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +00001386 mute_factor_array_.get(),
1387 algorithm_buffer_.get());
minyue@webrtc.orgc11348b2015-02-10 08:35:38 +00001388 int expand_length_correction = new_length -
1389 static_cast<int>(decoded_length / algorithm_buffer_->Channels());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001390
1391 // Update in-call and post-call statistics.
1392 if (expand_->MuteFactor(0) == 0) {
1393 // Expand generates only noise.
minyue@webrtc.orgc11348b2015-02-10 08:35:38 +00001394 stats_.ExpandedNoiseSamples(expand_length_correction);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001395 } else {
1396 // Expansion generates more than only noise.
minyue@webrtc.orgc11348b2015-02-10 08:35:38 +00001397 stats_.ExpandedVoiceSamples(expand_length_correction);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001398 }
1399
1400 last_mode_ = kModeMerge;
1401 // If last packet was decoded as an inband CNG, set mode to CNG instead.
1402 if (speech_type == AudioDecoder::kComfortNoise) {
1403 last_mode_ = kModeCodecInternalCng;
1404 }
1405 expand_->Reset();
1406 if (!play_dtmf) {
1407 dtmf_tone_generator_->Reset();
1408 }
1409}
1410
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001411int NetEqImpl::DoExpand(bool play_dtmf) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001412 while ((sync_buffer_->FutureLength() - expand_->overlap_length()) <
1413 static_cast<size_t>(output_size_samples_)) {
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001414 algorithm_buffer_->Clear();
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +00001415 int return_value = expand_->Process(algorithm_buffer_.get());
turaj@webrtc.org362a55e2013-09-20 16:25:28 +00001416 int length = static_cast<int>(algorithm_buffer_->Size());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001417
1418 // Update in-call and post-call statistics.
1419 if (expand_->MuteFactor(0) == 0) {
1420 // Expand operation generates only noise.
1421 stats_.ExpandedNoiseSamples(length);
1422 } else {
1423 // Expand operation generates more than only noise.
1424 stats_.ExpandedVoiceSamples(length);
1425 }
1426
1427 last_mode_ = kModeExpand;
1428
1429 if (return_value < 0) {
1430 return return_value;
1431 }
1432
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001433 sync_buffer_->PushBack(*algorithm_buffer_);
1434 algorithm_buffer_->Clear();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001435 }
1436 if (!play_dtmf) {
1437 dtmf_tone_generator_->Reset();
1438 }
1439 return 0;
1440}
1441
Henrik Lundincf808d22015-05-27 14:33:29 +02001442int NetEqImpl::DoAccelerate(int16_t* decoded_buffer,
1443 size_t decoded_length,
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001444 AudioDecoder::SpeechType speech_type,
Henrik Lundincf808d22015-05-27 14:33:29 +02001445 bool play_dtmf,
1446 bool fast_accelerate) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001447 const size_t required_samples = 240 * fs_mult_; // Must have 30 ms.
turaj@webrtc.org362a55e2013-09-20 16:25:28 +00001448 size_t borrowed_samples_per_channel = 0;
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001449 size_t num_channels = algorithm_buffer_->Channels();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001450 size_t decoded_length_per_channel = decoded_length / num_channels;
1451 if (decoded_length_per_channel < required_samples) {
1452 // Must move data from the |sync_buffer_| in order to get 30 ms.
turaj@webrtc.org362a55e2013-09-20 16:25:28 +00001453 borrowed_samples_per_channel = static_cast<int>(required_samples -
1454 decoded_length_per_channel);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001455 memmove(&decoded_buffer[borrowed_samples_per_channel * num_channels],
1456 decoded_buffer,
1457 sizeof(int16_t) * decoded_length);
1458 sync_buffer_->ReadInterleavedFromEnd(borrowed_samples_per_channel,
1459 decoded_buffer);
1460 decoded_length = required_samples * num_channels;
1461 }
1462
1463 int16_t samples_removed;
Henrik Lundincf808d22015-05-27 14:33:29 +02001464 Accelerate::ReturnCodes return_code =
1465 accelerate_->Process(decoded_buffer, decoded_length, fast_accelerate,
1466 algorithm_buffer_.get(), &samples_removed);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001467 stats_.AcceleratedSamples(samples_removed);
1468 switch (return_code) {
1469 case Accelerate::kSuccess:
1470 last_mode_ = kModeAccelerateSuccess;
1471 break;
1472 case Accelerate::kSuccessLowEnergy:
1473 last_mode_ = kModeAccelerateLowEnergy;
1474 break;
1475 case Accelerate::kNoStretch:
1476 last_mode_ = kModeAccelerateFail;
1477 break;
1478 case Accelerate::kError:
1479 // TODO(hlundin): Map to kModeError instead?
1480 last_mode_ = kModeAccelerateFail;
1481 return kAccelerateError;
1482 }
1483
1484 if (borrowed_samples_per_channel > 0) {
1485 // Copy borrowed samples back to the |sync_buffer_|.
turaj@webrtc.org362a55e2013-09-20 16:25:28 +00001486 size_t length = algorithm_buffer_->Size();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001487 if (length < borrowed_samples_per_channel) {
1488 // This destroys the beginning of the buffer, but will not cause any
1489 // problems.
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001490 sync_buffer_->ReplaceAtIndex(*algorithm_buffer_,
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001491 sync_buffer_->Size() -
1492 borrowed_samples_per_channel);
1493 sync_buffer_->PushFrontZeros(borrowed_samples_per_channel - length);
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001494 algorithm_buffer_->PopFront(length);
1495 assert(algorithm_buffer_->Empty());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001496 } else {
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001497 sync_buffer_->ReplaceAtIndex(*algorithm_buffer_,
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001498 borrowed_samples_per_channel,
1499 sync_buffer_->Size() -
1500 borrowed_samples_per_channel);
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001501 algorithm_buffer_->PopFront(borrowed_samples_per_channel);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001502 }
1503 }
1504
1505 // If last packet was decoded as an inband CNG, set mode to CNG instead.
1506 if (speech_type == AudioDecoder::kComfortNoise) {
1507 last_mode_ = kModeCodecInternalCng;
1508 }
1509 if (!play_dtmf) {
1510 dtmf_tone_generator_->Reset();
1511 }
1512 expand_->Reset();
1513 return 0;
1514}
1515
1516int NetEqImpl::DoPreemptiveExpand(int16_t* decoded_buffer,
1517 size_t decoded_length,
1518 AudioDecoder::SpeechType speech_type,
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001519 bool play_dtmf) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001520 const size_t required_samples = 240 * fs_mult_; // Must have 30 ms.
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001521 size_t num_channels = algorithm_buffer_->Channels();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001522 int borrowed_samples_per_channel = 0;
1523 int old_borrowed_samples_per_channel = 0;
1524 size_t decoded_length_per_channel = decoded_length / num_channels;
1525 if (decoded_length_per_channel < required_samples) {
1526 // Must move data from the |sync_buffer_| in order to get 30 ms.
turaj@webrtc.org362a55e2013-09-20 16:25:28 +00001527 borrowed_samples_per_channel = static_cast<int>(required_samples -
1528 decoded_length_per_channel);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001529 // Calculate how many of these were already played out.
Peter Kastingf045e4d2015-06-10 21:15:38 -07001530 const int future_length = static_cast<int>(sync_buffer_->FutureLength());
1531 old_borrowed_samples_per_channel =
1532 (borrowed_samples_per_channel > future_length) ?
1533 (borrowed_samples_per_channel - future_length) : 0;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001534 memmove(&decoded_buffer[borrowed_samples_per_channel * num_channels],
1535 decoded_buffer,
1536 sizeof(int16_t) * decoded_length);
1537 sync_buffer_->ReadInterleavedFromEnd(borrowed_samples_per_channel,
1538 decoded_buffer);
1539 decoded_length = required_samples * num_channels;
1540 }
1541
1542 int16_t samples_added;
henrik.lundin@webrtc.org40d3fc62013-09-18 12:19:50 +00001543 PreemptiveExpand::ReturnCodes return_code = preemptive_expand_->Process(
turaj@webrtc.org362a55e2013-09-20 16:25:28 +00001544 decoded_buffer, static_cast<int>(decoded_length),
1545 old_borrowed_samples_per_channel,
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +00001546 algorithm_buffer_.get(), &samples_added);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001547 stats_.PreemptiveExpandedSamples(samples_added);
1548 switch (return_code) {
1549 case PreemptiveExpand::kSuccess:
1550 last_mode_ = kModePreemptiveExpandSuccess;
1551 break;
1552 case PreemptiveExpand::kSuccessLowEnergy:
1553 last_mode_ = kModePreemptiveExpandLowEnergy;
1554 break;
1555 case PreemptiveExpand::kNoStretch:
1556 last_mode_ = kModePreemptiveExpandFail;
1557 break;
1558 case PreemptiveExpand::kError:
1559 // TODO(hlundin): Map to kModeError instead?
1560 last_mode_ = kModePreemptiveExpandFail;
1561 return kPreemptiveExpandError;
1562 }
1563
1564 if (borrowed_samples_per_channel > 0) {
1565 // Copy borrowed samples back to the |sync_buffer_|.
1566 sync_buffer_->ReplaceAtIndex(
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001567 *algorithm_buffer_, borrowed_samples_per_channel,
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001568 sync_buffer_->Size() - borrowed_samples_per_channel);
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001569 algorithm_buffer_->PopFront(borrowed_samples_per_channel);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001570 }
1571
1572 // If last packet was decoded as an inband CNG, set mode to CNG instead.
1573 if (speech_type == AudioDecoder::kComfortNoise) {
1574 last_mode_ = kModeCodecInternalCng;
1575 }
1576 if (!play_dtmf) {
1577 dtmf_tone_generator_->Reset();
1578 }
1579 expand_->Reset();
1580 return 0;
1581}
1582
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001583int NetEqImpl::DoRfc3389Cng(PacketList* packet_list, bool play_dtmf) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001584 if (!packet_list->empty()) {
1585 // Must have exactly one SID frame at this point.
1586 assert(packet_list->size() == 1);
1587 Packet* packet = packet_list->front();
1588 packet_list->pop_front();
henrik.lundin@webrtc.org73deaad2013-01-31 13:32:51 +00001589 if (!decoder_database_->IsComfortNoise(packet->header.payloadType)) {
1590#ifdef LEGACY_BITEXACT
1591 // This can happen due to a bug in GetDecision. Change the payload type
1592 // to a CNG type, and move on. Note that this means that we are in fact
1593 // sending a non-CNG payload to the comfort noise decoder for decoding.
1594 // Clearly wrong, but will maintain bit-exactness with legacy.
1595 if (fs_hz_ == 8000) {
1596 packet->header.payloadType =
1597 decoder_database_->GetRtpPayloadType(kDecoderCNGnb);
1598 } else if (fs_hz_ == 16000) {
1599 packet->header.payloadType =
1600 decoder_database_->GetRtpPayloadType(kDecoderCNGwb);
1601 } else if (fs_hz_ == 32000) {
1602 packet->header.payloadType =
1603 decoder_database_->GetRtpPayloadType(kDecoderCNGswb32kHz);
1604 } else if (fs_hz_ == 48000) {
1605 packet->header.payloadType =
1606 decoder_database_->GetRtpPayloadType(kDecoderCNGswb48kHz);
1607 }
1608 assert(decoder_database_->IsComfortNoise(packet->header.payloadType));
1609#else
1610 LOG(LS_ERROR) << "Trying to decode non-CNG payload as CNG.";
1611 return kOtherError;
1612#endif
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001613 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001614 // UpdateParameters() deletes |packet|.
1615 if (comfort_noise_->UpdateParameters(packet) ==
1616 ComfortNoise::kInternalError) {
1617 LOG_FERR0(LS_WARNING, UpdateParameters);
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001618 algorithm_buffer_->Zeros(output_size_samples_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001619 return -comfort_noise_->internal_error_code();
1620 }
1621 }
1622 int cn_return = comfort_noise_->Generate(output_size_samples_,
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +00001623 algorithm_buffer_.get());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001624 expand_->Reset();
1625 last_mode_ = kModeRfc3389Cng;
1626 if (!play_dtmf) {
1627 dtmf_tone_generator_->Reset();
1628 }
1629 if (cn_return == ComfortNoise::kInternalError) {
1630 LOG_FERR1(LS_WARNING, comfort_noise_->Generate, cn_return);
1631 decoder_error_code_ = comfort_noise_->internal_error_code();
1632 return kComfortNoiseErrorCode;
1633 } else if (cn_return == ComfortNoise::kUnknownPayloadType) {
1634 LOG_FERR1(LS_WARNING, comfort_noise_->Generate, cn_return);
1635 return kUnknownRtpPayloadType;
1636 }
1637 return 0;
1638}
1639
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001640void NetEqImpl::DoCodecInternalCng() {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001641 int length = 0;
1642 // TODO(hlundin): Will probably need a longer buffer for multi-channel.
1643 int16_t decoded_buffer[kMaxFrameSize];
1644 AudioDecoder* decoder = decoder_database_->GetActiveDecoder();
1645 if (decoder) {
1646 const uint8_t* dummy_payload = NULL;
1647 AudioDecoder::SpeechType speech_type;
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +00001648 length = decoder->Decode(
1649 dummy_payload, 0, fs_hz_, kMaxFrameSize * sizeof(int16_t),
1650 decoded_buffer, &speech_type);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001651 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001652 assert(mute_factor_array_.get());
henrik.lundin@webrtc.org40d3fc62013-09-18 12:19:50 +00001653 normal_->Process(decoded_buffer, length, last_mode_, mute_factor_array_.get(),
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +00001654 algorithm_buffer_.get());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001655 last_mode_ = kModeCodecInternalCng;
1656 expand_->Reset();
1657}
1658
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001659int NetEqImpl::DoDtmf(const DtmfEvent& dtmf_event, bool* play_dtmf) {
turaj@webrtc.org4d06db52013-03-27 18:31:42 +00001660 // This block of the code and the block further down, handling |dtmf_switch|
1661 // are commented out. Otherwise playing out-of-band DTMF would fail in VoE
1662 // test, DtmfTest.ManualSuccessfullySendsOutOfBandTelephoneEvents. This is
1663 // equivalent to |dtmf_switch| always be false.
1664 //
1665 // See http://webrtc-codereview.appspot.com/1195004/ for discussion
1666 // On this issue. This change might cause some glitches at the point of
1667 // switch from audio to DTMF. Issue 1545 is filed to track this.
1668 //
1669 // bool dtmf_switch = false;
1670 // if ((last_mode_ != kModeDtmf) && dtmf_tone_generator_->initialized()) {
1671 // // Special case; see below.
1672 // // We must catch this before calling Generate, since |initialized| is
1673 // // modified in that call.
1674 // dtmf_switch = true;
1675 // }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001676
1677 int dtmf_return_value = 0;
1678 if (!dtmf_tone_generator_->initialized()) {
1679 // Initialize if not already done.
1680 dtmf_return_value = dtmf_tone_generator_->Init(fs_hz_, dtmf_event.event_no,
1681 dtmf_event.volume);
1682 }
turaj@webrtc.org4d06db52013-03-27 18:31:42 +00001683
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001684 if (dtmf_return_value == 0) {
1685 // Generate DTMF signal.
1686 dtmf_return_value = dtmf_tone_generator_->Generate(output_size_samples_,
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +00001687 algorithm_buffer_.get());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001688 }
turaj@webrtc.org4d06db52013-03-27 18:31:42 +00001689
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001690 if (dtmf_return_value < 0) {
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001691 algorithm_buffer_->Zeros(output_size_samples_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001692 return dtmf_return_value;
1693 }
1694
turaj@webrtc.org4d06db52013-03-27 18:31:42 +00001695 // if (dtmf_switch) {
1696 // // This is the special case where the previous operation was DTMF
1697 // // overdub, but the current instruction is "regular" DTMF. We must make
1698 // // sure that the DTMF does not have any discontinuities. The first DTMF
1699 // // sample that we generate now must be played out immediately, therefore
1700 // // it must be copied to the speech buffer.
1701 // // TODO(hlundin): This code seems incorrect. (Legacy.) Write test and
1702 // // verify correct operation.
1703 // assert(false);
1704 // // Must generate enough data to replace all of the |sync_buffer_|
1705 // // "future".
1706 // int required_length = sync_buffer_->FutureLength();
1707 // assert(dtmf_tone_generator_->initialized());
1708 // dtmf_return_value = dtmf_tone_generator_->Generate(required_length,
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001709 // algorithm_buffer_);
1710 // assert((size_t) required_length == algorithm_buffer_->Size());
turaj@webrtc.org4d06db52013-03-27 18:31:42 +00001711 // if (dtmf_return_value < 0) {
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001712 // algorithm_buffer_->Zeros(output_size_samples_);
turaj@webrtc.org4d06db52013-03-27 18:31:42 +00001713 // return dtmf_return_value;
1714 // }
1715 //
1716 // // Overwrite the "future" part of the speech buffer with the new DTMF
1717 // // data.
1718 // // TODO(hlundin): It seems that this overwriting has gone lost.
1719 // // Not adapted for multi-channel yet.
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001720 // assert(algorithm_buffer_->Channels() == 1);
1721 // if (algorithm_buffer_->Channels() != 1) {
turaj@webrtc.org4d06db52013-03-27 18:31:42 +00001722 // LOG(LS_WARNING) << "DTMF not supported for more than one channel";
1723 // return kStereoNotSupported;
1724 // }
1725 // // Shuffle the remaining data to the beginning of algorithm buffer.
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001726 // algorithm_buffer_->PopFront(sync_buffer_->FutureLength());
turaj@webrtc.org4d06db52013-03-27 18:31:42 +00001727 // }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001728
Peter Kastingb7e50542015-06-11 12:55:50 -07001729 sync_buffer_->IncreaseEndTimestamp(
1730 static_cast<uint32_t>(output_size_samples_));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001731 expand_->Reset();
1732 last_mode_ = kModeDtmf;
1733
1734 // Set to false because the DTMF is already in the algorithm buffer.
1735 *play_dtmf = false;
1736 return 0;
1737}
1738
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001739void NetEqImpl::DoAlternativePlc(bool increase_timestamp) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001740 AudioDecoder* decoder = decoder_database_->GetActiveDecoder();
1741 int length;
1742 if (decoder && decoder->HasDecodePlc()) {
1743 // Use the decoder's packet-loss concealment.
1744 // TODO(hlundin): Will probably need a longer buffer for multi-channel.
1745 int16_t decoded_buffer[kMaxFrameSize];
1746 length = decoder->DecodePlc(1, decoded_buffer);
1747 if (length > 0) {
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001748 algorithm_buffer_->PushBackInterleaved(decoded_buffer, length);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001749 } else {
1750 length = 0;
1751 }
1752 } else {
1753 // Do simple zero-stuffing.
1754 length = output_size_samples_;
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001755 algorithm_buffer_->Zeros(length);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001756 // By not advancing the timestamp, NetEq inserts samples.
1757 stats_.AddZeros(length);
1758 }
1759 if (increase_timestamp) {
Peter Kastingb7e50542015-06-11 12:55:50 -07001760 sync_buffer_->IncreaseEndTimestamp(static_cast<uint32_t>(length));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001761 }
1762 expand_->Reset();
1763}
1764
1765int NetEqImpl::DtmfOverdub(const DtmfEvent& dtmf_event, size_t num_channels,
1766 int16_t* output) const {
1767 size_t out_index = 0;
1768 int overdub_length = output_size_samples_; // Default value.
1769
1770 if (sync_buffer_->dtmf_index() > sync_buffer_->next_index()) {
1771 // Special operation for transition from "DTMF only" to "DTMF overdub".
1772 out_index = std::min(
1773 sync_buffer_->dtmf_index() - sync_buffer_->next_index(),
1774 static_cast<size_t>(output_size_samples_));
turaj@webrtc.org362a55e2013-09-20 16:25:28 +00001775 overdub_length = output_size_samples_ - static_cast<int>(out_index);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001776 }
1777
henrik.lundin@webrtc.orgfd11bbf2013-09-30 20:38:44 +00001778 AudioMultiVector dtmf_output(num_channels);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001779 int dtmf_return_value = 0;
1780 if (!dtmf_tone_generator_->initialized()) {
1781 dtmf_return_value = dtmf_tone_generator_->Init(fs_hz_, dtmf_event.event_no,
1782 dtmf_event.volume);
1783 }
1784 if (dtmf_return_value == 0) {
1785 dtmf_return_value = dtmf_tone_generator_->Generate(overdub_length,
1786 &dtmf_output);
1787 assert((size_t) overdub_length == dtmf_output.Size());
1788 }
1789 dtmf_output.ReadInterleaved(overdub_length, &output[out_index]);
1790 return dtmf_return_value < 0 ? dtmf_return_value : 0;
1791}
1792
1793int NetEqImpl::ExtractPackets(int required_samples, PacketList* packet_list) {
1794 bool first_packet = true;
1795 uint8_t prev_payload_type = 0;
1796 uint32_t prev_timestamp = 0;
1797 uint16_t prev_sequence_number = 0;
1798 bool next_packet_available = false;
1799
henrik.lundin@webrtc.orge1d468c2013-01-30 07:37:20 +00001800 const RTPHeader* header = packet_buffer_->NextRtpHeader();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001801 assert(header);
1802 if (!header) {
1803 return -1;
1804 }
turaj@webrtc.org7df97062013-08-02 18:07:13 +00001805 uint32_t first_timestamp = header->timestamp;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001806 int extracted_samples = 0;
1807
1808 // Packet extraction loop.
1809 do {
1810 timestamp_ = header->timestamp;
1811 int discard_count = 0;
henrik.lundin@webrtc.orge1d468c2013-01-30 07:37:20 +00001812 Packet* packet = packet_buffer_->GetNextPacket(&discard_count);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001813 // |header| may be invalid after the |packet_buffer_| operation.
1814 header = NULL;
1815 if (!packet) {
1816 LOG_FERR1(LS_ERROR, GetNextPacket, discard_count) <<
1817 "Should always be able to extract a packet here";
1818 assert(false); // Should always be able to extract a packet here.
1819 return -1;
1820 }
1821 stats_.PacketsDiscarded(discard_count);
1822 // Store waiting time in ms; packets->waiting_time is in "output blocks".
1823 stats_.StoreWaitingTime(packet->waiting_time * kOutputSizeMs);
1824 assert(packet->payload_length > 0);
1825 packet_list->push_back(packet); // Store packet in list.
1826
1827 if (first_packet) {
1828 first_packet = false;
minyue@webrtc.orgd7301772013-08-29 00:58:14 +00001829 decoded_packet_sequence_number_ = prev_sequence_number =
1830 packet->header.sequenceNumber;
1831 decoded_packet_timestamp_ = prev_timestamp = packet->header.timestamp;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001832 prev_payload_type = packet->header.payloadType;
1833 }
1834
1835 // Store number of extracted samples.
1836 int packet_duration = 0;
1837 AudioDecoder* decoder = decoder_database_->GetDecoder(
1838 packet->header.payloadType);
1839 if (decoder) {
minyue@webrtc.orgb28bfa72014-03-21 12:07:40 +00001840 if (packet->sync_packet) {
1841 packet_duration = decoder_frame_length_;
1842 } else {
minyue@webrtc.org2c1bcf22015-02-17 10:17:09 +00001843 if (packet->primary) {
1844 packet_duration = decoder->PacketDuration(packet->payload,
1845 packet->payload_length);
1846 } else {
1847 packet_duration = decoder->
1848 PacketDurationRedundant(packet->payload, packet->payload_length);
1849 stats_.SecondaryDecodedSamples(packet_duration);
1850 }
minyue@webrtc.orgb28bfa72014-03-21 12:07:40 +00001851 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001852 } else {
pkasting@chromium.org026b8922015-01-30 19:53:42 +00001853 LOG_FERR1(LS_WARNING, GetDecoder,
1854 static_cast<int>(packet->header.payloadType))
1855 << "Could not find a decoder for a packet about to be extracted.";
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001856 assert(false);
1857 }
1858 if (packet_duration <= 0) {
1859 // Decoder did not return a packet duration. Assume that the packet
1860 // contains the same number of samples as the previous one.
1861 packet_duration = decoder_frame_length_;
1862 }
1863 extracted_samples = packet->header.timestamp - first_timestamp +
1864 packet_duration;
1865
1866 // Check what packet is available next.
1867 header = packet_buffer_->NextRtpHeader();
1868 next_packet_available = false;
1869 if (header && prev_payload_type == header->payloadType) {
1870 int16_t seq_no_diff = header->sequenceNumber - prev_sequence_number;
1871 int32_t ts_diff = header->timestamp - prev_timestamp;
1872 if (seq_no_diff == 1 ||
1873 (seq_no_diff == 0 && ts_diff == decoder_frame_length_)) {
1874 // The next sequence number is available, or the next part of a packet
1875 // that was split into pieces upon insertion.
1876 next_packet_available = true;
1877 }
1878 prev_sequence_number = header->sequenceNumber;
1879 }
1880 } while (extracted_samples < required_samples && next_packet_available);
1881
henrik.lundin@webrtc.org61217152014-09-22 08:30:07 +00001882 if (extracted_samples > 0) {
1883 // Delete old packets only when we are going to decode something. Otherwise,
1884 // we could end up in the situation where we never decode anything, since
1885 // all incoming packets are considered too old but the buffer will also
1886 // never be flooded and flushed.
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +00001887 packet_buffer_->DiscardAllOldPackets(timestamp_);
henrik.lundin@webrtc.org61217152014-09-22 08:30:07 +00001888 }
1889
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001890 return extracted_samples;
1891}
1892
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001893void NetEqImpl::UpdatePlcComponents(int fs_hz, size_t channels) {
1894 // Delete objects and create new ones.
1895 expand_.reset(expand_factory_->Create(background_noise_.get(),
1896 sync_buffer_.get(), &random_vector_,
1897 fs_hz, channels));
1898 merge_.reset(new Merge(fs_hz, channels, expand_.get(), sync_buffer_.get()));
1899}
1900
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001901void NetEqImpl::SetSampleRateAndChannels(int fs_hz, size_t channels) {
1902 LOG_API2(fs_hz, channels);
1903 // TODO(hlundin): Change to an enumerator and skip assert.
1904 assert(fs_hz == 8000 || fs_hz == 16000 || fs_hz == 32000 || fs_hz == 48000);
1905 assert(channels > 0);
1906
1907 fs_hz_ = fs_hz;
1908 fs_mult_ = fs_hz / 8000;
1909 output_size_samples_ = kOutputSizeMs * 8 * fs_mult_;
1910 decoder_frame_length_ = 3 * output_size_samples_; // Initialize to 30ms.
1911
1912 last_mode_ = kModeNormal;
1913
1914 // Create a new array of mute factors and set all to 1.
1915 mute_factor_array_.reset(new int16_t[channels]);
1916 for (size_t i = 0; i < channels; ++i) {
1917 mute_factor_array_[i] = 16384; // 1.0 in Q14.
1918 }
1919
1920 // Reset comfort noise decoder, if there is one active.
1921 AudioDecoder* cng_decoder = decoder_database_->GetActiveCngDecoder();
1922 if (cng_decoder) {
1923 cng_decoder->Init();
1924 }
1925
1926 // Reinit post-decode VAD with new sample rate.
1927 assert(vad_.get()); // Cannot be NULL here.
1928 vad_->Init();
1929
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001930 // Delete algorithm buffer and create a new one.
henrik.lundin@webrtc.orgfd11bbf2013-09-30 20:38:44 +00001931 algorithm_buffer_.reset(new AudioMultiVector(channels));
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001932
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001933 // Delete sync buffer and create a new one.
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +00001934 sync_buffer_.reset(new SyncBuffer(channels, kSyncBufferSize * fs_mult_));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001935
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00001936 // Delete BackgroundNoise object and create a new one.
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +00001937 background_noise_.reset(new BackgroundNoise(channels));
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00001938 background_noise_->set_mode(background_noise_mode_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001939
1940 // Reset random vector.
1941 random_vector_.Reset();
1942
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001943 UpdatePlcComponents(fs_hz, channels);
1944
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001945 // Move index so that we create a small set of future samples (all 0).
1946 sync_buffer_->set_next_index(sync_buffer_->next_index() -
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001947 expand_->overlap_length());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001948
henrik.lundin@webrtc.org40d3fc62013-09-18 12:19:50 +00001949 normal_.reset(new Normal(fs_hz, decoder_database_.get(), *background_noise_,
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +00001950 expand_.get()));
henrik.lundin@webrtc.orgd9faa462014-01-14 10:18:45 +00001951 accelerate_.reset(
1952 accelerate_factory_->Create(fs_hz, channels, *background_noise_));
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001953 preemptive_expand_.reset(preemptive_expand_factory_->Create(
1954 fs_hz, channels,
1955 *background_noise_,
1956 static_cast<int>(expand_->overlap_length())));
henrik.lundin@webrtc.org40d3fc62013-09-18 12:19:50 +00001957
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001958 // Delete ComfortNoise object and create a new one.
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +00001959 comfort_noise_.reset(new ComfortNoise(fs_hz, decoder_database_.get(),
1960 sync_buffer_.get()));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001961
1962 // Verify that |decoded_buffer_| is long enough.
1963 if (decoded_buffer_length_ < kMaxFrameSize * channels) {
1964 // Reallocate to larger size.
1965 decoded_buffer_length_ = kMaxFrameSize * channels;
1966 decoded_buffer_.reset(new int16_t[decoded_buffer_length_]);
1967 }
1968
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001969 // Create DecisionLogic if it is not created yet, then communicate new sample
1970 // rate and output size to DecisionLogic object.
1971 if (!decision_logic_.get()) {
henrik.lundin@webrtc.org7cbc4f92014-10-07 06:37:39 +00001972 CreateDecisionLogic();
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001973 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001974 decision_logic_->SetSampleRate(fs_hz_, output_size_samples_);
1975}
1976
1977NetEqOutputType NetEqImpl::LastOutputType() {
1978 assert(vad_.get());
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +00001979 assert(expand_.get());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001980 if (last_mode_ == kModeCodecInternalCng || last_mode_ == kModeRfc3389Cng) {
1981 return kOutputCNG;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001982 } else if (last_mode_ == kModeExpand && expand_->MuteFactor(0) == 0) {
1983 // Expand mode has faded down to background noise only (very long expand).
1984 return kOutputPLCtoCNG;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001985 } else if (last_mode_ == kModeExpand) {
1986 return kOutputPLC;
wu@webrtc.org24301a62013-12-13 19:17:43 +00001987 } else if (vad_->running() && !vad_->active_speech()) {
1988 return kOutputVADPassive;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001989 } else {
1990 return kOutputNormal;
1991 }
1992}
1993
henrik.lundin@webrtc.org7cbc4f92014-10-07 06:37:39 +00001994void NetEqImpl::CreateDecisionLogic() {
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001995 decision_logic_.reset(DecisionLogic::Create(fs_hz_, output_size_samples_,
henrik.lundin@webrtc.org7cbc4f92014-10-07 06:37:39 +00001996 playout_mode_,
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001997 decoder_database_.get(),
1998 *packet_buffer_.get(),
1999 delay_manager_.get(),
2000 buffer_level_filter_.get()));
2001}
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00002002} // namespace webrtc