|
ascii-chat 0.8.38
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. | |
🎨 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:
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 src/server/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 723 of file src/server/render.c.
References adaptive_sleep_do(), adaptive_sleep_init(), asciichat_errno_destroy(), AUDIO_RENDER_FPS, audio_ring_buffer_available_read(), audio_ring_buffer_read(), fps_frame_ns(), fps_init(), g_audio_mixer, g_server_should_exit, mixer_process_excluding_source(), opus_codec_create_encoder(), opus_codec_destroy(), opus_codec_encode(), OPUS_FRAME_SAMPLES, packet_queue_enqueue(), packet_queue_size(), and time_get_ns().
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 338 of file src/server/render.c.
References adaptive_sleep_do(), adaptive_sleep_init(), any_clients_sending_video(), asciichat_errno_destroy(), create_mixed_ascii_frame_for_client(), format_bytes_pretty(), format_duration_ns(), fps_frame_ns(), fps_init(), g_server_should_exit, time_get_ns(), video_frame_begin_write(), video_frame_commit(), and VIDEO_RENDER_FPS.
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 1124 of file src/server/render.c.
References client_audio_render_thread(), client_video_render_thread(), safe_snprintf(), server_context_t::tcp_server, and tcp_server_spawn_thread().
| 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 1258 of file src/server/render.c.
References asciichat_thread_join(), and g_server_should_exit.