audio_processing: Create now returns a pointer to the object
Affects
* NS
* AGC
* AEC
BUG=441
TESTED=locally on Linux and trybots
R=kwiberg@webrtc.org
Review URL: https://codereview.webrtc.org/1175903002.
Cr-Commit-Position: refs/heads/master@{#9411}
diff --git a/webrtc/modules/audio_processing/aec/aec_core.c b/webrtc/modules/audio_processing/aec/aec_core.c
index 32266f4..16e3389 100644
--- a/webrtc/modules/audio_processing/aec/aec_core.c
+++ b/webrtc/modules/audio_processing/aec/aec_core.c
@@ -1367,26 +1367,23 @@
#endif
}
-int WebRtcAec_CreateAec(AecCore** aecInst) {
+AecCore* WebRtcAec_CreateAec() {
int i;
AecCore* aec = malloc(sizeof(AecCore));
- *aecInst = aec;
- if (aec == NULL) {
- return -1;
+ if (!aec) {
+ return NULL;
}
aec->nearFrBuf = WebRtc_CreateBuffer(FRAME_LEN + PART_LEN, sizeof(float));
if (!aec->nearFrBuf) {
WebRtcAec_FreeAec(aec);
- aec = NULL;
- return -1;
+ return NULL;
}
aec->outFrBuf = WebRtc_CreateBuffer(FRAME_LEN + PART_LEN, sizeof(float));
if (!aec->outFrBuf) {
WebRtcAec_FreeAec(aec);
- aec = NULL;
- return -1;
+ return NULL;
}
for (i = 0; i < NUM_HIGH_BANDS_MAX; ++i) {
@@ -1394,15 +1391,13 @@
sizeof(float));
if (!aec->nearFrBufH[i]) {
WebRtcAec_FreeAec(aec);
- aec = NULL;
- return -1;
+ return NULL;
}
aec->outFrBufH[i] = WebRtc_CreateBuffer(FRAME_LEN + PART_LEN,
sizeof(float));
if (!aec->outFrBufH[i]) {
WebRtcAec_FreeAec(aec);
- aec = NULL;
- return -1;
+ return NULL;
}
}
@@ -1411,15 +1406,13 @@
WebRtc_CreateBuffer(kBufSizePartitions, sizeof(float) * 2 * PART_LEN1);
if (!aec->far_buf) {
WebRtcAec_FreeAec(aec);
- aec = NULL;
- return -1;
+ return NULL;
}
aec->far_buf_windowed =
WebRtc_CreateBuffer(kBufSizePartitions, sizeof(float) * 2 * PART_LEN1);
if (!aec->far_buf_windowed) {
WebRtcAec_FreeAec(aec);
- aec = NULL;
- return -1;
+ return NULL;
}
#ifdef WEBRTC_AEC_DEBUG_DUMP
aec->instance_index = webrtc_aec_instance_count;
@@ -1427,8 +1420,7 @@
WebRtc_CreateBuffer(kBufSizePartitions, sizeof(float) * PART_LEN);
if (!aec->far_time_buf) {
WebRtcAec_FreeAec(aec);
- aec = NULL;
- return -1;
+ return NULL;
}
aec->farFile = aec->nearFile = aec->outFile = aec->outLinearFile = NULL;
aec->debug_dump_count = 0;
@@ -1437,8 +1429,7 @@
WebRtc_CreateDelayEstimatorFarend(PART_LEN1, kHistorySizeBlocks);
if (aec->delay_estimator_farend == NULL) {
WebRtcAec_FreeAec(aec);
- aec = NULL;
- return -1;
+ return NULL;
}
// We create the delay_estimator with the same amount of maximum lookahead as
// the delay history size (kHistorySizeBlocks) for symmetry reasons.
@@ -1446,8 +1437,7 @@
aec->delay_estimator_farend, kHistorySizeBlocks);
if (aec->delay_estimator == NULL) {
WebRtcAec_FreeAec(aec);
- aec = NULL;
- return -1;
+ return NULL;
}
#ifdef WEBRTC_ANDROID
// DA-AEC assumes the system is causal from the beginning and will self adjust
@@ -1485,7 +1475,7 @@
aec_rdft_init();
- return 0;
+ return aec;
}
void WebRtcAec_FreeAec(AecCore* aec) {
diff --git a/webrtc/modules/audio_processing/aec/aec_core.h b/webrtc/modules/audio_processing/aec/aec_core.h
index aa5687a..2fa26db 100644
--- a/webrtc/modules/audio_processing/aec/aec_core.h
+++ b/webrtc/modules/audio_processing/aec/aec_core.h
@@ -51,7 +51,7 @@
typedef struct AecCore AecCore;
-int WebRtcAec_CreateAec(AecCore** aec);
+AecCore* WebRtcAec_CreateAec(); // Returns NULL on error.
void WebRtcAec_FreeAec(AecCore* aec);
int WebRtcAec_InitAec(AecCore* aec, int sampFreq);
void WebRtcAec_InitAec_SSE2(void);
diff --git a/webrtc/modules/audio_processing/aec/aec_resampler.c b/webrtc/modules/audio_processing/aec/aec_resampler.c
index 8b07e16..cef262e 100644
--- a/webrtc/modules/audio_processing/aec/aec_resampler.c
+++ b/webrtc/modules/audio_processing/aec/aec_resampler.c
@@ -40,14 +40,8 @@
int absLimit,
float* skewEst);
-int WebRtcAec_CreateResampler(void** resampInst) {
- AecResampler* obj = malloc(sizeof(AecResampler));
- *resampInst = obj;
- if (obj == NULL) {
- return -1;
- }
-
- return 0;
+void* WebRtcAec_CreateResampler() {
+ return malloc(sizeof(AecResampler));
}
int WebRtcAec_InitResampler(void* resampInst, int deviceSampleRateHz) {
diff --git a/webrtc/modules/audio_processing/aec/aec_resampler.h b/webrtc/modules/audio_processing/aec/aec_resampler.h
index debe069..a374992 100644
--- a/webrtc/modules/audio_processing/aec/aec_resampler.h
+++ b/webrtc/modules/audio_processing/aec/aec_resampler.h
@@ -20,8 +20,8 @@
kResamplerBufferSize = FRAME_LEN * 4
};
-// Unless otherwise specified, functions return 0 on success and -1 on error
-int WebRtcAec_CreateResampler(void** resampInst);
+// Unless otherwise specified, functions return 0 on success and -1 on error.
+void* WebRtcAec_CreateResampler(); // Returns NULL on error.
int WebRtcAec_InitResampler(void* resampInst, int deviceSampleRateHz);
void WebRtcAec_FreeResampler(void* resampInst);
diff --git a/webrtc/modules/audio_processing/aec/echo_cancellation.c b/webrtc/modules/audio_processing/aec/echo_cancellation.c
index 06d081d..ec16aaf 100644
--- a/webrtc/modules/audio_processing/aec/echo_cancellation.c
+++ b/webrtc/modules/audio_processing/aec/echo_cancellation.c
@@ -118,28 +118,22 @@
int16_t reported_delay_ms,
int32_t skew);
-int32_t WebRtcAec_Create(void** aecInst) {
- Aec* aecpc;
- if (aecInst == NULL) {
- return -1;
+void* WebRtcAec_Create() {
+ Aec* aecpc = malloc(sizeof(Aec));
+
+ if (!aecpc) {
+ return NULL;
}
- aecpc = malloc(sizeof(Aec));
- *aecInst = aecpc;
- if (aecpc == NULL) {
- return -1;
- }
-
- if (WebRtcAec_CreateAec(&aecpc->aec) == -1) {
+ aecpc->aec = WebRtcAec_CreateAec();
+ if (!aecpc->aec) {
WebRtcAec_Free(aecpc);
- aecpc = NULL;
- return -1;
+ return NULL;
}
-
- if (WebRtcAec_CreateResampler(&aecpc->resampler) == -1) {
+ aecpc->resampler = WebRtcAec_CreateResampler();
+ if (!aecpc->resampler) {
WebRtcAec_Free(aecpc);
- aecpc = NULL;
- return -1;
+ return NULL;
}
// Create far-end pre-buffer. The buffer size has to be large enough for
// largest possible drift compensation (kResamplerBufferSize) + "almost" an
@@ -148,8 +142,7 @@
WebRtc_CreateBuffer(PART_LEN2 + kResamplerBufferSize, sizeof(float));
if (!aecpc->far_pre_buf) {
WebRtcAec_Free(aecpc);
- aecpc = NULL;
- return -1;
+ return NULL;
}
aecpc->initFlag = 0;
@@ -168,7 +161,7 @@
}
#endif
- return 0;
+ return aecpc;
}
void WebRtcAec_Free(void* aecInst) {
diff --git a/webrtc/modules/audio_processing/aec/echo_cancellation_unittest.cc b/webrtc/modules/audio_processing/aec/echo_cancellation_unittest.cc
index 6c325e0..315ac3e 100644
--- a/webrtc/modules/audio_processing/aec/echo_cancellation_unittest.cc
+++ b/webrtc/modules/audio_processing/aec/echo_cancellation_unittest.cc
@@ -20,22 +20,20 @@
}
#include "testing/gtest/include/gtest/gtest.h"
+#include "webrtc/base/checks.h"
namespace webrtc {
TEST(EchoCancellationTest, CreateAndFreeHasExpectedBehavior) {
- EXPECT_EQ(-1, WebRtcAec_Create(NULL));
- void* handle = NULL;
- ASSERT_EQ(0, WebRtcAec_Create(&handle));
- EXPECT_TRUE(handle != NULL);
+ void* handle = WebRtcAec_Create();
+ ASSERT_TRUE(handle);
WebRtcAec_Free(nullptr);
WebRtcAec_Free(handle);
}
TEST(EchoCancellationTest, ApplyAecCoreHandle) {
- void* handle = NULL;
- ASSERT_EQ(0, WebRtcAec_Create(&handle));
- EXPECT_TRUE(handle != NULL);
+ void* handle = WebRtcAec_Create();
+ ASSERT_TRUE(handle);
EXPECT_TRUE(WebRtcAec_aec_core(NULL) == NULL);
AecCore* aec_core = WebRtcAec_aec_core(handle);
EXPECT_TRUE(aec_core != NULL);
diff --git a/webrtc/modules/audio_processing/aec/include/echo_cancellation.h b/webrtc/modules/audio_processing/aec/include/echo_cancellation.h
index 6914ed1..e49a084 100644
--- a/webrtc/modules/audio_processing/aec/include/echo_cancellation.h
+++ b/webrtc/modules/audio_processing/aec/include/echo_cancellation.h
@@ -64,19 +64,10 @@
/*
* Allocates the memory needed by the AEC. The memory needs to be initialized
- * separately using the WebRtcAec_Init() function.
- *
- * Inputs Description
- * -------------------------------------------------------------------
- * void** aecInst Pointer to the AEC instance to be created
- * and initialized
- *
- * Outputs Description
- * -------------------------------------------------------------------
- * int32_t return 0: OK
- * -1: error
+ * separately using the WebRtcAec_Init() function. Returns a pointer to the
+ * object or NULL on error.
*/
-int32_t WebRtcAec_Create(void** aecInst);
+void* WebRtcAec_Create();
/*
* This function releases the memory allocated by WebRtcAec_Create().
diff --git a/webrtc/modules/audio_processing/aec/system_delay_unittest.cc b/webrtc/modules/audio_processing/aec/system_delay_unittest.cc
index da28752..42800c5 100644
--- a/webrtc/modules/audio_processing/aec/system_delay_unittest.cc
+++ b/webrtc/modules/audio_processing/aec/system_delay_unittest.cc
@@ -9,6 +9,7 @@
*/
#include "testing/gtest/include/gtest/gtest.h"
+#include "webrtc/base/checks.h"
extern "C" {
#include "webrtc/modules/audio_processing/aec/aec_core.h"
}
@@ -67,7 +68,8 @@
}
void SystemDelayTest::SetUp() {
- ASSERT_EQ(0, WebRtcAec_Create(&handle_));
+ handle_ = WebRtcAec_Create();
+ ASSERT_TRUE(handle_);
self_ = reinterpret_cast<Aec*>(handle_);
}
diff --git a/webrtc/modules/audio_processing/agc/legacy/analog_agc.c b/webrtc/modules/audio_processing/agc/legacy/analog_agc.c
index bb56cfe..73adb5d 100644
--- a/webrtc/modules/audio_processing/agc/legacy/analog_agc.c
+++ b/webrtc/modules/audio_processing/agc/legacy/analog_agc.c
@@ -1313,31 +1313,19 @@
return 0;
}
-int WebRtcAgc_Create(void **agcInst)
-{
- LegacyAgc* stt;
- if (agcInst == NULL)
- {
- return -1;
- }
- stt = (LegacyAgc*)malloc(sizeof(LegacyAgc));
-
- *agcInst = stt;
- if (stt == NULL)
- {
- return -1;
- }
+void* WebRtcAgc_Create() {
+ LegacyAgc* stt = malloc(sizeof(LegacyAgc));
#ifdef WEBRTC_AGC_DEBUG_DUMP
- stt->fpt = fopen("./agc_test_log.txt", "wt");
- stt->agcLog = fopen("./agc_debug_log.txt", "wt");
- stt->digitalAgc.logFile = fopen("./agc_log.txt", "wt");
+ stt->fpt = fopen("./agc_test_log.txt", "wt");
+ stt->agcLog = fopen("./agc_debug_log.txt", "wt");
+ stt->digitalAgc.logFile = fopen("./agc_log.txt", "wt");
#endif
- stt->initFlag = 0;
- stt->lastError = 0;
+ stt->initFlag = 0;
+ stt->lastError = 0;
- return 0;
+ return stt;
}
void WebRtcAgc_Free(void *state) {
diff --git a/webrtc/modules/audio_processing/agc/legacy/gain_control.h b/webrtc/modules/audio_processing/agc/legacy/gain_control.h
index 3994f55..0ccba76 100644
--- a/webrtc/modules/audio_processing/agc/legacy/gain_control.h
+++ b/webrtc/modules/audio_processing/agc/legacy/gain_control.h
@@ -200,13 +200,10 @@
int WebRtcAgc_get_config(void* agcInst, WebRtcAgcConfig* config);
/*
- * This function creates an AGC instance, which will contain the state
- * information for one (duplex) channel.
- *
- * Return value : AGC instance if successful
- * : 0 (i.e., a NULL pointer) if unsuccessful
+ * This function creates and returns an AGC instance, which will contain the
+ * state information for one (duplex) channel.
*/
-int WebRtcAgc_Create(void **agcInst);
+void* WebRtcAgc_Create();
/*
* This function frees the AGC instance created at the beginning.
diff --git a/webrtc/modules/audio_processing/echo_cancellation_impl.cc b/webrtc/modules/audio_processing/echo_cancellation_impl.cc
index e28f127..fd3e1bf 100644
--- a/webrtc/modules/audio_processing/echo_cancellation_impl.cc
+++ b/webrtc/modules/audio_processing/echo_cancellation_impl.cc
@@ -337,14 +337,7 @@
}
void* EchoCancellationImpl::CreateHandle() const {
- Handle* handle = NULL;
- if (WebRtcAec_Create(&handle) != apm_->kNoError) {
- handle = NULL;
- } else {
- assert(handle != NULL);
- }
-
- return handle;
+ return WebRtcAec_Create();
}
void EchoCancellationImpl::DestroyHandle(void* handle) const {
diff --git a/webrtc/modules/audio_processing/gain_control_impl.cc b/webrtc/modules/audio_processing/gain_control_impl.cc
index 5f301c1..398cf5c 100644
--- a/webrtc/modules/audio_processing/gain_control_impl.cc
+++ b/webrtc/modules/audio_processing/gain_control_impl.cc
@@ -301,14 +301,7 @@
}
void* GainControlImpl::CreateHandle() const {
- Handle* handle = NULL;
- if (WebRtcAgc_Create(&handle) != apm_->kNoError) {
- handle = NULL;
- } else {
- assert(handle != NULL);
- }
-
- return handle;
+ return WebRtcAgc_Create();
}
void GainControlImpl::DestroyHandle(void* handle) const {
diff --git a/webrtc/modules/audio_processing/noise_suppression_impl.cc b/webrtc/modules/audio_processing/noise_suppression_impl.cc
index aa37e67..1501911 100644
--- a/webrtc/modules/audio_processing/noise_suppression_impl.cc
+++ b/webrtc/modules/audio_processing/noise_suppression_impl.cc
@@ -134,19 +134,11 @@
}
void* NoiseSuppressionImpl::CreateHandle() const {
- Handle* handle = NULL;
#if defined(WEBRTC_NS_FLOAT)
- if (WebRtcNs_Create(&handle) != apm_->kNoError)
+ return WebRtcNs_Create();
#elif defined(WEBRTC_NS_FIXED)
- if (WebRtcNsx_Create(&handle) != apm_->kNoError)
+ return WebRtcNsx_Create();
#endif
- {
- handle = NULL;
- } else {
- assert(handle != NULL);
- }
-
- return handle;
}
void NoiseSuppressionImpl::DestroyHandle(void* handle) const {
diff --git a/webrtc/modules/audio_processing/ns/include/noise_suppression.h b/webrtc/modules/audio_processing/ns/include/noise_suppression.h
index 14e686a..41bf9ac 100644
--- a/webrtc/modules/audio_processing/ns/include/noise_suppression.h
+++ b/webrtc/modules/audio_processing/ns/include/noise_suppression.h
@@ -20,20 +20,9 @@
#endif
/*
- * This function creates an instance to the noise suppression structure
- *
- * Input:
- * - NS_inst : Pointer to noise suppression instance that should be
- * created
- *
- * Output:
- * - NS_inst : Pointer to created noise suppression instance
- *
- * Return value : 0 - Ok
- * -1 - Error
+ * This function creates an instance of the floating point Noise Suppression.
*/
-int WebRtcNs_Create(NsHandle** NS_inst);
-
+NsHandle* WebRtcNs_Create();
/*
* This function frees the dynamic memory of a specified noise suppression
diff --git a/webrtc/modules/audio_processing/ns/include/noise_suppression_x.h b/webrtc/modules/audio_processing/ns/include/noise_suppression_x.h
index 736cb23..88fe4cd 100644
--- a/webrtc/modules/audio_processing/ns/include/noise_suppression_x.h
+++ b/webrtc/modules/audio_processing/ns/include/noise_suppression_x.h
@@ -20,20 +20,9 @@
#endif
/*
- * This function creates an instance to the noise reduction structure
- *
- * Input:
- * - nsxInst : Pointer to noise reduction instance that should be
- * created
- *
- * Output:
- * - nsxInst : Pointer to created noise reduction instance
- *
- * Return value : 0 - Ok
- * -1 - Error
+ * This function creates an instance of the fixed point Noise Suppression.
*/
-int WebRtcNsx_Create(NsxHandle** nsxInst);
-
+NsxHandle* WebRtcNsx_Create();
/*
* This function frees the dynamic memory of a specified Noise Suppression
diff --git a/webrtc/modules/audio_processing/ns/noise_suppression.c b/webrtc/modules/audio_processing/ns/noise_suppression.c
index 0efbebc..cdecd62 100644
--- a/webrtc/modules/audio_processing/ns/noise_suppression.c
+++ b/webrtc/modules/audio_processing/ns/noise_suppression.c
@@ -17,15 +17,10 @@
#include "webrtc/modules/audio_processing/ns/defines.h"
#include "webrtc/modules/audio_processing/ns/ns_core.h"
-int WebRtcNs_Create(NsHandle** NS_inst) {
- *NS_inst = (NsHandle*)malloc(sizeof(NoiseSuppressionC));
- if (*NS_inst != NULL) {
- (*(NoiseSuppressionC**)NS_inst)->initFlag = 0;
- return 0;
- } else {
- return -1;
- }
-
+NsHandle* WebRtcNs_Create() {
+ NoiseSuppressionC* self = malloc(sizeof(NoiseSuppressionC));
+ self->initFlag = 0;
+ return (NsHandle*)self;
}
void WebRtcNs_Free(NsHandle* NS_inst) {
diff --git a/webrtc/modules/audio_processing/ns/noise_suppression_x.c b/webrtc/modules/audio_processing/ns/noise_suppression_x.c
index a3b6d0f..150fe60 100644
--- a/webrtc/modules/audio_processing/ns/noise_suppression_x.c
+++ b/webrtc/modules/audio_processing/ns/noise_suppression_x.c
@@ -16,19 +16,12 @@
#include "webrtc/modules/audio_processing/ns/nsx_core.h"
#include "webrtc/modules/audio_processing/ns/nsx_defines.h"
-int WebRtcNsx_Create(NsxHandle** nsxInst) {
+NsxHandle* WebRtcNsx_Create() {
NoiseSuppressionFixedC* self = malloc(sizeof(NoiseSuppressionFixedC));
- *nsxInst = (NsxHandle*)self;
-
- if (self != NULL) {
- WebRtcSpl_Init();
- self->real_fft = NULL;
- self->initFlag = 0;
- return 0;
- } else {
- return -1;
- }
-
+ WebRtcSpl_Init();
+ self->real_fft = NULL;
+ self->initFlag = 0;
+ return (NsxHandle*)self;
}
void WebRtcNsx_Free(NsxHandle* nsxInst) {