blob: 501e5672d1c8e02dd68fa95d243c1adfc70dd496 [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>
ossu61a208b2016-09-20 01:38:00 -070017#include <utility>
ossu97ba30e2016-04-25 07:55:58 -070018#include <vector>
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000019
kwiberg087bd342017-02-10 08:15:44 -080020#include "webrtc/api/audio_codecs/audio_decoder.h"
henrik.lundin9c3efd02015-08-27 13:12:22 -070021#include "webrtc/base/checks.h"
Henrik Lundind67a2192015-08-03 12:54:37 +020022#include "webrtc/base/logging.h"
Tommid44c0772016-03-11 17:12:32 -080023#include "webrtc/base/safe_conversions.h"
kwibergac554ee2016-09-02 00:39:33 -070024#include "webrtc/base/sanitizer.h"
henrik.lundina689b442015-12-17 03:50:05 -080025#include "webrtc/base/trace_event.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000026#include "webrtc/common_audio/signal_processing/include/signal_processing_library.h"
henrik.lundin@webrtc.org9c55f0f2014-06-09 08:10:28 +000027#include "webrtc/modules/audio_coding/neteq/accelerate.h"
28#include "webrtc/modules/audio_coding/neteq/background_noise.h"
29#include "webrtc/modules/audio_coding/neteq/buffer_level_filter.h"
30#include "webrtc/modules/audio_coding/neteq/comfort_noise.h"
31#include "webrtc/modules/audio_coding/neteq/decision_logic.h"
32#include "webrtc/modules/audio_coding/neteq/decoder_database.h"
33#include "webrtc/modules/audio_coding/neteq/defines.h"
34#include "webrtc/modules/audio_coding/neteq/delay_manager.h"
35#include "webrtc/modules/audio_coding/neteq/delay_peak_detector.h"
36#include "webrtc/modules/audio_coding/neteq/dtmf_buffer.h"
37#include "webrtc/modules/audio_coding/neteq/dtmf_tone_generator.h"
38#include "webrtc/modules/audio_coding/neteq/expand.h"
henrik.lundin@webrtc.org9c55f0f2014-06-09 08:10:28 +000039#include "webrtc/modules/audio_coding/neteq/merge.h"
henrik.lundin91951862016-06-08 06:43:41 -070040#include "webrtc/modules/audio_coding/neteq/nack_tracker.h"
henrik.lundin@webrtc.org9c55f0f2014-06-09 08:10:28 +000041#include "webrtc/modules/audio_coding/neteq/normal.h"
henrik.lundin@webrtc.org9c55f0f2014-06-09 08:10:28 +000042#include "webrtc/modules/audio_coding/neteq/packet.h"
kwiberg087bd342017-02-10 08:15:44 -080043#include "webrtc/modules/audio_coding/neteq/packet_buffer.h"
henrik.lundin@webrtc.org9c55f0f2014-06-09 08:10:28 +000044#include "webrtc/modules/audio_coding/neteq/post_decode_vad.h"
45#include "webrtc/modules/audio_coding/neteq/preemptive_expand.h"
kwiberg087bd342017-02-10 08:15:44 -080046#include "webrtc/modules/audio_coding/neteq/red_payload_splitter.h"
henrik.lundin@webrtc.org9c55f0f2014-06-09 08:10:28 +000047#include "webrtc/modules/audio_coding/neteq/sync_buffer.h"
henrik.lundined497212016-04-25 10:11:38 -070048#include "webrtc/modules/audio_coding/neteq/tick_timer.h"
henrik.lundin@webrtc.org9c55f0f2014-06-09 08:10:28 +000049#include "webrtc/modules/audio_coding/neteq/timestamp_scaler.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010050#include "webrtc/modules/include/module_common_types.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000051
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000052namespace webrtc {
53
ossue3525782016-05-25 07:37:43 -070054NetEqImpl::Dependencies::Dependencies(
55 const NetEq::Config& config,
56 const rtc::scoped_refptr<AudioDecoderFactory>& decoder_factory)
henrik.lundin1d9061e2016-04-26 12:19:34 -070057 : tick_timer(new TickTimer),
58 buffer_level_filter(new BufferLevelFilter),
ossue3525782016-05-25 07:37:43 -070059 decoder_database(new DecoderDatabase(decoder_factory)),
henrik.lundinf3933702016-04-28 01:53:52 -070060 delay_peak_detector(new DelayPeakDetector(tick_timer.get())),
henrik.lundin1d9061e2016-04-26 12:19:34 -070061 delay_manager(new DelayManager(config.max_packets_in_buffer,
henrik.lundin8f8c96d2016-04-28 23:19:20 -070062 delay_peak_detector.get(),
63 tick_timer.get())),
henrik.lundin1d9061e2016-04-26 12:19:34 -070064 dtmf_buffer(new DtmfBuffer(config.sample_rate_hz)),
65 dtmf_tone_generator(new DtmfToneGenerator),
66 packet_buffer(
67 new PacketBuffer(config.max_packets_in_buffer, tick_timer.get())),
ossua70695a2016-09-22 02:06:28 -070068 red_payload_splitter(new RedPayloadSplitter),
henrik.lundin1d9061e2016-04-26 12:19:34 -070069 timestamp_scaler(new TimestampScaler(*decoder_database)),
70 accelerate_factory(new AccelerateFactory),
71 expand_factory(new ExpandFactory),
72 preemptive_expand_factory(new PreemptiveExpandFactory) {}
73
74NetEqImpl::Dependencies::~Dependencies() = default;
75
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +000076NetEqImpl::NetEqImpl(const NetEq::Config& config,
henrik.lundin1d9061e2016-04-26 12:19:34 -070077 Dependencies&& deps,
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +000078 bool create_components)
henrik.lundin1d9061e2016-04-26 12:19:34 -070079 : tick_timer_(std::move(deps.tick_timer)),
80 buffer_level_filter_(std::move(deps.buffer_level_filter)),
81 decoder_database_(std::move(deps.decoder_database)),
82 delay_manager_(std::move(deps.delay_manager)),
83 delay_peak_detector_(std::move(deps.delay_peak_detector)),
84 dtmf_buffer_(std::move(deps.dtmf_buffer)),
85 dtmf_tone_generator_(std::move(deps.dtmf_tone_generator)),
86 packet_buffer_(std::move(deps.packet_buffer)),
ossua70695a2016-09-22 02:06:28 -070087 red_payload_splitter_(std::move(deps.red_payload_splitter)),
henrik.lundin1d9061e2016-04-26 12:19:34 -070088 timestamp_scaler_(std::move(deps.timestamp_scaler)),
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000089 vad_(new PostDecodeVad()),
henrik.lundin1d9061e2016-04-26 12:19:34 -070090 expand_factory_(std::move(deps.expand_factory)),
91 accelerate_factory_(std::move(deps.accelerate_factory)),
92 preemptive_expand_factory_(std::move(deps.preemptive_expand_factory)),
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000093 last_mode_(kModeNormal),
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000094 decoded_buffer_length_(kMaxFrameSize),
95 decoded_buffer_(new int16_t[decoded_buffer_length_]),
96 playout_timestamp_(0),
97 new_codec_(false),
98 timestamp_(0),
99 reset_decoder_(false),
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000100 ssrc_(0),
101 first_packet_(true),
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000102 error_code_(0),
103 decoder_error_code_(0),
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000104 background_noise_mode_(config.background_noise_mode),
henrik.lundin@webrtc.org7cbc4f92014-10-07 06:37:39 +0000105 playout_mode_(config.playout_mode),
Henrik Lundincf808d22015-05-27 14:33:29 +0200106 enable_fast_accelerate_(config.enable_fast_accelerate),
henrik.lundin7a926812016-05-12 13:51:28 -0700107 nack_enabled_(false),
108 enable_muted_state_(config.enable_muted_state) {
Henrik Lundin905495c2015-05-25 16:58:41 +0200109 LOG(LS_INFO) << "NetEq config: " << config.ToString();
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000110 int fs = config.sample_rate_hz;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000111 if (fs != 8000 && fs != 16000 && fs != 32000 && fs != 48000) {
112 LOG(LS_ERROR) << "Sample rate " << fs << " Hz not supported. " <<
113 "Changing to 8000 Hz.";
114 fs = 8000;
115 }
henrik.lundin1d9061e2016-04-26 12:19:34 -0700116 delay_manager_->SetMaximumDelay(config.max_delay_ms);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000117 fs_hz_ = fs;
118 fs_mult_ = fs / 8000;
henrik.lundind89814b2015-11-23 06:49:25 -0800119 last_output_sample_rate_hz_ = fs;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700120 output_size_samples_ = static_cast<size_t>(kOutputSizeMs * 8 * fs_mult_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000121 decoder_frame_length_ = 3 * output_size_samples_;
122 WebRtcSpl_Init();
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +0000123 if (create_components) {
124 SetSampleRateAndChannels(fs, 1); // Default is 1 channel.
125 }
henrik.lundin9bc26672015-11-02 03:25:57 -0800126 RTC_DCHECK(!vad_->enabled());
127 if (config.enable_post_decode_vad) {
128 vad_->Enable();
129 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000130}
131
Henrik Lundind67a2192015-08-03 12:54:37 +0200132NetEqImpl::~NetEqImpl() = default;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000133
henrik.lundin10d095d2017-04-11 07:47:59 -0700134int NetEqImpl::InsertPacket(const WebRtcRTPHeader& rtp_header,
kwibergee2bac22015-11-11 10:34:00 -0800135 rtc::ArrayView<const uint8_t> payload,
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000136 uint32_t receive_timestamp) {
kwibergac554ee2016-09-02 00:39:33 -0700137 rtc::MsanCheckInitialized(payload);
henrik.lundina689b442015-12-17 03:50:05 -0800138 TRACE_EVENT0("webrtc", "NetEqImpl::InsertPacket");
Tommi9090e0b2016-01-20 13:39:36 +0100139 rtc::CritScope lock(&crit_sect_);
kwibergee2bac22015-11-11 10:34:00 -0800140 int error =
ossu17e3fa12016-09-08 04:52:55 -0700141 InsertPacketInternal(rtp_header, payload, receive_timestamp);
henrik.lundin@webrtc.orge7ce4372014-01-09 14:01:55 +0000142 if (error != 0) {
henrik.lundin@webrtc.orge7ce4372014-01-09 14:01:55 +0000143 error_code_ = error;
144 return kFail;
145 }
146 return kOK;
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +0000147}
148
henrik.lundin500c04b2016-03-08 02:36:04 -0800149namespace {
150void SetAudioFrameActivityAndType(bool vad_enabled,
henrik.lundin55480f52016-03-08 02:37:57 -0800151 NetEqImpl::OutputType type,
henrik.lundin500c04b2016-03-08 02:36:04 -0800152 AudioFrame::VADActivity last_vad_activity,
153 AudioFrame* audio_frame) {
154 switch (type) {
henrik.lundin55480f52016-03-08 02:37:57 -0800155 case NetEqImpl::OutputType::kNormalSpeech: {
henrik.lundin500c04b2016-03-08 02:36:04 -0800156 audio_frame->speech_type_ = AudioFrame::kNormalSpeech;
157 audio_frame->vad_activity_ = AudioFrame::kVadActive;
158 break;
159 }
henrik.lundin55480f52016-03-08 02:37:57 -0800160 case NetEqImpl::OutputType::kVadPassive: {
henrik.lundin500c04b2016-03-08 02:36:04 -0800161 // This should only be reached if the VAD is enabled.
162 RTC_DCHECK(vad_enabled);
163 audio_frame->speech_type_ = AudioFrame::kNormalSpeech;
164 audio_frame->vad_activity_ = AudioFrame::kVadPassive;
165 break;
166 }
henrik.lundin55480f52016-03-08 02:37:57 -0800167 case NetEqImpl::OutputType::kCNG: {
henrik.lundin500c04b2016-03-08 02:36:04 -0800168 audio_frame->speech_type_ = AudioFrame::kCNG;
169 audio_frame->vad_activity_ = AudioFrame::kVadPassive;
170 break;
171 }
henrik.lundin55480f52016-03-08 02:37:57 -0800172 case NetEqImpl::OutputType::kPLC: {
henrik.lundin500c04b2016-03-08 02:36:04 -0800173 audio_frame->speech_type_ = AudioFrame::kPLC;
174 audio_frame->vad_activity_ = last_vad_activity;
175 break;
176 }
henrik.lundin55480f52016-03-08 02:37:57 -0800177 case NetEqImpl::OutputType::kPLCCNG: {
henrik.lundin500c04b2016-03-08 02:36:04 -0800178 audio_frame->speech_type_ = AudioFrame::kPLCCNG;
179 audio_frame->vad_activity_ = AudioFrame::kVadPassive;
180 break;
181 }
182 default:
183 RTC_NOTREACHED();
184 }
185 if (!vad_enabled) {
186 // Always set kVadUnknown when receive VAD is inactive.
187 audio_frame->vad_activity_ = AudioFrame::kVadUnknown;
188 }
189}
henrik.lundinbc89de32016-03-08 05:20:14 -0800190} // namespace
henrik.lundin500c04b2016-03-08 02:36:04 -0800191
henrik.lundin7a926812016-05-12 13:51:28 -0700192int NetEqImpl::GetAudio(AudioFrame* audio_frame, bool* muted) {
henrik.lundine1ca1672016-01-08 03:50:08 -0800193 TRACE_EVENT0("webrtc", "NetEqImpl::GetAudio");
Tommi9090e0b2016-01-20 13:39:36 +0100194 rtc::CritScope lock(&crit_sect_);
henrik.lundin7a926812016-05-12 13:51:28 -0700195 int error = GetAudioInternal(audio_frame, muted);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000196 if (error != 0) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000197 error_code_ = error;
198 return kFail;
199 }
henrik.lundin5fac3f02016-08-24 11:18:49 -0700200 RTC_DCHECK_EQ(
201 audio_frame->sample_rate_hz_,
kwibergd3edd772017-03-01 18:52:48 -0800202 rtc::dchecked_cast<int>(audio_frame->samples_per_channel_ * 100));
henrik.lundin500c04b2016-03-08 02:36:04 -0800203 SetAudioFrameActivityAndType(vad_->enabled(), LastOutputType(),
204 last_vad_activity_, audio_frame);
205 last_vad_activity_ = audio_frame->vad_activity_;
henrik.lundin6d8e0112016-03-04 10:34:21 -0800206 last_output_sample_rate_hz_ = audio_frame->sample_rate_hz_;
henrik.lundind89814b2015-11-23 06:49:25 -0800207 RTC_DCHECK(last_output_sample_rate_hz_ == 8000 ||
208 last_output_sample_rate_hz_ == 16000 ||
209 last_output_sample_rate_hz_ == 32000 ||
210 last_output_sample_rate_hz_ == 48000)
211 << "Unexpected sample rate " << last_output_sample_rate_hz_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000212 return kOK;
213}
214
kwiberg1c07c702017-03-27 07:15:49 -0700215void NetEqImpl::SetCodecs(const std::map<int, SdpAudioFormat>& codecs) {
216 rtc::CritScope lock(&crit_sect_);
217 const std::vector<int> changed_payload_types =
218 decoder_database_->SetCodecs(codecs);
219 for (const int pt : changed_payload_types) {
220 packet_buffer_->DiscardPacketsWithPayloadType(pt);
221 }
222}
223
kwibergee1879c2015-10-29 06:20:28 -0700224int NetEqImpl::RegisterPayloadType(NetEqDecoder codec,
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800225 const std::string& name,
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000226 uint8_t rtp_payload_type) {
Tommi9090e0b2016-01-20 13:39:36 +0100227 rtc::CritScope lock(&crit_sect_);
Henrik Lundind67a2192015-08-03 12:54:37 +0200228 LOG(LS_VERBOSE) << "RegisterPayloadType "
kwibergee1879c2015-10-29 06:20:28 -0700229 << static_cast<int>(rtp_payload_type) << " "
230 << static_cast<int>(codec);
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800231 int ret = decoder_database_->RegisterPayload(rtp_payload_type, codec, name);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000232 if (ret != DecoderDatabase::kOK) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000233 switch (ret) {
234 case DecoderDatabase::kInvalidRtpPayloadType:
235 error_code_ = kInvalidRtpPayloadType;
236 break;
237 case DecoderDatabase::kCodecNotSupported:
238 error_code_ = kCodecNotSupported;
239 break;
240 case DecoderDatabase::kDecoderExists:
241 error_code_ = kDecoderExists;
242 break;
243 default:
244 error_code_ = kOtherError;
245 }
246 return kFail;
247 }
248 return kOK;
249}
250
251int NetEqImpl::RegisterExternalDecoder(AudioDecoder* decoder,
kwibergee1879c2015-10-29 06:20:28 -0700252 NetEqDecoder codec,
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800253 const std::string& codec_name,
kwiberg342f7402016-06-16 03:18:00 -0700254 uint8_t rtp_payload_type) {
Tommi9090e0b2016-01-20 13:39:36 +0100255 rtc::CritScope lock(&crit_sect_);
Henrik Lundind67a2192015-08-03 12:54:37 +0200256 LOG(LS_VERBOSE) << "RegisterExternalDecoder "
kwibergee1879c2015-10-29 06:20:28 -0700257 << static_cast<int>(rtp_payload_type) << " "
258 << static_cast<int>(codec);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000259 if (!decoder) {
260 LOG(LS_ERROR) << "Cannot register external decoder with NULL pointer";
261 assert(false);
262 return kFail;
263 }
kwiberg342f7402016-06-16 03:18:00 -0700264 int ret = decoder_database_->InsertExternal(rtp_payload_type, codec,
265 codec_name, decoder);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000266 if (ret != DecoderDatabase::kOK) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000267 switch (ret) {
268 case DecoderDatabase::kInvalidRtpPayloadType:
269 error_code_ = kInvalidRtpPayloadType;
270 break;
271 case DecoderDatabase::kCodecNotSupported:
272 error_code_ = kCodecNotSupported;
273 break;
274 case DecoderDatabase::kDecoderExists:
275 error_code_ = kDecoderExists;
276 break;
277 case DecoderDatabase::kInvalidSampleRate:
278 error_code_ = kInvalidSampleRate;
279 break;
280 case DecoderDatabase::kInvalidPointer:
281 error_code_ = kInvalidPointer;
282 break;
283 default:
284 error_code_ = kOtherError;
285 }
286 return kFail;
287 }
288 return kOK;
289}
290
kwiberg5adaf732016-10-04 09:33:27 -0700291bool NetEqImpl::RegisterPayloadType(int rtp_payload_type,
292 const SdpAudioFormat& audio_format) {
293 LOG(LS_VERBOSE) << "NetEqImpl::RegisterPayloadType: payload type "
294 << rtp_payload_type << ", codec " << audio_format;
295 rtc::CritScope lock(&crit_sect_);
296 switch (decoder_database_->RegisterPayload(rtp_payload_type, audio_format)) {
297 case DecoderDatabase::kOK:
298 return true;
299 case DecoderDatabase::kInvalidRtpPayloadType:
300 error_code_ = kInvalidRtpPayloadType;
301 return false;
302 case DecoderDatabase::kCodecNotSupported:
303 error_code_ = kCodecNotSupported;
304 return false;
305 case DecoderDatabase::kDecoderExists:
306 error_code_ = kDecoderExists;
307 return false;
308 case DecoderDatabase::kInvalidSampleRate:
309 error_code_ = kInvalidSampleRate;
310 return false;
311 case DecoderDatabase::kInvalidPointer:
312 error_code_ = kInvalidPointer;
313 return false;
314 default:
315 error_code_ = kOtherError;
316 return false;
317 }
318}
319
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000320int NetEqImpl::RemovePayloadType(uint8_t rtp_payload_type) {
Tommi9090e0b2016-01-20 13:39:36 +0100321 rtc::CritScope lock(&crit_sect_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000322 int ret = decoder_database_->Remove(rtp_payload_type);
323 if (ret == DecoderDatabase::kOK) {
ossu61a208b2016-09-20 01:38:00 -0700324 packet_buffer_->DiscardPacketsWithPayloadType(rtp_payload_type);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000325 return kOK;
326 } else if (ret == DecoderDatabase::kDecoderNotFound) {
327 error_code_ = kDecoderNotFound;
328 } else {
329 error_code_ = kOtherError;
330 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000331 return kFail;
332}
333
kwiberg6b19b562016-09-20 04:02:25 -0700334void NetEqImpl::RemoveAllPayloadTypes() {
335 rtc::CritScope lock(&crit_sect_);
336 decoder_database_->RemoveAll();
337}
338
turaj@webrtc.orgf1efc572013-08-16 23:44:24 +0000339bool NetEqImpl::SetMinimumDelay(int delay_ms) {
Tommi9090e0b2016-01-20 13:39:36 +0100340 rtc::CritScope lock(&crit_sect_);
turaj@webrtc.orgf1efc572013-08-16 23:44:24 +0000341 if (delay_ms >= 0 && delay_ms < 10000) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000342 assert(delay_manager_.get());
turaj@webrtc.orgf1efc572013-08-16 23:44:24 +0000343 return delay_manager_->SetMinimumDelay(delay_ms);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000344 }
345 return false;
346}
347
turaj@webrtc.orgf1efc572013-08-16 23:44:24 +0000348bool NetEqImpl::SetMaximumDelay(int delay_ms) {
Tommi9090e0b2016-01-20 13:39:36 +0100349 rtc::CritScope lock(&crit_sect_);
turaj@webrtc.orgf1efc572013-08-16 23:44:24 +0000350 if (delay_ms >= 0 && delay_ms < 10000) {
351 assert(delay_manager_.get());
352 return delay_manager_->SetMaximumDelay(delay_ms);
353 }
354 return false;
355}
356
357int NetEqImpl::LeastRequiredDelayMs() const {
Tommi9090e0b2016-01-20 13:39:36 +0100358 rtc::CritScope lock(&crit_sect_);
turaj@webrtc.orgf1efc572013-08-16 23:44:24 +0000359 assert(delay_manager_.get());
360 return delay_manager_->least_required_delay_ms();
361}
362
Karl Wiberg7f6c4d42015-04-09 15:44:22 +0200363int NetEqImpl::SetTargetDelay() {
364 return kNotImplemented;
365}
366
367int NetEqImpl::TargetDelay() {
368 return kNotImplemented;
369}
370
henrik.lundin9c3efd02015-08-27 13:12:22 -0700371int NetEqImpl::CurrentDelayMs() const {
Tommi9090e0b2016-01-20 13:39:36 +0100372 rtc::CritScope lock(&crit_sect_);
henrik.lundin9c3efd02015-08-27 13:12:22 -0700373 if (fs_hz_ == 0)
374 return 0;
375 // Sum up the samples in the packet buffer with the future length of the sync
376 // buffer, and divide the sum by the sample rate.
377 const size_t delay_samples =
ossu61a208b2016-09-20 01:38:00 -0700378 packet_buffer_->NumSamplesInBuffer(decoder_frame_length_) +
henrik.lundin9c3efd02015-08-27 13:12:22 -0700379 sync_buffer_->FutureLength();
380 // The division below will truncate.
381 const int delay_ms =
382 static_cast<int>(delay_samples) / rtc::CheckedDivExact(fs_hz_, 1000);
383 return delay_ms;
Karl Wiberg7f6c4d42015-04-09 15:44:22 +0200384}
385
henrik.lundinb3f1c5d2016-08-22 15:39:53 -0700386int NetEqImpl::FilteredCurrentDelayMs() const {
387 rtc::CritScope lock(&crit_sect_);
388 // Calculate the filtered packet buffer level in samples. The value from
389 // |buffer_level_filter_| is in number of packets, represented in Q8.
390 const size_t packet_buffer_samples =
391 (buffer_level_filter_->filtered_current_level() *
392 decoder_frame_length_) >>
393 8;
394 // Sum up the filtered packet buffer level with the future length of the sync
395 // buffer, and divide the sum by the sample rate.
396 const size_t delay_samples =
397 packet_buffer_samples + sync_buffer_->FutureLength();
398 // The division below will truncate. The return value is in ms.
399 return static_cast<int>(delay_samples) / rtc::CheckedDivExact(fs_hz_, 1000);
400}
401
henrik.lundin@webrtc.org7cbc4f92014-10-07 06:37:39 +0000402// Deprecated.
403// TODO(henrik.lundin) Delete.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000404void NetEqImpl::SetPlayoutMode(NetEqPlayoutMode mode) {
Tommi9090e0b2016-01-20 13:39:36 +0100405 rtc::CritScope lock(&crit_sect_);
henrik.lundin@webrtc.org7cbc4f92014-10-07 06:37:39 +0000406 if (mode != playout_mode_) {
407 playout_mode_ = mode;
408 CreateDecisionLogic();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000409 }
410}
411
henrik.lundin@webrtc.org7cbc4f92014-10-07 06:37:39 +0000412// Deprecated.
413// TODO(henrik.lundin) Delete.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000414NetEqPlayoutMode NetEqImpl::PlayoutMode() const {
Tommi9090e0b2016-01-20 13:39:36 +0100415 rtc::CritScope lock(&crit_sect_);
henrik.lundin@webrtc.org7cbc4f92014-10-07 06:37:39 +0000416 return playout_mode_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000417}
418
419int NetEqImpl::NetworkStatistics(NetEqNetworkStatistics* stats) {
Tommi9090e0b2016-01-20 13:39:36 +0100420 rtc::CritScope lock(&crit_sect_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000421 assert(decoder_database_.get());
Peter Kastingdce40cf2015-08-24 14:52:23 -0700422 const size_t total_samples_in_buffers =
ossu61a208b2016-09-20 01:38:00 -0700423 packet_buffer_->NumSamplesInBuffer(decoder_frame_length_) +
Peter Kastingdce40cf2015-08-24 14:52:23 -0700424 sync_buffer_->FutureLength();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000425 assert(delay_manager_.get());
426 assert(decision_logic_.get());
427 stats_.GetNetworkStatistics(fs_hz_, total_samples_in_buffers,
428 decoder_frame_length_, *delay_manager_.get(),
429 *decision_logic_.get(), stats);
430 return 0;
431}
432
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000433void NetEqImpl::GetRtcpStatistics(RtcpStatistics* stats) {
Tommi9090e0b2016-01-20 13:39:36 +0100434 rtc::CritScope lock(&crit_sect_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000435 if (stats) {
436 rtcp_.GetStatistics(false, stats);
437 }
438}
439
440void NetEqImpl::GetRtcpStatisticsNoReset(RtcpStatistics* stats) {
Tommi9090e0b2016-01-20 13:39:36 +0100441 rtc::CritScope lock(&crit_sect_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000442 if (stats) {
443 rtcp_.GetStatistics(true, stats);
444 }
445}
446
447void NetEqImpl::EnableVad() {
Tommi9090e0b2016-01-20 13:39:36 +0100448 rtc::CritScope lock(&crit_sect_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000449 assert(vad_.get());
450 vad_->Enable();
451}
452
453void NetEqImpl::DisableVad() {
Tommi9090e0b2016-01-20 13:39:36 +0100454 rtc::CritScope lock(&crit_sect_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000455 assert(vad_.get());
456 vad_->Disable();
457}
458
henrik.lundin15c51e32016-04-06 08:38:56 -0700459rtc::Optional<uint32_t> NetEqImpl::GetPlayoutTimestamp() const {
Tommi9090e0b2016-01-20 13:39:36 +0100460 rtc::CritScope lock(&crit_sect_);
henrik.lundin0d96ab72016-04-06 12:28:26 -0700461 if (first_packet_ || last_mode_ == kModeRfc3389Cng ||
462 last_mode_ == kModeCodecInternalCng) {
wu@webrtc.org94454b72014-06-05 20:34:08 +0000463 // We don't have a valid RTP timestamp until we have decoded our first
henrik.lundin0d96ab72016-04-06 12:28:26 -0700464 // RTP packet. Also, the RTP timestamp is not accurate while playing CNG,
465 // which is indicated by returning an empty value.
henrik.lundin9a410dd2016-04-06 01:39:22 -0700466 return rtc::Optional<uint32_t>();
wu@webrtc.org94454b72014-06-05 20:34:08 +0000467 }
henrik.lundin9a410dd2016-04-06 01:39:22 -0700468 return rtc::Optional<uint32_t>(
469 timestamp_scaler_->ToExternal(playout_timestamp_));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000470}
471
henrik.lundind89814b2015-11-23 06:49:25 -0800472int NetEqImpl::last_output_sample_rate_hz() const {
Tommi9090e0b2016-01-20 13:39:36 +0100473 rtc::CritScope lock(&crit_sect_);
henrik.lundind89814b2015-11-23 06:49:25 -0800474 return last_output_sample_rate_hz_;
475}
476
kwiberg6f0f6162016-09-20 03:07:46 -0700477rtc::Optional<CodecInst> NetEqImpl::GetDecoder(int payload_type) const {
478 rtc::CritScope lock(&crit_sect_);
479 const DecoderDatabase::DecoderInfo* di =
480 decoder_database_->GetDecoderInfo(payload_type);
481 if (!di) {
482 return rtc::Optional<CodecInst>();
483 }
484
485 // Create a CodecInst with some fields set. The remaining fields are zeroed,
486 // but we tell MSan to consider them uninitialized.
487 CodecInst ci = {0};
488 rtc::MsanMarkUninitialized(rtc::MakeArrayView(&ci, 1));
489 ci.pltype = payload_type;
kwiberge9413062016-11-03 05:29:05 -0700490 std::strncpy(ci.plname, di->get_name().c_str(), sizeof(ci.plname));
kwiberg6f0f6162016-09-20 03:07:46 -0700491 ci.plname[sizeof(ci.plname) - 1] = '\0';
solenberg2779bab2016-11-17 04:45:19 -0800492 ci.plfreq = di->IsRed() ? 8000 : di->SampleRateHz();
kwiberg6f0f6162016-09-20 03:07:46 -0700493 AudioDecoder* const decoder = di->GetDecoder();
494 ci.channels = decoder ? decoder->Channels() : 1;
495 return rtc::Optional<CodecInst>(ci);
496}
497
ossuf1b08da2016-09-23 02:19:43 -0700498rtc::Optional<SdpAudioFormat> NetEqImpl::GetDecoderFormat(
499 int payload_type) const {
kwibergc4ccd4d2016-09-21 10:55:15 -0700500 rtc::CritScope lock(&crit_sect_);
501 const DecoderDatabase::DecoderInfo* const di =
502 decoder_database_->GetDecoderInfo(payload_type);
503 if (!di) {
ossuf1b08da2016-09-23 02:19:43 -0700504 return rtc::Optional<SdpAudioFormat>(); // Payload type not registered.
kwibergc4ccd4d2016-09-21 10:55:15 -0700505 }
ossuf1b08da2016-09-23 02:19:43 -0700506 return rtc::Optional<SdpAudioFormat>(di->GetFormat());
kwibergc4ccd4d2016-09-21 10:55:15 -0700507}
508
Karl Wiberg7f6c4d42015-04-09 15:44:22 +0200509int NetEqImpl::SetTargetNumberOfChannels() {
510 return kNotImplemented;
511}
512
513int NetEqImpl::SetTargetSampleRate() {
514 return kNotImplemented;
515}
516
henrik.lundin@webrtc.orgb0f4b3d2014-11-04 08:53:10 +0000517int NetEqImpl::LastError() const {
Tommi9090e0b2016-01-20 13:39:36 +0100518 rtc::CritScope lock(&crit_sect_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000519 return error_code_;
520}
521
522int NetEqImpl::LastDecoderError() {
Tommi9090e0b2016-01-20 13:39:36 +0100523 rtc::CritScope lock(&crit_sect_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000524 return decoder_error_code_;
525}
526
527void NetEqImpl::FlushBuffers() {
Tommi9090e0b2016-01-20 13:39:36 +0100528 rtc::CritScope lock(&crit_sect_);
Henrik Lundind67a2192015-08-03 12:54:37 +0200529 LOG(LS_VERBOSE) << "FlushBuffers";
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000530 packet_buffer_->Flush();
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +0000531 assert(sync_buffer_.get());
532 assert(expand_.get());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000533 sync_buffer_->Flush();
534 sync_buffer_->set_next_index(sync_buffer_->next_index() -
535 expand_->overlap_length());
536 // Set to wait for new codec.
537 first_packet_ = true;
538}
539
turaj@webrtc.org3170b572013-08-30 15:36:53 +0000540void NetEqImpl::PacketBufferStatistics(int* current_num_packets,
henrik.lundin@webrtc.org116ed1d2014-04-28 08:20:04 +0000541 int* max_num_packets) const {
Tommi9090e0b2016-01-20 13:39:36 +0100542 rtc::CritScope lock(&crit_sect_);
henrik.lundin@webrtc.org116ed1d2014-04-28 08:20:04 +0000543 packet_buffer_->BufferStat(current_num_packets, max_num_packets);
turaj@webrtc.org3170b572013-08-30 15:36:53 +0000544}
545
henrik.lundin48ed9302015-10-29 05:36:24 -0700546void NetEqImpl::EnableNack(size_t max_nack_list_size) {
Tommi9090e0b2016-01-20 13:39:36 +0100547 rtc::CritScope lock(&crit_sect_);
henrik.lundin48ed9302015-10-29 05:36:24 -0700548 if (!nack_enabled_) {
549 const int kNackThresholdPackets = 2;
henrik.lundin91951862016-06-08 06:43:41 -0700550 nack_.reset(NackTracker::Create(kNackThresholdPackets));
henrik.lundin48ed9302015-10-29 05:36:24 -0700551 nack_enabled_ = true;
552 nack_->UpdateSampleRate(fs_hz_);
553 }
554 nack_->SetMaxNackListSize(max_nack_list_size);
555}
556
557void NetEqImpl::DisableNack() {
Tommi9090e0b2016-01-20 13:39:36 +0100558 rtc::CritScope lock(&crit_sect_);
henrik.lundin48ed9302015-10-29 05:36:24 -0700559 nack_.reset();
560 nack_enabled_ = false;
561}
562
563std::vector<uint16_t> NetEqImpl::GetNackList(int64_t round_trip_time_ms) const {
Tommi9090e0b2016-01-20 13:39:36 +0100564 rtc::CritScope lock(&crit_sect_);
henrik.lundin48ed9302015-10-29 05:36:24 -0700565 if (!nack_enabled_) {
566 return std::vector<uint16_t>();
567 }
568 RTC_DCHECK(nack_.get());
569 return nack_->GetNackList(round_trip_time_ms);
minyue@webrtc.orgd7301772013-08-29 00:58:14 +0000570}
571
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000572const SyncBuffer* NetEqImpl::sync_buffer_for_test() const {
Tommi9090e0b2016-01-20 13:39:36 +0100573 rtc::CritScope lock(&crit_sect_);
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000574 return sync_buffer_.get();
575}
576
minyue5bd33972016-05-02 04:46:11 -0700577Operations NetEqImpl::last_operation_for_test() const {
578 rtc::CritScope lock(&crit_sect_);
579 return last_operation_;
580}
581
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000582// Methods below this line are private.
583
henrik.lundin10d095d2017-04-11 07:47:59 -0700584int NetEqImpl::InsertPacketInternal(const WebRtcRTPHeader& rtp_header,
kwibergee2bac22015-11-11 10:34:00 -0800585 rtc::ArrayView<const uint8_t> payload,
ossu17e3fa12016-09-08 04:52:55 -0700586 uint32_t receive_timestamp) {
kwibergee2bac22015-11-11 10:34:00 -0800587 if (payload.empty()) {
588 LOG_F(LS_ERROR) << "payload is empty";
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000589 return kInvalidPointer;
590 }
ossu17e3fa12016-09-08 04:52:55 -0700591
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000592 PacketList packet_list;
ossua73f6c92016-10-24 08:25:28 -0700593 // Insert packet in a packet list.
594 packet_list.push_back([&rtp_header, &payload] {
henrik.lundin@webrtc.orge1d468c2013-01-30 07:37:20 +0000595 // Convert to Packet.
ossua73f6c92016-10-24 08:25:28 -0700596 Packet packet;
henrik.lundin10d095d2017-04-11 07:47:59 -0700597 packet.payload_type = rtp_header.header.payloadType;
598 packet.sequence_number = rtp_header.header.sequenceNumber;
599 packet.timestamp = rtp_header.header.timestamp;
ossua73f6c92016-10-24 08:25:28 -0700600 packet.payload.SetData(payload.data(), payload.size());
henrik.lundin84f8cd62016-04-26 07:45:16 -0700601 // Waiting time will be set upon inserting the packet in the buffer.
ossua73f6c92016-10-24 08:25:28 -0700602 RTC_DCHECK(!packet.waiting_time);
603 return packet;
604 }());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000605
henrik.lundin10d095d2017-04-11 07:47:59 -0700606 bool update_sample_rate_and_channels = first_packet_ ||
607 (rtp_header.header.ssrc != ssrc_);
dkirovbroadsofte851a9a2017-03-14 10:00:27 -0700608
609 if (update_sample_rate_and_channels) {
610 // Reset timestamp scaling.
611 timestamp_scaler_->Reset();
612 }
613
henrik.lundin10d095d2017-04-11 07:47:59 -0700614 if (!decoder_database_->IsRed(rtp_header.header.payloadType)) {
dkirovbroadsofte851a9a2017-03-14 10:00:27 -0700615 // Scale timestamp to internal domain (only for some codecs).
616 timestamp_scaler_->ToInternal(&packet_list);
617 }
618
619 // Store these for later use, since the first packet may very well disappear
620 // before we need these values.
621 uint32_t main_timestamp = packet_list.front().timestamp;
622 uint8_t main_payload_type = packet_list.front().payload_type;
623 uint16_t main_sequence_number = packet_list.front().sequence_number;
624
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000625 // Reinitialize NetEq if it's needed (changed SSRC or first call).
dkirovbroadsofte851a9a2017-03-14 10:00:27 -0700626 if (update_sample_rate_and_channels) {
henrik.lundin@webrtc.org6ff3ac12014-11-20 14:14:49 +0000627 // Note: |first_packet_| will be cleared further down in this method, once
628 // the packet has been successfully inserted into the packet buffer.
629
henrik.lundin10d095d2017-04-11 07:47:59 -0700630 rtcp_.Init(rtp_header.header.sequenceNumber);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000631
632 // Flush the packet buffer and DTMF buffer.
633 packet_buffer_->Flush();
634 dtmf_buffer_->Flush();
635
636 // Store new SSRC.
henrik.lundin10d095d2017-04-11 07:47:59 -0700637 ssrc_ = rtp_header.header.ssrc;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000638
turaj@webrtc.org4d06db52013-03-27 18:31:42 +0000639 // Update audio buffer timestamp.
dkirovbroadsofte851a9a2017-03-14 10:00:27 -0700640 sync_buffer_->IncreaseEndTimestamp(main_timestamp - timestamp_);
turaj@webrtc.org4d06db52013-03-27 18:31:42 +0000641
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000642 // Update codecs.
dkirovbroadsofte851a9a2017-03-14 10:00:27 -0700643 timestamp_ = main_timestamp;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000644 }
645
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +0000646 // Update RTCP statistics, only for regular packets.
henrik.lundin10d095d2017-04-11 07:47:59 -0700647 rtcp_.Update(rtp_header.header, receive_timestamp);
ossu7a377612016-10-18 04:06:13 -0700648
649 if (nack_enabled_) {
650 RTC_DCHECK(nack_);
651 if (update_sample_rate_and_channels) {
652 nack_->Reset();
653 }
henrik.lundin10d095d2017-04-11 07:47:59 -0700654 nack_->UpdateLastReceivedPacket(rtp_header.header.sequenceNumber,
655 rtp_header.header.timestamp);
ossu7a377612016-10-18 04:06:13 -0700656 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000657
658 // Check for RED payload type, and separate payloads into several packets.
henrik.lundin10d095d2017-04-11 07:47:59 -0700659 if (decoder_database_->IsRed(rtp_header.header.payloadType)) {
ossua70695a2016-09-22 02:06:28 -0700660 if (!red_payload_splitter_->SplitRed(&packet_list)) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000661 return kRedundancySplitError;
662 }
663 // Only accept a few RED payloads of the same type as the main data,
664 // DTMF events and CNG.
ossua70695a2016-09-22 02:06:28 -0700665 red_payload_splitter_->CheckRedPayloads(&packet_list, *decoder_database_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000666 }
667
668 // Check payload types.
669 if (decoder_database_->CheckPayloadTypes(packet_list) ==
670 DecoderDatabase::kDecoderNotFound) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000671 return kUnknownRtpPayloadType;
672 }
673
ossu7a377612016-10-18 04:06:13 -0700674 RTC_DCHECK(!packet_list.empty());
ossu7a377612016-10-18 04:06:13 -0700675
dkirovbroadsofte851a9a2017-03-14 10:00:27 -0700676 // Update main_timestamp, if new packets appear in the list
677 // after RED splitting.
henrik.lundin10d095d2017-04-11 07:47:59 -0700678 if (decoder_database_->IsRed(rtp_header.header.payloadType)) {
dkirovbroadsofte851a9a2017-03-14 10:00:27 -0700679 timestamp_scaler_->ToInternal(&packet_list);
680 main_timestamp = packet_list.front().timestamp;
681 main_payload_type = packet_list.front().payload_type;
682 main_sequence_number = packet_list.front().sequence_number;
683 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000684
685 // Process DTMF payloads. Cycle through the list of packets, and pick out any
686 // DTMF payloads found.
687 PacketList::iterator it = packet_list.begin();
688 while (it != packet_list.end()) {
ossua73f6c92016-10-24 08:25:28 -0700689 const Packet& current_packet = (*it);
690 RTC_DCHECK(!current_packet.payload.empty());
691 if (decoder_database_->IsDtmf(current_packet.payload_type)) {
minyue@webrtc.org9721db72013-08-06 05:36:26 +0000692 DtmfEvent event;
ossua73f6c92016-10-24 08:25:28 -0700693 int ret = DtmfBuffer::ParseEvent(current_packet.timestamp,
694 current_packet.payload.data(),
695 current_packet.payload.size(), &event);
minyue@webrtc.org9721db72013-08-06 05:36:26 +0000696 if (ret != DtmfBuffer::kOK) {
minyue@webrtc.org9721db72013-08-06 05:36:26 +0000697 return kDtmfParsingError;
698 }
699 if (dtmf_buffer_->InsertEvent(event) != DtmfBuffer::kOK) {
minyue@webrtc.org9721db72013-08-06 05:36:26 +0000700 return kDtmfInsertError;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000701 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000702 it = packet_list.erase(it);
703 } else {
704 ++it;
705 }
706 }
707
ossu17e3fa12016-09-08 04:52:55 -0700708 // Update bandwidth estimate, if the packet is not comfort noise.
709 if (!packet_list.empty() &&
ossu7a377612016-10-18 04:06:13 -0700710 !decoder_database_->IsComfortNoise(main_payload_type)) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000711 // The list can be empty here if we got nothing but DTMF payloads.
ossu7a377612016-10-18 04:06:13 -0700712 AudioDecoder* decoder = decoder_database_->GetDecoder(main_payload_type);
713 RTC_DCHECK(decoder); // Should always get a valid object, since we have
714 // already checked that the payload types are known.
ossua73f6c92016-10-24 08:25:28 -0700715 decoder->IncomingPacket(packet_list.front().payload.data(),
716 packet_list.front().payload.size(),
717 packet_list.front().sequence_number,
718 packet_list.front().timestamp,
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000719 receive_timestamp);
720 }
721
ossu61a208b2016-09-20 01:38:00 -0700722 PacketList parsed_packet_list;
723 while (!packet_list.empty()) {
ossua73f6c92016-10-24 08:25:28 -0700724 Packet& packet = packet_list.front();
ossu61a208b2016-09-20 01:38:00 -0700725 const DecoderDatabase::DecoderInfo* info =
ossua73f6c92016-10-24 08:25:28 -0700726 decoder_database_->GetDecoderInfo(packet.payload_type);
ossu61a208b2016-09-20 01:38:00 -0700727 if (!info) {
728 LOG(LS_WARNING) << "SplitAudio unknown payload type";
729 return kUnknownRtpPayloadType;
730 }
731
732 if (info->IsComfortNoise()) {
733 // Carry comfort noise packets along.
ossua73f6c92016-10-24 08:25:28 -0700734 parsed_packet_list.splice(parsed_packet_list.end(), packet_list,
735 packet_list.begin());
ossu61a208b2016-09-20 01:38:00 -0700736 } else {
ossua73f6c92016-10-24 08:25:28 -0700737 const auto sequence_number = packet.sequence_number;
738 const auto payload_type = packet.payload_type;
739 const Packet::Priority original_priority = packet.priority;
740 auto packet_from_result = [&] (AudioDecoder::ParseResult& result) {
741 Packet new_packet;
742 new_packet.sequence_number = sequence_number;
743 new_packet.payload_type = payload_type;
744 new_packet.timestamp = result.timestamp;
745 new_packet.priority.codec_level = result.priority;
746 new_packet.priority.red_level = original_priority.red_level;
747 new_packet.frame = std::move(result.frame);
748 return new_packet;
749 };
750
ossu61a208b2016-09-20 01:38:00 -0700751 std::vector<AudioDecoder::ParseResult> results =
ossua73f6c92016-10-24 08:25:28 -0700752 info->GetDecoder()->ParsePayload(std::move(packet.payload),
753 packet.timestamp);
754 if (results.empty()) {
755 packet_list.pop_front();
756 } else {
757 bool first = true;
758 for (auto& result : results) {
759 RTC_DCHECK(result.frame);
760 RTC_DCHECK_GE(result.priority, 0);
761 if (first) {
762 // Re-use the node and move it to parsed_packet_list.
763 packet_list.front() = packet_from_result(result);
764 parsed_packet_list.splice(parsed_packet_list.end(), packet_list,
765 packet_list.begin());
766 first = false;
767 } else {
768 parsed_packet_list.push_back(packet_from_result(result));
769 }
ossu61a208b2016-09-20 01:38:00 -0700770 }
ossu61a208b2016-09-20 01:38:00 -0700771 }
772 }
773 }
774
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000775 // Insert packets in buffer.
henrik.lundin116c84e2015-08-27 13:14:48 -0700776 const size_t buffer_length_before_insert =
777 packet_buffer_->NumPacketsInBuffer();
ossua70695a2016-09-22 02:06:28 -0700778 const int ret = packet_buffer_->InsertPacketList(
ossu61a208b2016-09-20 01:38:00 -0700779 &parsed_packet_list, *decoder_database_, &current_rtp_payload_type_,
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000780 &current_cng_rtp_payload_type_);
781 if (ret == PacketBuffer::kFlushed) {
782 // Reset DSP timestamp etc. if packet buffer flushed.
783 new_codec_ = true;
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000784 update_sample_rate_and_channels = true;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000785 } else if (ret != PacketBuffer::kOK) {
minyue@webrtc.org7bb54362013-08-06 05:40:57 +0000786 return kOtherError;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000787 }
henrik.lundin@webrtc.org6ff3ac12014-11-20 14:14:49 +0000788
789 if (first_packet_) {
790 first_packet_ = false;
791 // Update the codec on the next GetAudio call.
792 new_codec_ = true;
793 }
794
henrik.lundinda8bbf62016-08-31 03:14:11 -0700795 if (current_rtp_payload_type_) {
796 RTC_DCHECK(decoder_database_->GetDecoderInfo(*current_rtp_payload_type_))
797 << "Payload type " << static_cast<int>(*current_rtp_payload_type_)
798 << " is unknown where it shouldn't be";
799 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000800
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000801 if (update_sample_rate_and_channels && !packet_buffer_->Empty()) {
802 // We do not use |current_rtp_payload_type_| to |set payload_type|, but
803 // get the next RTP header from |packet_buffer_| to obtain the payload type.
804 // The reason for it is the following corner case. If NetEq receives a
805 // CNG packet with a sample rate different than the current CNG then it
806 // flushes its buffer, assuming send codec must have been changed. However,
807 // payload type of the hypothetically new send codec is not known.
ossu7a377612016-10-18 04:06:13 -0700808 const Packet* next_packet = packet_buffer_->PeekNextPacket();
809 RTC_DCHECK(next_packet);
810 const int payload_type = next_packet->payload_type;
ossu97ba30e2016-04-25 07:55:58 -0700811 size_t channels = 1;
812 if (!decoder_database_->IsComfortNoise(payload_type)) {
813 AudioDecoder* decoder = decoder_database_->GetDecoder(payload_type);
814 assert(decoder); // Payloads are already checked to be valid.
815 channels = decoder->Channels();
816 }
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000817 const DecoderDatabase::DecoderInfo* decoder_info =
818 decoder_database_->GetDecoderInfo(payload_type);
819 assert(decoder_info);
kwibergc0f2dcf2016-05-31 06:28:03 -0700820 if (decoder_info->SampleRateHz() != fs_hz_ ||
ossu97ba30e2016-04-25 07:55:58 -0700821 channels != algorithm_buffer_->Channels()) {
kwibergc0f2dcf2016-05-31 06:28:03 -0700822 SetSampleRateAndChannels(decoder_info->SampleRateHz(),
823 channels);
henrik.lundin48ed9302015-10-29 05:36:24 -0700824 }
825 if (nack_enabled_) {
826 RTC_DCHECK(nack_);
827 // Update the sample rate even if the rate is not new, because of Reset().
828 nack_->UpdateSampleRate(fs_hz_);
829 }
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000830 }
831
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000832 // TODO(hlundin): Move this code to DelayManager class.
833 const DecoderDatabase::DecoderInfo* dec_info =
ossu7a377612016-10-18 04:06:13 -0700834 decoder_database_->GetDecoderInfo(main_payload_type);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000835 assert(dec_info); // Already checked that the payload type is known.
ossuf1b08da2016-09-23 02:19:43 -0700836 delay_manager_->LastDecodedWasCngOrDtmf(dec_info->IsComfortNoise() ||
837 dec_info->IsDtmf());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000838 if (delay_manager_->last_pack_cng_or_dtmf() == 0) {
839 // Calculate the total speech length carried in each packet.
henrik.lundin116c84e2015-08-27 13:14:48 -0700840 const size_t buffer_length_after_insert =
841 packet_buffer_->NumPacketsInBuffer();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000842
henrik.lundin116c84e2015-08-27 13:14:48 -0700843 if (buffer_length_after_insert > buffer_length_before_insert) {
844 const size_t packet_length_samples =
845 (buffer_length_after_insert - buffer_length_before_insert) *
846 decoder_frame_length_;
847 if (packet_length_samples != decision_logic_->packet_length_samples()) {
848 decision_logic_->set_packet_length_samples(packet_length_samples);
849 delay_manager_->SetPacketAudioLength(
kwibergd3edd772017-03-01 18:52:48 -0800850 rtc::dchecked_cast<int>((1000 * packet_length_samples) / fs_hz_));
henrik.lundin116c84e2015-08-27 13:14:48 -0700851 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000852 }
853
854 // Update statistics.
ossu7a377612016-10-18 04:06:13 -0700855 if ((int32_t)(main_timestamp - timestamp_) >= 0 && !new_codec_) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000856 // Only update statistics if incoming packet is not older than last played
857 // out packet, and if new codec flag is not set.
ossu7a377612016-10-18 04:06:13 -0700858 delay_manager_->Update(main_sequence_number, main_timestamp, fs_hz_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000859 }
860 } else if (delay_manager_->last_pack_cng_or_dtmf() == -1) {
861 // This is first "normal" packet after CNG or DTMF.
862 // Reset packet time counter and measure time until next packet,
863 // but don't update statistics.
864 delay_manager_->set_last_pack_cng_or_dtmf(0);
865 delay_manager_->ResetPacketIatCount();
866 }
867 return 0;
868}
869
henrik.lundin7a926812016-05-12 13:51:28 -0700870int NetEqImpl::GetAudioInternal(AudioFrame* audio_frame, bool* muted) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000871 PacketList packet_list;
872 DtmfEvent dtmf_event;
873 Operations operation;
874 bool play_dtmf;
henrik.lundin7a926812016-05-12 13:51:28 -0700875 *muted = false;
henrik.lundined497212016-04-25 10:11:38 -0700876 tick_timer_->Increment();
henrik.lundin60f6ce22016-05-10 03:52:04 -0700877 stats_.IncreaseCounter(output_size_samples_, fs_hz_);
henrik.lundin7a926812016-05-12 13:51:28 -0700878
879 // Check for muted state.
880 if (enable_muted_state_ && expand_->Muted() && packet_buffer_->Empty()) {
881 RTC_DCHECK_EQ(last_mode_, kModeExpand);
882 playout_timestamp_ += static_cast<uint32_t>(output_size_samples_);
883 audio_frame->sample_rate_hz_ = fs_hz_;
884 audio_frame->samples_per_channel_ = output_size_samples_;
885 audio_frame->timestamp_ =
886 first_packet_
887 ? 0
888 : timestamp_scaler_->ToExternal(playout_timestamp_) -
889 static_cast<uint32_t>(audio_frame->samples_per_channel_);
890 audio_frame->num_channels_ = sync_buffer_->Channels();
henrik.lundin612c25e2016-05-25 08:21:04 -0700891 stats_.ExpandedNoiseSamples(output_size_samples_);
henrik.lundin7a926812016-05-12 13:51:28 -0700892 *muted = true;
893 return 0;
894 }
895
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000896 int return_value = GetDecision(&operation, &packet_list, &dtmf_event,
897 &play_dtmf);
898 if (return_value != 0) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000899 last_mode_ = kModeError;
900 return return_value;
901 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000902
903 AudioDecoder::SpeechType speech_type;
904 int length = 0;
905 int decode_return_value = Decode(&packet_list, &operation,
906 &length, &speech_type);
907
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000908 assert(vad_.get());
909 bool sid_frame_available =
910 (operation == kRfc3389Cng && !packet_list.empty());
Peter Kastingdce40cf2015-08-24 14:52:23 -0700911 vad_->Update(decoded_buffer_.get(), static_cast<size_t>(length), speech_type,
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000912 sid_frame_available, fs_hz_);
913
henrik.lundinb1fb72b2016-05-03 08:18:47 -0700914 if (sid_frame_available || speech_type == AudioDecoder::kComfortNoise) {
915 // Start a new stopwatch since we are decoding a new CNG packet.
916 generated_noise_stopwatch_ = tick_timer_->GetNewStopwatch();
917 }
918
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +0000919 algorithm_buffer_->Clear();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000920 switch (operation) {
921 case kNormal: {
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +0000922 DoNormal(decoded_buffer_.get(), length, speech_type, play_dtmf);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000923 break;
924 }
925 case kMerge: {
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +0000926 DoMerge(decoded_buffer_.get(), length, speech_type, play_dtmf);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000927 break;
928 }
929 case kExpand: {
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +0000930 return_value = DoExpand(play_dtmf);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000931 break;
932 }
Henrik Lundincf808d22015-05-27 14:33:29 +0200933 case kAccelerate:
934 case kFastAccelerate: {
935 const bool fast_accelerate =
936 enable_fast_accelerate_ && (operation == kFastAccelerate);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000937 return_value = DoAccelerate(decoded_buffer_.get(), length, speech_type,
Henrik Lundincf808d22015-05-27 14:33:29 +0200938 play_dtmf, fast_accelerate);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000939 break;
940 }
941 case kPreemptiveExpand: {
942 return_value = DoPreemptiveExpand(decoded_buffer_.get(), length,
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +0000943 speech_type, play_dtmf);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000944 break;
945 }
946 case kRfc3389Cng:
947 case kRfc3389CngNoPacket: {
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +0000948 return_value = DoRfc3389Cng(&packet_list, play_dtmf);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000949 break;
950 }
951 case kCodecInternalCng: {
952 // This handles the case when there is no transmission and the decoder
953 // should produce internal comfort noise.
954 // TODO(hlundin): Write test for codec-internal CNG.
minyuel6d92bf52015-09-23 15:20:39 +0200955 DoCodecInternalCng(decoded_buffer_.get(), length);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000956 break;
957 }
958 case kDtmf: {
959 // TODO(hlundin): Write test for this.
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +0000960 return_value = DoDtmf(dtmf_event, &play_dtmf);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000961 break;
962 }
963 case kAlternativePlc: {
964 // TODO(hlundin): Write test for this.
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +0000965 DoAlternativePlc(false);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000966 break;
967 }
968 case kAlternativePlcIncreaseTimestamp: {
969 // TODO(hlundin): Write test for this.
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +0000970 DoAlternativePlc(true);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000971 break;
972 }
973 case kAudioRepetitionIncreaseTimestamp: {
974 // TODO(hlundin): Write test for this.
Peter Kastingb7e50542015-06-11 12:55:50 -0700975 sync_buffer_->IncreaseEndTimestamp(
976 static_cast<uint32_t>(output_size_samples_));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000977 // Skipping break on purpose. Execution should move on into the
978 // next case.
kjellander@webrtc.org7d2b6a92015-01-28 18:37:58 +0000979 FALLTHROUGH();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000980 }
981 case kAudioRepetition: {
982 // TODO(hlundin): Write test for this.
983 // Copy last |output_size_samples_| from |sync_buffer_| to
984 // |algorithm_buffer|.
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +0000985 algorithm_buffer_->PushBackFromIndex(
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000986 *sync_buffer_, sync_buffer_->Size() - output_size_samples_);
987 expand_->Reset();
988 break;
989 }
990 case kUndefined: {
Henrik Lundind67a2192015-08-03 12:54:37 +0200991 LOG(LS_ERROR) << "Invalid operation kUndefined.";
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000992 assert(false); // This should not happen.
993 last_mode_ = kModeError;
994 return kInvalidOperation;
995 }
996 } // End of switch.
minyue5bd33972016-05-02 04:46:11 -0700997 last_operation_ = operation;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000998 if (return_value < 0) {
999 return return_value;
1000 }
1001
1002 if (last_mode_ != kModeRfc3389Cng) {
1003 comfort_noise_->Reset();
1004 }
1005
1006 // Copy from |algorithm_buffer| to |sync_buffer_|.
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001007 sync_buffer_->PushBack(*algorithm_buffer_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001008
1009 // Extract data from |sync_buffer_| to |output|.
turaj@webrtc.org362a55e2013-09-20 16:25:28 +00001010 size_t num_output_samples_per_channel = output_size_samples_;
1011 size_t num_output_samples = output_size_samples_ * sync_buffer_->Channels();
henrik.lundin6d8e0112016-03-04 10:34:21 -08001012 if (num_output_samples > AudioFrame::kMaxDataSizeSamples) {
1013 LOG(LS_WARNING) << "Output array is too short. "
1014 << AudioFrame::kMaxDataSizeSamples << " < "
1015 << output_size_samples_ << " * "
1016 << sync_buffer_->Channels();
1017 num_output_samples = AudioFrame::kMaxDataSizeSamples;
1018 num_output_samples_per_channel =
1019 AudioFrame::kMaxDataSizeSamples / sync_buffer_->Channels();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001020 }
henrik.lundin6d8e0112016-03-04 10:34:21 -08001021 sync_buffer_->GetNextAudioInterleaved(num_output_samples_per_channel,
1022 audio_frame);
1023 audio_frame->sample_rate_hz_ = fs_hz_;
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001024 if (sync_buffer_->FutureLength() < expand_->overlap_length()) {
1025 // The sync buffer should always contain |overlap_length| samples, but now
1026 // too many samples have been extracted. Reinstall the |overlap_length|
1027 // lookahead by moving the index.
1028 const size_t missing_lookahead_samples =
1029 expand_->overlap_length() - sync_buffer_->FutureLength();
henrikg91d6ede2015-09-17 00:24:34 -07001030 RTC_DCHECK_GE(sync_buffer_->next_index(), missing_lookahead_samples);
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001031 sync_buffer_->set_next_index(sync_buffer_->next_index() -
1032 missing_lookahead_samples);
1033 }
henrik.lundin6d8e0112016-03-04 10:34:21 -08001034 if (audio_frame->samples_per_channel_ != output_size_samples_) {
1035 LOG(LS_ERROR) << "audio_frame->samples_per_channel_ ("
1036 << audio_frame->samples_per_channel_
Henrik Lundind67a2192015-08-03 12:54:37 +02001037 << ") != output_size_samples_ (" << output_size_samples_
1038 << ")";
minyue@webrtc.orgdb1cefc2013-08-13 01:39:21 +00001039 // TODO(minyue): treatment of under-run, filling zeros
henrik.lundin6d8e0112016-03-04 10:34:21 -08001040 memset(audio_frame->data_, 0, num_output_samples * sizeof(int16_t));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001041 return kSampleUnderrun;
1042 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001043
1044 // Should always have overlap samples left in the |sync_buffer_|.
henrikg91d6ede2015-09-17 00:24:34 -07001045 RTC_DCHECK_GE(sync_buffer_->FutureLength(), expand_->overlap_length());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001046
1047 if (play_dtmf) {
henrik.lundin6d8e0112016-03-04 10:34:21 -08001048 return_value =
1049 DtmfOverdub(dtmf_event, sync_buffer_->Channels(), audio_frame->data_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001050 }
1051
1052 // Update the background noise parameters if last operation wrote data
1053 // straight from the decoder to the |sync_buffer_|. That is, none of the
1054 // operations that modify the signal can be followed by a parameter update.
1055 if ((last_mode_ == kModeNormal) ||
1056 (last_mode_ == kModeAccelerateFail) ||
1057 (last_mode_ == kModePreemptiveExpandFail) ||
1058 (last_mode_ == kModeRfc3389Cng) ||
1059 (last_mode_ == kModeCodecInternalCng)) {
1060 background_noise_->Update(*sync_buffer_, *vad_.get());
1061 }
1062
1063 if (operation == kDtmf) {
1064 // DTMF data was written the end of |sync_buffer_|.
1065 // Update index to end of DTMF data in |sync_buffer_|.
1066 sync_buffer_->set_dtmf_index(sync_buffer_->Size());
1067 }
1068
henrik.lundin@webrtc.orged865b52014-03-06 10:28:07 +00001069 if (last_mode_ != kModeExpand) {
1070 // If last operation was not expand, calculate the |playout_timestamp_| from
1071 // the |sync_buffer_|. However, do not update the |playout_timestamp_| if it
1072 // would be moved "backwards".
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001073 uint32_t temp_timestamp = sync_buffer_->end_timestamp() -
turaj@webrtc.org362a55e2013-09-20 16:25:28 +00001074 static_cast<uint32_t>(sync_buffer_->FutureLength());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001075 if (static_cast<int32_t>(temp_timestamp - playout_timestamp_) > 0) {
1076 playout_timestamp_ = temp_timestamp;
1077 }
1078 } else {
1079 // Use dead reckoning to estimate the |playout_timestamp_|.
Peter Kastingb7e50542015-06-11 12:55:50 -07001080 playout_timestamp_ += static_cast<uint32_t>(output_size_samples_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001081 }
henrik.lundin15c51e32016-04-06 08:38:56 -07001082 // Set the timestamp in the audio frame to zero before the first packet has
1083 // been inserted. Otherwise, subtract the frame size in samples to get the
1084 // timestamp of the first sample in the frame (playout_timestamp_ is the
1085 // last + 1).
1086 audio_frame->timestamp_ =
1087 first_packet_
1088 ? 0
1089 : timestamp_scaler_->ToExternal(playout_timestamp_) -
1090 static_cast<uint32_t>(audio_frame->samples_per_channel_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001091
henrik.lundinb1fb72b2016-05-03 08:18:47 -07001092 if (!(last_mode_ == kModeRfc3389Cng ||
1093 last_mode_ == kModeCodecInternalCng ||
1094 last_mode_ == kModeExpand)) {
1095 generated_noise_stopwatch_.reset();
1096 }
1097
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001098 if (decode_return_value) return decode_return_value;
1099 return return_value;
1100}
1101
1102int NetEqImpl::GetDecision(Operations* operation,
1103 PacketList* packet_list,
1104 DtmfEvent* dtmf_event,
1105 bool* play_dtmf) {
1106 // Initialize output variables.
1107 *play_dtmf = false;
1108 *operation = kUndefined;
1109
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +00001110 assert(sync_buffer_.get());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001111 uint32_t end_timestamp = sync_buffer_->end_timestamp();
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +00001112 if (!new_codec_) {
1113 const uint32_t five_seconds_samples = 5 * fs_hz_;
1114 packet_buffer_->DiscardOldPackets(end_timestamp, five_seconds_samples);
1115 }
ossu7a377612016-10-18 04:06:13 -07001116 const Packet* packet = packet_buffer_->PeekNextPacket();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001117
henrik.lundinb1fb72b2016-05-03 08:18:47 -07001118 RTC_DCHECK(!generated_noise_stopwatch_ ||
1119 generated_noise_stopwatch_->ElapsedTicks() >= 1);
1120 uint64_t generated_noise_samples =
1121 generated_noise_stopwatch_
1122 ? (generated_noise_stopwatch_->ElapsedTicks() - 1) *
1123 output_size_samples_ +
1124 decision_logic_->noise_fast_forward()
1125 : 0;
1126
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001127 if (decision_logic_->CngRfc3389On() || last_mode_ == kModeRfc3389Cng) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001128 // Because of timestamp peculiarities, we have to "manually" disallow using
1129 // a CNG packet with the same timestamp as the one that was last played.
1130 // This can happen when using redundancy and will cause the timing to shift.
ossu7a377612016-10-18 04:06:13 -07001131 while (packet && decoder_database_->IsComfortNoise(packet->payload_type) &&
1132 (end_timestamp >= packet->timestamp ||
1133 end_timestamp + generated_noise_samples > packet->timestamp)) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001134 // Don't use this packet, discard it.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001135 if (packet_buffer_->DiscardNextPacket() != PacketBuffer::kOK) {
1136 assert(false); // Must be ok by design.
1137 }
1138 // Check buffer again.
1139 if (!new_codec_) {
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +00001140 packet_buffer_->DiscardOldPackets(end_timestamp, 5 * fs_hz_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001141 }
ossu7a377612016-10-18 04:06:13 -07001142 packet = packet_buffer_->PeekNextPacket();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001143 }
1144 }
1145
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +00001146 assert(expand_.get());
turaj@webrtc.org362a55e2013-09-20 16:25:28 +00001147 const int samples_left = static_cast<int>(sync_buffer_->FutureLength() -
1148 expand_->overlap_length());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001149 if (last_mode_ == kModeAccelerateSuccess ||
1150 last_mode_ == kModeAccelerateLowEnergy ||
1151 last_mode_ == kModePreemptiveExpandSuccess ||
1152 last_mode_ == kModePreemptiveExpandLowEnergy) {
1153 // Subtract (samples_left + output_size_samples_) from sampleMemory.
Peter Kastingdce40cf2015-08-24 14:52:23 -07001154 decision_logic_->AddSampleMemory(
kwibergd3edd772017-03-01 18:52:48 -08001155 -(samples_left + rtc::dchecked_cast<int>(output_size_samples_)));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001156 }
1157
1158 // Check if it is time to play a DTMF event.
Peter Kastingb7e50542015-06-11 12:55:50 -07001159 if (dtmf_buffer_->GetEvent(
1160 static_cast<uint32_t>(
henrik.lundinb1fb72b2016-05-03 08:18:47 -07001161 end_timestamp + generated_noise_samples),
Peter Kastingb7e50542015-06-11 12:55:50 -07001162 dtmf_event)) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001163 *play_dtmf = true;
1164 }
1165
1166 // Get instruction.
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +00001167 assert(sync_buffer_.get());
1168 assert(expand_.get());
henrik.lundinb1fb72b2016-05-03 08:18:47 -07001169 generated_noise_samples =
1170 generated_noise_stopwatch_
1171 ? generated_noise_stopwatch_->ElapsedTicks() * output_size_samples_ +
1172 decision_logic_->noise_fast_forward()
1173 : 0;
1174 *operation = decision_logic_->GetDecision(
ossu7a377612016-10-18 04:06:13 -07001175 *sync_buffer_, *expand_, decoder_frame_length_, packet, last_mode_,
henrik.lundinb1fb72b2016-05-03 08:18:47 -07001176 *play_dtmf, generated_noise_samples, &reset_decoder_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001177
1178 // Check if we already have enough samples in the |sync_buffer_|. If so,
1179 // change decision to normal, unless the decision was merge, accelerate, or
1180 // preemptive expand.
kwibergd3edd772017-03-01 18:52:48 -08001181 if (samples_left >= rtc::dchecked_cast<int>(output_size_samples_) &&
1182 *operation != kMerge && *operation != kAccelerate &&
1183 *operation != kFastAccelerate && *operation != kPreemptiveExpand) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001184 *operation = kNormal;
1185 return 0;
1186 }
1187
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001188 decision_logic_->ExpandDecision(*operation);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001189
1190 // Check conditions for reset.
1191 if (new_codec_ || *operation == kUndefined) {
1192 // The only valid reason to get kUndefined is that new_codec_ is set.
1193 assert(new_codec_);
ossu7a377612016-10-18 04:06:13 -07001194 if (*play_dtmf && !packet) {
turaj@webrtc.org4d06db52013-03-27 18:31:42 +00001195 timestamp_ = dtmf_event->timestamp;
1196 } else {
ossu7a377612016-10-18 04:06:13 -07001197 if (!packet) {
Henrik Lundind67a2192015-08-03 12:54:37 +02001198 LOG(LS_ERROR) << "Packet missing where it shouldn't.";
turaj@webrtc.org4d06db52013-03-27 18:31:42 +00001199 return -1;
1200 }
ossu7a377612016-10-18 04:06:13 -07001201 timestamp_ = packet->timestamp;
ossu108ecec2016-07-08 08:45:18 -07001202 if (*operation == kRfc3389CngNoPacket &&
ossu7a377612016-10-18 04:06:13 -07001203 decoder_database_->IsComfortNoise(packet->payload_type)) {
turaj@webrtc.org4d06db52013-03-27 18:31:42 +00001204 // Change decision to CNG packet, since we do have a CNG packet, but it
1205 // was considered too early to use. Now, use it anyway.
1206 *operation = kRfc3389Cng;
1207 } else if (*operation != kRfc3389Cng) {
1208 *operation = kNormal;
1209 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001210 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001211 // Adjust |sync_buffer_| timestamp before setting |end_timestamp| to the
1212 // new value.
1213 sync_buffer_->IncreaseEndTimestamp(timestamp_ - end_timestamp);
turaj@webrtc.org4d06db52013-03-27 18:31:42 +00001214 end_timestamp = timestamp_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001215 new_codec_ = false;
1216 decision_logic_->SoftReset();
1217 buffer_level_filter_->Reset();
1218 delay_manager_->Reset();
1219 stats_.ResetMcu();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001220 }
1221
Peter Kastingdce40cf2015-08-24 14:52:23 -07001222 size_t required_samples = output_size_samples_;
1223 const size_t samples_10_ms = static_cast<size_t>(80 * fs_mult_);
1224 const size_t samples_20_ms = 2 * samples_10_ms;
1225 const size_t samples_30_ms = 3 * samples_10_ms;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001226
1227 switch (*operation) {
1228 case kExpand: {
1229 timestamp_ = end_timestamp;
1230 return 0;
1231 }
1232 case kRfc3389CngNoPacket:
1233 case kCodecInternalCng: {
1234 return 0;
1235 }
1236 case kDtmf: {
1237 // TODO(hlundin): Write test for this.
1238 // Update timestamp.
1239 timestamp_ = end_timestamp;
henrik.lundinb1fb72b2016-05-03 08:18:47 -07001240 const uint64_t generated_noise_samples =
1241 generated_noise_stopwatch_
1242 ? generated_noise_stopwatch_->ElapsedTicks() *
1243 output_size_samples_ +
1244 decision_logic_->noise_fast_forward()
1245 : 0;
1246 if (generated_noise_samples > 0 && last_mode_ != kModeDtmf) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001247 // Make a jump in timestamp due to the recently played comfort noise.
Peter Kastingb7e50542015-06-11 12:55:50 -07001248 uint32_t timestamp_jump =
henrik.lundinb1fb72b2016-05-03 08:18:47 -07001249 static_cast<uint32_t>(generated_noise_samples);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001250 sync_buffer_->IncreaseEndTimestamp(timestamp_jump);
1251 timestamp_ += timestamp_jump;
1252 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001253 return 0;
1254 }
Henrik Lundincf808d22015-05-27 14:33:29 +02001255 case kAccelerate:
1256 case kFastAccelerate: {
1257 // In order to do an accelerate we need at least 30 ms of audio data.
Peter Kastingdce40cf2015-08-24 14:52:23 -07001258 if (samples_left >= static_cast<int>(samples_30_ms)) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001259 // Already have enough data, so we do not need to extract any more.
1260 decision_logic_->set_sample_memory(samples_left);
1261 decision_logic_->set_prev_time_scale(true);
1262 return 0;
Peter Kastingdce40cf2015-08-24 14:52:23 -07001263 } else if (samples_left >= static_cast<int>(samples_10_ms) &&
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001264 decoder_frame_length_ >= samples_30_ms) {
1265 // Avoid decoding more data as it might overflow the playout buffer.
1266 *operation = kNormal;
1267 return 0;
Peter Kastingdce40cf2015-08-24 14:52:23 -07001268 } else if (samples_left < static_cast<int>(samples_20_ms) &&
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001269 decoder_frame_length_ < samples_30_ms) {
1270 // Build up decoded data by decoding at least 20 ms of audio data. Do
1271 // not perform accelerate yet, but wait until we only need to do one
1272 // decoding.
1273 required_samples = 2 * output_size_samples_;
1274 *operation = kNormal;
1275 }
1276 // If none of the above is true, we have one of two possible situations:
1277 // (1) 20 ms <= samples_left < 30 ms and decoder_frame_length_ < 30 ms; or
1278 // (2) samples_left < 10 ms and decoder_frame_length_ >= 30 ms.
1279 // In either case, we move on with the accelerate decision, and decode one
1280 // frame now.
1281 break;
1282 }
1283 case kPreemptiveExpand: {
1284 // In order to do a preemptive expand we need at least 30 ms of decoded
1285 // audio data.
Peter Kastingdce40cf2015-08-24 14:52:23 -07001286 if ((samples_left >= static_cast<int>(samples_30_ms)) ||
1287 (samples_left >= static_cast<int>(samples_10_ms) &&
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001288 decoder_frame_length_ >= samples_30_ms)) {
1289 // Already have enough data, so we do not need to extract any more.
1290 // Or, avoid decoding more data as it might overflow the playout buffer.
1291 // Still try preemptive expand, though.
1292 decision_logic_->set_sample_memory(samples_left);
1293 decision_logic_->set_prev_time_scale(true);
1294 return 0;
1295 }
Peter Kastingdce40cf2015-08-24 14:52:23 -07001296 if (samples_left < static_cast<int>(samples_20_ms) &&
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001297 decoder_frame_length_ < samples_30_ms) {
1298 // Build up decoded data by decoding at least 20 ms of audio data.
1299 // Still try to perform preemptive expand.
1300 required_samples = 2 * output_size_samples_;
1301 }
1302 // Move on with the preemptive expand decision.
1303 break;
1304 }
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001305 case kMerge: {
1306 required_samples =
1307 std::max(merge_->RequiredFutureSamples(), required_samples);
1308 break;
1309 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001310 default: {
1311 // Do nothing.
1312 }
1313 }
1314
1315 // Get packets from buffer.
1316 int extracted_samples = 0;
ossu7a377612016-10-18 04:06:13 -07001317 if (packet && *operation != kAlternativePlc &&
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001318 *operation != kAlternativePlcIncreaseTimestamp &&
1319 *operation != kAudioRepetition &&
1320 *operation != kAudioRepetitionIncreaseTimestamp) {
ossu7a377612016-10-18 04:06:13 -07001321 sync_buffer_->IncreaseEndTimestamp(packet->timestamp - end_timestamp);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001322 if (decision_logic_->CngOff()) {
1323 // Adjustment of timestamp only corresponds to an actual packet loss
1324 // if comfort noise is not played. If comfort noise was just played,
1325 // this adjustment of timestamp is only done to get back in sync with the
1326 // stream timestamp; no loss to report.
ossu7a377612016-10-18 04:06:13 -07001327 stats_.LostSamples(packet->timestamp - end_timestamp);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001328 }
1329
1330 if (*operation != kRfc3389Cng) {
1331 // We are about to decode and use a non-CNG packet.
1332 decision_logic_->SetCngOff();
1333 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001334
1335 extracted_samples = ExtractPackets(required_samples, packet_list);
1336 if (extracted_samples < 0) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001337 return kPacketBufferCorruption;
1338 }
1339 }
1340
Henrik Lundincf808d22015-05-27 14:33:29 +02001341 if (*operation == kAccelerate || *operation == kFastAccelerate ||
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001342 *operation == kPreemptiveExpand) {
1343 decision_logic_->set_sample_memory(samples_left + extracted_samples);
1344 decision_logic_->set_prev_time_scale(true);
1345 }
1346
Henrik Lundincf808d22015-05-27 14:33:29 +02001347 if (*operation == kAccelerate || *operation == kFastAccelerate) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001348 // Check that we have enough data (30ms) to do accelerate.
Peter Kastingdce40cf2015-08-24 14:52:23 -07001349 if (extracted_samples + samples_left < static_cast<int>(samples_30_ms)) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001350 // TODO(hlundin): Write test for this.
1351 // Not enough, do normal operation instead.
1352 *operation = kNormal;
1353 }
1354 }
1355
1356 timestamp_ = end_timestamp;
1357 return 0;
1358}
1359
1360int NetEqImpl::Decode(PacketList* packet_list, Operations* operation,
1361 int* decoded_length,
1362 AudioDecoder::SpeechType* speech_type) {
1363 *speech_type = AudioDecoder::kSpeech;
minyuel6d92bf52015-09-23 15:20:39 +02001364
1365 // When packet_list is empty, we may be in kCodecInternalCng mode, and for
1366 // that we use current active decoder.
1367 AudioDecoder* decoder = decoder_database_->GetActiveDecoder();
1368
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001369 if (!packet_list->empty()) {
ossua73f6c92016-10-24 08:25:28 -07001370 const Packet& packet = packet_list->front();
1371 uint8_t payload_type = packet.payload_type;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001372 if (!decoder_database_->IsComfortNoise(payload_type)) {
1373 decoder = decoder_database_->GetDecoder(payload_type);
1374 assert(decoder);
1375 if (!decoder) {
Henrik Lundind67a2192015-08-03 12:54:37 +02001376 LOG(LS_WARNING) << "Unknown payload type "
1377 << static_cast<int>(payload_type);
ossua73f6c92016-10-24 08:25:28 -07001378 packet_list->clear();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001379 return kDecoderNotFound;
1380 }
1381 bool decoder_changed;
1382 decoder_database_->SetActiveDecoder(payload_type, &decoder_changed);
1383 if (decoder_changed) {
1384 // We have a new decoder. Re-init some values.
1385 const DecoderDatabase::DecoderInfo* decoder_info = decoder_database_
1386 ->GetDecoderInfo(payload_type);
1387 assert(decoder_info);
1388 if (!decoder_info) {
Henrik Lundind67a2192015-08-03 12:54:37 +02001389 LOG(LS_WARNING) << "Unknown payload type "
1390 << static_cast<int>(payload_type);
ossua73f6c92016-10-24 08:25:28 -07001391 packet_list->clear();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001392 return kDecoderNotFound;
1393 }
tina.legrand@webrtc.orgba5a6c32014-03-23 09:58:48 +00001394 // If sampling rate or number of channels has changed, we need to make
1395 // a reset.
kwibergc0f2dcf2016-05-31 06:28:03 -07001396 if (decoder_info->SampleRateHz() != fs_hz_ ||
henrik.lundin@webrtc.org6dba1eb2015-03-18 09:47:08 +00001397 decoder->Channels() != algorithm_buffer_->Channels()) {
tina.legrand@webrtc.orgba5a6c32014-03-23 09:58:48 +00001398 // TODO(tlegrand): Add unittest to cover this event.
kwibergc0f2dcf2016-05-31 06:28:03 -07001399 SetSampleRateAndChannels(decoder_info->SampleRateHz(),
1400 decoder->Channels());
turaj@webrtc.orga6101d72013-10-01 22:01:09 +00001401 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001402 sync_buffer_->set_end_timestamp(timestamp_);
1403 playout_timestamp_ = timestamp_;
1404 }
1405 }
1406 }
1407
1408 if (reset_decoder_) {
1409 // TODO(hlundin): Write test for this.
Karl Wiberg43766482015-08-27 15:22:11 +02001410 if (decoder)
1411 decoder->Reset();
1412
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001413 // Reset comfort noise decoder.
ossu97ba30e2016-04-25 07:55:58 -07001414 ComfortNoiseDecoder* cng_decoder = decoder_database_->GetActiveCngDecoder();
Karl Wiberg43766482015-08-27 15:22:11 +02001415 if (cng_decoder)
1416 cng_decoder->Reset();
1417
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001418 reset_decoder_ = false;
1419 }
1420
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001421 *decoded_length = 0;
1422 // Update codec-internal PLC state.
1423 if ((*operation == kMerge) && decoder && decoder->HasDecodePlc()) {
1424 decoder->DecodePlc(1, &decoded_buffer_[*decoded_length]);
1425 }
1426
minyuel6d92bf52015-09-23 15:20:39 +02001427 int return_value;
1428 if (*operation == kCodecInternalCng) {
1429 RTC_DCHECK(packet_list->empty());
1430 return_value = DecodeCng(decoder, decoded_length, speech_type);
1431 } else {
1432 return_value = DecodeLoop(packet_list, *operation, decoder,
1433 decoded_length, speech_type);
1434 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001435
1436 if (*decoded_length < 0) {
1437 // Error returned from the decoder.
1438 *decoded_length = 0;
Peter Kastingb7e50542015-06-11 12:55:50 -07001439 sync_buffer_->IncreaseEndTimestamp(
1440 static_cast<uint32_t>(decoder_frame_length_));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001441 int error_code = 0;
1442 if (decoder)
1443 error_code = decoder->ErrorCode();
1444 if (error_code != 0) {
1445 // Got some error code from the decoder.
1446 decoder_error_code_ = error_code;
1447 return_value = kDecoderErrorCode;
Henrik Lundind67a2192015-08-03 12:54:37 +02001448 LOG(LS_WARNING) << "Decoder returned error code: " << error_code;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001449 } else {
1450 // Decoder does not implement error codes. Return generic error.
1451 return_value = kOtherDecoderError;
Henrik Lundind67a2192015-08-03 12:54:37 +02001452 LOG(LS_WARNING) << "Decoder error (no error code)";
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001453 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001454 *operation = kExpand; // Do expansion to get data instead.
1455 }
1456 if (*speech_type != AudioDecoder::kComfortNoise) {
1457 // Don't increment timestamp if codec returned CNG speech type
1458 // since in this case, the we will increment the CNGplayedTS counter.
1459 // Increase with number of samples per channel.
1460 assert(*decoded_length == 0 ||
henrik.lundin@webrtc.org6dba1eb2015-03-18 09:47:08 +00001461 (decoder && decoder->Channels() == sync_buffer_->Channels()));
turaj@webrtc.org362a55e2013-09-20 16:25:28 +00001462 sync_buffer_->IncreaseEndTimestamp(
1463 *decoded_length / static_cast<int>(sync_buffer_->Channels()));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001464 }
1465 return return_value;
1466}
1467
minyuel6d92bf52015-09-23 15:20:39 +02001468int NetEqImpl::DecodeCng(AudioDecoder* decoder, int* decoded_length,
1469 AudioDecoder::SpeechType* speech_type) {
1470 if (!decoder) {
1471 // This happens when active decoder is not defined.
1472 *decoded_length = -1;
1473 return 0;
1474 }
1475
kwibergd3edd772017-03-01 18:52:48 -08001476 while (*decoded_length < rtc::dchecked_cast<int>(output_size_samples_)) {
minyuel6d92bf52015-09-23 15:20:39 +02001477 const int length = decoder->Decode(
1478 nullptr, 0, fs_hz_,
1479 (decoded_buffer_length_ - *decoded_length) * sizeof(int16_t),
1480 &decoded_buffer_[*decoded_length], speech_type);
1481 if (length > 0) {
1482 *decoded_length += length;
minyuel6d92bf52015-09-23 15:20:39 +02001483 } else {
1484 // Error.
1485 LOG(LS_WARNING) << "Failed to decode CNG";
1486 *decoded_length = -1;
1487 break;
1488 }
1489 if (*decoded_length > static_cast<int>(decoded_buffer_length_)) {
1490 // Guard against overflow.
1491 LOG(LS_WARNING) << "Decoded too much CNG.";
1492 return kDecodedTooMuch;
1493 }
1494 }
1495 return 0;
1496}
1497
1498int NetEqImpl::DecodeLoop(PacketList* packet_list, const Operations& operation,
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001499 AudioDecoder* decoder, int* decoded_length,
1500 AudioDecoder::SpeechType* speech_type) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001501 // Do decoding.
ossua73f6c92016-10-24 08:25:28 -07001502 while (
1503 !packet_list->empty() &&
1504 !decoder_database_->IsComfortNoise(packet_list->front().payload_type)) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001505 assert(decoder); // At this point, we must have a decoder object.
1506 // The number of channels in the |sync_buffer_| should be the same as the
1507 // number decoder channels.
henrik.lundin@webrtc.org6dba1eb2015-03-18 09:47:08 +00001508 assert(sync_buffer_->Channels() == decoder->Channels());
1509 assert(decoded_buffer_length_ >= kMaxFrameSize * decoder->Channels());
minyuel6d92bf52015-09-23 15:20:39 +02001510 assert(operation == kNormal || operation == kAccelerate ||
1511 operation == kFastAccelerate || operation == kMerge ||
1512 operation == kPreemptiveExpand);
ossua73f6c92016-10-24 08:25:28 -07001513
1514 auto opt_result = packet_list->front().frame->Decode(
ossu61a208b2016-09-20 01:38:00 -07001515 rtc::ArrayView<int16_t>(&decoded_buffer_[*decoded_length],
1516 decoded_buffer_length_ - *decoded_length));
ossua73f6c92016-10-24 08:25:28 -07001517 packet_list->pop_front();
ossu61a208b2016-09-20 01:38:00 -07001518 if (opt_result) {
1519 const auto& result = *opt_result;
1520 *speech_type = result.speech_type;
1521 if (result.num_decoded_samples > 0) {
kwibergd3edd772017-03-01 18:52:48 -08001522 *decoded_length += rtc::dchecked_cast<int>(result.num_decoded_samples);
ossu61a208b2016-09-20 01:38:00 -07001523 // Update |decoder_frame_length_| with number of samples per channel.
1524 decoder_frame_length_ =
1525 result.num_decoded_samples / decoder->Channels();
1526 }
1527 } else {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001528 // Error.
ossu61a208b2016-09-20 01:38:00 -07001529 // TODO(ossu): What to put here?
1530 LOG(LS_WARNING) << "Decode error";
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001531 *decoded_length = -1;
ossua73f6c92016-10-24 08:25:28 -07001532 packet_list->clear();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001533 break;
1534 }
kwibergd3edd772017-03-01 18:52:48 -08001535 if (*decoded_length > rtc::dchecked_cast<int>(decoded_buffer_length_)) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001536 // Guard against overflow.
Henrik Lundind67a2192015-08-03 12:54:37 +02001537 LOG(LS_WARNING) << "Decoded too much.";
ossua73f6c92016-10-24 08:25:28 -07001538 packet_list->clear();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001539 return kDecodedTooMuch;
1540 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001541 } // End of decode loop.
1542
turaj@webrtc.org58cd3162013-10-31 15:15:55 +00001543 // If the list is not empty at this point, either a decoding error terminated
1544 // the while-loop, or list must hold exactly one CNG packet.
ossua73f6c92016-10-24 08:25:28 -07001545 assert(
1546 packet_list->empty() || *decoded_length < 0 ||
1547 (packet_list->size() == 1 &&
1548 decoder_database_->IsComfortNoise(packet_list->front().payload_type)));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001549 return 0;
1550}
1551
1552void NetEqImpl::DoNormal(const int16_t* decoded_buffer, size_t decoded_length,
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001553 AudioDecoder::SpeechType speech_type, bool play_dtmf) {
henrik.lundin@webrtc.org40d3fc62013-09-18 12:19:50 +00001554 assert(normal_.get());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001555 assert(mute_factor_array_.get());
henrik.lundin@webrtc.org40d3fc62013-09-18 12:19:50 +00001556 normal_->Process(decoded_buffer, decoded_length, last_mode_,
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +00001557 mute_factor_array_.get(), algorithm_buffer_.get());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001558 if (decoded_length != 0) {
1559 last_mode_ = kModeNormal;
1560 }
1561
1562 // If last packet was decoded as an inband CNG, set mode to CNG instead.
1563 if ((speech_type == AudioDecoder::kComfortNoise)
1564 || ((last_mode_ == kModeCodecInternalCng)
1565 && (decoded_length == 0))) {
1566 // TODO(hlundin): Remove second part of || statement above.
1567 last_mode_ = kModeCodecInternalCng;
1568 }
1569
1570 if (!play_dtmf) {
1571 dtmf_tone_generator_->Reset();
1572 }
1573}
1574
1575void NetEqImpl::DoMerge(int16_t* decoded_buffer, size_t decoded_length,
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001576 AudioDecoder::SpeechType speech_type, bool play_dtmf) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001577 assert(mute_factor_array_.get());
henrik.lundin@webrtc.org40d3fc62013-09-18 12:19:50 +00001578 assert(merge_.get());
Peter Kastingdce40cf2015-08-24 14:52:23 -07001579 size_t new_length = merge_->Process(decoded_buffer, decoded_length,
1580 mute_factor_array_.get(),
1581 algorithm_buffer_.get());
1582 size_t expand_length_correction = new_length -
1583 decoded_length / algorithm_buffer_->Channels();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001584
1585 // Update in-call and post-call statistics.
1586 if (expand_->MuteFactor(0) == 0) {
1587 // Expand generates only noise.
minyue@webrtc.orgc11348b2015-02-10 08:35:38 +00001588 stats_.ExpandedNoiseSamples(expand_length_correction);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001589 } else {
1590 // Expansion generates more than only noise.
minyue@webrtc.orgc11348b2015-02-10 08:35:38 +00001591 stats_.ExpandedVoiceSamples(expand_length_correction);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001592 }
1593
1594 last_mode_ = kModeMerge;
1595 // If last packet was decoded as an inband CNG, set mode to CNG instead.
1596 if (speech_type == AudioDecoder::kComfortNoise) {
1597 last_mode_ = kModeCodecInternalCng;
1598 }
1599 expand_->Reset();
1600 if (!play_dtmf) {
1601 dtmf_tone_generator_->Reset();
1602 }
1603}
1604
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001605int NetEqImpl::DoExpand(bool play_dtmf) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001606 while ((sync_buffer_->FutureLength() - expand_->overlap_length()) <
Peter Kastingdce40cf2015-08-24 14:52:23 -07001607 output_size_samples_) {
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001608 algorithm_buffer_->Clear();
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +00001609 int return_value = expand_->Process(algorithm_buffer_.get());
Peter Kastingdce40cf2015-08-24 14:52:23 -07001610 size_t length = algorithm_buffer_->Size();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001611
1612 // Update in-call and post-call statistics.
1613 if (expand_->MuteFactor(0) == 0) {
1614 // Expand operation generates only noise.
1615 stats_.ExpandedNoiseSamples(length);
1616 } else {
1617 // Expand operation generates more than only noise.
1618 stats_.ExpandedVoiceSamples(length);
1619 }
1620
1621 last_mode_ = kModeExpand;
1622
1623 if (return_value < 0) {
1624 return return_value;
1625 }
1626
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001627 sync_buffer_->PushBack(*algorithm_buffer_);
1628 algorithm_buffer_->Clear();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001629 }
1630 if (!play_dtmf) {
1631 dtmf_tone_generator_->Reset();
1632 }
henrik.lundinb1fb72b2016-05-03 08:18:47 -07001633
1634 if (!generated_noise_stopwatch_) {
1635 // Start a new stopwatch since we may be covering for a lost CNG packet.
1636 generated_noise_stopwatch_ = tick_timer_->GetNewStopwatch();
1637 }
1638
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001639 return 0;
1640}
1641
Henrik Lundincf808d22015-05-27 14:33:29 +02001642int NetEqImpl::DoAccelerate(int16_t* decoded_buffer,
1643 size_t decoded_length,
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001644 AudioDecoder::SpeechType speech_type,
Henrik Lundincf808d22015-05-27 14:33:29 +02001645 bool play_dtmf,
1646 bool fast_accelerate) {
Peter Kastingdce40cf2015-08-24 14:52:23 -07001647 const size_t required_samples =
1648 static_cast<size_t>(240 * fs_mult_); // Must have 30 ms.
turaj@webrtc.org362a55e2013-09-20 16:25:28 +00001649 size_t borrowed_samples_per_channel = 0;
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001650 size_t num_channels = algorithm_buffer_->Channels();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001651 size_t decoded_length_per_channel = decoded_length / num_channels;
1652 if (decoded_length_per_channel < required_samples) {
1653 // Must move data from the |sync_buffer_| in order to get 30 ms.
turaj@webrtc.org362a55e2013-09-20 16:25:28 +00001654 borrowed_samples_per_channel = static_cast<int>(required_samples -
1655 decoded_length_per_channel);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001656 memmove(&decoded_buffer[borrowed_samples_per_channel * num_channels],
1657 decoded_buffer,
1658 sizeof(int16_t) * decoded_length);
1659 sync_buffer_->ReadInterleavedFromEnd(borrowed_samples_per_channel,
1660 decoded_buffer);
1661 decoded_length = required_samples * num_channels;
1662 }
1663
Peter Kastingdce40cf2015-08-24 14:52:23 -07001664 size_t samples_removed;
Henrik Lundincf808d22015-05-27 14:33:29 +02001665 Accelerate::ReturnCodes return_code =
1666 accelerate_->Process(decoded_buffer, decoded_length, fast_accelerate,
1667 algorithm_buffer_.get(), &samples_removed);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001668 stats_.AcceleratedSamples(samples_removed);
1669 switch (return_code) {
1670 case Accelerate::kSuccess:
1671 last_mode_ = kModeAccelerateSuccess;
1672 break;
1673 case Accelerate::kSuccessLowEnergy:
1674 last_mode_ = kModeAccelerateLowEnergy;
1675 break;
1676 case Accelerate::kNoStretch:
1677 last_mode_ = kModeAccelerateFail;
1678 break;
1679 case Accelerate::kError:
1680 // TODO(hlundin): Map to kModeError instead?
1681 last_mode_ = kModeAccelerateFail;
1682 return kAccelerateError;
1683 }
1684
1685 if (borrowed_samples_per_channel > 0) {
1686 // Copy borrowed samples back to the |sync_buffer_|.
turaj@webrtc.org362a55e2013-09-20 16:25:28 +00001687 size_t length = algorithm_buffer_->Size();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001688 if (length < borrowed_samples_per_channel) {
1689 // This destroys the beginning of the buffer, but will not cause any
1690 // problems.
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001691 sync_buffer_->ReplaceAtIndex(*algorithm_buffer_,
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001692 sync_buffer_->Size() -
1693 borrowed_samples_per_channel);
1694 sync_buffer_->PushFrontZeros(borrowed_samples_per_channel - length);
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001695 algorithm_buffer_->PopFront(length);
1696 assert(algorithm_buffer_->Empty());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001697 } else {
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001698 sync_buffer_->ReplaceAtIndex(*algorithm_buffer_,
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001699 borrowed_samples_per_channel,
1700 sync_buffer_->Size() -
1701 borrowed_samples_per_channel);
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001702 algorithm_buffer_->PopFront(borrowed_samples_per_channel);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001703 }
1704 }
1705
1706 // If last packet was decoded as an inband CNG, set mode to CNG instead.
1707 if (speech_type == AudioDecoder::kComfortNoise) {
1708 last_mode_ = kModeCodecInternalCng;
1709 }
1710 if (!play_dtmf) {
1711 dtmf_tone_generator_->Reset();
1712 }
1713 expand_->Reset();
1714 return 0;
1715}
1716
1717int NetEqImpl::DoPreemptiveExpand(int16_t* decoded_buffer,
1718 size_t decoded_length,
1719 AudioDecoder::SpeechType speech_type,
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001720 bool play_dtmf) {
Peter Kastingdce40cf2015-08-24 14:52:23 -07001721 const size_t required_samples =
1722 static_cast<size_t>(240 * fs_mult_); // Must have 30 ms.
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001723 size_t num_channels = algorithm_buffer_->Channels();
Peter Kastingdce40cf2015-08-24 14:52:23 -07001724 size_t borrowed_samples_per_channel = 0;
1725 size_t old_borrowed_samples_per_channel = 0;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001726 size_t decoded_length_per_channel = decoded_length / num_channels;
1727 if (decoded_length_per_channel < required_samples) {
1728 // Must move data from the |sync_buffer_| in order to get 30 ms.
Peter Kastingdce40cf2015-08-24 14:52:23 -07001729 borrowed_samples_per_channel =
1730 required_samples - decoded_length_per_channel;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001731 // Calculate how many of these were already played out.
Peter Kastingf045e4d2015-06-10 21:15:38 -07001732 old_borrowed_samples_per_channel =
Peter Kastingdce40cf2015-08-24 14:52:23 -07001733 (borrowed_samples_per_channel > sync_buffer_->FutureLength()) ?
1734 (borrowed_samples_per_channel - sync_buffer_->FutureLength()) : 0;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001735 memmove(&decoded_buffer[borrowed_samples_per_channel * num_channels],
1736 decoded_buffer,
1737 sizeof(int16_t) * decoded_length);
1738 sync_buffer_->ReadInterleavedFromEnd(borrowed_samples_per_channel,
1739 decoded_buffer);
1740 decoded_length = required_samples * num_channels;
1741 }
1742
Peter Kastingdce40cf2015-08-24 14:52:23 -07001743 size_t samples_added;
henrik.lundin@webrtc.org40d3fc62013-09-18 12:19:50 +00001744 PreemptiveExpand::ReturnCodes return_code = preemptive_expand_->Process(
Peter Kastingdce40cf2015-08-24 14:52:23 -07001745 decoded_buffer, decoded_length,
turaj@webrtc.org362a55e2013-09-20 16:25:28 +00001746 old_borrowed_samples_per_channel,
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +00001747 algorithm_buffer_.get(), &samples_added);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001748 stats_.PreemptiveExpandedSamples(samples_added);
1749 switch (return_code) {
1750 case PreemptiveExpand::kSuccess:
1751 last_mode_ = kModePreemptiveExpandSuccess;
1752 break;
1753 case PreemptiveExpand::kSuccessLowEnergy:
1754 last_mode_ = kModePreemptiveExpandLowEnergy;
1755 break;
1756 case PreemptiveExpand::kNoStretch:
1757 last_mode_ = kModePreemptiveExpandFail;
1758 break;
1759 case PreemptiveExpand::kError:
1760 // TODO(hlundin): Map to kModeError instead?
1761 last_mode_ = kModePreemptiveExpandFail;
1762 return kPreemptiveExpandError;
1763 }
1764
1765 if (borrowed_samples_per_channel > 0) {
1766 // Copy borrowed samples back to the |sync_buffer_|.
1767 sync_buffer_->ReplaceAtIndex(
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001768 *algorithm_buffer_, borrowed_samples_per_channel,
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001769 sync_buffer_->Size() - borrowed_samples_per_channel);
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001770 algorithm_buffer_->PopFront(borrowed_samples_per_channel);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001771 }
1772
1773 // If last packet was decoded as an inband CNG, set mode to CNG instead.
1774 if (speech_type == AudioDecoder::kComfortNoise) {
1775 last_mode_ = kModeCodecInternalCng;
1776 }
1777 if (!play_dtmf) {
1778 dtmf_tone_generator_->Reset();
1779 }
1780 expand_->Reset();
1781 return 0;
1782}
1783
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001784int NetEqImpl::DoRfc3389Cng(PacketList* packet_list, bool play_dtmf) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001785 if (!packet_list->empty()) {
1786 // Must have exactly one SID frame at this point.
1787 assert(packet_list->size() == 1);
ossua73f6c92016-10-24 08:25:28 -07001788 const Packet& packet = packet_list->front();
1789 if (!decoder_database_->IsComfortNoise(packet.payload_type)) {
henrik.lundin@webrtc.org73deaad2013-01-31 13:32:51 +00001790 LOG(LS_ERROR) << "Trying to decode non-CNG payload as CNG.";
1791 return kOtherError;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001792 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001793 if (comfort_noise_->UpdateParameters(packet) ==
1794 ComfortNoise::kInternalError) {
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001795 algorithm_buffer_->Zeros(output_size_samples_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001796 return -comfort_noise_->internal_error_code();
1797 }
1798 }
1799 int cn_return = comfort_noise_->Generate(output_size_samples_,
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +00001800 algorithm_buffer_.get());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001801 expand_->Reset();
1802 last_mode_ = kModeRfc3389Cng;
1803 if (!play_dtmf) {
1804 dtmf_tone_generator_->Reset();
1805 }
1806 if (cn_return == ComfortNoise::kInternalError) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001807 decoder_error_code_ = comfort_noise_->internal_error_code();
1808 return kComfortNoiseErrorCode;
1809 } else if (cn_return == ComfortNoise::kUnknownPayloadType) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001810 return kUnknownRtpPayloadType;
1811 }
1812 return 0;
1813}
1814
minyuel6d92bf52015-09-23 15:20:39 +02001815void NetEqImpl::DoCodecInternalCng(const int16_t* decoded_buffer,
1816 size_t decoded_length) {
1817 RTC_DCHECK(normal_.get());
1818 RTC_DCHECK(mute_factor_array_.get());
1819 normal_->Process(decoded_buffer, decoded_length, last_mode_,
1820 mute_factor_array_.get(), algorithm_buffer_.get());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001821 last_mode_ = kModeCodecInternalCng;
1822 expand_->Reset();
1823}
1824
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001825int NetEqImpl::DoDtmf(const DtmfEvent& dtmf_event, bool* play_dtmf) {
turaj@webrtc.org4d06db52013-03-27 18:31:42 +00001826 // This block of the code and the block further down, handling |dtmf_switch|
1827 // are commented out. Otherwise playing out-of-band DTMF would fail in VoE
1828 // test, DtmfTest.ManualSuccessfullySendsOutOfBandTelephoneEvents. This is
1829 // equivalent to |dtmf_switch| always be false.
1830 //
1831 // See http://webrtc-codereview.appspot.com/1195004/ for discussion
1832 // On this issue. This change might cause some glitches at the point of
1833 // switch from audio to DTMF. Issue 1545 is filed to track this.
1834 //
1835 // bool dtmf_switch = false;
1836 // if ((last_mode_ != kModeDtmf) && dtmf_tone_generator_->initialized()) {
1837 // // Special case; see below.
1838 // // We must catch this before calling Generate, since |initialized| is
1839 // // modified in that call.
1840 // dtmf_switch = true;
1841 // }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001842
1843 int dtmf_return_value = 0;
1844 if (!dtmf_tone_generator_->initialized()) {
1845 // Initialize if not already done.
1846 dtmf_return_value = dtmf_tone_generator_->Init(fs_hz_, dtmf_event.event_no,
1847 dtmf_event.volume);
1848 }
turaj@webrtc.org4d06db52013-03-27 18:31:42 +00001849
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001850 if (dtmf_return_value == 0) {
1851 // Generate DTMF signal.
1852 dtmf_return_value = dtmf_tone_generator_->Generate(output_size_samples_,
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +00001853 algorithm_buffer_.get());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001854 }
turaj@webrtc.org4d06db52013-03-27 18:31:42 +00001855
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001856 if (dtmf_return_value < 0) {
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001857 algorithm_buffer_->Zeros(output_size_samples_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001858 return dtmf_return_value;
1859 }
1860
turaj@webrtc.org4d06db52013-03-27 18:31:42 +00001861 // if (dtmf_switch) {
1862 // // This is the special case where the previous operation was DTMF
1863 // // overdub, but the current instruction is "regular" DTMF. We must make
1864 // // sure that the DTMF does not have any discontinuities. The first DTMF
1865 // // sample that we generate now must be played out immediately, therefore
1866 // // it must be copied to the speech buffer.
1867 // // TODO(hlundin): This code seems incorrect. (Legacy.) Write test and
1868 // // verify correct operation.
1869 // assert(false);
1870 // // Must generate enough data to replace all of the |sync_buffer_|
1871 // // "future".
1872 // int required_length = sync_buffer_->FutureLength();
1873 // assert(dtmf_tone_generator_->initialized());
1874 // dtmf_return_value = dtmf_tone_generator_->Generate(required_length,
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001875 // algorithm_buffer_);
1876 // assert((size_t) required_length == algorithm_buffer_->Size());
turaj@webrtc.org4d06db52013-03-27 18:31:42 +00001877 // if (dtmf_return_value < 0) {
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001878 // algorithm_buffer_->Zeros(output_size_samples_);
turaj@webrtc.org4d06db52013-03-27 18:31:42 +00001879 // return dtmf_return_value;
1880 // }
1881 //
1882 // // Overwrite the "future" part of the speech buffer with the new DTMF
1883 // // data.
1884 // // TODO(hlundin): It seems that this overwriting has gone lost.
1885 // // Not adapted for multi-channel yet.
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001886 // assert(algorithm_buffer_->Channels() == 1);
1887 // if (algorithm_buffer_->Channels() != 1) {
turaj@webrtc.org4d06db52013-03-27 18:31:42 +00001888 // LOG(LS_WARNING) << "DTMF not supported for more than one channel";
1889 // return kStereoNotSupported;
1890 // }
1891 // // Shuffle the remaining data to the beginning of algorithm buffer.
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001892 // algorithm_buffer_->PopFront(sync_buffer_->FutureLength());
turaj@webrtc.org4d06db52013-03-27 18:31:42 +00001893 // }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001894
Peter Kastingb7e50542015-06-11 12:55:50 -07001895 sync_buffer_->IncreaseEndTimestamp(
1896 static_cast<uint32_t>(output_size_samples_));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001897 expand_->Reset();
1898 last_mode_ = kModeDtmf;
1899
1900 // Set to false because the DTMF is already in the algorithm buffer.
1901 *play_dtmf = false;
1902 return 0;
1903}
1904
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001905void NetEqImpl::DoAlternativePlc(bool increase_timestamp) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001906 AudioDecoder* decoder = decoder_database_->GetActiveDecoder();
Peter Kastingdce40cf2015-08-24 14:52:23 -07001907 size_t length;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001908 if (decoder && decoder->HasDecodePlc()) {
1909 // Use the decoder's packet-loss concealment.
1910 // TODO(hlundin): Will probably need a longer buffer for multi-channel.
1911 int16_t decoded_buffer[kMaxFrameSize];
1912 length = decoder->DecodePlc(1, decoded_buffer);
Peter Kastingdce40cf2015-08-24 14:52:23 -07001913 if (length > 0)
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001914 algorithm_buffer_->PushBackInterleaved(decoded_buffer, length);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001915 } else {
1916 // Do simple zero-stuffing.
1917 length = output_size_samples_;
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00001918 algorithm_buffer_->Zeros(length);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001919 // By not advancing the timestamp, NetEq inserts samples.
1920 stats_.AddZeros(length);
1921 }
1922 if (increase_timestamp) {
Peter Kastingb7e50542015-06-11 12:55:50 -07001923 sync_buffer_->IncreaseEndTimestamp(static_cast<uint32_t>(length));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001924 }
1925 expand_->Reset();
1926}
1927
1928int NetEqImpl::DtmfOverdub(const DtmfEvent& dtmf_event, size_t num_channels,
1929 int16_t* output) const {
1930 size_t out_index = 0;
Peter Kastingdce40cf2015-08-24 14:52:23 -07001931 size_t overdub_length = output_size_samples_; // Default value.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001932
1933 if (sync_buffer_->dtmf_index() > sync_buffer_->next_index()) {
1934 // Special operation for transition from "DTMF only" to "DTMF overdub".
1935 out_index = std::min(
1936 sync_buffer_->dtmf_index() - sync_buffer_->next_index(),
Peter Kastingdce40cf2015-08-24 14:52:23 -07001937 output_size_samples_);
1938 overdub_length = output_size_samples_ - out_index;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001939 }
1940
henrik.lundin@webrtc.orgfd11bbf2013-09-30 20:38:44 +00001941 AudioMultiVector dtmf_output(num_channels);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001942 int dtmf_return_value = 0;
1943 if (!dtmf_tone_generator_->initialized()) {
1944 dtmf_return_value = dtmf_tone_generator_->Init(fs_hz_, dtmf_event.event_no,
1945 dtmf_event.volume);
1946 }
1947 if (dtmf_return_value == 0) {
1948 dtmf_return_value = dtmf_tone_generator_->Generate(overdub_length,
1949 &dtmf_output);
Peter Kastingdce40cf2015-08-24 14:52:23 -07001950 assert(overdub_length == dtmf_output.Size());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001951 }
1952 dtmf_output.ReadInterleaved(overdub_length, &output[out_index]);
1953 return dtmf_return_value < 0 ? dtmf_return_value : 0;
1954}
1955
Peter Kastingdce40cf2015-08-24 14:52:23 -07001956int NetEqImpl::ExtractPackets(size_t required_samples,
1957 PacketList* packet_list) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001958 bool first_packet = true;
1959 uint8_t prev_payload_type = 0;
1960 uint32_t prev_timestamp = 0;
1961 uint16_t prev_sequence_number = 0;
1962 bool next_packet_available = false;
1963
ossu7a377612016-10-18 04:06:13 -07001964 const Packet* next_packet = packet_buffer_->PeekNextPacket();
1965 RTC_DCHECK(next_packet);
1966 if (!next_packet) {
Henrik Lundind67a2192015-08-03 12:54:37 +02001967 LOG(LS_ERROR) << "Packet buffer unexpectedly empty.";
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001968 return -1;
1969 }
ossu7a377612016-10-18 04:06:13 -07001970 uint32_t first_timestamp = next_packet->timestamp;
ossu61a208b2016-09-20 01:38:00 -07001971 size_t extracted_samples = 0;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001972
1973 // Packet extraction loop.
1974 do {
ossu7a377612016-10-18 04:06:13 -07001975 timestamp_ = next_packet->timestamp;
ossua73f6c92016-10-24 08:25:28 -07001976 rtc::Optional<Packet> packet = packet_buffer_->GetNextPacket();
ossu7a377612016-10-18 04:06:13 -07001977 // |next_packet| may be invalid after the |packet_buffer_| operation.
ossua73f6c92016-10-24 08:25:28 -07001978 next_packet = nullptr;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001979 if (!packet) {
Henrik Lundind67a2192015-08-03 12:54:37 +02001980 LOG(LS_ERROR) << "Should always be able to extract a packet here";
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001981 assert(false); // Should always be able to extract a packet here.
1982 return -1;
1983 }
henrik.lundin84f8cd62016-04-26 07:45:16 -07001984 stats_.StoreWaitingTime(packet->waiting_time->ElapsedMs());
ossu61a208b2016-09-20 01:38:00 -07001985 RTC_DCHECK(!packet->empty());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001986
1987 if (first_packet) {
1988 first_packet = false;
henrik.lundin48ed9302015-10-29 05:36:24 -07001989 if (nack_enabled_) {
1990 RTC_DCHECK(nack_);
1991 // TODO(henrik.lundin): Should we update this for all decoded packets?
ossu7a377612016-10-18 04:06:13 -07001992 nack_->UpdateLastDecodedPacket(packet->sequence_number,
1993 packet->timestamp);
henrik.lundin48ed9302015-10-29 05:36:24 -07001994 }
ossu7a377612016-10-18 04:06:13 -07001995 prev_sequence_number = packet->sequence_number;
1996 prev_timestamp = packet->timestamp;
1997 prev_payload_type = packet->payload_type;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001998 }
1999
ossucafb4972017-01-02 07:00:50 -08002000 const bool has_cng_packet =
2001 decoder_database_->IsComfortNoise(packet->payload_type);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00002002 // Store number of extracted samples.
ossu61a208b2016-09-20 01:38:00 -07002003 size_t packet_duration = 0;
2004 if (packet->frame) {
2005 packet_duration = packet->frame->Duration();
ossua70695a2016-09-22 02:06:28 -07002006 // TODO(ossu): Is this the correct way to track Opus FEC packets?
2007 if (packet->priority.codec_level > 0) {
kwibergd3edd772017-03-01 18:52:48 -08002008 stats_.SecondaryDecodedSamples(
2009 rtc::dchecked_cast<int>(packet_duration));
minyue@webrtc.orgb28bfa72014-03-21 12:07:40 +00002010 }
ossucafb4972017-01-02 07:00:50 -08002011 } else if (!has_cng_packet) {
Henrik Lundind67a2192015-08-03 12:54:37 +02002012 LOG(LS_WARNING) << "Unknown payload type "
ossu7a377612016-10-18 04:06:13 -07002013 << static_cast<int>(packet->payload_type);
ossu61a208b2016-09-20 01:38:00 -07002014 RTC_NOTREACHED();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00002015 }
ossu61a208b2016-09-20 01:38:00 -07002016
2017 if (packet_duration == 0) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00002018 // Decoder did not return a packet duration. Assume that the packet
2019 // contains the same number of samples as the previous one.
ossu61a208b2016-09-20 01:38:00 -07002020 packet_duration = decoder_frame_length_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00002021 }
ossu7a377612016-10-18 04:06:13 -07002022 extracted_samples = packet->timestamp - first_timestamp + packet_duration;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00002023
ossua73f6c92016-10-24 08:25:28 -07002024 packet_list->push_back(std::move(*packet)); // Store packet in list.
2025 packet = rtc::Optional<Packet>(); // Ensure it's never used after the move.
2026
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00002027 // Check what packet is available next.
ossu7a377612016-10-18 04:06:13 -07002028 next_packet = packet_buffer_->PeekNextPacket();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00002029 next_packet_available = false;
ossucafb4972017-01-02 07:00:50 -08002030 if (next_packet && prev_payload_type == next_packet->payload_type &&
2031 !has_cng_packet) {
ossu7a377612016-10-18 04:06:13 -07002032 int16_t seq_no_diff = next_packet->sequence_number - prev_sequence_number;
2033 size_t ts_diff = next_packet->timestamp - prev_timestamp;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00002034 if (seq_no_diff == 1 ||
2035 (seq_no_diff == 0 && ts_diff == decoder_frame_length_)) {
2036 // The next sequence number is available, or the next part of a packet
2037 // that was split into pieces upon insertion.
2038 next_packet_available = true;
2039 }
ossu7a377612016-10-18 04:06:13 -07002040 prev_sequence_number = next_packet->sequence_number;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00002041 }
ossu61a208b2016-09-20 01:38:00 -07002042 } while (extracted_samples < required_samples && next_packet_available);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00002043
henrik.lundin@webrtc.org61217152014-09-22 08:30:07 +00002044 if (extracted_samples > 0) {
2045 // Delete old packets only when we are going to decode something. Otherwise,
2046 // we could end up in the situation where we never decode anything, since
2047 // all incoming packets are considered too old but the buffer will also
2048 // never be flooded and flushed.
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +00002049 packet_buffer_->DiscardAllOldPackets(timestamp_);
henrik.lundin@webrtc.org61217152014-09-22 08:30:07 +00002050 }
2051
kwibergd3edd772017-03-01 18:52:48 -08002052 return rtc::dchecked_cast<int>(extracted_samples);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00002053}
2054
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00002055void NetEqImpl::UpdatePlcComponents(int fs_hz, size_t channels) {
2056 // Delete objects and create new ones.
2057 expand_.reset(expand_factory_->Create(background_noise_.get(),
2058 sync_buffer_.get(), &random_vector_,
Henrik Lundinbef77e22015-08-18 14:58:09 +02002059 &stats_, fs_hz, channels));
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00002060 merge_.reset(new Merge(fs_hz, channels, expand_.get(), sync_buffer_.get()));
2061}
2062
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00002063void NetEqImpl::SetSampleRateAndChannels(int fs_hz, size_t channels) {
Henrik Lundind67a2192015-08-03 12:54:37 +02002064 LOG(LS_VERBOSE) << "SetSampleRateAndChannels " << fs_hz << " " << channels;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00002065 // TODO(hlundin): Change to an enumerator and skip assert.
2066 assert(fs_hz == 8000 || fs_hz == 16000 || fs_hz == 32000 || fs_hz == 48000);
2067 assert(channels > 0);
2068
2069 fs_hz_ = fs_hz;
2070 fs_mult_ = fs_hz / 8000;
Peter Kastingdce40cf2015-08-24 14:52:23 -07002071 output_size_samples_ = static_cast<size_t>(kOutputSizeMs * 8 * fs_mult_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00002072 decoder_frame_length_ = 3 * output_size_samples_; // Initialize to 30ms.
2073
2074 last_mode_ = kModeNormal;
2075
2076 // Create a new array of mute factors and set all to 1.
2077 mute_factor_array_.reset(new int16_t[channels]);
2078 for (size_t i = 0; i < channels; ++i) {
2079 mute_factor_array_[i] = 16384; // 1.0 in Q14.
2080 }
2081
ossu97ba30e2016-04-25 07:55:58 -07002082 ComfortNoiseDecoder* cng_decoder = decoder_database_->GetActiveCngDecoder();
Karl Wiberg43766482015-08-27 15:22:11 +02002083 if (cng_decoder)
2084 cng_decoder->Reset();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00002085
2086 // Reinit post-decode VAD with new sample rate.
2087 assert(vad_.get()); // Cannot be NULL here.
2088 vad_->Init();
2089
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00002090 // Delete algorithm buffer and create a new one.
henrik.lundin@webrtc.orgfd11bbf2013-09-30 20:38:44 +00002091 algorithm_buffer_.reset(new AudioMultiVector(channels));
henrik.lundin@webrtc.orgc487c6a2013-09-02 07:59:30 +00002092
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00002093 // Delete sync buffer and create a new one.
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +00002094 sync_buffer_.reset(new SyncBuffer(channels, kSyncBufferSize * fs_mult_));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00002095
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00002096 // Delete BackgroundNoise object and create a new one.
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +00002097 background_noise_.reset(new BackgroundNoise(channels));
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00002098 background_noise_->set_mode(background_noise_mode_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00002099
2100 // Reset random vector.
2101 random_vector_.Reset();
2102
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00002103 UpdatePlcComponents(fs_hz, channels);
2104
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00002105 // Move index so that we create a small set of future samples (all 0).
2106 sync_buffer_->set_next_index(sync_buffer_->next_index() -
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00002107 expand_->overlap_length());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00002108
henrik.lundin@webrtc.org40d3fc62013-09-18 12:19:50 +00002109 normal_.reset(new Normal(fs_hz, decoder_database_.get(), *background_noise_,
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +00002110 expand_.get()));
henrik.lundin@webrtc.orgd9faa462014-01-14 10:18:45 +00002111 accelerate_.reset(
2112 accelerate_factory_->Create(fs_hz, channels, *background_noise_));
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00002113 preemptive_expand_.reset(preemptive_expand_factory_->Create(
Peter Kastingdce40cf2015-08-24 14:52:23 -07002114 fs_hz, channels, *background_noise_, expand_->overlap_length()));
henrik.lundin@webrtc.org40d3fc62013-09-18 12:19:50 +00002115
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00002116 // Delete ComfortNoise object and create a new one.
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +00002117 comfort_noise_.reset(new ComfortNoise(fs_hz, decoder_database_.get(),
2118 sync_buffer_.get()));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00002119
2120 // Verify that |decoded_buffer_| is long enough.
2121 if (decoded_buffer_length_ < kMaxFrameSize * channels) {
2122 // Reallocate to larger size.
2123 decoded_buffer_length_ = kMaxFrameSize * channels;
2124 decoded_buffer_.reset(new int16_t[decoded_buffer_length_]);
2125 }
2126
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00002127 // Create DecisionLogic if it is not created yet, then communicate new sample
2128 // rate and output size to DecisionLogic object.
2129 if (!decision_logic_.get()) {
henrik.lundin@webrtc.org7cbc4f92014-10-07 06:37:39 +00002130 CreateDecisionLogic();
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00002131 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00002132 decision_logic_->SetSampleRate(fs_hz_, output_size_samples_);
2133}
2134
henrik.lundin55480f52016-03-08 02:37:57 -08002135NetEqImpl::OutputType NetEqImpl::LastOutputType() {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00002136 assert(vad_.get());
henrik.lundin@webrtc.org0d5da252013-09-18 21:12:38 +00002137 assert(expand_.get());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00002138 if (last_mode_ == kModeCodecInternalCng || last_mode_ == kModeRfc3389Cng) {
henrik.lundin55480f52016-03-08 02:37:57 -08002139 return OutputType::kCNG;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00002140 } else if (last_mode_ == kModeExpand && expand_->MuteFactor(0) == 0) {
2141 // Expand mode has faded down to background noise only (very long expand).
henrik.lundin55480f52016-03-08 02:37:57 -08002142 return OutputType::kPLCCNG;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00002143 } else if (last_mode_ == kModeExpand) {
henrik.lundin55480f52016-03-08 02:37:57 -08002144 return OutputType::kPLC;
wu@webrtc.org24301a62013-12-13 19:17:43 +00002145 } else if (vad_->running() && !vad_->active_speech()) {
henrik.lundin55480f52016-03-08 02:37:57 -08002146 return OutputType::kVadPassive;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00002147 } else {
henrik.lundin55480f52016-03-08 02:37:57 -08002148 return OutputType::kNormalSpeech;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00002149 }
2150}
2151
henrik.lundin@webrtc.org7cbc4f92014-10-07 06:37:39 +00002152void NetEqImpl::CreateDecisionLogic() {
Henrik Lundin47b17dc2016-05-10 10:20:59 +02002153 decision_logic_.reset(DecisionLogic::Create(
2154 fs_hz_, output_size_samples_, playout_mode_, decoder_database_.get(),
2155 *packet_buffer_.get(), delay_manager_.get(), buffer_level_filter_.get(),
2156 tick_timer_.get()));
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00002157}
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00002158} // namespace webrtc