blob: 0bbe879980d8a2772a320877bc83c324e79d2b00 [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
17#ifdef __cplusplus
18extern "C" {
19#endif
niklase@google.com470e71d2011-07-07 08:21:25 +000020
andrew@webrtc.org9ae13542013-02-25 17:07:35 +000021#include <stddef.h> // size_t
niklase@google.com470e71d2011-07-07 08:21:25 +000022
peah4d234472016-04-10 22:51:07 -070023enum Wrap { SAME_WRAP, DIFF_WRAP };
24
25typedef 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.org9ae13542013-02-25 17:07:35 +000033
deadbeef922246a2017-02-26 04:18:12 -080034// Creates and initializes the buffer. Returns null on failure.
andrew@webrtc.org91f32552013-02-27 00:35:06 +000035RingBuffer* WebRtc_CreateBuffer(size_t element_count, size_t element_size);
andrew@webrtc.org6b630152015-01-15 00:09:53 +000036void WebRtc_InitBuffer(RingBuffer* handle);
andrew@webrtc.org9ae13542013-02-25 17:07:35 +000037void WebRtc_FreeBuffer(void* handle);
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +000038
saza5de06802017-07-10 01:01:09 -070039// 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.org9ae13542013-02-25 17:07:35 +000046//
deadbeef922246a2017-02-26 04:18:12 -080047// To force a copying to |data|, pass a null |data_ptr|.
andrew@webrtc.org9ae13542013-02-25 17:07:35 +000048//
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +000049// Returns number of elements read.
andrew@webrtc.org9ae13542013-02-25 17:07:35 +000050size_t WebRtc_ReadBuffer(RingBuffer* handle,
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +000051 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 Gerey665174f2018-06-19 15:03:05 +020056size_t WebRtc_WriteBuffer(RingBuffer* handle,
57 const void* data,
andrew@webrtc.org9ae13542013-02-25 17:07:35 +000058 size_t element_count);
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +000059
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.org9ae13542013-02-25 17:07:35 +000065int WebRtc_MoveReadPtr(RingBuffer* handle, int element_count);
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +000066
67// Returns number of available elements to read.
andrew@webrtc.org9ae13542013-02-25 17:07:35 +000068size_t WebRtc_available_read(const RingBuffer* handle);
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +000069
70// Returns number of available elements for write.
andrew@webrtc.org9ae13542013-02-25 17:07:35 +000071size_t WebRtc_available_write(const RingBuffer* handle);
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +000072
andrew@webrtc.org6b630152015-01-15 00:09:53 +000073#ifdef __cplusplus
74}
75#endif
76
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020077#endif // COMMON_AUDIO_RING_BUFFER_H_