ascii-chat 0.6.0
Real-time terminal-based video chat with ASCII art conversion
Loading...
Searching...
No Matches
ringbuffer.h
Go to the documentation of this file.
1#pragma once
2
71// C11 stdatomic.h conflicts with MSVC's C++ <atomic> header on Windows.
72// When compiling C++ with MSVC runtime headers, the atomic types are in std:: namespace.
73// We bring them into global namespace for compatibility with C-style struct definitions.
74#if defined(__cplusplus) && defined(_WIN32)
75#include <atomic>
76using std::atomic_bool;
77using std::atomic_int;
78using std::atomic_uint;
79#else
80#include <stdatomic.h>
81#endif
82#include <stdbool.h>
83#include <stddef.h>
84#include <stdint.h>
85#include "platform/mutex.h"
86
95typedef struct {
97 char *buffer;
101 size_t capacity;
103 _Atomic size_t head;
105 _Atomic size_t tail;
107 _Atomic size_t size;
113
114/* ============================================================================
115 * Audio Ring Buffer - Simple ring buffer for audio samples
116 * ============================================================================
117 */
118
135#define AUDIO_RING_BUFFER_SIZE 192000
136
148#define AUDIO_JITTER_BUFFER_THRESHOLD 960
149
159#define AUDIO_JITTER_LOW_WATER_MARK 480
160
172#define AUDIO_JITTER_HIGH_WATER_MARK (AUDIO_JITTER_BUFFER_THRESHOLD * 2)
173
182#define AUDIO_JITTER_TARGET_LEVEL (AUDIO_JITTER_BUFFER_THRESHOLD + 480)
183
189#define AUDIO_CROSSFADE_SAMPLES 256
190
230
231/* ============================================================================
232 * Ring Buffer API
233 * ============================================================================
234 */
235
248ringbuffer_t *ringbuffer_create(size_t element_size, size_t capacity);
249
260
276bool ringbuffer_write(ringbuffer_t *rb, const void *data);
277
293bool ringbuffer_read(ringbuffer_t *rb, void *data);
294
309bool ringbuffer_peek(ringbuffer_t *rb, void *data);
310
323size_t ringbuffer_size(const ringbuffer_t *rb);
324
337bool ringbuffer_is_empty(const ringbuffer_t *rb);
338
351bool ringbuffer_is_full(const ringbuffer_t *rb);
352
367
368/* ============================================================================
369 * Frame Buffer Specific
370 * ============================================================================
371 */
372
382typedef struct {
386 size_t size;
388 char *data;
389} frame_t;
390
413
421#define FRAME_MAGIC 0xDEADBEEF
423#define FRAME_FREED 0xFEEDFACE
424
441
452framebuffer_t *framebuffer_create(size_t capacity);
453
466
478
493bool framebuffer_write_frame(framebuffer_t *fb, const char *frame_data, size_t frame_size);
494
513
527
549bool framebuffer_write_multi_frame(framebuffer_t *fb, const char *frame_data, size_t frame_size,
550 uint32_t source_client_id, uint32_t frame_sequence, uint32_t timestamp);
551
570
588
unsigned int uint32_t
Definition common.h:58
pthread_mutex_t mutex_t
Mutex type (POSIX: pthread_mutex_t)
Definition mutex.h:38
struct audio_ring_buffer audio_ring_buffer_t
Audio ring buffer for real-time audio streaming.
void ringbuffer_clear(ringbuffer_t *rb)
Clear all elements from the buffer.
Definition ringbuffer.c:134
void framebuffer_destroy(framebuffer_t *fb)
Destroy a frame buffer.
Definition ringbuffer.c:203
#define AUDIO_RING_BUFFER_SIZE
Audio ring buffer size in samples (192000 samples = 4 seconds @ 48kHz)
Definition ringbuffer.h:135
void framebuffer_clear(framebuffer_t *fb)
Clear all frames from the buffer, freeing their data.
Definition ringbuffer.c:323
bool framebuffer_read_multi_frame(framebuffer_t *fb, multi_source_frame_t *frame)
Read a multi-source frame from the buffer.
Definition ringbuffer.c:418
framebuffer_t * framebuffer_create_multi(size_t capacity)
Create a multi-source frame buffer for multi-user support.
Definition ringbuffer.c:175
ringbuffer_t * ringbuffer_create(size_t element_size, size_t capacity)
Create a new ring buffer.
Definition ringbuffer.c:28
framebuffer_t * framebuffer_create(size_t capacity)
Create a frame buffer for ASCII frames.
Definition ringbuffer.c:147
size_t ringbuffer_size(const ringbuffer_t *rb)
Get current number of elements in the buffer.
Definition ringbuffer.c:122
bool framebuffer_read_frame(framebuffer_t *fb, frame_t *frame)
Read a frame from the buffer.
Definition ringbuffer.c:276
bool ringbuffer_is_full(const ringbuffer_t *rb)
Check if buffer is full.
Definition ringbuffer.c:130
void ringbuffer_destroy(ringbuffer_t *rb)
Destroy a ring buffer and free its memory.
Definition ringbuffer.c:54
bool ringbuffer_is_empty(const ringbuffer_t *rb)
Check if buffer is empty.
Definition ringbuffer.c:126
bool framebuffer_write_frame(framebuffer_t *fb, const char *frame_data, size_t frame_size)
Write a frame to the buffer.
Definition ringbuffer.c:222
bool framebuffer_write_multi_frame(framebuffer_t *fb, const char *frame_data, size_t frame_size, uint32_t source_client_id, uint32_t frame_sequence, uint32_t timestamp)
Write a multi-source frame to the buffer (for multi-user support)
Definition ringbuffer.c:379
bool framebuffer_peek_latest_multi_frame(framebuffer_t *fb, multi_source_frame_t *frame)
Peek at the latest multi-source frame without removing it.
Definition ringbuffer.c:453
bool ringbuffer_read(ringbuffer_t *rb, void *data)
Try to read an element from the ring buffer (non-blocking)
Definition ringbuffer.c:83
bool ringbuffer_write(ringbuffer_t *rb, const void *data)
Try to write an element to the ring buffer (non-blocking)
Definition ringbuffer.c:61
bool ringbuffer_peek(ringbuffer_t *rb, void *data)
Peek at the next element without removing it.
Definition ringbuffer.c:105
Cross-platform mutex interface for ascii-chat.
Audio ring buffer for real-time audio streaming.
Definition ringbuffer.h:208
atomic_uint read_index
Read index (consumer position) - LOCK-FREE with atomic operations.
Definition ringbuffer.h:214
float last_sample
Last sample value for smooth fade-out during underrun - NOT atomic (only written by reader)
Definition ringbuffer.h:222
atomic_bool jitter_buffer_filled
True after initial jitter buffer fill.
Definition ringbuffer.h:216
atomic_uint underrun_count
Count of underrun events for diagnostics.
Definition ringbuffer.h:224
atomic_int crossfade_samples_remaining
Samples remaining in crossfade (0 = no crossfade active)
Definition ringbuffer.h:218
mutex_t mutex
Mutex for SLOW PATH only (clear/destroy operations, not regular read/write)
Definition ringbuffer.h:228
bool jitter_buffer_enabled
Whether jitter buffering is enabled (false for capture, true for playback)
Definition ringbuffer.h:226
atomic_uint write_index
Write index (producer position) - LOCK-FREE with atomic operations.
Definition ringbuffer.h:212
float data[192000]
Audio sample data buffer.
Definition ringbuffer.h:210
atomic_bool crossfade_fade_in
True if we're fading in (recovering from underrun)
Definition ringbuffer.h:220
Frame structure that stores both data and actual size.
Definition ringbuffer.h:382
char * data
Pointer to frame data (not owned by this struct)
Definition ringbuffer.h:388
uint32_t magic
Magic number to detect corruption (FRAME_MAGIC when valid)
Definition ringbuffer.h:384
size_t size
Actual size of frame data in bytes.
Definition ringbuffer.h:386
Frame buffer structure for managing video frames.
Definition ringbuffer.h:435
mutex_t mutex
Mutex for thread-safe access to framebuffer operations.
Definition ringbuffer.h:439
ringbuffer_t * rb
Underlying ring buffer for frame storage.
Definition ringbuffer.h:437
Multi-source frame structure for multi-user support.
Definition ringbuffer.h:399
char * data
Pointer to frame data (not owned by this struct)
Definition ringbuffer.h:411
uint32_t timestamp
Timestamp when frame was captured.
Definition ringbuffer.h:407
uint32_t magic
Magic number to detect corruption (FRAME_MAGIC when valid)
Definition ringbuffer.h:401
uint32_t frame_sequence
Frame sequence number for ordering.
Definition ringbuffer.h:405
uint32_t source_client_id
Client ID that sent this frame.
Definition ringbuffer.h:403
size_t size
Actual size of frame data in bytes.
Definition ringbuffer.h:409
Lock-free ring buffer structure.
Definition ringbuffer.h:95
bool is_power_of_two
Whether capacity is power of 2 (enables bit masking optimization)
Definition ringbuffer.h:109
size_t capacity
Number of elements that can be stored.
Definition ringbuffer.h:101
size_t capacity_mask
Mask for fast modulo when capacity is power of 2.
Definition ringbuffer.h:111
_Atomic size_t tail
Read position (consumer) - atomic for lock-free operations.
Definition ringbuffer.h:105
_Atomic size_t size
Current number of elements - atomic for fast size checks.
Definition ringbuffer.h:107
size_t element_size
Size of each element in bytes.
Definition ringbuffer.h:99
char * buffer
The actual buffer memory.
Definition ringbuffer.h:97
_Atomic size_t head
Write position (producer) - atomic for lock-free operations.
Definition ringbuffer.h:103