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