blob: 3a3ad9809b7fefda8fd60cf104c41304b1f8a1e5 [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());
318 const int total_samples_in_buffers = packet_buffer_->NumSamplesInBuffer(
319 decoder_database_.get(), decoder_frame_length_) +
turaj@webrtc.org362a55e2013-09-20 16:25:28 +0000320 static_cast<int>(sync_buffer_->FutureLength());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000321 assert(delay_manager_.get());
322 assert(decision_logic_.get());
323 stats_.GetNetworkStatistics(fs_hz_, total_samples_in_buffers,
324 decoder_frame_length_, *delay_manager_.get(),
325 *decision_logic_.get(), stats);
326 return 0;
327}
328
329void NetEqImpl::WaitingTimes(std::vector<int>* waiting_times) {
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +0000330 CriticalSectionScoped lock(crit_sect_.get());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000331 stats_.WaitingTimes(waiting_times);
332}
333
334void NetEqImpl::GetRtcpStatistics(RtcpStatistics* stats) {
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +0000335 CriticalSectionScoped lock(crit_sect_.get());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000336 if (stats) {
337 rtcp_.GetStatistics(false, stats);
338 }
339}
340
341void NetEqImpl::GetRtcpStatisticsNoReset(RtcpStatistics* stats) {
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +0000342 CriticalSectionScoped lock(crit_sect_.get());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000343 if (stats) {
344 rtcp_.GetStatistics(true, stats);
345 }
346}
347
348void NetEqImpl::EnableVad() {
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +0000349 CriticalSectionScoped lock(crit_sect_.get());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000350 assert(vad_.get());
351 vad_->Enable();
352}
353
354void NetEqImpl::DisableVad() {
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +0000355 CriticalSectionScoped lock(crit_sect_.get());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000356 assert(vad_.get());
357 vad_->Disable();
358}
359
wu@webrtc.org94454b72014-06-05 20:34:08 +0000360bool NetEqImpl::GetPlayoutTimestamp(uint32_t* timestamp) {
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +0000361 CriticalSectionScoped lock(crit_sect_.get());
wu@webrtc.org94454b72014-06-05 20:34:08 +0000362 if (first_packet_) {
363 // We don't have a valid RTP timestamp until we have decoded our first
364 // RTP packet.
365 return false;
366 }
367 *timestamp = timestamp_scaler_->ToExternal(playout_timestamp_);
368 return true;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000369}
370
Karl Wiberg7f6c4d42015-04-09 15:44:22 +0200371int NetEqImpl::SetTargetNumberOfChannels() {
372 return kNotImplemented;
373}
374
375int NetEqImpl::SetTargetSampleRate() {
376 return kNotImplemented;
377}
378
henrik.lundin@webrtc.orgb0f4b3d2014-11-04 08:53:10 +0000379int NetEqImpl::LastError() const {
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +0000380 CriticalSectionScoped lock(crit_sect_.get());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000381 return error_code_;
382}
383
384int NetEqImpl::LastDecoderError() {
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +0000385 CriticalSectionScoped lock(crit_sect_.get());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000386 return decoder_error_code_;
387}
388
389void NetEqImpl::FlushBuffers() {
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +0000390 CriticalSectionScoped lock(crit_sect_.get());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000391 LOG_API0();
392 packet_buffer_->Flush();
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +0000393 assert(sync_buffer_.get());
394 assert(expand_.get());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000395 sync_buffer_->Flush();
396 sync_buffer_->set_next_index(sync_buffer_->next_index() -
397 expand_->overlap_length());
398 // Set to wait for new codec.
399 first_packet_ = true;
400}
401
turaj@webrtc.org3170b572013-08-30 15:36:53 +0000402void NetEqImpl::PacketBufferStatistics(int* current_num_packets,
henrik.lundin@webrtc.org116ed1d2014-04-28 08:20:04 +0000403 int* max_num_packets) const {
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +0000404 CriticalSectionScoped lock(crit_sect_.get());
henrik.lundin@webrtc.org116ed1d2014-04-28 08:20:04 +0000405 packet_buffer_->BufferStat(current_num_packets, max_num_packets);
turaj@webrtc.org3170b572013-08-30 15:36:53 +0000406}
407
turaj@webrtc.orgff43c852013-09-25 00:07:27 +0000408int NetEqImpl::DecodedRtpInfo(int* sequence_number, uint32_t* timestamp) const {
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +0000409 CriticalSectionScoped lock(crit_sect_.get());
minyue@webrtc.orgd7301772013-08-29 00:58:14 +0000410 if (decoded_packet_sequence_number_ < 0)
411 return -1;
412 *sequence_number = decoded_packet_sequence_number_;
413 *timestamp = decoded_packet_timestamp_;
414 return 0;
415}
416
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000417const SyncBuffer* NetEqImpl::sync_buffer_for_test() const {
418 CriticalSectionScoped lock(crit_sect_.get());
419 return sync_buffer_.get();
420}
421
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000422// Methods below this line are private.
423
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000424int NetEqImpl::InsertPacketInternal(const WebRtcRTPHeader& rtp_header,
425 const uint8_t* payload,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000426 size_t length_bytes,
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +0000427 uint32_t receive_timestamp,
428 bool is_sync_packet) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000429 if (!payload) {
430 LOG_F(LS_ERROR) << "payload == NULL";
431 return kInvalidPointer;
432 }
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +0000433 // Sanity checks for sync-packets.
434 if (is_sync_packet) {
435 if (decoder_database_->IsDtmf(rtp_header.header.payloadType) ||
436 decoder_database_->IsRed(rtp_header.header.payloadType) ||
437 decoder_database_->IsComfortNoise(rtp_header.header.payloadType)) {
438 LOG_F(LS_ERROR) << "Sync-packet with an unacceptable payload type "
pkasting@chromium.orgd3245462015-02-23 21:28:22 +0000439 << static_cast<int>(rtp_header.header.payloadType);
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +0000440 return kSyncPacketNotAccepted;
441 }
442 if (first_packet_ ||
443 rtp_header.header.payloadType != current_rtp_payload_type_ ||
444 rtp_header.header.ssrc != ssrc_) {
445 // Even if |current_rtp_payload_type_| is 0xFF, sync-packet isn't
446 // accepted.
pkasting@chromium.orgd3245462015-02-23 21:28:22 +0000447 LOG_F(LS_ERROR)
448 << "Changing codec, SSRC or first packet with sync-packet.";
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +0000449 return kSyncPacketNotAccepted;
450 }
451 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000452 PacketList packet_list;
453 RTPHeader main_header;
454 {
henrik.lundin@webrtc.orge1d468c2013-01-30 07:37:20 +0000455 // Convert to Packet.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000456 // Create |packet| within this separate scope, since it should not be used
457 // directly once it's been inserted in the packet list. This way, |packet|
458 // is not defined outside of this block.
henrik.lundin@webrtc.orge1d468c2013-01-30 07:37:20 +0000459 Packet* packet = new Packet;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000460 packet->header.markerBit = false;
461 packet->header.payloadType = rtp_header.header.payloadType;
462 packet->header.sequenceNumber = rtp_header.header.sequenceNumber;
463 packet->header.timestamp = rtp_header.header.timestamp;
464 packet->header.ssrc = rtp_header.header.ssrc;
465 packet->header.numCSRCs = 0;
466 packet->payload_length = length_bytes;
467 packet->primary = true;
468 packet->waiting_time = 0;
469 packet->payload = new uint8_t[packet->payload_length];
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +0000470 packet->sync_packet = is_sync_packet;
henrik.lundin@webrtc.org73deaad2013-01-31 13:32:51 +0000471 if (!packet->payload) {
472 LOG_F(LS_ERROR) << "Payload pointer is NULL.";
473 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000474 assert(payload); // Already checked above.
475 memcpy(packet->payload, payload, packet->payload_length);
476 // Insert packet in a packet list.
477 packet_list.push_back(packet);
478 // Save main payloads header for later.
479 memcpy(&main_header, &packet->header, sizeof(main_header));
480 }
481
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000482 bool update_sample_rate_and_channels = false;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000483 // Reinitialize NetEq if it's needed (changed SSRC or first call).
484 if ((main_header.ssrc != ssrc_) || first_packet_) {
henrik.lundin@webrtc.org6ff3ac12014-11-20 14:14:49 +0000485 // Note: |first_packet_| will be cleared further down in this method, once
486 // the packet has been successfully inserted into the packet buffer.
487
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000488 rtcp_.Init(main_header.sequenceNumber);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000489
490 // Flush the packet buffer and DTMF buffer.
491 packet_buffer_->Flush();
492 dtmf_buffer_->Flush();
493
494 // Store new SSRC.
495 ssrc_ = main_header.ssrc;
496
turaj@webrtc.org4d06db52013-03-27 18:31:42 +0000497 // Update audio buffer timestamp.
498 sync_buffer_->IncreaseEndTimestamp(main_header.timestamp - timestamp_);
499
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000500 // Update codecs.
501 timestamp_ = main_header.timestamp;
502 current_rtp_payload_type_ = main_header.payloadType;
503
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000504 // Reset timestamp scaling.
505 timestamp_scaler_->Reset();
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000506
henrik.lundin@webrtc.org6ff3ac12014-11-20 14:14:49 +0000507 // Trigger an update of sampling rate and the number of channels.
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000508 update_sample_rate_and_channels = true;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000509 }
510
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +0000511 // Update RTCP statistics, only for regular packets.
512 if (!is_sync_packet)
513 rtcp_.Update(main_header, receive_timestamp);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000514
515 // Check for RED payload type, and separate payloads into several packets.
516 if (decoder_database_->IsRed(main_header.payloadType)) {
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +0000517 assert(!is_sync_packet); // We had a sanity check for this.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000518 if (payload_splitter_->SplitRed(&packet_list) != PayloadSplitter::kOK) {
519 LOG_FERR1(LS_WARNING, SplitRed, packet_list.size());
520 PacketBuffer::DeleteAllPackets(&packet_list);
521 return kRedundancySplitError;
522 }
523 // Only accept a few RED payloads of the same type as the main data,
524 // DTMF events and CNG.
525 payload_splitter_->CheckRedPayloads(&packet_list, *decoder_database_);
526 // Update the stored main payload header since the main payload has now
527 // changed.
528 memcpy(&main_header, &packet_list.front()->header, sizeof(main_header));
529 }
530
531 // Check payload types.
532 if (decoder_database_->CheckPayloadTypes(packet_list) ==
533 DecoderDatabase::kDecoderNotFound) {
534 LOG_FERR1(LS_WARNING, CheckPayloadTypes, packet_list.size());
535 PacketBuffer::DeleteAllPackets(&packet_list);
536 return kUnknownRtpPayloadType;
537 }
538
539 // Scale timestamp to internal domain (only for some codecs).
540 timestamp_scaler_->ToInternal(&packet_list);
541
542 // Process DTMF payloads. Cycle through the list of packets, and pick out any
543 // DTMF payloads found.
544 PacketList::iterator it = packet_list.begin();
545 while (it != packet_list.end()) {
546 Packet* current_packet = (*it);
547 assert(current_packet);
548 assert(current_packet->payload);
549 if (decoder_database_->IsDtmf(current_packet->header.payloadType)) {
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +0000550 assert(!current_packet->sync_packet); // We had a sanity check for this.
minyue@webrtc.org9721db72013-08-06 05:36:26 +0000551 DtmfEvent event;
552 int ret = DtmfBuffer::ParseEvent(
553 current_packet->header.timestamp,
554 current_packet->payload,
555 current_packet->payload_length,
556 &event);
557 if (ret != DtmfBuffer::kOK) {
558 LOG_FERR2(LS_WARNING, ParseEvent, ret,
559 current_packet->payload_length);
560 PacketBuffer::DeleteAllPackets(&packet_list);
561 return kDtmfParsingError;
562 }
563 if (dtmf_buffer_->InsertEvent(event) != DtmfBuffer::kOK) {
564 LOG_FERR0(LS_WARNING, InsertEvent);
565 PacketBuffer::DeleteAllPackets(&packet_list);
566 return kDtmfInsertError;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000567 }
568 // TODO(hlundin): Let the destructor of Packet handle the payload.
569 delete [] current_packet->payload;
570 delete current_packet;
571 it = packet_list.erase(it);
572 } else {
573 ++it;
574 }
575 }
576
minyue@webrtc.org7549ff42014-04-02 15:03:01 +0000577 // Check for FEC in packets, and separate payloads into several packets.
578 int ret = payload_splitter_->SplitFec(&packet_list, decoder_database_.get());
579 if (ret != PayloadSplitter::kOK) {
580 LOG_FERR1(LS_WARNING, SplitFec, packet_list.size());
581 PacketBuffer::DeleteAllPackets(&packet_list);
582 switch (ret) {
583 case PayloadSplitter::kUnknownPayloadType:
584 return kUnknownRtpPayloadType;
585 default:
586 return kOtherError;
587 }
588 }
589
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000590 // Split payloads into smaller chunks. This also verifies that all payloads
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +0000591 // are of a known payload type. SplitAudio() method is protected against
592 // sync-packets.
minyue@webrtc.orgb28bfa72014-03-21 12:07:40 +0000593 ret = payload_splitter_->SplitAudio(&packet_list, *decoder_database_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000594 if (ret != PayloadSplitter::kOK) {
595 LOG_FERR1(LS_WARNING, SplitAudio, packet_list.size());
596 PacketBuffer::DeleteAllPackets(&packet_list);
597 switch (ret) {
598 case PayloadSplitter::kUnknownPayloadType:
599 return kUnknownRtpPayloadType;
600 case PayloadSplitter::kFrameSplitError:
601 return kFrameSplitError;
602 default:
603 return kOtherError;
604 }
605 }
606
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +0000607 // Update bandwidth estimate, if the packet is not sync-packet.
608 if (!packet_list.empty() && !packet_list.front()->sync_packet) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000609 // The list can be empty here if we got nothing but DTMF payloads.
610 AudioDecoder* decoder =
611 decoder_database_->GetDecoder(main_header.payloadType);
612 assert(decoder); // Should always get a valid object, since we have
613 // already checked that the payload types are known.
614 decoder->IncomingPacket(packet_list.front()->payload,
615 packet_list.front()->payload_length,
616 packet_list.front()->header.sequenceNumber,
617 packet_list.front()->header.timestamp,
618 receive_timestamp);
619 }
620
621 // Insert packets in buffer.
622 int temp_bufsize = packet_buffer_->NumPacketsInBuffer();
623 ret = packet_buffer_->InsertPacketList(
624 &packet_list,
625 *decoder_database_,
626 &current_rtp_payload_type_,
627 &current_cng_rtp_payload_type_);
628 if (ret == PacketBuffer::kFlushed) {
629 // Reset DSP timestamp etc. if packet buffer flushed.
630 new_codec_ = true;
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000631 update_sample_rate_and_channels = true;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000632 LOG_F(LS_WARNING) << "Packet buffer flushed";
633 } else if (ret != PacketBuffer::kOK) {
634 LOG_FERR1(LS_WARNING, InsertPacketList, packet_list.size());
635 PacketBuffer::DeleteAllPackets(&packet_list);
minyue@webrtc.org7bb54362013-08-06 05:40:57 +0000636 return kOtherError;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000637 }
henrik.lundin@webrtc.org6ff3ac12014-11-20 14:14:49 +0000638
639 if (first_packet_) {
640 first_packet_ = false;
641 // Update the codec on the next GetAudio call.
642 new_codec_ = true;
643 }
644
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000645 if (current_rtp_payload_type_ != 0xFF) {
646 const DecoderDatabase::DecoderInfo* dec_info =
647 decoder_database_->GetDecoderInfo(current_rtp_payload_type_);
648 if (!dec_info) {
649 assert(false); // Already checked that the payload type is known.
650 }
651 }
652
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000653 if (update_sample_rate_and_channels && !packet_buffer_->Empty()) {
654 // We do not use |current_rtp_payload_type_| to |set payload_type|, but
655 // get the next RTP header from |packet_buffer_| to obtain the payload type.
656 // The reason for it is the following corner case. If NetEq receives a
657 // CNG packet with a sample rate different than the current CNG then it
658 // flushes its buffer, assuming send codec must have been changed. However,
659 // payload type of the hypothetically new send codec is not known.
660 const RTPHeader* rtp_header = packet_buffer_->NextRtpHeader();
661 assert(rtp_header);
662 int payload_type = rtp_header->payloadType;
663 AudioDecoder* decoder = decoder_database_->GetDecoder(payload_type);
664 assert(decoder); // Payloads are already checked to be valid.
665 const DecoderDatabase::DecoderInfo* decoder_info =
666 decoder_database_->GetDecoderInfo(payload_type);
667 assert(decoder_info);
668 if (decoder_info->fs_hz != fs_hz_ ||
henrik.lundin@webrtc.org6dba1eb2015-03-18 09:47:08 +0000669 decoder->Channels() != algorithm_buffer_->Channels())
670 SetSampleRateAndChannels(decoder_info->fs_hz, decoder->Channels());
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000671 }
672
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000673 // TODO(hlundin): Move this code to DelayManager class.
674 const DecoderDatabase::DecoderInfo* dec_info =
675 decoder_database_->GetDecoderInfo(main_header.payloadType);
676 assert(dec_info); // Already checked that the payload type is known.
677 delay_manager_->LastDecoderType(dec_info->codec_type);
678 if (delay_manager_->last_pack_cng_or_dtmf() == 0) {
679 // Calculate the total speech length carried in each packet.
680 temp_bufsize = packet_buffer_->NumPacketsInBuffer() - temp_bufsize;
681 temp_bufsize *= decoder_frame_length_;
682
683 if ((temp_bufsize > 0) &&
684 (temp_bufsize != decision_logic_->packet_length_samples())) {
685 decision_logic_->set_packet_length_samples(temp_bufsize);
686 delay_manager_->SetPacketAudioLength((1000 * temp_bufsize) / fs_hz_);
687 }
688
689 // Update statistics.
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000690 if ((int32_t) (main_header.timestamp - timestamp_) >= 0 &&
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000691 !new_codec_) {
692 // Only update statistics if incoming packet is not older than last played
693 // out packet, and if new codec flag is not set.
694 delay_manager_->Update(main_header.sequenceNumber, main_header.timestamp,
695 fs_hz_);
696 }
697 } else if (delay_manager_->last_pack_cng_or_dtmf() == -1) {
698 // This is first "normal" packet after CNG or DTMF.
699 // Reset packet time counter and measure time until next packet,
700 // but don't update statistics.
701 delay_manager_->set_last_pack_cng_or_dtmf(0);
702 delay_manager_->ResetPacketIatCount();
703 }
704 return 0;
705}
706
707int NetEqImpl::GetAudioInternal(size_t max_length, int16_t* output,
708 int* samples_per_channel, int* num_channels) {
709 PacketList packet_list;
710 DtmfEvent dtmf_event;
711 Operations operation;
712 bool play_dtmf;
713 int return_value = GetDecision(&operation, &packet_list, &dtmf_event,
714 &play_dtmf);
715 if (return_value != 0) {
716 LOG_FERR1(LS_WARNING, GetDecision, return_value);
717 assert(false);
718 last_mode_ = kModeError;
719 return return_value;
720 }
turaj@webrtc.org0c0fae82013-09-25 17:42:17 +0000721 LOG(LS_VERBOSE) << "GetDecision returned operation=" << operation <<
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000722 " and " << packet_list.size() << " packet(s)";
723
724 AudioDecoder::SpeechType speech_type;
725 int length = 0;
726 int decode_return_value = Decode(&packet_list, &operation,
727 &length, &speech_type);
728
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000729 assert(vad_.get());
730 bool sid_frame_available =
731 (operation == kRfc3389Cng && !packet_list.empty());
732 vad_->Update(decoded_buffer_.get(), length, speech_type,
733 sid_frame_available, fs_hz_);
734
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +0000735 algorithm_buffer_->Clear();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000736 switch (operation) {
737 case kNormal: {
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +0000738 DoNormal(decoded_buffer_.get(), length, speech_type, play_dtmf);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000739 break;
740 }
741 case kMerge: {
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +0000742 DoMerge(decoded_buffer_.get(), length, speech_type, play_dtmf);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000743 break;
744 }
745 case kExpand: {
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +0000746 return_value = DoExpand(play_dtmf);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000747 break;
748 }
Henrik Lundincf808d22015-05-27 14:33:29 +0200749 case kAccelerate:
750 case kFastAccelerate: {
751 const bool fast_accelerate =
752 enable_fast_accelerate_ && (operation == kFastAccelerate);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000753 return_value = DoAccelerate(decoded_buffer_.get(), length, speech_type,
Henrik Lundincf808d22015-05-27 14:33:29 +0200754 play_dtmf, fast_accelerate);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000755 break;
756 }
757 case kPreemptiveExpand: {
758 return_value = DoPreemptiveExpand(decoded_buffer_.get(), length,
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +0000759 speech_type, play_dtmf);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000760 break;
761 }
762 case kRfc3389Cng:
763 case kRfc3389CngNoPacket: {
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +0000764 return_value = DoRfc3389Cng(&packet_list, play_dtmf);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000765 break;
766 }
767 case kCodecInternalCng: {
768 // This handles the case when there is no transmission and the decoder
769 // should produce internal comfort noise.
770 // TODO(hlundin): Write test for codec-internal CNG.
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +0000771 DoCodecInternalCng();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000772 break;
773 }
774 case kDtmf: {
775 // TODO(hlundin): Write test for this.
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +0000776 return_value = DoDtmf(dtmf_event, &play_dtmf);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000777 break;
778 }
779 case kAlternativePlc: {
780 // TODO(hlundin): Write test for this.
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +0000781 DoAlternativePlc(false);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000782 break;
783 }
784 case kAlternativePlcIncreaseTimestamp: {
785 // TODO(hlundin): Write test for this.
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +0000786 DoAlternativePlc(true);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000787 break;
788 }
789 case kAudioRepetitionIncreaseTimestamp: {
790 // TODO(hlundin): Write test for this.
Peter Kastingb7e50542015-06-11 12:55:50 -0700791 sync_buffer_->IncreaseEndTimestamp(
792 static_cast<uint32_t>(output_size_samples_));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000793 // Skipping break on purpose. Execution should move on into the
794 // next case.
kjellander@webrtc.org7d2b6a92015-01-28 18:37:58 +0000795 FALLTHROUGH();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000796 }
797 case kAudioRepetition: {
798 // TODO(hlundin): Write test for this.
799 // Copy last |output_size_samples_| from |sync_buffer_| to
800 // |algorithm_buffer|.
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +0000801 algorithm_buffer_->PushBackFromIndex(
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000802 *sync_buffer_, sync_buffer_->Size() - output_size_samples_);
803 expand_->Reset();
804 break;
805 }
806 case kUndefined: {
807 LOG_F(LS_ERROR) << "Invalid operation kUndefined.";
808 assert(false); // This should not happen.
809 last_mode_ = kModeError;
810 return kInvalidOperation;
811 }
812 } // End of switch.
813 if (return_value < 0) {
814 return return_value;
815 }
816
817 if (last_mode_ != kModeRfc3389Cng) {
818 comfort_noise_->Reset();
819 }
820
821 // Copy from |algorithm_buffer| to |sync_buffer_|.
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +0000822 sync_buffer_->PushBack(*algorithm_buffer_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000823
824 // Extract data from |sync_buffer_| to |output|.
turaj@webrtc.org362a55e2013-09-20 16:25:28 +0000825 size_t num_output_samples_per_channel = output_size_samples_;
826 size_t num_output_samples = output_size_samples_ * sync_buffer_->Channels();
827 if (num_output_samples > max_length) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000828 LOG(LS_WARNING) << "Output array is too short. " << max_length << " < " <<
829 output_size_samples_ << " * " << sync_buffer_->Channels();
830 num_output_samples = max_length;
turaj@webrtc.org362a55e2013-09-20 16:25:28 +0000831 num_output_samples_per_channel = static_cast<int>(
832 max_length / sync_buffer_->Channels());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000833 }
turaj@webrtc.org362a55e2013-09-20 16:25:28 +0000834 int samples_from_sync = static_cast<int>(
835 sync_buffer_->GetNextAudioInterleaved(num_output_samples_per_channel,
836 output));
837 *num_channels = static_cast<int>(sync_buffer_->Channels());
turaj@webrtc.org0c0fae82013-09-25 17:42:17 +0000838 LOG(LS_VERBOSE) << "Sync buffer (" << *num_channels << " channel(s)):" <<
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +0000839 " insert " << algorithm_buffer_->Size() << " samples, extract " <<
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000840 samples_from_sync << " samples";
841 if (samples_from_sync != output_size_samples_) {
842 LOG_F(LS_ERROR) << "samples_from_sync != output_size_samples_";
minyue@webrtc.orgdb1cefc2013-08-13 01:39:21 +0000843 // TODO(minyue): treatment of under-run, filling zeros
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000844 memset(output, 0, num_output_samples * sizeof(int16_t));
845 *samples_per_channel = output_size_samples_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000846 return kSampleUnderrun;
847 }
848 *samples_per_channel = output_size_samples_;
849
850 // Should always have overlap samples left in the |sync_buffer_|.
851 assert(sync_buffer_->FutureLength() >= expand_->overlap_length());
852
853 if (play_dtmf) {
854 return_value = DtmfOverdub(dtmf_event, sync_buffer_->Channels(), output);
855 }
856
857 // Update the background noise parameters if last operation wrote data
858 // straight from the decoder to the |sync_buffer_|. That is, none of the
859 // operations that modify the signal can be followed by a parameter update.
860 if ((last_mode_ == kModeNormal) ||
861 (last_mode_ == kModeAccelerateFail) ||
862 (last_mode_ == kModePreemptiveExpandFail) ||
863 (last_mode_ == kModeRfc3389Cng) ||
864 (last_mode_ == kModeCodecInternalCng)) {
865 background_noise_->Update(*sync_buffer_, *vad_.get());
866 }
867
868 if (operation == kDtmf) {
869 // DTMF data was written the end of |sync_buffer_|.
870 // Update index to end of DTMF data in |sync_buffer_|.
871 sync_buffer_->set_dtmf_index(sync_buffer_->Size());
872 }
873
henrik.lundin@webrtc.orged865b52014-03-06 10:28:07 +0000874 if (last_mode_ != kModeExpand) {
875 // If last operation was not expand, calculate the |playout_timestamp_| from
876 // the |sync_buffer_|. However, do not update the |playout_timestamp_| if it
877 // would be moved "backwards".
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000878 uint32_t temp_timestamp = sync_buffer_->end_timestamp() -
turaj@webrtc.org362a55e2013-09-20 16:25:28 +0000879 static_cast<uint32_t>(sync_buffer_->FutureLength());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000880 if (static_cast<int32_t>(temp_timestamp - playout_timestamp_) > 0) {
881 playout_timestamp_ = temp_timestamp;
882 }
883 } else {
884 // Use dead reckoning to estimate the |playout_timestamp_|.
Peter Kastingb7e50542015-06-11 12:55:50 -0700885 playout_timestamp_ += static_cast<uint32_t>(output_size_samples_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000886 }
887
888 if (decode_return_value) return decode_return_value;
889 return return_value;
890}
891
892int NetEqImpl::GetDecision(Operations* operation,
893 PacketList* packet_list,
894 DtmfEvent* dtmf_event,
895 bool* play_dtmf) {
896 // Initialize output variables.
897 *play_dtmf = false;
898 *operation = kUndefined;
899
900 // Increment time counters.
901 packet_buffer_->IncrementWaitingTimes();
902 stats_.IncreaseCounter(output_size_samples_, fs_hz_);
903
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +0000904 assert(sync_buffer_.get());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000905 uint32_t end_timestamp = sync_buffer_->end_timestamp();
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000906 if (!new_codec_) {
907 const uint32_t five_seconds_samples = 5 * fs_hz_;
908 packet_buffer_->DiscardOldPackets(end_timestamp, five_seconds_samples);
909 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000910 const RTPHeader* header = packet_buffer_->NextRtpHeader();
911
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +0000912 if (decision_logic_->CngRfc3389On() || last_mode_ == kModeRfc3389Cng) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000913 // Because of timestamp peculiarities, we have to "manually" disallow using
914 // a CNG packet with the same timestamp as the one that was last played.
915 // This can happen when using redundancy and will cause the timing to shift.
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000916 while (header && decoder_database_->IsComfortNoise(header->payloadType) &&
917 (end_timestamp >= header->timestamp ||
918 end_timestamp + decision_logic_->generated_noise_samples() >
919 header->timestamp)) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000920 // Don't use this packet, discard it.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000921 if (packet_buffer_->DiscardNextPacket() != PacketBuffer::kOK) {
922 assert(false); // Must be ok by design.
923 }
924 // Check buffer again.
925 if (!new_codec_) {
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000926 packet_buffer_->DiscardOldPackets(end_timestamp, 5 * fs_hz_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000927 }
928 header = packet_buffer_->NextRtpHeader();
929 }
930 }
931
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +0000932 assert(expand_.get());
turaj@webrtc.org362a55e2013-09-20 16:25:28 +0000933 const int samples_left = static_cast<int>(sync_buffer_->FutureLength() -
934 expand_->overlap_length());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000935 if (last_mode_ == kModeAccelerateSuccess ||
936 last_mode_ == kModeAccelerateLowEnergy ||
937 last_mode_ == kModePreemptiveExpandSuccess ||
938 last_mode_ == kModePreemptiveExpandLowEnergy) {
939 // Subtract (samples_left + output_size_samples_) from sampleMemory.
940 decision_logic_->AddSampleMemory(-(samples_left + output_size_samples_));
941 }
942
943 // Check if it is time to play a DTMF event.
Peter Kastingb7e50542015-06-11 12:55:50 -0700944 if (dtmf_buffer_->GetEvent(
945 static_cast<uint32_t>(
946 end_timestamp + decision_logic_->generated_noise_samples()),
947 dtmf_event)) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000948 *play_dtmf = true;
949 }
950
951 // Get instruction.
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +0000952 assert(sync_buffer_.get());
953 assert(expand_.get());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000954 *operation = decision_logic_->GetDecision(*sync_buffer_,
955 *expand_,
956 decoder_frame_length_,
957 header,
958 last_mode_,
959 *play_dtmf,
960 &reset_decoder_);
961
962 // Check if we already have enough samples in the |sync_buffer_|. If so,
963 // change decision to normal, unless the decision was merge, accelerate, or
964 // preemptive expand.
Henrik Lundincf808d22015-05-27 14:33:29 +0200965 if (samples_left >= output_size_samples_ && *operation != kMerge &&
966 *operation != kAccelerate && *operation != kFastAccelerate &&
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000967 *operation != kPreemptiveExpand) {
968 *operation = kNormal;
969 return 0;
970 }
971
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +0000972 decision_logic_->ExpandDecision(*operation);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000973
974 // Check conditions for reset.
975 if (new_codec_ || *operation == kUndefined) {
976 // The only valid reason to get kUndefined is that new_codec_ is set.
977 assert(new_codec_);
turaj@webrtc.org4d06db52013-03-27 18:31:42 +0000978 if (*play_dtmf && !header) {
979 timestamp_ = dtmf_event->timestamp;
980 } else {
981 assert(header);
982 if (!header) {
983 LOG_F(LS_ERROR) << "Packet missing where it shouldn't.";
984 return -1;
985 }
986 timestamp_ = header->timestamp;
987 if (*operation == kRfc3389CngNoPacket
988#ifndef LEGACY_BITEXACT
989 // Without this check, it can happen that a non-CNG packet is sent to
990 // the CNG decoder as if it was a SID frame. This is clearly a bug,
991 // but is kept for now to maintain bit-exactness with the test
992 // vectors.
993 && decoder_database_->IsComfortNoise(header->payloadType)
994#endif
995 ) {
996 // Change decision to CNG packet, since we do have a CNG packet, but it
997 // was considered too early to use. Now, use it anyway.
998 *operation = kRfc3389Cng;
999 } else if (*operation != kRfc3389Cng) {
1000 *operation = kNormal;
1001 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001002 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001003 // Adjust |sync_buffer_| timestamp before setting |end_timestamp| to the
1004 // new value.
1005 sync_buffer_->IncreaseEndTimestamp(timestamp_ - end_timestamp);
turaj@webrtc.org4d06db52013-03-27 18:31:42 +00001006 end_timestamp = timestamp_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001007 new_codec_ = false;
1008 decision_logic_->SoftReset();
1009 buffer_level_filter_->Reset();
1010 delay_manager_->Reset();
1011 stats_.ResetMcu();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001012 }
1013
1014 int required_samples = output_size_samples_;
1015 const int samples_10_ms = 80 * fs_mult_;
1016 const int samples_20_ms = 2 * samples_10_ms;
1017 const int samples_30_ms = 3 * samples_10_ms;
1018
1019 switch (*operation) {
1020 case kExpand: {
1021 timestamp_ = end_timestamp;
1022 return 0;
1023 }
1024 case kRfc3389CngNoPacket:
1025 case kCodecInternalCng: {
1026 return 0;
1027 }
1028 case kDtmf: {
1029 // TODO(hlundin): Write test for this.
1030 // Update timestamp.
1031 timestamp_ = end_timestamp;
1032 if (decision_logic_->generated_noise_samples() > 0 &&
1033 last_mode_ != kModeDtmf) {
1034 // Make a jump in timestamp due to the recently played comfort noise.
Peter Kastingb7e50542015-06-11 12:55:50 -07001035 uint32_t timestamp_jump =
1036 static_cast<uint32_t>(decision_logic_->generated_noise_samples());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001037 sync_buffer_->IncreaseEndTimestamp(timestamp_jump);
1038 timestamp_ += timestamp_jump;
1039 }
1040 decision_logic_->set_generated_noise_samples(0);
1041 return 0;
1042 }
Henrik Lundincf808d22015-05-27 14:33:29 +02001043 case kAccelerate:
1044 case kFastAccelerate: {
1045 // In order to do an accelerate we need at least 30 ms of audio data.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001046 if (samples_left >= samples_30_ms) {
1047 // Already have enough data, so we do not need to extract any more.
1048 decision_logic_->set_sample_memory(samples_left);
1049 decision_logic_->set_prev_time_scale(true);
1050 return 0;
1051 } else if (samples_left >= samples_10_ms &&
1052 decoder_frame_length_ >= samples_30_ms) {
1053 // Avoid decoding more data as it might overflow the playout buffer.
1054 *operation = kNormal;
1055 return 0;
1056 } else if (samples_left < samples_20_ms &&
1057 decoder_frame_length_ < samples_30_ms) {
1058 // Build up decoded data by decoding at least 20 ms of audio data. Do
1059 // not perform accelerate yet, but wait until we only need to do one
1060 // decoding.
1061 required_samples = 2 * output_size_samples_;
1062 *operation = kNormal;
1063 }
1064 // If none of the above is true, we have one of two possible situations:
1065 // (1) 20 ms <= samples_left < 30 ms and decoder_frame_length_ < 30 ms; or
1066 // (2) samples_left < 10 ms and decoder_frame_length_ >= 30 ms.
1067 // In either case, we move on with the accelerate decision, and decode one
1068 // frame now.
1069 break;
1070 }
1071 case kPreemptiveExpand: {
1072 // In order to do a preemptive expand we need at least 30 ms of decoded
1073 // audio data.
1074 if ((samples_left >= samples_30_ms) ||
1075 (samples_left >= samples_10_ms &&
1076 decoder_frame_length_ >= samples_30_ms)) {
1077 // Already have enough data, so we do not need to extract any more.
1078 // Or, avoid decoding more data as it might overflow the playout buffer.
1079 // Still try preemptive expand, though.
1080 decision_logic_->set_sample_memory(samples_left);
1081 decision_logic_->set_prev_time_scale(true);
1082 return 0;
1083 }
1084 if (samples_left < samples_20_ms &&
1085 decoder_frame_length_ < samples_30_ms) {
1086 // Build up decoded data by decoding at least 20 ms of audio data.
1087 // Still try to perform preemptive expand.
1088 required_samples = 2 * output_size_samples_;
1089 }
1090 // Move on with the preemptive expand decision.
1091 break;
1092 }
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001093 case kMerge: {
1094 required_samples =
1095 std::max(merge_->RequiredFutureSamples(), required_samples);
1096 break;
1097 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001098 default: {
1099 // Do nothing.
1100 }
1101 }
1102
1103 // Get packets from buffer.
1104 int extracted_samples = 0;
1105 if (header &&
1106 *operation != kAlternativePlc &&
1107 *operation != kAlternativePlcIncreaseTimestamp &&
1108 *operation != kAudioRepetition &&
1109 *operation != kAudioRepetitionIncreaseTimestamp) {
1110 sync_buffer_->IncreaseEndTimestamp(header->timestamp - end_timestamp);
1111 if (decision_logic_->CngOff()) {
1112 // Adjustment of timestamp only corresponds to an actual packet loss
1113 // if comfort noise is not played. If comfort noise was just played,
1114 // this adjustment of timestamp is only done to get back in sync with the
1115 // stream timestamp; no loss to report.
1116 stats_.LostSamples(header->timestamp - end_timestamp);
1117 }
1118
1119 if (*operation != kRfc3389Cng) {
1120 // We are about to decode and use a non-CNG packet.
1121 decision_logic_->SetCngOff();
1122 }
1123 // Reset CNG timestamp as a new packet will be delivered.
1124 // (Also if this is a CNG packet, since playedOutTS is updated.)
1125 decision_logic_->set_generated_noise_samples(0);
1126
1127 extracted_samples = ExtractPackets(required_samples, packet_list);
1128 if (extracted_samples < 0) {
1129 LOG_F(LS_WARNING) << "Failed to extract packets from buffer.";
1130 return kPacketBufferCorruption;
1131 }
1132 }
1133
Henrik Lundincf808d22015-05-27 14:33:29 +02001134 if (*operation == kAccelerate || *operation == kFastAccelerate ||
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001135 *operation == kPreemptiveExpand) {
1136 decision_logic_->set_sample_memory(samples_left + extracted_samples);
1137 decision_logic_->set_prev_time_scale(true);
1138 }
1139
Henrik Lundincf808d22015-05-27 14:33:29 +02001140 if (*operation == kAccelerate || *operation == kFastAccelerate) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001141 // Check that we have enough data (30ms) to do accelerate.
1142 if (extracted_samples + samples_left < samples_30_ms) {
1143 // TODO(hlundin): Write test for this.
1144 // Not enough, do normal operation instead.
1145 *operation = kNormal;
1146 }
1147 }
1148
1149 timestamp_ = end_timestamp;
1150 return 0;
1151}
1152
1153int NetEqImpl::Decode(PacketList* packet_list, Operations* operation,
1154 int* decoded_length,
1155 AudioDecoder::SpeechType* speech_type) {
1156 *speech_type = AudioDecoder::kSpeech;
1157 AudioDecoder* decoder = NULL;
1158 if (!packet_list->empty()) {
1159 const Packet* packet = packet_list->front();
pkasting@chromium.org0e81fdf2015-02-02 23:54:03 +00001160 uint8_t payload_type = packet->header.payloadType;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001161 if (!decoder_database_->IsComfortNoise(payload_type)) {
1162 decoder = decoder_database_->GetDecoder(payload_type);
1163 assert(decoder);
1164 if (!decoder) {
pkasting@chromium.org0e81fdf2015-02-02 23:54:03 +00001165 LOG_FERR1(LS_WARNING, GetDecoder, static_cast<int>(payload_type));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001166 PacketBuffer::DeleteAllPackets(packet_list);
1167 return kDecoderNotFound;
1168 }
1169 bool decoder_changed;
1170 decoder_database_->SetActiveDecoder(payload_type, &decoder_changed);
1171 if (decoder_changed) {
1172 // We have a new decoder. Re-init some values.
1173 const DecoderDatabase::DecoderInfo* decoder_info = decoder_database_
1174 ->GetDecoderInfo(payload_type);
1175 assert(decoder_info);
1176 if (!decoder_info) {
pkasting@chromium.org0e81fdf2015-02-02 23:54:03 +00001177 LOG_FERR1(LS_WARNING, GetDecoderInfo, static_cast<int>(payload_type));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001178 PacketBuffer::DeleteAllPackets(packet_list);
1179 return kDecoderNotFound;
1180 }
tina.legrand@webrtc.orgba5a6c32014-03-23 09:58:48 +00001181 // If sampling rate or number of channels has changed, we need to make
1182 // a reset.
turaj@webrtc.orga6101d72013-10-01 22:01:09 +00001183 if (decoder_info->fs_hz != fs_hz_ ||
henrik.lundin@webrtc.org6dba1eb2015-03-18 09:47:08 +00001184 decoder->Channels() != algorithm_buffer_->Channels()) {
tina.legrand@webrtc.orgba5a6c32014-03-23 09:58:48 +00001185 // TODO(tlegrand): Add unittest to cover this event.
henrik.lundin@webrtc.org6dba1eb2015-03-18 09:47:08 +00001186 SetSampleRateAndChannels(decoder_info->fs_hz, decoder->Channels());
turaj@webrtc.orga6101d72013-10-01 22:01:09 +00001187 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001188 sync_buffer_->set_end_timestamp(timestamp_);
1189 playout_timestamp_ = timestamp_;
1190 }
1191 }
1192 }
1193
1194 if (reset_decoder_) {
1195 // TODO(hlundin): Write test for this.
1196 // Reset decoder.
1197 if (decoder) {
1198 decoder->Init();
1199 }
1200 // Reset comfort noise decoder.
1201 AudioDecoder* cng_decoder = decoder_database_->GetActiveCngDecoder();
1202 if (cng_decoder) {
1203 cng_decoder->Init();
1204 }
1205 reset_decoder_ = false;
1206 }
1207
1208#ifdef LEGACY_BITEXACT
1209 // Due to a bug in old SignalMCU, it could happen that CNG operation was
1210 // decided, but a speech packet was provided. The speech packet will be used
1211 // to update the comfort noise decoder, as if it was a SID frame, which is
1212 // clearly wrong.
1213 if (*operation == kRfc3389Cng) {
1214 return 0;
1215 }
1216#endif
1217
1218 *decoded_length = 0;
1219 // Update codec-internal PLC state.
1220 if ((*operation == kMerge) && decoder && decoder->HasDecodePlc()) {
1221 decoder->DecodePlc(1, &decoded_buffer_[*decoded_length]);
1222 }
1223
1224 int return_value = DecodeLoop(packet_list, operation, decoder,
1225 decoded_length, speech_type);
1226
1227 if (*decoded_length < 0) {
1228 // Error returned from the decoder.
1229 *decoded_length = 0;
Peter Kastingb7e50542015-06-11 12:55:50 -07001230 sync_buffer_->IncreaseEndTimestamp(
1231 static_cast<uint32_t>(decoder_frame_length_));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001232 int error_code = 0;
1233 if (decoder)
1234 error_code = decoder->ErrorCode();
1235 if (error_code != 0) {
1236 // Got some error code from the decoder.
1237 decoder_error_code_ = error_code;
1238 return_value = kDecoderErrorCode;
1239 } else {
1240 // Decoder does not implement error codes. Return generic error.
1241 return_value = kOtherDecoderError;
1242 }
1243 LOG_FERR2(LS_WARNING, DecodeLoop, error_code, packet_list->size());
1244 *operation = kExpand; // Do expansion to get data instead.
1245 }
1246 if (*speech_type != AudioDecoder::kComfortNoise) {
1247 // Don't increment timestamp if codec returned CNG speech type
1248 // since in this case, the we will increment the CNGplayedTS counter.
1249 // Increase with number of samples per channel.
1250 assert(*decoded_length == 0 ||
henrik.lundin@webrtc.org6dba1eb2015-03-18 09:47:08 +00001251 (decoder && decoder->Channels() == sync_buffer_->Channels()));
turaj@webrtc.org362a55e2013-09-20 16:25:28 +00001252 sync_buffer_->IncreaseEndTimestamp(
1253 *decoded_length / static_cast<int>(sync_buffer_->Channels()));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001254 }
1255 return return_value;
1256}
1257
1258int NetEqImpl::DecodeLoop(PacketList* packet_list, Operations* operation,
1259 AudioDecoder* decoder, int* decoded_length,
1260 AudioDecoder::SpeechType* speech_type) {
1261 Packet* packet = NULL;
1262 if (!packet_list->empty()) {
1263 packet = packet_list->front();
1264 }
1265 // Do decoding.
1266 while (packet &&
1267 !decoder_database_->IsComfortNoise(packet->header.payloadType)) {
1268 assert(decoder); // At this point, we must have a decoder object.
1269 // The number of channels in the |sync_buffer_| should be the same as the
1270 // number decoder channels.
henrik.lundin@webrtc.org6dba1eb2015-03-18 09:47:08 +00001271 assert(sync_buffer_->Channels() == decoder->Channels());
1272 assert(decoded_buffer_length_ >= kMaxFrameSize * decoder->Channels());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001273 assert(*operation == kNormal || *operation == kAccelerate ||
Henrik Lundincf808d22015-05-27 14:33:29 +02001274 *operation == kFastAccelerate || *operation == kMerge ||
1275 *operation == kPreemptiveExpand);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001276 packet_list->pop_front();
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001277 size_t payload_length = packet->payload_length;
Peter Kastingcb180972015-06-11 12:42:27 -07001278 int16_t decode_length;
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001279 if (packet->sync_packet) {
1280 // Decode to silence with the same frame size as the last decode.
1281 LOG(LS_VERBOSE) << "Decoding sync-packet: " <<
1282 " ts=" << packet->header.timestamp <<
1283 ", sn=" << packet->header.sequenceNumber <<
1284 ", pt=" << static_cast<int>(packet->header.payloadType) <<
1285 ", ssrc=" << packet->header.ssrc <<
1286 ", len=" << packet->payload_length;
henrik.lundin@webrtc.org6dba1eb2015-03-18 09:47:08 +00001287 memset(&decoded_buffer_[*decoded_length], 0,
1288 decoder_frame_length_ * decoder->Channels() *
1289 sizeof(decoded_buffer_[0]));
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001290 decode_length = decoder_frame_length_;
1291 } else if (!packet->primary) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001292 // This is a redundant payload; call the special decoder method.
turaj@webrtc.org0c0fae82013-09-25 17:42:17 +00001293 LOG(LS_VERBOSE) << "Decoding packet (redundant):" <<
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001294 " ts=" << packet->header.timestamp <<
1295 ", sn=" << packet->header.sequenceNumber <<
1296 ", pt=" << static_cast<int>(packet->header.payloadType) <<
1297 ", ssrc=" << packet->header.ssrc <<
1298 ", len=" << packet->payload_length;
1299 decode_length = decoder->DecodeRedundant(
henrik.lundin@webrtc.org1eda4e32015-02-25 10:02:29 +00001300 packet->payload, packet->payload_length, fs_hz_,
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +00001301 (decoded_buffer_length_ - *decoded_length) * sizeof(int16_t),
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001302 &decoded_buffer_[*decoded_length], speech_type);
1303 } else {
turaj@webrtc.org0c0fae82013-09-25 17:42:17 +00001304 LOG(LS_VERBOSE) << "Decoding packet: ts=" << packet->header.timestamp <<
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001305 ", sn=" << packet->header.sequenceNumber <<
1306 ", pt=" << static_cast<int>(packet->header.payloadType) <<
1307 ", ssrc=" << packet->header.ssrc <<
1308 ", len=" << packet->payload_length;
henrik.lundin@webrtc.org1eda4e32015-02-25 10:02:29 +00001309 decode_length =
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +00001310 decoder->Decode(
1311 packet->payload, packet->payload_length, fs_hz_,
1312 (decoded_buffer_length_ - *decoded_length) * sizeof(int16_t),
1313 &decoded_buffer_[*decoded_length], speech_type);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001314 }
1315
1316 delete[] packet->payload;
1317 delete packet;
turaj@webrtc.org58cd3162013-10-31 15:15:55 +00001318 packet = NULL;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001319 if (decode_length > 0) {
1320 *decoded_length += decode_length;
1321 // Update |decoder_frame_length_| with number of samples per channel.
henrik.lundin@webrtc.org6dba1eb2015-03-18 09:47:08 +00001322 decoder_frame_length_ =
1323 decode_length / static_cast<int>(decoder->Channels());
1324 LOG(LS_VERBOSE) << "Decoded " << decode_length << " samples ("
1325 << decoder->Channels() << " channel(s) -> "
1326 << decoder_frame_length_ << " samples per channel)";
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001327 } else if (decode_length < 0) {
1328 // Error.
henrik.lundin@webrtc.org63464a92013-01-30 09:41:56 +00001329 LOG_FERR2(LS_WARNING, Decode, decode_length, payload_length);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001330 *decoded_length = -1;
1331 PacketBuffer::DeleteAllPackets(packet_list);
1332 break;
1333 }
1334 if (*decoded_length > static_cast<int>(decoded_buffer_length_)) {
1335 // Guard against overflow.
1336 LOG_F(LS_WARNING) << "Decoded too much.";
1337 PacketBuffer::DeleteAllPackets(packet_list);
1338 return kDecodedTooMuch;
1339 }
1340 if (!packet_list->empty()) {
1341 packet = packet_list->front();
1342 } else {
1343 packet = NULL;
1344 }
1345 } // End of decode loop.
1346
turaj@webrtc.org58cd3162013-10-31 15:15:55 +00001347 // If the list is not empty at this point, either a decoding error terminated
1348 // the while-loop, or list must hold exactly one CNG packet.
1349 assert(packet_list->empty() || *decoded_length < 0 ||
1350 (packet_list->size() == 1 && packet &&
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001351 decoder_database_->IsComfortNoise(packet->header.payloadType)));
1352 return 0;
1353}
1354
1355void NetEqImpl::DoNormal(const int16_t* decoded_buffer, size_t decoded_length,
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001356 AudioDecoder::SpeechType speech_type, bool play_dtmf) {
henrik.lundin@webrtc.org40d3fc62013-09-18 12:19:50 +00001357 assert(normal_.get());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001358 assert(mute_factor_array_.get());
henrik.lundin@webrtc.org40d3fc62013-09-18 12:19:50 +00001359 normal_->Process(decoded_buffer, decoded_length, last_mode_,
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +00001360 mute_factor_array_.get(), algorithm_buffer_.get());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001361 if (decoded_length != 0) {
1362 last_mode_ = kModeNormal;
1363 }
1364
1365 // If last packet was decoded as an inband CNG, set mode to CNG instead.
1366 if ((speech_type == AudioDecoder::kComfortNoise)
1367 || ((last_mode_ == kModeCodecInternalCng)
1368 && (decoded_length == 0))) {
1369 // TODO(hlundin): Remove second part of || statement above.
1370 last_mode_ = kModeCodecInternalCng;
1371 }
1372
1373 if (!play_dtmf) {
1374 dtmf_tone_generator_->Reset();
1375 }
1376}
1377
1378void NetEqImpl::DoMerge(int16_t* decoded_buffer, size_t decoded_length,
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001379 AudioDecoder::SpeechType speech_type, bool play_dtmf) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001380 assert(mute_factor_array_.get());
henrik.lundin@webrtc.org40d3fc62013-09-18 12:19:50 +00001381 assert(merge_.get());
1382 int new_length = merge_->Process(decoded_buffer, decoded_length,
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +00001383 mute_factor_array_.get(),
1384 algorithm_buffer_.get());
minyue@webrtc.orgc11348b2015-02-10 08:35:38 +00001385 int expand_length_correction = new_length -
1386 static_cast<int>(decoded_length / algorithm_buffer_->Channels());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001387
1388 // Update in-call and post-call statistics.
1389 if (expand_->MuteFactor(0) == 0) {
1390 // Expand generates only noise.
minyue@webrtc.orgc11348b2015-02-10 08:35:38 +00001391 stats_.ExpandedNoiseSamples(expand_length_correction);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001392 } else {
1393 // Expansion generates more than only noise.
minyue@webrtc.orgc11348b2015-02-10 08:35:38 +00001394 stats_.ExpandedVoiceSamples(expand_length_correction);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001395 }
1396
1397 last_mode_ = kModeMerge;
1398 // If last packet was decoded as an inband CNG, set mode to CNG instead.
1399 if (speech_type == AudioDecoder::kComfortNoise) {
1400 last_mode_ = kModeCodecInternalCng;
1401 }
1402 expand_->Reset();
1403 if (!play_dtmf) {
1404 dtmf_tone_generator_->Reset();
1405 }
1406}
1407
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001408int NetEqImpl::DoExpand(bool play_dtmf) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001409 while ((sync_buffer_->FutureLength() - expand_->overlap_length()) <
1410 static_cast<size_t>(output_size_samples_)) {
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001411 algorithm_buffer_->Clear();
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +00001412 int return_value = expand_->Process(algorithm_buffer_.get());
turaj@webrtc.org362a55e2013-09-20 16:25:28 +00001413 int length = static_cast<int>(algorithm_buffer_->Size());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001414
1415 // Update in-call and post-call statistics.
1416 if (expand_->MuteFactor(0) == 0) {
1417 // Expand operation generates only noise.
1418 stats_.ExpandedNoiseSamples(length);
1419 } else {
1420 // Expand operation generates more than only noise.
1421 stats_.ExpandedVoiceSamples(length);
1422 }
1423
1424 last_mode_ = kModeExpand;
1425
1426 if (return_value < 0) {
1427 return return_value;
1428 }
1429
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001430 sync_buffer_->PushBack(*algorithm_buffer_);
1431 algorithm_buffer_->Clear();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001432 }
1433 if (!play_dtmf) {
1434 dtmf_tone_generator_->Reset();
1435 }
1436 return 0;
1437}
1438
Henrik Lundincf808d22015-05-27 14:33:29 +02001439int NetEqImpl::DoAccelerate(int16_t* decoded_buffer,
1440 size_t decoded_length,
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001441 AudioDecoder::SpeechType speech_type,
Henrik Lundincf808d22015-05-27 14:33:29 +02001442 bool play_dtmf,
1443 bool fast_accelerate) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001444 const size_t required_samples = 240 * fs_mult_; // Must have 30 ms.
turaj@webrtc.org362a55e2013-09-20 16:25:28 +00001445 size_t borrowed_samples_per_channel = 0;
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001446 size_t num_channels = algorithm_buffer_->Channels();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001447 size_t decoded_length_per_channel = decoded_length / num_channels;
1448 if (decoded_length_per_channel < required_samples) {
1449 // Must move data from the |sync_buffer_| in order to get 30 ms.
turaj@webrtc.org362a55e2013-09-20 16:25:28 +00001450 borrowed_samples_per_channel = static_cast<int>(required_samples -
1451 decoded_length_per_channel);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001452 memmove(&decoded_buffer[borrowed_samples_per_channel * num_channels],
1453 decoded_buffer,
1454 sizeof(int16_t) * decoded_length);
1455 sync_buffer_->ReadInterleavedFromEnd(borrowed_samples_per_channel,
1456 decoded_buffer);
1457 decoded_length = required_samples * num_channels;
1458 }
1459
1460 int16_t samples_removed;
Henrik Lundincf808d22015-05-27 14:33:29 +02001461 Accelerate::ReturnCodes return_code =
1462 accelerate_->Process(decoded_buffer, decoded_length, fast_accelerate,
1463 algorithm_buffer_.get(), &samples_removed);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001464 stats_.AcceleratedSamples(samples_removed);
1465 switch (return_code) {
1466 case Accelerate::kSuccess:
1467 last_mode_ = kModeAccelerateSuccess;
1468 break;
1469 case Accelerate::kSuccessLowEnergy:
1470 last_mode_ = kModeAccelerateLowEnergy;
1471 break;
1472 case Accelerate::kNoStretch:
1473 last_mode_ = kModeAccelerateFail;
1474 break;
1475 case Accelerate::kError:
1476 // TODO(hlundin): Map to kModeError instead?
1477 last_mode_ = kModeAccelerateFail;
1478 return kAccelerateError;
1479 }
1480
1481 if (borrowed_samples_per_channel > 0) {
1482 // Copy borrowed samples back to the |sync_buffer_|.
turaj@webrtc.org362a55e2013-09-20 16:25:28 +00001483 size_t length = algorithm_buffer_->Size();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001484 if (length < borrowed_samples_per_channel) {
1485 // This destroys the beginning of the buffer, but will not cause any
1486 // problems.
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001487 sync_buffer_->ReplaceAtIndex(*algorithm_buffer_,
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001488 sync_buffer_->Size() -
1489 borrowed_samples_per_channel);
1490 sync_buffer_->PushFrontZeros(borrowed_samples_per_channel - length);
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001491 algorithm_buffer_->PopFront(length);
1492 assert(algorithm_buffer_->Empty());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001493 } else {
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001494 sync_buffer_->ReplaceAtIndex(*algorithm_buffer_,
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001495 borrowed_samples_per_channel,
1496 sync_buffer_->Size() -
1497 borrowed_samples_per_channel);
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001498 algorithm_buffer_->PopFront(borrowed_samples_per_channel);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001499 }
1500 }
1501
1502 // If last packet was decoded as an inband CNG, set mode to CNG instead.
1503 if (speech_type == AudioDecoder::kComfortNoise) {
1504 last_mode_ = kModeCodecInternalCng;
1505 }
1506 if (!play_dtmf) {
1507 dtmf_tone_generator_->Reset();
1508 }
1509 expand_->Reset();
1510 return 0;
1511}
1512
1513int NetEqImpl::DoPreemptiveExpand(int16_t* decoded_buffer,
1514 size_t decoded_length,
1515 AudioDecoder::SpeechType speech_type,
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001516 bool play_dtmf) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001517 const size_t required_samples = 240 * fs_mult_; // Must have 30 ms.
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001518 size_t num_channels = algorithm_buffer_->Channels();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001519 int borrowed_samples_per_channel = 0;
1520 int old_borrowed_samples_per_channel = 0;
1521 size_t decoded_length_per_channel = decoded_length / num_channels;
1522 if (decoded_length_per_channel < required_samples) {
1523 // Must move data from the |sync_buffer_| in order to get 30 ms.
turaj@webrtc.org362a55e2013-09-20 16:25:28 +00001524 borrowed_samples_per_channel = static_cast<int>(required_samples -
1525 decoded_length_per_channel);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001526 // Calculate how many of these were already played out.
Peter Kastingf045e4d2015-06-10 21:15:38 -07001527 const int future_length = static_cast<int>(sync_buffer_->FutureLength());
1528 old_borrowed_samples_per_channel =
1529 (borrowed_samples_per_channel > future_length) ?
1530 (borrowed_samples_per_channel - future_length) : 0;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001531 memmove(&decoded_buffer[borrowed_samples_per_channel * num_channels],
1532 decoded_buffer,
1533 sizeof(int16_t) * decoded_length);
1534 sync_buffer_->ReadInterleavedFromEnd(borrowed_samples_per_channel,
1535 decoded_buffer);
1536 decoded_length = required_samples * num_channels;
1537 }
1538
1539 int16_t samples_added;
henrik.lundin@webrtc.org40d3fc62013-09-18 12:19:50 +00001540 PreemptiveExpand::ReturnCodes return_code = preemptive_expand_->Process(
turaj@webrtc.org362a55e2013-09-20 16:25:28 +00001541 decoded_buffer, static_cast<int>(decoded_length),
1542 old_borrowed_samples_per_channel,
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +00001543 algorithm_buffer_.get(), &samples_added);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001544 stats_.PreemptiveExpandedSamples(samples_added);
1545 switch (return_code) {
1546 case PreemptiveExpand::kSuccess:
1547 last_mode_ = kModePreemptiveExpandSuccess;
1548 break;
1549 case PreemptiveExpand::kSuccessLowEnergy:
1550 last_mode_ = kModePreemptiveExpandLowEnergy;
1551 break;
1552 case PreemptiveExpand::kNoStretch:
1553 last_mode_ = kModePreemptiveExpandFail;
1554 break;
1555 case PreemptiveExpand::kError:
1556 // TODO(hlundin): Map to kModeError instead?
1557 last_mode_ = kModePreemptiveExpandFail;
1558 return kPreemptiveExpandError;
1559 }
1560
1561 if (borrowed_samples_per_channel > 0) {
1562 // Copy borrowed samples back to the |sync_buffer_|.
1563 sync_buffer_->ReplaceAtIndex(
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001564 *algorithm_buffer_, borrowed_samples_per_channel,
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001565 sync_buffer_->Size() - borrowed_samples_per_channel);
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001566 algorithm_buffer_->PopFront(borrowed_samples_per_channel);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001567 }
1568
1569 // If last packet was decoded as an inband CNG, set mode to CNG instead.
1570 if (speech_type == AudioDecoder::kComfortNoise) {
1571 last_mode_ = kModeCodecInternalCng;
1572 }
1573 if (!play_dtmf) {
1574 dtmf_tone_generator_->Reset();
1575 }
1576 expand_->Reset();
1577 return 0;
1578}
1579
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001580int NetEqImpl::DoRfc3389Cng(PacketList* packet_list, bool play_dtmf) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001581 if (!packet_list->empty()) {
1582 // Must have exactly one SID frame at this point.
1583 assert(packet_list->size() == 1);
1584 Packet* packet = packet_list->front();
1585 packet_list->pop_front();
henrik.lundin@webrtc.org73deaad2013-01-31 13:32:51 +00001586 if (!decoder_database_->IsComfortNoise(packet->header.payloadType)) {
1587#ifdef LEGACY_BITEXACT
1588 // This can happen due to a bug in GetDecision. Change the payload type
1589 // to a CNG type, and move on. Note that this means that we are in fact
1590 // sending a non-CNG payload to the comfort noise decoder for decoding.
1591 // Clearly wrong, but will maintain bit-exactness with legacy.
1592 if (fs_hz_ == 8000) {
1593 packet->header.payloadType =
1594 decoder_database_->GetRtpPayloadType(kDecoderCNGnb);
1595 } else if (fs_hz_ == 16000) {
1596 packet->header.payloadType =
1597 decoder_database_->GetRtpPayloadType(kDecoderCNGwb);
1598 } else if (fs_hz_ == 32000) {
1599 packet->header.payloadType =
1600 decoder_database_->GetRtpPayloadType(kDecoderCNGswb32kHz);
1601 } else if (fs_hz_ == 48000) {
1602 packet->header.payloadType =
1603 decoder_database_->GetRtpPayloadType(kDecoderCNGswb48kHz);
1604 }
1605 assert(decoder_database_->IsComfortNoise(packet->header.payloadType));
1606#else
1607 LOG(LS_ERROR) << "Trying to decode non-CNG payload as CNG.";
1608 return kOtherError;
1609#endif
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001610 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001611 // UpdateParameters() deletes |packet|.
1612 if (comfort_noise_->UpdateParameters(packet) ==
1613 ComfortNoise::kInternalError) {
1614 LOG_FERR0(LS_WARNING, UpdateParameters);
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001615 algorithm_buffer_->Zeros(output_size_samples_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001616 return -comfort_noise_->internal_error_code();
1617 }
1618 }
1619 int cn_return = comfort_noise_->Generate(output_size_samples_,
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +00001620 algorithm_buffer_.get());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001621 expand_->Reset();
1622 last_mode_ = kModeRfc3389Cng;
1623 if (!play_dtmf) {
1624 dtmf_tone_generator_->Reset();
1625 }
1626 if (cn_return == ComfortNoise::kInternalError) {
1627 LOG_FERR1(LS_WARNING, comfort_noise_->Generate, cn_return);
1628 decoder_error_code_ = comfort_noise_->internal_error_code();
1629 return kComfortNoiseErrorCode;
1630 } else if (cn_return == ComfortNoise::kUnknownPayloadType) {
1631 LOG_FERR1(LS_WARNING, comfort_noise_->Generate, cn_return);
1632 return kUnknownRtpPayloadType;
1633 }
1634 return 0;
1635}
1636
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001637void NetEqImpl::DoCodecInternalCng() {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001638 int length = 0;
1639 // TODO(hlundin): Will probably need a longer buffer for multi-channel.
1640 int16_t decoded_buffer[kMaxFrameSize];
1641 AudioDecoder* decoder = decoder_database_->GetActiveDecoder();
1642 if (decoder) {
1643 const uint8_t* dummy_payload = NULL;
1644 AudioDecoder::SpeechType speech_type;
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +00001645 length = decoder->Decode(
1646 dummy_payload, 0, fs_hz_, kMaxFrameSize * sizeof(int16_t),
1647 decoded_buffer, &speech_type);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001648 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001649 assert(mute_factor_array_.get());
henrik.lundin@webrtc.org40d3fc62013-09-18 12:19:50 +00001650 normal_->Process(decoded_buffer, length, last_mode_, mute_factor_array_.get(),
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +00001651 algorithm_buffer_.get());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001652 last_mode_ = kModeCodecInternalCng;
1653 expand_->Reset();
1654}
1655
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001656int NetEqImpl::DoDtmf(const DtmfEvent& dtmf_event, bool* play_dtmf) {
turaj@webrtc.org4d06db52013-03-27 18:31:42 +00001657 // This block of the code and the block further down, handling |dtmf_switch|
1658 // are commented out. Otherwise playing out-of-band DTMF would fail in VoE
1659 // test, DtmfTest.ManualSuccessfullySendsOutOfBandTelephoneEvents. This is
1660 // equivalent to |dtmf_switch| always be false.
1661 //
1662 // See http://webrtc-codereview.appspot.com/1195004/ for discussion
1663 // On this issue. This change might cause some glitches at the point of
1664 // switch from audio to DTMF. Issue 1545 is filed to track this.
1665 //
1666 // bool dtmf_switch = false;
1667 // if ((last_mode_ != kModeDtmf) && dtmf_tone_generator_->initialized()) {
1668 // // Special case; see below.
1669 // // We must catch this before calling Generate, since |initialized| is
1670 // // modified in that call.
1671 // dtmf_switch = true;
1672 // }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001673
1674 int dtmf_return_value = 0;
1675 if (!dtmf_tone_generator_->initialized()) {
1676 // Initialize if not already done.
1677 dtmf_return_value = dtmf_tone_generator_->Init(fs_hz_, dtmf_event.event_no,
1678 dtmf_event.volume);
1679 }
turaj@webrtc.org4d06db52013-03-27 18:31:42 +00001680
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001681 if (dtmf_return_value == 0) {
1682 // Generate DTMF signal.
1683 dtmf_return_value = dtmf_tone_generator_->Generate(output_size_samples_,
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +00001684 algorithm_buffer_.get());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001685 }
turaj@webrtc.org4d06db52013-03-27 18:31:42 +00001686
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001687 if (dtmf_return_value < 0) {
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001688 algorithm_buffer_->Zeros(output_size_samples_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001689 return dtmf_return_value;
1690 }
1691
turaj@webrtc.org4d06db52013-03-27 18:31:42 +00001692 // if (dtmf_switch) {
1693 // // This is the special case where the previous operation was DTMF
1694 // // overdub, but the current instruction is "regular" DTMF. We must make
1695 // // sure that the DTMF does not have any discontinuities. The first DTMF
1696 // // sample that we generate now must be played out immediately, therefore
1697 // // it must be copied to the speech buffer.
1698 // // TODO(hlundin): This code seems incorrect. (Legacy.) Write test and
1699 // // verify correct operation.
1700 // assert(false);
1701 // // Must generate enough data to replace all of the |sync_buffer_|
1702 // // "future".
1703 // int required_length = sync_buffer_->FutureLength();
1704 // assert(dtmf_tone_generator_->initialized());
1705 // dtmf_return_value = dtmf_tone_generator_->Generate(required_length,
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001706 // algorithm_buffer_);
1707 // assert((size_t) required_length == algorithm_buffer_->Size());
turaj@webrtc.org4d06db52013-03-27 18:31:42 +00001708 // if (dtmf_return_value < 0) {
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001709 // algorithm_buffer_->Zeros(output_size_samples_);
turaj@webrtc.org4d06db52013-03-27 18:31:42 +00001710 // return dtmf_return_value;
1711 // }
1712 //
1713 // // Overwrite the "future" part of the speech buffer with the new DTMF
1714 // // data.
1715 // // TODO(hlundin): It seems that this overwriting has gone lost.
1716 // // Not adapted for multi-channel yet.
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001717 // assert(algorithm_buffer_->Channels() == 1);
1718 // if (algorithm_buffer_->Channels() != 1) {
turaj@webrtc.org4d06db52013-03-27 18:31:42 +00001719 // LOG(LS_WARNING) << "DTMF not supported for more than one channel";
1720 // return kStereoNotSupported;
1721 // }
1722 // // Shuffle the remaining data to the beginning of algorithm buffer.
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001723 // algorithm_buffer_->PopFront(sync_buffer_->FutureLength());
turaj@webrtc.org4d06db52013-03-27 18:31:42 +00001724 // }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001725
Peter Kastingb7e50542015-06-11 12:55:50 -07001726 sync_buffer_->IncreaseEndTimestamp(
1727 static_cast<uint32_t>(output_size_samples_));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001728 expand_->Reset();
1729 last_mode_ = kModeDtmf;
1730
1731 // Set to false because the DTMF is already in the algorithm buffer.
1732 *play_dtmf = false;
1733 return 0;
1734}
1735
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001736void NetEqImpl::DoAlternativePlc(bool increase_timestamp) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001737 AudioDecoder* decoder = decoder_database_->GetActiveDecoder();
1738 int length;
1739 if (decoder && decoder->HasDecodePlc()) {
1740 // Use the decoder's packet-loss concealment.
1741 // TODO(hlundin): Will probably need a longer buffer for multi-channel.
1742 int16_t decoded_buffer[kMaxFrameSize];
1743 length = decoder->DecodePlc(1, decoded_buffer);
1744 if (length > 0) {
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001745 algorithm_buffer_->PushBackInterleaved(decoded_buffer, length);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001746 } else {
1747 length = 0;
1748 }
1749 } else {
1750 // Do simple zero-stuffing.
1751 length = output_size_samples_;
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001752 algorithm_buffer_->Zeros(length);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001753 // By not advancing the timestamp, NetEq inserts samples.
1754 stats_.AddZeros(length);
1755 }
1756 if (increase_timestamp) {
Peter Kastingb7e50542015-06-11 12:55:50 -07001757 sync_buffer_->IncreaseEndTimestamp(static_cast<uint32_t>(length));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001758 }
1759 expand_->Reset();
1760}
1761
1762int NetEqImpl::DtmfOverdub(const DtmfEvent& dtmf_event, size_t num_channels,
1763 int16_t* output) const {
1764 size_t out_index = 0;
1765 int overdub_length = output_size_samples_; // Default value.
1766
1767 if (sync_buffer_->dtmf_index() > sync_buffer_->next_index()) {
1768 // Special operation for transition from "DTMF only" to "DTMF overdub".
1769 out_index = std::min(
1770 sync_buffer_->dtmf_index() - sync_buffer_->next_index(),
1771 static_cast<size_t>(output_size_samples_));
turaj@webrtc.org362a55e2013-09-20 16:25:28 +00001772 overdub_length = output_size_samples_ - static_cast<int>(out_index);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001773 }
1774
henrik.lundin@webrtc.orgfd11bbf2013-09-30 20:38:44 +00001775 AudioMultiVector dtmf_output(num_channels);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001776 int dtmf_return_value = 0;
1777 if (!dtmf_tone_generator_->initialized()) {
1778 dtmf_return_value = dtmf_tone_generator_->Init(fs_hz_, dtmf_event.event_no,
1779 dtmf_event.volume);
1780 }
1781 if (dtmf_return_value == 0) {
1782 dtmf_return_value = dtmf_tone_generator_->Generate(overdub_length,
1783 &dtmf_output);
1784 assert((size_t) overdub_length == dtmf_output.Size());
1785 }
1786 dtmf_output.ReadInterleaved(overdub_length, &output[out_index]);
1787 return dtmf_return_value < 0 ? dtmf_return_value : 0;
1788}
1789
1790int NetEqImpl::ExtractPackets(int required_samples, PacketList* packet_list) {
1791 bool first_packet = true;
1792 uint8_t prev_payload_type = 0;
1793 uint32_t prev_timestamp = 0;
1794 uint16_t prev_sequence_number = 0;
1795 bool next_packet_available = false;
1796
henrik.lundin@webrtc.orge1d468c2013-01-30 07:37:20 +00001797 const RTPHeader* header = packet_buffer_->NextRtpHeader();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001798 assert(header);
1799 if (!header) {
1800 return -1;
1801 }
turaj@webrtc.org7df97062013-08-02 18:07:13 +00001802 uint32_t first_timestamp = header->timestamp;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001803 int extracted_samples = 0;
1804
1805 // Packet extraction loop.
1806 do {
1807 timestamp_ = header->timestamp;
1808 int discard_count = 0;
henrik.lundin@webrtc.orge1d468c2013-01-30 07:37:20 +00001809 Packet* packet = packet_buffer_->GetNextPacket(&discard_count);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001810 // |header| may be invalid after the |packet_buffer_| operation.
1811 header = NULL;
1812 if (!packet) {
1813 LOG_FERR1(LS_ERROR, GetNextPacket, discard_count) <<
1814 "Should always be able to extract a packet here";
1815 assert(false); // Should always be able to extract a packet here.
1816 return -1;
1817 }
1818 stats_.PacketsDiscarded(discard_count);
1819 // Store waiting time in ms; packets->waiting_time is in "output blocks".
1820 stats_.StoreWaitingTime(packet->waiting_time * kOutputSizeMs);
1821 assert(packet->payload_length > 0);
1822 packet_list->push_back(packet); // Store packet in list.
1823
1824 if (first_packet) {
1825 first_packet = false;
minyue@webrtc.orgd7301772013-08-29 00:58:14 +00001826 decoded_packet_sequence_number_ = prev_sequence_number =
1827 packet->header.sequenceNumber;
1828 decoded_packet_timestamp_ = prev_timestamp = packet->header.timestamp;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001829 prev_payload_type = packet->header.payloadType;
1830 }
1831
1832 // Store number of extracted samples.
1833 int packet_duration = 0;
1834 AudioDecoder* decoder = decoder_database_->GetDecoder(
1835 packet->header.payloadType);
1836 if (decoder) {
minyue@webrtc.orgb28bfa72014-03-21 12:07:40 +00001837 if (packet->sync_packet) {
1838 packet_duration = decoder_frame_length_;
1839 } else {
minyue@webrtc.org2c1bcf22015-02-17 10:17:09 +00001840 if (packet->primary) {
1841 packet_duration = decoder->PacketDuration(packet->payload,
1842 packet->payload_length);
1843 } else {
1844 packet_duration = decoder->
1845 PacketDurationRedundant(packet->payload, packet->payload_length);
1846 stats_.SecondaryDecodedSamples(packet_duration);
1847 }
minyue@webrtc.orgb28bfa72014-03-21 12:07:40 +00001848 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001849 } else {
pkasting@chromium.org026b8922015-01-30 19:53:42 +00001850 LOG_FERR1(LS_WARNING, GetDecoder,
1851 static_cast<int>(packet->header.payloadType))
1852 << "Could not find a decoder for a packet about to be extracted.";
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001853 assert(false);
1854 }
1855 if (packet_duration <= 0) {
1856 // Decoder did not return a packet duration. Assume that the packet
1857 // contains the same number of samples as the previous one.
1858 packet_duration = decoder_frame_length_;
1859 }
1860 extracted_samples = packet->header.timestamp - first_timestamp +
1861 packet_duration;
1862
1863 // Check what packet is available next.
1864 header = packet_buffer_->NextRtpHeader();
1865 next_packet_available = false;
1866 if (header && prev_payload_type == header->payloadType) {
1867 int16_t seq_no_diff = header->sequenceNumber - prev_sequence_number;
1868 int32_t ts_diff = header->timestamp - prev_timestamp;
1869 if (seq_no_diff == 1 ||
1870 (seq_no_diff == 0 && ts_diff == decoder_frame_length_)) {
1871 // The next sequence number is available, or the next part of a packet
1872 // that was split into pieces upon insertion.
1873 next_packet_available = true;
1874 }
1875 prev_sequence_number = header->sequenceNumber;
1876 }
1877 } while (extracted_samples < required_samples && next_packet_available);
1878
henrik.lundin@webrtc.org61217152014-09-22 08:30:07 +00001879 if (extracted_samples > 0) {
1880 // Delete old packets only when we are going to decode something. Otherwise,
1881 // we could end up in the situation where we never decode anything, since
1882 // all incoming packets are considered too old but the buffer will also
1883 // never be flooded and flushed.
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +00001884 packet_buffer_->DiscardAllOldPackets(timestamp_);
henrik.lundin@webrtc.org61217152014-09-22 08:30:07 +00001885 }
1886
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001887 return extracted_samples;
1888}
1889
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001890void NetEqImpl::UpdatePlcComponents(int fs_hz, size_t channels) {
1891 // Delete objects and create new ones.
1892 expand_.reset(expand_factory_->Create(background_noise_.get(),
1893 sync_buffer_.get(), &random_vector_,
1894 fs_hz, channels));
1895 merge_.reset(new Merge(fs_hz, channels, expand_.get(), sync_buffer_.get()));
1896}
1897
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001898void NetEqImpl::SetSampleRateAndChannels(int fs_hz, size_t channels) {
1899 LOG_API2(fs_hz, channels);
1900 // TODO(hlundin): Change to an enumerator and skip assert.
1901 assert(fs_hz == 8000 || fs_hz == 16000 || fs_hz == 32000 || fs_hz == 48000);
1902 assert(channels > 0);
1903
1904 fs_hz_ = fs_hz;
1905 fs_mult_ = fs_hz / 8000;
1906 output_size_samples_ = kOutputSizeMs * 8 * fs_mult_;
1907 decoder_frame_length_ = 3 * output_size_samples_; // Initialize to 30ms.
1908
1909 last_mode_ = kModeNormal;
1910
1911 // Create a new array of mute factors and set all to 1.
1912 mute_factor_array_.reset(new int16_t[channels]);
1913 for (size_t i = 0; i < channels; ++i) {
1914 mute_factor_array_[i] = 16384; // 1.0 in Q14.
1915 }
1916
1917 // Reset comfort noise decoder, if there is one active.
1918 AudioDecoder* cng_decoder = decoder_database_->GetActiveCngDecoder();
1919 if (cng_decoder) {
1920 cng_decoder->Init();
1921 }
1922
1923 // Reinit post-decode VAD with new sample rate.
1924 assert(vad_.get()); // Cannot be NULL here.
1925 vad_->Init();
1926
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001927 // Delete algorithm buffer and create a new one.
henrik.lundin@webrtc.orgfd11bbf2013-09-30 20:38:44 +00001928 algorithm_buffer_.reset(new AudioMultiVector(channels));
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001929
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001930 // Delete sync buffer and create a new one.
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +00001931 sync_buffer_.reset(new SyncBuffer(channels, kSyncBufferSize * fs_mult_));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001932
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00001933 // Delete BackgroundNoise object and create a new one.
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +00001934 background_noise_.reset(new BackgroundNoise(channels));
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00001935 background_noise_->set_mode(background_noise_mode_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001936
1937 // Reset random vector.
1938 random_vector_.Reset();
1939
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001940 UpdatePlcComponents(fs_hz, channels);
1941
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001942 // Move index so that we create a small set of future samples (all 0).
1943 sync_buffer_->set_next_index(sync_buffer_->next_index() -
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001944 expand_->overlap_length());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001945
henrik.lundin@webrtc.org40d3fc62013-09-18 12:19:50 +00001946 normal_.reset(new Normal(fs_hz, decoder_database_.get(), *background_noise_,
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +00001947 expand_.get()));
henrik.lundin@webrtc.orgd9faa462014-01-14 10:18:45 +00001948 accelerate_.reset(
1949 accelerate_factory_->Create(fs_hz, channels, *background_noise_));
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001950 preemptive_expand_.reset(preemptive_expand_factory_->Create(
1951 fs_hz, channels,
1952 *background_noise_,
1953 static_cast<int>(expand_->overlap_length())));
henrik.lundin@webrtc.org40d3fc62013-09-18 12:19:50 +00001954
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001955 // Delete ComfortNoise object and create a new one.
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +00001956 comfort_noise_.reset(new ComfortNoise(fs_hz, decoder_database_.get(),
1957 sync_buffer_.get()));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001958
1959 // Verify that |decoded_buffer_| is long enough.
1960 if (decoded_buffer_length_ < kMaxFrameSize * channels) {
1961 // Reallocate to larger size.
1962 decoded_buffer_length_ = kMaxFrameSize * channels;
1963 decoded_buffer_.reset(new int16_t[decoded_buffer_length_]);
1964 }
1965
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001966 // Create DecisionLogic if it is not created yet, then communicate new sample
1967 // rate and output size to DecisionLogic object.
1968 if (!decision_logic_.get()) {
henrik.lundin@webrtc.org7cbc4f92014-10-07 06:37:39 +00001969 CreateDecisionLogic();
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001970 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001971 decision_logic_->SetSampleRate(fs_hz_, output_size_samples_);
1972}
1973
1974NetEqOutputType NetEqImpl::LastOutputType() {
1975 assert(vad_.get());
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +00001976 assert(expand_.get());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001977 if (last_mode_ == kModeCodecInternalCng || last_mode_ == kModeRfc3389Cng) {
1978 return kOutputCNG;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001979 } else if (last_mode_ == kModeExpand && expand_->MuteFactor(0) == 0) {
1980 // Expand mode has faded down to background noise only (very long expand).
1981 return kOutputPLCtoCNG;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001982 } else if (last_mode_ == kModeExpand) {
1983 return kOutputPLC;
wu@webrtc.org24301a62013-12-13 19:17:43 +00001984 } else if (vad_->running() && !vad_->active_speech()) {
1985 return kOutputVADPassive;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001986 } else {
1987 return kOutputNormal;
1988 }
1989}
1990
henrik.lundin@webrtc.org7cbc4f92014-10-07 06:37:39 +00001991void NetEqImpl::CreateDecisionLogic() {
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001992 decision_logic_.reset(DecisionLogic::Create(fs_hz_, output_size_samples_,
henrik.lundin@webrtc.org7cbc4f92014-10-07 06:37:39 +00001993 playout_mode_,
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001994 decoder_database_.get(),
1995 *packet_buffer_.get(),
1996 delay_manager_.get(),
1997 buffer_level_filter_.get()));
1998}
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001999} // namespace webrtc