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