niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 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 | |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 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. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 13 | |
peah | faed4ab | 2016-04-05 14:57:48 -0700 | [diff] [blame^] | 14 | #ifndef WEBRTC_COMMON_AUDIO_RING_BUFFER_H_ |
| 15 | #define WEBRTC_COMMON_AUDIO_RING_BUFFER_H_ |
andrew@webrtc.org | 6b63015 | 2015-01-15 00:09:53 +0000 | [diff] [blame] | 16 | |
| 17 | #ifdef __cplusplus |
| 18 | extern "C" { |
| 19 | #endif |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 20 | |
andrew@webrtc.org | 9ae1354 | 2013-02-25 17:07:35 +0000 | [diff] [blame] | 21 | #include <stddef.h> // size_t |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 22 | |
andrew@webrtc.org | 9ae1354 | 2013-02-25 17:07:35 +0000 | [diff] [blame] | 23 | typedef struct RingBuffer RingBuffer; |
| 24 | |
andrew@webrtc.org | 041035b | 2015-01-26 21:23:53 +0000 | [diff] [blame] | 25 | // Creates and initializes the buffer. Returns NULL on failure. |
andrew@webrtc.org | 91f3255 | 2013-02-27 00:35:06 +0000 | [diff] [blame] | 26 | RingBuffer* WebRtc_CreateBuffer(size_t element_count, size_t element_size); |
andrew@webrtc.org | 6b63015 | 2015-01-15 00:09:53 +0000 | [diff] [blame] | 27 | void WebRtc_InitBuffer(RingBuffer* handle); |
andrew@webrtc.org | 9ae1354 | 2013-02-25 17:07:35 +0000 | [diff] [blame] | 28 | void WebRtc_FreeBuffer(void* handle); |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 29 | |
| 30 | // Reads data from the buffer. The |data_ptr| will point to the address where |
| 31 | // it is located. If all |element_count| data are feasible to read without |
| 32 | // buffer wrap around |data_ptr| will point to the location in the buffer. |
| 33 | // Otherwise, the data will be copied to |data| (memory allocation done by the |
| 34 | // user) and |data_ptr| points to the address of |data|. |data_ptr| is only |
| 35 | // guaranteed to be valid until the next call to WebRtc_WriteBuffer(). |
andrew@webrtc.org | 9ae1354 | 2013-02-25 17:07:35 +0000 | [diff] [blame] | 36 | // |
| 37 | // To force a copying to |data|, pass a NULL |data_ptr|. |
| 38 | // |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 39 | // Returns number of elements read. |
andrew@webrtc.org | 9ae1354 | 2013-02-25 17:07:35 +0000 | [diff] [blame] | 40 | size_t WebRtc_ReadBuffer(RingBuffer* handle, |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 41 | void** data_ptr, |
| 42 | void* data, |
| 43 | size_t element_count); |
| 44 | |
| 45 | // Writes |data| to buffer and returns the number of elements written. |
andrew@webrtc.org | 9ae1354 | 2013-02-25 17:07:35 +0000 | [diff] [blame] | 46 | size_t WebRtc_WriteBuffer(RingBuffer* handle, const void* data, |
| 47 | size_t element_count); |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 48 | |
| 49 | // Moves the buffer read position and returns the number of elements moved. |
| 50 | // Positive |element_count| moves the read position towards the write position, |
| 51 | // that is, flushing the buffer. Negative |element_count| moves the read |
| 52 | // position away from the the write position, that is, stuffing the buffer. |
| 53 | // Returns number of elements moved. |
andrew@webrtc.org | 9ae1354 | 2013-02-25 17:07:35 +0000 | [diff] [blame] | 54 | int WebRtc_MoveReadPtr(RingBuffer* handle, int element_count); |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 55 | |
| 56 | // Returns number of available elements to read. |
andrew@webrtc.org | 9ae1354 | 2013-02-25 17:07:35 +0000 | [diff] [blame] | 57 | size_t WebRtc_available_read(const RingBuffer* handle); |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 58 | |
| 59 | // Returns number of available elements for write. |
andrew@webrtc.org | 9ae1354 | 2013-02-25 17:07:35 +0000 | [diff] [blame] | 60 | size_t WebRtc_available_write(const RingBuffer* handle); |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 61 | |
andrew@webrtc.org | 6b63015 | 2015-01-15 00:09:53 +0000 | [diff] [blame] | 62 | #ifdef __cplusplus |
| 63 | } |
| 64 | #endif |
| 65 | |
peah | faed4ab | 2016-04-05 14:57:48 -0700 | [diff] [blame^] | 66 | #endif // WEBRTC_COMMON_AUDIO_RING_BUFFER_H_ |