• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
11 #include "modules/audio_coding/neteq/neteq_impl.h"
12 
13 #include <assert.h>
14 
15 #include <algorithm>
16 #include <cstdint>
17 #include <cstring>
18 #include <list>
19 #include <map>
20 #include <utility>
21 #include <vector>
22 
23 #include "api/audio_codecs/audio_decoder.h"
24 #include "api/neteq/tick_timer.h"
25 #include "common_audio/signal_processing/include/signal_processing_library.h"
26 #include "modules/audio_coding/codecs/cng/webrtc_cng.h"
27 #include "modules/audio_coding/neteq/accelerate.h"
28 #include "modules/audio_coding/neteq/background_noise.h"
29 #include "modules/audio_coding/neteq/comfort_noise.h"
30 #include "modules/audio_coding/neteq/decision_logic.h"
31 #include "modules/audio_coding/neteq/decoder_database.h"
32 #include "modules/audio_coding/neteq/dtmf_buffer.h"
33 #include "modules/audio_coding/neteq/dtmf_tone_generator.h"
34 #include "modules/audio_coding/neteq/expand.h"
35 #include "modules/audio_coding/neteq/merge.h"
36 #include "modules/audio_coding/neteq/nack_tracker.h"
37 #include "modules/audio_coding/neteq/normal.h"
38 #include "modules/audio_coding/neteq/packet.h"
39 #include "modules/audio_coding/neteq/packet_buffer.h"
40 #include "modules/audio_coding/neteq/post_decode_vad.h"
41 #include "modules/audio_coding/neteq/preemptive_expand.h"
42 #include "modules/audio_coding/neteq/red_payload_splitter.h"
43 #include "modules/audio_coding/neteq/statistics_calculator.h"
44 #include "modules/audio_coding/neteq/sync_buffer.h"
45 #include "modules/audio_coding/neteq/time_stretch.h"
46 #include "modules/audio_coding/neteq/timestamp_scaler.h"
47 #include "rtc_base/checks.h"
48 #include "rtc_base/logging.h"
49 #include "rtc_base/numerics/safe_conversions.h"
50 #include "rtc_base/sanitizer.h"
51 #include "rtc_base/strings/audio_format_to_string.h"
52 #include "rtc_base/trace_event.h"
53 #include "system_wrappers/include/clock.h"
54 #include "system_wrappers/include/field_trial.h"
55 
56 namespace webrtc {
57 namespace {
58 
CreateNetEqController(const NetEqControllerFactory & controller_factory,int base_min_delay,int max_packets_in_buffer,bool enable_rtx_handling,bool allow_time_stretching,TickTimer * tick_timer,webrtc::Clock * clock)59 std::unique_ptr<NetEqController> CreateNetEqController(
60     const NetEqControllerFactory& controller_factory,
61     int base_min_delay,
62     int max_packets_in_buffer,
63     bool enable_rtx_handling,
64     bool allow_time_stretching,
65     TickTimer* tick_timer,
66     webrtc::Clock* clock) {
67   NetEqController::Config config;
68   config.base_min_delay_ms = base_min_delay;
69   config.max_packets_in_buffer = max_packets_in_buffer;
70   config.enable_rtx_handling = enable_rtx_handling;
71   config.allow_time_stretching = allow_time_stretching;
72   config.tick_timer = tick_timer;
73   config.clock = clock;
74   return controller_factory.CreateNetEqController(config);
75 }
76 
GetDelayChainLengthMs(int config_extra_delay_ms)77 int GetDelayChainLengthMs(int config_extra_delay_ms) {
78   constexpr char kExtraDelayFieldTrial[] = "WebRTC-Audio-NetEqExtraDelay";
79   if (webrtc::field_trial::IsEnabled(kExtraDelayFieldTrial)) {
80     const auto field_trial_string =
81         webrtc::field_trial::FindFullName(kExtraDelayFieldTrial);
82     int extra_delay_ms = -1;
83     if (sscanf(field_trial_string.c_str(), "Enabled-%d", &extra_delay_ms) ==
84             1 &&
85         extra_delay_ms >= 0 && extra_delay_ms <= 2000) {
86       RTC_LOG(LS_INFO) << "Delay chain length set to " << extra_delay_ms
87                        << " ms in field trial";
88       return (extra_delay_ms / 10) * 10;  // Rounding down to multiple of 10.
89     }
90   }
91   // Field trial not set, or invalid value read. Use value from config.
92   return config_extra_delay_ms;
93 }
94 
95 }  // namespace
96 
Dependencies(const NetEq::Config & config,Clock * clock,const rtc::scoped_refptr<AudioDecoderFactory> & decoder_factory,const NetEqControllerFactory & controller_factory)97 NetEqImpl::Dependencies::Dependencies(
98     const NetEq::Config& config,
99     Clock* clock,
100     const rtc::scoped_refptr<AudioDecoderFactory>& decoder_factory,
101     const NetEqControllerFactory& controller_factory)
102     : clock(clock),
103       tick_timer(new TickTimer),
104       stats(new StatisticsCalculator),
105       decoder_database(
106           new DecoderDatabase(decoder_factory, config.codec_pair_id)),
107       dtmf_buffer(new DtmfBuffer(config.sample_rate_hz)),
108       dtmf_tone_generator(new DtmfToneGenerator),
109       packet_buffer(
110           new PacketBuffer(config.max_packets_in_buffer, tick_timer.get())),
111       neteq_controller(
112           CreateNetEqController(controller_factory,
113                                 config.min_delay_ms,
114                                 config.max_packets_in_buffer,
115                                 config.enable_rtx_handling,
116                                 !config.for_test_no_time_stretching,
117                                 tick_timer.get(),
118                                 clock)),
119       red_payload_splitter(new RedPayloadSplitter),
120       timestamp_scaler(new TimestampScaler(*decoder_database)),
121       accelerate_factory(new AccelerateFactory),
122       expand_factory(new ExpandFactory),
123       preemptive_expand_factory(new PreemptiveExpandFactory) {}
124 
125 NetEqImpl::Dependencies::~Dependencies() = default;
126 
NetEqImpl(const NetEq::Config & config,Dependencies && deps,bool create_components)127 NetEqImpl::NetEqImpl(const NetEq::Config& config,
128                      Dependencies&& deps,
129                      bool create_components)
130     : clock_(deps.clock),
131       tick_timer_(std::move(deps.tick_timer)),
132       decoder_database_(std::move(deps.decoder_database)),
133       dtmf_buffer_(std::move(deps.dtmf_buffer)),
134       dtmf_tone_generator_(std::move(deps.dtmf_tone_generator)),
135       packet_buffer_(std::move(deps.packet_buffer)),
136       red_payload_splitter_(std::move(deps.red_payload_splitter)),
137       timestamp_scaler_(std::move(deps.timestamp_scaler)),
138       vad_(new PostDecodeVad()),
139       expand_factory_(std::move(deps.expand_factory)),
140       accelerate_factory_(std::move(deps.accelerate_factory)),
141       preemptive_expand_factory_(std::move(deps.preemptive_expand_factory)),
142       stats_(std::move(deps.stats)),
143       controller_(std::move(deps.neteq_controller)),
144       last_mode_(Mode::kNormal),
145       decoded_buffer_length_(kMaxFrameSize),
146       decoded_buffer_(new int16_t[decoded_buffer_length_]),
147       playout_timestamp_(0),
148       new_codec_(false),
149       timestamp_(0),
150       reset_decoder_(false),
151       first_packet_(true),
152       enable_fast_accelerate_(config.enable_fast_accelerate),
153       nack_enabled_(false),
154       enable_muted_state_(config.enable_muted_state),
155       expand_uma_logger_("WebRTC.Audio.ExpandRatePercent",
156                          10,  // Report once every 10 s.
157                          tick_timer_.get()),
158       speech_expand_uma_logger_("WebRTC.Audio.SpeechExpandRatePercent",
159                                 10,  // Report once every 10 s.
160                                 tick_timer_.get()),
161       no_time_stretching_(config.for_test_no_time_stretching),
162       enable_rtx_handling_(config.enable_rtx_handling),
163       output_delay_chain_ms_(
164           GetDelayChainLengthMs(config.extra_output_delay_ms)),
165       output_delay_chain_(rtc::CheckedDivExact(output_delay_chain_ms_, 10)) {
166   RTC_LOG(LS_INFO) << "NetEq config: " << config.ToString();
167   int fs = config.sample_rate_hz;
168   if (fs != 8000 && fs != 16000 && fs != 32000 && fs != 48000) {
169     RTC_LOG(LS_ERROR) << "Sample rate " << fs
170                       << " Hz not supported. "
171                          "Changing to 8000 Hz.";
172     fs = 8000;
173   }
174   controller_->SetMaximumDelay(config.max_delay_ms);
175   fs_hz_ = fs;
176   fs_mult_ = fs / 8000;
177   last_output_sample_rate_hz_ = fs;
178   output_size_samples_ = static_cast<size_t>(kOutputSizeMs * 8 * fs_mult_);
179   controller_->SetSampleRate(fs_hz_, output_size_samples_);
180   decoder_frame_length_ = 2 * output_size_samples_;  // 20 ms.
181   if (create_components) {
182     SetSampleRateAndChannels(fs, 1);  // Default is 1 channel.
183   }
184   RTC_DCHECK(!vad_->enabled());
185   if (config.enable_post_decode_vad) {
186     vad_->Enable();
187   }
188 }
189 
190 NetEqImpl::~NetEqImpl() = default;
191 
InsertPacket(const RTPHeader & rtp_header,rtc::ArrayView<const uint8_t> payload)192 int NetEqImpl::InsertPacket(const RTPHeader& rtp_header,
193                             rtc::ArrayView<const uint8_t> payload) {
194   rtc::MsanCheckInitialized(payload);
195   TRACE_EVENT0("webrtc", "NetEqImpl::InsertPacket");
196   MutexLock lock(&mutex_);
197   if (InsertPacketInternal(rtp_header, payload) != 0) {
198     return kFail;
199   }
200   return kOK;
201 }
202 
InsertEmptyPacket(const RTPHeader &)203 void NetEqImpl::InsertEmptyPacket(const RTPHeader& /*rtp_header*/) {
204   // TODO(henrik.lundin) Handle NACK as well. This will make use of the
205   // rtp_header parameter.
206   // https://bugs.chromium.org/p/webrtc/issues/detail?id=7611
207   MutexLock lock(&mutex_);
208   controller_->RegisterEmptyPacket();
209 }
210 
211 namespace {
SetAudioFrameActivityAndType(bool vad_enabled,NetEqImpl::OutputType type,AudioFrame::VADActivity last_vad_activity,AudioFrame * audio_frame)212 void SetAudioFrameActivityAndType(bool vad_enabled,
213                                   NetEqImpl::OutputType type,
214                                   AudioFrame::VADActivity last_vad_activity,
215                                   AudioFrame* audio_frame) {
216   switch (type) {
217     case NetEqImpl::OutputType::kNormalSpeech: {
218       audio_frame->speech_type_ = AudioFrame::kNormalSpeech;
219       audio_frame->vad_activity_ = AudioFrame::kVadActive;
220       break;
221     }
222     case NetEqImpl::OutputType::kVadPassive: {
223       // This should only be reached if the VAD is enabled.
224       RTC_DCHECK(vad_enabled);
225       audio_frame->speech_type_ = AudioFrame::kNormalSpeech;
226       audio_frame->vad_activity_ = AudioFrame::kVadPassive;
227       break;
228     }
229     case NetEqImpl::OutputType::kCNG: {
230       audio_frame->speech_type_ = AudioFrame::kCNG;
231       audio_frame->vad_activity_ = AudioFrame::kVadPassive;
232       break;
233     }
234     case NetEqImpl::OutputType::kPLC: {
235       audio_frame->speech_type_ = AudioFrame::kPLC;
236       audio_frame->vad_activity_ = last_vad_activity;
237       break;
238     }
239     case NetEqImpl::OutputType::kPLCCNG: {
240       audio_frame->speech_type_ = AudioFrame::kPLCCNG;
241       audio_frame->vad_activity_ = AudioFrame::kVadPassive;
242       break;
243     }
244     case NetEqImpl::OutputType::kCodecPLC: {
245       audio_frame->speech_type_ = AudioFrame::kCodecPLC;
246       audio_frame->vad_activity_ = last_vad_activity;
247       break;
248     }
249     default:
250       RTC_NOTREACHED();
251   }
252   if (!vad_enabled) {
253     // Always set kVadUnknown when receive VAD is inactive.
254     audio_frame->vad_activity_ = AudioFrame::kVadUnknown;
255   }
256 }
257 }  // namespace
258 
GetAudio(AudioFrame * audio_frame,bool * muted,absl::optional<Operation> action_override)259 int NetEqImpl::GetAudio(AudioFrame* audio_frame,
260                         bool* muted,
261                         absl::optional<Operation> action_override) {
262   TRACE_EVENT0("webrtc", "NetEqImpl::GetAudio");
263   MutexLock lock(&mutex_);
264   if (GetAudioInternal(audio_frame, muted, action_override) != 0) {
265     return kFail;
266   }
267   RTC_DCHECK_EQ(
268       audio_frame->sample_rate_hz_,
269       rtc::dchecked_cast<int>(audio_frame->samples_per_channel_ * 100));
270   RTC_DCHECK_EQ(*muted, audio_frame->muted());
271   SetAudioFrameActivityAndType(vad_->enabled(), LastOutputType(),
272                                last_vad_activity_, audio_frame);
273   last_vad_activity_ = audio_frame->vad_activity_;
274   last_output_sample_rate_hz_ = audio_frame->sample_rate_hz_;
275   RTC_DCHECK(last_output_sample_rate_hz_ == 8000 ||
276              last_output_sample_rate_hz_ == 16000 ||
277              last_output_sample_rate_hz_ == 32000 ||
278              last_output_sample_rate_hz_ == 48000)
279       << "Unexpected sample rate " << last_output_sample_rate_hz_;
280 
281   if (!output_delay_chain_.empty()) {
282     if (output_delay_chain_empty_) {
283       for (auto& f : output_delay_chain_) {
284         f.CopyFrom(*audio_frame);
285       }
286       output_delay_chain_empty_ = false;
287       delayed_last_output_sample_rate_hz_ = last_output_sample_rate_hz_;
288     } else {
289       RTC_DCHECK_GE(output_delay_chain_ix_, 0);
290       RTC_DCHECK_LT(output_delay_chain_ix_, output_delay_chain_.size());
291       swap(output_delay_chain_[output_delay_chain_ix_], *audio_frame);
292       *muted = audio_frame->muted();
293       output_delay_chain_ix_ =
294           (output_delay_chain_ix_ + 1) % output_delay_chain_.size();
295       delayed_last_output_sample_rate_hz_ = audio_frame->sample_rate_hz();
296     }
297   }
298 
299   return kOK;
300 }
301 
SetCodecs(const std::map<int,SdpAudioFormat> & codecs)302 void NetEqImpl::SetCodecs(const std::map<int, SdpAudioFormat>& codecs) {
303   MutexLock lock(&mutex_);
304   const std::vector<int> changed_payload_types =
305       decoder_database_->SetCodecs(codecs);
306   for (const int pt : changed_payload_types) {
307     packet_buffer_->DiscardPacketsWithPayloadType(pt, stats_.get());
308   }
309 }
310 
RegisterPayloadType(int rtp_payload_type,const SdpAudioFormat & audio_format)311 bool NetEqImpl::RegisterPayloadType(int rtp_payload_type,
312                                     const SdpAudioFormat& audio_format) {
313   RTC_LOG(LS_VERBOSE) << "NetEqImpl::RegisterPayloadType: payload type "
314                       << rtp_payload_type << ", codec "
315                       << rtc::ToString(audio_format);
316   MutexLock lock(&mutex_);
317   return decoder_database_->RegisterPayload(rtp_payload_type, audio_format) ==
318          DecoderDatabase::kOK;
319 }
320 
RemovePayloadType(uint8_t rtp_payload_type)321 int NetEqImpl::RemovePayloadType(uint8_t rtp_payload_type) {
322   MutexLock lock(&mutex_);
323   int ret = decoder_database_->Remove(rtp_payload_type);
324   if (ret == DecoderDatabase::kOK || ret == DecoderDatabase::kDecoderNotFound) {
325     packet_buffer_->DiscardPacketsWithPayloadType(rtp_payload_type,
326                                                   stats_.get());
327     return kOK;
328   }
329   return kFail;
330 }
331 
RemoveAllPayloadTypes()332 void NetEqImpl::RemoveAllPayloadTypes() {
333   MutexLock lock(&mutex_);
334   decoder_database_->RemoveAll();
335 }
336 
SetMinimumDelay(int delay_ms)337 bool NetEqImpl::SetMinimumDelay(int delay_ms) {
338   MutexLock lock(&mutex_);
339   if (delay_ms >= 0 && delay_ms <= 10000) {
340     assert(controller_.get());
341     return controller_->SetMinimumDelay(
342         std::max(delay_ms - output_delay_chain_ms_, 0));
343   }
344   return false;
345 }
346 
SetMaximumDelay(int delay_ms)347 bool NetEqImpl::SetMaximumDelay(int delay_ms) {
348   MutexLock lock(&mutex_);
349   if (delay_ms >= 0 && delay_ms <= 10000) {
350     assert(controller_.get());
351     return controller_->SetMaximumDelay(
352         std::max(delay_ms - output_delay_chain_ms_, 0));
353   }
354   return false;
355 }
356 
SetBaseMinimumDelayMs(int delay_ms)357 bool NetEqImpl::SetBaseMinimumDelayMs(int delay_ms) {
358   MutexLock lock(&mutex_);
359   if (delay_ms >= 0 && delay_ms <= 10000) {
360     return controller_->SetBaseMinimumDelay(delay_ms);
361   }
362   return false;
363 }
364 
GetBaseMinimumDelayMs() const365 int NetEqImpl::GetBaseMinimumDelayMs() const {
366   MutexLock lock(&mutex_);
367   return controller_->GetBaseMinimumDelay();
368 }
369 
TargetDelayMs() const370 int NetEqImpl::TargetDelayMs() const {
371   MutexLock lock(&mutex_);
372   RTC_DCHECK(controller_.get());
373   return controller_->TargetLevelMs() + output_delay_chain_ms_;
374 }
375 
FilteredCurrentDelayMs() const376 int NetEqImpl::FilteredCurrentDelayMs() const {
377   MutexLock lock(&mutex_);
378   // Sum up the filtered packet buffer level with the future length of the sync
379   // buffer.
380   const int delay_samples =
381       controller_->GetFilteredBufferLevel() + sync_buffer_->FutureLength();
382   // The division below will truncate. The return value is in ms.
383   return delay_samples / rtc::CheckedDivExact(fs_hz_, 1000) +
384          output_delay_chain_ms_;
385 }
386 
NetworkStatistics(NetEqNetworkStatistics * stats)387 int NetEqImpl::NetworkStatistics(NetEqNetworkStatistics* stats) {
388   MutexLock lock(&mutex_);
389   assert(decoder_database_.get());
390   const size_t total_samples_in_buffers =
391       packet_buffer_->NumSamplesInBuffer(decoder_frame_length_) +
392       sync_buffer_->FutureLength();
393   assert(controller_.get());
394   stats->preferred_buffer_size_ms = controller_->TargetLevelMs();
395   stats->jitter_peaks_found = controller_->PeakFound();
396   stats_->GetNetworkStatistics(fs_hz_, total_samples_in_buffers,
397                                decoder_frame_length_, stats);
398   // Compensate for output delay chain.
399   stats->current_buffer_size_ms += output_delay_chain_ms_;
400   stats->preferred_buffer_size_ms += output_delay_chain_ms_;
401   stats->mean_waiting_time_ms += output_delay_chain_ms_;
402   stats->median_waiting_time_ms += output_delay_chain_ms_;
403   stats->min_waiting_time_ms += output_delay_chain_ms_;
404   stats->max_waiting_time_ms += output_delay_chain_ms_;
405   return 0;
406 }
407 
GetLifetimeStatistics() const408 NetEqLifetimeStatistics NetEqImpl::GetLifetimeStatistics() const {
409   MutexLock lock(&mutex_);
410   return stats_->GetLifetimeStatistics();
411 }
412 
GetOperationsAndState() const413 NetEqOperationsAndState NetEqImpl::GetOperationsAndState() const {
414   MutexLock lock(&mutex_);
415   auto result = stats_->GetOperationsAndState();
416   result.current_buffer_size_ms =
417       (packet_buffer_->NumSamplesInBuffer(decoder_frame_length_) +
418        sync_buffer_->FutureLength()) *
419       1000 / fs_hz_;
420   result.current_frame_size_ms = decoder_frame_length_ * 1000 / fs_hz_;
421   result.next_packet_available = packet_buffer_->PeekNextPacket() &&
422                                  packet_buffer_->PeekNextPacket()->timestamp ==
423                                      sync_buffer_->end_timestamp();
424   return result;
425 }
426 
EnableVad()427 void NetEqImpl::EnableVad() {
428   MutexLock lock(&mutex_);
429   assert(vad_.get());
430   vad_->Enable();
431 }
432 
DisableVad()433 void NetEqImpl::DisableVad() {
434   MutexLock lock(&mutex_);
435   assert(vad_.get());
436   vad_->Disable();
437 }
438 
GetPlayoutTimestamp() const439 absl::optional<uint32_t> NetEqImpl::GetPlayoutTimestamp() const {
440   MutexLock lock(&mutex_);
441   if (first_packet_ || last_mode_ == Mode::kRfc3389Cng ||
442       last_mode_ == Mode::kCodecInternalCng) {
443     // We don't have a valid RTP timestamp until we have decoded our first
444     // RTP packet. Also, the RTP timestamp is not accurate while playing CNG,
445     // which is indicated by returning an empty value.
446     return absl::nullopt;
447   }
448   size_t sum_samples_in_output_delay_chain = 0;
449   for (const auto& audio_frame : output_delay_chain_) {
450     sum_samples_in_output_delay_chain += audio_frame.samples_per_channel();
451   }
452   return timestamp_scaler_->ToExternal(
453       playout_timestamp_ -
454       static_cast<uint32_t>(sum_samples_in_output_delay_chain));
455 }
456 
last_output_sample_rate_hz() const457 int NetEqImpl::last_output_sample_rate_hz() const {
458   MutexLock lock(&mutex_);
459   return delayed_last_output_sample_rate_hz_.value_or(
460       last_output_sample_rate_hz_);
461 }
462 
GetDecoderFormat(int payload_type) const463 absl::optional<NetEq::DecoderFormat> NetEqImpl::GetDecoderFormat(
464     int payload_type) const {
465   MutexLock lock(&mutex_);
466   const DecoderDatabase::DecoderInfo* const di =
467       decoder_database_->GetDecoderInfo(payload_type);
468   if (di) {
469     const AudioDecoder* const decoder = di->GetDecoder();
470     // TODO(kwiberg): Why the special case for RED?
471     return DecoderFormat{
472         /*sample_rate_hz=*/di->IsRed() ? 8000 : di->SampleRateHz(),
473         /*num_channels=*/
474         decoder ? rtc::dchecked_cast<int>(decoder->Channels()) : 1,
475         /*sdp_format=*/di->GetFormat()};
476   } else {
477     // Payload type not registered.
478     return absl::nullopt;
479   }
480 }
481 
FlushBuffers()482 void NetEqImpl::FlushBuffers() {
483   MutexLock lock(&mutex_);
484   RTC_LOG(LS_VERBOSE) << "FlushBuffers";
485   packet_buffer_->Flush();
486   assert(sync_buffer_.get());
487   assert(expand_.get());
488   sync_buffer_->Flush();
489   sync_buffer_->set_next_index(sync_buffer_->next_index() -
490                                expand_->overlap_length());
491   // Set to wait for new codec.
492   first_packet_ = true;
493 }
494 
EnableNack(size_t max_nack_list_size)495 void NetEqImpl::EnableNack(size_t max_nack_list_size) {
496   MutexLock lock(&mutex_);
497   if (!nack_enabled_) {
498     const int kNackThresholdPackets = 2;
499     nack_.reset(NackTracker::Create(kNackThresholdPackets));
500     nack_enabled_ = true;
501     nack_->UpdateSampleRate(fs_hz_);
502   }
503   nack_->SetMaxNackListSize(max_nack_list_size);
504 }
505 
DisableNack()506 void NetEqImpl::DisableNack() {
507   MutexLock lock(&mutex_);
508   nack_.reset();
509   nack_enabled_ = false;
510 }
511 
GetNackList(int64_t round_trip_time_ms) const512 std::vector<uint16_t> NetEqImpl::GetNackList(int64_t round_trip_time_ms) const {
513   MutexLock lock(&mutex_);
514   if (!nack_enabled_) {
515     return std::vector<uint16_t>();
516   }
517   RTC_DCHECK(nack_.get());
518   return nack_->GetNackList(round_trip_time_ms);
519 }
520 
LastDecodedTimestamps() const521 std::vector<uint32_t> NetEqImpl::LastDecodedTimestamps() const {
522   MutexLock lock(&mutex_);
523   return last_decoded_timestamps_;
524 }
525 
SyncBufferSizeMs() const526 int NetEqImpl::SyncBufferSizeMs() const {
527   MutexLock lock(&mutex_);
528   return rtc::dchecked_cast<int>(sync_buffer_->FutureLength() /
529                                  rtc::CheckedDivExact(fs_hz_, 1000));
530 }
531 
sync_buffer_for_test() const532 const SyncBuffer* NetEqImpl::sync_buffer_for_test() const {
533   MutexLock lock(&mutex_);
534   return sync_buffer_.get();
535 }
536 
last_operation_for_test() const537 NetEq::Operation NetEqImpl::last_operation_for_test() const {
538   MutexLock lock(&mutex_);
539   return last_operation_;
540 }
541 
542 // Methods below this line are private.
543 
InsertPacketInternal(const RTPHeader & rtp_header,rtc::ArrayView<const uint8_t> payload)544 int NetEqImpl::InsertPacketInternal(const RTPHeader& rtp_header,
545                                     rtc::ArrayView<const uint8_t> payload) {
546   if (payload.empty()) {
547     RTC_LOG_F(LS_ERROR) << "payload is empty";
548     return kInvalidPointer;
549   }
550 
551   int64_t receive_time_ms = clock_->TimeInMilliseconds();
552   stats_->ReceivedPacket();
553 
554   PacketList packet_list;
555   // Insert packet in a packet list.
556   packet_list.push_back([&rtp_header, &payload, &receive_time_ms] {
557     // Convert to Packet.
558     Packet packet;
559     packet.payload_type = rtp_header.payloadType;
560     packet.sequence_number = rtp_header.sequenceNumber;
561     packet.timestamp = rtp_header.timestamp;
562     packet.payload.SetData(payload.data(), payload.size());
563     packet.packet_info = RtpPacketInfo(rtp_header, receive_time_ms);
564     // Waiting time will be set upon inserting the packet in the buffer.
565     RTC_DCHECK(!packet.waiting_time);
566     return packet;
567   }());
568 
569   bool update_sample_rate_and_channels = first_packet_;
570 
571   if (update_sample_rate_and_channels) {
572     // Reset timestamp scaling.
573     timestamp_scaler_->Reset();
574   }
575 
576   if (!decoder_database_->IsRed(rtp_header.payloadType)) {
577     // Scale timestamp to internal domain (only for some codecs).
578     timestamp_scaler_->ToInternal(&packet_list);
579   }
580 
581   // Store these for later use, since the first packet may very well disappear
582   // before we need these values.
583   uint32_t main_timestamp = packet_list.front().timestamp;
584   uint8_t main_payload_type = packet_list.front().payload_type;
585   uint16_t main_sequence_number = packet_list.front().sequence_number;
586 
587   // Reinitialize NetEq if it's needed (changed SSRC or first call).
588   if (update_sample_rate_and_channels) {
589     // Note: |first_packet_| will be cleared further down in this method, once
590     // the packet has been successfully inserted into the packet buffer.
591 
592     // Flush the packet buffer and DTMF buffer.
593     packet_buffer_->Flush();
594     dtmf_buffer_->Flush();
595 
596     // Update audio buffer timestamp.
597     sync_buffer_->IncreaseEndTimestamp(main_timestamp - timestamp_);
598 
599     // Update codecs.
600     timestamp_ = main_timestamp;
601   }
602 
603   if (nack_enabled_) {
604     RTC_DCHECK(nack_);
605     if (update_sample_rate_and_channels) {
606       nack_->Reset();
607     }
608     nack_->UpdateLastReceivedPacket(rtp_header.sequenceNumber,
609                                     rtp_header.timestamp);
610   }
611 
612   // Check for RED payload type, and separate payloads into several packets.
613   if (decoder_database_->IsRed(rtp_header.payloadType)) {
614     if (!red_payload_splitter_->SplitRed(&packet_list)) {
615       return kRedundancySplitError;
616     }
617     // Only accept a few RED payloads of the same type as the main data,
618     // DTMF events and CNG.
619     red_payload_splitter_->CheckRedPayloads(&packet_list, *decoder_database_);
620     if (packet_list.empty()) {
621       return kRedundancySplitError;
622     }
623   }
624 
625   // Check payload types.
626   if (decoder_database_->CheckPayloadTypes(packet_list) ==
627       DecoderDatabase::kDecoderNotFound) {
628     return kUnknownRtpPayloadType;
629   }
630 
631   RTC_DCHECK(!packet_list.empty());
632 
633   // Update main_timestamp, if new packets appear in the list
634   // after RED splitting.
635   if (decoder_database_->IsRed(rtp_header.payloadType)) {
636     timestamp_scaler_->ToInternal(&packet_list);
637     main_timestamp = packet_list.front().timestamp;
638     main_payload_type = packet_list.front().payload_type;
639     main_sequence_number = packet_list.front().sequence_number;
640   }
641 
642   // Process DTMF payloads. Cycle through the list of packets, and pick out any
643   // DTMF payloads found.
644   PacketList::iterator it = packet_list.begin();
645   while (it != packet_list.end()) {
646     const Packet& current_packet = (*it);
647     RTC_DCHECK(!current_packet.payload.empty());
648     if (decoder_database_->IsDtmf(current_packet.payload_type)) {
649       DtmfEvent event;
650       int ret = DtmfBuffer::ParseEvent(current_packet.timestamp,
651                                        current_packet.payload.data(),
652                                        current_packet.payload.size(), &event);
653       if (ret != DtmfBuffer::kOK) {
654         return kDtmfParsingError;
655       }
656       if (dtmf_buffer_->InsertEvent(event) != DtmfBuffer::kOK) {
657         return kDtmfInsertError;
658       }
659       it = packet_list.erase(it);
660     } else {
661       ++it;
662     }
663   }
664 
665   PacketList parsed_packet_list;
666   while (!packet_list.empty()) {
667     Packet& packet = packet_list.front();
668     const DecoderDatabase::DecoderInfo* info =
669         decoder_database_->GetDecoderInfo(packet.payload_type);
670     if (!info) {
671       RTC_LOG(LS_WARNING) << "SplitAudio unknown payload type";
672       return kUnknownRtpPayloadType;
673     }
674 
675     if (info->IsComfortNoise()) {
676       // Carry comfort noise packets along.
677       parsed_packet_list.splice(parsed_packet_list.end(), packet_list,
678                                 packet_list.begin());
679     } else {
680       const auto sequence_number = packet.sequence_number;
681       const auto payload_type = packet.payload_type;
682       const Packet::Priority original_priority = packet.priority;
683       const auto& packet_info = packet.packet_info;
684       auto packet_from_result = [&](AudioDecoder::ParseResult& result) {
685         Packet new_packet;
686         new_packet.sequence_number = sequence_number;
687         new_packet.payload_type = payload_type;
688         new_packet.timestamp = result.timestamp;
689         new_packet.priority.codec_level = result.priority;
690         new_packet.priority.red_level = original_priority.red_level;
691         new_packet.packet_info = packet_info;
692         new_packet.frame = std::move(result.frame);
693         return new_packet;
694       };
695 
696       std::vector<AudioDecoder::ParseResult> results =
697           info->GetDecoder()->ParsePayload(std::move(packet.payload),
698                                            packet.timestamp);
699       if (results.empty()) {
700         packet_list.pop_front();
701       } else {
702         bool first = true;
703         for (auto& result : results) {
704           RTC_DCHECK(result.frame);
705           RTC_DCHECK_GE(result.priority, 0);
706           if (first) {
707             // Re-use the node and move it to parsed_packet_list.
708             packet_list.front() = packet_from_result(result);
709             parsed_packet_list.splice(parsed_packet_list.end(), packet_list,
710                                       packet_list.begin());
711             first = false;
712           } else {
713             parsed_packet_list.push_back(packet_from_result(result));
714           }
715         }
716       }
717     }
718   }
719 
720   // Calculate the number of primary (non-FEC/RED) packets.
721   const size_t number_of_primary_packets = std::count_if(
722       parsed_packet_list.begin(), parsed_packet_list.end(),
723       [](const Packet& in) { return in.priority.codec_level == 0; });
724   if (number_of_primary_packets < parsed_packet_list.size()) {
725     stats_->SecondaryPacketsReceived(parsed_packet_list.size() -
726                                      number_of_primary_packets);
727   }
728 
729   // Insert packets in buffer.
730   const int ret = packet_buffer_->InsertPacketList(
731       &parsed_packet_list, *decoder_database_, &current_rtp_payload_type_,
732       &current_cng_rtp_payload_type_, stats_.get());
733   if (ret == PacketBuffer::kFlushed) {
734     // Reset DSP timestamp etc. if packet buffer flushed.
735     new_codec_ = true;
736     update_sample_rate_and_channels = true;
737   } else if (ret != PacketBuffer::kOK) {
738     return kOtherError;
739   }
740 
741   if (first_packet_) {
742     first_packet_ = false;
743     // Update the codec on the next GetAudio call.
744     new_codec_ = true;
745   }
746 
747   if (current_rtp_payload_type_) {
748     RTC_DCHECK(decoder_database_->GetDecoderInfo(*current_rtp_payload_type_))
749         << "Payload type " << static_cast<int>(*current_rtp_payload_type_)
750         << " is unknown where it shouldn't be";
751   }
752 
753   if (update_sample_rate_and_channels && !packet_buffer_->Empty()) {
754     // We do not use |current_rtp_payload_type_| to |set payload_type|, but
755     // get the next RTP header from |packet_buffer_| to obtain the payload type.
756     // The reason for it is the following corner case. If NetEq receives a
757     // CNG packet with a sample rate different than the current CNG then it
758     // flushes its buffer, assuming send codec must have been changed. However,
759     // payload type of the hypothetically new send codec is not known.
760     const Packet* next_packet = packet_buffer_->PeekNextPacket();
761     RTC_DCHECK(next_packet);
762     const int payload_type = next_packet->payload_type;
763     size_t channels = 1;
764     if (!decoder_database_->IsComfortNoise(payload_type)) {
765       AudioDecoder* decoder = decoder_database_->GetDecoder(payload_type);
766       assert(decoder);  // Payloads are already checked to be valid.
767       channels = decoder->Channels();
768     }
769     const DecoderDatabase::DecoderInfo* decoder_info =
770         decoder_database_->GetDecoderInfo(payload_type);
771     assert(decoder_info);
772     if (decoder_info->SampleRateHz() != fs_hz_ ||
773         channels != algorithm_buffer_->Channels()) {
774       SetSampleRateAndChannels(decoder_info->SampleRateHz(), channels);
775     }
776     if (nack_enabled_) {
777       RTC_DCHECK(nack_);
778       // Update the sample rate even if the rate is not new, because of Reset().
779       nack_->UpdateSampleRate(fs_hz_);
780     }
781   }
782 
783   const DecoderDatabase::DecoderInfo* dec_info =
784       decoder_database_->GetDecoderInfo(main_payload_type);
785   assert(dec_info);  // Already checked that the payload type is known.
786 
787   const bool last_cng_or_dtmf =
788       dec_info->IsComfortNoise() || dec_info->IsDtmf();
789   const size_t packet_length_samples =
790       number_of_primary_packets * decoder_frame_length_;
791   // Only update statistics if incoming packet is not older than last played
792   // out packet or RTX handling is enabled, and if new codec flag is not
793   // set.
794   const bool should_update_stats =
795       (enable_rtx_handling_ ||
796        static_cast<int32_t>(main_timestamp - timestamp_) >= 0) &&
797       !new_codec_;
798 
799   auto relative_delay = controller_->PacketArrived(
800       last_cng_or_dtmf, packet_length_samples, should_update_stats,
801       main_sequence_number, main_timestamp, fs_hz_);
802   if (relative_delay) {
803     stats_->RelativePacketArrivalDelay(relative_delay.value());
804   }
805   return 0;
806 }
807 
GetAudioInternal(AudioFrame * audio_frame,bool * muted,absl::optional<Operation> action_override)808 int NetEqImpl::GetAudioInternal(AudioFrame* audio_frame,
809                                 bool* muted,
810                                 absl::optional<Operation> action_override) {
811   PacketList packet_list;
812   DtmfEvent dtmf_event;
813   Operation operation;
814   bool play_dtmf;
815   *muted = false;
816   last_decoded_timestamps_.clear();
817   last_decoded_packet_infos_.clear();
818   tick_timer_->Increment();
819   stats_->IncreaseCounter(output_size_samples_, fs_hz_);
820   const auto lifetime_stats = stats_->GetLifetimeStatistics();
821   expand_uma_logger_.UpdateSampleCounter(lifetime_stats.concealed_samples,
822                                          fs_hz_);
823   speech_expand_uma_logger_.UpdateSampleCounter(
824       lifetime_stats.concealed_samples -
825           lifetime_stats.silent_concealed_samples,
826       fs_hz_);
827 
828   // Check for muted state.
829   if (enable_muted_state_ && expand_->Muted() && packet_buffer_->Empty()) {
830     RTC_DCHECK_EQ(last_mode_, Mode::kExpand);
831     audio_frame->Reset();
832     RTC_DCHECK(audio_frame->muted());  // Reset() should mute the frame.
833     playout_timestamp_ += static_cast<uint32_t>(output_size_samples_);
834     audio_frame->sample_rate_hz_ = fs_hz_;
835     audio_frame->samples_per_channel_ = output_size_samples_;
836     audio_frame->timestamp_ =
837         first_packet_
838             ? 0
839             : timestamp_scaler_->ToExternal(playout_timestamp_) -
840                   static_cast<uint32_t>(audio_frame->samples_per_channel_);
841     audio_frame->num_channels_ = sync_buffer_->Channels();
842     stats_->ExpandedNoiseSamples(output_size_samples_, false);
843     *muted = true;
844     return 0;
845   }
846   int return_value = GetDecision(&operation, &packet_list, &dtmf_event,
847                                  &play_dtmf, action_override);
848   if (return_value != 0) {
849     last_mode_ = Mode::kError;
850     return return_value;
851   }
852 
853   AudioDecoder::SpeechType speech_type;
854   int length = 0;
855   const size_t start_num_packets = packet_list.size();
856   int decode_return_value =
857       Decode(&packet_list, &operation, &length, &speech_type);
858 
859   assert(vad_.get());
860   bool sid_frame_available =
861       (operation == Operation::kRfc3389Cng && !packet_list.empty());
862   vad_->Update(decoded_buffer_.get(), static_cast<size_t>(length), speech_type,
863                sid_frame_available, fs_hz_);
864 
865   // This is the criterion that we did decode some data through the speech
866   // decoder, and the operation resulted in comfort noise.
867   const bool codec_internal_sid_frame =
868       (speech_type == AudioDecoder::kComfortNoise &&
869        start_num_packets > packet_list.size());
870 
871   if (sid_frame_available || codec_internal_sid_frame) {
872     // Start a new stopwatch since we are decoding a new CNG packet.
873     generated_noise_stopwatch_ = tick_timer_->GetNewStopwatch();
874   }
875 
876   algorithm_buffer_->Clear();
877   switch (operation) {
878     case Operation::kNormal: {
879       DoNormal(decoded_buffer_.get(), length, speech_type, play_dtmf);
880       if (length > 0) {
881         stats_->DecodedOutputPlayed();
882       }
883       break;
884     }
885     case Operation::kMerge: {
886       DoMerge(decoded_buffer_.get(), length, speech_type, play_dtmf);
887       break;
888     }
889     case Operation::kExpand: {
890       RTC_DCHECK_EQ(return_value, 0);
891       if (!current_rtp_payload_type_ || !DoCodecPlc()) {
892         return_value = DoExpand(play_dtmf);
893       }
894       RTC_DCHECK_GE(sync_buffer_->FutureLength() - expand_->overlap_length(),
895                     output_size_samples_);
896       break;
897     }
898     case Operation::kAccelerate:
899     case Operation::kFastAccelerate: {
900       const bool fast_accelerate =
901           enable_fast_accelerate_ && (operation == Operation::kFastAccelerate);
902       return_value = DoAccelerate(decoded_buffer_.get(), length, speech_type,
903                                   play_dtmf, fast_accelerate);
904       break;
905     }
906     case Operation::kPreemptiveExpand: {
907       return_value = DoPreemptiveExpand(decoded_buffer_.get(), length,
908                                         speech_type, play_dtmf);
909       break;
910     }
911     case Operation::kRfc3389Cng:
912     case Operation::kRfc3389CngNoPacket: {
913       return_value = DoRfc3389Cng(&packet_list, play_dtmf);
914       break;
915     }
916     case Operation::kCodecInternalCng: {
917       // This handles the case when there is no transmission and the decoder
918       // should produce internal comfort noise.
919       // TODO(hlundin): Write test for codec-internal CNG.
920       DoCodecInternalCng(decoded_buffer_.get(), length);
921       break;
922     }
923     case Operation::kDtmf: {
924       // TODO(hlundin): Write test for this.
925       return_value = DoDtmf(dtmf_event, &play_dtmf);
926       break;
927     }
928     case Operation::kUndefined: {
929       RTC_LOG(LS_ERROR) << "Invalid operation kUndefined.";
930       assert(false);  // This should not happen.
931       last_mode_ = Mode::kError;
932       return kInvalidOperation;
933     }
934   }  // End of switch.
935   last_operation_ = operation;
936   if (return_value < 0) {
937     return return_value;
938   }
939 
940   if (last_mode_ != Mode::kRfc3389Cng) {
941     comfort_noise_->Reset();
942   }
943 
944   // We treat it as if all packets referenced to by |last_decoded_packet_infos_|
945   // were mashed together when creating the samples in |algorithm_buffer_|.
946   RtpPacketInfos packet_infos(last_decoded_packet_infos_);
947 
948   // Copy samples from |algorithm_buffer_| to |sync_buffer_|.
949   //
950   // TODO(bugs.webrtc.org/10757):
951   //   We would in the future also like to pass |packet_infos| so that we can do
952   //   sample-perfect tracking of that information across |sync_buffer_|.
953   sync_buffer_->PushBack(*algorithm_buffer_);
954 
955   // Extract data from |sync_buffer_| to |output|.
956   size_t num_output_samples_per_channel = output_size_samples_;
957   size_t num_output_samples = output_size_samples_ * sync_buffer_->Channels();
958   if (num_output_samples > AudioFrame::kMaxDataSizeSamples) {
959     RTC_LOG(LS_WARNING) << "Output array is too short. "
960                         << AudioFrame::kMaxDataSizeSamples << " < "
961                         << output_size_samples_ << " * "
962                         << sync_buffer_->Channels();
963     num_output_samples = AudioFrame::kMaxDataSizeSamples;
964     num_output_samples_per_channel =
965         AudioFrame::kMaxDataSizeSamples / sync_buffer_->Channels();
966   }
967   sync_buffer_->GetNextAudioInterleaved(num_output_samples_per_channel,
968                                         audio_frame);
969   audio_frame->sample_rate_hz_ = fs_hz_;
970   // TODO(bugs.webrtc.org/10757):
971   //   We don't have the ability to properly track individual packets once their
972   //   audio samples have entered |sync_buffer_|. So for now, treat it as if
973   //   |packet_infos| from packets decoded by the current |GetAudioInternal()|
974   //   call were all consumed assembling the current audio frame and the current
975   //   audio frame only.
976   audio_frame->packet_infos_ = std::move(packet_infos);
977   if (sync_buffer_->FutureLength() < expand_->overlap_length()) {
978     // The sync buffer should always contain |overlap_length| samples, but now
979     // too many samples have been extracted. Reinstall the |overlap_length|
980     // lookahead by moving the index.
981     const size_t missing_lookahead_samples =
982         expand_->overlap_length() - sync_buffer_->FutureLength();
983     RTC_DCHECK_GE(sync_buffer_->next_index(), missing_lookahead_samples);
984     sync_buffer_->set_next_index(sync_buffer_->next_index() -
985                                  missing_lookahead_samples);
986   }
987   if (audio_frame->samples_per_channel_ != output_size_samples_) {
988     RTC_LOG(LS_ERROR) << "audio_frame->samples_per_channel_ ("
989                       << audio_frame->samples_per_channel_
990                       << ") != output_size_samples_ (" << output_size_samples_
991                       << ")";
992     // TODO(minyue): treatment of under-run, filling zeros
993     audio_frame->Mute();
994     return kSampleUnderrun;
995   }
996 
997   // Should always have overlap samples left in the |sync_buffer_|.
998   RTC_DCHECK_GE(sync_buffer_->FutureLength(), expand_->overlap_length());
999 
1000   // TODO(yujo): For muted frames, this can be a copy rather than an addition.
1001   if (play_dtmf) {
1002     return_value = DtmfOverdub(dtmf_event, sync_buffer_->Channels(),
1003                                audio_frame->mutable_data());
1004   }
1005 
1006   // Update the background noise parameters if last operation wrote data
1007   // straight from the decoder to the |sync_buffer_|. That is, none of the
1008   // operations that modify the signal can be followed by a parameter update.
1009   if ((last_mode_ == Mode::kNormal) || (last_mode_ == Mode::kAccelerateFail) ||
1010       (last_mode_ == Mode::kPreemptiveExpandFail) ||
1011       (last_mode_ == Mode::kRfc3389Cng) ||
1012       (last_mode_ == Mode::kCodecInternalCng)) {
1013     background_noise_->Update(*sync_buffer_, *vad_.get());
1014   }
1015 
1016   if (operation == Operation::kDtmf) {
1017     // DTMF data was written the end of |sync_buffer_|.
1018     // Update index to end of DTMF data in |sync_buffer_|.
1019     sync_buffer_->set_dtmf_index(sync_buffer_->Size());
1020   }
1021 
1022   if (last_mode_ != Mode::kExpand && last_mode_ != Mode::kCodecPlc) {
1023     // If last operation was not expand, calculate the |playout_timestamp_| from
1024     // the |sync_buffer_|. However, do not update the |playout_timestamp_| if it
1025     // would be moved "backwards".
1026     uint32_t temp_timestamp =
1027         sync_buffer_->end_timestamp() -
1028         static_cast<uint32_t>(sync_buffer_->FutureLength());
1029     if (static_cast<int32_t>(temp_timestamp - playout_timestamp_) > 0) {
1030       playout_timestamp_ = temp_timestamp;
1031     }
1032   } else {
1033     // Use dead reckoning to estimate the |playout_timestamp_|.
1034     playout_timestamp_ += static_cast<uint32_t>(output_size_samples_);
1035   }
1036   // Set the timestamp in the audio frame to zero before the first packet has
1037   // been inserted. Otherwise, subtract the frame size in samples to get the
1038   // timestamp of the first sample in the frame (playout_timestamp_ is the
1039   // last + 1).
1040   audio_frame->timestamp_ =
1041       first_packet_
1042           ? 0
1043           : timestamp_scaler_->ToExternal(playout_timestamp_) -
1044                 static_cast<uint32_t>(audio_frame->samples_per_channel_);
1045 
1046   if (!(last_mode_ == Mode::kRfc3389Cng ||
1047         last_mode_ == Mode::kCodecInternalCng || last_mode_ == Mode::kExpand ||
1048         last_mode_ == Mode::kCodecPlc)) {
1049     generated_noise_stopwatch_.reset();
1050   }
1051 
1052   if (decode_return_value)
1053     return decode_return_value;
1054   return return_value;
1055 }
1056 
GetDecision(Operation * operation,PacketList * packet_list,DtmfEvent * dtmf_event,bool * play_dtmf,absl::optional<Operation> action_override)1057 int NetEqImpl::GetDecision(Operation* operation,
1058                            PacketList* packet_list,
1059                            DtmfEvent* dtmf_event,
1060                            bool* play_dtmf,
1061                            absl::optional<Operation> action_override) {
1062   // Initialize output variables.
1063   *play_dtmf = false;
1064   *operation = Operation::kUndefined;
1065 
1066   assert(sync_buffer_.get());
1067   uint32_t end_timestamp = sync_buffer_->end_timestamp();
1068   if (!new_codec_) {
1069     const uint32_t five_seconds_samples = 5 * fs_hz_;
1070     packet_buffer_->DiscardOldPackets(end_timestamp, five_seconds_samples,
1071                                       stats_.get());
1072   }
1073   const Packet* packet = packet_buffer_->PeekNextPacket();
1074 
1075   RTC_DCHECK(!generated_noise_stopwatch_ ||
1076              generated_noise_stopwatch_->ElapsedTicks() >= 1);
1077   uint64_t generated_noise_samples =
1078       generated_noise_stopwatch_ ? (generated_noise_stopwatch_->ElapsedTicks() -
1079                                     1) * output_size_samples_ +
1080                                        controller_->noise_fast_forward()
1081                                  : 0;
1082 
1083   if (controller_->CngRfc3389On() || last_mode_ == Mode::kRfc3389Cng) {
1084     // Because of timestamp peculiarities, we have to "manually" disallow using
1085     // a CNG packet with the same timestamp as the one that was last played.
1086     // This can happen when using redundancy and will cause the timing to shift.
1087     while (packet && decoder_database_->IsComfortNoise(packet->payload_type) &&
1088            (end_timestamp >= packet->timestamp ||
1089             end_timestamp + generated_noise_samples > packet->timestamp)) {
1090       // Don't use this packet, discard it.
1091       if (packet_buffer_->DiscardNextPacket(stats_.get()) !=
1092           PacketBuffer::kOK) {
1093         assert(false);  // Must be ok by design.
1094       }
1095       // Check buffer again.
1096       if (!new_codec_) {
1097         packet_buffer_->DiscardOldPackets(end_timestamp, 5 * fs_hz_,
1098                                           stats_.get());
1099       }
1100       packet = packet_buffer_->PeekNextPacket();
1101     }
1102   }
1103 
1104   assert(expand_.get());
1105   const int samples_left = static_cast<int>(sync_buffer_->FutureLength() -
1106                                             expand_->overlap_length());
1107   if (last_mode_ == Mode::kAccelerateSuccess ||
1108       last_mode_ == Mode::kAccelerateLowEnergy ||
1109       last_mode_ == Mode::kPreemptiveExpandSuccess ||
1110       last_mode_ == Mode::kPreemptiveExpandLowEnergy) {
1111     // Subtract (samples_left + output_size_samples_) from sampleMemory.
1112     controller_->AddSampleMemory(
1113         -(samples_left + rtc::dchecked_cast<int>(output_size_samples_)));
1114   }
1115 
1116   // Check if it is time to play a DTMF event.
1117   if (dtmf_buffer_->GetEvent(
1118           static_cast<uint32_t>(end_timestamp + generated_noise_samples),
1119           dtmf_event)) {
1120     *play_dtmf = true;
1121   }
1122 
1123   // Get instruction.
1124   assert(sync_buffer_.get());
1125   assert(expand_.get());
1126   generated_noise_samples =
1127       generated_noise_stopwatch_
1128           ? generated_noise_stopwatch_->ElapsedTicks() * output_size_samples_ +
1129                 controller_->noise_fast_forward()
1130           : 0;
1131   NetEqController::NetEqStatus status;
1132   status.packet_buffer_info.dtx_or_cng =
1133       packet_buffer_->ContainsDtxOrCngPacket(decoder_database_.get());
1134   status.packet_buffer_info.num_samples =
1135       packet_buffer_->NumSamplesInBuffer(decoder_frame_length_);
1136   status.packet_buffer_info.span_samples = packet_buffer_->GetSpanSamples(
1137       decoder_frame_length_, last_output_sample_rate_hz_, true);
1138   status.packet_buffer_info.span_samples_no_dtx =
1139       packet_buffer_->GetSpanSamples(decoder_frame_length_,
1140                                      last_output_sample_rate_hz_, false);
1141   status.packet_buffer_info.num_packets = packet_buffer_->NumPacketsInBuffer();
1142   status.target_timestamp = sync_buffer_->end_timestamp();
1143   status.expand_mutefactor = expand_->MuteFactor(0);
1144   status.last_packet_samples = decoder_frame_length_;
1145   status.last_mode = last_mode_;
1146   status.play_dtmf = *play_dtmf;
1147   status.generated_noise_samples = generated_noise_samples;
1148   status.sync_buffer_samples = sync_buffer_->FutureLength();
1149   if (packet) {
1150     status.next_packet = {
1151         packet->timestamp, packet->frame && packet->frame->IsDtxPacket(),
1152         decoder_database_->IsComfortNoise(packet->payload_type)};
1153   }
1154   *operation = controller_->GetDecision(status, &reset_decoder_);
1155 
1156   // Disallow time stretching if this packet is DTX, because such a decision may
1157   // be based on earlier buffer level estimate, as we do not update buffer level
1158   // during DTX. When we have a better way to update buffer level during DTX,
1159   // this can be discarded.
1160   if (packet && packet->frame && packet->frame->IsDtxPacket() &&
1161       (*operation == Operation::kMerge ||
1162        *operation == Operation::kAccelerate ||
1163        *operation == Operation::kFastAccelerate ||
1164        *operation == Operation::kPreemptiveExpand)) {
1165     *operation = Operation::kNormal;
1166   }
1167 
1168   if (action_override) {
1169     // Use the provided action instead of the decision NetEq decided on.
1170     *operation = *action_override;
1171   }
1172   // Check if we already have enough samples in the |sync_buffer_|. If so,
1173   // change decision to normal, unless the decision was merge, accelerate, or
1174   // preemptive expand.
1175   if (samples_left >= rtc::dchecked_cast<int>(output_size_samples_) &&
1176       *operation != Operation::kMerge && *operation != Operation::kAccelerate &&
1177       *operation != Operation::kFastAccelerate &&
1178       *operation != Operation::kPreemptiveExpand) {
1179     *operation = Operation::kNormal;
1180     return 0;
1181   }
1182 
1183   controller_->ExpandDecision(*operation);
1184 
1185   // Check conditions for reset.
1186   if (new_codec_ || *operation == Operation::kUndefined) {
1187     // The only valid reason to get kUndefined is that new_codec_ is set.
1188     assert(new_codec_);
1189     if (*play_dtmf && !packet) {
1190       timestamp_ = dtmf_event->timestamp;
1191     } else {
1192       if (!packet) {
1193         RTC_LOG(LS_ERROR) << "Packet missing where it shouldn't.";
1194         return -1;
1195       }
1196       timestamp_ = packet->timestamp;
1197       if (*operation == Operation::kRfc3389CngNoPacket &&
1198           decoder_database_->IsComfortNoise(packet->payload_type)) {
1199         // Change decision to CNG packet, since we do have a CNG packet, but it
1200         // was considered too early to use. Now, use it anyway.
1201         *operation = Operation::kRfc3389Cng;
1202       } else if (*operation != Operation::kRfc3389Cng) {
1203         *operation = Operation::kNormal;
1204       }
1205     }
1206     // Adjust |sync_buffer_| timestamp before setting |end_timestamp| to the
1207     // new value.
1208     sync_buffer_->IncreaseEndTimestamp(timestamp_ - end_timestamp);
1209     end_timestamp = timestamp_;
1210     new_codec_ = false;
1211     controller_->SoftReset();
1212     stats_->ResetMcu();
1213   }
1214 
1215   size_t required_samples = output_size_samples_;
1216   const size_t samples_10_ms = static_cast<size_t>(80 * fs_mult_);
1217   const size_t samples_20_ms = 2 * samples_10_ms;
1218   const size_t samples_30_ms = 3 * samples_10_ms;
1219 
1220   switch (*operation) {
1221     case Operation::kExpand: {
1222       timestamp_ = end_timestamp;
1223       return 0;
1224     }
1225     case Operation::kRfc3389CngNoPacket:
1226     case Operation::kCodecInternalCng: {
1227       return 0;
1228     }
1229     case Operation::kDtmf: {
1230       // TODO(hlundin): Write test for this.
1231       // Update timestamp.
1232       timestamp_ = end_timestamp;
1233       const uint64_t generated_noise_samples =
1234           generated_noise_stopwatch_
1235               ? generated_noise_stopwatch_->ElapsedTicks() *
1236                         output_size_samples_ +
1237                     controller_->noise_fast_forward()
1238               : 0;
1239       if (generated_noise_samples > 0 && last_mode_ != Mode::kDtmf) {
1240         // Make a jump in timestamp due to the recently played comfort noise.
1241         uint32_t timestamp_jump =
1242             static_cast<uint32_t>(generated_noise_samples);
1243         sync_buffer_->IncreaseEndTimestamp(timestamp_jump);
1244         timestamp_ += timestamp_jump;
1245       }
1246       return 0;
1247     }
1248     case Operation::kAccelerate:
1249     case Operation::kFastAccelerate: {
1250       // In order to do an accelerate we need at least 30 ms of audio data.
1251       if (samples_left >= static_cast<int>(samples_30_ms)) {
1252         // Already have enough data, so we do not need to extract any more.
1253         controller_->set_sample_memory(samples_left);
1254         controller_->set_prev_time_scale(true);
1255         return 0;
1256       } else if (samples_left >= static_cast<int>(samples_10_ms) &&
1257                  decoder_frame_length_ >= samples_30_ms) {
1258         // Avoid decoding more data as it might overflow the playout buffer.
1259         *operation = Operation::kNormal;
1260         return 0;
1261       } else if (samples_left < static_cast<int>(samples_20_ms) &&
1262                  decoder_frame_length_ < samples_30_ms) {
1263         // Build up decoded data by decoding at least 20 ms of audio data. Do
1264         // not perform accelerate yet, but wait until we only need to do one
1265         // decoding.
1266         required_samples = 2 * output_size_samples_;
1267         *operation = Operation::kNormal;
1268       }
1269       // If none of the above is true, we have one of two possible situations:
1270       // (1) 20 ms <= samples_left < 30 ms and decoder_frame_length_ < 30 ms; or
1271       // (2) samples_left < 10 ms and decoder_frame_length_ >= 30 ms.
1272       // In either case, we move on with the accelerate decision, and decode one
1273       // frame now.
1274       break;
1275     }
1276     case Operation::kPreemptiveExpand: {
1277       // In order to do a preemptive expand we need at least 30 ms of decoded
1278       // audio data.
1279       if ((samples_left >= static_cast<int>(samples_30_ms)) ||
1280           (samples_left >= static_cast<int>(samples_10_ms) &&
1281            decoder_frame_length_ >= samples_30_ms)) {
1282         // Already have enough data, so we do not need to extract any more.
1283         // Or, avoid decoding more data as it might overflow the playout buffer.
1284         // Still try preemptive expand, though.
1285         controller_->set_sample_memory(samples_left);
1286         controller_->set_prev_time_scale(true);
1287         return 0;
1288       }
1289       if (samples_left < static_cast<int>(samples_20_ms) &&
1290           decoder_frame_length_ < samples_30_ms) {
1291         // Build up decoded data by decoding at least 20 ms of audio data.
1292         // Still try to perform preemptive expand.
1293         required_samples = 2 * output_size_samples_;
1294       }
1295       // Move on with the preemptive expand decision.
1296       break;
1297     }
1298     case Operation::kMerge: {
1299       required_samples =
1300           std::max(merge_->RequiredFutureSamples(), required_samples);
1301       break;
1302     }
1303     default: {
1304       // Do nothing.
1305     }
1306   }
1307 
1308   // Get packets from buffer.
1309   int extracted_samples = 0;
1310   if (packet) {
1311     sync_buffer_->IncreaseEndTimestamp(packet->timestamp - end_timestamp);
1312     if (controller_->CngOff()) {
1313       // Adjustment of timestamp only corresponds to an actual packet loss
1314       // if comfort noise is not played. If comfort noise was just played,
1315       // this adjustment of timestamp is only done to get back in sync with the
1316       // stream timestamp; no loss to report.
1317       stats_->LostSamples(packet->timestamp - end_timestamp);
1318     }
1319 
1320     if (*operation != Operation::kRfc3389Cng) {
1321       // We are about to decode and use a non-CNG packet.
1322       controller_->SetCngOff();
1323     }
1324 
1325     extracted_samples = ExtractPackets(required_samples, packet_list);
1326     if (extracted_samples < 0) {
1327       return kPacketBufferCorruption;
1328     }
1329   }
1330 
1331   if (*operation == Operation::kAccelerate ||
1332       *operation == Operation::kFastAccelerate ||
1333       *operation == Operation::kPreemptiveExpand) {
1334     controller_->set_sample_memory(samples_left + extracted_samples);
1335     controller_->set_prev_time_scale(true);
1336   }
1337 
1338   if (*operation == Operation::kAccelerate ||
1339       *operation == Operation::kFastAccelerate) {
1340     // Check that we have enough data (30ms) to do accelerate.
1341     if (extracted_samples + samples_left < static_cast<int>(samples_30_ms)) {
1342       // TODO(hlundin): Write test for this.
1343       // Not enough, do normal operation instead.
1344       *operation = Operation::kNormal;
1345     }
1346   }
1347 
1348   timestamp_ = end_timestamp;
1349   return 0;
1350 }
1351 
Decode(PacketList * packet_list,Operation * operation,int * decoded_length,AudioDecoder::SpeechType * speech_type)1352 int NetEqImpl::Decode(PacketList* packet_list,
1353                       Operation* operation,
1354                       int* decoded_length,
1355                       AudioDecoder::SpeechType* speech_type) {
1356   *speech_type = AudioDecoder::kSpeech;
1357 
1358   // When packet_list is empty, we may be in kCodecInternalCng mode, and for
1359   // that we use current active decoder.
1360   AudioDecoder* decoder = decoder_database_->GetActiveDecoder();
1361 
1362   if (!packet_list->empty()) {
1363     const Packet& packet = packet_list->front();
1364     uint8_t payload_type = packet.payload_type;
1365     if (!decoder_database_->IsComfortNoise(payload_type)) {
1366       decoder = decoder_database_->GetDecoder(payload_type);
1367       assert(decoder);
1368       if (!decoder) {
1369         RTC_LOG(LS_WARNING)
1370             << "Unknown payload type " << static_cast<int>(payload_type);
1371         packet_list->clear();
1372         return kDecoderNotFound;
1373       }
1374       bool decoder_changed;
1375       decoder_database_->SetActiveDecoder(payload_type, &decoder_changed);
1376       if (decoder_changed) {
1377         // We have a new decoder. Re-init some values.
1378         const DecoderDatabase::DecoderInfo* decoder_info =
1379             decoder_database_->GetDecoderInfo(payload_type);
1380         assert(decoder_info);
1381         if (!decoder_info) {
1382           RTC_LOG(LS_WARNING)
1383               << "Unknown payload type " << static_cast<int>(payload_type);
1384           packet_list->clear();
1385           return kDecoderNotFound;
1386         }
1387         // If sampling rate or number of channels has changed, we need to make
1388         // a reset.
1389         if (decoder_info->SampleRateHz() != fs_hz_ ||
1390             decoder->Channels() != algorithm_buffer_->Channels()) {
1391           // TODO(tlegrand): Add unittest to cover this event.
1392           SetSampleRateAndChannels(decoder_info->SampleRateHz(),
1393                                    decoder->Channels());
1394         }
1395         sync_buffer_->set_end_timestamp(timestamp_);
1396         playout_timestamp_ = timestamp_;
1397       }
1398     }
1399   }
1400 
1401   if (reset_decoder_) {
1402     // TODO(hlundin): Write test for this.
1403     if (decoder)
1404       decoder->Reset();
1405 
1406     // Reset comfort noise decoder.
1407     ComfortNoiseDecoder* cng_decoder = decoder_database_->GetActiveCngDecoder();
1408     if (cng_decoder)
1409       cng_decoder->Reset();
1410 
1411     reset_decoder_ = false;
1412   }
1413 
1414   *decoded_length = 0;
1415   // Update codec-internal PLC state.
1416   if ((*operation == Operation::kMerge) && decoder && decoder->HasDecodePlc()) {
1417     decoder->DecodePlc(1, &decoded_buffer_[*decoded_length]);
1418   }
1419 
1420   int return_value;
1421   if (*operation == Operation::kCodecInternalCng) {
1422     RTC_DCHECK(packet_list->empty());
1423     return_value = DecodeCng(decoder, decoded_length, speech_type);
1424   } else {
1425     return_value = DecodeLoop(packet_list, *operation, decoder, decoded_length,
1426                               speech_type);
1427   }
1428 
1429   if (*decoded_length < 0) {
1430     // Error returned from the decoder.
1431     *decoded_length = 0;
1432     sync_buffer_->IncreaseEndTimestamp(
1433         static_cast<uint32_t>(decoder_frame_length_));
1434     int error_code = 0;
1435     if (decoder)
1436       error_code = decoder->ErrorCode();
1437     if (error_code != 0) {
1438       // Got some error code from the decoder.
1439       return_value = kDecoderErrorCode;
1440       RTC_LOG(LS_WARNING) << "Decoder returned error code: " << error_code;
1441     } else {
1442       // Decoder does not implement error codes. Return generic error.
1443       return_value = kOtherDecoderError;
1444       RTC_LOG(LS_WARNING) << "Decoder error (no error code)";
1445     }
1446     *operation = Operation::kExpand;  // Do expansion to get data instead.
1447   }
1448   if (*speech_type != AudioDecoder::kComfortNoise) {
1449     // Don't increment timestamp if codec returned CNG speech type
1450     // since in this case, the we will increment the CNGplayedTS counter.
1451     // Increase with number of samples per channel.
1452     assert(*decoded_length == 0 ||
1453            (decoder && decoder->Channels() == sync_buffer_->Channels()));
1454     sync_buffer_->IncreaseEndTimestamp(
1455         *decoded_length / static_cast<int>(sync_buffer_->Channels()));
1456   }
1457   return return_value;
1458 }
1459 
DecodeCng(AudioDecoder * decoder,int * decoded_length,AudioDecoder::SpeechType * speech_type)1460 int NetEqImpl::DecodeCng(AudioDecoder* decoder,
1461                          int* decoded_length,
1462                          AudioDecoder::SpeechType* speech_type) {
1463   if (!decoder) {
1464     // This happens when active decoder is not defined.
1465     *decoded_length = -1;
1466     return 0;
1467   }
1468 
1469   while (*decoded_length < rtc::dchecked_cast<int>(output_size_samples_)) {
1470     const int length = decoder->Decode(
1471         nullptr, 0, fs_hz_,
1472         (decoded_buffer_length_ - *decoded_length) * sizeof(int16_t),
1473         &decoded_buffer_[*decoded_length], speech_type);
1474     if (length > 0) {
1475       *decoded_length += length;
1476     } else {
1477       // Error.
1478       RTC_LOG(LS_WARNING) << "Failed to decode CNG";
1479       *decoded_length = -1;
1480       break;
1481     }
1482     if (*decoded_length > static_cast<int>(decoded_buffer_length_)) {
1483       // Guard against overflow.
1484       RTC_LOG(LS_WARNING) << "Decoded too much CNG.";
1485       return kDecodedTooMuch;
1486     }
1487   }
1488   return 0;
1489 }
1490 
DecodeLoop(PacketList * packet_list,const Operation & operation,AudioDecoder * decoder,int * decoded_length,AudioDecoder::SpeechType * speech_type)1491 int NetEqImpl::DecodeLoop(PacketList* packet_list,
1492                           const Operation& operation,
1493                           AudioDecoder* decoder,
1494                           int* decoded_length,
1495                           AudioDecoder::SpeechType* speech_type) {
1496   RTC_DCHECK(last_decoded_timestamps_.empty());
1497   RTC_DCHECK(last_decoded_packet_infos_.empty());
1498 
1499   // Do decoding.
1500   while (!packet_list->empty() && !decoder_database_->IsComfortNoise(
1501                                       packet_list->front().payload_type)) {
1502     assert(decoder);  // At this point, we must have a decoder object.
1503     // The number of channels in the |sync_buffer_| should be the same as the
1504     // number decoder channels.
1505     assert(sync_buffer_->Channels() == decoder->Channels());
1506     assert(decoded_buffer_length_ >= kMaxFrameSize * decoder->Channels());
1507     assert(operation == Operation::kNormal ||
1508            operation == Operation::kAccelerate ||
1509            operation == Operation::kFastAccelerate ||
1510            operation == Operation::kMerge ||
1511            operation == Operation::kPreemptiveExpand);
1512 
1513     auto opt_result = packet_list->front().frame->Decode(
1514         rtc::ArrayView<int16_t>(&decoded_buffer_[*decoded_length],
1515                                 decoded_buffer_length_ - *decoded_length));
1516     last_decoded_timestamps_.push_back(packet_list->front().timestamp);
1517     last_decoded_packet_infos_.push_back(
1518         std::move(packet_list->front().packet_info));
1519     packet_list->pop_front();
1520     if (opt_result) {
1521       const auto& result = *opt_result;
1522       *speech_type = result.speech_type;
1523       if (result.num_decoded_samples > 0) {
1524         *decoded_length += rtc::dchecked_cast<int>(result.num_decoded_samples);
1525         // Update |decoder_frame_length_| with number of samples per channel.
1526         decoder_frame_length_ =
1527             result.num_decoded_samples / decoder->Channels();
1528       }
1529     } else {
1530       // Error.
1531       // TODO(ossu): What to put here?
1532       RTC_LOG(LS_WARNING) << "Decode error";
1533       *decoded_length = -1;
1534       last_decoded_packet_infos_.clear();
1535       packet_list->clear();
1536       break;
1537     }
1538     if (*decoded_length > rtc::dchecked_cast<int>(decoded_buffer_length_)) {
1539       // Guard against overflow.
1540       RTC_LOG(LS_WARNING) << "Decoded too much.";
1541       packet_list->clear();
1542       return kDecodedTooMuch;
1543     }
1544   }  // End of decode loop.
1545 
1546   // If the list is not empty at this point, either a decoding error terminated
1547   // the while-loop, or list must hold exactly one CNG packet.
1548   assert(packet_list->empty() || *decoded_length < 0 ||
1549          (packet_list->size() == 1 && decoder_database_->IsComfortNoise(
1550                                           packet_list->front().payload_type)));
1551   return 0;
1552 }
1553 
DoNormal(const int16_t * decoded_buffer,size_t decoded_length,AudioDecoder::SpeechType speech_type,bool play_dtmf)1554 void NetEqImpl::DoNormal(const int16_t* decoded_buffer,
1555                          size_t decoded_length,
1556                          AudioDecoder::SpeechType speech_type,
1557                          bool play_dtmf) {
1558   assert(normal_.get());
1559   normal_->Process(decoded_buffer, decoded_length, last_mode_,
1560                    algorithm_buffer_.get());
1561   if (decoded_length != 0) {
1562     last_mode_ = Mode::kNormal;
1563   }
1564 
1565   // If last packet was decoded as an inband CNG, set mode to CNG instead.
1566   if ((speech_type == AudioDecoder::kComfortNoise) ||
1567       ((last_mode_ == Mode::kCodecInternalCng) && (decoded_length == 0))) {
1568     // TODO(hlundin): Remove second part of || statement above.
1569     last_mode_ = Mode::kCodecInternalCng;
1570   }
1571 
1572   if (!play_dtmf) {
1573     dtmf_tone_generator_->Reset();
1574   }
1575 }
1576 
DoMerge(int16_t * decoded_buffer,size_t decoded_length,AudioDecoder::SpeechType speech_type,bool play_dtmf)1577 void NetEqImpl::DoMerge(int16_t* decoded_buffer,
1578                         size_t decoded_length,
1579                         AudioDecoder::SpeechType speech_type,
1580                         bool play_dtmf) {
1581   assert(merge_.get());
1582   size_t new_length =
1583       merge_->Process(decoded_buffer, decoded_length, algorithm_buffer_.get());
1584   // Correction can be negative.
1585   int expand_length_correction =
1586       rtc::dchecked_cast<int>(new_length) -
1587       rtc::dchecked_cast<int>(decoded_length / algorithm_buffer_->Channels());
1588 
1589   // Update in-call and post-call statistics.
1590   if (expand_->MuteFactor(0) == 0) {
1591     // Expand generates only noise.
1592     stats_->ExpandedNoiseSamplesCorrection(expand_length_correction);
1593   } else {
1594     // Expansion generates more than only noise.
1595     stats_->ExpandedVoiceSamplesCorrection(expand_length_correction);
1596   }
1597 
1598   last_mode_ = Mode::kMerge;
1599   // If last packet was decoded as an inband CNG, set mode to CNG instead.
1600   if (speech_type == AudioDecoder::kComfortNoise) {
1601     last_mode_ = Mode::kCodecInternalCng;
1602   }
1603   expand_->Reset();
1604   if (!play_dtmf) {
1605     dtmf_tone_generator_->Reset();
1606   }
1607 }
1608 
DoCodecPlc()1609 bool NetEqImpl::DoCodecPlc() {
1610   AudioDecoder* decoder = decoder_database_->GetActiveDecoder();
1611   if (!decoder) {
1612     return false;
1613   }
1614   const size_t channels = algorithm_buffer_->Channels();
1615   const size_t requested_samples_per_channel =
1616       output_size_samples_ -
1617       (sync_buffer_->FutureLength() - expand_->overlap_length());
1618   concealment_audio_.Clear();
1619   decoder->GeneratePlc(requested_samples_per_channel, &concealment_audio_);
1620   if (concealment_audio_.empty()) {
1621     // Nothing produced. Resort to regular expand.
1622     return false;
1623   }
1624   RTC_CHECK_GE(concealment_audio_.size(),
1625                requested_samples_per_channel * channels);
1626   sync_buffer_->PushBackInterleaved(concealment_audio_);
1627   RTC_DCHECK_NE(algorithm_buffer_->Channels(), 0);
1628   const size_t concealed_samples_per_channel =
1629       concealment_audio_.size() / channels;
1630 
1631   // Update in-call and post-call statistics.
1632   const bool is_new_concealment_event = (last_mode_ != Mode::kCodecPlc);
1633   if (std::all_of(concealment_audio_.cbegin(), concealment_audio_.cend(),
1634                   [](int16_t i) { return i == 0; })) {
1635     // Expand operation generates only noise.
1636     stats_->ExpandedNoiseSamples(concealed_samples_per_channel,
1637                                  is_new_concealment_event);
1638   } else {
1639     // Expand operation generates more than only noise.
1640     stats_->ExpandedVoiceSamples(concealed_samples_per_channel,
1641                                  is_new_concealment_event);
1642   }
1643   last_mode_ = Mode::kCodecPlc;
1644   if (!generated_noise_stopwatch_) {
1645     // Start a new stopwatch since we may be covering for a lost CNG packet.
1646     generated_noise_stopwatch_ = tick_timer_->GetNewStopwatch();
1647   }
1648   return true;
1649 }
1650 
DoExpand(bool play_dtmf)1651 int NetEqImpl::DoExpand(bool play_dtmf) {
1652   while ((sync_buffer_->FutureLength() - expand_->overlap_length()) <
1653          output_size_samples_) {
1654     algorithm_buffer_->Clear();
1655     int return_value = expand_->Process(algorithm_buffer_.get());
1656     size_t length = algorithm_buffer_->Size();
1657     bool is_new_concealment_event = (last_mode_ != Mode::kExpand);
1658 
1659     // Update in-call and post-call statistics.
1660     if (expand_->MuteFactor(0) == 0) {
1661       // Expand operation generates only noise.
1662       stats_->ExpandedNoiseSamples(length, is_new_concealment_event);
1663     } else {
1664       // Expand operation generates more than only noise.
1665       stats_->ExpandedVoiceSamples(length, is_new_concealment_event);
1666     }
1667 
1668     last_mode_ = Mode::kExpand;
1669 
1670     if (return_value < 0) {
1671       return return_value;
1672     }
1673 
1674     sync_buffer_->PushBack(*algorithm_buffer_);
1675     algorithm_buffer_->Clear();
1676   }
1677   if (!play_dtmf) {
1678     dtmf_tone_generator_->Reset();
1679   }
1680 
1681   if (!generated_noise_stopwatch_) {
1682     // Start a new stopwatch since we may be covering for a lost CNG packet.
1683     generated_noise_stopwatch_ = tick_timer_->GetNewStopwatch();
1684   }
1685 
1686   return 0;
1687 }
1688 
DoAccelerate(int16_t * decoded_buffer,size_t decoded_length,AudioDecoder::SpeechType speech_type,bool play_dtmf,bool fast_accelerate)1689 int NetEqImpl::DoAccelerate(int16_t* decoded_buffer,
1690                             size_t decoded_length,
1691                             AudioDecoder::SpeechType speech_type,
1692                             bool play_dtmf,
1693                             bool fast_accelerate) {
1694   const size_t required_samples =
1695       static_cast<size_t>(240 * fs_mult_);  // Must have 30 ms.
1696   size_t borrowed_samples_per_channel = 0;
1697   size_t num_channels = algorithm_buffer_->Channels();
1698   size_t decoded_length_per_channel = decoded_length / num_channels;
1699   if (decoded_length_per_channel < required_samples) {
1700     // Must move data from the |sync_buffer_| in order to get 30 ms.
1701     borrowed_samples_per_channel =
1702         static_cast<int>(required_samples - decoded_length_per_channel);
1703     memmove(&decoded_buffer[borrowed_samples_per_channel * num_channels],
1704             decoded_buffer, sizeof(int16_t) * decoded_length);
1705     sync_buffer_->ReadInterleavedFromEnd(borrowed_samples_per_channel,
1706                                          decoded_buffer);
1707     decoded_length = required_samples * num_channels;
1708   }
1709 
1710   size_t samples_removed;
1711   Accelerate::ReturnCodes return_code =
1712       accelerate_->Process(decoded_buffer, decoded_length, fast_accelerate,
1713                            algorithm_buffer_.get(), &samples_removed);
1714   stats_->AcceleratedSamples(samples_removed);
1715   switch (return_code) {
1716     case Accelerate::kSuccess:
1717       last_mode_ = Mode::kAccelerateSuccess;
1718       break;
1719     case Accelerate::kSuccessLowEnergy:
1720       last_mode_ = Mode::kAccelerateLowEnergy;
1721       break;
1722     case Accelerate::kNoStretch:
1723       last_mode_ = Mode::kAccelerateFail;
1724       break;
1725     case Accelerate::kError:
1726       // TODO(hlundin): Map to Modes::kError instead?
1727       last_mode_ = Mode::kAccelerateFail;
1728       return kAccelerateError;
1729   }
1730 
1731   if (borrowed_samples_per_channel > 0) {
1732     // Copy borrowed samples back to the |sync_buffer_|.
1733     size_t length = algorithm_buffer_->Size();
1734     if (length < borrowed_samples_per_channel) {
1735       // This destroys the beginning of the buffer, but will not cause any
1736       // problems.
1737       sync_buffer_->ReplaceAtIndex(
1738           *algorithm_buffer_,
1739           sync_buffer_->Size() - borrowed_samples_per_channel);
1740       sync_buffer_->PushFrontZeros(borrowed_samples_per_channel - length);
1741       algorithm_buffer_->PopFront(length);
1742       assert(algorithm_buffer_->Empty());
1743     } else {
1744       sync_buffer_->ReplaceAtIndex(
1745           *algorithm_buffer_, borrowed_samples_per_channel,
1746           sync_buffer_->Size() - borrowed_samples_per_channel);
1747       algorithm_buffer_->PopFront(borrowed_samples_per_channel);
1748     }
1749   }
1750 
1751   // If last packet was decoded as an inband CNG, set mode to CNG instead.
1752   if (speech_type == AudioDecoder::kComfortNoise) {
1753     last_mode_ = Mode::kCodecInternalCng;
1754   }
1755   if (!play_dtmf) {
1756     dtmf_tone_generator_->Reset();
1757   }
1758   expand_->Reset();
1759   return 0;
1760 }
1761 
DoPreemptiveExpand(int16_t * decoded_buffer,size_t decoded_length,AudioDecoder::SpeechType speech_type,bool play_dtmf)1762 int NetEqImpl::DoPreemptiveExpand(int16_t* decoded_buffer,
1763                                   size_t decoded_length,
1764                                   AudioDecoder::SpeechType speech_type,
1765                                   bool play_dtmf) {
1766   const size_t required_samples =
1767       static_cast<size_t>(240 * fs_mult_);  // Must have 30 ms.
1768   size_t num_channels = algorithm_buffer_->Channels();
1769   size_t borrowed_samples_per_channel = 0;
1770   size_t old_borrowed_samples_per_channel = 0;
1771   size_t decoded_length_per_channel = decoded_length / num_channels;
1772   if (decoded_length_per_channel < required_samples) {
1773     // Must move data from the |sync_buffer_| in order to get 30 ms.
1774     borrowed_samples_per_channel =
1775         required_samples - decoded_length_per_channel;
1776     // Calculate how many of these were already played out.
1777     old_borrowed_samples_per_channel =
1778         (borrowed_samples_per_channel > sync_buffer_->FutureLength())
1779             ? (borrowed_samples_per_channel - sync_buffer_->FutureLength())
1780             : 0;
1781     memmove(&decoded_buffer[borrowed_samples_per_channel * num_channels],
1782             decoded_buffer, sizeof(int16_t) * decoded_length);
1783     sync_buffer_->ReadInterleavedFromEnd(borrowed_samples_per_channel,
1784                                          decoded_buffer);
1785     decoded_length = required_samples * num_channels;
1786   }
1787 
1788   size_t samples_added;
1789   PreemptiveExpand::ReturnCodes return_code = preemptive_expand_->Process(
1790       decoded_buffer, decoded_length, old_borrowed_samples_per_channel,
1791       algorithm_buffer_.get(), &samples_added);
1792   stats_->PreemptiveExpandedSamples(samples_added);
1793   switch (return_code) {
1794     case PreemptiveExpand::kSuccess:
1795       last_mode_ = Mode::kPreemptiveExpandSuccess;
1796       break;
1797     case PreemptiveExpand::kSuccessLowEnergy:
1798       last_mode_ = Mode::kPreemptiveExpandLowEnergy;
1799       break;
1800     case PreemptiveExpand::kNoStretch:
1801       last_mode_ = Mode::kPreemptiveExpandFail;
1802       break;
1803     case PreemptiveExpand::kError:
1804       // TODO(hlundin): Map to Modes::kError instead?
1805       last_mode_ = Mode::kPreemptiveExpandFail;
1806       return kPreemptiveExpandError;
1807   }
1808 
1809   if (borrowed_samples_per_channel > 0) {
1810     // Copy borrowed samples back to the |sync_buffer_|.
1811     sync_buffer_->ReplaceAtIndex(
1812         *algorithm_buffer_, borrowed_samples_per_channel,
1813         sync_buffer_->Size() - borrowed_samples_per_channel);
1814     algorithm_buffer_->PopFront(borrowed_samples_per_channel);
1815   }
1816 
1817   // If last packet was decoded as an inband CNG, set mode to CNG instead.
1818   if (speech_type == AudioDecoder::kComfortNoise) {
1819     last_mode_ = Mode::kCodecInternalCng;
1820   }
1821   if (!play_dtmf) {
1822     dtmf_tone_generator_->Reset();
1823   }
1824   expand_->Reset();
1825   return 0;
1826 }
1827 
DoRfc3389Cng(PacketList * packet_list,bool play_dtmf)1828 int NetEqImpl::DoRfc3389Cng(PacketList* packet_list, bool play_dtmf) {
1829   if (!packet_list->empty()) {
1830     // Must have exactly one SID frame at this point.
1831     assert(packet_list->size() == 1);
1832     const Packet& packet = packet_list->front();
1833     if (!decoder_database_->IsComfortNoise(packet.payload_type)) {
1834       RTC_LOG(LS_ERROR) << "Trying to decode non-CNG payload as CNG.";
1835       return kOtherError;
1836     }
1837     if (comfort_noise_->UpdateParameters(packet) ==
1838         ComfortNoise::kInternalError) {
1839       algorithm_buffer_->Zeros(output_size_samples_);
1840       return -comfort_noise_->internal_error_code();
1841     }
1842   }
1843   int cn_return =
1844       comfort_noise_->Generate(output_size_samples_, algorithm_buffer_.get());
1845   expand_->Reset();
1846   last_mode_ = Mode::kRfc3389Cng;
1847   if (!play_dtmf) {
1848     dtmf_tone_generator_->Reset();
1849   }
1850   if (cn_return == ComfortNoise::kInternalError) {
1851     RTC_LOG(LS_WARNING) << "Comfort noise generator returned error code: "
1852                         << comfort_noise_->internal_error_code();
1853     return kComfortNoiseErrorCode;
1854   } else if (cn_return == ComfortNoise::kUnknownPayloadType) {
1855     return kUnknownRtpPayloadType;
1856   }
1857   return 0;
1858 }
1859 
DoCodecInternalCng(const int16_t * decoded_buffer,size_t decoded_length)1860 void NetEqImpl::DoCodecInternalCng(const int16_t* decoded_buffer,
1861                                    size_t decoded_length) {
1862   RTC_DCHECK(normal_.get());
1863   normal_->Process(decoded_buffer, decoded_length, last_mode_,
1864                    algorithm_buffer_.get());
1865   last_mode_ = Mode::kCodecInternalCng;
1866   expand_->Reset();
1867 }
1868 
DoDtmf(const DtmfEvent & dtmf_event,bool * play_dtmf)1869 int NetEqImpl::DoDtmf(const DtmfEvent& dtmf_event, bool* play_dtmf) {
1870   // This block of the code and the block further down, handling |dtmf_switch|
1871   // are commented out. Otherwise playing out-of-band DTMF would fail in VoE
1872   // test, DtmfTest.ManualSuccessfullySendsOutOfBandTelephoneEvents. This is
1873   // equivalent to |dtmf_switch| always be false.
1874   //
1875   // See http://webrtc-codereview.appspot.com/1195004/ for discussion
1876   // On this issue. This change might cause some glitches at the point of
1877   // switch from audio to DTMF. Issue 1545 is filed to track this.
1878   //
1879   //  bool dtmf_switch = false;
1880   //  if ((last_mode_ != Modes::kDtmf) &&
1881   //      dtmf_tone_generator_->initialized()) {
1882   //    // Special case; see below.
1883   //    // We must catch this before calling Generate, since |initialized| is
1884   //    // modified in that call.
1885   //    dtmf_switch = true;
1886   //  }
1887 
1888   int dtmf_return_value = 0;
1889   if (!dtmf_tone_generator_->initialized()) {
1890     // Initialize if not already done.
1891     dtmf_return_value = dtmf_tone_generator_->Init(fs_hz_, dtmf_event.event_no,
1892                                                    dtmf_event.volume);
1893   }
1894 
1895   if (dtmf_return_value == 0) {
1896     // Generate DTMF signal.
1897     dtmf_return_value = dtmf_tone_generator_->Generate(output_size_samples_,
1898                                                        algorithm_buffer_.get());
1899   }
1900 
1901   if (dtmf_return_value < 0) {
1902     algorithm_buffer_->Zeros(output_size_samples_);
1903     return dtmf_return_value;
1904   }
1905 
1906   //  if (dtmf_switch) {
1907   //    // This is the special case where the previous operation was DTMF
1908   //    // overdub, but the current instruction is "regular" DTMF. We must make
1909   //    // sure that the DTMF does not have any discontinuities. The first DTMF
1910   //    // sample that we generate now must be played out immediately, therefore
1911   //    // it must be copied to the speech buffer.
1912   //    // TODO(hlundin): This code seems incorrect. (Legacy.) Write test and
1913   //    // verify correct operation.
1914   //    assert(false);
1915   //    // Must generate enough data to replace all of the |sync_buffer_|
1916   //    // "future".
1917   //    int required_length = sync_buffer_->FutureLength();
1918   //    assert(dtmf_tone_generator_->initialized());
1919   //    dtmf_return_value = dtmf_tone_generator_->Generate(required_length,
1920   //                                                       algorithm_buffer_);
1921   //    assert((size_t) required_length == algorithm_buffer_->Size());
1922   //    if (dtmf_return_value < 0) {
1923   //      algorithm_buffer_->Zeros(output_size_samples_);
1924   //      return dtmf_return_value;
1925   //    }
1926   //
1927   //    // Overwrite the "future" part of the speech buffer with the new DTMF
1928   //    // data.
1929   //    // TODO(hlundin): It seems that this overwriting has gone lost.
1930   //    // Not adapted for multi-channel yet.
1931   //    assert(algorithm_buffer_->Channels() == 1);
1932   //    if (algorithm_buffer_->Channels() != 1) {
1933   //      RTC_LOG(LS_WARNING) << "DTMF not supported for more than one channel";
1934   //      return kStereoNotSupported;
1935   //    }
1936   //    // Shuffle the remaining data to the beginning of algorithm buffer.
1937   //    algorithm_buffer_->PopFront(sync_buffer_->FutureLength());
1938   //  }
1939 
1940   sync_buffer_->IncreaseEndTimestamp(
1941       static_cast<uint32_t>(output_size_samples_));
1942   expand_->Reset();
1943   last_mode_ = Mode::kDtmf;
1944 
1945   // Set to false because the DTMF is already in the algorithm buffer.
1946   *play_dtmf = false;
1947   return 0;
1948 }
1949 
DtmfOverdub(const DtmfEvent & dtmf_event,size_t num_channels,int16_t * output) const1950 int NetEqImpl::DtmfOverdub(const DtmfEvent& dtmf_event,
1951                            size_t num_channels,
1952                            int16_t* output) const {
1953   size_t out_index = 0;
1954   size_t overdub_length = output_size_samples_;  // Default value.
1955 
1956   if (sync_buffer_->dtmf_index() > sync_buffer_->next_index()) {
1957     // Special operation for transition from "DTMF only" to "DTMF overdub".
1958     out_index =
1959         std::min(sync_buffer_->dtmf_index() - sync_buffer_->next_index(),
1960                  output_size_samples_);
1961     overdub_length = output_size_samples_ - out_index;
1962   }
1963 
1964   AudioMultiVector dtmf_output(num_channels);
1965   int dtmf_return_value = 0;
1966   if (!dtmf_tone_generator_->initialized()) {
1967     dtmf_return_value = dtmf_tone_generator_->Init(fs_hz_, dtmf_event.event_no,
1968                                                    dtmf_event.volume);
1969   }
1970   if (dtmf_return_value == 0) {
1971     dtmf_return_value =
1972         dtmf_tone_generator_->Generate(overdub_length, &dtmf_output);
1973     assert(overdub_length == dtmf_output.Size());
1974   }
1975   dtmf_output.ReadInterleaved(overdub_length, &output[out_index]);
1976   return dtmf_return_value < 0 ? dtmf_return_value : 0;
1977 }
1978 
ExtractPackets(size_t required_samples,PacketList * packet_list)1979 int NetEqImpl::ExtractPackets(size_t required_samples,
1980                               PacketList* packet_list) {
1981   bool first_packet = true;
1982   uint8_t prev_payload_type = 0;
1983   uint32_t prev_timestamp = 0;
1984   uint16_t prev_sequence_number = 0;
1985   bool next_packet_available = false;
1986 
1987   const Packet* next_packet = packet_buffer_->PeekNextPacket();
1988   RTC_DCHECK(next_packet);
1989   if (!next_packet) {
1990     RTC_LOG(LS_ERROR) << "Packet buffer unexpectedly empty.";
1991     return -1;
1992   }
1993   uint32_t first_timestamp = next_packet->timestamp;
1994   size_t extracted_samples = 0;
1995 
1996   // Packet extraction loop.
1997   do {
1998     timestamp_ = next_packet->timestamp;
1999     absl::optional<Packet> packet = packet_buffer_->GetNextPacket();
2000     // |next_packet| may be invalid after the |packet_buffer_| operation.
2001     next_packet = nullptr;
2002     if (!packet) {
2003       RTC_LOG(LS_ERROR) << "Should always be able to extract a packet here";
2004       assert(false);  // Should always be able to extract a packet here.
2005       return -1;
2006     }
2007     const uint64_t waiting_time_ms = packet->waiting_time->ElapsedMs();
2008     stats_->StoreWaitingTime(waiting_time_ms);
2009     RTC_DCHECK(!packet->empty());
2010 
2011     if (first_packet) {
2012       first_packet = false;
2013       if (nack_enabled_) {
2014         RTC_DCHECK(nack_);
2015         // TODO(henrik.lundin): Should we update this for all decoded packets?
2016         nack_->UpdateLastDecodedPacket(packet->sequence_number,
2017                                        packet->timestamp);
2018       }
2019       prev_sequence_number = packet->sequence_number;
2020       prev_timestamp = packet->timestamp;
2021       prev_payload_type = packet->payload_type;
2022     }
2023 
2024     const bool has_cng_packet =
2025         decoder_database_->IsComfortNoise(packet->payload_type);
2026     // Store number of extracted samples.
2027     size_t packet_duration = 0;
2028     if (packet->frame) {
2029       packet_duration = packet->frame->Duration();
2030       // TODO(ossu): Is this the correct way to track Opus FEC packets?
2031       if (packet->priority.codec_level > 0) {
2032         stats_->SecondaryDecodedSamples(
2033             rtc::dchecked_cast<int>(packet_duration));
2034       }
2035     } else if (!has_cng_packet) {
2036       RTC_LOG(LS_WARNING) << "Unknown payload type "
2037                           << static_cast<int>(packet->payload_type);
2038       RTC_NOTREACHED();
2039     }
2040 
2041     if (packet_duration == 0) {
2042       // Decoder did not return a packet duration. Assume that the packet
2043       // contains the same number of samples as the previous one.
2044       packet_duration = decoder_frame_length_;
2045     }
2046     extracted_samples = packet->timestamp - first_timestamp + packet_duration;
2047 
2048     RTC_DCHECK(controller_);
2049     stats_->JitterBufferDelay(
2050         packet_duration, waiting_time_ms + output_delay_chain_ms_,
2051         controller_->TargetLevelMs() + output_delay_chain_ms_);
2052 
2053     packet_list->push_back(std::move(*packet));  // Store packet in list.
2054     packet = absl::nullopt;  // Ensure it's never used after the move.
2055 
2056     // Check what packet is available next.
2057     next_packet = packet_buffer_->PeekNextPacket();
2058     next_packet_available = false;
2059     if (next_packet && prev_payload_type == next_packet->payload_type &&
2060         !has_cng_packet) {
2061       int16_t seq_no_diff = next_packet->sequence_number - prev_sequence_number;
2062       size_t ts_diff = next_packet->timestamp - prev_timestamp;
2063       if ((seq_no_diff == 1 || seq_no_diff == 0) &&
2064           ts_diff <= packet_duration) {
2065         // The next sequence number is available, or the next part of a packet
2066         // that was split into pieces upon insertion.
2067         next_packet_available = true;
2068       }
2069       prev_sequence_number = next_packet->sequence_number;
2070       prev_timestamp = next_packet->timestamp;
2071     }
2072   } while (extracted_samples < required_samples && next_packet_available);
2073 
2074   if (extracted_samples > 0) {
2075     // Delete old packets only when we are going to decode something. Otherwise,
2076     // we could end up in the situation where we never decode anything, since
2077     // all incoming packets are considered too old but the buffer will also
2078     // never be flooded and flushed.
2079     packet_buffer_->DiscardAllOldPackets(timestamp_, stats_.get());
2080   }
2081 
2082   return rtc::dchecked_cast<int>(extracted_samples);
2083 }
2084 
UpdatePlcComponents(int fs_hz,size_t channels)2085 void NetEqImpl::UpdatePlcComponents(int fs_hz, size_t channels) {
2086   // Delete objects and create new ones.
2087   expand_.reset(expand_factory_->Create(background_noise_.get(),
2088                                         sync_buffer_.get(), &random_vector_,
2089                                         stats_.get(), fs_hz, channels));
2090   merge_.reset(new Merge(fs_hz, channels, expand_.get(), sync_buffer_.get()));
2091 }
2092 
SetSampleRateAndChannels(int fs_hz,size_t channels)2093 void NetEqImpl::SetSampleRateAndChannels(int fs_hz, size_t channels) {
2094   RTC_LOG(LS_VERBOSE) << "SetSampleRateAndChannels " << fs_hz << " "
2095                       << channels;
2096   // TODO(hlundin): Change to an enumerator and skip assert.
2097   assert(fs_hz == 8000 || fs_hz == 16000 || fs_hz == 32000 || fs_hz == 48000);
2098   assert(channels > 0);
2099 
2100   // Before changing the sample rate, end and report any ongoing expand event.
2101   stats_->EndExpandEvent(fs_hz_);
2102   fs_hz_ = fs_hz;
2103   fs_mult_ = fs_hz / 8000;
2104   output_size_samples_ = static_cast<size_t>(kOutputSizeMs * 8 * fs_mult_);
2105   decoder_frame_length_ = 3 * output_size_samples_;  // Initialize to 30ms.
2106 
2107   last_mode_ = Mode::kNormal;
2108 
2109   ComfortNoiseDecoder* cng_decoder = decoder_database_->GetActiveCngDecoder();
2110   if (cng_decoder)
2111     cng_decoder->Reset();
2112 
2113   // Reinit post-decode VAD with new sample rate.
2114   assert(vad_.get());  // Cannot be NULL here.
2115   vad_->Init();
2116 
2117   // Delete algorithm buffer and create a new one.
2118   algorithm_buffer_.reset(new AudioMultiVector(channels));
2119 
2120   // Delete sync buffer and create a new one.
2121   sync_buffer_.reset(new SyncBuffer(channels, kSyncBufferSize * fs_mult_));
2122 
2123   // Delete BackgroundNoise object and create a new one.
2124   background_noise_.reset(new BackgroundNoise(channels));
2125 
2126   // Reset random vector.
2127   random_vector_.Reset();
2128 
2129   UpdatePlcComponents(fs_hz, channels);
2130 
2131   // Move index so that we create a small set of future samples (all 0).
2132   sync_buffer_->set_next_index(sync_buffer_->next_index() -
2133                                expand_->overlap_length());
2134 
2135   normal_.reset(new Normal(fs_hz, decoder_database_.get(), *background_noise_,
2136                            expand_.get()));
2137   accelerate_.reset(
2138       accelerate_factory_->Create(fs_hz, channels, *background_noise_));
2139   preemptive_expand_.reset(preemptive_expand_factory_->Create(
2140       fs_hz, channels, *background_noise_, expand_->overlap_length()));
2141 
2142   // Delete ComfortNoise object and create a new one.
2143   comfort_noise_.reset(
2144       new ComfortNoise(fs_hz, decoder_database_.get(), sync_buffer_.get()));
2145 
2146   // Verify that |decoded_buffer_| is long enough.
2147   if (decoded_buffer_length_ < kMaxFrameSize * channels) {
2148     // Reallocate to larger size.
2149     decoded_buffer_length_ = kMaxFrameSize * channels;
2150     decoded_buffer_.reset(new int16_t[decoded_buffer_length_]);
2151   }
2152   RTC_CHECK(controller_) << "Unexpectedly found no NetEqController";
2153   controller_->SetSampleRate(fs_hz_, output_size_samples_);
2154 }
2155 
LastOutputType()2156 NetEqImpl::OutputType NetEqImpl::LastOutputType() {
2157   assert(vad_.get());
2158   assert(expand_.get());
2159   if (last_mode_ == Mode::kCodecInternalCng ||
2160       last_mode_ == Mode::kRfc3389Cng) {
2161     return OutputType::kCNG;
2162   } else if (last_mode_ == Mode::kExpand && expand_->MuteFactor(0) == 0) {
2163     // Expand mode has faded down to background noise only (very long expand).
2164     return OutputType::kPLCCNG;
2165   } else if (last_mode_ == Mode::kExpand) {
2166     return OutputType::kPLC;
2167   } else if (vad_->running() && !vad_->active_speech()) {
2168     return OutputType::kVadPassive;
2169   } else if (last_mode_ == Mode::kCodecPlc) {
2170     return OutputType::kCodecPLC;
2171   } else {
2172     return OutputType::kNormalSpeech;
2173   }
2174 }
2175 }  // namespace webrtc
2176