andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [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 | |
| 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 | |
peah | 31b44f2 | 2016-04-05 21:57:48 | [diff] [blame] | 14 | #ifndef WEBRTC_COMMON_AUDIO_RING_BUFFER_H_ |
| 15 | #define WEBRTC_COMMON_AUDIO_RING_BUFFER_H_ |
andrew@webrtc.org | 9441541 | 2015-01-15 00:09:53 | [diff] [blame] | 16 | |
| 17 | #ifdef __cplusplus |
| 18 | extern "C" { |
| 19 | #endif |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 20 | |
andrew@webrtc.org | b3e5a62 | 2013-02-25 17:07:35 | [diff] [blame] | 21 | #include <stddef.h> // size_t |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 22 | |
peah | bb916fc | 2016-04-11 05:51:07 | [diff] [blame] | 23 | enum Wrap { SAME_WRAP, DIFF_WRAP }; |
| 24 | |
| 25 | typedef struct RingBuffer { |
| 26 | size_t read_pos; |
| 27 | size_t write_pos; |
| 28 | size_t element_count; |
| 29 | size_t element_size; |
| 30 | enum Wrap rw_wrap; |
| 31 | char* data; |
| 32 | } RingBuffer; |
andrew@webrtc.org | b3e5a62 | 2013-02-25 17:07:35 | [diff] [blame] | 33 | |
andrew@webrtc.org | a03359c | 2015-01-26 21:23:53 | [diff] [blame] | 34 | // Creates and initializes the buffer. Returns NULL on failure. |
andrew@webrtc.org | db32d60 | 2013-02-27 00:35:06 | [diff] [blame] | 35 | RingBuffer* WebRtc_CreateBuffer(size_t element_count, size_t element_size); |
andrew@webrtc.org | 9441541 | 2015-01-15 00:09:53 | [diff] [blame] | 36 | void WebRtc_InitBuffer(RingBuffer* handle); |
andrew@webrtc.org | b3e5a62 | 2013-02-25 17:07:35 | [diff] [blame] | 37 | void WebRtc_FreeBuffer(void* handle); |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 38 | |
| 39 | // Reads data from the buffer. The |data_ptr| will point to the address where |
| 40 | // it is located. If all |element_count| data are feasible to read without |
| 41 | // buffer wrap around |data_ptr| will point to the location in the buffer. |
| 42 | // Otherwise, the data will be copied to |data| (memory allocation done by the |
| 43 | // user) and |data_ptr| points to the address of |data|. |data_ptr| is only |
| 44 | // guaranteed to be valid until the next call to WebRtc_WriteBuffer(). |
andrew@webrtc.org | b3e5a62 | 2013-02-25 17:07:35 | [diff] [blame] | 45 | // |
| 46 | // To force a copying to |data|, pass a NULL |data_ptr|. |
| 47 | // |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 48 | // Returns number of elements read. |
andrew@webrtc.org | b3e5a62 | 2013-02-25 17:07:35 | [diff] [blame] | 49 | size_t WebRtc_ReadBuffer(RingBuffer* handle, |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 50 | void** data_ptr, |
| 51 | void* data, |
| 52 | size_t element_count); |
| 53 | |
| 54 | // Writes |data| to buffer and returns the number of elements written. |
andrew@webrtc.org | b3e5a62 | 2013-02-25 17:07:35 | [diff] [blame] | 55 | size_t WebRtc_WriteBuffer(RingBuffer* handle, const void* data, |
| 56 | size_t element_count); |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 57 | |
| 58 | // Moves the buffer read position and returns the number of elements moved. |
| 59 | // Positive |element_count| moves the read position towards the write position, |
| 60 | // that is, flushing the buffer. Negative |element_count| moves the read |
| 61 | // position away from the the write position, that is, stuffing the buffer. |
| 62 | // Returns number of elements moved. |
andrew@webrtc.org | b3e5a62 | 2013-02-25 17:07:35 | [diff] [blame] | 63 | int WebRtc_MoveReadPtr(RingBuffer* handle, int element_count); |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 64 | |
| 65 | // Returns number of available elements to read. |
andrew@webrtc.org | b3e5a62 | 2013-02-25 17:07:35 | [diff] [blame] | 66 | size_t WebRtc_available_read(const RingBuffer* handle); |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 67 | |
| 68 | // Returns number of available elements for write. |
andrew@webrtc.org | b3e5a62 | 2013-02-25 17:07:35 | [diff] [blame] | 69 | size_t WebRtc_available_write(const RingBuffer* handle); |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 70 | |
andrew@webrtc.org | 9441541 | 2015-01-15 00:09:53 | [diff] [blame] | 71 | #ifdef __cplusplus |
| 72 | } |
| 73 | #endif |
| 74 | |
peah | 31b44f2 | 2016-04-05 21:57:48 | [diff] [blame] | 75 | #endif // WEBRTC_COMMON_AUDIO_RING_BUFFER_H_ |