blob: bcc40e107fff2bf44a8e954611134e70f2641208 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
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:17 +000011// 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:25 +000013
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020014#ifndef COMMON_AUDIO_RING_BUFFER_H_
15#define COMMON_AUDIO_RING_BUFFER_H_
andrew@webrtc.org6b630152015-01-15 00:09:53 +000016
Alessio Bazzicad4161a32018-08-31 10:41:37 +020017// TODO(alessiob): Used by AEC, AECm and AudioRingBuffer. Remove when possible.
18
andrew@webrtc.org6b630152015-01-15 00:09:53 +000019#ifdef __cplusplus
20extern "C" {
21#endif
niklase@google.com470e71d2011-07-07 08:21:25 +000022
andrew@webrtc.org9ae13542013-02-25 17:07:35 +000023#include <stddef.h> // size_t
niklase@google.com470e71d2011-07-07 08:21:25 +000024
peah4d234472016-04-10 22:51:07 -070025enum 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:35 +000035
deadbeef922246a2017-02-26 04:18:12 -080036// Creates and initializes the buffer. Returns null on failure.
andrew@webrtc.org91f32552013-02-27 00:35:06 +000037RingBuffer* WebRtc_CreateBuffer(size_t element_count, size_t element_size);
andrew@webrtc.org6b630152015-01-15 00:09:53 +000038void WebRtc_InitBuffer(RingBuffer* handle);
andrew@webrtc.org9ae13542013-02-25 17:07:35 +000039void WebRtc_FreeBuffer(void* handle);
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +000040
saza5de06802017-07-10 01:01:09 -070041// 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:35 +000048//
deadbeef922246a2017-02-26 04:18:12 -080049// To force a copying to |data|, pass a null |data_ptr|.
andrew@webrtc.org9ae13542013-02-25 17:07:35 +000050//
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +000051// Returns number of elements read.
andrew@webrtc.org9ae13542013-02-25 17:07:35 +000052size_t WebRtc_ReadBuffer(RingBuffer* handle,
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +000053 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 15:03:05 +020058size_t WebRtc_WriteBuffer(RingBuffer* handle,
59 const void* data,
andrew@webrtc.org9ae13542013-02-25 17:07:35 +000060 size_t element_count);
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +000061
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:35 +000067int WebRtc_MoveReadPtr(RingBuffer* handle, int element_count);
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +000068
69// Returns number of available elements to read.
andrew@webrtc.org9ae13542013-02-25 17:07:35 +000070size_t WebRtc_available_read(const RingBuffer* handle);
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +000071
72// Returns number of available elements for write.
andrew@webrtc.org9ae13542013-02-25 17:07:35 +000073size_t WebRtc_available_write(const RingBuffer* handle);
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +000074
andrew@webrtc.org6b630152015-01-15 00:09:53 +000075#ifdef __cplusplus
76}
77#endif
78
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020079#endif // COMMON_AUDIO_RING_BUFFER_H_