1 /* 2 * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved. 3 * 4 * Use of this source code is governed by a BSD-style license 5 * that can be found in the LICENSE file in the root of the source 6 * tree. An additional intellectual property rights grant can be found 7 * in the file PATENTS. All contributing project authors may 8 * be found in the AUTHORS file in the root of the source tree. 9 */ 10 11 // A ring buffer to hold arbitrary data. Provides no thread safety. Unless 12 // otherwise specified, functions return 0 on success and -1 on error. 13 14 #ifndef COMMON_AUDIO_RING_BUFFER_H_ 15 #define COMMON_AUDIO_RING_BUFFER_H_ 16 17 // TODO(alessiob): Used by AEC, AECm and AudioRingBuffer. Remove when possible. 18 19 #ifdef __cplusplus 20 extern "C" { 21 #endif 22 23 #include <stddef.h> // size_t 24 25 enum Wrap { SAME_WRAP, DIFF_WRAP }; 26 27 typedef struct RingBuffer { 28 size_t read_pos; 29 size_t write_pos; 30 size_t element_count; 31 size_t element_size; 32 enum Wrap rw_wrap; 33 char* data; 34 } RingBuffer; 35 36 // Creates and initializes the buffer. Returns null on failure. 37 RingBuffer* WebRtc_CreateBuffer(size_t element_count, size_t element_size); 38 void WebRtc_InitBuffer(RingBuffer* handle); 39 void WebRtc_FreeBuffer(void* handle); 40 41 // Reads data from the buffer. Returns the number of elements that were read. 42 // The |data_ptr| will point to the address where the read data is located. 43 // If no data can be read, |data_ptr| is set to |NULL|. If all data can be read 44 // without buffer wrap around then |data_ptr| will point to the location in the 45 // buffer. Otherwise, the data will be copied to |data| (memory allocation done 46 // by the user) and |data_ptr| points to the address of |data|. |data_ptr| is 47 // only guaranteed to be valid until the next call to WebRtc_WriteBuffer(). 48 // 49 // To force a copying to |data|, pass a null |data_ptr|. 50 // 51 // Returns number of elements read. 52 size_t WebRtc_ReadBuffer(RingBuffer* handle, 53 void** data_ptr, 54 void* data, 55 size_t element_count); 56 57 // Writes |data| to buffer and returns the number of elements written. 58 size_t WebRtc_WriteBuffer(RingBuffer* handle, 59 const void* data, 60 size_t element_count); 61 62 // Moves the buffer read position and returns the number of elements moved. 63 // Positive |element_count| moves the read position towards the write position, 64 // that is, flushing the buffer. Negative |element_count| moves the read 65 // position away from the the write position, that is, stuffing the buffer. 66 // Returns number of elements moved. 67 int WebRtc_MoveReadPtr(RingBuffer* handle, int element_count); 68 69 // Returns number of available elements to read. 70 size_t WebRtc_available_read(const RingBuffer* handle); 71 72 // Returns number of available elements for write. 73 size_t WebRtc_available_write(const RingBuffer* handle); 74 75 #ifdef __cplusplus 76 } 77 #endif 78 79 #endif // COMMON_AUDIO_RING_BUFFER_H_ 80