From de4703c5d1290da22feeb708fe915179884e210f Mon Sep 17 00:00:00 2001 From: Bjorn Volcker Date: Wed, 27 May 2015 07:22:58 +0200 Subject: [PATCH] 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} --- webrtc/common_audio/vad/include/webrtc_vad.h | 7 ++----- webrtc/common_audio/vad/vad.cc | 3 ++- webrtc/common_audio/vad/vad_unittest.cc | 20 ++++++++++--------- webrtc/common_audio/vad/webrtc_vad.c | 18 ++++------------- .../audio_coding/neteq/post_decode_vad.cc | 3 ++- .../audio_coding/neteq/test/RTPencode.cc | 4 ++-- .../audio_processing/agc/standalone_vad.cc | 8 ++++---- .../audio_processing/voice_detection_impl.cc | 9 +-------- 8 files changed, 28 insertions(+), 44 deletions(-) diff --git a/webrtc/common_audio/vad/include/webrtc_vad.h b/webrtc/common_audio/vad/include/webrtc_vad.h index 053827303b..896493e046 100644 --- a/webrtc/common_audio/vad/include/webrtc_vad.h +++ b/webrtc/common_audio/vad/include/webrtc_vad.h @@ -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. // diff --git a/webrtc/common_audio/vad/vad.cc b/webrtc/common_audio/vad/vad.cc index 9cc0c19877..fd09c3f7f9 100644 --- a/webrtc/common_audio/vad/vad.cc +++ b/webrtc/common_audio/vad/vad.cc @@ -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); } diff --git a/webrtc/common_audio/vad/vad_unittest.cc b/webrtc/common_audio/vad/vad_unittest.cc index a1127ad244..ce0c745ec4 100644 --- a/webrtc/common_audio/vad/vad_unittest.cc +++ b/webrtc/common_audio/vad/vad_unittest.cc @@ -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 diff --git a/webrtc/common_audio/vad/webrtc_vad.c b/webrtc/common_audio/vad/webrtc_vad.c index 8a9b9317d8..fcf03f8058 100644 --- a/webrtc/common_audio/vad/webrtc_vad.c +++ b/webrtc/common_audio/vad/webrtc_vad.c @@ -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) { diff --git a/webrtc/modules/audio_coding/neteq/post_decode_vad.cc b/webrtc/modules/audio_coding/neteq/post_decode_vad.cc index 7ae7f97abc..07496730cd 100644 --- a/webrtc/modules/audio_coding/neteq/post_decode_vad.cc +++ b/webrtc/modules/audio_coding/neteq/post_decode_vad.cc @@ -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; diff --git a/webrtc/modules/audio_coding/neteq/test/RTPencode.cc b/webrtc/modules/audio_coding/neteq/test/RTPencode.cc index 4e779b49b0..b4263c7e4b 100644 --- a/webrtc/modules/audio_coding/neteq/test/RTPencode.cc +++ b/webrtc/modules/audio_coding/neteq/test/RTPencode.cc @@ -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); } diff --git a/webrtc/modules/audio_processing/agc/standalone_vad.cc b/webrtc/modules/audio_processing/agc/standalone_vad.cc index afd9d7b6dd..e859325454 100644 --- a/webrtc/modules/audio_processing/agc/standalone_vad.cc +++ b/webrtc/modules/audio_processing/agc/standalone_vad.cc @@ -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); } diff --git a/webrtc/modules/audio_processing/voice_detection_impl.cc b/webrtc/modules/audio_processing/voice_detection_impl.cc index da51a10ab1..0883536d52 100644 --- a/webrtc/modules/audio_processing/voice_detection_impl.cc +++ b/webrtc/modules/audio_processing/voice_detection_impl.cc @@ -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 {