Let AudioCodingModule::SendCodec return Maybe<CodecInst>

And deal with the consequences thereof...

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

Cr-Commit-Position: refs/heads/master@{#10497}
This commit is contained in:
kwiberg
2015-11-03 11:20:50 -08:00
committed by Commit bot
parent 969aeb1910
commit 1fd4a4ab35
15 changed files with 72 additions and 78 deletions

View File

@ -93,9 +93,9 @@ class AcmReceiverTestOldApi : public AudioPacketizationCallback,
if (timestamp_ == 0) { // This is the first time inserting audio. if (timestamp_ == 0) { // This is the first time inserting audio.
ASSERT_EQ(0, acm_->RegisterSendCodec(codec)); ASSERT_EQ(0, acm_->RegisterSendCodec(codec));
} else { } else {
CodecInst current_codec; auto current_codec = acm_->SendCodec();
ASSERT_EQ(0, acm_->SendCodec(&current_codec)); ASSERT_TRUE(current_codec);
if (!CodecsEqual(codec, current_codec)) if (!CodecsEqual(codec, *current_codec))
ASSERT_EQ(0, acm_->RegisterSendCodec(codec)); ASSERT_EQ(0, acm_->RegisterSendCodec(codec));
} }
AudioFrame frame; AudioFrame frame;

View File

@ -206,9 +206,9 @@ void AudioCodingModuleImpl::RegisterExternalSendCodec(
} }
// Get current send codec. // Get current send codec.
int AudioCodingModuleImpl::SendCodec(CodecInst* current_codec) const { rtc::Maybe<CodecInst> AudioCodingModuleImpl::SendCodec() const {
CriticalSectionScoped lock(acm_crit_sect_.get()); CriticalSectionScoped lock(acm_crit_sect_.get());
return codec_manager_.GetCodecInst(current_codec); return codec_manager_.GetCodecInst();
} }
// Get current send frequency. // Get current send frequency.

View File

@ -48,7 +48,7 @@ class AudioCodingModuleImpl final : public AudioCodingModule {
AudioEncoder* external_speech_encoder) override; AudioEncoder* external_speech_encoder) override;
// Get current send codec. // Get current send codec.
int SendCodec(CodecInst* current_codec) const override; rtc::Maybe<CodecInst> SendCodec() const override;
// Get current send frequency. // Get current send frequency.
int SendFrequency() const override; int SendFrequency() const override;

View File

