diff --git a/webrtc/modules/audio_coding/main/acm2/codec_manager.cc b/webrtc/modules/audio_coding/main/acm2/codec_manager.cc index 236f5e033d..a1a478e213 100644 --- a/webrtc/modules/audio_coding/main/acm2/codec_manager.cc +++ b/webrtc/modules/audio_coding/main/acm2/codec_manager.cc @@ -264,14 +264,16 @@ int CodecManager::RegisterEncoder(const CodecInst& send_codec) { // VAD/DTX not supported. dtx_enabled_ = false; } - if (!codec_owner_.SetEncoders( - send_codec, dtx_enabled_ ? CngPayloadType(send_codec.plfreq) : -1, - vad_mode_, red_enabled_ ? RedPayloadType(send_codec.plfreq) : -1)) + AudioEncoder* enc = rent_a_codec_.RentEncoder(send_codec); + if (!enc) return -1; + codec_owner_.SetEncoders( + enc, dtx_enabled_ ? CngPayloadType(send_codec.plfreq) : -1, + vad_mode_, red_enabled_ ? RedPayloadType(send_codec.plfreq) : -1); RTC_DCHECK(codec_owner_.Encoder()); codec_fec_enabled_ = codec_fec_enabled_ && - codec_owner_.Encoder()->SetFec(codec_fec_enabled_); + enc->SetFec(codec_fec_enabled_); send_codec_inst_ = send_codec; return 0; @@ -281,10 +283,12 @@ int CodecManager::RegisterEncoder(const CodecInst& send_codec) { if (send_codec_inst_.plfreq != send_codec.plfreq || send_codec_inst_.pacsize != send_codec.pacsize || send_codec_inst_.channels != send_codec.channels) { - if (!codec_owner_.SetEncoders( - send_codec, dtx_enabled_ ? CngPayloadType(send_codec.plfreq) : -1, - vad_mode_, red_enabled_ ? RedPayloadType(send_codec.plfreq) : -1)) + AudioEncoder* enc = rent_a_codec_.RentEncoder(send_codec); + if (!enc) return -1; + codec_owner_.SetEncoders( + enc, dtx_enabled_ ? CngPayloadType(send_codec.plfreq) : -1, + vad_mode_, red_enabled_ ? RedPayloadType(send_codec.plfreq) : -1); RTC_DCHECK(codec_owner_.Encoder()); } send_codec_inst_.plfreq = send_codec.plfreq; @@ -418,7 +422,7 @@ int CodecManager::SetCodecFEC(bool enable_codec_fec) { } AudioDecoder* CodecManager::GetAudioDecoder(const CodecInst& codec) { - return IsIsac(codec) ? codec_owner_.GetIsacDecoder() : nullptr; + return IsIsac(codec) ? rent_a_codec_.RentIsacDecoder() : nullptr; } int CodecManager::CngPayloadType(int sample_rate_hz) const { diff --git a/webrtc/modules/audio_coding/main/acm2/codec_manager.h b/webrtc/modules/audio_coding/main/acm2/codec_manager.h index 26b5a2802e..00414dce97 100644 --- a/webrtc/modules/audio_coding/main/acm2/codec_manager.h +++ b/webrtc/modules/audio_coding/main/acm2/codec_manager.h @@ -16,6 +16,7 @@ #include "webrtc/base/scoped_ptr.h" #include "webrtc/base/thread_checker.h" #include "webrtc/modules/audio_coding/main/acm2/codec_owner.h" +#include "webrtc/modules/audio_coding/main/acm2/rent_a_codec.h" #include "webrtc/modules/audio_coding/main/include/audio_coding_module_typedefs.h" #include "webrtc/common_types.h" @@ -81,6 +82,7 @@ class CodecManager final { bool red_enabled_; bool codec_fec_enabled_; CodecOwner codec_owner_; + RentACodec rent_a_codec_; bool encoder_is_opus_; RTC_DISALLOW_COPY_AND_ASSIGN(CodecManager); diff --git a/webrtc/modules/audio_coding/main/acm2/codec_owner.cc b/webrtc/modules/audio_coding/main/acm2/codec_owner.cc index 43f684ec7f..6b9809f836 100644 --- a/webrtc/modules/audio_coding/main/acm2/codec_owner.cc +++ b/webrtc/modules/audio_coding/main/acm2/codec_owner.cc @@ -77,17 +77,6 @@ void CreateCngEncoder(int cng_payload_type, } } // namespace -bool CodecOwner::SetEncoders(const CodecInst& speech_inst, - int cng_payload_type, - ACMVADMode vad_mode, - int red_payload_type) { - AudioEncoder* speech_encoder = rent_a_codec_.RentEncoder(speech_inst); - if (!speech_encoder) - return false; - SetEncoders(speech_encoder, cng_payload_type, vad_mode, red_payload_type); - return true; -} - void CodecOwner::SetEncoders(AudioEncoder* external_speech_encoder, int cng_payload_type, ACMVADMode vad_mode, @@ -110,10 +99,6 @@ void CodecOwner::ChangeCngAndRed(int cng_payload_type, CreateCngEncoder(cng_payload_type, vad_mode, encoder, &cng_encoder_); } -AudioDecoder* CodecOwner::GetIsacDecoder() { - return rent_a_codec_.RentIsacDecoder(); -} - AudioEncoder* CodecOwner::Encoder() { const auto& const_this = *this; return const_cast(const_this.Encoder()); diff --git a/webrtc/modules/audio_coding/main/acm2/codec_owner.h b/webrtc/modules/audio_coding/main/acm2/codec_owner.h index 29ac098906..bfeaee6ddc 100644 --- a/webrtc/modules/audio_coding/main/acm2/codec_owner.h +++ b/webrtc/modules/audio_coding/main/acm2/codec_owner.h @@ -16,7 +16,6 @@ #include "webrtc/common_types.h" #include "webrtc/modules/audio_coding/codecs/audio_encoder.h" #include "webrtc/modules/audio_coding/codecs/audio_decoder.h" -#include "webrtc/modules/audio_coding/main/acm2/rent_a_codec.h" #include "webrtc/modules/audio_coding/main/include/audio_coding_module_typedefs.h" namespace webrtc { @@ -27,13 +26,6 @@ class CodecOwner { CodecOwner(); ~CodecOwner(); - // Start using the specified encoder. Returns false on error. - // TODO(kwiberg): Don't handle errors here (bug 5033) - bool SetEncoders(const CodecInst& speech_inst, - int cng_payload_type, - ACMVADMode vad_mode, - int red_payload_type) WARN_UNUSED_RESULT; - void SetEncoders(AudioEncoder* external_speech_encoder, int cng_payload_type, ACMVADMode vad_mode, @@ -43,10 +35,6 @@ class CodecOwner { ACMVADMode vad_mode, int red_payload_type); - // Returns a pointer to an iSAC decoder owned by the CodecOwner. The decoder - // will live as long as the CodecOwner exists. - AudioDecoder* GetIsacDecoder(); - AudioEncoder* Encoder(); const AudioEncoder* Encoder() const; @@ -58,8 +46,6 @@ class CodecOwner { rtc::scoped_ptr cng_encoder_; rtc::scoped_ptr red_encoder_; - RentACodec rent_a_codec_; - RTC_DISALLOW_COPY_AND_ASSIGN(CodecOwner); }; diff --git a/webrtc/modules/audio_coding/main/acm2/codec_owner_unittest.cc b/webrtc/modules/audio_coding/main/acm2/codec_owner_unittest.cc index 6c4d38f8c7..317c6bd7c0 100644 --- a/webrtc/modules/audio_coding/main/acm2/codec_owner_unittest.cc +++ b/webrtc/modules/audio_coding/main/acm2/codec_owner_unittest.cc @@ -15,6 +15,7 @@ #include "webrtc/base/safe_conversions.h" #include "webrtc/modules/audio_coding/codecs/mock/mock_audio_encoder.h" #include "webrtc/modules/audio_coding/main/acm2/codec_owner.h" +#include "webrtc/modules/audio_coding/main/acm2/rent_a_codec.h" namespace webrtc { namespace acm2 { @@ -36,8 +37,9 @@ class CodecOwnerTest : public ::testing::Test { CodecOwnerTest() : timestamp_(0) {} void CreateCodec() { - ASSERT_TRUE( - codec_owner_.SetEncoders(kDefaultCodecInst, kCngPt, VADNormal, -1)); + AudioEncoder *enc = rent_a_codec_.RentEncoder(kDefaultCodecInst); + ASSERT_TRUE(enc); + codec_owner_.SetEncoders(enc, kCngPt, VADNormal, -1); } void EncodeAndVerify(size_t expected_out_length, @@ -95,6 +97,7 @@ class CodecOwnerTest : public ::testing::Test { } CodecOwner codec_owner_; + RentACodec rent_a_codec_; uint32_t timestamp_; }; @@ -172,7 +175,9 @@ TEST_F(CodecOwnerTest, ExternalEncoder) { // Change to internal encoder. CodecInst codec_inst = kDefaultCodecInst; codec_inst.pacsize = kPacketSizeSamples; - ASSERT_TRUE(codec_owner_.SetEncoders(codec_inst, -1, VADNormal, -1)); + AudioEncoder* enc = rent_a_codec_.RentEncoder(codec_inst); + ASSERT_TRUE(enc); + codec_owner_.SetEncoders(enc, -1, VADNormal, -1); // Don't expect any more calls to the external encoder. info = codec_owner_.Encoder()->Encode(1, audio, arraysize(encoded), encoded); external_encoder.Mark("B"); @@ -199,12 +204,5 @@ TEST_F(CodecOwnerTest, NoCngAndRedNoSpeechEncoderReset) { TestCngAndRedResetSpeechEncoder(false, false); } -TEST_F(CodecOwnerTest, SetEncodersError) { - CodecInst codec_inst = kDefaultCodecInst; - static const char bad_name[] = "Robert'); DROP TABLE Students;"; - std::memcpy(codec_inst.plname, bad_name, sizeof bad_name); - EXPECT_FALSE(codec_owner_.SetEncoders(codec_inst, -1, VADNormal, -1)); -} - } // namespace acm2 } // namespace webrtc diff --git a/webrtc/modules/audio_coding/main/acm2/rent_a_codec_unittest.cc b/webrtc/modules/audio_coding/main/acm2/rent_a_codec_unittest.cc new file mode 100644 index 0000000000..01ba02457a --- /dev/null +++ b/webrtc/modules/audio_coding/main/acm2/rent_a_codec_unittest.cc @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. + * + * Use of this source code is governed by a BSD-style license + * that can be found in the LICENSE file in the root of the source + * tree. An additional intellectual property rights grant can be found + * in the file PATENTS. All contributing project authors may + * be found in the AUTHORS file in the root of the source tree. + */ + +#include "testing/gtest/include/gtest/gtest.h" +#include "webrtc/modules/audio_coding/main/acm2/rent_a_codec.h" + +namespace webrtc { +namespace acm2 { + +TEST(RentACodecTest, RentEncoderError) { + const CodecInst codec_inst = { + 0, "Robert'); DROP TABLE Students;", 8000, 160, 1, 64000}; + RentACodec rent_a_codec; + EXPECT_FALSE(rent_a_codec.RentEncoder(codec_inst)); +} + +} // namespace acm2 +} // namespace webrtc diff --git a/webrtc/modules/modules.gyp b/webrtc/modules/modules.gyp index fc2945f87f..a8143761bf 100644 --- a/webrtc/modules/modules.gyp +++ b/webrtc/modules/modules.gyp @@ -103,6 +103,7 @@ 'audio_coding/main/acm2/call_statistics_unittest.cc', 'audio_coding/main/acm2/codec_owner_unittest.cc', 'audio_coding/main/acm2/initial_delay_manager_unittest.cc', + 'audio_coding/main/acm2/rent_a_codec_unittest.cc', 'audio_coding/codecs/cng/cng_unittest.cc', 'audio_coding/codecs/isac/fix/source/filters_unittest.cc', 'audio_coding/codecs/isac/fix/source/filterbanks_unittest.cc',