54 if (payload && payload_len > 0) {
61 size_t total_size =
sizeof(header) + payload_len;
70 memcpy(packet, &header,
sizeof(header));
71 if (payload && payload_len > 0) {
72 memcpy(packet +
sizeof(header), payload, payload_len);
92 if (!transport || !samples || num_samples == 0) {
104 size_t samples_size = num_samples *
sizeof(float);
105 size_t total_size =
sizeof(header) + samples_size;
113 memcpy(buffer, &header,
sizeof(header));
114 memcpy(buffer +
sizeof(header), samples, samples_size);
123 if (!transport || !opus_data || opus_len == 0) {
133 if (!transport || !opus_data || !frame_sizes || frame_count == 0) {
145 size_t sizes_len = frame_count *
sizeof(
uint16_t);
146 size_t total_size =
sizeof(header) + sizes_len + opus_len;
154 memcpy(buffer, header,
sizeof(header));
159 for (
uint32_t i = 0; i < frame_count; i++) {
163 memcpy(buffer +
sizeof(header) + sizes_len, opus_data, opus_len);
213 msg_len = strlen(message);
224 size_t total_size =
sizeof(header) + msg_len;
231 memcpy(buffer, &header,
sizeof(header));
233 memcpy(buffer +
sizeof(header), message, msg_len);
243 const char *message) {
244 if (!transport || !message) {
248 size_t msg_len = strlen(message);
256 size_t total_size =
sizeof(header) + msg_len;
264 memcpy(buffer, &header,
sizeof(header));
265 memcpy(buffer +
sizeof(header), message, msg_len);
278 if (!transport || !response) {
286 if (!transport || !info) {
294 if (!transport || !response) {
asciichat_error_t error_code
⚠️‼️ Error and/or exit() when things go bad.
🗃️ Lock-Free Unified Memory Buffer Pool with Lazy Allocation
Hardware-Accelerated CRC32 Checksum Computation.
🔄 Network byte order conversion helpers
#define HOST_TO_NET_U16(val)
#define HOST_TO_NET_U32(val)
#define AUDIO_SAMPLE_RATE
Audio sample rate (48kHz professional quality, Opus-compatible)
void buffer_pool_free(buffer_pool_t *pool, void *data, size_t size)
Free a buffer back to the pool (lock-free)
void * buffer_pool_alloc(buffer_pool_t *pool, size_t size)
Allocate a buffer from the pool (lock-free fast path)
#define SET_ERRNO(code, context_msg,...)
Set error code with custom context message and log it.
asciichat_error_t
Error and exit codes - unified status values (0-255)
uint32_t magic
Magic number (PACKET_MAGIC) for packet validation.
uint16_t flags
Additional flags (REMOTE_LOG_FLAG_*)
uint32_t message_length
Message payload length in bytes (0-512)
uint8_t log_level
Log level associated with the message (log_level_t cast to uint8_t)
uint32_t client_id
Client ID (0 = server, >0 = client identifier)
uint32_t message_length
Length of message payload in bytes (0-512)
uint32_t length
Payload data length in bytes (0 for header-only packets)
uint32_t error_code
Error code from asciichat_error_t enumeration.
uint16_t type
Packet type (packet_type_t enumeration)
uint32_t channels
Number of audio channels (1=mono, 2=stereo)
uint8_t direction
Direction hint so receivers can annotate origin.
uint32_t batch_count
Number of audio chunks in this batch (usually AUDIO_BATCH_COUNT = 32)
uint32_t sample_rate
Sample rate in Hz (e.g., 44100, 48000)
uint32_t total_samples
Total audio samples across all chunks (typically 8192)
uint32_t crc32
CRC32 checksum of payload data (0 if length == 0)
packet_type_t
Network protocol packet type enumeration.
#define PACKET_MAGIC
Packet magic number (0xDEADBEEF)
#define MAX_ERROR_MESSAGE_LENGTH
Maximum error message length (512 bytes)
@ PACKET_TYPE_AUDIO_OPUS_BATCH
Batched Opus-encoded audio frames.
@ PACKET_TYPE_AUDIO_OPUS
Opus-encoded single audio frame.
@ PACKET_TYPE_ACIP_SESSION_INFO
Session info response (Discovery Server -> Client)
@ PACKET_TYPE_PONG
Keepalive pong response.
@ PACKET_TYPE_REMOTE_LOG
Bidirectional remote logging packet.
@ PACKET_TYPE_ACIP_SESSION_CREATED
Session created response (Discovery Server -> Client)
@ PACKET_TYPE_ERROR_MESSAGE
Error packet with asciichat_error_t code and human-readable message.
@ PACKET_TYPE_PING
Keepalive ping packet.
@ PACKET_TYPE_AUDIO_BATCH
Batched audio packets for efficiency.
@ PACKET_TYPE_ACIP_SESSION_JOINED
Session joined response (Discovery Server -> Client)
#define asciichat_crc32(data, len)
Main CRC32 dispatcher macro - use this in application code.
🔊 Audio Capture and Playback Interface for ascii-chat
📝 Logging API with multiple log levels and terminal output control
✅ Safe Integer Arithmetic and Overflow Detection
Packet protocol implementation with encryption and compression support.
asciichat_error_t acip_send_audio_opus(acip_transport_t *transport, const void *opus_data, size_t opus_len)
Send Opus-encoded audio packet.
asciichat_error_t packet_send_via_transport(acip_transport_t *transport, packet_type_t type, const void *payload, size_t payload_len)
Send packet via transport with proper header (exported for generic wrappers)
asciichat_error_t acip_send_ping(acip_transport_t *transport)
Send ping packet.
asciichat_error_t acip_send_session_joined(acip_transport_t *transport, const acip_session_joined_t *response)
Send SESSION_JOINED response packet.
asciichat_error_t acip_send_remote_log(acip_transport_t *transport, uint8_t log_level, uint8_t direction, const char *message)
Send remote log packet.
asciichat_error_t acip_send_error(acip_transport_t *transport, uint32_t error_code, const char *message)
Send error message packet.
asciichat_error_t acip_send_session_created(acip_transport_t *transport, const acip_session_created_t *response)
Send SESSION_CREATED response packet.
asciichat_error_t acip_send_pong(acip_transport_t *transport)
Send pong packet.
asciichat_error_t acip_send_audio_opus_batch(acip_transport_t *transport, const void *opus_data, size_t opus_len, const uint16_t *frame_sizes, uint32_t frame_count, uint32_t sample_rate, uint32_t frame_duration)
Send batched Opus-encoded audio frames.
asciichat_error_t acip_send_audio_batch(acip_transport_t *transport, const float *samples, uint32_t num_samples, uint32_t batch_count)
Send audio batch packet.
asciichat_error_t acip_send_session_info(acip_transport_t *transport, const acip_session_info_t *info)
Send SESSION_INFO response packet.
ACIP shared/bidirectional packet sending functions.
Transport instance structure.
Audio batch packet structure (Packet Type 28)
Error packet structure carrying error code and textual description.
Remote log packet structure carrying log level and message text.
⏱️ High-precision timing utilities using sokol_time.h and uthash
Transport abstraction layer for ACIP protocol.