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