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 File Reference

Shared initialization and teardown for client-like modes. More...

Go to the source code of this file.

Data Structures

struct  client_like_state_t

Macros

#define SESSION_CLIENT_LIKE_WEBCAM_INDEX_BUFFER_SIZE   32

Functions

terminal_fd_reader_t * session_client_like_get_stdin_reader (void)
asciichat_error_t session_client_like_run (const session_client_like_config_t *config)

Variables

bool(*)(void *) session_client_like_get_render_should_exit (void)

Detailed Description

Shared initialization and teardown for client-like modes.

Definition in file client_like.c.

Macro Definition Documentation

◆ SESSION_CLIENT_LIKE_WEBCAM_INDEX_BUFFER_SIZE

#define SESSION_CLIENT_LIKE_WEBCAM_INDEX_BUFFER_SIZE   32

Definition at line 3 of file client_like.c.

Function Documentation

◆ session_client_like_run()

asciichat_error_t session_client_like_run ( const session_client_like_config_t * config)

Run a client-like mode with fully shared initialization and teardown

This function orchestrates the complete lifecycle of client-like modes:

Initialization (automatic from GET_OPTION())

Terminal:

  • Forces stderr when stdout is piped (terminal_should_force_stderr)
  • Disables terminal logging during rendering, re-enables on exit
  • Handles snapshot mode log visibility

Keepawake:

  • Validates –keepawake / –no-keepawake mutual exclusivity
  • Calls platform_enable_keepawake() unless –no-keepawake set
  • Disables keepawake on exit

Splash Screen:

  • Creates temporary display context
  • Calls splash_intro_start() to show splash animation
  • Holds splash visible during media probing and audio init
  • Calls splash_intro_done() when capture and display are ready

Media / Webcam (session_capture_ctx_t):

  • Selects source type from options (priority order):
    1. –url → MEDIA_SOURCE_FILE (FFmpeg handles http/rtsp/rtmp)
    2. –file → MEDIA_SOURCE_FILE; "-" → MEDIA_SOURCE_STDIN
    3. –test-pattern → MEDIA_SOURCE_TEST
    4. (default) → MEDIA_SOURCE_WEBCAM
  • FPS: uses GET_OPTION(fps) if > 0, otherwise probes with temporary media source
  • Applies GET_OPTION(media_seek_timestamp) as initial seek
  • Applies GET_OPTION(media_loop) for file looping
  • Calls session_capture_create() to open webcam or media file
  • Destroys capture on exit

Audio (audio_context_t):

Display (session_display_ctx_t):

  • Calls session_display_create() with snapshot and color settings
  • Passes audio context if initialized
  • Destroys display on exit

Mode-Specific Loop

Calls config->run_fn(capture, display, config->run_user_data) once all initialization is complete. This is where each mode calls session_render_loop() or runs its own protocol-thread-driven loop.

Cleanup (always runs, even on error)

Cleanup sequence (order is critical):

  1. Re-enable terminal logging
  2. audio_terminate_portaudio_final() - Free PortAudio device resources FIRST
  3. audio_stop_duplex() + audio_destroy() + SAFE_FREE
  4. session_display_destroy()
  5. session_capture_destroy()
  6. media_source_destroy() - Free capture buffers and source state
  7. session_log_buffer_destroy()
  8. platform_disable_keepawake()
  9. Print '
    ' to STDOUT_FILENO if print_newline_on_tty_exit && TTY
Parameters
configMode configuration. Must not be NULL. run_fn must not be NULL.
Returns
ASCIICHAT_OK on success, or the first error code from initialization or from run_fn. Cleanup always runs regardless of return value.

Definition at line 881 of file client_like.c.

881 {
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}
#define SET_ERRNO(code,...)
#define SAFE_GETENV(name)
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:48
@ ERROR_AUDIO
Definition error_codes.h:66
@ ASCIICHAT_OK
Definition error_codes.h:50
@ ERROR_INVALID_PARAM
#define log_debug(...)
Log a DEBUG message.
Definition log/log.h:589
#define GET_OPTION(field)
Definition options.h:1333
@ AUDIO_CAPTURE_SOURCE_MIC
Definition options.h:291
#define ASCII_OTEL_SCOPE(id)
Definition otel.h:77
session_client_like_run_fn run_fn

References ASCII_OTEL_SCOPE, ASCIICHAT_OK, client_like_state_t::audio_available, AUDIO_CAPTURE_SOURCE_MIC, ERROR_AUDIO, ERROR_INVALID_PARAM, GET_OPTION, log_debug, session_client_like_config::run_fn, SAFE_GETENV, SET_ERRNO, and client_like_state_t::should_init_audio.

Referenced by client_main(), discovery_main(), and mirror_main().

Variable Documentation

◆ session_client_like_get_render_should_exit

bool(*)(void *) session_client_like_get_render_should_exit(void) ( void )

Definition at line 98 of file client_like.c.

98 {
99 return g_render_should_exit;
100}