ascii-chat
Binary v0.16.114-28-gf5913657f
Library lib/v0.3.2-3302-gf5913657f
Video chat in your terminal
Loading...
Searching...
No Matches
common/session/protocol.c
Go to the documentation of this file.
1#include <recast/defer.h>
3#include "session/protocol.h"
6#include <ascii-chat/common.h>
7#include <ascii-chat/buffer.h>
14#include <string.h>
15#include <options/options.h>
16#include <options/rcu.h>
18#include <libplatform/terminal.h>
21
22static void receive_frame(const ascii_frame_packet_t *header, const void *data, size_t length, void *context) {
23 session_protocol_t *protocol = context;
25 uint32_t effects = 0;
26 bool has_contract = (header->flags & FRAME_FLAG_RENDER_CONTRACT) != 0;
27 if (header->flags & ~(FRAME_FLAG_RENDER_CONTRACT | FRAME_FLAG_IS_COMPRESSED | 1u)) {
28 protocol->callback_result = SET_ERRNO(ERROR_INVALID_FRAME, "Unknown participant frame flags");
29 return;
30 }
31 if (has_contract) {
32 size_t prefix;
33 protocol->callback_result = frame_render_contract_decode(data, length, &contract, &effects, &prefix);
34 if (protocol->callback_result != ASCIICHAT_OK)
35 return;
36 data = (const uint8_t *)data + prefix;
37 length -= prefix;
38 if (contract.revision && contract.revision == protocol->settings_revision && protocol->effective_render_settings.palette[0] &&
39 strcmp(contract.palette, protocol->effective_render_settings.palette) != 0 && protocol->effective_render_settings.caps.utf8_support) {
40 protocol->callback_result = SET_ERRNO(ERROR_INVALID_FRAME, "Frame palette contradicts acknowledged revision");
41 return;
42 }
43 }
44 bool compressed = (header->flags & FRAME_FLAG_IS_COMPRESSED) != 0;
45 size_t expected = compressed ? header->compressed_size : header->original_size;
46 if (!header->original_size || expected > FRAME_RENDER_MAX_BYTES || length != expected || (compressed && !header->compressed_size) || (!compressed && header->compressed_size)) {
47 protocol->callback_result = SET_ERRNO(ERROR_INVALID_FRAME, "Invalid participant ASCII frame length");
48 return;
49 }
50 // Bound decoded work before allocation/decompression, matching the UI frame budget.
51 if (header->original_size > FRAME_RENDER_MAX_BYTES) {
52 protocol->callback_result = SET_ERRNO(ERROR_INVALID_FRAME, "Participant ASCII frame exceeds byte budget");
53 return;
54 }
56 char *text;
57 if (protocol->media.frame_workspace) {
58 protocol->callback_result = packet_decode_frame_data_workspace(protocol->media.frame_workspace, data, length, compressed, header->original_size, header->compressed_size);
59 if (protocol->callback_result != ASCIICHAT_OK)
60 return;
61 text = protocol->media.frame_workspace->data;
62 } else {
63 text = packet_decode_frame_data_malloc(data, length, compressed, header->original_size, header->compressed_size);
64 }
65 if (!text) {
67 protocol->callback_result = errno_peek_since(decode_scope, &cause) ? cause.code : SET_ERRNO(ERROR_INVALID_FRAME, "Could not decode participant ASCII frame");
68 return;
69 }
70 if (asciichat_crc32(text, header->original_size) != header->checksum) {
71 protocol->callback_result = SET_ERRNO(ERROR_INVALID_FRAME, "Participant ASCII frame checksum mismatch");
72 } else if ((protocol->callback_result = session_frame_validate_contract(text, header->original_size, header->width, header->height, has_contract ? &contract : NULL, effects)) == ASCIICHAT_OK) {
73 // A valid old frame may arrive after a resize/settings update. Skip it
74 // without disconnecting the transport or disturbing audio and recovery.
75 if (protocol->media.terminal_output) {
76 // Redirected snapshots have no physical viewport; the UI fallback is
77 // not a bound on their explicitly configured or default output size.
78 terminal_size_t viewport = (ui_controller_is_presenting() || terminal_is_stdout_tty()) ? ui_controller_size() : (terminal_size_t){0};
80 if ((viewport.cols > 0 && header->width > (uint32_t)viewport.cols) || (viewport.rows > 0 && header->height > (uint32_t)viewport.rows) || header->width > width || header->height > height) {
81 protocol->frames_skipped_viewport++;
82 goto done;
83 }
84 }
85 protocol->frames_received++;
86 if (protocol->media.on_frame)
87 protocol->callback_result = protocol->media.on_frame(header, text, protocol->media.context);
88 }
89done:
90 if (!protocol->media.frame_workspace)
91 SAFE_FREE(text);
92}
93
94static void receive_samples(const audio_batch_packet_t *header, const float *samples, size_t count, void *context) {
95 session_protocol_t *protocol = context;
96 if (header && (header->sample_rate != AUDIO_SAMPLE_RATE || (header->channels != 1 && header->channels != 2) || count % header->channels)) {
97 protocol->callback_result = SET_ERRNO(ERROR_NETWORK_PROTOCOL, "Unsupported participant audio format");
98 return;
99 }
100 if (protocol->media.on_samples) {
101 if (header && header->channels == 1) {
102 float *stereo = SAFE_MALLOC(count * AUDIO_CHANNELS * sizeof(float), float *);
103 protocol->callback_result = audio_convert_channels(samples, count, 1, stereo, AUDIO_CHANNELS);
104 if (protocol->callback_result == ASCIICHAT_OK)
105 protocol->callback_result = protocol->media.on_samples(stereo, count * AUDIO_CHANNELS, protocol->media.context);
106 SAFE_FREE(stereo);
107 } else {
108 protocol->callback_result = protocol->media.on_samples(samples, count, protocol->media.context);
109 }
110 }
111}
112
113static void receive_opus(const void *data, size_t length, void *context) {
114 session_protocol_t *protocol = context;
115 if (!protocol->media.decode_opus || !protocol->media.on_samples)
116 return;
117 float samples[AUDIO_OPUS_MAX_DECODE_SAMPLES * AUDIO_CHANNELS]; // 120 ms at 48 kHz.
118 size_t decoded = 0;
119 protocol->callback_result = protocol->media.decode_opus(data, length, samples, AUDIO_OPUS_MAX_DECODE_SAMPLES * AUDIO_CHANNELS, &decoded, protocol->media.context);
120 if (protocol->callback_result != ASCIICHAT_OK)
121 return;
123 protocol->callback_result = SET_ERRNO(ERROR_AUDIO, "Participant decoder exceeded sample capacity");
124 return;
125 }
126 if (decoded)
127 receive_samples(NULL, samples, decoded, context);
128}
129
130static void receive_opus_batch(const void *data, size_t length, void *context) {
131 session_protocol_t *protocol = context;
132 const uint8_t *opus;
133 const uint16_t *sizes;
134 size_t opus_length;
135 int rate, duration, count;
136 protocol->callback_result = packet_parse_opus_batch(data, length, &opus, &opus_length, &sizes, &rate, &duration, &count);
137 if (protocol->callback_result != ASCIICHAT_OK)
138 return;
139 if (rate != AUDIO_SAMPLE_RATE || duration < 1 || duration > 120 || count < 1 || count > 256) {
140 protocol->callback_result = SET_ERRNO(ERROR_NETWORK_PROTOCOL, "Invalid participant Opus batch format");
141 return;
142 }
143 // Validate the whole batch before delivering any samples, including unaligned input.
144 size_t total = 0;
145 for (int i = 0; i < count; i++) {
146 uint16_t size;
147 memcpy(&size, (const uint8_t *)sizes + (size_t)i * sizeof(size), sizeof(size));
148 size = NET_TO_HOST_U16(size);
149 if (!size || size > opus_length - total) {
150 protocol->callback_result = SET_ERRNO(ERROR_NETWORK_PROTOCOL, "Truncated participant Opus batch");
151 return;
152 }
153 total += size;
154 }
155 if (total != opus_length) {
156 protocol->callback_result = SET_ERRNO(ERROR_NETWORK_PROTOCOL, "Trailing participant Opus batch data");
157 return;
158 }
159 size_t offset = 0;
160 for (int i = 0; i < count; i++) {
161 uint16_t size;
162 memcpy(&size, (const uint8_t *)sizes + (size_t)i * sizeof(size), sizeof(size));
163 size = NET_TO_HOST_U16(size);
164 receive_opus(opus + offset, size, context);
165 if (protocol->callback_result != ASCIICHAT_OK)
166 return;
167 offset += size;
168 }
169}
170
171static void receive_state(const server_state_packet_t *state, void *context) {
172 session_protocol_t *protocol = context;
174 if (!protocol->server_state_initialized || protocol->active_client_count != active)
175 protocol->layout_changed = true;
176 protocol->server_state_initialized = true;
177 protocol->active_client_count = active;
178 if (protocol->control.on_server_state)
179 protocol->control.on_server_state(state, protocol->control.app_ctx);
180}
181
182static void receive_error(const error_packet_t *header, const char *message, void *context) {
183 session_protocol_t *protocol = context;
184 if (protocol->control.on_error)
185 protocol->control.on_error(header, message, protocol->control.app_ctx);
186 else
187 protocol->callback_result = SET_ERRNO(ERROR_NETWORK, "Participant peer error: %s", message);
188}
189
191 *protocol = (session_protocol_t){.transport = transport, .media = media, .control = control};
192 acip_client_session_init(&protocol->client_session, transport, &control);
193 protocol->effective_render_settings.caps.utf8_support = GET_OPTION(force_utf8) != UTF8_SETTING_FALSE;
194}
195
196asciichat_error_t session_protocol_dispatch(session_protocol_t *protocol, packet_type_t type, const void *payload, size_t length) {
197 if (!protocol || (!payload && length))
198 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid participant protocol packet");
199 protocol->callback_result = ASCIICHAT_OK;
200 if (type == PACKET_TYPE_ACIP_SETTINGS_ACK) {
202 const char *json;
203 size_t json_length;
204 asciichat_error_t result = acip_settings_decode(payload, length, &header, &json, &json_length);
205 if (result != ASCIICHAT_OK)
206 return result;
207 if ((header.request && header.request < protocol->settings_request) || header.revision < protocol->settings_revision)
208 return ASCIICHAT_OK;
209 options_t *current = SAFE_MALLOC(sizeof(*current), options_t *), *candidate = SAFE_MALLOC(sizeof(*candidate), options_t *);
211 result = options_prepare_json(current, json, json_length, candidate, true);
212 if (result == ASCIICHAT_OK)
213 result = acip_render_settings_prepare(&protocol->effective_render_settings, candidate);
214 SAFE_FREE(current);
215 SAFE_FREE(candidate);
216 if (result != ASCIICHAT_OK)
217 return result;
218 protocol->effective_render_settings.revision = header.revision;
219 if (header.request)
220 protocol->settings_request = header.request;
221 protocol->settings_revision = header.revision;
222 protocol->settings_status = header.status;
223 if (header.status)
224 log_warn("Remote rendering update rejected (request=%u, status=%u)", header.request, header.status);
225 else
226 log_info("Remote rendering update accepted (request=%u, revision=%u)", header.request, header.revision);
227 return ASCIICHAT_OK;
228 }
230 .on_ascii_frame = receive_frame, .on_audio_batch = receive_samples, .on_audio_opus_batch = receive_opus_batch, .on_server_state = receive_state, .on_error = receive_error, .app_ctx = protocol};
231 bool audio = type == PACKET_TYPE_AUDIO_BATCH || type == PACKET_TYPE_AUDIO_OPUS_BATCH;
232 bool common = audio || type == PACKET_TYPE_ASCII_FRAME || type == PACKET_TYPE_SERVER_STATE || type == PACKET_TYPE_ERROR_MESSAGE;
233 asciichat_error_t result;
234 if (type == PACKET_TYPE_PING && !protocol->control.on_ping)
235 result = acip_send_pong(protocol->transport);
236 else {
237 acip_client_session_t dispatch = protocol->client_session;
238 dispatch.callbacks = common ? media : protocol->control;
239 result = acip_client_session_dispatch(&dispatch, type, payload, length);
240 }
241 if (result == ASCIICHAT_OK)
242 result = protocol->callback_result;
243 if (result == ASCIICHAT_OK) {
244 protocol->packets_received++;
245 if (audio) {
246 protocol->audio_packets_received++;
247 if (protocol->media.on_audio_packet)
248 protocol->media.on_audio_packet(length, protocol->media.context);
249 }
250 }
251 return result;
252}
253
255 const options_t *opts = options_get();
256 // Log the dimensions being sent to server (helps debug dimension mismatch issues)
257 log_debug("Sending terminal size to server: %ux%u (auto_width=%d, auto_height=%d)", width, height, opts->auto_width, opts->auto_height);
258
259 // Connection and transport availability is checked by threaded_send_packet()
260
261 // Build terminal capabilities packet locally
262 // Detect terminal capabilities automatically
263 terminal_capabilities_t caps = detect_terminal_capabilities(options_terminal_settings());
264
265 // Set wants_padding based on snapshot mode and TTY status
266 // Disable padding when:
267 // - In snapshot mode (one frame and exit)
268 // - When stdout is not a TTY (piped/redirected output)
269 // Enable padding for interactive terminal sessions
270 bool is_snapshot_mode = opts->snapshot_mode;
271 bool is_interactive = terminal_is_interactive();
272 caps.wants_padding = is_interactive && !is_snapshot_mode;
273
274 log_debug("Client capabilities: wants_padding=%d (snapshot=%d, interactive=%d, stdin_tty=%d, stdout_tty=%d)", caps.wants_padding, is_snapshot_mode, is_interactive, terminal_is_stdin_tty(),
276
277 // Apply user's color mode override
279
280 // TERM_COLOR_AUTO is an option/configuration sentinel, not a wire-level
281 // capability. A remote peer must receive a concrete color level; this can
282 // otherwise make the server reject the complete capabilities packet. The
283 // browser/native renderer contract supports truecolor as the safe default.
284 terminal_color_mode_t normalized_color = acip_render_settings_normalize_color_mode(caps.color_level);
285 if (normalized_color != caps.color_level) {
286 log_debug("Terminal color detection returned AUTO; advertising truecolor");
287 caps.color_level = normalized_color;
288 caps.color_count = 16777216;
289 caps.capabilities |= TERM_CAP_COLOR_16 | TERM_CAP_COLOR_256 | TERM_CAP_COLOR_TRUE;
290 }
291
292 // Check if detection was reliable, use fallback only for auto-detection
293 if (!caps.detection_reliable && opts->color_mode == COLOR_MODE_AUTO) {
294 log_warn("Terminal capability detection not reliable, using fallback");
295 SAFE_MEMSET(&caps, sizeof(caps), 0, sizeof(caps));
296 caps.color_level = TERM_COLOR_NONE;
297 caps.color_count = 2;
298 caps.capabilities = 0;
299 SAFE_STRNCPY(caps.term_type, "unknown", sizeof(caps.term_type));
300 SAFE_STRNCPY(caps.colorterm, "", sizeof(caps.colorterm));
301 caps.detection_reliable = 0;
302 // Preserve wants_padding even in fallback mode
303 caps.wants_padding = is_interactive && !is_snapshot_mode;
304 }
305
306 // Convert to network packet format with proper byte order
307 terminal_capabilities_packet_t net_packet = {0};
308 if (opts->matrix_rain)
309 caps.capabilities |= TERM_CAP_MATRIX_RAIN;
310 else
311 caps.capabilities &= ~(uint32_t)TERM_CAP_MATRIX_RAIN;
312 if (opts->renderer == ASCII_RENDERER_GLYPH)
313 caps.capabilities |= TERM_CAP_GLYPH_RENDERER;
314 if (opts->palette_explicit)
315 caps.capabilities |= TERM_CAP_EXPLICIT_PALETTE;
316 if (opts->stabilize)
317 caps.capabilities |= TERM_CAP_STABILIZE;
318 net_packet.capabilities = HOST_TO_NET_U32(caps.capabilities);
319 net_packet.color_level = HOST_TO_NET_U32(caps.color_level);
320 net_packet.color_count = HOST_TO_NET_U32(caps.color_count);
321 net_packet.render_mode = HOST_TO_NET_U32(opts->render_mode);
322 net_packet.color_filter = (uint8_t)opts->color_filter;
323 net_packet.width = HOST_TO_NET_U16(width);
324 net_packet.height = HOST_TO_NET_U16(height);
325 net_packet.palette_type = HOST_TO_NET_U32(opts->palette_type);
326 net_packet.utf8_support = HOST_TO_NET_U32(caps.utf8_support ? 1 : 0);
327
328 if (opts->palette_type == PALETTE_CUSTOM && opts->palette_custom_set) {
329 const char *palette_custom = opts && opts->palette_custom_set ? opts->palette_custom : "";
330 SAFE_STRNCPY(net_packet.palette_custom, palette_custom, sizeof(net_packet.palette_custom));
331 net_packet.palette_custom[sizeof(net_packet.palette_custom) - 1] = '\0';
332 } else {
333 SAFE_MEMSET(net_packet.palette_custom, sizeof(net_packet.palette_custom), 0, sizeof(net_packet.palette_custom));
334 }
335
336 // Set desired FPS
337 int fps = opts->fps;
338 if (fps > 0) {
339 net_packet.desired_fps = (uint8_t)(fps > 144 ? 144 : fps);
340 } else {
341 net_packet.desired_fps = caps.desired_fps;
342 }
343
344 if (net_packet.desired_fps == 0) {
345 net_packet.desired_fps = DEFAULT_MAX_FPS;
346 }
347
348 SAFE_STRNCPY(net_packet.term_type, caps.term_type, sizeof(net_packet.term_type));
349 net_packet.term_type[sizeof(net_packet.term_type) - 1] = '\0';
350
351 SAFE_STRNCPY(net_packet.colorterm, caps.colorterm, sizeof(net_packet.colorterm));
352 net_packet.colorterm[sizeof(net_packet.colorterm) - 1] = '\0';
353
354 net_packet.detection_reliable = caps.detection_reliable;
355 // Send UTF-8 support flag: true for AUTO (default) and TRUE settings, false for FALSE setting
356 net_packet.utf8_support = (opts->force_utf8 != UTF8_SETTING_FALSE) ? 1 : 0;
357
358 // Set wants_padding flag (1=padding enabled, 0=no padding for snapshot/piped modes)
359 net_packet.wants_padding = caps.wants_padding ? 1 : 0;
360
361 // Set codec capabilities (client supports all codecs by default)
364
365 *packet = net_packet;
366}
367
368asciichat_error_t session_protocol_update_settings(terminal_size_t *advertised, uint64_t *advertised_revision, session_protocol_send_settings_fn send, void *context) {
369 if (!advertised || !advertised_revision || !send)
370 return SET_ERRNO(ERROR_INVALID_PARAM, "Settings update requires state and a send callback");
371 if (advertised->cols > 0 && advertised->rows > 0 && *advertised_revision == options_revision())
372 return ASCIICHAT_OK;
374 asciichat_error_t result = options_read_snapshot(snapshot);
375 if (result != ASCIICHAT_OK) {
376 return result;
377 }
378 terminal_size_t size = {.cols = snapshot->values.width, .rows = snapshot->values.height};
379 if (size.cols <= 0 || size.rows <= 0) {
380 return ASCIICHAT_OK;
381 }
382 if (snapshot->values.color_mode == TERM_COLOR_AUTO) {
383 terminal_capabilities_t caps = detect_terminal_capabilities(options_terminal_settings());
384 snapshot->values.color_mode = caps.detection_reliable ? caps.color_level : TERM_COLOR_NONE;
385 }
386 result = send(&snapshot->values, (uint32_t)snapshot->revision, context);
387 if (result == ASCIICHAT_OK) {
388 *advertised = size;
389 *advertised_revision = snapshot->revision;
390 }
391
392 return result;
393}
bool errno_peek_since(asciichat_errno_scope_t scope, asciichat_error_context_t *out)
asciichat_errno_scope_t errno_checkpoint(void)
#define SET_ERRNO(code,...)
asciichat_error_t audio_convert_channels(const float *input, size_t frames, unsigned input_channels, float *output, unsigned output_channels)
#define AUDIO_OPUS_MAX_DECODE_SAMPLES
#define AUDIO_SAMPLE_RATE
Definition audio/limits.h:9
#define AUDIO_CHANNELS
Video and audio codec definitions and capability bitmasks.
#define AUDIO_CODEC_CAP_ALL
Definition codecs.h:89
asciichat_error_t session_protocol_update_settings(terminal_size_t *advertised, uint64_t *advertised_revision, session_protocol_send_settings_fn send, void *context)
asciichat_error_t session_protocol_dispatch(session_protocol_t *protocol, packet_type_t type, const void *payload, size_t length)
void session_protocol_init(session_protocol_t *protocol, acip_transport_t *transport, session_protocol_media_t media, acip_client_callbacks_t control)
void session_protocol_build_capabilities(unsigned short width, unsigned short height, terminal_capabilities_packet_t *packet)
Hardware-Accelerated CRC32 Checksum Computation.
🔄 Network byte order conversion helpers
#define HOST_TO_NET_U16(val)
Definition endian.h:100
#define HOST_TO_NET_U32(val)
Definition endian.h:70
#define NET_TO_HOST_U16(val)
Definition endian.h:115
#define NET_TO_HOST_U32(val)
Definition endian.h:85
unsigned int height
Definition fft.c:24
unsigned int width
Definition fft.c:24
asciichat_error_t frame_render_contract_decode(const void *wire, size_t available, acip_render_settings_t *settings, uint32_t *effects, size_t *length)
asciichat_error_t session_frame_validate_contract(const char *text, size_t length, uint32_t width, uint32_t height, const acip_render_settings_t *settings, uint32_t effects)
unsigned short uint16_t
unsigned int uint32_t
#define SAFE_STRNCPY(dst, src, size)
#define SAFE_FREE(ptr)
#define SAFE_MEMSET(dest, dest_size, ch, count)
#define SAFE_MALLOC_DEFER_FREE(name, size, cast)
#define SAFE_MALLOC(size, cast)
unsigned long long uint64_t
unsigned char uint8_t
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:48
@ ERROR_NETWORK
Definition error_codes.h:77
@ ERROR_AUDIO
Definition error_codes.h:66
@ ERROR_NETWORK_PROTOCOL
Definition error_codes.h:81
@ ASCIICHAT_OK
Definition error_codes.h:50
@ ERROR_INVALID_FRAME
@ ERROR_INVALID_PARAM
#define DEFAULT_MAX_FPS
Default maximum frame rate (frames per second).
#define log_warn(...)
Log a WARN message.
Definition log/log.h:591
#define log_info(...)
Log an INFO message.
Definition log/log.h:578
#define log_debug(...)
Log a DEBUG message.
Definition log/log.h:565
#define VIDEO_CODEC_CAP_ALL
Default video codec capabilities (all codecs supported).
Definition codecs.h:56
packet_type_t
Network protocol packet type enumeration.
Definition packet.h:315
@ PACKET_TYPE_AUDIO_OPUS_BATCH
Batched Opus-encoded audio frames.
Definition packet.h:412
@ PACKET_TYPE_SERVER_STATE
Server sends current state to clients.
Definition packet.h:435
@ PACKET_TYPE_ACIP_SETTINGS_ACK
Settings acknowledgment (Participant -> Initiator).
Definition packet.h:501
@ PACKET_TYPE_ASCII_FRAME
Complete ASCII frame with all metadata.
Definition packet.h:397
@ PACKET_TYPE_ERROR_MESSAGE
Error packet with asciichat_error_t code and human-readable message.
Definition packet.h:388
@ PACKET_TYPE_PING
Keepalive ping packet.
Definition packet.h:421
@ PACKET_TYPE_AUDIO_BATCH
Batched audio packets for efficiency.
Definition packet.h:410
#define GET_OPTION(field)
Definition options.h:1339
#define COLOR_MODE_AUTO
Backward compatibility aliases for color mode enum values.
Definition options.h:223
struct options_state options_t
Immutable, RCU-published snapshot of the application's runtime configuration.
const options_t * options_get(void)
Get pointer to current options struct (lock-free, thread-safe).
Definition rcu.c:513
#define options_terminal_settings()
Definition options.h:1287
@ UTF8_SETTING_FALSE
Force UTF-8 OFF: disable UTF-8 support.
Definition options.h:269
asciichat_error_t packet_parse_opus_batch(const void *packet_data, size_t packet_len, const uint8_t **out_opus_data, size_t *out_opus_size, const uint16_t **out_frame_sizes, int *out_sample_rate, int *out_frame_duration, int *out_frame_count)
Parse Opus audio batch packet header and extract frame data.
asciichat_error_t packet_decode_frame_data_workspace(buffer_workspace_t *workspace, const void *data, size_t length, bool compressed, uint32_t original_size, uint32_t compressed_size)
char * packet_decode_frame_data_malloc(const char *data, size_t length, bool compressed, uint32_t original_size, uint32_t compressed_size)
Decode frame data (malloc version for client handlers).
@ PALETTE_CUSTOM
User-defined via –palette-chars.
Definition palette.h:110
#define FRAME_FLAG_RENDER_CONTRACT
Explicit versioned render contract precedes encoded ASCII text.
#define FRAME_FLAG_IS_COMPRESSED
Frame data is compressed.
struct acip_transport acip_transport_t
Forward declaration of ACIP transport.
Definition host.h:508
#define asciichat_crc32(data, len)
Main CRC32 dispatcher macro - use this in application code.
Definition crc32.h:153
🔊 Audio Capture and Playback Interface for ascii-chat
⚙️ Common definitions, error codes, macros, and types shared throughout the application
void acip_client_session_init(acip_client_session_t *session, acip_transport_t *transport, const acip_client_callbacks_t *callbacks)
asciichat_error_t acip_client_session_dispatch(acip_client_session_t *session, packet_type_t type, const void *payload, size_t length)
asciichat_error_t acip_render_settings_prepare(acip_render_settings_t *candidate, const options_t *values)
terminal_color_mode_t acip_render_settings_normalize_color_mode(terminal_color_mode_t mode)
asciichat_error_t acip_settings_decode(const void *payload, size_t length, acip_settings_header_t *header, const char **json, size_t *json_length)
void acip_render_settings_options(const acip_render_settings_t *state, options_t *values)
⚙️ Unified options parsing system for ascii-chat with builder pattern and lock-free access
ASCII Palette Management for Video-to-ASCII Conversion.
uint64_t options_revision(void)
Definition rcu.c:945
asciichat_error_t options_prepare_json(const options_t *current, const char *json, size_t length, options_t *candidate, bool remote_only)
Definition rcu.c:1076
asciichat_error_t options_read_snapshot(options_snapshot_t *snapshot)
Definition rcu.c:934
#define FRAME_RENDER_MAX_BYTES
asciichat_error_t acip_send_pong(acip_transport_t *transport)
Send pong packet.
Definition send.c:382
ACIP shared/bidirectional packet sending functions.
asciichat_error_t(* session_protocol_send_settings_fn)(const options_t *values, uint32_t request, void *context)
terminal_capabilities_t apply_color_mode_override(terminal_capabilities_t caps, const terminal_options_t *)
terminal_capabilities_t detect_terminal_capabilities(const terminal_options_t *options)
unsigned short int terminal_get_effective_height(int height_option, unsigned short fallback_height)
bool terminal_is_interactive(void)
bool terminal_is_stdin_tty(void)
bool terminal_is_stdout_tty(void)
unsigned short int terminal_get_effective_width(int width_option, unsigned short fallback_width)
🔤 String Manipulation and Shell Escaping Utilities
Client-side packet handler callbacks.
Definition handlers.h:55
void(* on_error)(const error_packet_t *header, const char *message, void *ctx)
Called when error message received.
Definition handlers.h:75
void(* on_ping)(void *ctx)
Called when ping received (should send pong).
Definition handlers.h:81
void * app_ctx
Application context (passed to all callbacks).
Definition handlers.h:117
void(* on_server_state)(const server_state_packet_t *state, void *ctx)
Called when server state update received.
Definition handlers.h:72
acip render settings keeps the values that move through the subsystem: caps, width,...
acip settings header keeps the values that move through the subsystem: schema, request,...
The wire metadata for one rendered ASCII frame. width and height describe the terminal grid,...
Definition packet.h:920
When code needs to work with asciichat errno snapshot, it keeps total, copied here instead of scatter...
The metadata prefix for a batch of encoded audio samples. batch_count, total_samples,...
Definition packet.h:983
Error packet structure carrying error code and textual description.
Definition packet.h:778
bool snapshot_mode
Snapshot mode (one frame and exit).
Definition options.h:1142
terminal_color_mode_t color_mode
Color mode (auto/none/16/256/truecolor).
Definition options.h:1084
int force_utf8
UTF-8 support setting (auto/true/false).
Definition options.h:1089
bool palette_explicit
Palette explicitly selected.
Definition options.h:1223
bool matrix_rain
Matrix digital rain effect (false = disabled).
Definition options.h:1145
bool auto_height
Auto-detect height from terminal.
Definition options.h:994
char palette_custom[256]
Custom palette characters.
Definition options.h:1221
bool palette_custom_set
True if custom palette was set.
Definition options.h:1222
terminal_render_mode_t render_mode
Render mode (foreground/background/half-block).
Definition options.h:1087
bool stabilize
Reduce frame-to-frame character flicker.
Definition options.h:1131
color_filter_t color_filter
Monochromatic color filter (none/black/white/green/etc).
Definition options.h:1085
int fps
Target framerate (0 = auto, 1-144).
Definition options.h:1091
ascii_renderer_t renderer
Character selection algorithm.
Definition options.h:1133
palette_type_t palette_type
Selected palette type.
Definition options.h:1220
bool auto_width
Auto-detect width from terminal.
Definition options.h:993
Server state packet structure.
Definition packet.h:757
uint32_t active_client_count
Number of clients actively sending video/audio streams.
Definition packet.h:761
asciichat_error_t(* on_samples)(const float *samples, size_t count, void *context)
asciichat_error_t(* decode_opus)(const void *data, size_t length, float *samples, size_t capacity, size_t *decoded, void *context)
asciichat_error_t(* on_frame)(const ascii_frame_packet_t *header, const char *text, void *context)
void(* on_audio_packet)(size_t length, void *context)
session protocol media keeps the values that move through the subsystem: context, terminal_output,...
acip_render_settings_t effective_render_settings
session_protocol_media_t media
acip_client_session_t client_session
acip_client_callbacks_t control
Terminal capabilities packet structure (Packet Type 5).
Definition packet.h:1097
uint32_t codec_capabilities_video
Video codec capabilities bitmask (VIDEO_CODEC_CAP_*).
Definition packet.h:1129
uint32_t capabilities
Terminal capabilities bitmask (TERM_CAP_* flags).
Definition packet.h:1099
uint32_t color_level
Color level enum value (terminal_color_mode_t).
Definition packet.h:1101
uint32_t color_count
Actual color count (16, 256, or 16777216).
Definition packet.h:1103
uint16_t height
Terminal height in characters.
Definition packet.h:1109
uint8_t wants_padding
Whether client wants frame padding (1=padding enabled, 0=no padding for snapshot/piped modes).
Definition packet.h:1127
uint32_t render_mode
Render mode enum value (foreground/background/half-block).
Definition packet.h:1105
uint16_t width
Terminal width in characters.
Definition packet.h:1107
uint8_t desired_fps
Client's desired frame rate (1-144 FPS).
Definition packet.h:1123
uint8_t detection_reliable
Detection reliability flag (1=reliable detection, 0=best guess).
Definition packet.h:1115
uint32_t codec_capabilities_audio
Audio codec capabilities bitmask (AUDIO_CODEC_CAP_*).
Definition packet.h:1131
char palette_custom[64]
Custom palette characters (if palette_type == PALETTE_CUSTOM).
Definition packet.h:1121
char colorterm[32]
$COLORTERM environment variable value (for debugging)
Definition packet.h:1113
uint32_t palette_type
Palette type enum value (palette_type_t).
Definition packet.h:1119
uint8_t color_filter
Monochromatic color filter enum value (color_filter_t).
Definition packet.h:1125
uint32_t utf8_support
UTF-8 support flag (0=no UTF-8, 1=UTF-8 supported).
Definition packet.h:1117
char term_type[32]
$TERM environment variable value (for debugging)
Definition packet.h:1111
@ TERM_CAP_STABILIZE
@ TERM_CAP_EXPLICIT_PALETTE
@ TERM_CAP_GLYPH_RENDERER
⏱️ High-precision timing utilities using sokol_time.h and uthash
bool ui_controller_is_presenting(void)
terminal_size_t ui_controller_size(void)
@ ASCII_RENDERER_GLYPH