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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 14 | #ifndef COMMON_AUDIO_RING_BUFFER_H_ |
| 15 | #define 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 | |
peah | 4d23447 | 2016-04-10 22:51:07 -0700 | [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 | 9ae1354 | 2013-02-25 17:07:35 +0000 | [diff] [blame] | 33 | |
deadbeef | 922246a | 2017-02-26 04:18:12 -0800 | [diff] [blame] | 34 | // Creates and initializes the buffer. Returns null on failure. |
andrew@webrtc.org | 91f3255 | 2013-02-27 00:35:06 +0000 | [diff] [blame] | 35 | RingBuffer* WebRtc_CreateBuffer(size_t element_count, size_t element_size); |
andrew@webrtc.org | 6b63015 | 2015-01-15 00:09:53 +0000 | [diff] [blame] | 36 | void WebRtc_InitBuffer(RingBuffer* handle); |
andrew@webrtc.org | 9ae1354 | 2013-02-25 17:07:35 +0000 | [diff] [blame] | 37 | void WebRtc_FreeBuffer(void* handle); |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 38 | |
saza | 5de0680 | 2017-07-10 01:01:09 -0700 | [diff] [blame] | 39 | // Reads data from the buffer. Returns the number of elements that were read. |
| 40 | // The |data_ptr| will point to the address where the read data is located. |
| 41 | // If no data can be read, |data_ptr| is set to |NULL|. If all data can be read |
| 42 | // without buffer wrap around then |data_ptr| will point to the location in the |
| 43 | // buffer. Otherwise, the data will be copied to |data| (memory allocation done |
| 44 | // by the user) and |data_ptr| points to the address of |data|. |data_ptr| is |
| 45 | // only guaranteed to be valid until the next call to WebRtc_WriteBuffer(). |
andrew@webrtc.org | 9ae1354 | 2013-02-25 17:07:35 +0000 | [diff] [blame] | 46 | // |
deadbeef | 922246a | 2017-02-26 04:18:12 -0800 | [diff] [blame] | 47 | // To force a copying to |data|, pass a null |data_ptr|. |
andrew@webrtc.org | 9ae1354 | 2013-02-25 17:07:35 +0000 | [diff] [blame] | 48 | // |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 49 | // Returns number of elements read. |
andrew@webrtc.org | 9ae1354 | 2013-02-25 17:07:35 +0000 | [diff] [blame] | 50 | size_t WebRtc_ReadBuffer(RingBuffer* handle, |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 51 | void** data_ptr, |
| 52 | void* data, |
| 53 | size_t element_count); |
| 54 | |
| 55 | // Writes |data| to buffer and returns the number of elements written. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 56 | size_t WebRtc_WriteBuffer(RingBuffer* handle, |
| 57 | const void* data, |
andrew@webrtc.org | 9ae1354 | 2013-02-25 17:07:35 +0000 | [diff] [blame] | 58 | size_t element_count); |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 59 | |
| 60 | // Moves the buffer read position and returns the number of elements moved. |
| 61 | // Positive |element_count| moves the read position towards the write position, |
| 62 | // that is, flushing the buffer. Negative |element_count| moves the read |
| 63 | // position away from the the write position, that is, stuffing the buffer. |
| 64 | // Returns number of elements moved. |
andrew@webrtc.org | 9ae1354 | 2013-02-25 17:07:35 +0000 | [diff] [blame] | 65 | int WebRtc_MoveReadPtr(RingBuffer* handle, int element_count); |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 66 | |
| 67 | // Returns number of available elements to read. |
andrew@webrtc.org | 9ae1354 | 2013-02-25 17:07:35 +0000 | [diff] [blame] | 68 | size_t WebRtc_available_read(const RingBuffer* handle); |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 69 | |
| 70 | // Returns number of available elements for write. |
andrew@webrtc.org | 9ae1354 | 2013-02-25 17:07:35 +0000 | [diff] [blame] | 71 | size_t WebRtc_available_write(const RingBuffer* handle); |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 72 | |
andrew@webrtc.org | 6b63015 | 2015-01-15 00:09:53 +0000 | [diff] [blame] | 73 | #ifdef __cplusplus |
| 74 | } |
| 75 | #endif |
| 76 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 77 | #endif // COMMON_AUDIO_RING_BUFFER_H_ |