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