Refactor common_audio/vad: Create now returns the handle directly instead of an error code

Changed the WebRtcVad_Create() function to the more conventional format of returning the handle directly instead of an error code to take care of.
In addition NULL was changed to nullptr in the files where it applied.

Affected components:
* AGC
* VAD
* NetEQ

BUG=441, 3347
TESTED=locally on Linux and trybots
R=kwiberg@webrtc.org

Review URL: https://webrtc-codereview.appspot.com/51919004

Cr-Commit-Position: refs/heads/master@{#9291}
This commit is contained in:
Bjorn Volcker 2015-05-27 07:22:58 +02:00
parent afef4bfd1c
commit de4703c5d1
8 changed files with 28 additions and 44 deletions

View File

@ -25,11 +25,8 @@ extern "C" {
#endif
// Creates an instance to the VAD structure.
//
// - handle [o] : Pointer to the VAD instance that should be created.
//
// returns : 0 - (OK), -1 - (Error)
int WebRtcVad_Create(VadInst** handle);
// Returns a null pointer if create was unsuccessful.
VadInst* WebRtcVad_Create();
// Frees the dynamic memory of a specified VAD instance.
//

View File

@ -15,7 +15,8 @@
namespace webrtc {
Vad::Vad(enum Aggressiveness mode) {
CHECK_EQ(WebRtcVad_Create(&handle_), 0);
handle_ = WebRtcVad_Create();
CHECK(handle_);
CHECK_EQ(WebRtcVad_Init(handle_), 0);
CHECK_EQ(WebRtcVad_set_mode(handle_, mode), 0);
}

View File

@ -14,6 +14,7 @@
#include "testing/gtest/include/gtest/gtest.h"
#include "webrtc/base/checks.h"
#include "webrtc/common_audio/signal_processing/include/signal_processing_library.h"
#include "webrtc/common_audio/vad/include/webrtc_vad.h"
#include "webrtc/typedefs.h"
@ -57,7 +58,7 @@ TEST_F(VadTest, ApiTest) {
// This API test runs through the APIs for all possible valid and invalid
// combinations.
VadInst* handle = NULL;
VadInst* handle = WebRtcVad_Create();
int16_t zeros[kMaxFrameLength] = { 0 };
// Construct a speech signal that will trigger the VAD in all modes. It is
@ -67,14 +68,14 @@ TEST_F(VadTest, ApiTest) {
speech[i] = (i * i);
}
// NULL instance tests
EXPECT_EQ(-1, WebRtcVad_Create(NULL));
EXPECT_EQ(-1, WebRtcVad_Init(NULL));
EXPECT_EQ(-1, WebRtcVad_set_mode(NULL, kModes[0]));
EXPECT_EQ(-1, WebRtcVad_Process(NULL, kRates[0], speech, kFrameLengths[0]));
// nullptr instance tests
EXPECT_EQ(-1, WebRtcVad_Init(nullptr));
EXPECT_EQ(-1, WebRtcVad_set_mode(nullptr, kModes[0]));
EXPECT_EQ(-1,
WebRtcVad_Process(nullptr, kRates[0], speech, kFrameLengths[0]));
// WebRtcVad_Create()
ASSERT_EQ(0, WebRtcVad_Create(&handle));
CHECK(handle);
// Not initialized tests
EXPECT_EQ(-1, WebRtcVad_Process(handle, kRates[0], speech, kFrameLengths[0]));
@ -93,8 +94,9 @@ TEST_F(VadTest, ApiTest) {
kModesSize) + 1));
// WebRtcVad_Process() tests
// NULL speech pointer
EXPECT_EQ(-1, WebRtcVad_Process(handle, kRates[0], NULL, kFrameLengths[0]));
// nullptr as speech pointer
EXPECT_EQ(-1,
WebRtcVad_Process(handle, kRates[0], nullptr, kFrameLengths[0]));
// Invalid sampling rate
EXPECT_EQ(-1, WebRtcVad_Process(handle, 9999, speech, kFrameLengths[0]));
// All zeros as input should work

View File

@ -22,26 +22,16 @@ static const int kValidRates[] = { 8000, 16000, 32000, 48000 };
static const size_t kRatesSize = sizeof(kValidRates) / sizeof(*kValidRates);
static const int kMaxFrameLengthMs = 30;
int WebRtcVad_Create(VadInst** handle) {
VadInstT* self = NULL;
if (handle == NULL) {
return -1;
}
*handle = NULL;
self = (VadInstT*) malloc(sizeof(VadInstT));
*handle = (VadInst*) self;
VadInst* WebRtcVad_Create() {
VadInstT* self = (VadInstT*)malloc(sizeof(VadInstT));
if (self == NULL) {
return -1;
return NULL;
}
WebRtcSpl_Init();
self->init_flag = 0;
return 0;
return (VadInst*)self;
}
void WebRtcVad_Free(VadInst* handle) {

View File

@ -20,7 +20,8 @@ PostDecodeVad::~PostDecodeVad() {
void PostDecodeVad::Enable() {
if (!vad_instance_) {
// Create the instance.
if (WebRtcVad_Create(&vad_instance_) != 0) {
vad_instance_ = WebRtcVad_Create();
if (vad_instance_ == nullptr) {
// Failed to create instance.
Disable();
return;

View File

@ -908,8 +908,8 @@ int NetEQTest_init_coders(webrtc::NetEqDecoder coder, int enc_frameSize, int bit
for (int k = 0; k < numChannels; k++)
{
ok=WebRtcVad_Create(&VAD_inst[k]);
if (ok!=0) {
VAD_inst[k] = WebRtcVad_Create();
if (!VAD_inst[k]) {
printf("Error: Couldn't allocate memory for VAD instance\n");
exit(0);
}

View File

@ -31,15 +31,15 @@ StandaloneVad::~StandaloneVad() {
}
StandaloneVad* StandaloneVad::Create() {
VadInst* vad = NULL;
if (WebRtcVad_Create(&vad) < 0)
return NULL;
VadInst* vad = WebRtcVad_Create();
if (!vad)
return nullptr;
int err = WebRtcVad_Init(vad);
err |= WebRtcVad_set_mode(vad, kDefaultStandaloneVadMode);
if (err != 0) {
WebRtcVad_Free(vad);
return NULL;
return nullptr;
}
return new StandaloneVad(vad);
}

View File

@ -148,14 +148,7 @@ int VoiceDetectionImpl::Initialize() {
}
void* VoiceDetectionImpl::CreateHandle() const {
Handle* handle = NULL;
if (WebRtcVad_Create(&handle) != apm_->kNoError) {
handle = NULL;
} else {
assert(handle != NULL);
}
return handle;
return WebRtcVad_Create();
}
void VoiceDetectionImpl::DestroyHandle(void* handle) const {