ascii-chat
Binary v0.16.50-198-g78f51665f
Library lib/v0.3.2-2240-g78f51665f
Video chat in your terminal
Loading...
Searching...
No Matches
client_like.c
Go to the documentation of this file.
1
2/* Storage capacities in elements; text buffers include the terminating NUL. */
3#define SESSION_CLIENT_LIKE_WEBCAM_INDEX_BUFFER_SIZE 32
4
5#include <ascii-chat/otel.h>
14
16#include "session/client_like.h"
17#include "session/capture.h"
18#include "session/display.h"
19#include "session/render.h"
21#include "../../client/audio.h"
22
30
31#include <ascii-chat/log/log.h>
32#include <options/options.h>
33#include <options/runtime.h>
34#include <libplatform/compiler.h>
35#include <libplatform/system.h>
36#include <libplatform/terminal.h>
37#include <libplatform/terminal.h>
38#include <ascii-chat/util/url.h>
40
41#include <string.h>
42#include <libplatform/atomic.h>
43
44// Forward declarations (implemented below)
45static bool client_like_capture_should_exit_adapter(void *user_data);
46static bool client_like_display_should_exit_adapter(void *user_data);
47static bool client_like_is_networked(session_client_like_kind_t kind);
48
49static asciichat_error_t client_like_validate_mirror_webcam_selection(const session_client_like_config_t *config) {
50 if (!config || config->kind != SESSION_CLIENT_LIKE_KIND_MIRROR || !terminal_is_stdout_tty() || GET_OPTION(test_pattern) || GET_OPTION(waveform) || GET_OPTION(fft)) {
51 return ASCIICHAT_OK;
52 }
53
54 const char *media_file = GET_OPTION(media_file);
55 const char *media_url = GET_OPTION(media_url);
56 if ((media_file && media_file[0] != '\0') || (media_url && media_url[0] != '\0')) {
57 return ASCIICHAT_OK;
58 }
59
60 webcam_device_info_t *devices = NULL;
61 unsigned int device_count = 0;
62 asciichat_error_t result = webcam_list_devices(&devices, &device_count);
63 if (result != ASCIICHAT_OK) {
64 return SET_ERRNO(ERROR_WEBCAM, "Unable to enumerate webcams before starting mirror mode");
65 }
66
67 unsigned int selected_index = (unsigned int)GET_OPTION(webcam_index);
68 if (selected_index >= device_count) {
69 if (device_count == 0) {
70 result = SET_ERRNO(ERROR_WEBCAM, "Webcam index %u is unavailable: no webcam devices were found (run --list-webcams)", selected_index);
71 } else {
72 result = SET_ERRNO(ERROR_WEBCAM,
73 "Webcam index %u is unavailable: %u webcam device(s) found; valid indices are 0-%u "
74 "(run --list-webcams)",
75 selected_index, device_count, device_count - 1);
76 }
77 } else {
78 result = ASCIICHAT_OK;
79 }
80
82 return result;
83}
84
85// Adapter callbacks own a config snapshot for the duration of the session.
86static session_client_like_config_t g_current_config = {0};
87
88// Module-level adapter functions for render loop access
89static bool (*g_render_should_exit)(void *) = NULL;
90
91// Module-level stdin reader for ASCII-to-video rendering (stdin render mode only)
92static terminal_fd_reader_t *g_stdin_reader = NULL;
93
94/* ============================================================================
95 * Public Accessors
96 * ============================================================================ */
97
99 return g_render_should_exit;
100}
101
103 return g_stdin_reader;
104}
105
106/* ============================================================================
107 * Exit Condition Adapters
108 * ============================================================================ */
109
114static bool client_like_capture_should_exit_adapter(void *user_data) {
115 (void)user_data;
117 return true;
118 }
119 if (g_current_config.custom_should_exit) {
120 return g_current_config.custom_should_exit(g_current_config.exit_user_data);
121 }
122 return false;
123}
124
129static bool client_like_display_should_exit_adapter(void *user_data) {
130 (void)user_data;
132 return true;
133 }
134 if (g_current_config.custom_should_exit) {
135 return g_current_config.custom_should_exit(g_current_config.exit_user_data);
136 }
137 return false;
138}
139
140static bool client_like_is_networked(session_client_like_kind_t kind) {
142}
143
144// Resources owned by the session; setup helpers borrow this state so every
145// partially initialized session follows the same teardown order.
165
166static void client_like_setup_terminal_logging(void) {
167
168 // Force stderr when stdout is piped (prevents ASCII corruption in output stream)
169 const char *output = GET_OPTION(render_file);
170 bool should_force_stderr = terminal_should_force_stderr() || GET_OPTION(render_kitty) || strcmp(output, "--kitty") == 0 || strcmp(output, "-") == 0 || strcmp(output, "pipe:") == 0;
171 log_debug("terminal_should_force_stderr()=%d", should_force_stderr);
172
173 if (should_force_stderr) {
174 // Redirect logs to stderr to prevent corruption of stdout (for pipes)
175 // But keep terminal output enabled so we can see initialization errors
177 }
178
179 // Note: Force all logging to stderr if encoding to pipe to prevent corrupting binary video output
180 // (implemented via should_force_stderr check above)
181 const char *render_file_early = GET_OPTION(render_file);
182 log_debug("[CLIENT_LIKE_RUN] render_file_early=%s (ptr=%p)", render_file_early ? render_file_early : "(null)", (void *)render_file_early);
183 if (render_file_early && render_file_early[0] != '\0' && (strcmp(render_file_early, "-") == 0 || strcmp(render_file_early, "pipe:") == 0)) {
184 log_info("--render-file=- will output binary video to stdout, logging sent to stderr");
185 }
186}
187
188static asciichat_error_t client_like_setup_keepawake(void) {
189
190 // Validate mutual exclusivity
191 bool enable_ka = GET_OPTION(enable_keepawake);
192 bool disable_ka = GET_OPTION(disable_keepawake);
193 log_debug("enable_keepawake=%d, disable_keepawake=%d", enable_ka, disable_ka);
194
195 if (enable_ka && disable_ka) {
196 return SET_ERRNO(ERROR_INVALID_PARAM, "--keepawake and --no-keepawake are mutually exclusive");
197 }
198
199 // Enable keepawake unless explicitly disabled
200 if (!disable_ka) {
202 bool suppressed = errno_notifications_suppress(true);
203 asciichat_error_t keepawake_result = platform_enable_keepawake();
205 if (keepawake_result != ASCIICHAT_OK) {
206 if (enable_ka)
207 log_warn("Unable to inhibit system sleep: %s", asciichat_error_string(keepawake_result));
208 else
209 log_debug("Unable to inhibit system sleep: %s", asciichat_error_string(keepawake_result));
210 }
212 }
213
214 return ASCIICHAT_OK;
215}
216
217static asciichat_error_t client_like_create_session_display(client_like_state_t *state) {
218 // Create display context early to share TTY initialization between splash and rendering
219 // This prevents double TTY init which was corrupting the terminal state
220 session_display_config_t display_config = {0};
221 display_config.snapshot_mode = GET_OPTION(snapshot_mode);
222 display_config.palette_type = GET_OPTION(palette_type);
223 display_config.custom_palette = GET_OPTION(palette_custom_set) ? GET_OPTION(palette_custom) : NULL;
224 display_config.color_mode = TERM_COLOR_AUTO;
225 display_config.should_exit_callback = client_like_display_should_exit_adapter;
226 display_config.callback_data = NULL;
227 display_config.defer_render_file = true;
228
229 log_debug("[SETUP_DISPLAY_EARLY] Creating display context (before splash)");
230 state->display = session_display_create(&display_config);
231 if (!state->display) {
232 log_debug("[SETUP_DISPLAY_EARLY] session_display_create() returned NULL");
233 return SET_ERRNO(ERROR_DISPLAY, "Failed to initialize display context");
234 }
235 log_debug("[SETUP_DISPLAY_EARLY] Display context created, will use for splash");
236
237 // Register global display context for signal handlers (Ctrl+C help screen closure)
238 // This makes the context available to signal handlers for the entire app lifetime
240 return ASCIICHAT_OK;
241}
242
243static asciichat_error_t client_like_prepare_session_splash(const session_client_like_config_t *config, client_like_state_t *state, bool *stop) {
245 log_debug("Starting splash with display context");
246
247 // Use the display context we just created for splash animation
248 if (splash_should_display(true))
250 if (splash_is_running()) {
252 }
253
254 if (config->prepare_fn) {
255 result = config->prepare_fn(config->run_user_data);
256 if (result != ASCIICHAT_OK)
257 return result;
258 }
259
260 // Detect if we're using media vs webcam (needed for splash timing)
261 const char *media_url = GET_OPTION(media_url);
262 const char *media_file = GET_OPTION(media_file);
263 bool has_media = (media_url && strlen(media_url) > 0) || (media_file && strlen(media_file) > 0);
264
265 // Show splash briefly for webcam, but skip sleep for media (which takes time anyway)
266 if (splash_is_running() && !has_media && !GET_OPTION(snapshot_mode)) {
267 APP_CALLBACK_VOID(platform_pump_events);
268 APP_CALLBACK_VOID_UINT(platform_sleep_ms, 250);
269 }
270
271 // Restore stderr now that splash animation and post-splash logging are done
272 // This allows logs to appear on screen again after this point
274
275 // Discovery keeps the invitation as the only waiting UI.
276 if (splash_should_display(true))
278 if (splash_should_display(true) && GET_OPTION(detected_mode) != MODE_DISCOVERY) {
279 if (update_banner_has_update() && !GET_OPTION(snapshot_mode)) {
280 bool wants_update = update_banner_show_prompt(state->display);
281 if (wants_update) {
283 *stop = true;
284 return ASCIICHAT_OK;
285 }
287 }
289 }
290 }
291 return ASCIICHAT_OK;
292}
293
294static void client_like_configure_session_logging(const session_client_like_config_t *config) {
295 log_debug("About to disable terminal logging (snapshot=%d, discovery=%d)", GET_OPTION(snapshot_mode), config->discovery != NULL);
296 // For discovery mode, skip disabling logging initially - we'll handle it after role is determined
297 // Discovery participants should show logs, only hosts should have logs disabled for rendering
298 bool should_disable_logging = !GET_OPTION(snapshot_mode) && (config->discovery == NULL);
299 if (should_disable_logging) {
301 } else {
302 log_debug("Skipping log disable (snapshot=%d or discovery=%d)", GET_OPTION(snapshot_mode), config->discovery != NULL);
303 }
304}
305
306static void client_like_configure_url_capture(client_like_state_t *state) {
307 log_info("Using network media source (webcam disabled)");
309 state->capture_config.path = state->media_url_val;
310 state->capture_config.loop = false;
311}
312
313static void client_like_configure_file_capture(client_like_state_t *state) {
314 if (strcmp(state->media_file_val, "-") == 0) {
315 log_info("Using stdin for media streaming (webcam disabled)");
317 state->capture_config.path = NULL;
318 state->capture_config.loop = false;
319 } else {
320 log_info("Using media file: %s (webcam disabled)", state->media_file_val);
322 state->capture_config.path = state->media_file_val;
323 state->capture_config.loop = GET_OPTION(media_loop);
324 }
325}
326
327static uint32_t client_like_select_capture_fps(client_like_state_t *state) {
328 if (GET_OPTION(fps) > 0) {
329 uint32_t fps = (uint32_t)GET_OPTION(fps);
330 log_info("Using user-specified FPS: %u", fps);
331 return fps;
332 }
333
334 // Probe media files here; webcam FPS is available after the camera opens.
336 log_debug("Probing media source FPS");
338 state->probe_source = media_source_create_interruptible(state->capture_config.type, state->capture_config.path, client_like_capture_should_exit_adapter, NULL);
339 if (state->probe_source) {
340 ASSERT_NO_ERRNO_SINCE(probe_scope);
342 double source_fps = media_source_get_video_fps(state->probe_source);
343 // Keep conversion in range and never select zero after rounding.
344 if (source_fps >= 0.5 && source_fps <= UINT32_MAX - 0.5) {
345 uint32_t fps = (uint32_t)(source_fps + 0.5);
346 log_info("Using detected source FPS: %u", fps);
347 return fps;
348 }
349 log_warn("Media source has no usable FPS, using default 60 FPS");
350 } else {
352 log_warn("Failed to probe media source FPS, using default 60 FPS");
353 }
354 }
355 return 60;
356}
357
358static asciichat_error_t client_like_configure_media_capture(const session_client_like_config_t *config, client_like_state_t *state) {
359 // capture_config starts zeroed with the session state.
360 state->capture_config.resize_for_network = client_like_is_networked(config->kind);
361 state->capture_config.should_exit_callback = client_like_capture_should_exit_adapter;
362 state->capture_config.callback_data = NULL;
363
364 // Select media source based on options (priority order)
365 state->media_url_val = GET_OPTION(media_url);
366 state->media_file_val = GET_OPTION(media_file);
367 log_debug("media_url=%s, media_file=%s", state->media_url_val ? state->media_url_val : "(null)", state->media_file_val ? state->media_file_val : "(null)");
368
369 if (state->media_url_val && strlen(state->media_url_val) > 0) {
371 client_like_configure_url_capture(state);
372 } else if (state->media_file_val && strlen(state->media_file_val) > 0) {
373 client_like_configure_file_capture(state);
374 } else if (GET_OPTION(test_pattern)) {
375 // Test pattern
376 log_debug("Using test pattern");
377 log_info("Using test pattern");
379 state->capture_config.path = NULL;
380 state->capture_config.loop = false;
381 } else {
382 // Default: Try webcam if stdout is a TTY, otherwise use test pattern
383 // (webcam + piped output often fails, test pattern is reliable fallback)
384 bool stdout_is_tty = terminal_is_stdout_tty();
385 if (stdout_is_tty || SAFE_GETENV("ASCII_CHAT_MCP_WEBCAM")) {
386 // Explicit MCP webcam capture also works with the isolated worker's pipe.
387 bool visualizing = GET_OPTION(waveform) || GET_OPTION(fft);
388 log_debug("Using %s", visualizing ? "an audio visualization frame clock" : "the local webcam");
389 log_info("%s", visualizing ? "Visualizing audio" : "Using local webcam");
391 static char webcam_index_str[SESSION_CLIENT_LIKE_WEBCAM_INDEX_BUFFER_SIZE];
392 safe_snprintf(webcam_index_str, sizeof(webcam_index_str), "%u", GET_OPTION(webcam_index));
393 state->capture_config.path = webcam_index_str;
394 if (!visualizing)
395 log_info("Using webcam device index %u", GET_OPTION(webcam_index));
396 state->capture_config.loop = false;
397 } else {
398 // Piped output mode: use test pattern (webcam + pipes often fails)
399 log_debug("Stdout is piped, defaulting to test pattern for reliability");
400 log_info("Stdout is piped - using test pattern instead of webcam");
402 state->capture_config.path = NULL;
403 state->capture_config.loop = false;
404 }
405 }
406
407 // Synthetic media has no source-provided cadence. Normalize its implicit
408 // capture rate before configuring the pipeline so the no-argument
409 // `mirror --test-pattern` path follows the same live-frame schedule as an
410 // explicit `--fps 60` invocation.
411 if (GET_OPTION(test_pattern) && GET_OPTION(fps) <= 0)
412 options_set_int("fps", 60);
413 state->capture_config.target_fps = client_like_select_capture_fps(state);
415
416 // Apply initial seek if specified
417 state->capture_config.initial_seek_timestamp = GET_OPTION(media_seek_timestamp);
418
419 // Clean up probe source (don't reuse for actual capture)
420 if (state->probe_source) {
422 state->probe_source = NULL;
423 }
424
425 // Pass probed FPS to display context for correct render-file duration
426 if (state->capture_config.target_fps > 0) {
428 log_debug("Set display render FPS to %u for correct video encoding duration", state->capture_config.target_fps);
429 }
430
432 if (render_init_result != ASCIICHAT_OK) {
433 return render_init_result;
434 }
435 return ASCIICHAT_OK;
436}
437
438static asciichat_error_t client_like_create_network_capture(const session_client_like_config_t *config, client_like_state_t *state) {
439 log_debug("Setting up network transports");
440
441 bool networked_mode = client_like_is_networked(config->kind);
442
443 switch (config->kind) {
445 log_debug("Mirror mode detected - will use local capture with media source");
446 break;
448 log_debug("Client/Network mode detected - will use network capture without local media source");
449 break;
451 log_debug("Discovery mode detected - discovery session will manage networking");
452 break;
453 }
454
455 // ============================================================================
456 // SETUP: Capture Context
457 // ============================================================================
458
459 // Check for stdin render mode: read ASCII frames from stdin, render to video
460 // Only enable stdin render mode if:
461 // 1. render-file is "-" (output to stdout)
462 // 2. stdin is not a TTY
463 // 3. --height is explicitly specified (user must opt-in to stdin render mode)
464 // 4. NO media source is being used (not reading from file/URL/test-pattern)
465 // This prevents accidental activation when piping to other processes or using mirror mode
466 const char *render_file_opt = GET_OPTION(render_file);
467 int frame_height = GET_OPTION(height);
468 bool height_explicitly_set = (frame_height != OPT_HEIGHT_DEFAULT);
469 bool has_media_source = (state->media_url_val && strlen(state->media_url_val) > 0) || (state->media_file_val && strlen(state->media_file_val) > 0) || GET_OPTION(test_pattern);
470
471 // Enable audio capture if render-file is active and media source exists
472 if (render_file_opt && strlen(render_file_opt) > 0 && has_media_source) {
473 state->capture_config.enable_audio = true;
474 log_debug("Audio capture enabled for render-file encoding");
475 }
476
477 state->stdin_render_mode =
478 (render_file_opt && strcmp(render_file_opt, "-") == 0 && !terminal_is_stdin_tty() && height_explicitly_set && !has_media_source && config->kind != SESSION_CLIENT_LIKE_KIND_MIRROR);
479
480 if (state->stdin_render_mode) {
481 // Stdin render mode: read ASCII frames from stdin, output video to stdout
482 log_info("Stdin render mode enabled: reading ASCII frames from stdin, output to stdout");
483
484 // Get frame dimensions (--height must be explicitly set to reach here)
485 int frame_width = GET_OPTION(width);
486
487 // Validate stdin render mode constraints (--height is already validated above since height_explicitly_set is true)
488 bool width_was_explicitly_set = (frame_width != OPT_WIDTH_DEFAULT);
489
490 if (width_was_explicitly_set) {
491 return SET_ERRNO(ERROR_USAGE, "Stdin render mode does not accept --width (auto-detected from frames).\n"
492 "Only specify: --height <rows>");
493 }
494
495 asciichat_error_t stdin_err = terminal_fd_reader_create(STDIN_FILENO, frame_height, &g_stdin_reader);
496 if (stdin_err != ASCIICHAT_OK) {
497 return SET_ERRNO(ERROR_MEDIA_INIT, "Failed to initialize stdin frame reader: %s", asciichat_error_string(stdin_err));
498 }
499
500 // For stdin render mode, create a minimal capture context for compatibility
502 if (!state->capture) {
503 terminal_fd_reader_destroy(g_stdin_reader);
504 g_stdin_reader = NULL;
505 return SET_ERRNO(ERROR_MEDIA_INIT, "Failed to initialize capture context for stdin rendering");
506 }
507
508 log_info("stdin render mode: reading %d-line frames from stdin, auto-detecting width", frame_height);
509 }
510
511 // Choose capture type based on mode determined earlier:
512 // - Mirror mode: needs to capture local media (webcam, file, test pattern)
513 // - Network modes (client/discovery): receive frames from network, no local capture
514 // Discovery peers may use a local media source (for example the synthetic
515 // test pattern used by headless WebRTC tests) as their outgoing stream.
516 // Keep the network-only capture for ordinary client mode, but do not discard
517 // an explicitly requested source before the discovery transport is active.
518 bool discovery_has_local_source = config->kind == SESSION_CLIENT_LIKE_KIND_DISCOVERY;
519 if (!state->stdin_render_mode && networked_mode && !discovery_has_local_source) {
520 // Network mode: create minimal capture context without media source
521 log_debug("Network mode detected - using network capture (no local media source)");
522 uint64_t net_cap_start = time_get_ns();
524 double net_cap_ms = time_ns_to_ms(time_elapsed_ns(net_cap_start, time_get_ns()));
525 log_debug("★ INIT_CHECKPOINT: Network capture created in %.1f ms", net_cap_ms);
526 if (!state->capture) {
527 return SET_ERRNO(ERROR_MEDIA_INIT, "Failed to initialize network capture context");
528 }
529 log_debug("Network capture FPS set to %u", state->capture_config.target_fps);
530 }
531 return ASCIICHAT_OK;
532}
533
534static void client_like_prepare_session_audio(client_like_state_t *state) {
535 // Skip audio for immediate snapshots
536 state->should_init_audio = GET_OPTION(audio_enabled);
537 if (GET_OPTION(snapshot_mode) && GET_OPTION(snapshot_delay) == 0.0) {
538 state->should_init_audio = false;
539 log_debug("Skipping audio initialization for immediate snapshot");
540 }
541
542 // Pass stdin_reader to display if in stdin render mode
543 if (state->stdin_render_mode && g_stdin_reader) {
544 session_display_set_stdin_reader(state->display, g_stdin_reader);
545 log_debug("stdin_reader passed to display context");
546 }
547}
548
549// Return true when shutdown was requested before capture startup.
550static bool client_like_finish_session_splash(const session_client_like_config_t *config) {
551 log_debug("[SETUP_SPLASH] About to end splash screen");
552
553 // The splash screen will display during initialization and exit when done.
554 // The initialization process (media probing, display setup, etc.) naturally
555 // takes time, keeping the splash visible. The splash is kept alive during
556 // connection attempts (TCP and WebSocket) to prevent a blank screen during
557 // network operations. splash_intro_done() is called only after a successful
558 // connection is established.
559
560 log_debug("[SETUP_SPLASH] Splash will remain visible during connection attempts");
561
562 // Exit early if shutdown was requested (e.g., user pressed Ctrl-C)
564 log_debug("[SETUP] Shutdown requested, exiting early");
565 log_debug("[SETUP_SPLASH] Ending splash due to early shutdown request");
568 return true;
569 }
570
571 // Wait for splash animation to finish before proceeding to render loop
572 // (splash_anim thread uses the display, must not be disrupted before cleanup)
573 // But skip for immediate snapshots (snapshot-delay=0) which need to output first frame immediately
574 // Networked modes retain their input monitor until the first received frame.
575 if (config->kind == SESSION_CLIENT_LIKE_KIND_MIRROR) {
577 if (!(GET_OPTION(snapshot_mode) && GET_OPTION(snapshot_delay) == 0.0)) {
579 }
580 } else {
581 log_debug("[SETUP_SPLASH] Networked mode: keeping splash visible while connecting");
582 }
583
584 return false;
585}
586
587static asciichat_error_t client_like_create_local_capture(client_like_state_t *state) {
588 log_debug("[MIRROR_CHECK] stdin_render_mode=%d, capture=%p", state->stdin_render_mode, (void *)state->capture);
589 if (!state->stdin_render_mode && !state->capture) {
590 // Mirror mode: create capture context with local media source (NOW, after splash finishes)
591 log_debug("Mirror mode detected - creating mirror capture after splash screen");
592 uint64_t mirror_cap_start = time_get_ns();
594 double mirror_cap_ms = time_ns_to_ms(time_elapsed_ns(mirror_cap_start, time_get_ns()));
595 log_debug("★ INIT_CHECKPOINT: Mirror capture created in %.1f ms (after splash)", mirror_cap_ms);
596 if (!state->capture) {
597 return SET_ERRNO(ERROR_MEDIA_INIT, "Failed to initialize mirror capture source");
598 }
599 if (state->capture_config.type == MEDIA_SOURCE_WEBCAM && GET_OPTION(fps) == 0)
601 }
602 return ASCIICHAT_OK;
603}
604
605static uint64_t client_like_restore_video_after_audio_failure(uint64_t revision) {
606 const options_edit_t edits[] = {
607 {.name = "waveform", .type = OPTIONS_VALUE_BOOL, .value.boolean = false},
608 {.name = "fft", .type = OPTIONS_VALUE_BOOL, .value.boolean = false},
609 };
610 const options_patch_t patch = {.edits = edits, .count = 2, .expected_revision = revision, .origin = OPTIONS_ORIGIN_LOCAL};
611 options_patch_result_t result = {0};
613 asciichat_error_t error = options_apply_patch(&patch, &result);
615 if (error == ASCIICHAT_OK && result.changed)
616 log_warn("Audio is unavailable; returning to video instead of audio visualization");
617 return error == ASCIICHAT_OK ? result.revision : revision;
618}
619
620static void *client_like_update_mirror_audio(media_source_t *source, void *data) {
621 client_like_state_t *state = data;
622 bool file_audio = media_source_has_audio(source);
623 bool needed = GET_OPTION(audio_enabled) && (file_audio || GET_OPTION(waveform) || GET_OPTION(fft) || GET_OPTION(test_audio) || GET_OPTION(render_file)[0]);
624 if (!needed) {
625 if (state->audio_ctx && state->audio_ctx->running) {
627 LOG_ERRNO_IF_SET("Cannot stop idle mirror audio");
628 else
629 log_info("Mirror audio idle");
630 }
631 return state->audio_ctx;
632 }
633 if (state->audio_ctx && state->audio_ctx->running)
634 return state->audio_ctx;
635 uint64_t revision = options_revision();
636 if (state->audio_failed_revision == revision + 1)
637 return state->audio_ctx;
638 if (!state->audio_ctx) {
639 state->audio_ctx = SAFE_CALLOC(1, sizeof(*state->audio_ctx), audio_context_t *);
640 if (audio_init(state->audio_ctx) != ASCIICHAT_OK) {
641 LOG_ERRNO_IF_SET("Cannot initialize mirror audio");
642 SAFE_FREE(state->audio_ctx);
643 state->audio_failed_revision = client_like_restore_video_after_audio_failure(revision) + 1;
644 return NULL;
645 }
646 }
647 state->audio_ctx->capture_media_source = file_audio && GET_OPTION(audio_capture_source) != AUDIO_CAPTURE_SOURCE_MIC ? source : NULL;
648 state->audio_ctx->monitor_local_media = true;
649 state->audio_ctx->playback_only = !audio_should_enable_microphone(GET_OPTION(audio_capture_source), file_audio);
652 LOG_ERRNO_IF_SET("Cannot start mirror audio");
654 // Keep the initialized context alive for input-thread borrowers and retries.
655 state->audio_failed_revision = client_like_restore_video_after_audio_failure(revision) + 1;
656 state->audio_available = false;
657 return state->audio_ctx;
658 }
659 state->audio_available = true;
660 state->audio_failed_revision = 0;
661 log_info("Mirror audio started on demand");
662 return state->audio_ctx;
663}
664
665static void client_like_start_session_audio(const session_client_like_config_t *config, client_like_state_t *state) {
666 if (state->should_init_audio && config->kind == SESSION_CLIENT_LIKE_KIND_MIRROR) {
667 session_capture_set_audio_updater(state->capture, client_like_update_mirror_audio, state);
669 session_capture_set_audio_context(state->capture, client_like_update_mirror_audio(source, state));
670 }
671}
672
673static asciichat_error_t client_like_run_session_attempts(const session_client_like_config_t *config, client_like_state_t *state) {
675 // Connection/attempt loop - wraps run_fn with reconnection logic
676 int attempt = 0;
677 int max_attempts = config->max_reconnect_attempts;
678
679 log_debug("[CLIENT_LIKE_LOOP] Starting connection loop, max_attempts=%d", max_attempts);
680
681 while (true) {
682 attempt++;
685 if (attempt > 1) {
687 }
688
690 {
691 ASCII_OTEL_SCOPE(SESSION_ATTEMPT);
692 ASCII_OTEL_ATTR_INT("session.attempt", attempt);
693 ASCII_OTEL_ATTR_STRING("session.attempt.kind", attempt > 1 ? "reconnect" : "initial");
694 result = config->run_fn(state->capture, state->display, config->run_user_data);
695 ASCII_OTEL_ATTR_INT("session.result", result);
697 ascii_otel_metric(ASCII_OTEL_METRIC_CONNECTION_FAILURE, 1);
698 if (result != ASCIICHAT_OK && result != ERROR_SIGNAL_INTERRUPT)
699 ascii_otel_error("Session attempt failed");
700 }
701
702 // Exit immediately if run_fn succeeded
703 if (result == ASCIICHAT_OK) {
704 ASSERT_NO_ERRNO_SINCE(attempt_scope);
706 // Connection succeeded - splash screen cleanup now happens when first frame renders
707 // This ensures the splash stays visible during TCP/WebSocket/datachannel connection attempts
708
709 break;
710 }
711
712 // Check if we should attempt reconnection
713 bool should_retry = false;
714
715 if (max_attempts != 0) { // max_attempts == 0 means no retries
716 // Check custom reconnect logic if provided
717 if (config->should_reconnect_callback) {
718 should_retry = config->should_reconnect_callback(result, attempt, config->reconnect_user_data);
719 } else {
720 should_retry = true; // Default: always retry
721 }
722
723 // Check retry limits if still planning to retry
724 if (should_retry && max_attempts > 0 && attempt >= max_attempts) {
725 log_debug("Reached maximum reconnection attempts (%d), giving up", max_attempts);
726 should_retry = false;
727 }
728 }
729
730 // If not retrying or shutdown requested, exit loop
731 if (!should_retry || APP_CALLBACK_BOOL(should_exit)) {
732 break;
733 }
734
735 LOG_ERRNO_IF_SET("Connection attempt failed; retrying");
737
740
741 // Reconnection will happen - show splash screen during reconnection attempt
742 // Reset first_frame flag so splash cleanup runs on next successful connection
744
745 // splash_intro_start() handles all the logic:
746 // - If splash should display: shows splash and disables console logging
747 // - If splash shouldn't display: restores stderr and returns without changing logging state
749
750 // Log reconnection message
751 if (max_attempts == -1) {
752 log_warn("Connection failed, retrying...");
753 } else if (max_attempts > 0) {
754 log_warn("Connection failed, retrying (attempt %d/%d)...", attempt + 1, max_attempts);
755 }
756
757 // Apply reconnection delay if configured
758 // Check APP_CALLBACK_BOOL(should_exit) frequently during sleep so SIGTERM can interrupt reconnection attempts
759 unsigned int retry_delay_ms = config->reconnect_delay_ms;
760 if (result == ERROR_RATE_LIMITED || result == ERROR_SESSION_FULL) {
761 unsigned int exponent = attempt > 5 ? 5 : (unsigned int)attempt;
762 unsigned int rejection_delay_ms = 1000U << exponent;
763 if (retry_delay_ms < rejection_delay_ms)
764 retry_delay_ms = rejection_delay_ms;
765 }
766 if (retry_delay_ms > 0) {
767 ASCII_OTEL_SCOPE(RECONNECT_WAIT);
768 ASCII_OTEL_ATTR_INT("reconnect.delay_ms", retry_delay_ms);
769 unsigned int remaining_ms = retry_delay_ms;
770 const unsigned int check_interval_ms = 100; // Check exit flag every 100ms
771
772 while (remaining_ms > 0 && !APP_CALLBACK_BOOL(should_exit)) {
773 unsigned int sleep_ms = (remaining_ms < check_interval_ms) ? remaining_ms : check_interval_ms;
774 APP_CALLBACK_VOID(platform_pump_events);
775 APP_CALLBACK_VOID_UINT(platform_sleep_ms, sleep_ms);
776 remaining_ms -= sleep_ms;
777 }
778 }
779
780 // Continue loop to retry
781 }
782 return result;
783}
784
785static asciichat_error_t client_like_cleanup_session(const session_client_like_config_t *config, client_like_state_t *state, asciichat_error_t result) {
786 ASCII_OTEL_SCOPE(SESSION_CLEANUP);
787 // The statistics worker owns the UI/input path. Stop it before tearing down
788 // the display and capture objects it may still be polling.
790 // A shutdown request is the reason cleanup is most important. Returning
791 // here used to leave the display, capture, audio, and global callbacks live
792 // while main continued through process-wide teardown. That made Ctrl+C
793 // races intermittent and allowed renderer transitions to touch partially
794 // destroyed session state. Cleanup is ordered and idempotent, so continue
795 // through the normal teardown path even after an interrupt.
796 // Preparation can fail before the normal update check wait above.
799 // Stop the terminal writer before restoring output or destroying the display.
803 // Re-enable terminal output for shutdown logs
805
806 g_current_config = (session_client_like_config_t){0};
807 g_render_should_exit = NULL;
808
809#ifndef NDEBUG
810 // Join the registry inspector before freeing the session's synchronization objects.
811 debug_sync_cleanup_thread();
814#endif
815
816 // Stop audio thread before destroying audio context to prevent use-after-free
817 // The audio worker thread may still be logging when we destroy the buffer
819
820 // Stop streams before releasing the local media source.
821 if (state->audio_ctx) {
824 audio_destroy(state->audio_ctx);
825 SAFE_FREE(state->audio_ctx);
826 state->audio_ctx = NULL;
827 }
828 // Destroy display (check global context to see if discovery mode already destroyed it)
829 // Discovery participant mode may destroy the display early and clear the global context
831 if (state->display && state->display == current_global) {
832 // Display is still valid (not destroyed by discovery mode)
834 if (result == ASCIICHAT_OK)
835 result = finish_err;
837 state->display = NULL;
838 } else if (state->display && state->display != current_global) {
839 // Display was already destroyed (by discovery participant mode)
840 // Just NULL it out, don't call destroy again
841 log_debug("[CLEANUP] Display was already destroyed by discovery mode, skipping destroy");
842 state->display = NULL;
843 }
844
845 // Destroy capture
846 if (state->capture) {
848 state->capture = NULL;
849 }
850
851 // Destroy stdin reader if active (stdin render mode)
852 if (g_stdin_reader) {
853 terminal_fd_reader_destroy(g_stdin_reader);
854 g_stdin_reader = NULL;
855 }
856
857 // Clean up probe source if still allocated (shouldn't happen, but be safe)
858 if (state->probe_source) {
860 state->probe_source = NULL;
861 }
862
863 // Disable keepawake (re-allow OS to sleep)
864 log_debug("[CLEANUP] Disabling keepawake");
866 log_debug("[CLEANUP] keepawake disabled");
867
868 // Write newline to separate final frame from prompt (if configured)
869 log_debug("About to check print_newline_on_tty_exit");
871 log_debug("Writing newline");
872 const char newline = '\n';
873 (void)ui_controller_write(STDOUT_FILENO, &newline, 1);
874 }
875
877
878 return result;
879}
880
882 ASCII_OTEL_SCOPE(SESSION_CLIENT_LIKE_RUN);
883 if (!config || !config->run_fn) {
884 return SET_ERRNO(ERROR_INVALID_PARAM, "config or run_fn is NULL");
885 }
886
887 g_current_config = *config;
888 g_render_should_exit = client_like_display_should_exit_adapter;
889 client_like_state_t state = {0};
891 {
892 ASCII_OTEL_SCOPE(SESSION_INITIALIZE);
893 client_like_setup_terminal_logging();
894 result = client_like_setup_keepawake();
895 if (result != ASCIICHAT_OK) {
896 goto cleanup;
897 }
898 result = client_like_validate_mirror_webcam_selection(config);
899 if (result != ASCIICHAT_OK) {
900 goto cleanup;
901 }
902 result = client_like_create_session_display(&state);
903 if (result != ASCIICHAT_OK) {
904 goto cleanup;
905 }
906 // An accepted update prompt ends the session successfully before media setup.
907 bool stop = false;
908 result = client_like_prepare_session_splash(config, &state, &stop);
909 if (result != ASCIICHAT_OK || stop) {
910 goto cleanup;
911 }
912 client_like_configure_session_logging(config);
913 result = client_like_configure_media_capture(config, &state);
914 if (result != ASCIICHAT_OK) {
915 goto cleanup;
916 }
917 result = client_like_create_network_capture(config, &state);
918 if (result != ASCIICHAT_OK) {
919 goto cleanup;
920 }
921 client_like_prepare_session_audio(&state);
922 if (client_like_finish_session_splash(config)) {
923 goto cleanup;
924 }
925 result = client_like_create_local_capture(&state);
926 if (result != ASCIICHAT_OK) {
927 goto cleanup;
928 }
929 client_like_start_session_audio(config, &state);
930 if (SAFE_GETENV("ASCII_CHAT_MCP_WORKER") && state.should_init_audio && GET_OPTION(audio_capture_source) == AUDIO_CAPTURE_SOURCE_MIC && !state.audio_available) {
931 result = SET_ERRNO(ERROR_AUDIO, "Requested microphone capture could not start");
932 goto cleanup;
933 }
934 log_debug("[MARKER] Audio setup complete at line 614");
935 }
936 log_debug("[SETUP_COMPLETE] All setup complete, about to start connection loop");
937 result = client_like_run_session_attempts(config, &state);
938
939cleanup:
940 return client_like_cleanup_session(config, &state, result);
941}
Application-level callbacks for library code.
#define APP_CALLBACK_BOOL(callback_name)
#define APP_CALLBACK_VOID(callback_name)
#define APP_CALLBACK_VOID_UINT(callback_name, value)
void errno_scope_end(asciichat_errno_scope_t scope, asciichat_errno_outcome_t outcome)
bool errno_notifications_suppress(bool suppress)
asciichat_errno_scope_t errno_scope_begin(void)
#define SET_ERRNO(code,...)
#define ASSERT_NO_ERRNO_SINCE(scope)
@ ASCIICHAT_ERRNO_HANDLED
@ ASCIICHAT_ERRNO_DISMISSED
#define LOG_ERRNO_IF_SET(message)
asciichat_error_t session_client_like_run(const session_client_like_config_t *config)
bool(*)(void *) session_client_like_get_render_should_exit(void)
Definition client_like.c:98
#define SESSION_CLIENT_LIKE_WEBCAM_INDEX_BUFFER_SIZE
Definition client_like.c:3
Shared initialization and teardown for client-like modes (mirror, client, discovery).
struct session_client_like_config session_client_like_config_t
@ SESSION_CLIENT_LIKE_KIND_CLIENT
Definition client_like.h:65
@ SESSION_CLIENT_LIKE_KIND_DISCOVERY
Definition client_like.h:66
@ SESSION_CLIENT_LIKE_KIND_MIRROR
Definition client_like.h:64
enum session_client_like_kind session_client_like_kind_t
void session_capture_set_audio_context(session_capture_ctx_t *ctx, void *audio_ctx)
Set the audio context for playback control.
void session_capture_set_audio_updater(session_capture_ctx_t *ctx, session_capture_audio_update_fn update, void *user_data)
📹 Unified media capture abstraction for session-based video sources
void debug_errno_cleanup_thread(void)
Definition errno.c:110
Dedicated worker for all pending errno stacks and handled history. Signal handlers may only request a...
audio_visualization_source_t source
Definition fft.c:25
unsigned int height
Definition fft.c:24
unsigned int width
Definition fft.c:24
bool audio_should_enable_microphone(audio_capture_source_t source, bool has_media_audio)
Determine if microphone should be enabled based on audio source setting and media state.
asciichat_error_t audio_init(audio_context_t *ctx)
Initialize audio context and PortAudio.
asciichat_error_t audio_start_duplex(audio_context_t *ctx)
Start full-duplex audio (simultaneous capture and playback).
void audio_destroy(audio_context_t *ctx)
Destroy audio context and clean up resources.
asciichat_error_t audio_stop_duplex(audio_context_t *ctx)
Stop full-duplex audio.
unsigned int uint32_t
#define SAFE_FREE(ptr)
#define SAFE_GETENV(name)
#define SAFE_CALLOC(count, size, cast)
unsigned long long uint64_t
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:48
@ ERROR_SIGNAL_INTERRUPT
@ ERROR_AUDIO
Definition error_codes.h:66
@ ASCIICHAT_OK
Definition error_codes.h:50
@ ERROR_RATE_LIMITED
Definition error_codes.h:84
@ ERROR_SESSION_FULL
Definition error_codes.h:86
@ ERROR_DISPLAY
@ ERROR_MEDIA_INIT
Definition error_codes.h:69
@ ERROR_USAGE
Definition error_codes.h:52
@ ERROR_INVALID_PARAM
@ ERROR_WEBCAM
Definition error_codes.h:63
#define log_warn(...)
Log a WARN message.
Definition log/log.h:615
void log_set_force_stderr(bool enabled)
Force all terminal log output to stderr.
#define log_info(...)
Log an INFO message.
Definition log/log.h:602
#define log_debug(...)
Log a DEBUG message.
Definition log/log.h:589
void log_set_terminal_output(bool enabled)
Control stderr output to terminal.
bool media_source_has_audio(media_source_t *source)
Check if media source has audio stream.
Definition source.c:774
void media_source_destroy(media_source_t *source)
Destroy media source and free resources.
Definition source.c:486
double media_source_get_video_fps(media_source_t *source)
Get video frame rate in frames per second.
Definition source.c:1015
void media_source_set_audio_context(media_source_t *source, void *audio_ctx)
Set audio context for playback buffer management.
Definition source.c:1072
@ MEDIA_SOURCE_STDIN
Piped or redirected input.
Definition source.h:104
@ MEDIA_SOURCE_WEBCAM
Hardware webcam device.
Definition source.h:102
@ MEDIA_SOURCE_FILE
Media file (video/audio).
Definition source.h:103
@ MEDIA_SOURCE_TEST
Test pattern generator.
Definition source.h:105
uint64_t time_elapsed_ns(uint64_t start_ns, uint64_t end_ns)
Calculate elapsed time with wraparound safety.
Definition time.c:117
#define GET_OPTION(field)
Definition options.h:1333
asciichat_error_t options_set_int(const char *name, int value)
Check if an option was explicitly set via command-line.
Definition rcu.c:887
#define OPT_HEIGHT_DEFAULT
Default terminal height in characters.
Definition options.h:419
#define OPT_WIDTH_DEFAULT
Default terminal width in characters.
Definition options.h:408
@ AUDIO_CAPTURE_SOURCE_MIC
Definition options.h:291
@ MODE_DISCOVERY
Discovery mode - participant that can dynamically become host.
Definition options.h:921
void session_display_destroy(session_display_ctx_t *ctx)
void session_display_set_render_fps(session_display_ctx_t *ctx, uint32_t fps)
Set the render FPS for file output encoding.
session_display_ctx_t * session_display_create(const session_display_config_t *config)
Create a new session display context.
asciichat_error_t session_display_finish_render_file(session_display_ctx_t *ctx)
Destroy session display context and free resources.
void session_display_set_stdin_reader(session_display_ctx_t *ctx, void *reader)
Set stdin frame reader for ASCII-to-video rendering.
bool splash_is_running(void)
Check if splash screen animation is currently running.
Definition splash.c:440
bool update_banner_install_update(void)
void update_banner_cancel_check(void)
struct session_display_ctx session_display_ctx_t
session display config is intentionally a small, concrete record. Fields such as snapshot_mode,...
int splash_intro_done(void)
Signal that intro splash should end (first frame ready to render).
Definition splash.c:421
void update_banner_wait_for_check(void)
Wait for the background update check thread to finish.
bool update_banner_show_prompt(session_display_ctx_t *ctx)
Show the update prompt screen and wait for user input.
void splash_restore_stderr(void)
Restore stderr after splash animation completes.
Definition splash.c:431
int splash_intro_start(session_display_ctx_t *ctx)
Start animated intro screen with rainbow ASCII art (non-blocking).
Definition splash.c:370
session_capture_ctx_t * session_mirror_capture_create(const session_capture_config_t *config)
Create a new session capture context.
session_display_ctx_t * session_display_get_global_context(void)
Get the global display context (used for signal handlers and special cleanup).
struct session_capture_ctx session_capture_ctx_t
session capture config keeps the values that move through the subsystem: type, path,...
bool splash_should_display(bool is_intro)
Render splash header with rainbow animation (callback for terminal_screen).
Definition splash.c:242
void session_display_reset_first_frame(session_display_ctx_t *ctx)
Reset the first frame flag to allow splash screen to show again on next render.
void session_display_set_global_context(session_display_ctx_t *ctx)
Set the global display context (for signal handlers like Ctrl+C).
bool update_banner_has_update(void)
Check if an update is available (thread-safe).
uint32_t session_capture_get_target_fps(session_capture_ctx_t *ctx)
Get the target FPS configured for this capture context.
void splash_wait_for_animation(void)
Wait for splash animation thread to fully exit.
Definition splash.c:446
session_capture_ctx_t * session_network_capture_create(uint32_t target_fps)
Create network mode capture context without media source.
void * session_capture_get_media_source(session_capture_ctx_t *ctx)
Get the underlying media source from capture context.
void session_capture_destroy(session_capture_ctx_t *ctx)
Destroy session capture context and free resources.
terminal_fd_reader_t * session_client_like_get_stdin_reader(void)
asciichat_error_t webcam_list_devices(webcam_device_info_t **out_devices, unsigned int *out_count)
Enumerate available webcam devices.
void webcam_free_device_list(webcam_device_info_t *devices)
Free device list returned by webcam_list_devices().
🔊 Audio Capture and Playback Interface for ascii-chat
Statistics worker for error totals and memory reports. Signal handlers may only request a report.
🆘 Interactive keyboard help overlay for session keyboard shortcuts
void debug_stats_cleanup_thread(void)
void stats_counter_add(stats_scope_t *scope, stats_counter_id_t id, uint64_t amount)
📝 Logging API with multiple log levels and terminal output control
bool should_exit(void)
Definition main.c:248
@ OPTIONS_VALUE_BOOL
@ OPTIONS_ORIGIN_LOCAL
⚙️ Unified options parsing system for ascii-chat with builder pattern and lock-free access
void ascii_otel_error(const char *message)
Definition otel.c:779
void ascii_otel_metric(ascii_otel_metric_t metric, double value)
Definition otel.c:159
#define ASCII_OTEL_SCOPE(id)
Definition otel.h:77
#define ASCII_OTEL_ATTR_INT(key, value)
Definition otel.h:72
#define ASCII_OTEL_ATTR_STRING(key, value)
Definition otel.h:71
uint64_t options_revision(void)
Definition rcu.c:869
asciichat_error_t options_apply_patch(const options_patch_t *patch, options_patch_result_t *result)
Definition rcu.c:826
struct terminal_fd_reader_s terminal_fd_reader_t
void terminal_fd_reader_destroy(terminal_fd_reader_t *reader)
Destroy terminal FD reader.
Definition reader.c:146
asciichat_error_t terminal_fd_reader_create(int fd, int frame_height, terminal_fd_reader_t **out)
Create a terminal FD reader from a file descriptor.
Definition reader.c:33
Read ANSI ASCII frames from file descriptor, chunked by height.
void stats_runtime_stop(void)
Definition runtime.c:611
stats_scope_t * stats_runtime_scope(void)
Definition runtime.c:78
media_source_t * media_source_create_interruptible(media_source_type_t type, const char *path, bool(*cancel)(void *), void *data)
Definition source.c:215
🎬 Unified media source abstraction for webcam, files, and stdin
Intro and status screen display for ascii-chat with animated rainbow effects.
void audio_stop_thread(client_connection_t *connection)
Stop audio capture thread.
ascii-chat Client Audio Processing Management Interface
asciichat_error_t session_display_init_render_file(session_display_ctx_t *ctx, uint32_t fps)
Perform complete terminal reset.
🖥️ Unified terminal display abstraction for session-based rendering
Centralized render loop abstraction for all display modes.
void platform_disable_keepawake(void)
int platform_enable_keepawake(void)
bool terminal_is_stdin_tty(void)
bool terminal_is_stdout_tty(void)
bool terminal_should_force_stderr(void)
🔤 String Manipulation and Shell Escaping Utilities
When code needs to work with asciichat errno snapshot, it keeps total, copied here instead of scatter...
When code needs to work with audio device info, it keeps index, max_input_channels,...
bool monitor_local_media
Play local file audio in solo sessions.
void * capture_media_source
Borrowed local media audio; configure before starting duplex.
bool running
True if duplex stream is active.
bool playback_only
True for playback-only mode (mirror), skip microphone capture.
session_capture_config_t capture_config
const char * media_url_val
audio_context_t * audio_ctx
session_capture_ctx_t * capture
const char * media_file_val
media_source_t * probe_source
session_display_ctx_t * display
uint64_t audio_failed_revision
Media source for video and audio capture.
Definition source.c:47
Media-source and loader settings used to create a session capture context.
media_source_type_t type
Media source type (WEBCAM, FILE, STDIN, TEST).
const char * path
Device index (for webcam) or file path (for file sources).
uint32_t target_fps
Target frame rate in FPS (e.g., 60 for display, 144 for network).
double initial_seek_timestamp
Seek to this timestamp after opening media (0.0 = no seek).
session_capture_should_exit_fn should_exit_callback
Optional cancellation callback, also invoked by media loader threads. The callback and its data must ...
void * callback_data
Opaque data passed to should_exit_callback.
bool resize_for_network
Resize frames to network-optimal dimensions (MAX_FRAME_WIDTH x MAX_FRAME_HEIGHT).
bool loop
Enable loop playback for file sources.
bool enable_audio
Enable audio capture from media source.
session_client_like_kind_t kind
bool(* should_reconnect_callback)(asciichat_error_t last_error, int attempt_number, void *user_data)
unsigned int reconnect_delay_ms
session_client_like_run_fn run_fn
asciichat_error_t(* prepare_fn)(void *user_data)
Terminal-rendering and snapshot settings used to create a display context.
void * callback_data
Opaque data passed to should_exit_callback.
palette_type_t palette_type
Palette type for ASCII rendering.
bool defer_render_file
Defer render-file encoder creation until media timing is known.
const char * custom_palette
Custom palette characters (required if palette_type == PALETTE_CUSTOM).
session_display_should_exit_fn should_exit_callback
Optional: callback to check if initialization should be cancelled (e.g., shutdown signal).
bool snapshot_mode
Enable snapshot mode (single frame capture).
terminal_color_mode_t color_mode
Color mode override (TERM_COLOR_AUTO for auto-detection).
test pattern is intentionally a small, concrete record. Fields such as image, rgba give callers the i...
Webcam device information structure.
Definition webcam.h:93
void ui_controller_remove(ui_screen_t screen)
void ui_controller_set_effective_fps(unsigned int fps)
void ui_controller_redraw(void)
void ui_controller_title_activity(ui_title_activity_t activity)
asciichat_error_t ui_controller_write(int fd, const char *data, size_t len)
@ UI_TITLE_CONNECTING
@ UI_TITLE_RECONNECTING
@ UI_TITLE_LOADING_URL
@ UI_SCREEN_SPLASH
Update available prompt screen shown after splash.
🌐 Production-Grade URL Validation and Parsing