| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 #include "webrtc/modules/audio_coding/neteq/neteq_impl.h" | 11 #include "webrtc/modules/audio_coding/neteq/neteq_impl.h" |
| 12 | 12 |
| 13 #include <assert.h> | 13 #include <assert.h> |
| 14 #include <memory.h> // memset | 14 #include <memory.h> // memset |
| 15 | 15 |
| 16 #include <algorithm> | 16 #include <algorithm> |
| 17 #include <vector> |
| 17 | 18 |
| 18 #include "webrtc/base/checks.h" | 19 #include "webrtc/base/checks.h" |
| 19 #include "webrtc/base/logging.h" | 20 #include "webrtc/base/logging.h" |
| 20 #include "webrtc/base/safe_conversions.h" | 21 #include "webrtc/base/safe_conversions.h" |
| 21 #include "webrtc/base/trace_event.h" | 22 #include "webrtc/base/trace_event.h" |
| 22 #include "webrtc/common_audio/signal_processing/include/signal_processing_librar
y.h" | 23 #include "webrtc/common_audio/signal_processing/include/signal_processing_librar
y.h" |
| 23 #include "webrtc/modules/audio_coding/codecs/audio_decoder.h" | 24 #include "webrtc/modules/audio_coding/codecs/audio_decoder.h" |
| 24 #include "webrtc/modules/audio_coding/neteq/accelerate.h" | 25 #include "webrtc/modules/audio_coding/neteq/accelerate.h" |
| 25 #include "webrtc/modules/audio_coding/neteq/background_noise.h" | 26 #include "webrtc/modules/audio_coding/neteq/background_noise.h" |
| 26 #include "webrtc/modules/audio_coding/neteq/buffer_level_filter.h" | 27 #include "webrtc/modules/audio_coding/neteq/buffer_level_filter.h" |
| (...skipping 630 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 657 switch (ret) { | 658 switch (ret) { |
| 658 case PayloadSplitter::kUnknownPayloadType: | 659 case PayloadSplitter::kUnknownPayloadType: |
| 659 return kUnknownRtpPayloadType; | 660 return kUnknownRtpPayloadType; |
| 660 case PayloadSplitter::kFrameSplitError: | 661 case PayloadSplitter::kFrameSplitError: |
| 661 return kFrameSplitError; | 662 return kFrameSplitError; |
| 662 default: | 663 default: |
| 663 return kOtherError; | 664 return kOtherError; |
| 664 } | 665 } |
| 665 } | 666 } |
| 666 | 667 |
| 667 // Update bandwidth estimate, if the packet is not sync-packet. | 668 // Update bandwidth estimate, if the packet is not sync-packet nor comfort |
| 668 if (!packet_list.empty() && !packet_list.front()->sync_packet) { | 669 // noise. |
| 670 if (!packet_list.empty() && !packet_list.front()->sync_packet && |
| 671 !decoder_database_->IsComfortNoise(main_header.payloadType)) { |
| 669 // The list can be empty here if we got nothing but DTMF payloads. | 672 // The list can be empty here if we got nothing but DTMF payloads. |
| 670 AudioDecoder* decoder = | 673 AudioDecoder* decoder = |
| 671 decoder_database_->GetDecoder(main_header.payloadType); | 674 decoder_database_->GetDecoder(main_header.payloadType); |
| 672 assert(decoder); // Should always get a valid object, since we have | 675 assert(decoder); // Should always get a valid object, since we have |
| 673 // already checked that the payload types are known. | 676 // already checked that the payload types are known. |
| 674 decoder->IncomingPacket(packet_list.front()->payload, | 677 decoder->IncomingPacket(packet_list.front()->payload, |
| 675 packet_list.front()->payload_length, | 678 packet_list.front()->payload_length, |
| 676 packet_list.front()->header.sequenceNumber, | 679 packet_list.front()->header.sequenceNumber, |
| 677 packet_list.front()->header.timestamp, | 680 packet_list.front()->header.timestamp, |
| 678 receive_timestamp); | 681 receive_timestamp); |
| 679 } | 682 } |
| 680 | 683 |
| 681 if (nack_enabled_) { | 684 if (nack_enabled_) { |
| 682 RTC_DCHECK(nack_); | 685 RTC_DCHECK(nack_); |
| 683 if (update_sample_rate_and_channels) { | 686 if (update_sample_rate_and_channels) { |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 721 if (update_sample_rate_and_channels && !packet_buffer_->Empty()) { | 724 if (update_sample_rate_and_channels && !packet_buffer_->Empty()) { |
| 722 // We do not use |current_rtp_payload_type_| to |set payload_type|, but | 725 // We do not use |current_rtp_payload_type_| to |set payload_type|, but |
| 723 // get the next RTP header from |packet_buffer_| to obtain the payload type. | 726 // get the next RTP header from |packet_buffer_| to obtain the payload type. |
| 724 // The reason for it is the following corner case. If NetEq receives a | 727 // The reason for it is the following corner case. If NetEq receives a |
| 725 // CNG packet with a sample rate different than the current CNG then it | 728 // CNG packet with a sample rate different than the current CNG then it |
| 726 // flushes its buffer, assuming send codec must have been changed. However, | 729 // flushes its buffer, assuming send codec must have been changed. However, |
| 727 // payload type of the hypothetically new send codec is not known. | 730 // payload type of the hypothetically new send codec is not known. |
| 728 const RTPHeader* rtp_header = packet_buffer_->NextRtpHeader(); | 731 const RTPHeader* rtp_header = packet_buffer_->NextRtpHeader(); |
| 729 assert(rtp_header); | 732 assert(rtp_header); |
| 730 int payload_type = rtp_header->payloadType; | 733 int payload_type = rtp_header->payloadType; |
| 731 AudioDecoder* decoder = decoder_database_->GetDecoder(payload_type); | 734 size_t channels = 1; |
| 732 assert(decoder); // Payloads are already checked to be valid. | 735 if (!decoder_database_->IsComfortNoise(payload_type)) { |
| 736 AudioDecoder* decoder = decoder_database_->GetDecoder(payload_type); |
| 737 assert(decoder); // Payloads are already checked to be valid. |
| 738 channels = decoder->Channels(); |
| 739 } |
| 733 const DecoderDatabase::DecoderInfo* decoder_info = | 740 const DecoderDatabase::DecoderInfo* decoder_info = |
| 734 decoder_database_->GetDecoderInfo(payload_type); | 741 decoder_database_->GetDecoderInfo(payload_type); |
| 735 assert(decoder_info); | 742 assert(decoder_info); |
| 736 if (decoder_info->fs_hz != fs_hz_ || | 743 if (decoder_info->fs_hz != fs_hz_ || |
| 737 decoder->Channels() != algorithm_buffer_->Channels()) { | 744 channels != algorithm_buffer_->Channels()) { |
| 738 SetSampleRateAndChannels(decoder_info->fs_hz, decoder->Channels()); | 745 SetSampleRateAndChannels(decoder_info->fs_hz, channels); |
| 739 } | 746 } |
| 740 if (nack_enabled_) { | 747 if (nack_enabled_) { |
| 741 RTC_DCHECK(nack_); | 748 RTC_DCHECK(nack_); |
| 742 // Update the sample rate even if the rate is not new, because of Reset(). | 749 // Update the sample rate even if the rate is not new, because of Reset(). |
| 743 nack_->UpdateSampleRate(fs_hz_); | 750 nack_->UpdateSampleRate(fs_hz_); |
| 744 } | 751 } |
| 745 } | 752 } |
| 746 | 753 |
| 747 // TODO(hlundin): Move this code to DelayManager class. | 754 // TODO(hlundin): Move this code to DelayManager class. |
| 748 const DecoderDatabase::DecoderInfo* dec_info = | 755 const DecoderDatabase::DecoderInfo* dec_info = |
| (...skipping 541 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1290 } | 1297 } |
| 1291 } | 1298 } |
| 1292 } | 1299 } |
| 1293 | 1300 |
| 1294 if (reset_decoder_) { | 1301 if (reset_decoder_) { |
| 1295 // TODO(hlundin): Write test for this. | 1302 // TODO(hlundin): Write test for this. |
| 1296 if (decoder) | 1303 if (decoder) |
| 1297 decoder->Reset(); | 1304 decoder->Reset(); |
| 1298 | 1305 |
| 1299 // Reset comfort noise decoder. | 1306 // Reset comfort noise decoder. |
| 1300 AudioDecoder* cng_decoder = decoder_database_->GetActiveCngDecoder(); | 1307 ComfortNoiseDecoder* cng_decoder = decoder_database_->GetActiveCngDecoder(); |
| 1301 if (cng_decoder) | 1308 if (cng_decoder) |
| 1302 cng_decoder->Reset(); | 1309 cng_decoder->Reset(); |
| 1303 | 1310 |
| 1304 reset_decoder_ = false; | 1311 reset_decoder_ = false; |
| 1305 } | 1312 } |
| 1306 | 1313 |
| 1307 #ifdef LEGACY_BITEXACT | 1314 #ifdef LEGACY_BITEXACT |
| 1308 // Due to a bug in old SignalMCU, it could happen that CNG operation was | 1315 // Due to a bug in old SignalMCU, it could happen that CNG operation was |
| 1309 // decided, but a speech packet was provided. The speech packet will be used | 1316 // decided, but a speech packet was provided. The speech packet will be used |
| 1310 // to update the comfort noise decoder, as if it was a SID frame, which is | 1317 // to update the comfort noise decoder, as if it was a SID frame, which is |
| (...skipping 637 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1948 } else { | 1955 } else { |
| 1949 if (packet->primary) { | 1956 if (packet->primary) { |
| 1950 packet_duration = decoder->PacketDuration(packet->payload, | 1957 packet_duration = decoder->PacketDuration(packet->payload, |
| 1951 packet->payload_length); | 1958 packet->payload_length); |
| 1952 } else { | 1959 } else { |
| 1953 packet_duration = decoder-> | 1960 packet_duration = decoder-> |
| 1954 PacketDurationRedundant(packet->payload, packet->payload_length); | 1961 PacketDurationRedundant(packet->payload, packet->payload_length); |
| 1955 stats_.SecondaryDecodedSamples(packet_duration); | 1962 stats_.SecondaryDecodedSamples(packet_duration); |
| 1956 } | 1963 } |
| 1957 } | 1964 } |
| 1958 } else { | 1965 } else if (!decoder_database_->IsComfortNoise(packet->header.payloadType)) { |
| 1959 LOG(LS_WARNING) << "Unknown payload type " | 1966 LOG(LS_WARNING) << "Unknown payload type " |
| 1960 << static_cast<int>(packet->header.payloadType); | 1967 << static_cast<int>(packet->header.payloadType); |
| 1961 assert(false); | 1968 assert(false); |
| 1962 } | 1969 } |
| 1963 if (packet_duration <= 0) { | 1970 if (packet_duration <= 0) { |
| 1964 // Decoder did not return a packet duration. Assume that the packet | 1971 // Decoder did not return a packet duration. Assume that the packet |
| 1965 // contains the same number of samples as the previous one. | 1972 // contains the same number of samples as the previous one. |
| 1966 packet_duration = rtc::checked_cast<int>(decoder_frame_length_); | 1973 packet_duration = rtc::checked_cast<int>(decoder_frame_length_); |
| 1967 } | 1974 } |
| 1968 extracted_samples = packet->header.timestamp - first_timestamp + | 1975 extracted_samples = packet->header.timestamp - first_timestamp + |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2016 decoder_frame_length_ = 3 * output_size_samples_; // Initialize to 30ms. | 2023 decoder_frame_length_ = 3 * output_size_samples_; // Initialize to 30ms. |
| 2017 | 2024 |
| 2018 last_mode_ = kModeNormal; | 2025 last_mode_ = kModeNormal; |
| 2019 | 2026 |
| 2020 // Create a new array of mute factors and set all to 1. | 2027 // Create a new array of mute factors and set all to 1. |
| 2021 mute_factor_array_.reset(new int16_t[channels]); | 2028 mute_factor_array_.reset(new int16_t[channels]); |
| 2022 for (size_t i = 0; i < channels; ++i) { | 2029 for (size_t i = 0; i < channels; ++i) { |
| 2023 mute_factor_array_[i] = 16384; // 1.0 in Q14. | 2030 mute_factor_array_[i] = 16384; // 1.0 in Q14. |
| 2024 } | 2031 } |
| 2025 | 2032 |
| 2026 AudioDecoder* cng_decoder = decoder_database_->GetActiveCngDecoder(); | 2033 ComfortNoiseDecoder* cng_decoder = decoder_database_->GetActiveCngDecoder(); |
| 2027 if (cng_decoder) | 2034 if (cng_decoder) |
| 2028 cng_decoder->Reset(); | 2035 cng_decoder->Reset(); |
| 2029 | 2036 |
| 2030 // Reinit post-decode VAD with new sample rate. | 2037 // Reinit post-decode VAD with new sample rate. |
| 2031 assert(vad_.get()); // Cannot be NULL here. | 2038 assert(vad_.get()); // Cannot be NULL here. |
| 2032 vad_->Init(); | 2039 vad_->Init(); |
| 2033 | 2040 |
| 2034 // Delete algorithm buffer and create a new one. | 2041 // Delete algorithm buffer and create a new one. |
| 2035 algorithm_buffer_.reset(new AudioMultiVector(channels)); | 2042 algorithm_buffer_.reset(new AudioMultiVector(channels)); |
| 2036 | 2043 |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2095 | 2102 |
| 2096 void NetEqImpl::CreateDecisionLogic() { | 2103 void NetEqImpl::CreateDecisionLogic() { |
| 2097 decision_logic_.reset(DecisionLogic::Create(fs_hz_, output_size_samples_, | 2104 decision_logic_.reset(DecisionLogic::Create(fs_hz_, output_size_samples_, |
| 2098 playout_mode_, | 2105 playout_mode_, |
| 2099 decoder_database_.get(), | 2106 decoder_database_.get(), |
| 2100 *packet_buffer_.get(), | 2107 *packet_buffer_.get(), |
| 2101 delay_manager_.get(), | 2108 delay_manager_.get(), |
| 2102 buffer_level_filter_.get())); | 2109 buffer_level_filter_.get())); |
| 2103 } | 2110 } |
| 2104 } // namespace webrtc | 2111 } // namespace webrtc |
| OLD | NEW |