|
ascii-chat 0.6.0
Real-time terminal-based video chat with ASCII art conversion
|
🎨 Per-client rendering threads: 60fps video and 100fps audio processing with rate limiting More...
Go to the source code of this file.
Macros | |
| #define | OPUS_FRAME_SAMPLES 960 |
Functions | |
| void * | client_video_render_thread (void *arg) |
| Interruptible sleep function with platform-specific optimizations. | |
| void * | client_audio_render_thread (void *arg) |
| Main audio rendering thread function for individual clients. | |
| int | create_client_render_threads (server_context_t *server_ctx, client_info_t *client) |
| Create and initialize per-client rendering threads. | |
| void | stop_client_render_threads (client_info_t *client) |
| Stop and cleanup per-client rendering threads. | |
Variables | |
| rwlock_t | g_client_manager_rwlock |
| Reader-writer lock protecting the global client manager. | |
| atomic_bool | g_server_should_exit |
| Global shutdown flag from main.c - coordinate graceful thread termination. | |
| mixer_t * | g_audio_mixer |
| Global audio mixer from main.c - provides multi-client audio mixing. | |
🎨 Per-client rendering threads: 60fps video and 100fps audio processing with rate limiting
PER-CLIENT THREAD MODEL: Each connected client spawns exactly 2 dedicated threads:
LINEAR SCALING:
TIMING PRECISION:
VIDEO RATE LIMITING:
AUDIO RATE LIMITING:
PER-CLIENT MUTEX PROTECTION: All client state access uses the snapshot pattern:
CRITICAL SYNCHRONIZATION POINTS:
SHUTDOWN SEQUENCE:
INTERRUPTIBLE OPERATIONS:
CROSS-PLATFORM TIMING:
THREAD MANAGEMENT:
The original server.c contained rendering logic mixed with connection management, making it impossible to:
This separation provides:
Definition in file render.c.
| #define OPUS_FRAME_SAMPLES 960 |
| void * client_audio_render_thread | ( | void * | arg | ) |
Main audio rendering thread function for individual clients.
This is the audio processing thread that generates personalized audio mixes for a specific client at 100fps (10ms intervals, 480 samples @ 48kHz). Each client receives audio from all other clients while excluding their own audio to prevent echo and feedback.
ECHO PREVENTION:
TIMING PRECISION:
BUFFER MANAGEMENT:
STATE SYNCHRONIZATION:
MIXER COORDINATION:
AUDIO MIXING:
AUDIO DELIVERY:
| arg | Pointer to client_info_t for the target client |
Definition at line 673 of file render.c.
References client_info::active, asciichat_errno_cleanup(), client_info::audio_queue, AUDIO_RENDER_FPS, client_info::audio_render_thread_running, audio_ring_buffer_available_read(), audio_ring_buffer_read(), client_info::client_id, client_info::client_state_mutex, client_info::display_name, fps_frame(), fps_init(), g_audio_mixer, g_server_should_exit, GET_OPTION, INVALID_SOCKET_VALUE, log_debug, log_debug_every, log_error, log_info, log_info_every, LOG_RATE_DEFAULT, LOG_RATE_FAST, LOG_RATE_SLOW, log_warn, log_warn_every, mixer_t::max_sources, mixer_process_excluding_source(), mutex_lock, mutex_unlock, OPUS_APPLICATION_AUDIO, opus_codec_create_encoder(), opus_codec_destroy(), opus_codec_encode(), OPUS_FRAME_SAMPLES, packet_queue_enqueue(), packet_queue_size(), PACKET_TYPE_AUDIO_OPUS, platform_sleep_usec(), SAFE_MEMCPY, SAFE_MEMSET, SAFE_STRNCPY, client_info::shutting_down, client_info::socket, mixer_t::source_buffers, and mixer_t::source_ids.
Referenced by create_client_render_threads().
| void * client_video_render_thread | ( | void * | arg | ) |
Interruptible sleep function with platform-specific optimizations.
Provides a sleep mechanism that can be interrupted by the global shutdown signal, enabling responsive thread termination. The implementation varies by platform to optimize for responsiveness vs CPU usage.
WINDOWS IMPLEMENTATION:
POSIX IMPLEMENTATION (Linux/macOS):
PERFORMANCE CHARACTERISTICS:
ERROR HANDLING:
| usec | Sleep duration in microseconds |
Main video rendering thread function for individual clients
This is the core video processing thread that generates personalized ASCII art frames for a specific client at 60fps. Each connected client gets their own dedicated video thread, providing linear performance scaling and personalized rendering based on terminal capabilities.
TIMING PRECISION:
CLIENT-SPECIFIC PROCESSING:
STATE SYNCHRONIZATION:
SHUTDOWN COORDINATION:
FRAME GENERATION:
FRAME DELIVERY:
| arg | Pointer to client_info_t for the target client |
Definition at line 359 of file render.c.
References client_info::active, video_frame_buffer_t::allocated_buffer_size, any_clients_sending_video(), asciichat_errno_cleanup(), video_frame_t::capture_timestamp_us, client_info::client_id, create_mixed_ascii_frame_for_client(), video_frame_t::data, terminal_capabilities_t::desired_fps, format_bytes_pretty(), fps_frame(), fps_init(), g_server_should_exit, client_info::has_terminal_caps, client_info::height, INVALID_SOCKET_VALUE, client_info::last_rendered_grid_sources, log_debug, log_debug_every, log_error, log_info, LOG_RATE_FAST, LOG_RATE_NORMAL, log_warn, client_info::outgoing_video_buffer, platform_sleep_usec(), SAFE_FREE, client_info::shutting_down, video_frame_t::size, client_info::socket, client_info::terminal_caps, video_frame_begin_write(), video_frame_commit(), VIDEO_RENDER_FPS, client_info::video_render_thread_running, and client_info::width.
Referenced by create_client_render_threads().
| int create_client_render_threads | ( | server_context_t * | server_ctx, |
| client_info_t * | client | ||
| ) |
Create and initialize per-client rendering threads.
Sets up the complete per-client threading infrastructure including both video and audio rendering threads plus all necessary synchronization primitives. This function is called once per client during connection establishment.
PER-CLIENT MUTEXES: Each client gets dedicated mutexes for fine-grained locking:
THREAD RUNNING FLAGS:
PARTIAL FAILURE HANDLING: If thread creation fails partway through:
RESOURCE LEAK PREVENTION:
| client | Target client for thread creation |
Definition at line 1121 of file render.c.
References ASCIICHAT_OK, client_info::audio_render_thread_running, client_audio_render_thread(), client_info::client_id, client_video_render_thread(), log_debug, log_error, client_info::socket, server_context_t::tcp_server, tcp_server_spawn_thread(), and client_info::video_render_thread_running.
| void stop_client_render_threads | ( | client_info_t * | client | ) |
Stop and cleanup per-client rendering threads.
Performs graceful shutdown of both video and audio rendering threads for a specific client, including proper thread joining and resource cleanup. This function ensures deterministic cleanup without resource leaks.
THREAD COORDINATION:
DETERMINISTIC CLEANUP:
THREAD JOIN FAILURES:
NULL CLIENT HANDLING:
MUTEX CLEANUP:
THREAD HANDLE MANAGEMENT:
| client | Target client for thread cleanup |
Definition at line 1255 of file render.c.
References asciichat_thread_init(), asciichat_thread_is_initialized(), asciichat_thread_join(), client_info::audio_render_thread, client_info::audio_render_thread_running, client_info::client_id, ERROR_INVALID_PARAM, g_server_should_exit, log_debug, log_error, log_warn, SAFE_STRERROR, SET_ERRNO, client_info::video_render_thread, and client_info::video_render_thread_running.
|
extern |
Global audio mixer from main.c - provides multi-client audio mixing.
Audio render threads use this mixer to combine audio from multiple clients while excluding the target client's own audio (prevents echo/feedback).
Global audio mixer from main.c - provides multi-client audio mixing.
Global audio mixer from main.c.
The mixer combines audio streams from multiple clients, excluding each client's own audio from their outbound stream (preventing echo). Created once during server initialization and shared by all audio render threads.
THREAD SAFETY: The mixer itself is thread-safe and can be used concurrently by multiple render.c audio threads without external synchronization.
Definition at line 158 of file server/main.c.
Referenced by client_audio_render_thread(), and server_main().
|
extern |
Reader-writer lock protecting the global client manager.
This lock enables high-performance concurrent access patterns:
USAGE PATTERN:
Definition at line 204 of file src/server/client.c.
|
extern |
Global shutdown flag from main.c - coordinate graceful thread termination.
All render threads monitor this flag to detect server shutdown and exit their processing loops gracefully. This prevents resource leaks and ensures clean shutdown behavior.
Global shutdown flag from main.c - coordinate graceful thread termination.
Global shutdown flag from main.c - used to avoid error spam during shutdown.
Global shutdown flag from main.c.
This flag is the primary coordination mechanism for clean server shutdown. It's atomic to ensure thread-safe access without mutexes, as it's checked frequently in tight loops across all worker threads.
USAGE PATTERN:
Definition at line 135 of file server/main.c.
Referenced by client_audio_render_thread(), client_video_render_thread(), and stop_client_render_threads().