blob: ee9a0c67863ad51d30b10ec66f097c1de7815910 [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
14#ifndef WEBRTC_MODULES_AUDIO_PROCESSING_UTILITY_RING_BUFFER_H_
15#define WEBRTC_MODULES_AUDIO_PROCESSING_UTILITY_RING_BUFFER_H_
16
andrew@webrtc.org9ae13542013-02-25 17:07:35 +000017#include <stddef.h> // size_t
niklase@google.com470e71d2011-07-07 08:21:25 +000018
andrew@webrtc.org9ae13542013-02-25 17:07:35 +000019typedef struct RingBuffer RingBuffer;
20
21int WebRtc_CreateBuffer(RingBuffer** handle,
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +000022 size_t element_count,
23 size_t element_size);
andrew@webrtc.org9ae13542013-02-25 17:07:35 +000024int WebRtc_InitBuffer(RingBuffer* handle);
25void WebRtc_FreeBuffer(void* handle);
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +000026
27// Reads data from the buffer. The |data_ptr| will point to the address where
28// it is located. If all |element_count| data are feasible to read without
29// buffer wrap around |data_ptr| will point to the location in the buffer.
30// Otherwise, the data will be copied to |data| (memory allocation done by the
31// user) and |data_ptr| points to the address of |data|. |data_ptr| is only
32// guaranteed to be valid until the next call to WebRtc_WriteBuffer().
andrew@webrtc.org9ae13542013-02-25 17:07:35 +000033//
34// To force a copying to |data|, pass a NULL |data_ptr|.
35//
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +000036// Returns number of elements read.
andrew@webrtc.org9ae13542013-02-25 17:07:35 +000037size_t WebRtc_ReadBuffer(RingBuffer* handle,
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +000038 void** data_ptr,
39 void* data,
40 size_t element_count);
41
42// Writes |data| to buffer and returns the number of elements written.
andrew@webrtc.org9ae13542013-02-25 17:07:35 +000043size_t WebRtc_WriteBuffer(RingBuffer* handle, const void* data,
44 size_t element_count);
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +000045
46// Moves the buffer read position and returns the number of elements moved.
47// Positive |element_count| moves the read position towards the write position,
48// that is, flushing the buffer. Negative |element_count| moves the read
49// position away from the the write position, that is, stuffing the buffer.
50// Returns number of elements moved.
andrew@webrtc.org9ae13542013-02-25 17:07:35 +000051int WebRtc_MoveReadPtr(RingBuffer* handle, int element_count);
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +000052
53// Returns number of available elements to read.
andrew@webrtc.org9ae13542013-02-25 17:07:35 +000054size_t WebRtc_available_read(const RingBuffer* handle);
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +000055
56// Returns number of available elements for write.
andrew@webrtc.org9ae13542013-02-25 17:07:35 +000057size_t WebRtc_available_write(const RingBuffer* handle);
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +000058
andrew@webrtc.org9ae13542013-02-25 17:07:35 +000059#endif // WEBRTC_MODULES_AUDIO_PROCESSING_UTILITY_RING_BUFFER_H_