AudioDecoderIsacT: Require caller to always specify sample rate

This gets rid of the complex & icky state where the sample rate is not
yet determined.

BUG=webrtc:5801

Review-Url: https://codereview.webrtc.org/2020353003
Cr-Commit-Position: refs/heads/master@{#13011}
This commit is contained in:
kwiberg
2016-06-02 02:58:59 -07:00
committed by Commit bot
parent 52033d6ea1
commit abe95ba323
10 changed files with 24 additions and 52 deletions

View File

@ -710,7 +710,7 @@ int AudioCodingModuleImpl::RegisterReceiveCodec(const CodecInst& codec) {
rtc::CritScope lock(&acm_crit_sect_); rtc::CritScope lock(&acm_crit_sect_);
auto* ef = encoder_factory_.get(); auto* ef = encoder_factory_.get();
return RegisterReceiveCodecUnlocked( return RegisterReceiveCodecUnlocked(
codec, [ef] { return ef->rent_a_codec.RentIsacDecoder(); }); codec, [&] { return ef->rent_a_codec.RentIsacDecoder(codec.plfreq); });
} }
int AudioCodingModuleImpl::RegisterReceiveCodec( int AudioCodingModuleImpl::RegisterReceiveCodec(

View File

@ -279,11 +279,14 @@ std::unique_ptr<AudioEncoder> CreateCngEncoder(
} }
std::unique_ptr<AudioDecoder> CreateIsacDecoder( std::unique_ptr<AudioDecoder> CreateIsacDecoder(
int sample_rate_hz,
const rtc::scoped_refptr<LockedIsacBandwidthInfo>& bwinfo) { const rtc::scoped_refptr<LockedIsacBandwidthInfo>& bwinfo) {
#if defined(WEBRTC_CODEC_ISACFX) #if defined(WEBRTC_CODEC_ISACFX)
return std::unique_ptr<AudioDecoder>(new AudioDecoderIsacFix(bwinfo)); return std::unique_ptr<AudioDecoder>(
new AudioDecoderIsacFix(sample_rate_hz, bwinfo));
#elif defined(WEBRTC_CODEC_ISAC) #elif defined(WEBRTC_CODEC_ISAC)
return std::unique_ptr<AudioDecoder>(new AudioDecoderIsac(bwinfo)); return std::unique_ptr<AudioDecoder>(
new AudioDecoderIsac(sample_rate_hz, bwinfo));
#else #else
FATAL() << "iSAC is not supported."; FATAL() << "iSAC is not supported.";
return std::unique_ptr<AudioDecoder>(); return std::unique_ptr<AudioDecoder>();
@ -357,8 +360,8 @@ std::unique_ptr<AudioEncoder> RentACodec::RentEncoderStack(
return encoder_stack; return encoder_stack;
} }
std::unique_ptr<AudioDecoder> RentACodec::RentIsacDecoder() { std::unique_ptr<AudioDecoder> RentACodec::RentIsacDecoder(int sample_rate_hz) {
return CreateIsacDecoder(isac_bandwidth_info_); return CreateIsacDecoder(sample_rate_hz, isac_bandwidth_info_);
} }
} // namespace acm2 } // namespace acm2

View File

@ -220,7 +220,7 @@ class RentACodec {
std::unique_ptr<AudioEncoder> RentEncoderStack(StackParameters* param); std::unique_ptr<AudioEncoder> RentEncoderStack(StackParameters* param);
// Creates and returns an iSAC decoder. // Creates and returns an iSAC decoder.
std::unique_ptr<AudioDecoder> RentIsacDecoder(); std::unique_ptr<AudioDecoder> RentIsacDecoder(int sample_rate_hz);
private: private:
std::unique_ptr<AudioEncoder> speech_encoder_; std::unique_ptr<AudioEncoder> speech_encoder_;

View File

@ -26,9 +26,6 @@ namespace webrtc {
template <typename T> template <typename T>
class AudioDecoderIsacT final : public AudioDecoder { class AudioDecoderIsacT final : public AudioDecoder {
public: public:
AudioDecoderIsacT();
explicit AudioDecoderIsacT(
const rtc::scoped_refptr<LockedIsacBandwidthInfo>& bwinfo);
explicit AudioDecoderIsacT(int sample_rate_hz); explicit AudioDecoderIsacT(int sample_rate_hz);
AudioDecoderIsacT(int sample_rate_hz, AudioDecoderIsacT(int sample_rate_hz,
const rtc::scoped_refptr<LockedIsacBandwidthInfo>& bwinfo); const rtc::scoped_refptr<LockedIsacBandwidthInfo>& bwinfo);
@ -52,11 +49,8 @@ class AudioDecoderIsacT final : public AudioDecoder {
SpeechType* speech_type) override; SpeechType* speech_type) override;
private: private:
AudioDecoderIsacT(rtc::Optional<int> sample_rate_hz,
const rtc::scoped_refptr<LockedIsacBandwidthInfo>& bwinfo);
typename T::instance_type* isac_state_; typename T::instance_type* isac_state_;
rtc::Optional<int> sample_rate_hz_; int sample_rate_hz_;
rtc::scoped_refptr<LockedIsacBandwidthInfo> bwinfo_; rtc::scoped_refptr<LockedIsacBandwidthInfo> bwinfo_;
RTC_DISALLOW_COPY_AND_ASSIGN(AudioDecoderIsacT); RTC_DISALLOW_COPY_AND_ASSIGN(AudioDecoderIsacT);

View File

@ -17,33 +17,17 @@
namespace webrtc { namespace webrtc {
template <typename T>
AudioDecoderIsacT<T>::AudioDecoderIsacT()
: AudioDecoderIsacT(rtc::Optional<int>(), nullptr) {}
template <typename T>
AudioDecoderIsacT<T>::AudioDecoderIsacT(
const rtc::scoped_refptr<LockedIsacBandwidthInfo>& bwinfo)
: AudioDecoderIsacT(rtc::Optional<int>(), bwinfo) {}
template <typename T> template <typename T>
AudioDecoderIsacT<T>::AudioDecoderIsacT(int sample_rate_hz) AudioDecoderIsacT<T>::AudioDecoderIsacT(int sample_rate_hz)
: AudioDecoderIsacT(rtc::Optional<int>(sample_rate_hz), nullptr) {} : AudioDecoderIsacT(sample_rate_hz, nullptr) {}
template <typename T> template <typename T>
AudioDecoderIsacT<T>::AudioDecoderIsacT( AudioDecoderIsacT<T>::AudioDecoderIsacT(
int sample_rate_hz, int sample_rate_hz,
const rtc::scoped_refptr<LockedIsacBandwidthInfo>& bwinfo) const rtc::scoped_refptr<LockedIsacBandwidthInfo>& bwinfo)
: AudioDecoderIsacT(rtc::Optional<int>(sample_rate_hz), bwinfo) {}
template <typename T>
AudioDecoderIsacT<T>::AudioDecoderIsacT(
rtc::Optional<int> sample_rate_hz,
const rtc::scoped_refptr<LockedIsacBandwidthInfo>& bwinfo)
: sample_rate_hz_(sample_rate_hz), bwinfo_(bwinfo) { : sample_rate_hz_(sample_rate_hz), bwinfo_(bwinfo) {
RTC_CHECK(!sample_rate_hz || *sample_rate_hz == 16000 || RTC_CHECK(sample_rate_hz == 16000 || sample_rate_hz == 32000)
*sample_rate_hz == 32000) << "Unsupported sample rate " << sample_rate_hz;
<< "Unsupported sample rate " << *sample_rate_hz;
RTC_CHECK_EQ(0, T::Create(&isac_state_)); RTC_CHECK_EQ(0, T::Create(&isac_state_));
T::DecoderInit(isac_state_); T::DecoderInit(isac_state_);
if (bwinfo_) { if (bwinfo_) {
@ -51,9 +35,7 @@ AudioDecoderIsacT<T>::AudioDecoderIsacT(
T::GetBandwidthInfo(isac_state_, &bi); T::GetBandwidthInfo(isac_state_, &bi);
bwinfo_->Set(bi); bwinfo_->Set(bi);
} }
if (sample_rate_hz_) { RTC_CHECK_EQ(0, T::SetDecSampRate(isac_state_, sample_rate_hz_));
RTC_CHECK_EQ(0, T::SetDecSampRate(isac_state_, *sample_rate_hz_));
}
} }
template <typename T> template <typename T>
@ -67,14 +49,7 @@ int AudioDecoderIsacT<T>::DecodeInternal(const uint8_t* encoded,
int sample_rate_hz, int sample_rate_hz,
int16_t* decoded, int16_t* decoded,
SpeechType* speech_type) { SpeechType* speech_type) {
if (sample_rate_hz_) { RTC_CHECK_EQ(sample_rate_hz_, sample_rate_hz);
RTC_CHECK_EQ(*sample_rate_hz_, sample_rate_hz);
} else {
RTC_CHECK(sample_rate_hz == 16000 || sample_rate_hz == 32000)
<< "Unsupported sample rate " << sample_rate_hz;
sample_rate_hz_ = rtc::Optional<int>(sample_rate_hz);
RTC_CHECK_EQ(0, T::SetDecSampRate(isac_state_, *sample_rate_hz_));
}
int16_t temp_type = 1; // Default is speech. int16_t temp_type = 1; // Default is speech.
int ret = int ret =
T::DecodeInternal(isac_state_, encoded, encoded_len, decoded, &temp_type); T::DecodeInternal(isac_state_, encoded, encoded_len, decoded, &temp_type);
@ -121,8 +96,7 @@ int AudioDecoderIsacT<T>::ErrorCode() {
template <typename T> template <typename T>
int AudioDecoderIsacT<T>::SampleRateHz() const { int AudioDecoderIsacT<T>::SampleRateHz() const {
RTC_CHECK(sample_rate_hz_) << "Sample rate not set yet!"; return sample_rate_hz_;
return *sample_rate_hz_;
} }
template <typename T> template <typename T>

View File

@ -45,8 +45,9 @@ int32_t AudioCoder::SetEncodeCodec(const CodecInst& codec_inst) {
} }
int32_t AudioCoder::SetDecodeCodec(const CodecInst& codec_inst) { int32_t AudioCoder::SetDecodeCodec(const CodecInst& codec_inst) {
if (acm_->RegisterReceiveCodec( if (acm_->RegisterReceiveCodec(codec_inst, [&] {
codec_inst, [&] { return rent_a_codec_.RentIsacDecoder(); }) == -1) { return rent_a_codec_.RentIsacDecoder(codec_inst.plfreq);
}) == -1) {
return -1; return -1;
} }
memcpy(&receive_codec_, &codec_inst, sizeof(CodecInst)); memcpy(&receive_codec_, &codec_inst, sizeof(CodecInst));

View File

@ -13,10 +13,10 @@
namespace webrtc { namespace webrtc {
void FuzzOneInput(const uint8_t* data, size_t size) { void FuzzOneInput(const uint8_t* data, size_t size) {
AudioDecoderIsac dec(nullptr);
const int sample_rate_hz = size % 2 == 0 ? 16000 : 32000; // 16 or 32 kHz. const int sample_rate_hz = size % 2 == 0 ? 16000 : 32000; // 16 or 32 kHz.
static const size_t kAllocatedOuputSizeSamples = 32000 / 10; // 100 ms. static const size_t kAllocatedOuputSizeSamples = 32000 / 10; // 100 ms.
int16_t output[kAllocatedOuputSizeSamples]; int16_t output[kAllocatedOuputSizeSamples];
AudioDecoderIsac dec(sample_rate_hz);
FuzzAudioDecoder(DecoderFunctionType::kNormalDecode, data, size, &dec, FuzzAudioDecoder(DecoderFunctionType::kNormalDecode, data, size, &dec,
sample_rate_hz, sizeof(output), output); sample_rate_hz, sizeof(output), output);
} }

View File

@ -13,7 +13,7 @@
namespace webrtc { namespace webrtc {
void FuzzOneInput(const uint8_t* data, size_t size) { void FuzzOneInput(const uint8_t* data, size_t size) {
AudioDecoderIsac dec(nullptr); AudioDecoderIsac dec(16000);
FuzzAudioDecoderIncomingPacket(data, size, &dec); FuzzAudioDecoderIncomingPacket(data, size, &dec);
} }
} // namespace webrtc } // namespace webrtc

View File

@ -13,10 +13,10 @@
namespace webrtc { namespace webrtc {
void FuzzOneInput(const uint8_t* data, size_t size) { void FuzzOneInput(const uint8_t* data, size_t size) {
AudioDecoderIsacFix dec(nullptr);
static const int kSampleRateHz = 16000; static const int kSampleRateHz = 16000;
static const size_t kAllocatedOuputSizeSamples = 16000 / 10; // 100 ms. static const size_t kAllocatedOuputSizeSamples = 16000 / 10; // 100 ms.
int16_t output[kAllocatedOuputSizeSamples]; int16_t output[kAllocatedOuputSizeSamples];
AudioDecoderIsacFix dec(kSampleRateHz);
FuzzAudioDecoder(DecoderFunctionType::kNormalDecode, data, size, &dec, FuzzAudioDecoder(DecoderFunctionType::kNormalDecode, data, size, &dec,
kSampleRateHz, sizeof(output), output); kSampleRateHz, sizeof(output), output);
} }

View File

@ -49,8 +49,8 @@ namespace {
bool RegisterReceiveCodec(std::unique_ptr<AudioCodingModule>* acm, bool RegisterReceiveCodec(std::unique_ptr<AudioCodingModule>* acm,
acm2::RentACodec* rac, acm2::RentACodec* rac,
const CodecInst& ci) { const CodecInst& ci) {
const int result = const int result = (*acm)->RegisterReceiveCodec(
(*acm)->RegisterReceiveCodec(ci, [&] { return rac->RentIsacDecoder(); }); ci, [&] { return rac->RentIsacDecoder(ci.plfreq); });
return result == 0; return result == 0;
} }