ascii-chat
Binary v0.16.114-28-gf5913657f
Library lib/v0.3.2-3302-gf5913657f
Video chat in your terminal
Loading...
Searching...
No Matches
protocol_adapter.h File Reference

Go to the source code of this file.

Functions

asciichat_error_t client_protocol_start (client_connection_t *connection)
bool client_protocol_connection_lost (client_connection_t *connection)

Function Documentation

◆ client_protocol_connection_lost()

bool client_protocol_connection_lost ( client_connection_t * connection)

Definition at line 569 of file protocol_adapter.c.

569 {
570 return atomic_load_bool(&connection->data_thread_exited) ||
571 server_connection_is_lost(connection);
572}
bool atomic_load_bool(atomic_t *a)
Atomic pointer wrapper.
bool server_connection_is_lost(client_connection_t *connection)
Check if connection loss has been detected.

References atomic_load_bool(), client_connection::data_thread_exited, and server_connection_is_lost().

◆ client_protocol_start()

asciichat_error_t client_protocol_start ( client_connection_t * connection)

Definition at line 425 of file protocol_adapter.c.

425 {
426 log_warn("[FRAME_RECV_INIT] 🟢 PROTOCOL_START: Starting client protocol "
427 "initialization");
428
429 // The server can send Opus audio as soon as streaming starts. Initialize the
430 // decoder before either transport's receive worker can dispatch those
431 // packets.
432 asciichat_error_t audio_result = audio_connection_init(connection);
433 if (audio_result != ASCIICHAT_OK)
434 return audio_result;
435 if (client_connection_checkpoint("audio-pipeline") != ASCIICHAT_OK)
436 return SET_ERRNO(ERROR_THREAD, "Could not start client protocol workers");
437
438 acip_client_callbacks_t control = g_acip_client_callbacks;
439 control.app_ctx = connection;
441 &connection->protocol, server_connection_get_transport(connection),
442 (session_protocol_media_t){.terminal_output = true,
443 .on_frame = client_present_frame,
444 .on_samples = client_play_samples,
445 .decode_opus = client_decode_opus,
446 .on_audio_packet = client_track_audio,
447 .context = connection,
448 .frame_workspace =
449 &connection->decoded_ascii_workspace},
450 control);
451
452 // Reset display state for new connection
454
455 // Start receiving before the capability/settings exchange. The server may
456 // reply to the initial settings packet immediately, and that reply must not
457 // wait for the worker that dispatches it.
458 atomic_store_bool(&connection->data_thread_exited, false);
460 data_reception_thread_func, connection, 1,
461 "data_reception") != ASCIICHAT_OK) {
462 log_error("[FRAME_RECV_INIT] Failed to start data reception thread");
463 LOG_ERRNO_IF_SET("Data reception thread creation failed");
464 return SET_ERRNO(ERROR_THREAD, "Could not start client protocol workers");
465 }
466
468 return SET_ERRNO(ERROR_THREAD, "Could not start client protocol workers");
469
470 // Send CLIENT_CAPABILITIES packet (after crypto is ready for both TCP and WS)
471 // Server expects this as the first packet after crypto handshake
472 log_debug("[FRAME_RECV_INIT] Sending CLIENT_CAPABILITIES (terminal size)");
474 connection, (int)terminal_get_effective_width(GET_OPTION(width), 80),
476 if (cap_result != ASCIICHAT_OK) {
477 log_error("[FRAME_RECV_INIT] Failed to send terminal size capabilities");
478 return client_startup_peer_error(connection, cap_result);
479 }
480
481 // Capabilities establish the viewer geometry, but renderer preferences are
482 // carried by SETTINGS_SYNC. Send them before STREAM_START so the server
483 // cannot render the first frames from its zero-initialized (luminance)
484 // fallback while the asynchronous settings update is still in flight.
485 options_snapshot_t initial_settings;
486 if (options_read_snapshot(&initial_settings) != ASCIICHAT_OK)
487 return client_startup_peer_error(connection, ERROR_CONFIG);
488 asciichat_error_t settings_result =
490 &initial_settings.values, 1, UINT32_MAX);
491 if (settings_result != ASCIICHAT_OK) {
492 log_error("[FRAME_RECV_INIT] Failed to send initial rendering settings");
493 return client_startup_peer_error(connection, settings_result);
494 }
495
496 // Send STREAM_START packet with combined stream types
497 uint32_t stream_types = STREAM_TYPE_VIDEO; // Always have video
498 if (GET_OPTION(audio_enabled)) {
499 stream_types |= STREAM_TYPE_AUDIO; // Add audio if enabled
500 }
501 log_debug("[FRAME_RECV_INIT] Sending STREAM_START: types=0x%x (video%s)",
502 stream_types, (stream_types & STREAM_TYPE_AUDIO) ? "+audio" : "");
503 asciichat_error_t stream_result =
504 threaded_send_stream_start_packet(connection, stream_types);
505 if (stream_result != ASCIICHAT_OK) {
506 log_error("[FRAME_RECV_INIT] Failed to send stream start");
507 return client_startup_peer_error(connection, stream_result);
508 }
509
510 // Start webcam capture thread
511 log_debug("Starting webcam capture thread...");
512 if (capture_start_thread(connection) != 0) {
513 log_error("Failed to start webcam capture thread");
514 return SET_ERRNO(ERROR_THREAD, "Could not start client protocol workers");
515 }
516 log_debug("Webcam capture thread started successfully");
518 return SET_ERRNO(ERROR_THREAD, "Could not start client protocol workers");
519
520 // Start local media audio only after the connection succeeds. Starting the
521 // worker during client initialization would consume file audio while dialing.
522 audio_context_t *audio_context = connection->audio_context;
523 if (audio_context && audio_context->initialized) {
525 audio_context);
526 audio_context->capture_media_source =
528 if (audio_start_duplex(audio_context) != ASCIICHAT_OK) {
529 log_error("Failed to start full-duplex audio");
530 return SET_ERRNO(ERROR_THREAD, "Could not start client protocol workers");
531 }
532 }
533
534 if (client_connection_checkpoint("audio-duplex") != ASCIICHAT_OK)
535 return SET_ERRNO(ERROR_THREAD, "Could not start client protocol workers");
536
537 // Initialize audio sender thread BEFORE starting audio capture
538 // This ensures sender is ready when capture thread starts queueing packets
539 // Must happen after connection succeeds to prevent deadlock if connection
540 // fails
541 if (audio_sender_init(connection) != ASCIICHAT_OK)
542 return SET_ERRNO(ERROR_THREAD, "Could not start client protocol workers");
543
544 // Start audio capture thread if audio is enabled
545 log_debug("Starting audio capture thread...");
546 if (audio_start_thread(connection) != 0) {
547 log_error("Failed to start audio capture thread");
548 return SET_ERRNO(ERROR_THREAD, "Could not start client protocol workers");
549 }
550 log_debug("Audio capture thread started successfully (or skipped if audio "
551 "disabled)");
552
553 if (client_connection_checkpoint("audio-capture") != ASCIICHAT_OK)
554 return SET_ERRNO(ERROR_THREAD, "Could not start client protocol workers");
555
556 // Start keepalive/ping thread to prevent server timeout
557 log_debug("Starting keepalive/ping thread...");
558 if (keepalive_start_thread(connection) != 0) {
559 log_error("Failed to start keepalive/ping thread");
560 return SET_ERRNO(ERROR_THREAD, "Could not start client protocol workers");
561 }
562 log_debug("Keepalive/ping thread started successfully");
563
564 if (client_connection_checkpoint("keepalive") != ASCIICHAT_OK)
565 return SET_ERRNO(ERROR_THREAD, "Could not start client protocol workers");
566 return ASCIICHAT_OK;
567}
#define SET_ERRNO(code,...)
#define LOG_ERRNO_IF_SET(message)
void atomic_store_bool(atomic_t *a, bool value)
Atomically store a boolean value.
int capture_start_thread(client_connection_t *connection)
Start capture thread.
void session_capture_set_audio_context(session_capture_ctx_t *ctx, void *audio_ctx)
Set the audio context for playback control.
void session_protocol_init(session_protocol_t *protocol, acip_transport_t *transport, session_protocol_media_t media, acip_client_callbacks_t control)
unsigned int height
Definition fft.c:24
unsigned int width
Definition fft.c:24
asciichat_error_t audio_start_duplex(audio_context_t *ctx)
Start full-duplex audio (simultaneous capture and playback).
void display_reset_for_new_connection(client_connection_t *connection)
Reset display state for new connection.
unsigned int uint32_t
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:48
@ ASCIICHAT_OK
Definition error_codes.h:50
@ ERROR_CONFIG
Definition error_codes.h:56
@ ERROR_THREAD
#define log_warn(...)
Log a WARN message.
Definition log/log.h:591
#define log_error(...)
Log an ERROR message.
Definition log/log.h:604
#define log_debug(...)
Log a DEBUG message.
Definition log/log.h:565
#define STREAM_TYPE_VIDEO
Video stream.
Definition packet.h:1016
#define STREAM_TYPE_AUDIO
Audio stream.
Definition packet.h:1017
#define GET_OPTION(field)
Definition options.h:1339
void * session_capture_get_media_source(session_capture_ctx_t *ctx)
Get the underlying media source from capture context.
int keepalive_start_thread(client_connection_t *connection)
Start keepalive/ping thread.
Definition keepalive.c:219
asciichat_error_t acip_settings_send(acip_transport_t *transport, const options_t *values, uint32_t request, uint32_t expected_revision)
asciichat_error_t options_read_snapshot(options_snapshot_t *snapshot)
Definition rcu.c:934
asciichat_error_t audio_connection_init(client_connection_t *connection)
int audio_start_thread(client_connection_t *connection)
Start audio capture thread.
asciichat_error_t audio_sender_init(client_connection_t *connection)
Initialize async audio sender queue and thread.
asciichat_error_t client_connection_checkpoint(const char *stage)
asciichat_error_t threaded_send_terminal_size_with_auto_detect(client_connection_t *connection, unsigned short width, unsigned short height)
Thread-safe terminal size packet transmission with auto-detection.
asciichat_error_t threaded_send_stream_start_packet(client_connection_t *connection, uint32_t stream_type)
Thread-safe stream start packet transmission.
acip_transport_t * server_connection_get_transport(client_connection_t *connection)
Get ACIP transport instance.
unsigned short int terminal_get_effective_height(int height_option, unsigned short fallback_height)
unsigned short int terminal_get_effective_width(int width_option, unsigned short fallback_width)
Client-side packet handler callbacks.
Definition handlers.h:55
void * app_ctx
Application context (passed to all callbacks).
Definition handlers.h:117
When code needs to work with audio device info, it keeps index, max_input_channels,...
bool initialized
True if context has been initialized.
thread_pool_t * client_worker_pool
session_protocol_t protocol
session_capture_ctx_t * capture_context
buffer_workspace_t decoded_ascii_workspace
audio_context_t * audio_context
asciichat_error_t thread_pool_spawn(thread_pool_t *pool, void *(*thread_func)(void *), void *thread_arg, int stop_id, const char *thread_name)
Spawn a worker thread in the pool.

References acip_settings_send(), acip_client_callbacks_t::app_ctx, ASCIICHAT_OK, atomic_store_bool(), audio_connection_init(), client_connection::audio_context, audio_sender_init(), audio_start_duplex(), audio_start_thread(), client_connection::capture_context, audio_context_t::capture_media_source, capture_start_thread(), client_connection_checkpoint(), client_connection::client_worker_pool, client_connection::data_thread_exited, client_connection::decoded_ascii_workspace, display_reset_for_new_connection(), ERROR_CONFIG, ERROR_THREAD, GET_OPTION, height, audio_context_t::initialized, keepalive_start_thread(), log_debug, LOG_ERRNO_IF_SET, log_error, log_warn, options_read_snapshot(), client_connection::protocol, server_connection_get_transport(), session_capture_get_media_source(), session_capture_set_audio_context(), session_protocol_init(), SET_ERRNO, STREAM_TYPE_AUDIO, STREAM_TYPE_VIDEO, terminal_get_effective_height(), terminal_get_effective_width(), thread_pool_spawn(), threaded_send_stream_start_packet(), threaded_send_terminal_size_with_auto_detect(), options_snapshot_t::values, and width.

Referenced by client_connection_start().