Move ring_buffer to common_audio.

In preparation for adding a C++ wrapper in common_audio. Also, change
the return type of Init to void and call it from Create.

R=aluebs@webrtc.org

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

git-svn-id: http://webrtc.googlecode.com/svn/trunk@8068 4adac7df-926f-26a2-2b94-8c16560cd09d
diff --git a/webrtc/common_audio/BUILD.gn b/webrtc/common_audio/BUILD.gn
index 1a077c6..ac09f96 100644
--- a/webrtc/common_audio/BUILD.gn
+++ b/webrtc/common_audio/BUILD.gn
@@ -37,6 +37,8 @@
     "resampler/resampler.cc",
     "resampler/sinc_resampler.cc",
     "resampler/sinc_resampler.h",
+    "ring_buffer.c",
+    "ring_buffer.h",
     "signal_processing/include/real_fft.h",
     "signal_processing/include/signal_processing_library.h",
     "signal_processing/include/spl_inl.h",
diff --git a/webrtc/common_audio/common_audio.gyp b/webrtc/common_audio/common_audio.gyp
index 6b0a1a3..056013b 100644
--- a/webrtc/common_audio/common_audio.gyp
+++ b/webrtc/common_audio/common_audio.gyp
@@ -32,6 +32,7 @@
         'audio_converter.cc',
         'audio_converter.h',
         'audio_util.cc',
+        'audio_util.h',
         'blocker.cc',
         'blocker.h',
         'fir_filter.cc',
@@ -47,6 +48,8 @@
         'resampler/resampler.cc',
         'resampler/sinc_resampler.cc',
         'resampler/sinc_resampler.h',
+        'ring_buffer.c',
+        'ring_buffer.h',
         'signal_processing/include/real_fft.h',
         'signal_processing/include/signal_processing_library.h',
         'signal_processing/include/spl_inl.h',
@@ -235,6 +238,7 @@
             'resampler/sinc_resampler_unittest.cc',
             'resampler/sinusoidal_linear_chirp_source.cc',
             'resampler/sinusoidal_linear_chirp_source.h',
+            'ring_buffer_unittest.cc',
             'signal_processing/real_fft_unittest.cc',
             'signal_processing/signal_processing_unittest.cc',
             'vad/vad_core_unittest.cc',
diff --git a/webrtc/modules/audio_processing/utility/ring_buffer.c b/webrtc/common_audio/ring_buffer.c
similarity index 97%
rename from webrtc/modules/audio_processing/utility/ring_buffer.c
rename to webrtc/common_audio/ring_buffer.c
index f65c6fb..60fb5df 100644
--- a/webrtc/modules/audio_processing/utility/ring_buffer.c
+++ b/webrtc/common_audio/ring_buffer.c
@@ -11,7 +11,7 @@
 // A ring buffer to hold arbitrary data. Provides no thread safety. Unless
 // otherwise specified, functions return 0 on success and -1 on error.
 
-#include "webrtc/modules/audio_processing/utility/ring_buffer.h"
+#include "webrtc/common_audio/ring_buffer.h"
 
 #include <stddef.h>  // size_t
 #include <stdlib.h>
@@ -85,23 +85,18 @@
 
   self->element_count = element_count;
   self->element_size = element_size;
+  WebRtc_InitBuffer(self);
 
   return self;
 }
 
