1b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org/*
2b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org *  Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
3b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org *
4b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org *  Use of this source code is governed by a BSD-style license
5b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org *  that can be found in the LICENSE file in the root of the source
6b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org *  tree. An additional intellectual property rights grant can be found
7b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org *  in the file PATENTS.  All contributing project authors may
8b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org *  be found in the AUTHORS file in the root of the source tree.
9b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org */
10b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
11b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org// A ring buffer to hold arbitrary data. Provides no thread safety. Unless
12b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org// otherwise specified, functions return 0 on success and -1 on error.
13b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
14b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org#ifndef WEBRTC_MODULES_AUDIO_PROCESSING_UTILITY_RING_BUFFER_H_
15b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org#define WEBRTC_MODULES_AUDIO_PROCESSING_UTILITY_RING_BUFFER_H_
16b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
17b3e5a623285b082a23655cdc02f35a40f652acdfandrew@webrtc.org#include <stddef.h>  // size_t
18b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
19b3e5a623285b082a23655cdc02f35a40f652acdfandrew@webrtc.orgtypedef struct RingBuffer RingBuffer;
20b3e5a623285b082a23655cdc02f35a40f652acdfandrew@webrtc.org
21db32d60125dfe78a1eb475f39672484c63b034b1andrew@webrtc.org// Returns NULL on failure.
22db32d60125dfe78a1eb475f39672484c63b034b1andrew@webrtc.orgRingBuffer* WebRtc_CreateBuffer(size_t element_count, size_t element_size);
23b3e5a623285b082a23655cdc02f35a40f652acdfandrew@webrtc.orgint WebRtc_InitBuffer(RingBuffer* handle);
24b3e5a623285b082a23655cdc02f35a40f652acdfandrew@webrtc.orgvoid WebRtc_FreeBuffer(void* handle);
25b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
26b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org// Reads data from the buffer. The |data_ptr| will point to the address where
27b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org// it is located. If all |element_count| data are feasible to read without
28b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org// buffer wrap around |data_ptr| will point to the location in the buffer.
29b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org// Otherwise, the data will be copied to |data| (memory allocation done by the
30b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org// user) and |data_ptr| points to the address of |data|. |data_ptr| is only
31b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org// guaranteed to be valid until the next call to WebRtc_WriteBuffer().
32b3e5a623285b082a23655cdc02f35a40f652acdfandrew@webrtc.org//
33b3e5a623285b082a23655cdc02f35a40f652acdfandrew@webrtc.org// To force a copying to |data|, pass a NULL |data_ptr|.
34b3e5a623285b082a23655cdc02f35a40f652acdfandrew@webrtc.org//
35b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org// Returns number of elements read.
36b3e5a623285b082a23655cdc02f35a40f652acdfandrew@webrtc.orgsize_t WebRtc_ReadBuffer(RingBuffer* handle,
37b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org                         void** data_ptr,
38b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org                         void* data,
39b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org                         size_t element_count);
40b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
41b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org// Writes |data| to buffer and returns the number of elements written.
42b3e5a623285b082a23655cdc02f35a40f652acdfandrew@webrtc.orgsize_t WebRtc_WriteBuffer(RingBuffer* handle, const void* data,
43b3e5a623285b082a23655cdc02f35a40f652acdfandrew@webrtc.org                          size_t element_count);
44b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
45b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org// Moves the buffer read position and returns the number of elements moved.
46b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org// Positive |element_count| moves the read position towards the write position,
47b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org// that is, flushing the buffer. Negative |element_count| moves the read
48b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org// position away from the the write position, that is, stuffing the buffer.
49b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org// Returns number of elements moved.
50b3e5a623285b082a23655cdc02f35a40f652acdfandrew@webrtc.orgint WebRtc_MoveReadPtr(RingBuffer* handle, int element_count);
51b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
52b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org// Returns number of available elements to read.
53b3e5a623285b082a23655cdc02f35a40f652acdfandrew@webrtc.orgsize_t WebRtc_available_read(const RingBuffer* handle);
54b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
55b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org// Returns number of available elements for write.
56b3e5a623285b082a23655cdc02f35a40f652acdfandrew@webrtc.orgsize_t WebRtc_available_write(const RingBuffer* handle);
57b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
58b3e5a623285b082a23655cdc02f35a40f652acdfandrew@webrtc.org#endif  // WEBRTC_MODULES_AUDIO_PROCESSING_UTILITY_RING_BUFFER_H_
59