blob: 636594e729cf645ecfa874b8ed765176aa151c78 [file] [log] [blame]
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001/*
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
henrik.lundin@webrtc.org9c55f0f2014-06-09 08:10:28 +000011#include "webrtc/modules/audio_coding/neteq/neteq_impl.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000012
13#include <assert.h>
14#include <memory.h> // memset
15
16#include <algorithm>
17
Henrik Lundind8a03fa2015-06-03 11:55:45 +020018#include "webrtc/base/checks.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000019#include "webrtc/common_audio/signal_processing/include/signal_processing_library.h"
kwiberg@webrtc.orge04a93b2014-12-09 10:12:53 +000020#include "webrtc/modules/audio_coding/codecs/audio_decoder.h"
henrik.lundin@webrtc.org9c55f0f2014-06-09 08:10:28 +000021#include "webrtc/modules/audio_coding/neteq/accelerate.h"
22#include "webrtc/modules/audio_coding/neteq/background_noise.h"
23#include "webrtc/modules/audio_coding/neteq/buffer_level_filter.h"
24#include "webrtc/modules/audio_coding/neteq/comfort_noise.h"
25#include "webrtc/modules/audio_coding/neteq/decision_logic.h"
26#include "webrtc/modules/audio_coding/neteq/decoder_database.h"
27#include "webrtc/modules/audio_coding/neteq/defines.h"
28#include "webrtc/modules/audio_coding/neteq/delay_manager.h"
29#include "webrtc/modules/audio_coding/neteq/delay_peak_detector.h"
30#include "webrtc/modules/audio_coding/neteq/dtmf_buffer.h"
31#include "webrtc/modules/audio_coding/neteq/dtmf_tone_generator.h"
32#include "webrtc/modules/audio_coding/neteq/expand.h"
henrik.lundin@webrtc.org9c55f0f2014-06-09 08:10:28 +000033#include "webrtc/modules/audio_coding/neteq/merge.h"
34#include "webrtc/modules/audio_coding/neteq/normal.h"
35#include "webrtc/modules/audio_coding/neteq/packet_buffer.h"
36#include "webrtc/modules/audio_coding/neteq/packet.h"
37#include "webrtc/modules/audio_coding/neteq/payload_splitter.h"
38#include "webrtc/modules/audio_coding/neteq/post_decode_vad.h"
39#include "webrtc/modules/audio_coding/neteq/preemptive_expand.h"
40#include "webrtc/modules/audio_coding/neteq/sync_buffer.h"
41#include "webrtc/modules/audio_coding/neteq/timestamp_scaler.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000042#include "webrtc/modules/interface/module_common_types.h"
43#include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
44#include "webrtc/system_wrappers/interface/logging.h"
45
46// Modify the code to obtain backwards bit-exactness. Once bit-exactness is no
47// longer required, this #define should be removed (and the code that it
48// enables).
49#define LEGACY_BITEXACT
50
51namespace webrtc {
52
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +000053NetEqImpl::NetEqImpl(const NetEq::Config& config,
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000054 BufferLevelFilter* buffer_level_filter,
55 DecoderDatabase* decoder_database,
56 DelayManager* delay_manager,
57 DelayPeakDetector* delay_peak_detector,
58 DtmfBuffer* dtmf_buffer,
59 DtmfToneGenerator* dtmf_tone_generator,
60 PacketBuffer* packet_buffer,
61 PayloadSplitter* payload_splitter,
henrik.lundin@webrtc.orgd9faa462014-01-14 10:18:45 +000062 TimestampScaler* timestamp_scaler,
63 AccelerateFactory* accelerate_factory,
64 ExpandFactory* expand_factory,
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +000065 PreemptiveExpandFactory* preemptive_expand_factory,
66 bool create_components)
henrik.lundin@webrtc.org2f816bb2014-06-05 10:37:13 +000067 : crit_sect_(CriticalSectionWrapper::CreateCriticalSection()),
68 buffer_level_filter_(buffer_level_filter),
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000069 decoder_database_(decoder_database),
70 delay_manager_(delay_manager),
71 delay_peak_detector_(delay_peak_detector),
72 dtmf_buffer_(dtmf_buffer),
73 dtmf_tone_generator_(dtmf_tone_generator),
74 packet_buffer_(packet_buffer),
75 payload_splitter_(payload_splitter),
76 timestamp_scaler_(timestamp_scaler),
77 vad_(new PostDecodeVad()),
henrik.lundin@webrtc.orgd9faa462014-01-14 10:18:45 +000078 expand_factory_(expand_factory),
79 accelerate_factory_(accelerate_factory),
80 preemptive_expand_factory_(preemptive_expand_factory),
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000081 last_mode_(kModeNormal),
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000082 decoded_buffer_length_(kMaxFrameSize),
83 decoded_buffer_(new int16_t[decoded_buffer_length_]),
84 playout_timestamp_(0),
85 new_codec_(false),
86 timestamp_(0),
87 reset_decoder_(false),
88 current_rtp_payload_type_(0xFF), // Invalid RTP payload type.
89 current_cng_rtp_payload_type_(0xFF), // Invalid RTP payload type.
90 ssrc_(0),
91 first_packet_(true),
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000092 error_code_(0),
93 decoder_error_code_(0),
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +000094 background_noise_mode_(config.background_noise_mode),
henrik.lundin@webrtc.org7cbc4f92014-10-07 06:37:39 +000095 playout_mode_(config.playout_mode),
Henrik Lundincf808d22015-05-27 14:33:29 +020096 enable_fast_accelerate_(config.enable_fast_accelerate),
minyue@webrtc.orgd7301772013-08-29 00:58:14 +000097 decoded_packet_sequence_number_(-1),
98 decoded_packet_timestamp_(0) {
Henrik Lundin905495c2015-05-25 16:58:41 +020099 LOG(LS_INFO) << "NetEq config: " << config.ToString();
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000100 int fs = config.sample_rate_hz;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000101 if (fs != 8000 && fs != 16000 && fs != 32000 && fs != 48000) {
102 LOG(LS_ERROR) << "Sample rate " << fs << " Hz not supported. " <<
103 "Changing to 8000 Hz.";
104 fs = 8000;
105 }
andrew@webrtc.org0569d932014-04-09 17:48:48 +0000106 LOG(LS_VERBOSE) << "Create NetEqImpl object with fs = " << fs << ".";
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000107 fs_hz_ = fs;
108 fs_mult_ = fs / 8000;
109 output_size_samples_ = kOutputSizeMs * 8 * fs_mult_;
110 decoder_frame_length_ = 3 * output_size_samples_;
111 WebRtcSpl_Init();
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +0000112 if (create_components) {
113 SetSampleRateAndChannels(fs, 1); // Default is 1 channel.
114 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000115}
116
117NetEqImpl::~NetEqImpl() {
118 LOG(LS_INFO) << "Deleting NetEqImpl object.";
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000119}
120
121int NetEqImpl::InsertPacket(const WebRtcRTPHeader& rtp_header,
122 const uint8_t* payload,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000123 size_t length_bytes,
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000124 uint32_t receive_timestamp) {
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +0000125 CriticalSectionScoped lock(crit_sect_.get());
turaj@webrtc.org0c0fae82013-09-25 17:42:17 +0000126 LOG(LS_VERBOSE) << "InsertPacket: ts=" << rtp_header.header.timestamp <<
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000127 ", sn=" << rtp_header.header.sequenceNumber <<
128 ", pt=" << static_cast<int>(rtp_header.header.payloadType) <<
129 ", ssrc=" << rtp_header.header.ssrc <<
130 ", len=" << length_bytes;
131 int error = InsertPacketInternal(rtp_header, payload, length_bytes,
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +0000132 receive_timestamp, false);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000133 if (error != 0) {
134 LOG_FERR1(LS_WARNING, InsertPacketInternal, error);
135 error_code_ = error;
136 return kFail;
137 }
138 return kOK;
139}
140
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +0000141int NetEqImpl::InsertSyncPacket(const WebRtcRTPHeader& rtp_header,
142 uint32_t receive_timestamp) {
143 CriticalSectionScoped lock(crit_sect_.get());
144 LOG(LS_VERBOSE) << "InsertPacket-Sync: ts="
145 << rtp_header.header.timestamp <<
146 ", sn=" << rtp_header.header.sequenceNumber <<
147 ", pt=" << static_cast<int>(rtp_header.header.payloadType) <<
148 ", ssrc=" << rtp_header.header.ssrc;
149
150 const uint8_t kSyncPayload[] = { 's', 'y', 'n', 'c' };
151 int error = InsertPacketInternal(
152 rtp_header, kSyncPayload, sizeof(kSyncPayload), receive_timestamp, true);
153
henrik.lundin@webrtc.orge7ce4372014-01-09 14:01:55 +0000154 if (error != 0) {
155 LOG_FERR1(LS_WARNING, InsertPacketInternal, error);
156 error_code_ = error;
157 return kFail;
158 }
159 return kOK;
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +0000160}
161
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000162int NetEqImpl::GetAudio(size_t max_length, int16_t* output_audio,
163 int* samples_per_channel, int* num_channels,
164 NetEqOutputType* type) {
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +0000165 CriticalSectionScoped lock(crit_sect_.get());
turaj@webrtc.org0c0fae82013-09-25 17:42:17 +0000166 LOG(LS_VERBOSE) << "GetAudio";
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000167 int error = GetAudioInternal(max_length, output_audio, samples_per_channel,
168 num_channels);
turaj@webrtc.org0c0fae82013-09-25 17:42:17 +0000169 LOG(LS_VERBOSE) << "Produced " << *samples_per_channel <<
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000170 " samples/channel for " << *num_channels << " channel(s)";
171 if (error != 0) {
172 LOG_FERR1(LS_WARNING, GetAudioInternal, error);
173 error_code_ = error;
174 return kFail;
175 }
176 if (type) {
177 *type = LastOutputType();
178 }
179 return kOK;
180}
181
182int NetEqImpl::RegisterPayloadType(enum NetEqDecoder codec,
183 uint8_t rtp_payload_type) {
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +0000184 CriticalSectionScoped lock(crit_sect_.get());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000185 LOG_API2(static_cast<int>(rtp_payload_type), codec);
186 int ret = decoder_database_->RegisterPayload(rtp_payload_type, codec);
187 if (ret != DecoderDatabase::kOK) {
pkasting@chromium.org026b8922015-01-30 19:53:42 +0000188 LOG_FERR2(LS_WARNING, RegisterPayload, static_cast<int>(rtp_payload_type),
189 codec);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000190 switch (ret) {
191 case DecoderDatabase::kInvalidRtpPayloadType:
192 error_code_ = kInvalidRtpPayloadType;
193 break;
194 case DecoderDatabase::kCodecNotSupported:
195 error_code_ = kCodecNotSupported;
196 break;
197 case DecoderDatabase::kDecoderExists:
198 error_code_ = kDecoderExists;
199 break;
200 default:
201 error_code_ = kOtherError;
202 }
203 return kFail;
204 }
205 return kOK;
206}
207
208int NetEqImpl::RegisterExternalDecoder(AudioDecoder* decoder,
209 enum NetEqDecoder codec,
Karl Wibergd8399e62015-05-25 14:39:56 +0200210 uint8_t rtp_payload_type,
211 int sample_rate_hz) {
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +0000212 CriticalSectionScoped lock(crit_sect_.get());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000213 LOG_API2(static_cast<int>(rtp_payload_type), codec);
214 if (!decoder) {
215 LOG(LS_ERROR) << "Cannot register external decoder with NULL pointer";
216 assert(false);
217 return kFail;
218 }
219 int ret = decoder_database_->InsertExternal(rtp_payload_type, codec,
220 sample_rate_hz, decoder);
221 if (ret != DecoderDatabase::kOK) {
pkasting@chromium.org026b8922015-01-30 19:53:42 +0000222 LOG_FERR2(LS_WARNING, InsertExternal, static_cast<int>(rtp_payload_type),
223 codec);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000224 switch (ret) {
225 case DecoderDatabase::kInvalidRtpPayloadType:
226 error_code_ = kInvalidRtpPayloadType;
227 break;
228 case DecoderDatabase::kCodecNotSupported:
229 error_code_ = kCodecNotSupported;
230 break;
231 case DecoderDatabase::kDecoderExists:
232 error_code_ = kDecoderExists;
233 break;
234 case DecoderDatabase::kInvalidSampleRate:
235 error_code_ = kInvalidSampleRate;
236 break;
237 case DecoderDatabase::kInvalidPointer:
238 error_code_ = kInvalidPointer;
239 break;
240 default:
241 error_code_ = kOtherError;
242 }
243 return kFail;
244 }
245 return kOK;
246}
247
248int NetEqImpl::RemovePayloadType(uint8_t rtp_payload_type) {
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +0000249 CriticalSectionScoped lock(crit_sect_.get());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000250 LOG_API1(static_cast<int>(rtp_payload_type));
251 int ret = decoder_database_->Remove(rtp_payload_type);
252 if (ret == DecoderDatabase::kOK) {
253 return kOK;
254 } else if (ret == DecoderDatabase::kDecoderNotFound) {
255 error_code_ = kDecoderNotFound;
256 } else {
257 error_code_ = kOtherError;
258 }
pkasting@chromium.org026b8922015-01-30 19:53:42 +0000259 LOG_FERR1(LS_WARNING, Remove, static_cast<int>(rtp_payload_type));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000260 return kFail;
261}
262
turaj@webrtc.orgf1efc572013-08-16 23:44:24 +0000263bool NetEqImpl::SetMinimumDelay(int delay_ms) {
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +0000264 CriticalSectionScoped lock(crit_sect_.get());
turaj@webrtc.orgf1efc572013-08-16 23:44:24 +0000265 if (delay_ms >= 0 && delay_ms < 10000) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000266 assert(delay_manager_.get());
turaj@webrtc.orgf1efc572013-08-16 23:44:24 +0000267 return delay_manager_->SetMinimumDelay(delay_ms);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000268 }
269 return false;
270}
271
turaj@webrtc.orgf1efc572013-08-16 23:44:24 +0000272bool NetEqImpl::SetMaximumDelay(int delay_ms) {
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +0000273 CriticalSectionScoped lock(crit_sect_.get());
turaj@webrtc.orgf1efc572013-08-16 23:44:24 +0000274 if (delay_ms >= 0 && delay_ms < 10000) {
275 assert(delay_manager_.get());
276 return delay_manager_->SetMaximumDelay(delay_ms);
277 }
278 return false;
279}
280
281int NetEqImpl::LeastRequiredDelayMs() const {
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +0000282 CriticalSectionScoped lock(crit_sect_.get());
turaj@webrtc.orgf1efc572013-08-16 23:44:24 +0000283 assert(delay_manager_.get());
284 return delay_manager_->least_required_delay_ms();
285}
286
Karl Wiberg7f6c4d42015-04-09 15:44:22 +0200287int NetEqImpl::SetTargetDelay() {
288 return kNotImplemented;
289}
290
291int NetEqImpl::TargetDelay() {
292 return kNotImplemented;
293}
294
Henrik Lundind8a03fa2015-06-03 11:55:45 +0200295int NetEqImpl::CurrentDelayMs() const {
296 CriticalSectionScoped lock(crit_sect_.get());
297 if (fs_hz_ == 0)
298 return 0;
299 // Sum up the samples in the packet buffer with the future length of the sync
300 // buffer, and divide the sum by the sample rate.
301 const int delay_samples =
302 packet_buffer_->NumSamplesInBuffer(decoder_database_.get(),
303 decoder_frame_length_) +
304 static_cast<int>(sync_buffer_->FutureLength());
305 // The division below will truncate.
306 const int delay_ms = delay_samples / rtc::CheckedDivExact(fs_hz_, 1000);
307 return delay_ms;
Karl Wiberg7f6c4d42015-04-09 15:44:22 +0200308}
309
henrik.lundin@webrtc.org7cbc4f92014-10-07 06:37:39 +0000310// Deprecated.
311// TODO(henrik.lundin) Delete.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000312void NetEqImpl::SetPlayoutMode(NetEqPlayoutMode mode) {
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +0000313 CriticalSectionScoped lock(crit_sect_.get());
henrik.lundin@webrtc.org7cbc4f92014-10-07 06:37:39 +0000314 if (mode != playout_mode_) {
315 playout_mode_ = mode;
316 CreateDecisionLogic();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000317 }
318}
319
henrik.lundin@webrtc.org7cbc4f92014-10-07 06:37:39 +0000320// Deprecated.
321// TODO(henrik.lundin) Delete.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000322NetEqPlayoutMode NetEqImpl::PlayoutMode() const {
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +0000323 CriticalSectionScoped lock(crit_sect_.get());
henrik.lundin@webrtc.org7cbc4f92014-10-07 06:37:39 +0000324 return playout_mode_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000325}
326
327int NetEqImpl::NetworkStatistics(NetEqNetworkStatistics* stats) {
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +0000328 CriticalSectionScoped lock(crit_sect_.get());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000329 assert(decoder_database_.get());
330 const int total_samples_in_buffers = packet_buffer_->NumSamplesInBuffer(
331 decoder_database_.get(), decoder_frame_length_) +
turaj@webrtc.org362a55e2013-09-20 16:25:28 +0000332 static_cast<int>(sync_buffer_->FutureLength());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000333 assert(delay_manager_.get());
334 assert(decision_logic_.get());
335 stats_.GetNetworkStatistics(fs_hz_, total_samples_in_buffers,
336 decoder_frame_length_, *delay_manager_.get(),
337 *decision_logic_.get(), stats);
338 return 0;
339}
340
341void NetEqImpl::WaitingTimes(std::vector<int>* waiting_times) {
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 stats_.WaitingTimes(waiting_times);
344}
345
346void NetEqImpl::GetRtcpStatistics(RtcpStatistics* stats) {
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +0000347 CriticalSectionScoped lock(crit_sect_.get());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000348 if (stats) {
349 rtcp_.GetStatistics(false, stats);
350 }
351}
352
353void NetEqImpl::GetRtcpStatisticsNoReset(RtcpStatistics* stats) {
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +0000354 CriticalSectionScoped lock(crit_sect_.get());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000355 if (stats) {
356 rtcp_.GetStatistics(true, stats);
357 }
358}
359
360void NetEqImpl::EnableVad() {
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +0000361 CriticalSectionScoped lock(crit_sect_.get());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000362 assert(vad_.get());
363 vad_->Enable();
364}
365
366void NetEqImpl::DisableVad() {
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +0000367 CriticalSectionScoped lock(crit_sect_.get());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000368 assert(vad_.get());
369 vad_->Disable();
370}
371
wu@webrtc.org94454b72014-06-05 20:34:08 +0000372bool NetEqImpl::GetPlayoutTimestamp(uint32_t* timestamp) {
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +0000373 CriticalSectionScoped lock(crit_sect_.get());
wu@webrtc.org94454b72014-06-05 20:34:08 +0000374 if (first_packet_) {
375 // We don't have a valid RTP timestamp until we have decoded our first
376 // RTP packet.
377 return false;
378 }
379 *timestamp = timestamp_scaler_->ToExternal(playout_timestamp_);
380 return true;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000381}
382
Karl Wiberg7f6c4d42015-04-09 15:44:22 +0200383int NetEqImpl::SetTargetNumberOfChannels() {
384 return kNotImplemented;
385}
386
387int NetEqImpl::SetTargetSampleRate() {
388 return kNotImplemented;
389}
390
henrik.lundin@webrtc.orgb0f4b3d2014-11-04 08:53:10 +0000391int NetEqImpl::LastError() const {
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +0000392 CriticalSectionScoped lock(crit_sect_.get());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000393 return error_code_;
394}
395
396int NetEqImpl::LastDecoderError() {
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +0000397 CriticalSectionScoped lock(crit_sect_.get());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000398 return decoder_error_code_;
399}
400
401void NetEqImpl::FlushBuffers() {
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +0000402 CriticalSectionScoped lock(crit_sect_.get());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000403 LOG_API0();
404 packet_buffer_->Flush();
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +0000405 assert(sync_buffer_.get());
406 assert(expand_.get());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000407 sync_buffer_->Flush();
408 sync_buffer_->set_next_index(sync_buffer_->next_index() -
409 expand_->overlap_length());
410 // Set to wait for new codec.
411 first_packet_ = true;
412}
413
turaj@webrtc.org3170b572013-08-30 15:36:53 +0000414void NetEqImpl::PacketBufferStatistics(int* current_num_packets,
henrik.lundin@webrtc.org116ed1d2014-04-28 08:20:04 +0000415 int* max_num_packets) const {
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +0000416 CriticalSectionScoped lock(crit_sect_.get());
henrik.lundin@webrtc.org116ed1d2014-04-28 08:20:04 +0000417 packet_buffer_->BufferStat(current_num_packets, max_num_packets);
turaj@webrtc.org3170b572013-08-30 15:36:53 +0000418}
419
turaj@webrtc.orgff43c852013-09-25 00:07:27 +0000420int NetEqImpl::DecodedRtpInfo(int* sequence_number, uint32_t* timestamp) const {
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +0000421 CriticalSectionScoped lock(crit_sect_.get());
minyue@webrtc.orgd7301772013-08-29 00:58:14 +0000422 if (decoded_packet_sequence_number_ < 0)
423 return -1;
424 *sequence_number = decoded_packet_sequence_number_;
425 *timestamp = decoded_packet_timestamp_;
426 return 0;
427}
428
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000429const SyncBuffer* NetEqImpl::sync_buffer_for_test() const {
430 CriticalSectionScoped lock(crit_sect_.get());
431 return sync_buffer_.get();
432}
433
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000434// Methods below this line are private.
435
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000436int NetEqImpl::InsertPacketInternal(const WebRtcRTPHeader& rtp_header,
437 const uint8_t* payload,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000438 size_t length_bytes,
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +0000439 uint32_t receive_timestamp,
440 bool is_sync_packet) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000441 if (!payload) {
442 LOG_F(LS_ERROR) << "payload == NULL";
443 return kInvalidPointer;
444 }
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +0000445 // Sanity checks for sync-packets.
446 if (is_sync_packet) {
447 if (decoder_database_->IsDtmf(rtp_header.header.payloadType) ||
448 decoder_database_->IsRed(rtp_header.header.payloadType) ||
449 decoder_database_->IsComfortNoise(rtp_header.header.payloadType)) {
450 LOG_F(LS_ERROR) << "Sync-packet with an unacceptable payload type "
pkasting@chromium.orgd3245462015-02-23 21:28:22 +0000451 << static_cast<int>(rtp_header.header.payloadType);
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +0000452 return kSyncPacketNotAccepted;
453 }
454 if (first_packet_ ||
455 rtp_header.header.payloadType != current_rtp_payload_type_ ||
456 rtp_header.header.ssrc != ssrc_) {
457 // Even if |current_rtp_payload_type_| is 0xFF, sync-packet isn't
458 // accepted.
pkasting@chromium.orgd3245462015-02-23 21:28:22 +0000459 LOG_F(LS_ERROR)
460 << "Changing codec, SSRC or first packet with sync-packet.";
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +0000461 return kSyncPacketNotAccepted;
462 }
463 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000464 PacketList packet_list;
465 RTPHeader main_header;
466 {
henrik.lundin@webrtc.orge1d468c2013-01-30 07:37:20 +0000467 // Convert to Packet.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000468 // Create |packet| within this separate scope, since it should not be used
469 // directly once it's been inserted in the packet list. This way, |packet|
470 // is not defined outside of this block.
henrik.lundin@webrtc.orge1d468c2013-01-30 07:37:20 +0000471 Packet* packet = new Packet;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000472 packet->header.markerBit = false;
473 packet->header.payloadType = rtp_header.header.payloadType;
474 packet->header.sequenceNumber = rtp_header.header.sequenceNumber;
475 packet->header.timestamp = rtp_header.header.timestamp;
476 packet->header.ssrc = rtp_header.header.ssrc;
477 packet->header.numCSRCs = 0;
478 packet->payload_length = length_bytes;
479 packet->primary = true;
480 packet->waiting_time = 0;
481 packet->payload = new uint8_t[packet->payload_length];
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +0000482 packet->sync_packet = is_sync_packet;
henrik.lundin@webrtc.org73deaad2013-01-31 13:32:51 +0000483 if (!packet->payload) {
484 LOG_F(LS_ERROR) << "Payload pointer is NULL.";
485 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000486 assert(payload); // Already checked above.
487 memcpy(packet->payload, payload, packet->payload_length);
488 // Insert packet in a packet list.
489 packet_list.push_back(packet);
490 // Save main payloads header for later.
491 memcpy(&main_header, &packet->header, sizeof(main_header));
492 }
493
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000494 bool update_sample_rate_and_channels = false;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000495 // Reinitialize NetEq if it's needed (changed SSRC or first call).
496 if ((main_header.ssrc != ssrc_) || first_packet_) {
henrik.lundin@webrtc.org6ff3ac12014-11-20 14:14:49 +0000497 // Note: |first_packet_| will be cleared further down in this method, once
498 // the packet has been successfully inserted into the packet buffer.
499
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000500 rtcp_.Init(main_header.sequenceNumber);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000501
502 // Flush the packet buffer and DTMF buffer.
503 packet_buffer_->Flush();
504 dtmf_buffer_->Flush();
505
506 // Store new SSRC.
507 ssrc_ = main_header.ssrc;
508
turaj@webrtc.org4d06db52013-03-27 18:31:42 +0000509 // Update audio buffer timestamp.
510 sync_buffer_->IncreaseEndTimestamp(main_header.timestamp - timestamp_);
511
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000512 // Update codecs.
513 timestamp_ = main_header.timestamp;
514 current_rtp_payload_type_ = main_header.payloadType;
515
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000516 // Reset timestamp scaling.
517 timestamp_scaler_->Reset();
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000518
henrik.lundin@webrtc.org6ff3ac12014-11-20 14:14:49 +0000519 // Trigger an update of sampling rate and the number of channels.
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000520 update_sample_rate_and_channels = true;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000521 }
522
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +0000523 // Update RTCP statistics, only for regular packets.
524 if (!is_sync_packet)
525 rtcp_.Update(main_header, receive_timestamp);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000526
527 // Check for RED payload type, and separate payloads into several packets.
528 if (decoder_database_->IsRed(main_header.payloadType)) {
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +0000529 assert(!is_sync_packet); // We had a sanity check for this.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000530 if (payload_splitter_->SplitRed(&packet_list) != PayloadSplitter::kOK) {
531 LOG_FERR1(LS_WARNING, SplitRed, packet_list.size());
532 PacketBuffer::DeleteAllPackets(&packet_list);
533 return kRedundancySplitError;
534 }
535 // Only accept a few RED payloads of the same type as the main data,
536 // DTMF events and CNG.
537 payload_splitter_->CheckRedPayloads(&packet_list, *decoder_database_);
538 // Update the stored main payload header since the main payload has now
539 // changed.
540 memcpy(&main_header, &packet_list.front()->header, sizeof(main_header));
541 }
542
543 // Check payload types.
544 if (decoder_database_->CheckPayloadTypes(packet_list) ==
545 DecoderDatabase::kDecoderNotFound) {
546 LOG_FERR1(LS_WARNING, CheckPayloadTypes, packet_list.size());
547 PacketBuffer::DeleteAllPackets(&packet_list);
548 return kUnknownRtpPayloadType;
549 }
550
551 // Scale timestamp to internal domain (only for some codecs).
552 timestamp_scaler_->ToInternal(&packet_list);
553
554 // Process DTMF payloads. Cycle through the list of packets, and pick out any
555 // DTMF payloads found.
556 PacketList::iterator it = packet_list.begin();
557 while (it != packet_list.end()) {
558 Packet* current_packet = (*it);
559 assert(current_packet);
560 assert(current_packet->payload);
561 if (decoder_database_->IsDtmf(current_packet->header.payloadType)) {
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +0000562 assert(!current_packet->sync_packet); // We had a sanity check for this.
minyue@webrtc.org9721db72013-08-06 05:36:26 +0000563 DtmfEvent event;
564 int ret = DtmfBuffer::ParseEvent(
565 current_packet->header.timestamp,
566 current_packet->payload,
567 current_packet->payload_length,
568 &event);
569 if (ret != DtmfBuffer::kOK) {
570 LOG_FERR2(LS_WARNING, ParseEvent, ret,
571 current_packet->payload_length);
572 PacketBuffer::DeleteAllPackets(&packet_list);
573 return kDtmfParsingError;
574 }
575 if (dtmf_buffer_->InsertEvent(event) != DtmfBuffer::kOK) {
576 LOG_FERR0(LS_WARNING, InsertEvent);
577 PacketBuffer::DeleteAllPackets(&packet_list);
578 return kDtmfInsertError;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000579 }
580 // TODO(hlundin): Let the destructor of Packet handle the payload.
581 delete [] current_packet->payload;
582 delete current_packet;
583 it = packet_list.erase(it);
584 } else {
585 ++it;
586 }
587 }
588
minyue@webrtc.org7549ff42014-04-02 15:03:01 +0000589 // Check for FEC in packets, and separate payloads into several packets.
590 int ret = payload_splitter_->SplitFec(&packet_list, decoder_database_.get());
591 if (ret != PayloadSplitter::kOK) {
592 LOG_FERR1(LS_WARNING, SplitFec, packet_list.size());
593 PacketBuffer::DeleteAllPackets(&packet_list);
594 switch (ret) {
595 case PayloadSplitter::kUnknownPayloadType:
596 return kUnknownRtpPayloadType;
597 default:
598 return kOtherError;
599 }
600 }
601
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000602 // Split payloads into smaller chunks. This also verifies that all payloads
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +0000603 // are of a known payload type. SplitAudio() method is protected against
604 // sync-packets.
minyue@webrtc.orgb28bfa72014-03-21 12:07:40 +0000605 ret = payload_splitter_->SplitAudio(&packet_list, *decoder_database_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000606 if (ret != PayloadSplitter::kOK) {
607 LOG_FERR1(LS_WARNING, SplitAudio, packet_list.size());
608 PacketBuffer::DeleteAllPackets(&packet_list);
609 switch (ret) {
610 case PayloadSplitter::kUnknownPayloadType:
611 return kUnknownRtpPayloadType;
612 case PayloadSplitter::kFrameSplitError:
613 return kFrameSplitError;
614 default:
615 return kOtherError;
616 }
617 }
618
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +0000619 // Update bandwidth estimate, if the packet is not sync-packet.
620 if (!packet_list.empty() && !packet_list.front()->sync_packet) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000621 // The list can be empty here if we got nothing but DTMF payloads.
622 AudioDecoder* decoder =
623 decoder_database_->GetDecoder(main_header.payloadType);
624 assert(decoder); // Should always get a valid object, since we have
625 // already checked that the payload types are known.
626 decoder->IncomingPacket(packet_list.front()->payload,
627 packet_list.front()->payload_length,
628 packet_list.front()->header.sequenceNumber,
629 packet_list.front()->header.timestamp,
630 receive_timestamp);
631 }
632
633 // Insert packets in buffer.
634 int temp_bufsize = packet_buffer_->NumPacketsInBuffer();
635 ret = packet_buffer_->InsertPacketList(
636 &packet_list,
637 *decoder_database_,
638 &current_rtp_payload_type_,
639 &current_cng_rtp_payload_type_);
640 if (ret == PacketBuffer::kFlushed) {
641 // Reset DSP timestamp etc. if packet buffer flushed.
642 new_codec_ = true;
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000643 update_sample_rate_and_channels = true;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000644 LOG_F(LS_WARNING) << "Packet buffer flushed";
645 } else if (ret != PacketBuffer::kOK) {
646 LOG_FERR1(LS_WARNING, InsertPacketList, packet_list.size());
647 PacketBuffer::DeleteAllPackets(&packet_list);
minyue@webrtc.org7bb54362013-08-06 05:40:57 +0000648 return kOtherError;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000649 }
henrik.lundin@webrtc.org6ff3ac12014-11-20 14:14:49 +0000650
651 if (first_packet_) {
652 first_packet_ = false;
653 // Update the codec on the next GetAudio call.
654 new_codec_ = true;
655 }
656
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000657 if (current_rtp_payload_type_ != 0xFF) {
658 const DecoderDatabase::DecoderInfo* dec_info =
659 decoder_database_->GetDecoderInfo(current_rtp_payload_type_);
660 if (!dec_info) {
661 assert(false); // Already checked that the payload type is known.
662 }
663 }
664
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000665 if (update_sample_rate_and_channels && !packet_buffer_->Empty()) {
666 // We do not use |current_rtp_payload_type_| to |set payload_type|, but
667 // get the next RTP header from |packet_buffer_| to obtain the payload type.
668 // The reason for it is the following corner case. If NetEq receives a
669 // CNG packet with a sample rate different than the current CNG then it
670 // flushes its buffer, assuming send codec must have been changed. However,
671 // payload type of the hypothetically new send codec is not known.
672 const RTPHeader* rtp_header = packet_buffer_->NextRtpHeader();
673 assert(rtp_header);
674 int payload_type = rtp_header->payloadType;
675 AudioDecoder* decoder = decoder_database_->GetDecoder(payload_type);
676 assert(decoder); // Payloads are already checked to be valid.
677 const DecoderDatabase::DecoderInfo* decoder_info =
678 decoder_database_->GetDecoderInfo(payload_type);
679 assert(decoder_info);
680 if (decoder_info->fs_hz != fs_hz_ ||
henrik.lundin@webrtc.org6dba1eb2015-03-18 09:47:08 +0000681 decoder->Channels() != algorithm_buffer_->Channels())
682 SetSampleRateAndChannels(decoder_info->fs_hz, decoder->Channels());
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000683 }
684
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000685 // TODO(hlundin): Move this code to DelayManager class.
686 const DecoderDatabase::DecoderInfo* dec_info =
687 decoder_database_->GetDecoderInfo(main_header.payloadType);
688 assert(dec_info); // Already checked that the payload type is known.
689 delay_manager_->LastDecoderType(dec_info->codec_type);
690 if (delay_manager_->last_pack_cng_or_dtmf() == 0) {
691 // Calculate the total speech length carried in each packet.
692 temp_bufsize = packet_buffer_->NumPacketsInBuffer() - temp_bufsize;
693 temp_bufsize *= decoder_frame_length_;
694
695 if ((temp_bufsize > 0) &&
696 (temp_bufsize != decision_logic_->packet_length_samples())) {
697 decision_logic_->set_packet_length_samples(temp_bufsize);
698 delay_manager_->SetPacketAudioLength((1000 * temp_bufsize) / fs_hz_);
699 }
700
701 // Update statistics.
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000702 if ((int32_t) (main_header.timestamp - timestamp_) >= 0 &&
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000703 !new_codec_) {
704 // Only update statistics if incoming packet is not older than last played
705 // out packet, and if new codec flag is not set.
706 delay_manager_->Update(main_header.sequenceNumber, main_header.timestamp,
707 fs_hz_);
708 }
709 } else if (delay_manager_->last_pack_cng_or_dtmf() == -1) {
710 // This is first "normal" packet after CNG or DTMF.
711 // Reset packet time counter and measure time until next packet,
712 // but don't update statistics.
713 delay_manager_->set_last_pack_cng_or_dtmf(0);
714 delay_manager_->ResetPacketIatCount();
715 }
716 return 0;
717}
718
719int NetEqImpl::GetAudioInternal(size_t max_length, int16_t* output,
720 int* samples_per_channel, int* num_channels) {
721 PacketList packet_list;
722 DtmfEvent dtmf_event;
723 Operations operation;
724 bool play_dtmf;
725 int return_value = GetDecision(&operation, &packet_list, &dtmf_event,
726 &play_dtmf);
727 if (return_value != 0) {
728 LOG_FERR1(LS_WARNING, GetDecision, return_value);
729 assert(false);
730 last_mode_ = kModeError;
731 return return_value;
732 }
turaj@webrtc.org0c0fae82013-09-25 17:42:17 +0000733 LOG(LS_VERBOSE) << "GetDecision returned operation=" << operation <<
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000734 " and " << packet_list.size() << " packet(s)";
735
736 AudioDecoder::SpeechType speech_type;
737 int length = 0;
738 int decode_return_value = Decode(&packet_list, &operation,
739 &length, &speech_type);
740
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000741 assert(vad_.get());
742 bool sid_frame_available =
743 (operation == kRfc3389Cng && !packet_list.empty());
744 vad_->Update(decoded_buffer_.get(), length, speech_type,
745 sid_frame_available, fs_hz_);
746
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +0000747 algorithm_buffer_->Clear();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000748 switch (operation) {
749 case kNormal: {
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +0000750 DoNormal(decoded_buffer_.get(), length, speech_type, play_dtmf);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000751 break;
752 }
753 case kMerge: {
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +0000754 DoMerge(decoded_buffer_.get(), length, speech_type, play_dtmf);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000755 break;
756 }
757 case kExpand: {
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +0000758 return_value = DoExpand(play_dtmf);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000759 break;
760 }
Henrik Lundincf808d22015-05-27 14:33:29 +0200761 case kAccelerate:
762 case kFastAccelerate: {
763 const bool fast_accelerate =
764 enable_fast_accelerate_ && (operation == kFastAccelerate);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000765 return_value = DoAccelerate(decoded_buffer_.get(), length, speech_type,
Henrik Lundincf808d22015-05-27 14:33:29 +0200766 play_dtmf, fast_accelerate);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000767 break;
768 }
769 case kPreemptiveExpand: {
770 return_value = DoPreemptiveExpand(decoded_buffer_.get(), length,
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +0000771 speech_type, play_dtmf);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000772 break;
773 }
774 case kRfc3389Cng:
775 case kRfc3389CngNoPacket: {
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +0000776 return_value = DoRfc3389Cng(&packet_list, play_dtmf);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000777 break;
778 }
779 case kCodecInternalCng: {
780 // This handles the case when there is no transmission and the decoder
781 // should produce internal comfort noise.
782 // TODO(hlundin): Write test for codec-internal CNG.
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +0000783 DoCodecInternalCng();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000784 break;
785 }
786 case kDtmf: {
787 // TODO(hlundin): Write test for this.
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +0000788 return_value = DoDtmf(dtmf_event, &play_dtmf);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000789 break;
790 }
791 case kAlternativePlc: {
792 // TODO(hlundin): Write test for this.
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +0000793 DoAlternativePlc(false);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000794 break;
795 }
796 case kAlternativePlcIncreaseTimestamp: {
797 // TODO(hlundin): Write test for this.
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +0000798 DoAlternativePlc(true);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000799 break;
800 }
801 case kAudioRepetitionIncreaseTimestamp: {
802 // TODO(hlundin): Write test for this.
803 sync_buffer_->IncreaseEndTimestamp(output_size_samples_);
804 // Skipping break on purpose. Execution should move on into the
805 // next case.
kjellander@webrtc.org7d2b6a92015-01-28 18:37:58 +0000806 FALLTHROUGH();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000807 }
808 case kAudioRepetition: {
809 // TODO(hlundin): Write test for this.
810 // Copy last |output_size_samples_| from |sync_buffer_| to
811 // |algorithm_buffer|.
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +0000812 algorithm_buffer_->PushBackFromIndex(
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000813 *sync_buffer_, sync_buffer_->Size() - output_size_samples_);
814 expand_->Reset();
815 break;
816 }
817 case kUndefined: {
818 LOG_F(LS_ERROR) << "Invalid operation kUndefined.";
819 assert(false); // This should not happen.
820 last_mode_ = kModeError;
821 return kInvalidOperation;
822 }
823 } // End of switch.
824 if (return_value < 0) {
825 return return_value;
826 }
827
828 if (last_mode_ != kModeRfc3389Cng) {
829 comfort_noise_->Reset();
830 }
831
832 // Copy from |algorithm_buffer| to |sync_buffer_|.
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +0000833 sync_buffer_->PushBack(*algorithm_buffer_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000834
835 // Extract data from |sync_buffer_| to |output|.
turaj@webrtc.org362a55e2013-09-20 16:25:28 +0000836 size_t num_output_samples_per_channel = output_size_samples_;
837 size_t num_output_samples = output_size_samples_ * sync_buffer_->Channels();
838 if (num_output_samples > max_length) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000839 LOG(LS_WARNING) << "Output array is too short. " << max_length << " < " <<
840 output_size_samples_ << " * " << sync_buffer_->Channels();
841 num_output_samples = max_length;
turaj@webrtc.org362a55e2013-09-20 16:25:28 +0000842 num_output_samples_per_channel = static_cast<int>(
843 max_length / sync_buffer_->Channels());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000844 }
turaj@webrtc.org362a55e2013-09-20 16:25:28 +0000845 int samples_from_sync = static_cast<int>(
846 sync_buffer_->GetNextAudioInterleaved(num_output_samples_per_channel,
847 output));
848 *num_channels = static_cast<int>(sync_buffer_->Channels());
turaj@webrtc.org0c0fae82013-09-25 17:42:17 +0000849 LOG(LS_VERBOSE) << "Sync buffer (" << *num_channels << " channel(s)):" <<
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +0000850 " insert " << algorithm_buffer_->Size() << " samples, extract " <<
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000851 samples_from_sync << " samples";
852 if (samples_from_sync != output_size_samples_) {
853 LOG_F(LS_ERROR) << "samples_from_sync != output_size_samples_";
minyue@webrtc.orgdb1cefc2013-08-13 01:39:21 +0000854 // TODO(minyue): treatment of under-run, filling zeros
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000855 memset(output, 0, num_output_samples * sizeof(int16_t));
856 *samples_per_channel = output_size_samples_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000857 return kSampleUnderrun;
858 }
859 *samples_per_channel = output_size_samples_;
860
861 // Should always have overlap samples left in the |sync_buffer_|.
862 assert(sync_buffer_->FutureLength() >= expand_->overlap_length());
863
864 if (play_dtmf) {
865 return_value = DtmfOverdub(dtmf_event, sync_buffer_->Channels(), output);
866 }
867
868 // Update the background noise parameters if last operation wrote data
869 // straight from the decoder to the |sync_buffer_|. That is, none of the
870 // operations that modify the signal can be followed by a parameter update.
871 if ((last_mode_ == kModeNormal) ||
872 (last_mode_ == kModeAccelerateFail) ||
873 (last_mode_ == kModePreemptiveExpandFail) ||
874 (last_mode_ == kModeRfc3389Cng) ||
875 (last_mode_ == kModeCodecInternalCng)) {
876 background_noise_->Update(*sync_buffer_, *vad_.get());
877 }
878
879 if (operation == kDtmf) {
880 // DTMF data was written the end of |sync_buffer_|.
881 // Update index to end of DTMF data in |sync_buffer_|.
882 sync_buffer_->set_dtmf_index(sync_buffer_->Size());
883 }
884
henrik.lundin@webrtc.orged865b52014-03-06 10:28:07 +0000885 if (last_mode_ != kModeExpand) {
886 // If last operation was not expand, calculate the |playout_timestamp_| from
887 // the |sync_buffer_|. However, do not update the |playout_timestamp_| if it
888 // would be moved "backwards".
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000889 uint32_t temp_timestamp = sync_buffer_->end_timestamp() -
turaj@webrtc.org362a55e2013-09-20 16:25:28 +0000890 static_cast<uint32_t>(sync_buffer_->FutureLength());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000891 if (static_cast<int32_t>(temp_timestamp - playout_timestamp_) > 0) {
892 playout_timestamp_ = temp_timestamp;
893 }
894 } else {
895 // Use dead reckoning to estimate the |playout_timestamp_|.
896 playout_timestamp_ += output_size_samples_;
897 }
898
899 if (decode_return_value) return decode_return_value;
900 return return_value;
901}
902
903int NetEqImpl::GetDecision(Operations* operation,
904 PacketList* packet_list,
905 DtmfEvent* dtmf_event,
906 bool* play_dtmf) {
907 // Initialize output variables.
908 *play_dtmf = false;
909 *operation = kUndefined;
910
911 // Increment time counters.
912 packet_buffer_->IncrementWaitingTimes();
913 stats_.IncreaseCounter(output_size_samples_, fs_hz_);
914
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +0000915 assert(sync_buffer_.get());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000916 uint32_t end_timestamp = sync_buffer_->end_timestamp();
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000917 if (!new_codec_) {
918 const uint32_t five_seconds_samples = 5 * fs_hz_;
919 packet_buffer_->DiscardOldPackets(end_timestamp, five_seconds_samples);
920 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000921 const RTPHeader* header = packet_buffer_->NextRtpHeader();
922
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +0000923 if (decision_logic_->CngRfc3389On() || last_mode_ == kModeRfc3389Cng) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000924 // Because of timestamp peculiarities, we have to "manually" disallow using
925 // a CNG packet with the same timestamp as the one that was last played.
926 // This can happen when using redundancy and will cause the timing to shift.
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000927 while (header && decoder_database_->IsComfortNoise(header->payloadType) &&
928 (end_timestamp >= header->timestamp ||
929 end_timestamp + decision_logic_->generated_noise_samples() >
930 header->timestamp)) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000931 // Don't use this packet, discard it.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000932 if (packet_buffer_->DiscardNextPacket() != PacketBuffer::kOK) {
933 assert(false); // Must be ok by design.
934 }
935 // Check buffer again.
936 if (!new_codec_) {
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000937 packet_buffer_->DiscardOldPackets(end_timestamp, 5 * fs_hz_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000938 }
939 header = packet_buffer_->NextRtpHeader();
940 }
941 }
942
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +0000943 assert(expand_.get());
turaj@webrtc.org362a55e2013-09-20 16:25:28 +0000944 const int samples_left = static_cast<int>(sync_buffer_->FutureLength() -
945 expand_->overlap_length());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000946 if (last_mode_ == kModeAccelerateSuccess ||
947 last_mode_ == kModeAccelerateLowEnergy ||
948 last_mode_ == kModePreemptiveExpandSuccess ||
949 last_mode_ == kModePreemptiveExpandLowEnergy) {
950 // Subtract (samples_left + output_size_samples_) from sampleMemory.
951 decision_logic_->AddSampleMemory(-(samples_left + output_size_samples_));
952 }
953
954 // Check if it is time to play a DTMF event.
955 if (dtmf_buffer_->GetEvent(end_timestamp +
956 decision_logic_->generated_noise_samples(),
957 dtmf_event)) {
958 *play_dtmf = true;
959 }
960
961 // Get instruction.
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +0000962 assert(sync_buffer_.get());
963 assert(expand_.get());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000964 *operation = decision_logic_->GetDecision(*sync_buffer_,
965 *expand_,
966 decoder_frame_length_,
967 header,
968 last_mode_,
969 *play_dtmf,
970 &reset_decoder_);
971
972 // Check if we already have enough samples in the |sync_buffer_|. If so,
973 // change decision to normal, unless the decision was merge, accelerate, or
974 // preemptive expand.
Henrik Lundincf808d22015-05-27 14:33:29 +0200975 if (samples_left >= output_size_samples_ && *operation != kMerge &&
976 *operation != kAccelerate && *operation != kFastAccelerate &&
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000977 *operation != kPreemptiveExpand) {
978 *operation = kNormal;
979 return 0;
980 }
981
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +0000982 decision_logic_->ExpandDecision(*operation);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000983
984 // Check conditions for reset.
985 if (new_codec_ || *operation == kUndefined) {
986 // The only valid reason to get kUndefined is that new_codec_ is set.
987 assert(new_codec_);
turaj@webrtc.org4d06db52013-03-27 18:31:42 +0000988 if (*play_dtmf && !header) {
989 timestamp_ = dtmf_event->timestamp;
990 } else {
991 assert(header);
992 if (!header) {
993 LOG_F(LS_ERROR) << "Packet missing where it shouldn't.";
994 return -1;
995 }
996 timestamp_ = header->timestamp;
997 if (*operation == kRfc3389CngNoPacket
998#ifndef LEGACY_BITEXACT
999 // Without this check, it can happen that a non-CNG packet is sent to
1000 // the CNG decoder as if it was a SID frame. This is clearly a bug,
1001 // but is kept for now to maintain bit-exactness with the test
1002 // vectors.
1003 && decoder_database_->IsComfortNoise(header->payloadType)
1004#endif
1005 ) {
1006 // Change decision to CNG packet, since we do have a CNG packet, but it
1007 // was considered too early to use. Now, use it anyway.
1008 *operation = kRfc3389Cng;
1009 } else if (*operation != kRfc3389Cng) {
1010 *operation = kNormal;
1011 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001012 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001013 // Adjust |sync_buffer_| timestamp before setting |end_timestamp| to the
1014 // new value.
1015 sync_buffer_->IncreaseEndTimestamp(timestamp_ - end_timestamp);
turaj@webrtc.org4d06db52013-03-27 18:31:42 +00001016 end_timestamp = timestamp_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001017 new_codec_ = false;
1018 decision_logic_->SoftReset();
1019 buffer_level_filter_->Reset();
1020 delay_manager_->Reset();
1021 stats_.ResetMcu();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001022 }
1023
1024 int required_samples = output_size_samples_;
1025 const int samples_10_ms = 80 * fs_mult_;
1026 const int samples_20_ms = 2 * samples_10_ms;
1027 const int samples_30_ms = 3 * samples_10_ms;
1028
1029 switch (*operation) {
1030 case kExpand: {
1031 timestamp_ = end_timestamp;
1032 return 0;
1033 }
1034 case kRfc3389CngNoPacket:
1035 case kCodecInternalCng: {
1036 return 0;
1037 }
1038 case kDtmf: {
1039 // TODO(hlundin): Write test for this.
1040 // Update timestamp.
1041 timestamp_ = end_timestamp;
1042 if (decision_logic_->generated_noise_samples() > 0 &&
1043 last_mode_ != kModeDtmf) {
1044 // Make a jump in timestamp due to the recently played comfort noise.
1045 uint32_t timestamp_jump = decision_logic_->generated_noise_samples();
1046 sync_buffer_->IncreaseEndTimestamp(timestamp_jump);
1047 timestamp_ += timestamp_jump;
1048 }
1049 decision_logic_->set_generated_noise_samples(0);
1050 return 0;
1051 }
Henrik Lundincf808d22015-05-27 14:33:29 +02001052 case kAccelerate:
1053 case kFastAccelerate: {
1054 // In order to do an accelerate we need at least 30 ms of audio data.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001055 if (samples_left >= samples_30_ms) {
1056 // Already have enough data, so we do not need to extract any more.
1057 decision_logic_->set_sample_memory(samples_left);
1058 decision_logic_->set_prev_time_scale(true);
1059 return 0;
1060 } else if (samples_left >= samples_10_ms &&
1061 decoder_frame_length_ >= samples_30_ms) {
1062 // Avoid decoding more data as it might overflow the playout buffer.
1063 *operation = kNormal;
1064 return 0;
1065 } else if (samples_left < samples_20_ms &&
1066 decoder_frame_length_ < samples_30_ms) {
1067 // Build up decoded data by decoding at least 20 ms of audio data. Do
1068 // not perform accelerate yet, but wait until we only need to do one
1069 // decoding.
1070 required_samples = 2 * output_size_samples_;
1071 *operation = kNormal;
1072 }
1073 // If none of the above is true, we have one of two possible situations:
1074 // (1) 20 ms <= samples_left < 30 ms and decoder_frame_length_ < 30 ms; or
1075 // (2) samples_left < 10 ms and decoder_frame_length_ >= 30 ms.
1076 // In either case, we move on with the accelerate decision, and decode one
1077 // frame now.
1078 break;
1079 }
1080 case kPreemptiveExpand: {
1081 // In order to do a preemptive expand we need at least 30 ms of decoded
1082 // audio data.
1083 if ((samples_left >= samples_30_ms) ||
1084 (samples_left >= samples_10_ms &&
1085 decoder_frame_length_ >= samples_30_ms)) {
1086 // Already have enough data, so we do not need to extract any more.
1087 // Or, avoid decoding more data as it might overflow the playout buffer.
1088 // Still try preemptive expand, though.
1089 decision_logic_->set_sample_memory(samples_left);
1090 decision_logic_->set_prev_time_scale(true);
1091 return 0;
1092 }
1093 if (samples_left < samples_20_ms &&
1094 decoder_frame_length_ < samples_30_ms) {
1095 // Build up decoded data by decoding at least 20 ms of audio data.
1096 // Still try to perform preemptive expand.
1097 required_samples = 2 * output_size_samples_;
1098 }
1099 // Move on with the preemptive expand decision.
1100 break;
1101 }
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001102 case kMerge: {
1103 required_samples =
1104 std::max(merge_->RequiredFutureSamples(), required_samples);
1105 break;
1106 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001107 default: {
1108 // Do nothing.
1109 }
1110 }
1111
1112 // Get packets from buffer.
1113 int extracted_samples = 0;
1114 if (header &&
1115 *operation != kAlternativePlc &&
1116 *operation != kAlternativePlcIncreaseTimestamp &&
1117 *operation != kAudioRepetition &&
1118 *operation != kAudioRepetitionIncreaseTimestamp) {
1119 sync_buffer_->IncreaseEndTimestamp(header->timestamp - end_timestamp);
1120 if (decision_logic_->CngOff()) {
1121 // Adjustment of timestamp only corresponds to an actual packet loss
1122 // if comfort noise is not played. If comfort noise was just played,
1123 // this adjustment of timestamp is only done to get back in sync with the
1124 // stream timestamp; no loss to report.
1125 stats_.LostSamples(header->timestamp - end_timestamp);
1126 }
1127
1128 if (*operation != kRfc3389Cng) {
1129 // We are about to decode and use a non-CNG packet.
1130 decision_logic_->SetCngOff();
1131 }
1132 // Reset CNG timestamp as a new packet will be delivered.
1133 // (Also if this is a CNG packet, since playedOutTS is updated.)
1134 decision_logic_->set_generated_noise_samples(0);
1135
1136 extracted_samples = ExtractPackets(required_samples, packet_list);
1137 if (extracted_samples < 0) {
1138 LOG_F(LS_WARNING) << "Failed to extract packets from buffer.";
1139 return kPacketBufferCorruption;
1140 }
1141 }
1142
Henrik Lundincf808d22015-05-27 14:33:29 +02001143 if (*operation == kAccelerate || *operation == kFastAccelerate ||
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001144 *operation == kPreemptiveExpand) {
1145 decision_logic_->set_sample_memory(samples_left + extracted_samples);
1146 decision_logic_->set_prev_time_scale(true);
1147 }
1148
Henrik Lundincf808d22015-05-27 14:33:29 +02001149 if (*operation == kAccelerate || *operation == kFastAccelerate) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001150 // Check that we have enough data (30ms) to do accelerate.
1151 if (extracted_samples + samples_left < samples_30_ms) {
1152 // TODO(hlundin): Write test for this.
1153 // Not enough, do normal operation instead.
1154 *operation = kNormal;
1155 }
1156 }
1157
1158 timestamp_ = end_timestamp;
1159 return 0;
1160}
1161
1162int NetEqImpl::Decode(PacketList* packet_list, Operations* operation,
1163 int* decoded_length,
1164 AudioDecoder::SpeechType* speech_type) {
1165 *speech_type = AudioDecoder::kSpeech;
1166 AudioDecoder* decoder = NULL;
1167 if (!packet_list->empty()) {
1168 const Packet* packet = packet_list->front();
pkasting@chromium.org0e81fdf2015-02-02 23:54:03 +00001169 uint8_t payload_type = packet->header.payloadType;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001170 if (!decoder_database_->IsComfortNoise(payload_type)) {
1171 decoder = decoder_database_->GetDecoder(payload_type);
1172 assert(decoder);
1173 if (!decoder) {
pkasting@chromium.org0e81fdf2015-02-02 23:54:03 +00001174 LOG_FERR1(LS_WARNING, GetDecoder, static_cast<int>(payload_type));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001175 PacketBuffer::DeleteAllPackets(packet_list);
1176 return kDecoderNotFound;
1177 }
1178 bool decoder_changed;
1179 decoder_database_->SetActiveDecoder(payload_type, &decoder_changed);
1180 if (decoder_changed) {
1181 // We have a new decoder. Re-init some values.
1182 const DecoderDatabase::DecoderInfo* decoder_info = decoder_database_
1183 ->GetDecoderInfo(payload_type);
1184 assert(decoder_info);
1185 if (!decoder_info) {
pkasting@chromium.org0e81fdf2015-02-02 23:54:03 +00001186 LOG_FERR1(LS_WARNING, GetDecoderInfo, static_cast<int>(payload_type));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001187 PacketBuffer::DeleteAllPackets(packet_list);
1188 return kDecoderNotFound;
1189 }
tina.legrand@webrtc.orgba5a6c32014-03-23 09:58:48 +00001190 // If sampling rate or number of channels has changed, we need to make
1191 // a reset.
turaj@webrtc.orga6101d72013-10-01 22:01:09 +00001192 if (decoder_info->fs_hz != fs_hz_ ||
henrik.lundin@webrtc.org6dba1eb2015-03-18 09:47:08 +00001193 decoder->Channels() != algorithm_buffer_->Channels()) {
tina.legrand@webrtc.orgba5a6c32014-03-23 09:58:48 +00001194 // TODO(tlegrand): Add unittest to cover this event.
henrik.lundin@webrtc.org6dba1eb2015-03-18 09:47:08 +00001195 SetSampleRateAndChannels(decoder_info->fs_hz, decoder->Channels());
turaj@webrtc.orga6101d72013-10-01 22:01:09 +00001196 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001197 sync_buffer_->set_end_timestamp(timestamp_);
1198 playout_timestamp_ = timestamp_;
1199 }
1200 }
1201 }
1202
1203 if (reset_decoder_) {
1204 // TODO(hlundin): Write test for this.
1205 // Reset decoder.
1206 if (decoder) {
1207 decoder->Init();
1208 }
1209 // Reset comfort noise decoder.
1210 AudioDecoder* cng_decoder = decoder_database_->GetActiveCngDecoder();
1211 if (cng_decoder) {
1212 cng_decoder->Init();
1213 }
1214 reset_decoder_ = false;
1215 }
1216
1217#ifdef LEGACY_BITEXACT
1218 // Due to a bug in old SignalMCU, it could happen that CNG operation was
1219 // decided, but a speech packet was provided. The speech packet will be used
1220 // to update the comfort noise decoder, as if it was a SID frame, which is
1221 // clearly wrong.
1222 if (*operation == kRfc3389Cng) {
1223 return 0;
1224 }
1225#endif
1226
1227 *decoded_length = 0;
1228 // Update codec-internal PLC state.
1229 if ((*operation == kMerge) && decoder && decoder->HasDecodePlc()) {
1230 decoder->DecodePlc(1, &decoded_buffer_[*decoded_length]);
1231 }
1232
1233 int return_value = DecodeLoop(packet_list, operation, decoder,
1234 decoded_length, speech_type);
1235
1236 if (*decoded_length < 0) {
1237 // Error returned from the decoder.
1238 *decoded_length = 0;
1239 sync_buffer_->IncreaseEndTimestamp(decoder_frame_length_);
1240 int error_code = 0;
1241 if (decoder)
1242 error_code = decoder->ErrorCode();
1243 if (error_code != 0) {
1244 // Got some error code from the decoder.
1245 decoder_error_code_ = error_code;
1246 return_value = kDecoderErrorCode;
1247 } else {
1248 // Decoder does not implement error codes. Return generic error.
1249 return_value = kOtherDecoderError;
1250 }
1251 LOG_FERR2(LS_WARNING, DecodeLoop, error_code, packet_list->size());
1252 *operation = kExpand; // Do expansion to get data instead.
1253 }
1254 if (*speech_type != AudioDecoder::kComfortNoise) {
1255 // Don't increment timestamp if codec returned CNG speech type
1256 // since in this case, the we will increment the CNGplayedTS counter.
1257 // Increase with number of samples per channel.
1258 assert(*decoded_length == 0 ||
henrik.lundin@webrtc.org6dba1eb2015-03-18 09:47:08 +00001259 (decoder && decoder->Channels() == sync_buffer_->Channels()));
turaj@webrtc.org362a55e2013-09-20 16:25:28 +00001260 sync_buffer_->IncreaseEndTimestamp(
1261 *decoded_length / static_cast<int>(sync_buffer_->Channels()));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001262 }
1263 return return_value;
1264}
1265
1266int NetEqImpl::DecodeLoop(PacketList* packet_list, Operations* operation,
1267 AudioDecoder* decoder, int* decoded_length,
1268 AudioDecoder::SpeechType* speech_type) {
1269 Packet* packet = NULL;
1270 if (!packet_list->empty()) {
1271 packet = packet_list->front();
1272 }
1273 // Do decoding.
1274 while (packet &&
1275 !decoder_database_->IsComfortNoise(packet->header.payloadType)) {
1276 assert(decoder); // At this point, we must have a decoder object.
1277 // The number of channels in the |sync_buffer_| should be the same as the
1278 // number decoder channels.
henrik.lundin@webrtc.org6dba1eb2015-03-18 09:47:08 +00001279 assert(sync_buffer_->Channels() == decoder->Channels());
1280 assert(decoded_buffer_length_ >= kMaxFrameSize * decoder->Channels());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001281 assert(*operation == kNormal || *operation == kAccelerate ||
Henrik Lundincf808d22015-05-27 14:33:29 +02001282 *operation == kFastAccelerate || *operation == kMerge ||
1283 *operation == kPreemptiveExpand);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001284 packet_list->pop_front();
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001285 size_t payload_length = packet->payload_length;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001286 int16_t decode_length;
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001287 if (packet->sync_packet) {
1288 // Decode to silence with the same frame size as the last decode.
1289 LOG(LS_VERBOSE) << "Decoding sync-packet: " <<
1290 " ts=" << packet->header.timestamp <<
1291 ", sn=" << packet->header.sequenceNumber <<
1292 ", pt=" << static_cast<int>(packet->header.payloadType) <<
1293 ", ssrc=" << packet->header.ssrc <<
1294 ", len=" << packet->payload_length;
henrik.lundin@webrtc.org6dba1eb2015-03-18 09:47:08 +00001295 memset(&decoded_buffer_[*decoded_length], 0,
1296 decoder_frame_length_ * decoder->Channels() *
1297 sizeof(decoded_buffer_[0]));
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001298 decode_length = decoder_frame_length_;
1299 } else if (!packet->primary) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001300 // This is a redundant payload; call the special decoder method.
turaj@webrtc.org0c0fae82013-09-25 17:42:17 +00001301 LOG(LS_VERBOSE) << "Decoding packet (redundant):" <<
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001302 " ts=" << packet->header.timestamp <<
1303 ", sn=" << packet->header.sequenceNumber <<
1304 ", pt=" << static_cast<int>(packet->header.payloadType) <<
1305 ", ssrc=" << packet->header.ssrc <<
1306 ", len=" << packet->payload_length;
1307 decode_length = decoder->DecodeRedundant(
henrik.lundin@webrtc.org1eda4e32015-02-25 10:02:29 +00001308 packet->payload, packet->payload_length, fs_hz_,
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +00001309 (decoded_buffer_length_ - *decoded_length) * sizeof(int16_t),
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001310 &decoded_buffer_[*decoded_length], speech_type);
1311 } else {
turaj@webrtc.org0c0fae82013-09-25 17:42:17 +00001312 LOG(LS_VERBOSE) << "Decoding packet: ts=" << packet->header.timestamp <<
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001313 ", sn=" << packet->header.sequenceNumber <<
1314 ", pt=" << static_cast<int>(packet->header.payloadType) <<
1315 ", ssrc=" << packet->header.ssrc <<
1316 ", len=" << packet->payload_length;
henrik.lundin@webrtc.org1eda4e32015-02-25 10:02:29 +00001317 decode_length =
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +00001318 decoder->Decode(
1319 packet->payload, packet->payload_length, fs_hz_,
1320 (decoded_buffer_length_ - *decoded_length) * sizeof(int16_t),
1321 &decoded_buffer_[*decoded_length], speech_type);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001322 }
1323
1324 delete[] packet->payload;
1325 delete packet;
turaj@webrtc.org58cd3162013-10-31 15:15:55 +00001326 packet = NULL;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001327 if (decode_length > 0) {
1328 *decoded_length += decode_length;
1329 // Update |decoder_frame_length_| with number of samples per channel.
henrik.lundin@webrtc.org6dba1eb2015-03-18 09:47:08 +00001330 decoder_frame_length_ =
1331 decode_length / static_cast<int>(decoder->Channels());
1332 LOG(LS_VERBOSE) << "Decoded " << decode_length << " samples ("
1333 << decoder->Channels() << " channel(s) -> "
1334 << decoder_frame_length_ << " samples per channel)";
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001335 } else if (decode_length < 0) {
1336 // Error.
henrik.lundin@webrtc.org63464a92013-01-30 09:41:56 +00001337 LOG_FERR2(LS_WARNING, Decode, decode_length, payload_length);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001338 *decoded_length = -1;
1339 PacketBuffer::DeleteAllPackets(packet_list);
1340 break;
1341 }
1342 if (*decoded_length > static_cast<int>(decoded_buffer_length_)) {
1343 // Guard against overflow.
1344 LOG_F(LS_WARNING) << "Decoded too much.";
1345 PacketBuffer::DeleteAllPackets(packet_list);
1346 return kDecodedTooMuch;
1347 }
1348 if (!packet_list->empty()) {
1349 packet = packet_list->front();
1350 } else {
1351 packet = NULL;
1352 }
1353 } // End of decode loop.
1354
turaj@webrtc.org58cd3162013-10-31 15:15:55 +00001355 // If the list is not empty at this point, either a decoding error terminated
1356 // the while-loop, or list must hold exactly one CNG packet.
1357 assert(packet_list->empty() || *decoded_length < 0 ||
1358 (packet_list->size() == 1 && packet &&
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001359 decoder_database_->IsComfortNoise(packet->header.payloadType)));
1360 return 0;
1361}
1362
1363void NetEqImpl::DoNormal(const int16_t* decoded_buffer, size_t decoded_length,
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001364 AudioDecoder::SpeechType speech_type, bool play_dtmf) {
henrik.lundin@webrtc.org40d3fc62013-09-18 12:19:50 +00001365 assert(normal_.get());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001366 assert(mute_factor_array_.get());
henrik.lundin@webrtc.org40d3fc62013-09-18 12:19:50 +00001367 normal_->Process(decoded_buffer, decoded_length, last_mode_,
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +00001368 mute_factor_array_.get(), algorithm_buffer_.get());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001369 if (decoded_length != 0) {
1370 last_mode_ = kModeNormal;
1371 }
1372
1373 // If last packet was decoded as an inband CNG, set mode to CNG instead.
1374 if ((speech_type == AudioDecoder::kComfortNoise)
1375 || ((last_mode_ == kModeCodecInternalCng)
1376 && (decoded_length == 0))) {
1377 // TODO(hlundin): Remove second part of || statement above.
1378 last_mode_ = kModeCodecInternalCng;
1379 }
1380
1381 if (!play_dtmf) {
1382 dtmf_tone_generator_->Reset();
1383 }
1384}
1385
1386void NetEqImpl::DoMerge(int16_t* decoded_buffer, size_t decoded_length,
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001387 AudioDecoder::SpeechType speech_type, bool play_dtmf) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001388 assert(mute_factor_array_.get());
henrik.lundin@webrtc.org40d3fc62013-09-18 12:19:50 +00001389 assert(merge_.get());
1390 int new_length = merge_->Process(decoded_buffer, decoded_length,
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +00001391 mute_factor_array_.get(),
1392 algorithm_buffer_.get());
minyue@webrtc.orgc11348b2015-02-10 08:35:38 +00001393 int expand_length_correction = new_length -
1394 static_cast<int>(decoded_length / algorithm_buffer_->Channels());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001395
1396 // Update in-call and post-call statistics.
1397 if (expand_->MuteFactor(0) == 0) {
1398 // Expand generates only noise.
minyue@webrtc.orgc11348b2015-02-10 08:35:38 +00001399 stats_.ExpandedNoiseSamples(expand_length_correction);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001400 } else {
1401 // Expansion generates more than only noise.
minyue@webrtc.orgc11348b2015-02-10 08:35:38 +00001402 stats_.ExpandedVoiceSamples(expand_length_correction);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001403 }
1404
1405 last_mode_ = kModeMerge;
1406 // If last packet was decoded as an inband CNG, set mode to CNG instead.
1407 if (speech_type == AudioDecoder::kComfortNoise) {
1408 last_mode_ = kModeCodecInternalCng;
1409 }
1410 expand_->Reset();
1411 if (!play_dtmf) {
1412 dtmf_tone_generator_->Reset();
1413 }
1414}
1415
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001416int NetEqImpl::DoExpand(bool play_dtmf) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001417 while ((sync_buffer_->FutureLength() - expand_->overlap_length()) <
1418 static_cast<size_t>(output_size_samples_)) {
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001419 algorithm_buffer_->Clear();
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +00001420 int return_value = expand_->Process(algorithm_buffer_.get());
turaj@webrtc.org362a55e2013-09-20 16:25:28 +00001421 int length = static_cast<int>(algorithm_buffer_->Size());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001422
1423 // Update in-call and post-call statistics.
1424 if (expand_->MuteFactor(0) == 0) {
1425 // Expand operation generates only noise.
1426 stats_.ExpandedNoiseSamples(length);
1427 } else {
1428 // Expand operation generates more than only noise.
1429 stats_.ExpandedVoiceSamples(length);
1430 }
1431
1432 last_mode_ = kModeExpand;
1433
1434 if (return_value < 0) {
1435 return return_value;
1436 }
1437
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001438 sync_buffer_->PushBack(*algorithm_buffer_);
1439 algorithm_buffer_->Clear();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001440 }
1441 if (!play_dtmf) {
1442 dtmf_tone_generator_->Reset();
1443 }
1444 return 0;
1445}
1446
Henrik Lundincf808d22015-05-27 14:33:29 +02001447int NetEqImpl::DoAccelerate(int16_t* decoded_buffer,
1448 size_t decoded_length,
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001449 AudioDecoder::SpeechType speech_type,
Henrik Lundincf808d22015-05-27 14:33:29 +02001450 bool play_dtmf,
1451 bool fast_accelerate) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001452 const size_t required_samples = 240 * fs_mult_; // Must have 30 ms.
turaj@webrtc.org362a55e2013-09-20 16:25:28 +00001453 size_t borrowed_samples_per_channel = 0;
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001454 size_t num_channels = algorithm_buffer_->Channels();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001455 size_t decoded_length_per_channel = decoded_length / num_channels;
1456 if (decoded_length_per_channel < required_samples) {
1457 // Must move data from the |sync_buffer_| in order to get 30 ms.
turaj@webrtc.org362a55e2013-09-20 16:25:28 +00001458 borrowed_samples_per_channel = static_cast<int>(required_samples -
1459 decoded_length_per_channel);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001460 memmove(&decoded_buffer[borrowed_samples_per_channel * num_channels],
1461 decoded_buffer,
1462 sizeof(int16_t) * decoded_length);
1463 sync_buffer_->ReadInterleavedFromEnd(borrowed_samples_per_channel,
1464 decoded_buffer);
1465 decoded_length = required_samples * num_channels;
1466 }
1467
1468 int16_t samples_removed;
Henrik Lundincf808d22015-05-27 14:33:29 +02001469 Accelerate::ReturnCodes return_code =
1470 accelerate_->Process(decoded_buffer, decoded_length, fast_accelerate,
1471 algorithm_buffer_.get(), &samples_removed);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001472 stats_.AcceleratedSamples(samples_removed);
1473 switch (return_code) {
1474 case Accelerate::kSuccess:
1475 last_mode_ = kModeAccelerateSuccess;
1476 break;
1477 case Accelerate::kSuccessLowEnergy:
1478 last_mode_ = kModeAccelerateLowEnergy;
1479 break;
1480 case Accelerate::kNoStretch:
1481 last_mode_ = kModeAccelerateFail;
1482 break;
1483 case Accelerate::kError:
1484 // TODO(hlundin): Map to kModeError instead?
1485 last_mode_ = kModeAccelerateFail;
1486 return kAccelerateError;
1487 }
1488
1489 if (borrowed_samples_per_channel > 0) {
1490 // Copy borrowed samples back to the |sync_buffer_|.
turaj@webrtc.org362a55e2013-09-20 16:25:28 +00001491 size_t length = algorithm_buffer_->Size();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001492 if (length < borrowed_samples_per_channel) {
1493 // This destroys the beginning of the buffer, but will not cause any
1494 // problems.
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001495 sync_buffer_->ReplaceAtIndex(*algorithm_buffer_,
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001496 sync_buffer_->Size() -
1497 borrowed_samples_per_channel);
1498 sync_buffer_->PushFrontZeros(borrowed_samples_per_channel - length);
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001499 algorithm_buffer_->PopFront(length);
1500 assert(algorithm_buffer_->Empty());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001501 } else {
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001502 sync_buffer_->ReplaceAtIndex(*algorithm_buffer_,
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001503 borrowed_samples_per_channel,
1504 sync_buffer_->Size() -
1505 borrowed_samples_per_channel);
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001506 algorithm_buffer_->PopFront(borrowed_samples_per_channel);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001507 }
1508 }
1509
1510 // If last packet was decoded as an inband CNG, set mode to CNG instead.
1511 if (speech_type == AudioDecoder::kComfortNoise) {
1512 last_mode_ = kModeCodecInternalCng;
1513 }
1514 if (!play_dtmf) {
1515 dtmf_tone_generator_->Reset();
1516 }
1517 expand_->Reset();
1518 return 0;
1519}
1520
1521int NetEqImpl::DoPreemptiveExpand(int16_t* decoded_buffer,
1522 size_t decoded_length,
1523 AudioDecoder::SpeechType speech_type,
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001524 bool play_dtmf) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001525 const size_t required_samples = 240 * fs_mult_; // Must have 30 ms.
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001526 size_t num_channels = algorithm_buffer_->Channels();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001527 int borrowed_samples_per_channel = 0;
1528 int old_borrowed_samples_per_channel = 0;
1529 size_t decoded_length_per_channel = decoded_length / num_channels;
1530 if (decoded_length_per_channel < required_samples) {
1531 // Must move data from the |sync_buffer_| in order to get 30 ms.
turaj@webrtc.org362a55e2013-09-20 16:25:28 +00001532 borrowed_samples_per_channel = static_cast<int>(required_samples -
1533 decoded_length_per_channel);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001534 // Calculate how many of these were already played out.
turaj@webrtc.org362a55e2013-09-20 16:25:28 +00001535 old_borrowed_samples_per_channel = static_cast<int>(
1536 borrowed_samples_per_channel - sync_buffer_->FutureLength());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001537 old_borrowed_samples_per_channel = std::max(
1538 0, old_borrowed_samples_per_channel);
1539 memmove(&decoded_buffer[borrowed_samples_per_channel * num_channels],
1540 decoded_buffer,
1541 sizeof(int16_t) * decoded_length);
1542 sync_buffer_->ReadInterleavedFromEnd(borrowed_samples_per_channel,
1543 decoded_buffer);
1544 decoded_length = required_samples * num_channels;
1545 }
1546
1547 int16_t samples_added;
henrik.lundin@webrtc.org40d3fc62013-09-18 12:19:50 +00001548 PreemptiveExpand::ReturnCodes return_code = preemptive_expand_->Process(
turaj@webrtc.org362a55e2013-09-20 16:25:28 +00001549 decoded_buffer, static_cast<int>(decoded_length),
1550 old_borrowed_samples_per_channel,
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +00001551 algorithm_buffer_.get(), &samples_added);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001552 stats_.PreemptiveExpandedSamples(samples_added);
1553 switch (return_code) {
1554 case PreemptiveExpand::kSuccess:
1555 last_mode_ = kModePreemptiveExpandSuccess;
1556 break;
1557 case PreemptiveExpand::kSuccessLowEnergy:
1558 last_mode_ = kModePreemptiveExpandLowEnergy;
1559 break;
1560 case PreemptiveExpand::kNoStretch:
1561 last_mode_ = kModePreemptiveExpandFail;
1562 break;
1563 case PreemptiveExpand::kError:
1564 // TODO(hlundin): Map to kModeError instead?
1565 last_mode_ = kModePreemptiveExpandFail;
1566 return kPreemptiveExpandError;
1567 }
1568
1569 if (borrowed_samples_per_channel > 0) {
1570 // Copy borrowed samples back to the |sync_buffer_|.
1571 sync_buffer_->ReplaceAtIndex(
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001572 *algorithm_buffer_, borrowed_samples_per_channel,
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001573 sync_buffer_->Size() - borrowed_samples_per_channel);
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001574 algorithm_buffer_->PopFront(borrowed_samples_per_channel);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001575 }
1576
1577 // If last packet was decoded as an inband CNG, set mode to CNG instead.
1578 if (speech_type == AudioDecoder::kComfortNoise) {
1579 last_mode_ = kModeCodecInternalCng;
1580 }
1581 if (!play_dtmf) {
1582 dtmf_tone_generator_->Reset();
1583 }
1584 expand_->Reset();
1585 return 0;
1586}
1587
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001588int NetEqImpl::DoRfc3389Cng(PacketList* packet_list, bool play_dtmf) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001589 if (!packet_list->empty()) {
1590 // Must have exactly one SID frame at this point.
1591 assert(packet_list->size() == 1);
1592 Packet* packet = packet_list->front();
1593 packet_list->pop_front();
henrik.lundin@webrtc.org73deaad2013-01-31 13:32:51 +00001594 if (!decoder_database_->IsComfortNoise(packet->header.payloadType)) {
1595#ifdef LEGACY_BITEXACT
1596 // This can happen due to a bug in GetDecision. Change the payload type
1597 // to a CNG type, and move on. Note that this means that we are in fact
1598 // sending a non-CNG payload to the comfort noise decoder for decoding.
1599 // Clearly wrong, but will maintain bit-exactness with legacy.
1600 if (fs_hz_ == 8000) {
1601 packet->header.payloadType =
1602 decoder_database_->GetRtpPayloadType(kDecoderCNGnb);
1603 } else if (fs_hz_ == 16000) {
1604 packet->header.payloadType =
1605 decoder_database_->GetRtpPayloadType(kDecoderCNGwb);
1606 } else if (fs_hz_ == 32000) {
1607 packet->header.payloadType =
1608 decoder_database_->GetRtpPayloadType(kDecoderCNGswb32kHz);
1609 } else if (fs_hz_ == 48000) {
1610 packet->header.payloadType =
1611 decoder_database_->GetRtpPayloadType(kDecoderCNGswb48kHz);
1612 }
1613 assert(decoder_database_->IsComfortNoise(packet->header.payloadType));
1614#else
1615 LOG(LS_ERROR) << "Trying to decode non-CNG payload as CNG.";
1616 return kOtherError;
1617#endif
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001618 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001619 // UpdateParameters() deletes |packet|.
1620 if (comfort_noise_->UpdateParameters(packet) ==
1621 ComfortNoise::kInternalError) {
1622 LOG_FERR0(LS_WARNING, UpdateParameters);
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001623 algorithm_buffer_->Zeros(output_size_samples_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001624 return -comfort_noise_->internal_error_code();
1625 }
1626 }
1627 int cn_return = comfort_noise_->Generate(output_size_samples_,
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +00001628 algorithm_buffer_.get());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001629 expand_->Reset();
1630 last_mode_ = kModeRfc3389Cng;
1631 if (!play_dtmf) {
1632 dtmf_tone_generator_->Reset();
1633 }
1634 if (cn_return == ComfortNoise::kInternalError) {
1635 LOG_FERR1(LS_WARNING, comfort_noise_->Generate, cn_return);
1636 decoder_error_code_ = comfort_noise_->internal_error_code();
1637 return kComfortNoiseErrorCode;
1638 } else if (cn_return == ComfortNoise::kUnknownPayloadType) {
1639 LOG_FERR1(LS_WARNING, comfort_noise_->Generate, cn_return);
1640 return kUnknownRtpPayloadType;
1641 }
1642 return 0;
1643}
1644
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001645void NetEqImpl::DoCodecInternalCng() {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001646 int length = 0;
1647 // TODO(hlundin): Will probably need a longer buffer for multi-channel.
1648 int16_t decoded_buffer[kMaxFrameSize];
1649 AudioDecoder* decoder = decoder_database_->GetActiveDecoder();
1650 if (decoder) {
1651 const uint8_t* dummy_payload = NULL;
1652 AudioDecoder::SpeechType speech_type;
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +00001653 length = decoder->Decode(
1654 dummy_payload, 0, fs_hz_, kMaxFrameSize * sizeof(int16_t),
1655 decoded_buffer, &speech_type);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001656 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001657 assert(mute_factor_array_.get());
henrik.lundin@webrtc.org40d3fc62013-09-18 12:19:50 +00001658 normal_->Process(decoded_buffer, length, last_mode_, mute_factor_array_.get(),
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +00001659 algorithm_buffer_.get());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001660 last_mode_ = kModeCodecInternalCng;
1661 expand_->Reset();
1662}
1663
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001664int NetEqImpl::DoDtmf(const DtmfEvent& dtmf_event, bool* play_dtmf) {
turaj@webrtc.org4d06db52013-03-27 18:31:42 +00001665 // This block of the code and the block further down, handling |dtmf_switch|
1666 // are commented out. Otherwise playing out-of-band DTMF would fail in VoE
1667 // test, DtmfTest.ManualSuccessfullySendsOutOfBandTelephoneEvents. This is
1668 // equivalent to |dtmf_switch| always be false.
1669 //
1670 // See http://webrtc-codereview.appspot.com/1195004/ for discussion
1671 // On this issue. This change might cause some glitches at the point of
1672 // switch from audio to DTMF. Issue 1545 is filed to track this.
1673 //
1674 // bool dtmf_switch = false;
1675 // if ((last_mode_ != kModeDtmf) && dtmf_tone_generator_->initialized()) {
1676 // // Special case; see below.
1677 // // We must catch this before calling Generate, since |initialized| is
1678 // // modified in that call.
1679 // dtmf_switch = true;
1680 // }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001681
1682 int dtmf_return_value = 0;
1683 if (!dtmf_tone_generator_->initialized()) {
1684 // Initialize if not already done.
1685 dtmf_return_value = dtmf_tone_generator_->Init(fs_hz_, dtmf_event.event_no,
1686 dtmf_event.volume);
1687 }
turaj@webrtc.org4d06db52013-03-27 18:31:42 +00001688
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001689 if (dtmf_return_value == 0) {
1690 // Generate DTMF signal.
1691 dtmf_return_value = dtmf_tone_generator_->Generate(output_size_samples_,
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +00001692 algorithm_buffer_.get());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001693 }
turaj@webrtc.org4d06db52013-03-27 18:31:42 +00001694
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001695 if (dtmf_return_value < 0) {
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001696 algorithm_buffer_->Zeros(output_size_samples_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001697 return dtmf_return_value;
1698 }
1699
turaj@webrtc.org4d06db52013-03-27 18:31:42 +00001700 // if (dtmf_switch) {
1701 // // This is the special case where the previous operation was DTMF
1702 // // overdub, but the current instruction is "regular" DTMF. We must make
1703 // // sure that the DTMF does not have any discontinuities. The first DTMF
1704 // // sample that we generate now must be played out immediately, therefore
1705 // // it must be copied to the speech buffer.
1706 // // TODO(hlundin): This code seems incorrect. (Legacy.) Write test and
1707 // // verify correct operation.
1708 // assert(false);
1709 // // Must generate enough data to replace all of the |sync_buffer_|
1710 // // "future".
1711 // int required_length = sync_buffer_->FutureLength();
1712 // assert(dtmf_tone_generator_->initialized());
1713 // dtmf_return_value = dtmf_tone_generator_->Generate(required_length,
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001714 // algorithm_buffer_);
1715 // assert((size_t) required_length == algorithm_buffer_->Size());
turaj@webrtc.org4d06db52013-03-27 18:31:42 +00001716 // if (dtmf_return_value < 0) {
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001717 // algorithm_buffer_->Zeros(output_size_samples_);
turaj@webrtc.org4d06db52013-03-27 18:31:42 +00001718 // return dtmf_return_value;
1719 // }
1720 //
1721 // // Overwrite the "future" part of the speech buffer with the new DTMF
1722 // // data.
1723 // // TODO(hlundin): It seems that this overwriting has gone lost.
1724 // // Not adapted for multi-channel yet.
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001725 // assert(algorithm_buffer_->Channels() == 1);
1726 // if (algorithm_buffer_->Channels() != 1) {
turaj@webrtc.org4d06db52013-03-27 18:31:42 +00001727 // LOG(LS_WARNING) << "DTMF not supported for more than one channel";
1728 // return kStereoNotSupported;
1729 // }
1730 // // Shuffle the remaining data to the beginning of algorithm buffer.
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001731 // algorithm_buffer_->PopFront(sync_buffer_->FutureLength());
turaj@webrtc.org4d06db52013-03-27 18:31:42 +00001732 // }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001733
1734 sync_buffer_->IncreaseEndTimestamp(output_size_samples_);
1735 expand_->Reset();
1736 last_mode_ = kModeDtmf;
1737
1738 // Set to false because the DTMF is already in the algorithm buffer.
1739 *play_dtmf = false;
1740 return 0;
1741}
1742
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001743void NetEqImpl::DoAlternativePlc(bool increase_timestamp) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001744 AudioDecoder* decoder = decoder_database_->GetActiveDecoder();
1745 int length;
1746 if (decoder && decoder->HasDecodePlc()) {
1747 // Use the decoder's packet-loss concealment.
1748 // TODO(hlundin): Will probably need a longer buffer for multi-channel.
1749 int16_t decoded_buffer[kMaxFrameSize];
1750 length = decoder->DecodePlc(1, decoded_buffer);
1751 if (length > 0) {
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001752 algorithm_buffer_->PushBackInterleaved(decoded_buffer, length);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001753 } else {
1754 length = 0;
1755 }
1756 } else {
1757 // Do simple zero-stuffing.
1758 length = output_size_samples_;
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001759 algorithm_buffer_->Zeros(length);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001760 // By not advancing the timestamp, NetEq inserts samples.
1761 stats_.AddZeros(length);
1762 }
1763 if (increase_timestamp) {
1764 sync_buffer_->IncreaseEndTimestamp(length);
1765 }
1766 expand_->Reset();
1767}
1768
1769int NetEqImpl::DtmfOverdub(const DtmfEvent& dtmf_event, size_t num_channels,
1770 int16_t* output) const {
1771 size_t out_index = 0;
1772 int overdub_length = output_size_samples_; // Default value.
1773
1774 if (sync_buffer_->dtmf_index() > sync_buffer_->next_index()) {
1775 // Special operation for transition from "DTMF only" to "DTMF overdub".
1776 out_index = std::min(
1777 sync_buffer_->dtmf_index() - sync_buffer_->next_index(),
1778 static_cast<size_t>(output_size_samples_));
turaj@webrtc.org362a55e2013-09-20 16:25:28 +00001779 overdub_length = output_size_samples_ - static_cast<int>(out_index);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001780 }
1781
henrik.lundin@webrtc.orgfd11bbf2013-09-30 20:38:44 +00001782 AudioMultiVector dtmf_output(num_channels);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001783 int dtmf_return_value = 0;
1784 if (!dtmf_tone_generator_->initialized()) {
1785 dtmf_return_value = dtmf_tone_generator_->Init(fs_hz_, dtmf_event.event_no,
1786 dtmf_event.volume);
1787 }
1788 if (dtmf_return_value == 0) {
1789 dtmf_return_value = dtmf_tone_generator_->Generate(overdub_length,
1790 &dtmf_output);
1791 assert((size_t) overdub_length == dtmf_output.Size());
1792 }
1793 dtmf_output.ReadInterleaved(overdub_length, &output[out_index]);
1794 return dtmf_return_value < 0 ? dtmf_return_value : 0;
1795}
1796
1797int NetEqImpl::ExtractPackets(int required_samples, PacketList* packet_list) {
1798 bool first_packet = true;
1799 uint8_t prev_payload_type = 0;
1800 uint32_t prev_timestamp = 0;
1801 uint16_t prev_sequence_number = 0;
1802 bool next_packet_available = false;
1803
henrik.lundin@webrtc.orge1d468c2013-01-30 07:37:20 +00001804 const RTPHeader* header = packet_buffer_->NextRtpHeader();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001805 assert(header);
1806 if (!header) {
1807 return -1;
1808 }
turaj@webrtc.org7df97062013-08-02 18:07:13 +00001809 uint32_t first_timestamp = header->timestamp;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001810 int extracted_samples = 0;
1811
1812 // Packet extraction loop.
1813 do {
1814 timestamp_ = header->timestamp;
1815 int discard_count = 0;
henrik.lundin@webrtc.orge1d468c2013-01-30 07:37:20 +00001816 Packet* packet = packet_buffer_->GetNextPacket(&discard_count);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001817 // |header| may be invalid after the |packet_buffer_| operation.
1818 header = NULL;
1819 if (!packet) {
1820 LOG_FERR1(LS_ERROR, GetNextPacket, discard_count) <<
1821 "Should always be able to extract a packet here";
1822 assert(false); // Should always be able to extract a packet here.
1823 return -1;
1824 }
1825 stats_.PacketsDiscarded(discard_count);
1826 // Store waiting time in ms; packets->waiting_time is in "output blocks".
1827 stats_.StoreWaitingTime(packet->waiting_time * kOutputSizeMs);
1828 assert(packet->payload_length > 0);
1829 packet_list->push_back(packet); // Store packet in list.
1830
1831 if (first_packet) {
1832 first_packet = false;
minyue@webrtc.orgd7301772013-08-29 00:58:14 +00001833 decoded_packet_sequence_number_ = prev_sequence_number =
1834 packet->header.sequenceNumber;
1835 decoded_packet_timestamp_ = prev_timestamp = packet->header.timestamp;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001836 prev_payload_type = packet->header.payloadType;
1837 }
1838
1839 // Store number of extracted samples.
1840 int packet_duration = 0;
1841 AudioDecoder* decoder = decoder_database_->GetDecoder(
1842 packet->header.payloadType);
1843 if (decoder) {
minyue@webrtc.orgb28bfa72014-03-21 12:07:40 +00001844 if (packet->sync_packet) {
1845 packet_duration = decoder_frame_length_;
1846 } else {
minyue@webrtc.org2c1bcf22015-02-17 10:17:09 +00001847 if (packet->primary) {
1848 packet_duration = decoder->PacketDuration(packet->payload,
1849 packet->payload_length);
1850 } else {
1851 packet_duration = decoder->
1852 PacketDurationRedundant(packet->payload, packet->payload_length);
1853 stats_.SecondaryDecodedSamples(packet_duration);
1854 }
minyue@webrtc.orgb28bfa72014-03-21 12:07:40 +00001855 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001856 } else {
pkasting@chromium.org026b8922015-01-30 19:53:42 +00001857 LOG_FERR1(LS_WARNING, GetDecoder,
1858 static_cast<int>(packet->header.payloadType))
1859 << "Could not find a decoder for a packet about to be extracted.";
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001860 assert(false);
1861 }
1862 if (packet_duration <= 0) {
1863 // Decoder did not return a packet duration. Assume that the packet
1864 // contains the same number of samples as the previous one.
1865 packet_duration = decoder_frame_length_;
1866 }
1867 extracted_samples = packet->header.timestamp - first_timestamp +
1868 packet_duration;
1869
1870 // Check what packet is available next.
1871 header = packet_buffer_->NextRtpHeader();
1872 next_packet_available = false;
1873 if (header && prev_payload_type == header->payloadType) {
1874 int16_t seq_no_diff = header->sequenceNumber - prev_sequence_number;
1875 int32_t ts_diff = header->timestamp - prev_timestamp;
1876 if (seq_no_diff == 1 ||
1877 (seq_no_diff == 0 && ts_diff == decoder_frame_length_)) {
1878 // The next sequence number is available, or the next part of a packet
1879 // that was split into pieces upon insertion.
1880 next_packet_available = true;
1881 }
1882 prev_sequence_number = header->sequenceNumber;
1883 }
1884 } while (extracted_samples < required_samples && next_packet_available);
1885
henrik.lundin@webrtc.org61217152014-09-22 08:30:07 +00001886 if (extracted_samples > 0) {
1887 // Delete old packets only when we are going to decode something. Otherwise,
1888 // we could end up in the situation where we never decode anything, since
1889 // all incoming packets are considered too old but the buffer will also
1890 // never be flooded and flushed.
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +00001891 packet_buffer_->DiscardAllOldPackets(timestamp_);
henrik.lundin@webrtc.org61217152014-09-22 08:30:07 +00001892 }
1893
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001894 return extracted_samples;
1895}
1896
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001897void NetEqImpl::UpdatePlcComponents(int fs_hz, size_t channels) {
1898 // Delete objects and create new ones.
1899 expand_.reset(expand_factory_->Create(background_noise_.get(),
1900 sync_buffer_.get(), &random_vector_,
1901 fs_hz, channels));
1902 merge_.reset(new Merge(fs_hz, channels, expand_.get(), sync_buffer_.get()));
1903}
1904
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001905void NetEqImpl::SetSampleRateAndChannels(int fs_hz, size_t channels) {
1906 LOG_API2(fs_hz, channels);
1907 // TODO(hlundin): Change to an enumerator and skip assert.
1908 assert(fs_hz == 8000 || fs_hz == 16000 || fs_hz == 32000 || fs_hz == 48000);
1909 assert(channels > 0);
1910
1911 fs_hz_ = fs_hz;
1912 fs_mult_ = fs_hz / 8000;
1913 output_size_samples_ = kOutputSizeMs * 8 * fs_mult_;
1914 decoder_frame_length_ = 3 * output_size_samples_; // Initialize to 30ms.
1915
1916 last_mode_ = kModeNormal;
1917
1918 // Create a new array of mute factors and set all to 1.
1919 mute_factor_array_.reset(new int16_t[channels]);
1920 for (size_t i = 0; i < channels; ++i) {
1921 mute_factor_array_[i] = 16384; // 1.0 in Q14.
1922 }
1923
1924 // Reset comfort noise decoder, if there is one active.
1925 AudioDecoder* cng_decoder = decoder_database_->GetActiveCngDecoder();
1926 if (cng_decoder) {
1927 cng_decoder->Init();
1928 }
1929
1930 // Reinit post-decode VAD with new sample rate.
1931 assert(vad_.get()); // Cannot be NULL here.
1932 vad_->Init();
1933
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001934 // Delete algorithm buffer and create a new one.
henrik.lundin@webrtc.orgfd11bbf2013-09-30 20:38:44 +00001935 algorithm_buffer_.reset(new AudioMultiVector(channels));
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001936
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001937 // Delete sync buffer and create a new one.
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +00001938 sync_buffer_.reset(new SyncBuffer(channels, kSyncBufferSize * fs_mult_));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001939
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00001940 // Delete BackgroundNoise object and create a new one.
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +00001941 background_noise_.reset(new BackgroundNoise(channels));
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00001942 background_noise_->set_mode(background_noise_mode_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001943
1944 // Reset random vector.
1945 random_vector_.Reset();
1946
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001947 UpdatePlcComponents(fs_hz, channels);
1948
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001949 // Move index so that we create a small set of future samples (all 0).
1950 sync_buffer_->set_next_index(sync_buffer_->next_index() -
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001951 expand_->overlap_length());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001952
henrik.lundin@webrtc.org40d3fc62013-09-18 12:19:50 +00001953 normal_.reset(new Normal(fs_hz, decoder_database_.get(), *background_noise_,
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +00001954 expand_.get()));
henrik.lundin@webrtc.orgd9faa462014-01-14 10:18:45 +00001955 accelerate_.reset(
1956 accelerate_factory_->Create(fs_hz, channels, *background_noise_));
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001957 preemptive_expand_.reset(preemptive_expand_factory_->Create(
1958 fs_hz, channels,
1959 *background_noise_,
1960 static_cast<int>(expand_->overlap_length())));
henrik.lundin@webrtc.org40d3fc62013-09-18 12:19:50 +00001961
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001962 // Delete ComfortNoise object and create a new one.
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +00001963 comfort_noise_.reset(new ComfortNoise(fs_hz, decoder_database_.get(),
1964 sync_buffer_.get()));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001965
1966 // Verify that |decoded_buffer_| is long enough.
1967 if (decoded_buffer_length_ < kMaxFrameSize * channels) {
1968 // Reallocate to larger size.
1969 decoded_buffer_length_ = kMaxFrameSize * channels;
1970 decoded_buffer_.reset(new int16_t[decoded_buffer_length_]);
1971 }
1972
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001973 // Create DecisionLogic if it is not created yet, then communicate new sample
1974 // rate and output size to DecisionLogic object.
1975 if (!decision_logic_.get()) {
henrik.lundin@webrtc.org7cbc4f92014-10-07 06:37:39 +00001976 CreateDecisionLogic();
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001977 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001978 decision_logic_->SetSampleRate(fs_hz_, output_size_samples_);
1979}
1980
1981NetEqOutputType NetEqImpl::LastOutputType() {
1982 assert(vad_.get());
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +00001983 assert(expand_.get());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001984 if (last_mode_ == kModeCodecInternalCng || last_mode_ == kModeRfc3389Cng) {
1985 return kOutputCNG;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001986 } else if (last_mode_ == kModeExpand && expand_->MuteFactor(0) == 0) {
1987 // Expand mode has faded down to background noise only (very long expand).
1988 return kOutputPLCtoCNG;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001989 } else if (last_mode_ == kModeExpand) {
1990 return kOutputPLC;
wu@webrtc.org24301a62013-12-13 19:17:43 +00001991 } else if (vad_->running() && !vad_->active_speech()) {
1992 return kOutputVADPassive;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001993 } else {
1994 return kOutputNormal;
1995 }
1996}
1997
henrik.lundin@webrtc.org7cbc4f92014-10-07 06:37:39 +00001998void NetEqImpl::CreateDecisionLogic() {
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001999 decision_logic_.reset(DecisionLogic::Create(fs_hz_, output_size_samples_,
henrik.lundin@webrtc.org7cbc4f92014-10-07 06:37:39 +00002000 playout_mode_,
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00002001 decoder_database_.get(),
2002 *packet_buffer_.get(),
2003 delay_manager_.get(),
2004 buffer_level_filter_.get()));
2005}
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00002006} // namespace webrtc