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