ascii-chat
Binary v0.16.50-198-g78f51665f
Library lib/v0.3.2-2240-g78f51665f
Video chat in your terminal
Loading...
Searching...
No Matches
src/server/client.c
Go to the documentation of this file.
1
2/* Storage capacities in elements; text buffers include the terminating NUL. */
3#define SERVER_CLIENT_PRETTY_BUFFER_SIZE 64
4
5/* Existing buffer budgets in bytes, including NUL for text storage. */
6#define SERVER_OPUS_BATCH_OVERHEAD 20
7
9#include <ascii-chat/otel.h>
10#include "session/server_like.h"
13/* Media client state and render-worker ownership. The network runtime owns
14 * establishment, packet reception, transport callbacks, and connection teardown. */
15
16#include <libplatform/atomic.h>
18#include <stdio.h>
19#include <string.h>
20#include <time.h>
21#include <errno.h>
22#ifndef _WIN32
23#include <netinet/tcp.h>
24#endif
25
26#include "client.h"
27#include "main.h"
28#include "protocol.h"
29#include "render.h"
30#include "stream.h"
31#include "connections.h"
37#include <ascii-chat/common.h>
42#include <options/options.h>
43#include <options/rcu.h> // For RCU-based options access
44#include <ascii-chat/buffer.h>
58#include <ascii-chat/uthash.h>
62#include <libplatform/compiler.h>
63#include <libplatform/system.h>
64#include <libplatform/terminal.h>
67#include <libplatform/socket.h>
70#include <ascii-chat/log/io.h>
71
72// FFmpeg headers for H.265 decoding
73#include <libavcodec/avcodec.h>
74#include <libavutil/frame.h>
75#include <libavutil/imgutils.h>
76#include <libavutil/pixfmt.h>
77#include <libswscale/swscale.h>
78
79// Debug flags
80#define DEBUG_NETWORK 1
81#define DEBUG_THREADS 1
82#define DEBUG_MEMORY 1
83
84// =============================================================================
85// Packet Handler Dispatch (O(1) hash table lookup)
86// =============================================================================
87
88typedef void (*client_packet_handler_t)(client_info_t *client, const void *data, size_t len);
89
90#define CLIENT_DISPATCH_HASH_SIZE 32
91#define CLIENT_DISPATCH_HANDLER_COUNT 14
92
100
101#define CLIENT_DISPATCH_HASH(type) ((type) % CLIENT_DISPATCH_HASH_SIZE)
102
103static inline int client_dispatch_hash_lookup(const client_dispatch_entry_t *table, packet_type_t type) {
105 for (int i = 0; i < CLIENT_DISPATCH_HASH_SIZE; i++) {
106 uint32_t slot = (h + i) % CLIENT_DISPATCH_HASH_SIZE;
107 if (table[slot].key == 0)
108 return -1;
109 if (table[slot].key == type)
110 return table[slot].handler_idx;
111 }
112 return -1;
113}
114
115// Handler array (indexed by hash lookup result)
116static const client_packet_handler_t g_client_dispatch_handlers[CLIENT_DISPATCH_HANDLER_COUNT] = {
131};
132
133// Hash table mapping packet type -> handler index
134// clang-format off
135static const client_dispatch_entry_t g_client_dispatch_hash[CLIENT_DISPATCH_HASH_SIZE] = {
136 [0] = {PACKET_TYPE_AUDIO_BATCH, 2}, // hash(4000)=0
137 [1] = {PACKET_TYPE_PROTOCOL_VERSION, 0}, // hash(1)=1
138 [2] = {PACKET_TYPE_AUDIO_OPUS_BATCH, 3}, // hash(4001)=1, probed->2
139 [8] = {PACKET_TYPE_CLIENT_CAPABILITIES, 8}, // hash(5000)=8
140 [9] = {PACKET_TYPE_PING, 9}, // hash(5001)=9
141 [10] = {PACKET_TYPE_PONG, 10}, // hash(5002)=10
142 [11] = {PACKET_TYPE_CLIENT_JOIN, 4}, // hash(5003)=11
143 [12] = {PACKET_TYPE_CLIENT_LEAVE, 5}, // hash(5004)=12
144 [13] = {PACKET_TYPE_STREAM_START, 6}, // hash(5005)=13
145 [14] = {PACKET_TYPE_STREAM_STOP, 7}, // hash(5006)=14
146 [20] = {PACKET_TYPE_REMOTE_LOG, 11}, // hash(2004)=20
147 [25] = {PACKET_TYPE_IMAGE_FRAME, 1}, // hash(3001)=25
148 [27] = {PACKET_TYPE_IMAGE_FRAME_H264, 13},
149 [26] = {PACKET_TYPE_IMAGE_FRAME_H265, 12}, // hash(3002)=26
150};
151// clang-format on
152
153// Forward declarations for static helper functions
154static inline void cleanup_client_all_buffers(client_info_t *client);
155
156static void handle_client_error_packet(client_info_t *client, const void *data, size_t len) {
157 asciichat_error_t reported_error = ASCIICHAT_OK;
158 char message[MAX_ERROR_MESSAGE_LENGTH + 1] = {0};
159
160 asciichat_error_t parse_result = packet_parse_error_message(data, len, &reported_error, message, sizeof(message), NULL);
161 const char *client_id = client ? client->client_id : "unknown";
162
163 if (parse_result != ASCIICHAT_OK) {
164 log_warn("Failed to parse error packet from client %s: %s", client_id, asciichat_error_format(NULL, parse_result));
165 return;
166 }
167
168 reported_error = network_error_public_code(reported_error);
169 log_error("Client %s reported %s", client_id, asciichat_error_format(NULL, reported_error));
171 SET_ERRNO(reported_error, "Client %s notification: %s", client_id, asciichat_error_string(reported_error));
173}
174
193
208
211
212// Forward declarations for internal functions
214static int start_client_threads(server_context_t *server_ctx, client_info_t *client);
215
216static void initialize_default_render_capabilities(client_info_t *client) {
217 if (!client) {
218 return;
219 }
220
221 // Keep safe defaults for early control packets, but do not mark capabilities
222 // complete. The render thread must wait for the browser's actual dimensions
223 // and requested frame rate instead of flooding a just-opened DataChannel at
224 // the fallback rate.
225 client->width = 64;
226 client->height = 16;
227 client->terminal_caps.color_level = TERM_COLOR_NONE;
228 client->terminal_caps.color_count = 2;
229 client->terminal_caps.render_mode = RENDER_MODE_FOREGROUND;
230 client->terminal_caps.text_renderer = ASCII_RENDERER_DEFAULT;
231 client->terminal_caps.palette_type = PALETTE_STANDARD;
232 client->terminal_caps.desired_fps = VIDEO_RENDER_FPS;
233 client->terminal_caps.wants_padding = false;
234 client->terminal_caps.utf8_support = false;
235 client->terminal_caps.detection_reliable = false;
238 client->has_terminal_caps = false;
239}
240
241/* ============================================================================
242 * Client Lookup Functions
243 * ============================================================================
244 */
245
269client_info_t *find_client_by_id(const char *client_id) {
270 if (!client_id || client_id[0] == '\0') {
271 SET_ERRNO(ERROR_INVALID_PARAM, "Invalid client ID");
272 return NULL;
273 }
274
275 // Protect uthash lookup with read lock to prevent concurrent access issues
276 rwlock_rdlock(&g_client_manager_rwlock);
277
278 client_info_t *result = NULL;
279 HASH_FIND_STR(g_client_manager.clients_by_id, client_id, result);
280
281 rwlock_rdunlock(&g_client_manager_rwlock);
282
283 if (!result) {
284 log_warn("Client not found for ID %s", client_id);
285 }
286
287 return result;
288}
289
291static int start_client_threads(server_context_t *server_ctx, client_info_t *client) {
292 if (!server_ctx || !client) {
293 SET_ERRNO(ERROR_INVALID_PARAM, "server_ctx or client is NULL");
294 return -1;
295 }
296
297 const char *client_id = client->client_id;
298
299 // Step 0: Send initial server state BEFORE spawning receive thread
300 // This prevents use-after-free if the receive thread immediately errors and calls remove_client()
301 if (send_server_state_to_client(client) != 0) {
302 log_warn("Failed to send initial server state to client %s", client_id);
303 }
304
305 // Get current client count for state packet
306 rwlock_rdlock(&g_client_manager_rwlock);
307 uint32_t connected_count = g_client_manager.client_count;
308 rwlock_rdunlock(&g_client_manager_rwlock);
309
311 state.connected_client_count = connected_count;
312 state.active_client_count = 0; // Will be updated by broadcast thread
313 memset(state.reserved, 0, sizeof(state.reserved));
314
315 // Convert to network byte order
316 server_state_packet_t net_state;
319 memset(net_state.reserved, 0, sizeof(net_state.reserved));
320
321 // Send initial server state via ACIP transport
324 log_warn("Failed to send initial server state to client %s: %s", client_id, asciichat_error_format(NULL, packet_result));
325 } else {
326 log_debug("Sent initial server state to client %s: %u connected clients", client_id, state.connected_client_count);
327 }
328
329 atomic_store_bool(&client->event_ready, true);
330 return create_client_render_threads(server_ctx, client);
331}
332
333static void server_client_initialize_transport_slot(server_context_t *server_ctx, client_info_t *client, acip_transport_t *transport, const char *new_client_id, const char *client_ip, int slot) {
334 // Caller holds the client-manager write lock.
335 // Free any existing buffers from previous client in this slot
336 if (client->incoming_video_buffer) {
338 client->incoming_video_buffer = NULL;
339 }
340 if (client->incoming_audio_buffer) {
342 client->incoming_audio_buffer = NULL;
343 }
344
345 memset(client, 0, sizeof(client_info_t));
346
347 // Set up transport-specific fields
348 client->transport_encrypted = acip_transport_get_type(transport) == ACIP_TRANSPORT_WEBRTC;
349 client->security_ready = client->transport_encrypted;
350 client->transport = transport; // Use provided transport
351 SAFE_STRNCPY(client->client_id, new_client_id, sizeof(client->client_id) - 1);
352 SAFE_STRNCPY(client->client_ip, client_ip, sizeof(client->client_ip) - 1);
353 client->port = 0; // transport doesn't use port numbers
354 atomic_store_bool(&client->active, true);
355 client->server_ctx = server_ctx; // Store server context for receive thread cleanup
356 log_info("Added new transport client %s from %s (transport=%p, type=%d, slot=%d)", new_client_id, client_ip, transport, (int)acip_transport_get_type(transport), slot);
357 atomic_store_bool(&client->shutting_down, false);
358 atomic_store_u64(&client->last_sent_grid_sources, 0); // Send thread updates this
359 log_debug("transport client slot assigned: client_id=%s assigned to slot %d", client->client_id, slot);
360 client->connected_at = time(NULL);
361
362 // Security is borrowed from the network owner after negotiation succeeds.
363 client->crypto_handshake_ctx = NULL;
364 client->crypto_initialized = false;
365
366 // Use noun-based client name for display_name as well
367 SAFE_STRNCPY(client->display_name, new_client_id, sizeof(client->display_name) - 1);
368}
369
370static asciichat_error_t server_client_allocate_transport_buffers(client_info_t *client, const char *new_client_id) {
371 // Create individual video buffer for this client using modern double-buffering
372 client->incoming_video_buffer = video_frame_buffer_create(new_client_id);
373 if (!client->incoming_video_buffer) {
374 log_error("Failed to create video buffer for transport client %s", new_client_id);
375 return SET_ERRNO(ERROR_MEMORY, "Failed to create video buffer for transport client %s", new_client_id);
376 }
377
378 // Create individual audio buffer for this client
380 if (!client->incoming_audio_buffer) {
381 log_error("Failed to create audio buffer for transport client %s", new_client_id);
382 return SET_ERRNO(ERROR_MEMORY, "Failed to create audio buffer for transport client %s", new_client_id);
383 }
384
385 return ASCIICHAT_OK;
386}
387
388static asciichat_error_t server_client_initialize_transport_sync(client_info_t *client, const char *new_client_id) {
389 // Finish synchronization before publishing this client in the registry.
390 if (mutex_init(&client->client_state_mutex, "client_state") != ASCIICHAT_OK)
391 return SET_ERRNO(ERROR_THREAD, "Failed to initialize client state mutex");
392 if (mutex_init(&client->send_mutex, "client_send") != ASCIICHAT_OK) {
393 mutex_destroy(&client->client_state_mutex);
394 return SET_ERRNO(ERROR_THREAD, "Failed to initialize client send mutex");
395 }
396 initialize_default_render_capabilities(client);
397 if (g_audio_mixer && client->incoming_audio_buffer && mixer_add_source(g_audio_mixer, new_client_id, client->incoming_audio_buffer) < 0)
398 log_warn("Failed to add client %s to audio mixer", new_client_id);
399 return ASCIICHAT_OK;
400}
401
402/* Allocate application state for a borrowed connection transport. Media starts
403 * after the shared runtime completes negotiation and receives capabilities. */
404client_info_t *add_transport_client(server_context_t *server_ctx, acip_transport_t *transport, const char *client_ip) {
405 if (!server_ctx || !transport || !client_ip) {
406 SET_ERRNO(ERROR_INVALID_PARAM, "Invalid parameters");
407 return NULL;
408 }
409
410 rwlock_wrlock(&g_client_manager_rwlock);
411
412 // Find empty slot - this is the authoritative check
413 int slot = -1;
414 int existing_count = 0;
415 for (int i = 0; i < MAX_CLIENTS; i++) {
416 if (slot == -1 && g_client_manager.clients[i].client_id[0] == '\0') {
417 slot = i; // Take first available slot
418 }
419 // Count only active clients
420 if (g_client_manager.clients[i].client_id[0] != '\0' && atomic_load_bool(&g_client_manager.clients[i].active)) {
421 existing_count++;
422 }
423 }
424
425 // Check if we've hit the configured max-clients limit (not the array size)
426 if (existing_count >= GET_OPTION(max_clients)) {
427 rwlock_wrunlock(&g_client_manager_rwlock);
428 SET_ERRNO(ERROR_RESOURCE_EXHAUSTED, "Maximum client limit reached (%d/%d active clients)", existing_count, GET_OPTION(max_clients));
429 log_error("Maximum client limit reached (%d/%d active clients)", existing_count, GET_OPTION(max_clients));
430 return NULL;
431 }
432
433 if (slot == -1) {
434 rwlock_wrunlock(&g_client_manager_rwlock);
435 SET_ERRNO(ERROR_RESOURCE_EXHAUSTED, "No available client slots (all %d array slots are in use)", MAX_CLIENTS);
436 log_error("No available client slots (all %d array slots are in use)", MAX_CLIENTS);
437 return NULL;
438 }
439
440 // Update client_count to match actual count before adding new client
441 g_client_manager.client_count = existing_count;
442
443 // Generate unique client ID for transport client (noun only, without counter or port)
444 char new_client_id[MAX_CLIENT_ID_LEN];
445 if (generate_client_id(new_client_id, sizeof(new_client_id)) != 0) {
446 rwlock_wrunlock(&g_client_manager_rwlock);
447 log_error("Failed to generate unique client ID for transport client");
448 return NULL;
449 }
450
451 // Initialize client
452 client_info_t *client = &g_client_manager.clients[slot];
453
454 server_client_initialize_transport_slot(server_ctx, client, transport, new_client_id, client_ip, slot);
455
456 if (server_client_allocate_transport_buffers(client, new_client_id) != ASCIICHAT_OK || server_client_initialize_transport_sync(client, new_client_id) != ASCIICHAT_OK) {
457 goto error_cleanup_transport;
458 }
459
460 g_client_manager.client_count = existing_count + 1; // We just added a client
461 log_debug("Client count updated: now %d clients (added transport client_id=%s to slot %d)", g_client_manager.client_count, new_client_id, slot);
462
463 // Add client to uthash table for O(1) lookup
464 HASH_ADD_STR(g_client_manager.clients_by_id, client_id, client);
465 log_debug("Added transport client %s to uthash table", new_client_id);
466
467 // Release the write lock IMMEDIATELY after adding to hash table
468 // The client is fully initialized before other threads can look it up.
469 rwlock_wrunlock(&g_client_manager_rwlock);
470
471 // Register client with session_host (for discovery mode support)
472 // transport clients use INVALID_SOCKET_VALUE since they don't have a TCP socket
473 if (server_ctx->session_host) {
474 client->session_client_id = session_host_add_client(server_ctx->session_host, NULL, ACIP_TRANSPORT_BORROWED, client_ip, 0);
475 if (client->session_client_id == 0) {
476 log_warn("Failed to register transport client %s with session_host", new_client_id);
477 } else {
478 log_debug("transport client %s registered with session_host as %u", new_client_id, client->session_client_id);
479 }
480 }
481
482 // Broadcast server state to ALL clients AFTER the new client is fully set up
483 // This notifies all clients (including the new one) about the updated grid
485
486 return client;
487
488error_cleanup_transport:
489 // Clean up all partially allocated resources for transport client
490 // NOTE: This label is reached when allocation or initialization fails BEFORE
491 // the client is added to the hash table. Don't call remove_client() here.
492 cleanup_client_all_buffers(client);
493 memset(client, 0, sizeof(*client));
494 rwlock_wrunlock(&g_client_manager_rwlock);
495 return NULL;
496}
497
506
507static asciichat_error_t server_client_begin_removal(server_context_t *server_ctx, const char *client_id, server_client_removal_t *removal) {
508 // Phase 1: Mark client inactive and prepare for cleanup while holding write lock
509
510 log_debug("SOCKET_DEBUG: Attempting to remove client %s", client_id);
511 rwlock_wrlock(&g_client_manager_rwlock);
512
513 for (int i = 0; i < MAX_CLIENTS; i++) {
514 client_info_t *client = &g_client_manager.clients[i];
515 if (strcmp(client->client_id, client_id) == 0 && client->client_id[0] != '\0') {
516 if (client->network_connection && !atomic_load_bool(&client->event_cleanup)) {
518 rwlock_wrunlock(&g_client_manager_rwlock);
519 return ASCIICHAT_OK;
520 }
521 // Check if already being removed by another thread
522 // This prevents double-free and use-after-free crashes during concurrent cleanup
523 if (atomic_load_bool(&client->shutting_down)) {
524 rwlock_wrunlock(&g_client_manager_rwlock);
525 log_debug("Client %s already being removed by another thread, skipping", client_id);
526 return ASCIICHAT_OK; // Return success - removal is in progress
527 }
528 // Mark as shutting down and inactive immediately to stop new operations
529 log_debug("Setting active=false in remove_client (client_id=%s)", client_id);
530 log_info("Removing client %s - marking inactive and clearing video flags", client_id);
531 atomic_store_bool(&client->shutting_down, true);
532 atomic_store_bool(&client->active, false);
533 atomic_store_bool(&client->is_sending_video, false);
534 atomic_store_bool(&client->is_sending_audio, false);
535 removal->client = client;
536
537 // Store display name before clearing
539
540 // Shutdown packet queues to unblock send thread
541 // Video now uses double buffer, no queue to shutdown
542
543 break;
544 }
545 }
546
547 // If client not found, unlock and return
548 if (!removal->client) {
549 rwlock_wrunlock(&g_client_manager_rwlock);
550 log_warn("Cannot remove client %s: not found", client_id);
551 return SET_ERRNO(ERROR_NOT_FOUND, "Cannot remove client %s: not found", client_id);
552 }
553
554 // Unregister client from session_host (for discovery mode support)
555 // NOTE: Client may not be registered if crypto handshake failed before session_host registration
556 if (server_ctx->session_host && removal->client->session_client_id != 0) {
558 if (session_result != ASCIICHAT_OK) {
559 // ERROR_NOT_FOUND (91) is expected if client failed crypto before being registered with session_host
560 if (session_result == ERROR_NOT_FOUND) {
561 log_debug("Client %s not found in session_host (likely failed crypto before registration)", client_id);
562 } else {
563 log_warn("Failed to unregister client %s from session_host: %s", client_id, asciichat_error_format(NULL, session_result));
564 }
565 } else {
566 log_debug("Client %s unregistered from session_host", client_id);
567 }
568 }
569
570 // Release write lock before joining threads.
571 // This prevents deadlock with render threads that need read locks.
572 rwlock_wrunlock(&g_client_manager_rwlock);
573
574 return ASCIICHAT_OK;
575}
576
577static void server_client_release_resources_locked(const char *client_id, server_client_removal_t *removal) {
578 // Other clients' render threads can still read this client's audio buffer.
579 // Removing the source waits for the mixer lock before releasing that buffer.
580 if (g_audio_mixer) {
582#ifdef DEBUG_AUDIO
583 log_debug("Removed client %s from audio mixer", client_id);
584#endif
585 }
586
587 cleanup_client_all_buffers(removal->client);
588
589 // Remove from uthash table
590 // Verify client is actually in the hash table before deleting.
591 // Another thread might have already removed it.
592 if (removal->client) {
593 client_info_t *hash_entry = NULL;
594 HASH_FIND_STR(g_client_manager.clients_by_id, removal->client->client_id, hash_entry);
595 if (hash_entry == removal->client) {
596 HASH_DELETE(hh, g_client_manager.clients_by_id, removal->client);
597 log_debug("Removed client %s from uthash table", client_id);
598 } else {
599 log_warn("Client %s already removed from hash table by another thread (found=%p, expected=%p)", client_id, (void *)hash_entry, (void *)removal->client);
600 }
601 } else {
602 log_warn("Failed to remove client %s from hash table (client not found)", client_id);
603 }
604
605 removal->client->crypto_handshake_ctx = NULL;
606 removal->client->crypto_initialized = false;
607}
608
609static void server_client_reset_slot_locked(const char *client_id, server_client_removal_t *removal) {
610 // Verify all threads have actually exited before resetting client_id.
611 // Threads that are still starting (at RtlUserThreadStart) haven't checked client_id yet.
612 // We must ensure threads are fully joined before zeroing the client struct.
613 // Use exponential backoff for thread termination verification
614 int retry_count = 0;
615 const int max_retries = 5;
616 while (retry_count < max_retries && (thread_is_initialized(&removal->client->video_render_thread) || thread_is_initialized(&removal->client->audio_render_thread))) {
617 // Exponential backoff: 10ms, 20ms, 40ms, 80ms, 160ms
618 uint32_t delay_ms = 10 * (1 << retry_count);
619 log_warn("Client %s: Some threads still appear initialized (attempt %d/%d), waiting %ums", client_id, retry_count + 1, max_retries, delay_ms);
620 platform_sleep_us(delay_ms * 1000);
621 retry_count++;
622 }
623
624 if (retry_count == max_retries) {
625 log_error("Client %s: Threads did not terminate after %d retries, proceeding with cleanup anyway", client_id, max_retries);
626 }
627
628 // Only reset client_id to 0 AFTER confirming threads are joined
629 // This prevents threads that are starting from accessing a zeroed client struct
630 // Reset client_id to NULL before destroying mutexes to prevent race conditions.
631 // This ensures worker threads can detect shutdown and exit before the mutex is destroyed.
632 // If we destroy the mutex first, threads might try to access a destroyed mutex.
633 removal->client->client_id[0] = '\0'; // Clear the client_id string
634
635 // Wait for threads to observe the client_id reset
636 // Use sufficient delay for memory visibility across all CPU cores
637 platform_sleep_us(5 * US_PER_MS_INT); // 5ms delay for memory barrier propagation
638
639 // Destroy mutexes and condition variables
640 // Always destroy these even if threads did not join properly
641 // to prevent issues when the slot is reused
642 mutex_destroy(&removal->client->client_state_mutex);
643 mutex_destroy(&removal->client->send_mutex);
644
645 // Clear client structure
646 // NOTE: After memset, the mutex handles are zeroed but the OS resources
647 // have been released by the destroy calls above
648 memset(removal->client, 0, sizeof(client_info_t));
649}
650
651static bool server_client_finish_removal(const char *client_id, server_client_removal_t *removal) {
652 // Phase 3: Clean up resources with write lock
653 rwlock_wrlock(&g_client_manager_rwlock);
654
655 // Re-validate target_client pointer after reacquiring lock.
656 // Another thread might have invalidated the pointer while we had the lock released.
657 if (removal->client) {
658 // Verify client_id still matches and client is still in shutting_down state
659 bool still_shutting_down = atomic_load_bool(&removal->client->shutting_down);
660 if (strcmp(removal->client->client_id, client_id) != 0 || !still_shutting_down) {
661 log_warn("Client %s pointer invalidated during thread cleanup (id=%s, shutting_down=%d)", client_id, removal->client->client_id, still_shutting_down);
662 rwlock_wrunlock(&g_client_manager_rwlock);
663 return false; // Another thread completed the cleanup
664 }
665 }
666
667 server_client_release_resources_locked(client_id, removal);
668
669 server_client_reset_slot_locked(client_id, removal);
670
671 // Recalculate client count
672 int remaining_count = 0;
673 for (int j = 0; j < MAX_CLIENTS; j++) {
674 if (g_client_manager.clients[j].client_id[0] != '\0') {
675 remaining_count++;
676 }
677 }
678 g_client_manager.client_count = remaining_count;
679
680 log_debug("Client removed: client_id=%s (%s) removed, remaining clients: %d", client_id, removal->display_name, remaining_count);
681
682 rwlock_wrunlock(&g_client_manager_rwlock);
683
684 return true;
685}
686
687int remove_client(server_context_t *server_ctx, const char *client_id) {
688 if (!server_ctx) {
689 SET_ERRNO(ERROR_INVALID_PARAM, "Cannot remove client %s: NULL server_ctx", client_id);
690 return -1;
691 }
692
693 server_client_removal_t removal = {0};
694 if (server_client_begin_removal(server_ctx, client_id, &removal) != ASCIICHAT_OK) {
695 return -1;
696 }
697 if (!removal.client) {
698 return 0; // Another thread is already removing this client.
699 }
700
702 return -1;
703 if (!server_client_finish_removal(client_id, &removal)) {
704 return 0;
705 }
706
708 return 0;
709}
710
711/* ============================================================================
712 * Client Thread Functions
713 * ============================================================================
714 */
715
716// Forward declaration for ACIP server callbacks (defined later in file)
717static const acip_server_callbacks_t g_acip_server_callbacks;
718
719asciichat_error_t server_dispatch_packet(client_info_t *client, packet_type_t type, const void *data, size_t length) {
721 return handle_runtime_settings(client, data, length);
722 return acip_handle_server_packet(client->transport, type, data, length, client, &g_acip_server_callbacks);
723}
724
732static void broadcast_state(network_connection_t *connection, void *arg) {
733 if (network_connection_state(connection) != NETWORK_READY)
734 return;
736 client_info_t *client = state ? atomic_ptr_load(&state->client) : NULL;
737 if (!client || !atomic_load_bool(&client->event_ready) || !atomic_load_bool(&client->active))
738 return;
739 mutex_lock(&client->send_mutex);
741 mutex_unlock(&client->send_mutex);
742}
743static server_state_packet_t server_state_snapshot(void) {
744 server_state_packet_t state = {0};
745 rwlock_rdlock(&g_client_manager_rwlock);
746 uint32_t active = 0;
747 for (int i = 0; i < MAX_CLIENTS; ++i)
748 if (atomic_load_bool(&g_client_manager.clients[i].active) && atomic_load_bool(&g_client_manager.clients[i].is_sending_video))
749 ++active;
751 state.active_client_count = HOST_TO_NET_U32(active);
752 rwlock_rdunlock(&g_client_manager_rwlock);
753 return state;
754}
756 server_state_packet_t state = server_state_snapshot();
757 return acip_send_server_state(transport, &state);
758}
761 return;
762 server_state_packet_t state = server_state_snapshot();
764}
765
766int start_transport_client_threads(server_context_t *server_ctx, const char *client_id) {
767 if (!server_ctx) {
768 SET_ERRNO(ERROR_INVALID_PARAM, "Server context is NULL");
769 return -1;
770 }
771
772 client_info_t *client = find_client_by_id(client_id);
773 if (!client) {
774 SET_ERRNO(ERROR_NOT_FOUND, "Client %s not found", client_id);
775 return -1;
776 }
777
778 log_debug("Starting application workers for client %s...", client_id);
779 return start_client_threads(server_ctx, client);
780}
781
783 if (!client) {
784 SET_ERRNO(ERROR_INVALID_PARAM, "Client is NULL");
785 return;
786 }
787
788 if (client->digital_rain) {
790 client->digital_rain = NULL;
791 }
792
793 if (client->incoming_video_buffer) {
795 client->incoming_video_buffer = NULL;
796 }
797
798 if (client->incoming_audio_buffer) {
800 client->incoming_audio_buffer = NULL;
801 }
802
803 // Clean up Opus decoder
804 if (client->opus_decoder) {
806 client->opus_decoder = NULL;
807 }
808
809 // Clean up video decoder (persistent per-client decoder)
810 if (client->video_decoder_ctx) {
811 avcodec_free_context((AVCodecContext **)&client->video_decoder_ctx);
812 client->video_decoder_ctx = NULL;
813 }
814 if (client->video_decode_frame) {
815 av_frame_free((AVFrame **)&client->video_decode_frame);
816 client->video_decode_frame = NULL;
817 }
818 if (client->video_rgb_frame) {
819 av_frame_free((AVFrame **)&client->video_rgb_frame);
820 client->video_rgb_frame = NULL;
821 }
822 if (client->video_sws_ctx) {
823 sws_freeContext(client->video_sws_ctx);
824 client->video_sws_ctx = NULL;
825 }
826}
827
829 if (!client)
830 return;
831
832 // Async dispatch: clean up received packet queue
833
834 // Video now uses double buffer, cleaned up in cleanup_client_media_buffers
835}
836
846static inline void cleanup_client_all_buffers(client_info_t *client) {
850}
851
862static void acip_server_on_protocol_version(const protocol_version_packet_t *version, void *client_ctx, void *app_ctx);
863static void acip_server_on_image_frame(const image_frame_packet_t *header, const void *pixel_data, size_t data_len, void *client_ctx, void *app_ctx);
864static void acip_server_on_image_frame_h264(uint32_t width, uint32_t height, uint8_t flags, const void *data, size_t data_len, void *client_ctx, void *app_ctx);
865static void acip_server_on_image_frame_h265(uint32_t width, uint32_t height, uint8_t flags, const void *h265_data, size_t data_len, void *client_ctx, void *app_ctx);
866static void acip_server_on_audio(const void *audio_data, size_t audio_len, void *client_ctx, void *app_ctx);
867static void acip_server_on_audio_batch(const audio_batch_packet_t *header, const float *samples, size_t num_samples, void *client_ctx, void *app_ctx);
868static void acip_server_on_audio_opus(const void *opus_data, size_t opus_len, void *client_ctx, void *app_ctx);
869static void acip_server_on_audio_opus_batch(const void *batch_data, size_t batch_len, void *client_ctx, void *app_ctx);
870static void acip_server_on_client_join(const void *join_data, size_t data_len, void *client_ctx, void *app_ctx);
871static void acip_server_on_client_leave(void *client_ctx, void *app_ctx);
872static void acip_server_on_stream_start(uint32_t stream_types, void *client_ctx, void *app_ctx);
873static void acip_server_on_stream_stop(uint32_t stream_types, void *client_ctx, void *app_ctx);
874static void acip_server_on_capabilities(const void *cap_data, size_t data_len, void *client_ctx, void *app_ctx);
875static void acip_server_on_ping(void *client_ctx, void *app_ctx);
876static void acip_server_on_pong(void *client_ctx, void *app_ctx);
877static void acip_server_on_error(const error_packet_t *header, const char *message, void *client_ctx, void *app_ctx);
878static void acip_server_on_remote_log(const remote_log_packet_t *header, const char *message, void *client_ctx, void *app_ctx);
879
886static const acip_server_callbacks_t g_acip_server_callbacks = {
887 .on_protocol_version = acip_server_on_protocol_version,
888 .on_image_frame = acip_server_on_image_frame,
889 .on_image_frame_h265 = acip_server_on_image_frame_h265,
890 .on_image_frame_h264 = acip_server_on_image_frame_h264,
891 .on_audio = acip_server_on_audio,
892 .on_audio_batch = acip_server_on_audio_batch,
893 .on_audio_opus = acip_server_on_audio_opus,
894 .on_audio_opus_batch = acip_server_on_audio_opus_batch,
895 .on_client_join = acip_server_on_client_join,
896 .on_client_leave = acip_server_on_client_leave,
897 .on_stream_start = acip_server_on_stream_start,
898 .on_stream_stop = acip_server_on_stream_stop,
899 .on_capabilities = acip_server_on_capabilities,
900 .on_ping = acip_server_on_ping,
901 .on_pong = acip_server_on_pong,
902 .on_error = acip_server_on_error,
903 .on_remote_log = acip_server_on_remote_log,
904
905 .app_ctx = NULL // Not used - client context passed per-call
906};
907
908// Callback implementations (delegate to existing handlers)
909
910static void acip_server_on_protocol_version(const protocol_version_packet_t *version, void *client_ctx, void *app_ctx) {
911 // TODO: Use app_ctx for context-aware protocol handling or metrics collection in future versions
912 (void)app_ctx;
913 client_info_t *client = (client_info_t *)client_ctx;
914 handle_protocol_version_packet(client, (void *)version, sizeof(*version));
915}
916
917static void acip_server_on_image_frame(const image_frame_packet_t *header, const void *pixel_data, size_t data_len, void *client_ctx, void *app_ctx) {
918 (void)app_ctx;
919 client_info_t *client = (client_info_t *)client_ctx;
920
921 // Reject empty or missing RGB payloads before diagnostics sample the first
922 // pixel. Malformed frames must not turn a logging path into a server crash.
923 if (!header || !client || !pixel_data || data_len < 3) {
924 log_warn("Ignoring empty or invalid WebRTC image frame payload (data_len=%zu)", data_len);
925 return;
926 }
927
928 log_dev("CALLBACK_IMAGE_FRAME: client_id=%s width=%u height=%u pixel_format=%u compressed_size=%u data_len=%zu", client->client_id, header->width, header->height, header->pixel_format,
929 header->compressed_size, data_len);
930
931 size_t center_pixel_offset = ((size_t)(header->height / 2) * header->width + header->width / 2) * 3;
932 const uint8_t *first_pixel = pixel_data;
933 const uint8_t *center_pixel = center_pixel_offset + 3 <= data_len ? (const uint8_t *)pixel_data + center_pixel_offset : first_pixel;
934 log_debug_every(US_PER_SEC_INT, "WebRTC pixel sample client=%s size=%ux%u bytes=%zu first=%02x%02x%02x center=%02x%02x%02x", client->client_id, header->width, header->height, data_len,
935 first_pixel[0], first_pixel[1], first_pixel[2], center_pixel[0], center_pixel[1], center_pixel[2]);
936
937 // Validate frame dimensions to prevent DoS and buffer overflow attacks
938 if (header->width == 0 || header->height == 0) {
939 log_error("Invalid image dimensions: %ux%u (width and height must be > 0)", header->width, header->height);
940 disconnect_client_for_bad_data(client, "IMAGE_FRAME invalid dimensions");
941 return;
942 }
943
944 const uint32_t MAX_WIDTH = 8192;
945 const uint32_t MAX_HEIGHT = 8192;
946 if (header->width > MAX_WIDTH || header->height > MAX_HEIGHT) {
947 log_error("Image dimensions too large: %ux%u (max: %ux%u)", header->width, header->height, MAX_WIDTH, MAX_HEIGHT);
948 disconnect_client_for_bad_data(client, "IMAGE_FRAME dimensions too large");
949 return;
950 }
951
952 // Auto-enable video stream if not already enabled
953 // Use atomic_exchange to atomically set flag and check old value
954 bool was_sending_video = atomic_exchange_bool(&client->is_sending_video, true);
955 if (!was_sending_video) {
956 log_info("Client %s auto-enabled video stream (received IMAGE_FRAME)", client->client_id);
957 log_info_client(client, "First video frame received - streaming active");
958 }
959
960 // Measure frame arrival at the server independently of composition and
961 // delivery so low FPS can be attributed to the publishing client or server.
962 uint64_t receive_now_ns = time_get_ns();
963 double receive_fps = 0.0;
964 uint32_t received_in_window = 0;
965 uint64_t receive_window_ns = 0;
966 mutex_lock(&client->client_state_mutex);
967 client->frames_received_logged++;
968 if (client->video_receive_rate_window_ns == 0) {
969 client->video_receive_rate_window_ns = receive_now_ns;
970 }
972 receive_window_ns = receive_now_ns - client->video_receive_rate_window_ns;
973 if (receive_window_ns >= NS_PER_SEC_INT) {
974 received_in_window = client->video_receive_rate_window_frames;
975 receive_fps = (double)received_in_window * NS_PER_SEC_INT / (double)receive_window_ns;
976 client->video_receive_rate_window_ns = receive_now_ns;
978 }
979 bool log_frame_count = client->frames_received_logged % 25000 == 0;
980 uint32_t total_received = client->frames_received_logged;
981 mutex_unlock(&client->client_state_mutex);
982 if (log_frame_count) {
984 format_bytes_pretty(data_len, pretty, sizeof(pretty));
985 log_debug("Client %s has sent %u IMAGE_FRAME packets (%s)", client->client_id, total_received, pretty);
986 }
987 if (received_in_window > 0) {
988 log_info("Server image ingress client=%s fps=%.1f frames=%u elapsed=%.2fs", client->client_id, receive_fps, received_in_window, (double)receive_window_ns / (double)NS_PER_SEC_INT);
989 }
990
991 // Sample a small prefix for duplicate-frame diagnostics; hashing every pixel
992 // here repeats work performed by the image conversion path on every frame.
993 size_t hash_len = data_len < 1000 ? data_len : 1000;
994 uint32_t incoming_pixel_hash = hash_len ? fnv1a_hash_bytes(pixel_data, hash_len) : FNV1A_32_OFFSET_BASIS;
995
996 // Per-client hash tracking to detect duplicate frames
997 bool is_new_frame = (incoming_pixel_hash != client->last_received_frame_hash);
998
999 // Inspect first few pixels of incoming frame
1000 uint32_t first_pixel_rgb = 0;
1001 if (data_len >= 3) {
1002 first_pixel_rgb = ((uint32_t)((unsigned char *)pixel_data)[0] << 16) | ((uint32_t)((unsigned char *)pixel_data)[1] << 8) | (uint32_t)((unsigned char *)pixel_data)[2];
1003 }
1004 if (is_new_frame) {
1005 log_dev("RECV_FRAME #%llu NEW: client=%s dimensions=%ux%u pixel_size=%zu hash=0x%08x first_rgb=0x%06x prev=0x%08x", (unsigned long long)client->frames_received, client->client_id, header->width,
1006 header->height, data_len, incoming_pixel_hash, first_pixel_rgb, client->last_received_frame_hash);
1007 client->last_received_frame_hash = incoming_pixel_hash;
1008 } else {
1009 log_dev("RECV_FRAME #%llu DUP: client=%s dimensions=%ux%u pixel_size=%zu hash=0x%08x first_rgb=0x%06x", (unsigned long long)client->frames_received, client->client_id, header->width,
1010 header->height, data_len, incoming_pixel_hash, first_pixel_rgb);
1011 }
1012 if (client->frames_received_logged > 0 && client->frames_received_logged % 60 == 0) {
1013 log_info("Incoming video signature client=%s frames=%u state=%s hash=0x%08x size=%ux%u bytes=%zu", client->client_id, client->frames_received_logged, is_new_frame ? "changing" : "duplicate",
1014 incoming_pixel_hash, header->width, header->height, data_len);
1015 }
1016
1017 // Store frame data to incoming_video_buffer
1018 // pixel_data points to websocket reassembly buffer which will be freed after callback returns,
1019 // so we must copy it immediately into the persistent frame buffer (which is pre-allocated to 2MB)
1020 if (client->incoming_video_buffer) {
1022 log_dev("VFB: client=%s allocated_size=%zu active=%d", client->client_id, vfb->allocated_buffer_size, vfb->active);
1023
1025 if (frame && frame->data && data_len > 0) {
1026 // Debug: Check frame buffer capacity
1028 size_t total_size = sizeof(uint32_t) * 2 + data_len;
1029
1030 log_dev("FRAME_STORAGE: client=%s capacity=%zu data_len=%zu total=%zu", client->client_id, buffer_capacity, data_len, total_size);
1031
1032 // CRITICAL: Bounds check BEFORE memcpy to prevent heap-buffer-overflow
1033 // If buffer is too small, skip frame storage instead of overflowing
1034 // NOTE: Use >= not > because we need one free byte after the last byte
1035 if (total_size >= buffer_capacity) {
1036 log_error("FRAME_STORAGE_OVERFLOW_PREVENTED: client=%s, need=%zu bytes, capacity=%zu bytes (cannot store "
1037 "frame, would overflow at byte %zu)",
1038 client->client_id, total_size, buffer_capacity, buffer_capacity);
1039 disconnect_client_for_bad_data(client, "FRAME_STORAGE_BUFFER_TOO_SMALL");
1040 return;
1041 }
1042
1043 // Copy frame data: width (4B) + height (4B) + pixel data
1044 uint32_t width_net = HOST_TO_NET_U32(header->width);
1045 uint32_t height_net = HOST_TO_NET_U32(header->height);
1046
1047 memcpy(frame->data, &width_net, sizeof(uint32_t));
1048 memcpy((char *)frame->data + sizeof(uint32_t), &height_net, sizeof(uint32_t));
1049 memcpy((char *)frame->data + sizeof(uint32_t) * 2, pixel_data, data_len);
1050
1051 frame->size = total_size;
1052 frame->width = header->width;
1053 frame->height = header->height;
1054 frame->capture_timestamp_ns = time_get_ns();
1055 frame->sequence_number = ++client->frames_received;
1056
1058 log_debug_every(NS_PER_SEC_INT * 5, "Frame committed: client %s, seq=%llu, size=%zu", client->client_id, (unsigned long long)frame->sequence_number, total_size);
1059 } else {
1060 log_warn_every(NS_PER_SEC_INT * 5, "FRAME_STORE_SKIP: client=%s, frame=%p, frame->data=%p, data_len=%zu", client->client_id, (void *)frame, frame ? (void *)frame->data : NULL, data_len);
1061 }
1062 } else {
1063 log_warn_every(NS_PER_SEC_INT * 3, "NO_INCOMING_VIDEO_BUFFER: client=%s", client->client_id);
1064 }
1065}
1066
1068static asciichat_error_t video_decoder_ensure_initialized(client_info_t *client, enum AVCodecID codec_id) {
1069 AVCodecContext *ctx = client->video_decoder_ctx;
1070 if (ctx && ctx->codec_id == codec_id && client->video_decode_frame && client->video_rgb_frame)
1071 return ASCIICHAT_OK;
1072 if (ctx)
1073 avcodec_free_context((AVCodecContext **)&client->video_decoder_ctx);
1074 const AVCodec *codec = avcodec_find_decoder(codec_id);
1075 if (!codec)
1076 return SET_ERRNO(ERROR_NOT_SUPPORTED, "Video decoder %s unavailable", avcodec_get_name(codec_id));
1077 ctx = avcodec_alloc_context3(codec);
1078 if (!ctx)
1079 return SET_ERRNO(ERROR_MEMORY, "Failed to allocate video decoder");
1080 ctx->max_pixels = 8192 * 8192;
1081 if (avcodec_open2(ctx, codec, NULL) < 0) {
1082 avcodec_free_context(&ctx);
1083 return SET_ERRNO(ERROR_INVALID_STATE, "Failed to open video decoder");
1084 }
1085 client->video_decoder_ctx = ctx;
1086 if (!client->video_decode_frame)
1087 client->video_decode_frame = av_frame_alloc();
1088 if (!client->video_rgb_frame)
1089 client->video_rgb_frame = av_frame_alloc();
1090 if (!client->video_decode_frame || !client->video_rgb_frame)
1091 return SET_ERRNO(ERROR_MEMORY, "Failed to allocate decoded video frames");
1092 return ASCIICHAT_OK;
1093}
1094
1095static int server_receive_decoded_frame(AVCodecContext *ctx, AVFrame *frame) {
1096 ASCII_OTEL_SCOPE(FFMPEG_FRAME_RECEIVE);
1097 int result = avcodec_receive_frame(ctx, frame);
1098 if (result < 0 && result != AVERROR(EAGAIN) && result != AVERROR_EOF)
1099 ascii_otel_error("FFmpeg server frame receive failed");
1100 return result;
1101}
1102
1103static void acip_server_on_encoded_frame(uint32_t width, uint32_t height, uint8_t flags, const void *data, size_t data_len, void *client_ctx, enum AVCodecID codec_id) {
1104 ASCII_OTEL_SCOPE(FFMPEG_VIDEO_DECODE);
1105 ascii_otel_scope_int(&otel_scope, "media.codec.id", codec_id);
1106 ascii_otel_scope_int(&otel_scope, "video.width", width);
1107 ascii_otel_scope_int(&otel_scope, "video.height", height);
1108 client_info_t *client = client_ctx;
1109 video_codec_t capability = codec_id == AV_CODEC_ID_H264 ? VIDEO_CODEC_H264 : VIDEO_CODEC_H265;
1110 if (!VIDEO_CODEC_SUPPORTED(client->codec_capabilities_video, capability) || width == 0 || height == 0 || width > 8192 || height > 8192 || !data || data_len == 0 || data_len > INT_MAX) {
1111 disconnect_client_for_bad_data(client, "Invalid encoded video frame or unadvertised codec");
1112 return;
1113 }
1114 if (video_decoder_ensure_initialized(client, codec_id) != ASCIICHAT_OK) {
1115 disconnect_client_for_bad_data(client, "Failed to initialize video decoder");
1116 return;
1117 }
1118 AVCodecContext *ctx = client->video_decoder_ctx;
1119 AVFrame *frame = client->video_decode_frame;
1120 AVFrame *rgb = client->video_rgb_frame;
1121 if (flags & 2)
1122 avcodec_flush_buffers(ctx);
1123 AVPacket *packet = av_packet_alloc();
1124 if (!packet || av_new_packet(packet, (int)data_len) < 0) {
1125 av_packet_free(&packet);
1126 return;
1127 }
1128 // FFmpeg allocates the required zero padding after compressed input.
1129 memcpy(packet->data, data, data_len);
1130 uint64_t decode_start = time_get_ns();
1131 int result;
1132 {
1133 ASCII_OTEL_SCOPE(FFMPEG_PACKET_SEND);
1134 result = avcodec_send_packet(ctx, packet);
1135 if (result < 0 && result != AVERROR(EAGAIN) && result != AVERROR_EOF)
1136 ascii_otel_error("FFmpeg server packet send failed");
1137 }
1138 av_packet_free(&packet);
1139 if (result < 0) {
1140 log_warn("%s input decode failed: %d", avcodec_get_name(codec_id), result);
1141 return;
1142 }
1143 while ((result = server_receive_decoded_frame(ctx, frame)) == 0) {
1144 stats_duration_record(stats_runtime_scope(), STATS_DURATION_DECODE, time_get_ns() - decode_start);
1145 if (frame->width != (int)width || frame->height != (int)height) {
1146 disconnect_client_for_bad_data(client, "Decoded video dimensions do not match packet header");
1147 return;
1148 }
1149 struct SwsContext *sws =
1150 sws_getCachedContext(client->video_sws_ctx, frame->width, frame->height, frame->format, frame->width, frame->height, AV_PIX_FMT_RGB24, SWS_FAST_BILINEAR, NULL, NULL, NULL);
1151 client->video_sws_ctx = sws;
1152 if (!sws)
1153 return;
1154 // swscale SIMD output needs aligned rows, including padding after the last row.
1155 int rgb_size = av_image_get_buffer_size(AV_PIX_FMT_RGB24, frame->width, frame->height, 32);
1156 size_t packed_size = (size_t)frame->width * frame->height * 3;
1157 if (rgb_size <= 0 || !client->incoming_video_buffer || packed_size + 8 > client->incoming_video_buffer->allocated_buffer_size)
1158 return;
1159 uint8_t *pixels = SAFE_MALLOC((size_t)rgb_size + AV_INPUT_BUFFER_PADDING_SIZE, uint8_t *);
1160 if (!pixels)
1161 return;
1162 av_image_fill_arrays(rgb->data, rgb->linesize, pixels, AV_PIX_FMT_RGB24, frame->width, frame->height, 32);
1163 int rows;
1164 {
1165 ASCII_OTEL_SCOPE(FFMPEG_SCALE);
1166 rows = sws_scale(sws, (const uint8_t *const *)frame->data, frame->linesize, 0, frame->height, rgb->data, rgb->linesize);
1167 if (rows != frame->height)
1168 ascii_otel_error("FFmpeg server frame scale failed");
1169 }
1170 if (rows == frame->height) {
1171 atomic_store_bool(&client->is_sending_video, true);
1173 if (vf && vf->data) {
1175 memcpy(vf->data, dimensions, sizeof(dimensions));
1176 for (int y = 0; y < frame->height; y++) {
1177 memcpy((uint8_t *)vf->data + sizeof(dimensions) + (size_t)y * width * 3, rgb->data[0] + (size_t)y * rgb->linesize[0], (size_t)width * 3);
1178 }
1179 vf->size = sizeof(dimensions) + packed_size;
1180 vf->width = width;
1181 vf->height = height;
1182 vf->is_keyframe = (flags & 1) != 0;
1183 vf->capture_timestamp_ns = time_get_ns();
1184 vf->sequence_number = ++client->frames_received;
1187 }
1188 }
1189 SAFE_FREE(pixels);
1190 }
1191 if (result != AVERROR(EAGAIN) && result != AVERROR_EOF)
1192 log_warn("%s output decode failed: %d", avcodec_get_name(codec_id), result);
1193}
1194
1195static void acip_server_on_image_frame_h265(uint32_t width, uint32_t height, uint8_t flags, const void *data, size_t data_len, void *client_ctx, void *app_ctx) {
1196 (void)app_ctx;
1197 acip_server_on_encoded_frame(width, height, flags, data, data_len, client_ctx, AV_CODEC_ID_HEVC);
1198}
1199
1200static void acip_server_on_image_frame_h264(uint32_t width, uint32_t height, uint8_t flags, const void *data, size_t data_len, void *client_ctx, void *app_ctx) {
1201 (void)app_ctx;
1202 acip_server_on_encoded_frame(width, height, flags, data, data_len, client_ctx, AV_CODEC_ID_H264);
1203}
1204
1205void handle_image_frame_h264_packet(client_info_t *client, const void *data, size_t len) {
1206 asciichat_error_t result = acip_handle_server_packet(client->transport, PACKET_TYPE_IMAGE_FRAME_H264, data, len, client, &g_acip_server_callbacks);
1207 if (result != ASCIICHAT_OK)
1208 disconnect_client_for_bad_data(client, "Invalid H.264 packet");
1209}
1210
1211static void acip_server_on_audio(const void *audio_data, size_t audio_len, void *client_ctx, void *app_ctx) {
1212 (void)app_ctx;
1213 client_info_t *client = (client_info_t *)client_ctx;
1214 handle_audio_packet(client, (void *)audio_data, audio_len);
1215}
1216
1217static void acip_server_on_audio_batch(const audio_batch_packet_t *header, const float *samples, size_t num_samples, void *client_ctx, void *app_ctx) {
1218 (void)app_ctx;
1219 if (header->sample_rate != AUDIO_SAMPLE_RATE || (header->channels != 1 && header->channels != 2) || num_samples % header->channels)
1220 return;
1221 client_info_t *client = (client_info_t *)client_ctx;
1222
1223 // ACIP handler already dequantized samples - write directly to audio buffer
1224 // This is more efficient than calling the existing handler which would re-dequantize
1225 log_debug_every(LOG_RATE_DEFAULT, "Received audio batch from client %s (samples=%zu, is_sending_audio=%d)", client->client_id, num_samples, atomic_load_bool(&client->is_sending_audio));
1226
1227 if (!atomic_load_bool(&client->is_sending_audio)) {
1228 log_debug("Ignoring audio batch - client %s not in audio streaming mode", client->client_id);
1229 return;
1230 }
1231
1232 if (client->incoming_audio_buffer) {
1233 asciichat_error_t write_result = ASCIICHAT_OK;
1234 size_t frames = num_samples / header->channels;
1235 float *stereo = SAFE_MALLOC(frames * AUDIO_CHANNELS * sizeof(float), float *);
1236 if (audio_convert_channels(samples, frames, header->channels, stereo, AUDIO_CHANNELS) == ASCIICHAT_OK)
1237 write_result = audio_ring_buffer_write(client->incoming_audio_buffer, stereo, (int)(frames * AUDIO_CHANNELS));
1238 SAFE_FREE(stereo);
1239 if (write_result != ASCIICHAT_OK) {
1240 log_error("Failed to write decoded audio batch to buffer: %s", asciichat_error_format(NULL, write_result));
1241 }
1242 }
1243}
1244
1245static void acip_server_on_audio_opus(const void *opus_data, size_t opus_len, void *client_ctx, void *app_ctx) {
1246 (void)app_ctx;
1247 client_info_t *client = (client_info_t *)client_ctx;
1248
1249 // Special handling: Convert single-frame Opus to batch format
1250 // This maintains compatibility with existing server-side Opus batch processing
1251
1252 if (opus_len < 16) {
1253 log_warn("AUDIO_OPUS packet too small: %zu bytes", opus_len);
1254 return;
1255 }
1256
1257 const uint8_t *payload = (const uint8_t *)opus_data;
1258 // Use unaligned read helpers - network data may not be aligned
1259 int sample_rate = (int)NET_TO_HOST_U32(read_u32_unaligned(payload));
1260 int frame_duration = (int)NET_TO_HOST_U32(read_u32_unaligned(payload + 4));
1261 // Reserved bytes at offset 8-15
1262 size_t actual_opus_size = opus_len - 16;
1263
1264 if (actual_opus_size > 0 && actual_opus_size <= AUDIO_OPUS_SERVER_PACKET_CAPACITY && sample_rate == 48000 && frame_duration == 20) {
1265 // Create a synthetic Opus batch packet (frame_count=1)
1266 uint8_t batch_buffer[AUDIO_OPUS_SERVER_PACKET_CAPACITY + SERVER_OPUS_BATCH_OVERHEAD]; // Max Opus + header
1267 uint8_t *batch_ptr = batch_buffer;
1268
1269 // Write batch header (batch_buffer is stack-aligned, writes are safe)
1270 write_u32_unaligned(batch_ptr, HOST_TO_NET_U32((uint32_t)sample_rate));
1271 batch_ptr += 4;
1272 write_u32_unaligned(batch_ptr, HOST_TO_NET_U32((uint32_t)frame_duration));
1273 batch_ptr += 4;
1274 write_u32_unaligned(batch_ptr, HOST_TO_NET_U32(1)); // frame_count = 1
1275 batch_ptr += 4;
1276 memset(batch_ptr, 0, 4); // reserved
1277 batch_ptr += 4;
1278
1279 // Write frame size
1280 write_u16_unaligned(batch_ptr, HOST_TO_NET_U16((uint16_t)actual_opus_size));
1281 batch_ptr += 2;
1282
1283 // Write Opus data
1284 memcpy(batch_ptr, payload + 16, actual_opus_size);
1285 batch_ptr += actual_opus_size;
1286
1287 // Process as batch packet
1288 size_t batch_size = (size_t)(batch_ptr - batch_buffer);
1289 handle_audio_opus_batch_packet(client, batch_buffer, batch_size);
1290 }
1291}
1292
1293static void acip_server_on_audio_opus_batch(const void *batch_data, size_t batch_len, void *client_ctx, void *app_ctx) {
1294 (void)app_ctx;
1295 client_info_t *client = (client_info_t *)client_ctx;
1296 handle_audio_opus_batch_packet(client, (void *)batch_data, batch_len);
1297}
1298
1299static void acip_server_on_client_join(const void *join_data, size_t data_len, void *client_ctx, void *app_ctx) {
1300 (void)app_ctx;
1301 client_info_t *client = (client_info_t *)client_ctx;
1302 handle_client_join_packet(client, (void *)join_data, data_len);
1303}
1304
1305static void acip_server_on_client_leave(void *client_ctx, void *app_ctx) {
1306 (void)app_ctx;
1307 client_info_t *client = (client_info_t *)client_ctx;
1308 handle_client_leave_packet(client, NULL, 0);
1309}
1310
1311static void acip_server_on_stream_start(uint32_t stream_types, void *client_ctx, void *app_ctx) {
1312 (void)app_ctx;
1313 client_info_t *client = (client_info_t *)client_ctx;
1314 // The ACIP parser has already converted these flags to host byte order.
1315 // The shared packet handler consumes the network-order payload used by the
1316 // TCP packet dispatcher, so restore that representation at this boundary.
1317 uint32_t stream_types_net = HOST_TO_NET_U32(stream_types);
1318 handle_stream_start_packet(client, &stream_types_net, sizeof(stream_types_net));
1319}
1320
1321static void acip_server_on_stream_stop(uint32_t stream_types, void *client_ctx, void *app_ctx) {
1322 (void)app_ctx;
1323 client_info_t *client = (client_info_t *)client_ctx;
1324 uint32_t stream_types_net = HOST_TO_NET_U32(stream_types);
1325 handle_stream_stop_packet(client, &stream_types_net, sizeof(stream_types_net));
1326}
1327
1328static void acip_server_on_capabilities(const void *cap_data, size_t data_len, void *client_ctx, void *app_ctx) {
1329 (void)app_ctx;
1330 client_info_t *client = (client_info_t *)client_ctx;
1331 handle_client_capabilities_packet(client, (void *)cap_data, data_len);
1332}
1333
1334static void acip_server_on_ping(void *client_ctx, void *app_ctx) {
1335 (void)app_ctx;
1336 client_info_t *client = (client_info_t *)client_ctx;
1337
1338 // Respond with PONG using ACIP transport
1339 // Keep the lock through the packet write: this handler runs on the receive
1340 // thread and otherwise races the video/audio sender on TCP.
1341 mutex_lock(&client->send_mutex);
1342 if (atomic_load_bool(&client->shutting_down) || !client->transport) {
1343 mutex_unlock(&client->send_mutex);
1344 return; // Client is shutting down, skip pong
1345 }
1346 acip_transport_t *pong_transport = client->transport;
1347 asciichat_error_t pong_result = acip_send_pong(pong_transport);
1348 mutex_unlock(&client->send_mutex);
1349
1350 if (pong_result != ASCIICHAT_OK) {
1351 SET_ERRNO(ERROR_NETWORK, "Failed to send PONG response to client %s: %s", client->client_id, asciichat_error_string(pong_result));
1352 }
1353}
1354
1355static void acip_server_on_pong(void *client_ctx, void *app_ctx) {
1356 (void)client_ctx;
1357 (void)app_ctx;
1358 // Client acknowledged our PING - no action needed
1359}
1360
1361static void acip_server_on_error(const error_packet_t *header, const char *message, void *client_ctx, void *app_ctx) {
1362 (void)app_ctx;
1363 client_info_t *client = (client_info_t *)client_ctx;
1364
1365 // Reconstruct full packet for existing handler
1366 size_t msg_len = strlen(message);
1367 size_t total_len = sizeof(*header) + msg_len;
1368 uint8_t *full_packet = SAFE_MALLOC(total_len, uint8_t *);
1369 if (!full_packet) {
1370 log_error("Failed to allocate buffer for ERROR_MESSAGE reconstruction");
1371 return;
1372 }
1373
1374 memcpy(full_packet, header, sizeof(*header));
1375 // NOLINTNEXTLINE(bugprone-not-null-terminated-result) - binary packet data, not string
1376 memcpy(full_packet + sizeof(*header), message, msg_len);
1377
1378 handle_client_error_packet(client, full_packet, total_len);
1379 SAFE_FREE(full_packet);
1380}
1381
1382static void acip_server_on_remote_log(const remote_log_packet_t *header, const char *message, void *client_ctx, void *app_ctx) {
1383 (void)app_ctx;
1384 client_info_t *client = (client_info_t *)client_ctx;
1385
1386 // Reconstruct full packet for existing handler
1387 size_t msg_len = strlen(message);
1388 size_t total_len = sizeof(*header) + msg_len;
1389 uint8_t *full_packet = SAFE_MALLOC(total_len, uint8_t *);
1390 if (!full_packet) {
1391 log_error("Failed to allocate buffer for REMOTE_LOG reconstruction");
1392 return;
1393 }
1394
1395 memcpy(full_packet, header, sizeof(*header));
1396 // NOLINTNEXTLINE(bugprone-not-null-terminated-result) - binary packet data, not string
1397 memcpy(full_packet + sizeof(*header), message, msg_len);
1398
1399 handle_remote_log_packet_from_client(client, full_packet, total_len);
1400 SAFE_FREE(full_packet);
1401}
1402
1411void process_decrypted_packet(client_info_t *client, packet_type_t type, void *data, size_t len) {
1412 if (type == 5000) { // CLIENT_CAPABILITIES
1413 log_debug("CLIENT: client_id=%s, data=%p, len=%zu", client->client_id, data, len);
1414 }
1415
1416 // Rate limiting: Check and record packet-specific rate limits
1417 if (g_rate_limiter) {
1418 bool allowed = false;
1421 if (result != ASCIICHAT_OK || !allowed) {
1422 if (result == ASCIICHAT_OK)
1423 SET_ERRNO(ERROR_RATE_LIMITED, "Packet quota exceeded");
1424 LOG_ERRNO_IF_SET("Packet rate-limit enforcement rejected client");
1426 mutex_lock(&client->send_mutex);
1428 mutex_unlock(&client->send_mutex);
1429 if (sent != ASCIICHAT_OK)
1430 LOG_ERRNO_IF_SET("Could not notify client about rate-limit decision");
1432 if (result == ASCIICHAT_OK)
1434 atomic_store_bool(&client->active, false);
1435 return;
1436 }
1437 ASSERT_NO_ERRNO_SINCE(limit_scope);
1439 }
1440
1441 // O(1) dispatch via hash table lookup
1442 int idx = client_dispatch_hash_lookup(g_client_dispatch_hash, type);
1443 if (type == 5000 || type == 3001) {
1444 log_error("DISPATCH_LOOKUP: type=%d, idx=%d (len=%zu)", type, idx, len);
1445 }
1446 if (idx < 0) {
1447 disconnect_client_for_bad_data(client, "Unknown packet type: %d (len=%zu)", type, len);
1448 return;
1449 }
1450
1451 if (type == 5000 || type == 3001) {
1452 log_error("DISPATCH_HANDLER: type=%d, calling handler[%d]...", type, idx);
1453 }
1454 g_client_dispatch_handlers[idx](client, data, len);
1455 if (type == 5000 || type == 3001) {
1456 log_error("DISPATCH_HANDLER: type=%d, handler returned", type);
1457 }
1458}
Application-level callbacks for library code.
void errno_scope_end(asciichat_errno_scope_t scope, asciichat_errno_outcome_t outcome)
asciichat_errno_scope_t errno_scope_begin(void)
const char * asciichat_error_format(const char *message, asciichat_error_t code)
Synchronized per-thread pending error stacks and value snapshots. SET_ERRNO pushes,...
#define SET_ERRNO(code,...)
#define ASSERT_NO_ERRNO_SINCE(scope)
@ ASCIICHAT_ERRNO_HANDLED
#define LOG_ERRNO_IF_SET(message)
void atomic_store_bool(atomic_t *a, bool value)
Atomically store a boolean value.
bool atomic_load_bool(atomic_t *a)
Atomic pointer wrapper.
void * atomic_ptr_load(atomic_ptr_t *a)
Atomically load a pointer.
void atomic_store_u64(atomic_t *a, uint64_t value)
Atomically store a uint64_t value.
bool atomic_exchange_bool(atomic_t *a, bool new_value)
Atomically exchange a boolean and return the old value.
asciichat_error_t audio_convert_channels(const float *input, size_t frames, unsigned input_channels, float *output, unsigned output_channels)
#define AUDIO_SAMPLE_RATE
Definition audio/limits.h:8
#define AUDIO_CHANNELS
Definition audio/limits.h:9
#define AUDIO_OPUS_SERVER_PACKET_CAPACITY
size_t buffer_capacity(const void *data)
Definition buffer.c:278
ClientPacketResult packet_result(int code, const uint8_t *data, size_t length)
Video and audio codec definitions and capability bitmasks.
video_codec_t
Definition codecs.h:28
@ VIDEO_CODEC_H264
H.264 (AVC) video codec.
Definition codecs.h:32
@ VIDEO_CODEC_H265
H.265 (HEVC) video codec.
Definition codecs.h:30
Hardware-Accelerated CRC32 Checksum Computation.
Named object registry for debugging — log identifiable resource names.
🔄 Network byte order conversion helpers
#define HOST_TO_NET_U16(val)
Definition endian.h:100
#define HOST_TO_NET_U32(val)
Definition endian.h:70
#define NET_TO_HOST_U32(val)
Definition endian.h:85
Dedicated worker for all pending errno stacks and handled history. Signal handlers may only request a...
unsigned int height
Definition fft.c:24
unsigned int width
Definition fft.c:24
asciichat_error_t audio_ring_buffer_write(audio_ring_buffer_t *rb, const float *data, int samples)
Write audio samples to ring buffer.
void audio_ring_buffer_destroy(audio_ring_buffer_t *rb)
Destroy an audio ring buffer.
void mixer_remove_source(mixer_t *mixer, const char *client_id)
Remove an audio source from the mixer.
Definition mixer.c:463
void opus_codec_destroy(opus_codec_t *codec)
Destroy an Opus codec instance.
Definition opus.c:271
int mixer_add_source(mixer_t *mixer, const char *client_id, audio_ring_buffer_t *buffer)
Add an audio source to the mixer.
Definition mixer.c:402
unsigned short uint16_t
unsigned int uint32_t
#define SAFE_STRNCPY(dst, src, size)
#define SAFE_FREE(ptr)
#define SAFE_MALLOC(size, cast)
#define read_u32_unaligned
unsigned long long uint64_t
unsigned char uint8_t
#define write_u32_unaligned
#define write_u16_unaligned
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:48
@ ERROR_INVALID_STATE
@ ERROR_NETWORK
Definition error_codes.h:76
@ ERROR_RESOURCE_EXHAUSTED
@ ERROR_NOT_FOUND
@ ERROR_MEMORY
Definition error_codes.h:55
@ ASCIICHAT_OK
Definition error_codes.h:50
@ ERROR_RATE_LIMITED
Definition error_codes.h:84
@ ERROR_INVALID_PARAM
@ ERROR_INTERNAL
Definition error_codes.h:91
@ ERROR_THREAD
@ ERROR_NOT_SUPPORTED
Definition error_codes.h:73
#define MAX_CLIENTS
Maximum possible clients (static array size) - actual runtime limit set by –max-clients (1-128).
#define MAX_DISPLAY_NAME_LEN
Maximum display name length in characters.
#define MAX_CLIENT_ID_LEN
Maximum client ID length - format: "noun.N (transport:port)" e.g., "mountain.0 (tcp:48036)".
#define LOG_RATE_DEFAULT
Log rate limit: 5 seconds (5,000,000 microseconds) - default for audio/video packets.
Definition log_rates.h:36
#define log_warn(...)
Log a WARN message.
Definition log/log.h:615
#define log_dev(...)
Log a DEV message (most verbose, development only).
Definition log/log.h:575
#define log_info_client(client, fmt,...)
Server sends INFO log message to client.
Definition network/log.h:65
#define log_error(...)
Log an ERROR message.
Definition log/log.h:628
#define log_info(...)
Log an INFO message.
Definition log/log.h:602
#define log_debug(...)
Log a DEBUG message.
Definition log/log.h:589
#define VIDEO_CODEC_SUPPORTED(cap, codec)
Check if a video codec is supported in capabilities bitmask.
Definition codecs.h:89
#define NS_PER_SEC_INT
Definition time.h:173
#define US_PER_MS_INT
Definition time.h:176
#define US_PER_SEC_INT
Definition time.h:177
packet_type_t
Network protocol packet type enumeration.
Definition packet.h:315
#define MAX_ERROR_MESSAGE_LENGTH
Maximum error message length (512 bytes).
Definition packet.h:153
@ PACKET_TYPE_AUDIO_OPUS_BATCH
Batched Opus-encoded audio frames.
Definition packet.h:412
@ PACKET_TYPE_CLIENT_LEAVE
Clean disconnect notification.
Definition packet.h:427
@ PACKET_TYPE_IMAGE_FRAME
Complete RGB image with dimensions.
Definition packet.h:399
@ PACKET_TYPE_PONG
Keepalive pong response.
Definition packet.h:423
@ PACKET_TYPE_STREAM_START
Client requests to start sending video/audio.
Definition packet.h:429
@ PACKET_TYPE_ACIP_SETTINGS_SYNC
Settings sync (Initiator -> Host -> All Participants).
Definition packet.h:499
@ PACKET_TYPE_REMOTE_LOG
Bidirectional remote logging packet.
Definition packet.h:390
@ PACKET_TYPE_PROTOCOL_VERSION
Protocol version and capabilities negotiation.
Definition packet.h:317
@ PACKET_TYPE_IMAGE_FRAME_H265
H.265-encoded frame from client (HEVC/H.265 codec).
Definition packet.h:401
@ PACKET_TYPE_CLIENT_JOIN
Client announces capability to send media.
Definition packet.h:425
@ PACKET_TYPE_CLIENT_CAPABILITIES
Client reports terminal capabilities.
Definition packet.h:419
@ PACKET_TYPE_IMAGE_FRAME_H264
Definition packet.h:403
@ PACKET_TYPE_PING
Keepalive ping packet.
Definition packet.h:421
@ PACKET_TYPE_AUDIO_BATCH
Batched audio packets for efficiency.
Definition packet.h:410
@ PACKET_TYPE_STREAM_STOP
Client stops sending media.
Definition packet.h:431
#define GET_OPTION(field)
Definition options.h:1333
#define ASCII_RENDERER_DEFAULT
Consolidated options structure.
Definition options.h:962
int initialize_client_palette(palette_type_t palette_type, const char *custom_chars, char client_palette_chars[ASCII_PALETTE_BUFFER_SIZE], size_t *client_palette_len, char client_luminance_palette[ASCII_LUMINANCE_LEVELS])
Initialize client palette with full configuration.
Definition palette.c:292
@ PALETTE_STANDARD
Standard ASCII palette: " ...',;:clodxkO0KXNWM".
Definition palette.h:100
struct acip_transport acip_transport_t
Forward declaration of ACIP transport.
Definition host.h:508
asciichat_error_t session_host_remove_client(session_host_t *host, uint32_t client_id)
Remove a client by ID.
Definition host.c:1342
void format_bytes_pretty(size_t bytes, char *out, size_t out_capacity)
Format byte count into human-readable string.
Definition util/format.c:11
#define FNV1A_32_OFFSET_BASIS
FNV-1a 32-bit offset basis.
Definition fnv1a.h:39
struct client_info client_info_t
void disconnect_client_for_bad_data(client_info_t *client, const char *format,...)
void video_frame_buffer_destroy(video_frame_buffer_t *vfb)
Destroy frame buffer and free all resources.
Definition video_frame.c:97
video_frame_t * video_frame_begin_write(video_frame_buffer_t *vfb)
Writer API: Start writing a new frame.
video_frame_buffer_t * video_frame_buffer_create(const char *client_id)
Create a double-buffered video frame manager.
Definition video_frame.c:19
void video_frame_commit(video_frame_buffer_t *vfb)
Writer API: Commit the frame and swap buffers.
void digital_rain_destroy(digital_rain_t *rain)
Destroy digital rain effect context.
asciichat_error_t acip_handle_server_packet(acip_transport_t *transport, packet_type_t type, const void *payload, size_t payload_len, void *client_ctx, const acip_server_callbacks_t *callbacks)
Handle incoming packet on server side.
Definition handlers.c:730
ACIP protocol packet handlers (transport-agnostic).
uint32_t session_host_add_client(session_host_t *host, acip_transport_t *transport, acip_transport_ownership_t ownership, const char *ip, int port)
Definition host.c:1094
🔊 Audio Capture and Playback Interface for ascii-chat
⚙️ Common definitions, error codes, macros, and types shared throughout the application
Common declarations and data structures for cryptographic handshake.
Server-side handshake functions.
ACIP server-side protocol API.
Transport abstraction layer for ACIP protocol.
@ ACIP_TRANSPORT_WEBRTC
WebRTC DataChannel (P2P).
struct network_connection network_connection_t
Network error handling utilities.
Capture stdout/stderr and redirect to logging system.
audio_ring_buffer_t * audio_ring_buffer_create_format(unsigned channels, bool capture)
asciichat_error_t acip_send_server_state(acip_transport_t *transport, const server_state_packet_t *state)
Send server state update to client (server → client).
network_connection_state_t network_connection_state(network_connection_t *c)
void network_connection_request_stop(network_connection_t *c)
void * network_connection_context(network_connection_t *c)
acip_transport_t * network_connection_transport(network_connection_t *c)
void network_server_foreach(network_server_t *s, network_connection_visit_fn visit, void *context)
asciichat_error_t check_and_record_packet_rate_limit(rate_limiter_t *rate_limiter, const char *client_ip, packet_type_t packet_type, bool *allowed)
Map packet type to rate event type and check rate limit.
asciichat_error_t network_error_public_code(asciichat_error_t code)
void stats_counter_add(stats_scope_t *scope, stats_counter_id_t id, uint64_t amount)
void stats_duration_record(stats_scope_t *scope, stats_duration_id_t id, uint64_t elapsed_ns)
#define ascii_stabilizer_reset
#define log_debug_every(interval_us, fmt,...)
Rate-limited DEBUG logging.
Definition log/log.h:743
#define log_warn_every(interval_us, fmt,...)
Rate-limited WARN logging.
Definition log/log.h:749
Multi-Source Audio Mixing and Processing System.
🌐 Core network I/O operations with timeout support
int generate_client_id(char *buffer, size_t buffer_size)
Generate a unique client ID (noun only, no counter or port).
Definition nouns.c:482
Wordlist and utilities for generating friendly session and client names.
⚙️ Unified options parsing system for ascii-chat with builder pattern and lock-free access
Opus audio codec wrapper for real-time encoding/decoding.
void ascii_otel_scope_int(ascii_otel_scope_t *s, const char *key, int64_t value)
Definition otel.c:707
void ascii_otel_error(const char *message)
Definition otel.c:779
#define ASCII_OTEL_SCOPE(id)
Definition otel.h:77
asciichat_error_t packet_parse_error_message(const void *data, size_t len, asciichat_error_t *out_error_code, char *message_buffer, size_t message_buffer_size, size_t *out_message_length)
Parse an error packet payload into components.
Definition packet.c:793
ASCII Palette Management for Video-to-ASCII Conversion.
stats_scope_t * stats_runtime_scope(void)
Definition runtime.c:78
asciichat_error_t acip_send_error(acip_transport_t *transport, uint32_t error_code, const char *message)
Send error message packet.
Definition send.c:402
asciichat_error_t acip_send_pong(acip_transport_t *transport)
Send pong packet.
Definition send.c:382
ACIP shared/bidirectional packet sending functions.
rate_limiter_t * g_rate_limiter
Global rate limiter for connection attempts and packet processing.
mixer_t *volatile g_audio_mixer
Global audio mixer instance for multi-client audio processing.
void handle_client_join_packet(client_info_t *client, const void *data, size_t len)
Process CLIENT_JOIN packet - client announces identity and capabilities.
void handle_pong_packet(client_info_t *client, const void *data, size_t len)
Handle PONG packet - client acknowledged our PING.
void handle_protocol_version_packet(client_info_t *client, const void *data, size_t len)
Process PROTOCOL_VERSION packet - validate protocol compatibility.
void handle_image_frame_packet(client_info_t *client, void *data, size_t len)
Process IMAGE_FRAME packet - store client's video data for rendering.
void handle_audio_batch_packet(client_info_t *client, const void *data, size_t len)
Process AUDIO_BATCH packet - store efficiently batched audio samples.
void handle_image_frame_h265_packet(client_info_t *client, const void *data, size_t len)
Process H.265-encoded image frame from client.
void handle_audio_packet(client_info_t *client, const void *data, size_t len)
Process AUDIO packet - store single audio sample batch (legacy format).
void handle_client_leave_packet(client_info_t *client, const void *data, size_t len)
Process CLIENT_LEAVE packet - handle clean client disconnect.
void handle_stream_stop_packet(client_info_t *client, const void *data, size_t len)
Process STREAM_STOP packet - client requests to halt media transmission.
int send_server_state_to_client(client_info_t *client)
Send current server state to a specific client.
void handle_client_capabilities_packet(client_info_t *client, const void *data, size_t len)
void handle_stream_start_packet(client_info_t *client, const void *data, size_t len)
Process STREAM_START packet - client requests to begin media transmission.
void handle_remote_log_packet_from_client(client_info_t *client, const void *data, size_t len)
asciichat_error_t handle_runtime_settings(client_info_t *client, const void *data, size_t length)
void handle_audio_opus_batch_packet(client_info_t *client, const void *data, size_t len)
Process AUDIO_OPUS_BATCH packet - efficient Opus-encoded audio batch from client.
void handle_ping_packet(client_info_t *client, const void *data, size_t len)
Handle PING packet - respond with PONG.
network_server_t * session_server_like_get_network_server(void)
void handle_image_frame_h264_packet(client_info_t *client, const void *data, size_t len)
#define CLIENT_DISPATCH_HANDLER_COUNT
void cleanup_client_media_buffers(client_info_t *client)
void process_decrypted_packet(client_info_t *client, packet_type_t type, void *data, size_t len)
asciichat_error_t server_send_state(acip_transport_t *transport)
void cleanup_client_packet_queues(client_info_t *client)
rwlock_t g_client_manager_rwlock
Reader-writer lock protecting the global client manager.
#define SERVER_CLIENT_PRETTY_BUFFER_SIZE
bool g_client_manager_rwlock_initialized
void(* client_packet_handler_t)(client_info_t *client, const void *data, size_t len)
int start_transport_client_threads(server_context_t *server_ctx, const char *client_id)
client_info_t * find_client_by_id(const char *client_id)
Fast O(1) client lookup by ID using hash table.
client_manager_t g_client_manager
Hash table entry for client packet dispatch.
int remove_client(server_context_t *server_ctx, const char *client_id)
asciichat_error_t server_dispatch_packet(client_info_t *client, packet_type_t type, const void *data, size_t length)
#define CLIENT_DISPATCH_HASH(type)
void broadcast_server_state_to_all_clients(void)
Notify all clients of state changes.
#define SERVER_OPUS_BATCH_OVERHEAD
client_info_t * add_transport_client(server_context_t *server_ctx, acip_transport_t *transport, const char *client_ip)
#define CLIENT_DISPATCH_HASH_SIZE
Per-client state management and lifecycle orchestration.
Server packet processing and protocol implementation.
int create_client_render_threads(server_context_t *server_ctx, client_info_t *client)
Create and initialize per-client rendering threads.
asciichat_error_t stop_client_render_threads(client_info_t *client)
Stop and cleanup per-client rendering threads.
Per-client rendering threads with rate limiting.
#define VIDEO_RENDER_FPS
Multi-client video mixing and ASCII frame generation.
🔤 String Manipulation and Shell Escaping Utilities
Server-side packet handler callbacks.
Definition handlers.h:147
When code needs to work with asciichat errno snapshot, it keeps total, copied here instead of scatter...
The metadata prefix for a batch of encoded audio samples. batch_count, total_samples,...
Definition packet.h:979
Hash table entry for client packet dispatch.
uint8_t handler_idx
Handler index (0-based).
packet_type_t key
Packet type (0 = empty slot).
bool transport_encrypted
TLS or DTLS already encrypts this client's transport.
terminal_capabilities_t terminal_caps
char display_name[MAX_DISPLAY_NAME_LEN]
crypto_handshake_context_t * crypto_handshake_ctx
network_connection_t * network_connection
video_frame_buffer_t * incoming_video_buffer
platform_thread_t audio_render_thread
audio_ring_buffer_t * incoming_audio_buffer
platform_thread_t video_render_thread
char client_palette_chars[ASCII_PALETTE_BUFFER_SIZE]
char client_ip[INET_ADDRSTRLEN]
char client_luminance_palette[ASCII_LUMINANCE_LEVELS]
ascii_stabilizer_t render_stabilizer
char client_id[MAX_CLIENT_ID_LEN]
Global client manager structure for server-side client coordination.
Error packet structure carrying error code and textual description.
Definition packet.h:778
Describes one compressed image sent through ACIP. width, height, and pixel_format tell the decoder ho...
Definition packet.h:948
State for one Opus encoding or decoding direction.
Definition opus.h:105
Protocol version negotiation packet structure (Packet Type 1).
Definition packet.h:871
Remote log packet structure carrying log level and message text.
Definition packet.h:792
char display_name[MAX_DISPLAY_NAME_LEN]
Server context - encapsulates all server state.
session_host_t * session_host
Session host for discovery mode support.
Server state packet structure.
Definition packet.h:757
uint32_t reserved[6]
Reserved fields for future use (must be zero).
Definition packet.h:763
uint32_t active_client_count
Number of clients actively sending video/audio streams.
Definition packet.h:761
uint32_t connected_client_count
Total number of currently connected clients.
Definition packet.h:759
One captured, decoded, or rendered video frame and its timing metadata.
bool active
True if buffer is active (receiving frames).
size_t allocated_buffer_size
Size of allocated data buffers (for cleanup).
When code needs to work with simple frame swap, it keeps current_frame, frame_a, frame_b,...
uint64_t sequence_number
Frame sequence number (for drop detection).
uint32_t height
Frame height in pixels.
bool is_keyframe
True if this is a keyframe (important frame).
size_t size
Size of frame data in bytes.
uint32_t width
Frame width in pixels.
uint64_t capture_timestamp_ns
Timestamp when frame was captured (nanoseconds).
void * data
Frame data pointer (points to pre-allocated buffer).
⏱️ High-precision timing utilities using sokol_time.h and uthash
📊 String Formatting Utilities