-int WebRtc_InitBuffer(RingBuffer* self) {
-  if (!self) {
-    return -1;
-  }
-
+void WebRtc_InitBuffer(RingBuffer* self) {
   self->read_pos = 0;
   self->write_pos = 0;
   self->rw_wrap = SAME_WRAP;
 
   // Initialize buffer to zeros
   memset(self->data, 0, self->element_count * self->element_size);
-
-  return 0;
 }
 
 void WebRtc_FreeBuffer(void* handle) {
diff --git a/webrtc/modules/audio_processing/utility/ring_buffer.h b/webrtc/common_audio/ring_buffer.h
similarity index 90%
rename from webrtc/modules/audio_processing/utility/ring_buffer.h
rename to webrtc/common_audio/ring_buffer.h
index 61d25ba..861b8ac 100644
--- a/webrtc/modules/audio_processing/utility/ring_buffer.h
+++ b/webrtc/common_audio/ring_buffer.h
@@ -11,8 +11,12 @@
 // A ring buffer to hold arbitrary data. Provides no thread safety. Unless
 // otherwise specified, functions return 0 on success and -1 on error.
 
-#ifndef WEBRTC_MODULES_AUDIO_PROCESSING_UTILITY_RING_BUFFER_H_
-#define WEBRTC_MODULES_AUDIO_PROCESSING_UTILITY_RING_BUFFER_H_
+#ifndef WEBRTC_COMMON_AUDIO_RING_BUFFER_H_
+#define WEBRTC_COMMON_AUDIO_RING_BUFFER_H_
+
+#ifdef __cplusplus
+extern "C" {
+#endif
 
 #include <stddef.h>  // size_t
 
@@ -20,7 +24,7 @@
 
 // Returns NULL on failure.
 RingBuffer* WebRtc_CreateBuffer(size_t element_count, size_t element_size);
-int WebRtc_InitBuffer(RingBuffer* handle);
+void WebRtc_InitBuffer(RingBuffer* handle);
 void WebRtc_FreeBuffer(void* handle);
 
 // Reads data from the buffer. The |data_ptr| will point to the address where
@@ -55,4 +59,8 @@
 // Returns number of available elements for write.
 size_t WebRtc_available_write(const RingBuffer* handle);
 
-#endif  // WEBRTC_MODULES_AUDIO_PROCESSING_UTILITY_RING_BUFFER_H_
+#ifdef __cplusplus
+}
+#endif
+
+#endif  // WEBRTC_COMMON_AUDIO_RING_BUFFER_H_
diff --git a/webrtc/modules/audio_processing/utility/ring_buffer_unittest.cc b/webrtc/common_audio/ring_buffer_unittest.cc
similarity index 95%
rename from webrtc/modules/audio_processing/utility/ring_buffer_unittest.cc
rename to webrtc/common_audio/ring_buffer_unittest.cc
index f5c36c2..e1043bd 100644
--- a/webrtc/modules/audio_processing/utility/ring_buffer_unittest.cc
+++ b/webrtc/common_audio/ring_buffer_unittest.cc
@@ -8,14 +8,11 @@
  *  be found in the AUTHORS file in the root of the source tree.
  */
 
-// TODO(ajm): Make this a comprehensive test.
-
-extern "C" {
-#include "webrtc/modules/audio_processing/utility/ring_buffer.h"
-}
+#include "webrtc/common_audio/ring_buffer.h"
 
 #include <stdlib.h>
 #include <time.h>
+#include <algorithm>
 
 #include "testing/gtest/include/gtest/gtest.h"
 #include "webrtc/system_wrappers/interface/scoped_ptr.h"
@@ -65,7 +62,7 @@
     scoped_ptr<int[]> read_data(new int[buffer_size]);
     scoped_ring_buffer buffer(WebRtc_CreateBuffer(buffer_size, sizeof(int)));
     ASSERT_TRUE(buffer.get() != NULL);
-    ASSERT_EQ(0, WebRtc_InitBuffer(buffer.get()));
+    WebRtc_InitBuffer(buffer.get());
     int buffer_consumed = 0;
     int write_element = 0;
     int read_element = 0;
@@ -123,7 +120,7 @@
 
   scoped_ring_buffer buffer(WebRtc_CreateBuffer(kDataSize, sizeof(int)));
   ASSERT_TRUE(buffer.get() != NULL);
-  ASSERT_EQ(0, WebRtc_InitBuffer(buffer.get()));
+  WebRtc_InitBuffer(buffer.get());
 
   SetIncrementingData(write_data, kDataSize, 0);
   EXPECT_EQ(kDataSize, WebRtc_WriteBuffer(buffer.get(), write_data, kDataSize));
diff --git a/webrtc/modules/audio_processing/BUILD.gn b/webrtc/modules/audio_processing/BUILD.gn
index 97a212e..6e968d5 100644
--- a/webrtc/modules/audio_processing/BUILD.gn
+++ b/webrtc/modules/audio_processing/BUILD.gn
@@ -120,8 +120,6 @@
     "utility/delay_estimator_wrapper.h",
     "utility/fft4g.c",
     "utility/fft4g.h",
-    "utility/ring_buffer.c",
-    "utility/ring_buffer.h",
     "voice_detection_impl.cc",
     "voice_detection_impl.h",
   ]
diff --git a/webrtc/modules/audio_processing/aec/aec_core.c b/webrtc/modules/audio_processing/aec/aec_core.c
index 61f9e56..ef04997 100644
--- a/webrtc/modules/audio_processing/aec/aec_core.c
+++ b/webrtc/modules/audio_processing/aec/aec_core.c
@@ -24,12 +24,12 @@
 #include <stdlib.h>
 #include <string.h>
 
+#include "webrtc/common_audio/ring_buffer.h"
 #include "webrtc/common_audio/signal_processing/include/signal_processing_library.h"
 #include "webrtc/modules/audio_processing/aec/aec_common.h"
 #include "webrtc/modules/audio_processing/aec/aec_core_internal.h"
 #include "webrtc/modules/audio_processing/aec/aec_rdft.h"
 #include "webrtc/modules/audio_processing/utility/delay_estimator_wrapper.h"
-#include "webrtc/modules/audio_processing/utility/ring_buffer.h"
 #include "webrtc/system_wrappers/interface/cpu_features_wrapper.h"
 #include "webrtc/typedefs.h"
 
@@ -1452,33 +1452,16 @@
     aec->normal_error_threshold = 1.5e-6f;
   }
 