@ -343,7 +343,7 @@ void CodecManager::RegisterEncoder(AudioEncoder* external_speech_encoder) {
codec_owner_.SetEncoders(external_speech_encoder, cng_pt, vad_mode_, red_pt); codec_owner_.SetEncoders(external_speech_encoder, cng_pt, vad_mode_, red_pt);
} }
int CodecManager::GetCodecInst(CodecInst* current_codec) const { rtc::Maybe<CodecInst> CodecManager::GetCodecInst() const {
int dummy_id = 0; int dummy_id = 0;
WEBRTC_TRACE(webrtc::kTraceStream, webrtc::kTraceAudioCoding, dummy_id, WEBRTC_TRACE(webrtc::kTraceStream, webrtc::kTraceAudioCoding, dummy_id,
"SendCodec()"); "SendCodec()");
@ -351,10 +351,9 @@ int CodecManager::GetCodecInst(CodecInst* current_codec) const {
if (!codec_owner_.Encoder()) { if (!codec_owner_.Encoder()) {
WEBRTC_TRACE(webrtc::kTraceStream, webrtc::kTraceAudioCoding, dummy_id, WEBRTC_TRACE(webrtc::kTraceStream, webrtc::kTraceAudioCoding, dummy_id,
"SendCodec Failed, no codec is registered"); "SendCodec Failed, no codec is registered");
return -1; return rtc::Maybe<CodecInst>();
} }
*current_codec = send_codec_inst_; return rtc::Maybe<CodecInst>(send_codec_inst_);
return 0;
} }
bool CodecManager::SetCopyRed(bool enable) { bool CodecManager::SetCopyRed(bool enable) {

View File

@ -12,6 +12,7 @@
#define WEBRTC_MODULES_AUDIO_CODING_MAIN_ACM2_CODEC_MANAGER_H_ #define WEBRTC_MODULES_AUDIO_CODING_MAIN_ACM2_CODEC_MANAGER_H_
#include "webrtc/base/constructormagic.h" #include "webrtc/base/constructormagic.h"
#include "webrtc/base/maybe.h"
#include "webrtc/base/scoped_ptr.h" #include "webrtc/base/scoped_ptr.h"
#include "webrtc/base/thread_checker.h" #include "webrtc/base/thread_checker.h"
#include "webrtc/modules/audio_coding/main/acm2/codec_owner.h" #include "webrtc/modules/audio_coding/main/acm2/codec_owner.h"
@ -34,7 +35,7 @@ class CodecManager final {
void RegisterEncoder(AudioEncoder* external_speech_encoder); void RegisterEncoder(AudioEncoder* external_speech_encoder);
int GetCodecInst(CodecInst* current_codec) const; rtc::Maybe<CodecInst> GetCodecInst() const;
bool SetCopyRed(bool enable); bool SetCopyRed(bool enable);

View File

@ -13,6 +13,7 @@
#include <vector> #include <vector>
#include "webrtc/base/maybe.h"
#include "webrtc/common_types.h" #include "webrtc/common_types.h"
#include "webrtc/modules/audio_coding/main/acm2/acm_codec_database.h" #include "webrtc/modules/audio_coding/main/acm2/acm_codec_database.h"
#include "webrtc/modules/audio_coding/main/include/audio_coding_module_typedefs.h" #include "webrtc/modules/audio_coding/main/include/audio_coding_module_typedefs.h"
@ -210,14 +211,10 @@ class AudioCodingModule {
// int32_t SendCodec() // int32_t SendCodec()
// Get parameters for the codec currently registered as send codec. // Get parameters for the codec currently registered as send codec.
// //
// Output:
// -current_send_codec : parameters of the send codec.
//
// Return value: // Return value:
// -1 if failed to get send codec, // The send codec, or nothing if we don't have one
// 0 if succeeded.
// //
virtual int32_t SendCodec(CodecInst* current_send_codec) const = 0; virtual rtc::Maybe<CodecInst> SendCodec() const = 0;
/////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////
// int32_t SendFrequency() // int32_t SendFrequency()

View File

@ -823,9 +823,11 @@ void APITest::TestRegisteration(char sendSide) {
exit(-1); exit(-1);
} }
CodecInst myCodec; auto myCodec = sendACM->SendCodec();
if (sendACM->SendCodec(&myCodec) < 0) { if (!myCodec) {
AudioCodingModule::Codec(_codecCntrA, &myCodec); CodecInst ci;
AudioCodingModule::Codec(_codecCntrA, &ci);
myCodec = rtc::Maybe<CodecInst>(ci);
} }
if (!_randomTest) { if (!_randomTest) {
@ -837,12 +839,12 @@ void APITest::TestRegisteration(char sendSide) {
*thereIsDecoder = false; *thereIsDecoder = false;
} }
//myEvent->Wait(20); //myEvent->Wait(20);
CHECK_ERROR_MT(receiveACM->UnregisterReceiveCodec(myCodec.pltype)); CHECK_ERROR_MT(receiveACM->UnregisterReceiveCodec(myCodec->pltype));
Wait(1000); Wait(1000);
int currentPayload = myCodec.pltype; int currentPayload = myCodec->pltype;
if (!FixedPayloadTypeCodec(myCodec.plname)) { if (!FixedPayloadTypeCodec(myCodec->plname)) {
int32_t i; int32_t i;
for (i = 0; i < 32; i++) { for (i = 0; i < 32; i++) {
if (!_payloadUsed[i]) { if (!_payloadUsed[i]) {
@ -850,9 +852,9 @@ void APITest::TestRegisteration(char sendSide) {
fprintf(stdout, fprintf(stdout,
"Register receive codec with new Payload, AUDIO BACK.\n"); "Register receive codec with new Payload, AUDIO BACK.\n");
} }
//myCodec.pltype = i + 96; //myCodec->pltype = i + 96;
//CHECK_ERROR_MT(receiveACM->RegisterReceiveCodec(myCodec)); //CHECK_ERROR_MT(receiveACM->RegisterReceiveCodec(*myCodec));
//CHECK_ERROR_MT(sendACM->RegisterSendCodec(myCodec)); //CHECK_ERROR_MT(sendACM->RegisterSendCodec(*myCodec));
//myEvent->Wait(20); //myEvent->Wait(20);
//{ //{
// WriteLockScoped wl(_apiTestRWLock); // WriteLockScoped wl(_apiTestRWLock);
@ -868,17 +870,17 @@ void APITest::TestRegisteration(char sendSide) {
// *thereIsDecoder = false; // *thereIsDecoder = false;
//} //}
//myEvent->Wait(20); //myEvent->Wait(20);
//CHECK_ERROR_MT(receiveACM->UnregisterReceiveCodec(myCodec.pltype)); //CHECK_ERROR_MT(receiveACM->UnregisterReceiveCodec(myCodec->pltype));
Wait(1000); Wait(1000);
myCodec.pltype = currentPayload; myCodec->pltype = currentPayload;
if (!_randomTest) { if (!_randomTest) {
fprintf(stdout, fprintf(stdout,
"Register receive codec with default Payload, AUDIO BACK.\n"); "Register receive codec with default Payload, AUDIO BACK.\n");
fflush (stdout); fflush (stdout);
} }
CHECK_ERROR_MT(receiveACM->RegisterReceiveCodec(myCodec)); CHECK_ERROR_MT(receiveACM->RegisterReceiveCodec(*myCodec));
//CHECK_ERROR_MT(sendACM->RegisterSendCodec(myCodec)); //CHECK_ERROR_MT(sendACM->RegisterSendCodec(*myCodec));
myEvent->Wait(20); myEvent->Wait(20);
{ {
WriteLockScoped wl(_apiTestRWLock); WriteLockScoped wl(_apiTestRWLock);
@ -890,7 +892,7 @@ void APITest::TestRegisteration(char sendSide) {
} }
} }
if (i == 32) { if (i == 32) {
CHECK_ERROR_MT(receiveACM->RegisterReceiveCodec(myCodec)); CHECK_ERROR_MT(receiveACM->RegisterReceiveCodec(*myCodec));
{ {
WriteLockScoped wl(_apiTestRWLock); WriteLockScoped wl(_apiTestRWLock);
*thereIsDecoder = true; *thereIsDecoder = true;
@ -902,9 +904,9 @@ void APITest::TestRegisteration(char sendSide) {
"Register receive codec with fixed Payload, AUDIO BACK.\n"); "Register receive codec with fixed Payload, AUDIO BACK.\n");
fflush (stdout); fflush (stdout);
} }
CHECK_ERROR_MT(receiveACM->RegisterReceiveCodec(myCodec)); CHECK_ERROR_MT(receiveACM->RegisterReceiveCodec(*myCodec));
//CHECK_ERROR_MT(receiveACM->UnregisterReceiveCodec(myCodec.pltype)); //CHECK_ERROR_MT(receiveACM->UnregisterReceiveCodec(myCodec->pltype));
//CHECK_ERROR_MT(receiveACM->RegisterReceiveCodec(myCodec)); //CHECK_ERROR_MT(receiveACM->RegisterReceiveCodec(*myCodec));
myEvent->Wait(20); myEvent->Wait(20);
{ {
WriteLockScoped wl(_apiTestRWLock); WriteLockScoped wl(_apiTestRWLock);
@ -1001,22 +1003,17 @@ void APITest::TestSendVAD(char side) {
} }
void APITest::CurrentCodec(char side) { void APITest::CurrentCodec(char side) {
CodecInst myCodec; auto myCodec = (side == 'A' ? _acmA : _acmB)->SendCodec();
if (side == 'A') {
_acmA->SendCodec(&myCodec);
} else {
_acmB->SendCodec(&myCodec);
}
if (!_randomTest) { if (!_randomTest) {
fprintf(stdout, "\n\n"); fprintf(stdout, "\n\n");
fprintf(stdout, "Send codec in Side A\n"); fprintf(stdout, "Send codec in Side A\n");
fprintf(stdout, "----------------------------\n"); fprintf(stdout, "----------------------------\n");
fprintf(stdout, "Name................. %s\n", myCodec.plname); fprintf(stdout, "Name................. %s\n", myCodec->plname);
fprintf(stdout, "Sampling Frequency... %d\n", myCodec.plfreq); fprintf(stdout, "Sampling Frequency... %d\n", myCodec->plfreq);
fprintf(stdout, "Rate................. %d\n", myCodec.rate); fprintf(stdout, "Rate................. %d\n", myCodec->rate);
fprintf(stdout, "Payload-type......... %d\n", myCodec.pltype); fprintf(stdout, "Payload-type......... %d\n", myCodec->pltype);
fprintf(stdout, "Packet-size.......... %d\n", myCodec.pacsize); fprintf(stdout, "Packet-size.......... %d\n", myCodec->pacsize);
} }
Wait(100); Wait(100);

View File

@ -339,8 +339,7 @@ std::string EncodeDecodeTest::EncodeToFile(int fileType,
_sender.codeId = codeId; _sender.codeId = codeId;
_sender.Setup(acm.get(), &rtpFile, "audio_coding/testfile32kHz", 32000, 1); _sender.Setup(acm.get(), &rtpFile, "audio_coding/testfile32kHz", 32000, 1);
struct CodecInst sendCodecInst; if (acm->SendCodec()) {
if (acm->SendCodec(&sendCodecInst) >= 0) {
_sender.Run(); _sender.Run();
} }
_sender.Teardown(); _sender.Teardown();

View File

@ -143,8 +143,7 @@ void PacketLossTest::Perform() {
sender_->Setup(acm.get(), &rtpFile, in_file_name_, sample_rate_hz_, channels_, sender_->Setup(acm.get(), &rtpFile, in_file_name_, sample_rate_hz_, channels_,
expected_loss_rate_); expected_loss_rate_);
struct CodecInst sendCodecInst; if (acm->SendCodec()) {
if (acm->SendCodec(&sendCodecInst) >= 0) {
sender_->Run(); sender_->Run();
} }
sender_->Teardown(); sender_->Teardown();

View File

@ -477,8 +477,7 @@ void TestAllCodecs::OpenOutFile(int test_number) {
void TestAllCodecs::DisplaySendReceiveCodec() { void TestAllCodecs::DisplaySendReceiveCodec() {
CodecInst my_codec_param; CodecInst my_codec_param;
acm_a_->SendCodec(&my_codec_param); printf("%s -> ", acm_a_->SendCodec()->plname);
printf("%s -> ", my_codec_param.plname);
acm_b_->ReceiveCodec(&my_codec_param); acm_b_->ReceiveCodec(&my_codec_param);
printf("%s\n", my_codec_param.plname); printf("%s\n", my_codec_param.plname);
} }

View File

@ -823,14 +823,15 @@ void TestStereo::OpenOutFile(int16_t test_number) {
} }
void TestStereo::DisplaySendReceiveCodec() { void TestStereo::DisplaySendReceiveCodec() {
CodecInst my_codec_param; auto send_codec = acm_a_->SendCodec();
acm_a_->SendCodec(&my_codec_param);
if (test_mode_ != 0) { if (test_mode_ != 0) {
printf("%s -> ", my_codec_param.plname); ASSERT_TRUE(send_codec);
printf("%s -> ", send_codec->plname);
} }
acm_b_->ReceiveCodec(&my_codec_param); CodecInst receive_codec;
acm_b_->ReceiveCodec(&receive_codec);
if (test_mode_ != 0) { if (test_mode_ != 0) {
printf("%s\n", my_codec_param.plname); printf("%s\n", receive_codec.plname);
} }
} }

View File

@ -209,9 +209,9 @@ void TestWebRtcVadDtx::SetVAD(bool enable_dtx, bool enable_vad,
EXPECT_EQ(0, acm_send_->SetVAD(enable_dtx, enable_vad, vad_mode)); EXPECT_EQ(0, acm_send_->SetVAD(enable_dtx, enable_vad, vad_mode));
EXPECT_EQ(0, acm_send_->VAD(&dtx_enabled_, &vad_enabled_, &mode)); EXPECT_EQ(0, acm_send_->VAD(&dtx_enabled_, &vad_enabled_, &mode));
CodecInst codec_param; auto codec_param = acm_send_->SendCodec();
acm_send_->SendCodec(&codec_param); ASSERT_TRUE(codec_param);
if (STR_CASE_CMP(codec_param.plname, "opus") == 0) { if (STR_CASE_CMP(codec_param->plname, "opus") == 0) {
// If send codec is Opus, WebRTC VAD/DTX cannot be used. // If send codec is Opus, WebRTC VAD/DTX cannot be used.
enable_dtx = enable_vad = false; enable_dtx = enable_vad = false;
} }

View File

@ -250,10 +250,8 @@ void TwoWayCommunication::Perform() {
AudioFrame audioFrame; AudioFrame audioFrame;
CodecInst codecInst_B; auto codecInst_B = _acmB->SendCodec();
CodecInst dummy; ASSERT_TRUE(codecInst_B);
EXPECT_EQ(0, _acmB->SendCodec(&codecInst_B));
// In the following loop we tests that the code can handle misuse of the APIs. // In the following loop we tests that the code can handle misuse of the APIs.
// In the middle of a session with data flowing between two sides, called A // In the middle of a session with data flowing between two sides, called A
@ -285,15 +283,15 @@ void TwoWayCommunication::Perform() {
} }
// Re-register send codec on side B. // Re-register send codec on side B.
if (((secPassed % 5) == 4) && (msecPassed >= 990)) { if (((secPassed % 5) == 4) && (msecPassed >= 990)) {
EXPECT_EQ(0, _acmB->RegisterSendCodec(codecInst_B)); EXPECT_EQ(0, _acmB->RegisterSendCodec(*codecInst_B));
EXPECT_EQ(0, _acmB->SendCodec(&dummy)); EXPECT_TRUE(_acmB->SendCodec());
} }
// Initialize receiver on side A. // Initialize receiver on side A.
if (((secPassed % 7) == 6) && (msecPassed == 0)) if (((secPassed % 7) == 6) && (msecPassed == 0))
EXPECT_EQ(0, _acmA->InitializeReceiver()); EXPECT_EQ(0, _acmA->InitializeReceiver());
// Re-register codec on side A. // Re-register codec on side A.
if (((secPassed % 7) == 6) && (msecPassed >= 990)) { if (((secPassed % 7) == 6) && (msecPassed >= 990)) {
EXPECT_EQ(0, _acmA->RegisterReceiveCodec(codecInst_B)); EXPECT_EQ(0, _acmA->RegisterReceiveCodec(*codecInst_B));
} }
} }
} }

View File

@ -47,21 +47,21 @@ int16_t SetISAConfig(ACMTestISACConfig& isacConfig, AudioCodingModule* acm,
if ((isacConfig.currentRateBitPerSec != 0) if ((isacConfig.currentRateBitPerSec != 0)
|| (isacConfig.currentFrameSizeMsec != 0)) { || (isacConfig.currentFrameSizeMsec != 0)) {
CodecInst sendCodec; auto sendCodec = acm->SendCodec();
EXPECT_EQ(0, acm->SendCodec(&sendCodec)); EXPECT_TRUE(sendCodec);
if (isacConfig.currentRateBitPerSec < 0) { if (isacConfig.currentRateBitPerSec < 0) {
// Register iSAC in adaptive (channel-dependent) mode. // Register iSAC in adaptive (channel-dependent) mode.
sendCodec.rate = -1; sendCodec->rate = -1;
EXPECT_EQ(0, acm->RegisterSendCodec(sendCodec)); EXPECT_EQ(0, acm->RegisterSendCodec(*sendCodec));
} else { } else {
if (isacConfig.currentRateBitPerSec != 0) { if (isacConfig.currentRateBitPerSec != 0) {
sendCodec.rate = isacConfig.currentRateBitPerSec; sendCodec->rate = isacConfig.currentRateBitPerSec;
} }
if (isacConfig.currentFrameSizeMsec != 0) { if (isacConfig.currentFrameSizeMsec != 0) {
sendCodec.pacsize = isacConfig.currentFrameSizeMsec sendCodec->pacsize = isacConfig.currentFrameSizeMsec
* (sendCodec.plfreq / 1000); * (sendCodec->plfreq / 1000);
} }
EXPECT_EQ(0, acm->RegisterSendCodec(sendCodec)); EXPECT_EQ(0, acm->RegisterSendCodec(*sendCodec));
} }
} }
@ -238,7 +238,6 @@ void ISACTest::EncodeDecode(int testNr, ACMTestISACConfig& wbISACConfig,
_channel_B2A->ResetStats(); _channel_B2A->ResetStats();
char currentTime[500]; char currentTime[500];
CodecInst sendCodec;
EventTimerWrapper* myEvent = EventTimerWrapper::Create(); EventTimerWrapper* myEvent = EventTimerWrapper::Create();
EXPECT_TRUE(myEvent->StartTimer(true, 10)); EXPECT_TRUE(myEvent->StartTimer(true, 10));
while (!(_inFileA.EndOfFile() || _inFileA.Rewinded())) { while (!(_inFileA.EndOfFile() || _inFileA.Rewinded())) {
@ -248,8 +247,8 @@ void ISACTest::EncodeDecode(int testNr, ACMTestISACConfig& wbISACConfig,
if ((adaptiveMode) && (_testMode != 0)) { if ((adaptiveMode) && (_testMode != 0)) {
myEvent->Wait(5000); myEvent->Wait(5000);
EXPECT_EQ(0, _acmA->SendCodec(&sendCodec)); EXPECT_TRUE(_acmA->SendCodec());
EXPECT_EQ(0, _acmB->SendCodec(&sendCodec)); EXPECT_TRUE(_acmB->SendCodec());
} }
} }

View File

@ -1242,7 +1242,12 @@ Channel::DeRegisterVoiceEngineObserver()
int32_t int32_t
Channel::GetSendCodec(CodecInst& codec) Channel::GetSendCodec(CodecInst& codec)
{ {
return (audio_coding_->SendCodec(&codec)); auto send_codec = audio_coding_->SendCodec();
if (send_codec) {
codec = *send_codec;
return 0;
}
return -1;
} }
int32_t int32_t