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