-  if (WebRtc_InitBuffer(aec->nearFrBuf) == -1) {
-    return -1;
-  }
-
-  if (WebRtc_InitBuffer(aec->outFrBuf) == -1) {
-    return -1;
-  }
-
-  if (WebRtc_InitBuffer(aec->nearFrBufH) == -1) {
-    return -1;
-  }
-
-  if (WebRtc_InitBuffer(aec->outFrBufH) == -1) {
-    return -1;
-  }
+  WebRtc_InitBuffer(aec->nearFrBuf);
+  WebRtc_InitBuffer(aec->outFrBuf);
+  WebRtc_InitBuffer(aec->nearFrBufH);
+  WebRtc_InitBuffer(aec->outFrBufH);
 
   // Initialize far-end buffers.
-  if (WebRtc_InitBuffer(aec->far_buf) == -1) {
-    return -1;
-  }
-  if (WebRtc_InitBuffer(aec->far_buf_windowed) == -1) {
-    return -1;
-  }
+  WebRtc_InitBuffer(aec->far_buf);
+  WebRtc_InitBuffer(aec->far_buf_windowed);
 #ifdef WEBRTC_AEC_DEBUG_DUMP
-  if (WebRtc_InitBuffer(aec->far_time_buf) == -1) {
-    return -1;
-  }
+  WebRtc_InitBuffer(aec->far_time_buf);
   {
     int process_rate = sampFreq > 16000 ? 16000 : sampFreq;
     ReopenWav(&aec->farFile, "aec_far",
diff --git a/webrtc/modules/audio_processing/aec/aec_core_internal.h b/webrtc/modules/audio_processing/aec/aec_core_internal.h
index a8f0c0c..fd33acd7 100644
--- a/webrtc/modules/audio_processing/aec/aec_core_internal.h
+++ b/webrtc/modules/audio_processing/aec/aec_core_internal.h
@@ -11,10 +11,10 @@
 #ifndef WEBRTC_MODULES_AUDIO_PROCESSING_AEC_AEC_CORE_INTERNAL_H_
 #define WEBRTC_MODULES_AUDIO_PROCESSING_AEC_AEC_CORE_INTERNAL_H_
 
+#include "webrtc/common_audio/ring_buffer.h"
 #include "webrtc/common_audio/wav_file.h"
 #include "webrtc/modules/audio_processing/aec/aec_common.h"
 #include "webrtc/modules/audio_processing/aec/aec_core.h"
-#include "webrtc/modules/audio_processing/utility/ring_buffer.h"
 #include "webrtc/typedefs.h"
 
 // Number of partitions for the extended filter mode. The first one is an enum
diff --git a/webrtc/modules/audio_processing/aec/echo_cancellation.c b/webrtc/modules/audio_processing/aec/echo_cancellation.c
index c8b851c..99883c4 100644
--- a/webrtc/modules/audio_processing/aec/echo_cancellation.c
+++ b/webrtc/modules/audio_processing/aec/echo_cancellation.c
@@ -20,11 +20,11 @@
 #include <stdlib.h>
 #include <string.h>
 
+#include "webrtc/common_audio/ring_buffer.h"
 #include "webrtc/common_audio/signal_processing/include/signal_processing_library.h"
 #include "webrtc/modules/audio_processing/aec/aec_core.h"
 #include "webrtc/modules/audio_processing/aec/aec_resampler.h"
 #include "webrtc/modules/audio_processing/aec/echo_cancellation_internal.h"
-#include "webrtc/modules/audio_processing/utility/ring_buffer.h"
 #include "webrtc/typedefs.h"
 
 // Measured delays [ms]
@@ -222,10 +222,7 @@
     return -1;
   }
 
-  if (WebRtc_InitBuffer(aecpc->far_pre_buf) == -1) {
-    aecpc->lastError = AEC_UNSPECIFIED_ERROR;
-    return -1;
-  }
+  WebRtc_InitBuffer(aecpc->far_pre_buf);
   WebRtc_MoveReadPtr(aecpc->far_pre_buf, -PART_LEN);  // Start overlap.
 
   aecpc->initFlag = initCheck;  // indicates that initialization has been done
diff --git a/webrtc/modules/audio_processing/aec/echo_cancellation_internal.h b/webrtc/modules/audio_processing/aec/echo_cancellation_internal.h
index 0dad5fb..95a6cf3 100644
--- a/webrtc/modules/audio_processing/aec/echo_cancellation_internal.h
+++ b/webrtc/modules/audio_processing/aec/echo_cancellation_internal.h
@@ -11,8 +11,8 @@
 #ifndef WEBRTC_MODULES_AUDIO_PROCESSING_AEC_ECHO_CANCELLATION_INTERNAL_H_
 #define WEBRTC_MODULES_AUDIO_PROCESSING_AEC_ECHO_CANCELLATION_INTERNAL_H_
 
+#include "webrtc/common_audio/ring_buffer.h"
 #include "webrtc/modules/audio_processing/aec/aec_core.h"
-#include "webrtc/modules/audio_processing/utility/ring_buffer.h"
 
 typedef struct {
   int delayCtr;
diff --git a/webrtc/modules/audio_processing/aecm/aecm_core.c b/webrtc/modules/audio_processing/aecm/aecm_core.c
index 921b470..de8ca27 100644
--- a/webrtc/modules/audio_processing/aecm/aecm_core.c
+++ b/webrtc/modules/audio_processing/aecm/aecm_core.c
@@ -14,10 +14,10 @@
 #include <stddef.h>
 #include <stdlib.h>
 
+#include "webrtc/common_audio/ring_buffer.h"
 #include "webrtc/common_audio/signal_processing/include/real_fft.h"
 #include "webrtc/modules/audio_processing/aecm/include/echo_control_mobile.h"
 #include "webrtc/modules/audio_processing/utility/delay_estimator_wrapper.h"
-#include "webrtc/modules/audio_processing/utility/ring_buffer.h"
 #include "webrtc/system_wrappers/interface/compile_assert_c.h"
 #include "webrtc/system_wrappers/interface/cpu_features_wrapper.h"
 #include "webrtc/typedefs.h"
diff --git a/webrtc/modules/audio_processing/aecm/aecm_core.h b/webrtc/modules/audio_processing/aecm/aecm_core.h
index 7476570..03655b9 100644
--- a/webrtc/modules/audio_processing/aecm/aecm_core.h
+++ b/webrtc/modules/audio_processing/aecm/aecm_core.h
@@ -13,9 +13,9 @@
 #ifndef WEBRTC_MODULES_AUDIO_PROCESSING_AECM_AECM_CORE_H_
 #define WEBRTC_MODULES_AUDIO_PROCESSING_AECM_AECM_CORE_H_
 
+#include "webrtc/common_audio/ring_buffer.h"
 #include "webrtc/common_audio/signal_processing/include/signal_processing_library.h"
 #include "webrtc/modules/audio_processing/aecm/aecm_defines.h"
-#include "webrtc/modules/audio_processing/utility/ring_buffer.h"
 #include "webrtc/typedefs.h"
 
 #ifdef _MSC_VER  // visual c++
diff --git a/webrtc/modules/audio_processing/aecm/aecm_core_c.c b/webrtc/modules/audio_processing/aecm/aecm_core_c.c
index d35f760..c691938 100644
--- a/webrtc/modules/audio_processing/aecm/aecm_core_c.c
+++ b/webrtc/modules/audio_processing/aecm/aecm_core_c.c
@@ -14,10 +14,10 @@
 #include <stddef.h>
 #include <stdlib.h>
 
+#include "webrtc/common_audio/ring_buffer.h"
 #include "webrtc/common_audio/signal_processing/include/real_fft.h"
 #include "webrtc/modules/audio_processing/aecm/include/echo_control_mobile.h"
 #include "webrtc/modules/audio_processing/utility/delay_estimator_wrapper.h"
-#include "webrtc/modules/audio_processing/utility/ring_buffer.h"
 #include "webrtc/system_wrappers/interface/compile_assert_c.h"
 #include "webrtc/system_wrappers/interface/cpu_features_wrapper.h"
 #include "webrtc/typedefs.h"
diff --git a/webrtc/modules/audio_processing/aecm/echo_control_mobile.c b/webrtc/modules/audio_processing/aecm/echo_control_mobile.c
index 3a3125e..389433b 100644
--- a/webrtc/modules/audio_processing/aecm/echo_control_mobile.c
+++ b/webrtc/modules/audio_processing/aecm/echo_control_mobile.c
@@ -15,9 +15,9 @@
 #endif
 #include <stdlib.h>
 
+#include "webrtc/common_audio/ring_buffer.h"
 #include "webrtc/common_audio/signal_processing/include/signal_processing_library.h"
 #include "webrtc/modules/audio_processing/aecm/aecm_core.h"
-#include "webrtc/modules/audio_processing/utility/ring_buffer.h"
 
 #define BUF_SIZE_FRAMES 50 // buffer size (frames)
 // Maximum length of resampled signal. Must be an integer multiple of frames
@@ -182,11 +182,7 @@
     }
 
     // Initialize farend buffer
-    if (WebRtc_InitBuffer(aecm->farendBuf) == -1)
-    {
-        aecm->lastError = AECM_UNSPECIFIED_ERROR;
-        return -1;
-    }
+    WebRtc_InitBuffer(aecm->farendBuf);
 
     aecm->initFlag = kInitCheck; // indicates that initialization has been done
 
diff --git a/webrtc/modules/audio_processing/audio_processing.gypi b/webrtc/modules/audio_processing/audio_processing.gypi
index 664fb2d..c26054d 100644
--- a/webrtc/modules/audio_processing/audio_processing.gypi
+++ b/webrtc/modules/audio_processing/audio_processing.gypi
@@ -129,8 +129,6 @@
         'utility/delay_estimator_wrapper.h',
         'utility/fft4g.c',
         'utility/fft4g.h',
-        'utility/ring_buffer.c',
-        'utility/ring_buffer.h',
         'voice_detection_impl.cc',
         'voice_detection_impl.h',
       ],
diff --git a/webrtc/modules/modules.gyp b/webrtc/modules/modules.gyp
index dea0f4e..5d11aee 100644
--- a/webrtc/modules/modules.gyp
+++ b/webrtc/modules/modules.gyp
@@ -194,7 +194,6 @@
             'audio_processing/transient/wpd_node_unittest.cc',
             'audio_processing/transient/wpd_tree_unittest.cc',
             'audio_processing/utility/delay_estimator_unittest.cc',
-            'audio_processing/utility/ring_buffer_unittest.cc',
             'bitrate_controller/bitrate_controller_unittest.cc',
             'bitrate_controller/remb_suppressor_unittest.cc',
             'bitrate_controller/send_side_bandwidth_estimation_unittest.cc',