ring_buffer.h 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*
  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. // A ring buffer to hold arbitrary data. Provides no thread safety. Unless
  11. // otherwise specified, functions return 0 on success and -1 on error.
  12. #ifndef COMMON_AUDIO_RING_BUFFER_H_
  13. #define COMMON_AUDIO_RING_BUFFER_H_
  14. // TODO(alessiob): Used by AEC, AECm and AudioRingBuffer. Remove when possible.
  15. #ifdef __cplusplus
  16. extern "C" {
  17. #endif
  18. #include <stddef.h> // size_t
  19. enum Wrap { SAME_WRAP, DIFF_WRAP };
  20. typedef struct RingBuffer {
  21. size_t read_pos;
  22. size_t write_pos;
  23. size_t element_count;
  24. size_t element_size;
  25. enum Wrap rw_wrap;
  26. char* data;
  27. } RingBuffer;
  28. // Creates and initializes the buffer. Returns null on failure.
  29. RingBuffer* WebRtc_CreateBuffer(size_t element_count, size_t element_size);
  30. void WebRtc_InitBuffer(RingBuffer* handle);
  31. void WebRtc_FreeBuffer(void* handle);
  32. // Reads data from the buffer. Returns the number of elements that were read.
  33. // The |data_ptr| will point to the address where the read data is located.
  34. // If no data can be read, |data_ptr| is set to |NULL|. If all data can be read
  35. // without buffer wrap around then |data_ptr| will point to the location in the
  36. // buffer. Otherwise, the data will be copied to |data| (memory allocation done
  37. // by the user) and |data_ptr| points to the address of |data|. |data_ptr| is
  38. // only guaranteed to be valid until the next call to WebRtc_WriteBuffer().
  39. //
  40. // To force a copying to |data|, pass a null |data_ptr|.
  41. //
  42. // Returns number of elements read.
  43. size_t WebRtc_ReadBuffer(RingBuffer* handle,
  44. void** data_ptr,
  45. void* data,
  46. size_t element_count);
  47. // Writes |data| to buffer and returns the number of elements written.
  48. size_t WebRtc_WriteBuffer(RingBuffer* handle,
  49. const void* data,
  50. size_t element_count);
  51. // Moves the buffer read position and returns the number of elements moved.
  52. // Positive |element_count| moves the read position towards the write position,
  53. // that is, flushing the buffer. Negative |element_count| moves the read
  54. // position away from the the write position, that is, stuffing the buffer.
  55. // Returns number of elements moved.
  56. int WebRtc_MoveReadPtr(RingBuffer* handle, int element_count);
  57. // Returns number of available elements to read.
  58. size_t WebRtc_available_read(const RingBuffer* handle);
  59. // Returns number of available elements for write.
  60. size_t WebRtc_available_write(const RingBuffer* handle);
  61. #ifdef __cplusplus
  62. }
  63. #endif
  64. #endif // COMMON_AUDIO_RING_BUFFER_H_