blob: bcc40e107fff2bf44a8e954611134e70f2641208 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:251/*
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.org7270a6b2011-12-28 08:44:1711// 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.com470e71d2011-07-07 08:21:2513
Mirko Bonadei92ea95e2017-09-15 04:47:3114#ifndef COMMON_AUDIO_RING_BUFFER_H_
15#define COMMON_AUDIO_RING_BUFFER_H_
andrew@webrtc.org6b630152015-01-15 00:09:5316
Alessio Bazzicad4161a32018-08-31 08:41:3717// TODO(alessiob): Used by AEC, AECm and AudioRingBuffer. Remove when possible.
18
andrew@webrtc.org6b630152015-01-15 00:09:5319#ifdef __cplusplus
20extern "C" {
21#endif
niklase@google.com470e71d2011-07-07 08:21:2522
andrew@webrtc.org9ae13542013-02-25 17:07:3523#include <stddef.h> // size_t
niklase@google.com470e71d2011-07-07 08:21:2524
peah4d234472016-04-11 05:51:0725enum Wrap { SAME_WRAP, DIFF_WRAP };
26
27typedef 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;
andrew@webrtc.org9ae13542013-02-25 17:07:3535
deadbeef922246a2017-02-26 12:18:1236// Creates and initializes the buffer. Returns null on failure.
andrew@webrtc.org91f32552013-02-27 00:35:0637RingBuffer* WebRtc_CreateBuffer(size_t element_count, size_t element_size);
andrew@webrtc.org6b630152015-01-15 00:09:5338void WebRtc_InitBuffer(RingBuffer* handle);
andrew@webrtc.org9ae13542013-02-25 17:07:3539void WebRtc_FreeBuffer(void* handle);
bjornv@webrtc.org7270a6b2011-12-28 08:44:1740
saza5de06802017-07-10 08:01:0941// 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().
andrew@webrtc.org9ae13542013-02-25 17:07:3548//
deadbeef922246a2017-02-26 12:18:1249// To force a copying to |data|, pass a null |data_ptr|.
andrew@webrtc.org9ae13542013-02-25 17:07:3550//
bjornv@webrtc.org7270a6b2011-12-28 08:44:1751// Returns number of elements read.
andrew@webrtc.org9ae13542013-02-25 17:07:3552size_t WebRtc_ReadBuffer(RingBuffer* handle,
bjornv@webrtc.org7270a6b2011-12-28 08:44:1753 void** data_ptr,
54 void* data,
55 size_t element_count);
56
57// Writes |data| to buffer and returns the number of elements written.
Yves Gerey665174f2018-06-19 13:03:0558size_t WebRtc_WriteBuffer(RingBuffer* handle,
59 const void* data,
andrew@webrtc.org9ae13542013-02-25 17:07:3560 size_t element_count);
bjornv@webrtc.org7270a6b2011-12-28 08:44:1761
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.
andrew@webrtc.org9ae13542013-02-25 17:07:3567int WebRtc_MoveReadPtr(RingBuffer* handle, int element_count);
bjornv@webrtc.org7270a6b2011-12-28 08:44:1768
69// Returns number of available elements to read.
andrew@webrtc.org9ae13542013-02-25 17:07:3570size_t WebRtc_available_read(const RingBuffer* handle);
bjornv@webrtc.org7270a6b2011-12-28 08:44:1771
72// Returns number of available elements for write.
andrew@webrtc.org9ae13542013-02-25 17:07:3573size_t WebRtc_available_write(const RingBuffer* handle);
bjornv@webrtc.org7270a6b2011-12-28 08:44:1774
andrew@webrtc.org6b630152015-01-15 00:09:5375#ifdef __cplusplus
76}
77#endif
78
Mirko Bonadei92ea95e2017-09-15 04:47:3179#endif // COMMON_AUDIO_RING_BUFFER_H_