Move the Rent-A-Codec™ from CodecOwner to CodecManager

Future CLs will move it even further down the stack.

BUG=webrtc:5028

Review URL: https://codereview.webrtc.org/1431103002

Cr-Commit-Position: refs/heads/master@{#10580}
This commit is contained in:
kwiberg
2015-11-10 06:35:21 -08:00
committed by Commit bot
parent cf3e13d82d
commit c95c366f5a
7 changed files with 48 additions and 47 deletions

View File

@ -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 {

View File

@ -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);

View File

@ -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<AudioEncoder*>(const_this.Encoder());

View File

@ -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<AudioEncoder> cng_encoder_;
rtc::scoped_ptr<AudioEncoder> red_encoder_;
RentACodec rent_a_codec_;
RTC_DISALLOW_COPY_AND_ASSIGN(CodecOwner);
};

View File

@ -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

View File

@ -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