diff --git a/webrtc/api/audio_codecs/g722/audio_decoder_g722.cc b/webrtc/api/audio_codecs/g722/audio_decoder_g722.cc index e4cb1b7d69..346a7f2779 100644 --- a/webrtc/api/audio_codecs/g722/audio_decoder_g722.cc +++ b/webrtc/api/audio_codecs/g722/audio_decoder_g722.cc @@ -23,8 +23,10 @@ namespace webrtc { rtc::Optional AudioDecoderG722::SdpToConfig( const SdpAudioFormat& format) { return STR_CASE_CMP(format.name.c_str(), "g722") == 0 && - format.clockrate_hz == 8000 - ? rtc::Optional(Config()) + format.clockrate_hz == 8000 && + (format.num_channels == 1 || format.num_channels == 2) + ? rtc::Optional( + Config{rtc::dchecked_cast(format.num_channels)}) : rtc::Optional(); } @@ -35,7 +37,14 @@ void AudioDecoderG722::AppendSupportedDecoders( std::unique_ptr AudioDecoderG722::MakeAudioDecoder( Config config) { - return rtc::MakeUnique(); + switch (config.num_channels) { + case 1: + return rtc::MakeUnique(); + case 2: + return rtc::MakeUnique(); + default: + return nullptr; + } } } // namespace webrtc diff --git a/webrtc/api/audio_codecs/g722/audio_decoder_g722.h b/webrtc/api/audio_codecs/g722/audio_decoder_g722.h index 32a46d7348..ef536628bb 100644 --- a/webrtc/api/audio_codecs/g722/audio_decoder_g722.h +++ b/webrtc/api/audio_codecs/g722/audio_decoder_g722.h @@ -25,7 +25,10 @@ namespace webrtc { // // NOTE: This struct is still under development and may change without notice. struct AudioDecoderG722 { - struct Config {}; // Empty---no config values needed! + struct Config { + bool IsOk() const { return num_channels == 1 || num_channels == 2; } + int num_channels; + }; static rtc::Optional SdpToConfig(const SdpAudioFormat& audio_format); static void AppendSupportedDecoders(std::vector* specs); static std::unique_ptr MakeAudioDecoder(Config config); diff --git a/webrtc/api/audio_codecs/test/audio_decoder_factory_template_unittest.cc b/webrtc/api/audio_codecs/test/audio_decoder_factory_template_unittest.cc index 21fbc7c616..c05fcd6e2f 100644 --- a/webrtc/api/audio_codecs/test/audio_decoder_factory_template_unittest.cc +++ b/webrtc/api/audio_codecs/test/audio_decoder_factory_template_unittest.cc @@ -119,9 +119,16 @@ TEST(AudioDecoderFactoryTemplateTest, G722) { EXPECT_FALSE(factory->IsSupportedDecoder({"foo", 8000, 1})); EXPECT_TRUE(factory->IsSupportedDecoder({"g722", 8000, 1})); EXPECT_EQ(nullptr, factory->MakeAudioDecoder({"bar", 16000, 1})); - auto dec = factory->MakeAudioDecoder({"g722", 8000, 1}); - ASSERT_NE(nullptr, dec); - EXPECT_EQ(16000, dec->SampleRateHz()); + auto dec1 = factory->MakeAudioDecoder({"g722", 8000, 1}); + ASSERT_NE(nullptr, dec1); + EXPECT_EQ(16000, dec1->SampleRateHz()); + EXPECT_EQ(1u, dec1->Channels()); + auto dec2 = factory->MakeAudioDecoder({"g722", 8000, 2}); + ASSERT_NE(nullptr, dec2); + EXPECT_EQ(16000, dec2->SampleRateHz()); + EXPECT_EQ(2u, dec2->Channels()); + auto dec3 = factory->MakeAudioDecoder({"g722", 8000, 3}); + ASSERT_EQ(nullptr, dec3); } } // namespace webrtc diff --git a/webrtc/modules/audio_coding/codecs/builtin_audio_decoder_factory_internal.cc b/webrtc/modules/audio_coding/codecs/builtin_audio_decoder_factory_internal.cc index cbf735ee2e..332ad06e26 100644 --- a/webrtc/modules/audio_coding/codecs/builtin_audio_decoder_factory_internal.cc +++ b/webrtc/modules/audio_coding/codecs/builtin_audio_decoder_factory_internal.cc @@ -135,7 +135,7 @@ NamedDecoderConstructor decoder_constructors[] = { return true; } else if (format.num_channels == 2) { if (out) { - out->reset(new AudioDecoderG722Stereo); + out->reset(new AudioDecoderG722StereoImpl); } return true; } diff --git a/webrtc/modules/audio_coding/codecs/g722/audio_decoder_g722.cc b/webrtc/modules/audio_coding/codecs/g722/audio_decoder_g722.cc index d38888f53e..f092e516ed 100644 --- a/webrtc/modules/audio_coding/codecs/g722/audio_decoder_g722.cc +++ b/webrtc/modules/audio_coding/codecs/g722/audio_decoder_g722.cc @@ -69,23 +69,23 @@ size_t AudioDecoderG722Impl::Channels() const { return 1; } -AudioDecoderG722Stereo::AudioDecoderG722Stereo() { +AudioDecoderG722StereoImpl::AudioDecoderG722StereoImpl() { WebRtcG722_CreateDecoder(&dec_state_left_); WebRtcG722_CreateDecoder(&dec_state_right_); WebRtcG722_DecoderInit(dec_state_left_); WebRtcG722_DecoderInit(dec_state_right_); } -AudioDecoderG722Stereo::~AudioDecoderG722Stereo() { +AudioDecoderG722StereoImpl::~AudioDecoderG722StereoImpl() { WebRtcG722_FreeDecoder(dec_state_left_); WebRtcG722_FreeDecoder(dec_state_right_); } -int AudioDecoderG722Stereo::DecodeInternal(const uint8_t* encoded, - size_t encoded_len, - int sample_rate_hz, - int16_t* decoded, - SpeechType* speech_type) { +int AudioDecoderG722StereoImpl::DecodeInternal(const uint8_t* encoded, + size_t encoded_len, + int sample_rate_hz, + int16_t* decoded, + SpeechType* speech_type) { RTC_DCHECK_EQ(SampleRateHz(), sample_rate_hz); int16_t temp_type = 1; // Default is speech. // De-interleave the bit-stream into two separate payloads. @@ -112,20 +112,20 @@ int AudioDecoderG722Stereo::DecodeInternal(const uint8_t* encoded, return static_cast(ret); } -int AudioDecoderG722Stereo::SampleRateHz() const { +int AudioDecoderG722StereoImpl::SampleRateHz() const { return 16000; } -size_t AudioDecoderG722Stereo::Channels() const { +size_t AudioDecoderG722StereoImpl::Channels() const { return 2; } -void AudioDecoderG722Stereo::Reset() { +void AudioDecoderG722StereoImpl::Reset() { WebRtcG722_DecoderInit(dec_state_left_); WebRtcG722_DecoderInit(dec_state_right_); } -std::vector AudioDecoderG722Stereo::ParsePayload( +std::vector AudioDecoderG722StereoImpl::ParsePayload( rtc::Buffer&& payload, uint32_t timestamp) { return LegacyEncodedAudioFrame::SplitBySamples(this, std::move(payload), @@ -134,9 +134,10 @@ std::vector AudioDecoderG722Stereo::ParsePayload( // Split the stereo packet and place left and right channel after each other // in the output array. -void AudioDecoderG722Stereo::SplitStereoPacket(const uint8_t* encoded, - size_t encoded_len, - uint8_t* encoded_deinterleaved) { +void AudioDecoderG722StereoImpl::SplitStereoPacket( + const uint8_t* encoded, + size_t encoded_len, + uint8_t* encoded_deinterleaved) { // Regroup the 4 bits/sample so |l1 l2| |r1 r2| |l3 l4| |r3 r4| ..., // where "lx" is 4 bits representing left sample number x, and "rx" right // sample. Two samples fit in one byte, represented with |...|. diff --git a/webrtc/modules/audio_coding/codecs/g722/audio_decoder_g722.h b/webrtc/modules/audio_coding/codecs/g722/audio_decoder_g722.h index ab104f2e20..0a530190fe 100644 --- a/webrtc/modules/audio_coding/codecs/g722/audio_decoder_g722.h +++ b/webrtc/modules/audio_coding/codecs/g722/audio_decoder_g722.h @@ -42,10 +42,10 @@ class AudioDecoderG722Impl final : public AudioDecoder { RTC_DISALLOW_COPY_AND_ASSIGN(AudioDecoderG722Impl); }; -class AudioDecoderG722Stereo final : public AudioDecoder { +class AudioDecoderG722StereoImpl final : public AudioDecoder { public: - AudioDecoderG722Stereo(); - ~AudioDecoderG722Stereo() override; + AudioDecoderG722StereoImpl(); + ~AudioDecoderG722StereoImpl() override; void Reset() override; std::vector ParsePayload(rtc::Buffer&& payload, uint32_t timestamp) override; @@ -71,7 +71,7 @@ class AudioDecoderG722Stereo final : public AudioDecoder { G722DecInst* dec_state_left_; G722DecInst* dec_state_right_; - RTC_DISALLOW_COPY_AND_ASSIGN(AudioDecoderG722Stereo); + RTC_DISALLOW_COPY_AND_ASSIGN(AudioDecoderG722StereoImpl); }; } // namespace webrtc diff --git a/webrtc/modules/audio_coding/neteq/audio_decoder_unittest.cc b/webrtc/modules/audio_coding/neteq/audio_decoder_unittest.cc index 8dcb416217..6d71b85941 100644 --- a/webrtc/modules/audio_coding/neteq/audio_decoder_unittest.cc +++ b/webrtc/modules/audio_coding/neteq/audio_decoder_unittest.cc @@ -418,7 +418,7 @@ class AudioDecoderG722StereoTest : public AudioDecoderTest { codec_input_rate_hz_ = 16000; frame_size_ = 160; data_length_ = 10 * frame_size_; - decoder_ = new AudioDecoderG722Stereo; + decoder_ = new AudioDecoderG722StereoImpl; assert(decoder_); AudioEncoderG722Config config; config.frame_size_ms = 10;