ascii-chat
Binary v0.16.50-198-g78f51665f
Library lib/v0.3.2-2240-g78f51665f
Video chat in your terminal
Loading...
Searching...
No Matches

🏠 Server-side session hosting implementation More...

Go to the source code of this file.

Data Structures

struct  session_host_client_t
 Keeps the host-side rendering history used to compare successive frames and stabilize output for connected participants. Its buffers and dimensions belong to the host render target and must be reset when the source or terminal geometry changes. More...
struct  host_render_history
 Owns the server-side session state, including connected participants, host callbacks, render history, and shutdown coordination. The session host creates it once, keeps it alive for the session lifetime, and releases its child resources during teardown. More...
struct  session_host
struct  host_connection_t

Macros

#define SESSION_HOST_KEY_PATH_BUFFER_SIZE   512
#define HOST_AUDIO_BUFFER_FRAMES   10
#define SESSION_HOST_DEFAULT_MAX_CLIENTS   MAX_SESSION_PARTICIPANTS
 Default maximum clients.

Typedefs

typedef struct host_render_history host_render_history_t
 Owns the server-side session state, including connected participants, host callbacks, render history, and shutdown coordination. The session host creates it once, keeps it alive for the session lifetime, and releases its child resources during teardown.
typedef struct session_host session_host_t
 Opaque session host handle.

Functions

session_host_t * session_host_create (const session_host_config_t *config)
 Create a new session host.
asciichat_error_t session_host_destroy (session_host_t *host)
 Destroy session host and free resources.
asciichat_error_t session_host_receive_packet (session_host_t *host, uint32_t client_id, packet_type_t ptype, const void *data, size_t len)
asciichat_error_t session_host_render_video (session_host_t *host)
asciichat_error_t session_host_render_audio (session_host_t *host)
asciichat_error_t session_host_start (session_host_t *host)
 Start accepting client connections.
asciichat_error_t session_host_stop (session_host_t *host)
 Stop accepting connections and disconnect all clients.
asciichat_error_t session_host_accept_peer (session_host_t *host, acip_transport_t *transport)
bool session_host_is_running (session_host_t *host)
 Check if host is running.
uint32_t session_host_add_client (session_host_t *host, acip_transport_t *transport, acip_transport_ownership_t ownership, const char *ip, int port)
uint32_t session_host_add_memory_participant (session_host_t *host)
 Add a memory participant (host's own media).
asciichat_error_t session_host_inject_frame (session_host_t *host, uint32_t participant_id, const image_t *frame)
 Inject a video frame from memory participant.
asciichat_error_t session_host_inject_audio (session_host_t *host, uint32_t participant_id, const float *samples, size_t count)
 Inject audio samples from memory participant.
asciichat_error_t session_host_remove_client (session_host_t *host, uint32_t client_id)
 Remove a client by ID.
asciichat_error_t session_host_find_client (session_host_t *host, uint32_t client_id, session_host_client_info_t *info)
 Find a client by ID.
int session_host_get_client_count (session_host_t *host)
 Get number of connected clients.
int session_host_get_client_ids (session_host_t *host, uint32_t *ids, int max_ids)
 Get list of all connected client IDs.
asciichat_error_t session_host_broadcast_frame (session_host_t *host, const char *frame)
 Broadcast ASCII frame to all clients.
asciichat_error_t session_host_send_frame (session_host_t *host, uint32_t client_id, const char *frame)
 Send ASCII frame to a specific client.
asciichat_error_t session_host_set_render_target (session_host_t *host, const session_host_render_target_t *target)
asciichat_error_t session_host_set_display (session_host_t *host, struct session_display_ctx *display)
 Set a native terminal display context for local frame rendering.
asciichat_error_t session_host_start_render (session_host_t *host)
 Start media rendering thread (video mixing and audio distribution).
void session_host_stop_render (session_host_t *host)
 Stop media rendering thread.
asciichat_error_t session_host_set_client_transport (session_host_t *host, uint32_t client_id, acip_transport_t *transport, acip_transport_ownership_t ownership)
 Select a transport for a specific client in the host.
acip_transport_t * session_host_get_client_transport (session_host_t *host, uint32_t client_id)
 Get the current transport for a specific client in the host.
bool session_host_client_has_transport (session_host_t *host, uint32_t client_id)
 Check if a specific client has an active ACIP transport.

Detailed Description

🏠 Server-side session hosting implementation

Implements the session host abstraction for server-side client management and session coordination.

Author
Zachary Fogg me@zf.nosp@m.o.gg
Date
January 2026

Definition in file host.c.

Macro Definition Documentation

◆ HOST_AUDIO_BUFFER_FRAMES

#define HOST_AUDIO_BUFFER_FRAMES   10

Definition at line 5 of file host.c.

Referenced by session_host_add_client(), and session_host_add_memory_participant().

◆ SESSION_HOST_DEFAULT_MAX_CLIENTS

#define SESSION_HOST_DEFAULT_MAX_CLIENTS   MAX_SESSION_PARTICIPANTS

Default maximum clients.

Definition at line 68 of file host.c.

Referenced by session_host_create().

◆ SESSION_HOST_KEY_PATH_BUFFER_SIZE

#define SESSION_HOST_KEY_PATH_BUFFER_SIZE   512

Definition at line 4 of file host.c.

Typedef Documentation

◆ host_render_history_t

Owns the server-side session state, including connected participants, host callbacks, render history, and shutdown coordination. The session host creates it once, keeps it alive for the session lifetime, and releases its child resources during teardown.

Contains server state, client list, and callback configuration.

Function Documentation

◆ session_host_accept_peer()

asciichat_error_t session_host_accept_peer ( session_host_t * host,
acip_transport_t * transport )

Definition at line 1071 of file host.c.

1071 {
1072#ifndef __EMSCRIPTEN__
1073 if (!host || !host->network_server)
1074 return SET_ERRNO(ERROR_INVALID_STATE, "Host network is not running");
1075 return network_server_adopt(host->network_server, transport, "peer", true);
1076#else
1077 (void)host;
1078 (void)transport;
1079 return SET_ERRNO(ERROR_NOT_SUPPORTED, "Browser peers use the browser host bridge");
1080#endif
1081}
#define SET_ERRNO(code,...)
@ ERROR_INVALID_STATE
@ ERROR_NOT_SUPPORTED
Definition error_codes.h:73
asciichat_error_t network_server_adopt(network_server_t *s, acip_transport_t *transport, const char *peer, bool secure)

References ERROR_INVALID_STATE, ERROR_NOT_SUPPORTED, network_server_adopt(), and SET_ERRNO.

◆ session_host_add_client()

uint32_t session_host_add_client ( session_host_t * host,
acip_transport_t * transport,
acip_transport_ownership_t ownership,
const char * ip,
int port )

Register a transport-backed client. The same API accepts TCP, WebSocket, and WebRTC. Ownership transfers only on success. NULL registers a participant whose transport will be supplied later. on_client_join runs after admission.

Definition at line 1094 of file host.c.

1094 {
1095 if (!host || !host->initialized) {
1096 return 0;
1097 }
1098
1099 if ((ownership != ACIP_TRANSPORT_BORROWED && ownership != ACIP_TRANSPORT_OWNED) || (!transport && ownership == ACIP_TRANSPORT_OWNED)) {
1100 SET_ERRNO(ERROR_INVALID_PARAM, "Invalid transport ownership");
1101 return 0;
1102 }
1103
1104 mutex_lock(&host->clients_mutex);
1105
1106 // Check if we have room
1107 if (host->client_count >= host->max_clients) {
1108 mutex_unlock(&host->clients_mutex);
1109 SET_ERRNO(ERROR_SESSION_FULL, "Maximum clients reached");
1110 return 0;
1111 }
1112
1113 // Find empty slot
1114 for (int i = 0; i < host->max_clients; i++) {
1115 if (!host->clients[i].active) {
1116 host->clients[i].participant_type = PARTICIPANT_TYPE_NETWORK;
1117 host->clients[i].client_id = host->next_client_id++;
1118 host->clients[i].connection = NULL;
1119 if (ip) {
1120 SAFE_STRNCPY(host->clients[i].ip_address, ip, sizeof(host->clients[i].ip_address));
1121 }
1122 host->clients[i].port = port;
1123 host->clients[i].active = true;
1124 host->clients[i].video_active = false;
1125 host->clients[i].audio_active = false;
1126 host->clients[i].connected_at = (uint64_t)time(NULL);
1127 host->clients[i].transport = NULL;
1128 host->clients[i].transport_ownership = ACIP_TRANSPORT_BORROWED;
1129 host->clients[i].render_width = 0;
1130 host->clients[i].render_height = 0;
1131 memset(&host->clients[i].rendering, 0, sizeof(host->clients[i].rendering));
1132 host->clients[i].rendering.fps_limit = DEFAULT_MAX_FPS;
1133 host->clients[i].next_video_deadline = 0;
1134
1135 // Allocate media buffers
1136 host->clients[i].incoming_video = image_new(480, 270); // Network-optimal size (HD preview)
1137 host->clients[i].incoming_audio = ringbuffer_create(sizeof(float), (OPUS_FRAME_SAMPLES * AUDIO_CHANNELS) * HOST_AUDIO_BUFFER_FRAMES); // ~200ms buffer @ 48kHz
1138
1139 if (!host->clients[i].incoming_video || !host->clients[i].incoming_audio) {
1140 // Cleanup on allocation failure
1141 if (host->clients[i].incoming_video) {
1142 image_destroy(host->clients[i].incoming_video);
1143 host->clients[i].incoming_video = NULL;
1144 }
1145 if (host->clients[i].incoming_audio) {
1146 ringbuffer_destroy(host->clients[i].incoming_audio);
1147 host->clients[i].incoming_audio = NULL;
1148 }
1149 host->clients[i].active = false;
1150 mutex_unlock(&host->clients_mutex);
1151 SET_ERRNO(ERROR_MEMORY, "Failed to allocate media buffers for client");
1152 return 0;
1153 }
1154
1155 host->clients[i].transport = transport;
1156 host->clients[i].transport_ownership = ownership;
1157 uint32_t client_id = host->clients[i].client_id;
1158 host->client_count++;
1159
1160 mutex_unlock(&host->clients_mutex);
1161
1162 // Invoke callback
1163 if (host->callbacks.on_client_join) {
1164 host->callbacks.on_client_join(host, client_id, host->user_data);
1165 }
1166
1167 return client_id;
1168 }
1169 }
1170
1171 mutex_unlock(&host->clients_mutex);
1172 return 0;
1173}
#define OPUS_FRAME_SAMPLES
#define AUDIO_CHANNELS
Definition audio/limits.h:9
unsigned int uint32_t
#define SAFE_STRNCPY(dst, src, size)
unsigned long long uint64_t
@ ERROR_MEMORY
Definition error_codes.h:55
@ ERROR_SESSION_FULL
Definition error_codes.h:86
@ ERROR_INVALID_PARAM
#define DEFAULT_MAX_FPS
Default maximum frame rate (frames per second).
ringbuffer_t * ringbuffer_create(size_t element_size, size_t capacity)
Create a new ring buffer.
Definition ringbuffer.c:34
void ringbuffer_destroy(ringbuffer_t *rb)
Destroy a ring buffer and free its memory.
Definition ringbuffer.c:60
void image_destroy(image_t *image)
Destroy an image allocated with image_new().
image_t * image_new(size_t width, size_t height)
Create a new image with standard allocation.
#define HOST_AUDIO_BUFFER_FRAMES
Definition host.c:5

References ACIP_TRANSPORT_BORROWED, ACIP_TRANSPORT_OWNED, AUDIO_CHANNELS, DEFAULT_MAX_FPS, ERROR_INVALID_PARAM, ERROR_MEMORY, ERROR_SESSION_FULL, HOST_AUDIO_BUFFER_FRAMES, image_destroy(), image_new(), OPUS_FRAME_SAMPLES, PARTICIPANT_TYPE_NETWORK, ringbuffer_create(), ringbuffer_destroy(), SAFE_STRNCPY, and SET_ERRNO.

Referenced by add_transport_client(), and BrowserHostBinding::addClient().

◆ session_host_receive_packet()

asciichat_error_t session_host_receive_packet ( session_host_t * host,
uint32_t client_id,
packet_type_t type,
const void * data,
size_t len )

Process a packet delivered by an event-driven transport.

Definition at line 343 of file host.c.

343 {
344 if (!host || !host->initialized || (!data && len))
345 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid host packet");
346 acip_transport_t *client_transport = session_host_get_client_transport(host, client_id);
347 switch (ptype) {
349 // Client sent a video frame - parse and store in incoming buffer
350 if (data && len >= sizeof(image_frame_packet_t)) {
351 const image_frame_packet_t *frame_hdr = (const image_frame_packet_t *)data;
352 const uint8_t *pixel_data = (const uint8_t *)data + sizeof(image_frame_packet_t);
353 size_t pixel_data_size = len - sizeof(image_frame_packet_t);
354 uint32_t width = NET_TO_HOST_U32(frame_hdr->width);
356 uint32_t pixel_format = NET_TO_HOST_U32(frame_hdr->pixel_format);
357 size_t bytes_per_pixel = (pixel_format == PIXEL_FORMAT_RGBA || pixel_format == PIXEL_FORMAT_BGRA) ? 4 : 3;
358 size_t expected_size = 0;
359 size_t pixel_count = 0;
360
361 if (pixel_format < PIXEL_FORMAT_RGB || pixel_format > PIXEL_FORMAT_BGRA || width == 0 || height == 0 || width > INT_MAX || height > INT_MAX ||
362 checked_size_mul((size_t)width, (size_t)height, &pixel_count) != ASCIICHAT_OK || checked_size_mul(pixel_count, bytes_per_pixel, &expected_size) != ASCIICHAT_OK ||
363 expected_size != pixel_data_size) {
364 log_warn("Discarding malformed video frame from client %u (%ux%u, format %u, payload %zu)", client_id, width, height, pixel_format, pixel_data_size);
365 } else {
366 // Frame dimensions and pixel format are network-order fields. Resize
367 // this participant's image when its capture dimensions change.
368 mutex_lock(&host->clients_mutex);
369 for (int j = 0; j < host->max_clients; j++) {
370 if (host->clients[j].active && host->clients[j].client_id == client_id && host->clients[j].incoming_video) {
371 image_t *img = host->clients[j].incoming_video;
372 if (img->w != (int)width || img->h != (int)height) {
373 image_t *resized = image_new(width, height);
374 if (!resized) {
375 log_warn("Could not allocate %ux%u video frame for client %u", width, height, client_id);
376 break;
377 }
378 image_destroy(img);
379 host->clients[j].incoming_video = img = resized;
380 }
381
382 uint8_t *dst = (uint8_t *)img->pixels;
383 if (pixel_format == PIXEL_FORMAT_RGB) {
384 memcpy(dst, pixel_data, pixel_count * 3);
385 } else if (pixel_format == PIXEL_FORMAT_BGR) {
386 for (size_t p = 0; p < pixel_count; p++) {
387 dst[p * 3] = pixel_data[p * 3 + 2];
388 dst[p * 3 + 1] = pixel_data[p * 3 + 1];
389 dst[p * 3 + 2] = pixel_data[p * 3];
390 }
391 } else {
392 const bool bgra = pixel_format == PIXEL_FORMAT_BGRA;
393 for (size_t p = 0; p < pixel_count; p++) {
394 const uint8_t *src = pixel_data + p * 4;
395 dst[p * 3] = src[bgra ? 2 : 0];
396 dst[p * 3 + 1] = src[1];
397 dst[p * 3 + 2] = src[bgra ? 0 : 2];
398 }
399 }
400 log_debug_every(500 * US_PER_MS_INT, "Frame received from client %u (%ux%u, format %u)", client_id, width, height, pixel_format);
401 break;
402 }
403 }
404 mutex_unlock(&host->clients_mutex);
405 }
406 }
407 break;
408
410 // Client sent Opus-encoded audio batch
411 if (data && len > 16) { // Must have at least header
412 const uint8_t *batch_data = (const uint8_t *)data;
413 // Parse header: sample_rate (4), frame_duration (4), frame_count (4), reserved (4)
414 (void)batch_data[0]; // Avoid unused variable warning if we don't use sample_rate/frame_duration
415 uint32_t batch_frame_count_net = 0;
416 memcpy(&batch_frame_count_net, batch_data + 8, sizeof(batch_frame_count_net));
417 uint32_t batch_frame_count = NET_TO_HOST_U32(batch_frame_count_net);
418
419 size_t frame_sizes_len = (size_t)batch_frame_count * sizeof(uint16_t);
420 if (batch_frame_count > 0 && batch_frame_count <= 1000 && 16 + frame_sizes_len <= len) {
421 const uint8_t *frame_sizes = batch_data + 16;
422 const uint8_t *opus_frames = batch_data + 16 + (batch_frame_count * sizeof(uint16_t));
423 const uint8_t *batch_end = batch_data + len;
424
425 // Find client and decode audio
426 mutex_lock(&host->clients_mutex);
427 for (int j = 0; j < host->max_clients; j++) {
428 if (host->clients[j].client_id == client_id && host->clients[j].active && host->clients[j].incoming_audio) {
429 if (!host->clients[j].opus_decoder)
431 if (!host->clients[j].opus_decoder) {
432 mutex_unlock(&host->clients_mutex);
433 return SET_ERRNO(ERROR_AUDIO, "Failed to initialize participant decoder");
434 }
435 // Each participant owns an independent predictive Opus stream.
436 const uint8_t *current_frame = opus_frames;
437 for (uint32_t k = 0; k < batch_frame_count; k++) {
438 uint16_t frame_size_net = 0;
439 memcpy(&frame_size_net, frame_sizes + k * sizeof(frame_size_net), sizeof(frame_size_net));
440 uint16_t frame_size = NET_TO_HOST_U16(frame_size_net);
441 if ((size_t)(batch_end - current_frame) < frame_size) {
442 break;
443 }
444 if (frame_size > 0) {
445 // Allocate buffer for decoded samples
446 float decoded_samples[(OPUS_FRAME_SAMPLES * AUDIO_CHANNELS)]; // Max 20ms @ 48kHz
447 int decoded_count = opus_codec_decode(host->clients[j].opus_decoder, current_frame, (int)frame_size, decoded_samples, (OPUS_FRAME_SAMPLES * AUDIO_CHANNELS));
448 if (decoded_count > 0) {
449 host->clients[j].audio_active = true;
450 // Write samples to ringbuffer one at a time
451 for (int s = 0; s < decoded_count; s++) {
452 ringbuffer_write(host->clients[j].incoming_audio, &decoded_samples[s]);
453 }
454 }
455 }
456 current_frame += frame_size;
457 }
458 log_debug_every(NS_PER_MS_INT, "Audio batch received from client %u (%u frames)", client_id, batch_frame_count);
459 break;
460 }
461 }
462 mutex_unlock(&host->clients_mutex);
463 }
464 }
465 break;
466
468 log_info("Client %u started streaming", client_id);
469 mutex_lock(&host->clients_mutex);
470 for (int j = 0; j < host->max_clients; j++) {
471 if (host->clients[j].client_id == client_id) {
472 host->clients[j].video_active = true;
473 break;
474 }
475 }
476 mutex_unlock(&host->clients_mutex);
477 break;
478
482 size_t reply_length = 0;
484 session_host_client_t *target = NULL;
485 mutex_lock(&host->clients_mutex);
486 for (int j = 0; j < host->max_clients; ++j) {
487 if (host->clients[j].active && host->clients[j].client_id == client_id) {
488 target = &host->clients[j];
489 break;
490 }
491 }
492 if (!target)
493 result = SET_ERRNO(ERROR_INVALID_STATE, "Unknown settings participant");
494 else if (ptype == PACKET_TYPE_CLIENT_CAPABILITIES) {
495 if (!data || len != sizeof(terminal_capabilities_packet_t))
496 result = SET_ERRNO(ERROR_INVALID_PARAM, "Invalid capabilities");
497 else {
499 memcpy(&packet, data, sizeof(packet));
500 if (target->rendering.width) {
501 // Codec advertisements cannot change an established viewer's settings.
502 mutex_unlock(&host->clients_mutex);
503 return ASCIICHAT_OK;
504 }
505 result = acip_render_settings_capabilities(&target->rendering, &packet);
506 if (result == ASCIICHAT_OK) {
508 size_t query_length;
509 acip_settings_header_t header = {.schema = 1, .expected_revision = UINT32_MAX};
510 result = acip_settings_encode(&header, "", query, sizeof(query), &query_length);
511 if (result == ASCIICHAT_OK)
512 result = acip_settings_request(&target->rendering, query, query_length, reply, sizeof(reply), &reply_length);
513 }
514 }
515 } else if (!target->rendering.revision)
516 result = SET_ERRNO(ERROR_INVALID_STATE, "Settings received before capabilities");
517 else
518 result = acip_settings_request(&target->rendering, data, len, reply, sizeof(reply), &reply_length);
519 if (target && result == ASCIICHAT_OK) {
520 target->render_width = target->rendering.width;
521 target->render_height = target->rendering.height;
522 if (reply_length)
523 result = packet_send_via_transport(target->transport, PACKET_TYPE_ACIP_SETTINGS_ACK, reply, reply_length, 0);
524 }
525 mutex_unlock(&host->clients_mutex);
526 return result;
527 }
528
530 log_info("Client %u stopped streaming", client_id);
531 mutex_lock(&host->clients_mutex);
532 for (int j = 0; j < host->max_clients; j++) {
533 if (host->clients[j].client_id == client_id) {
534 host_forget_render_history(host, client_id);
535 host->clients[j].video_active = false;
536 host->clients[j].audio_active = false;
537 break;
538 }
539 }
540 mutex_unlock(&host->clients_mutex);
541 break;
542
543 case PACKET_TYPE_PING:
544 // Respond with PONG
545 log_debug_every(NS_PER_MS_INT, "PING from client %u", client_id);
546 acip_send_pong(client_transport);
547 break;
548
550 log_info("Client %u requested disconnect", client_id);
551 // Mark for removal (will be handled separately to avoid deadlock)
552 break;
553
554 default:
555 log_warn("Unknown packet type %u from client %u", ptype, client_id);
556 break;
557 }
558
559 return ASCIICHAT_OK;
560}
#define AUDIO_SAMPLE_RATE
Definition audio/limits.h:8
#define NET_TO_HOST_U16(val)
Definition endian.h:115
#define NET_TO_HOST_U32(val)
Definition endian.h:85
unsigned int height
Definition fft.c:24
unsigned int width
Definition fft.c:24
int opus_codec_decode(opus_codec_t *codec, const uint8_t *data, size_t data_len, float *out_samples, int out_num_samples)
Decode Opus audio frame.
Definition opus.c:171
opus_codec_t * opus_codec_create_decoder_channels(int sample_rate, int channels)
Definition opus.c:91
unsigned short uint16_t
unsigned char uint8_t
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:48
@ ERROR_AUDIO
Definition error_codes.h:66
@ ASCIICHAT_OK
Definition error_codes.h:50
#define log_warn(...)
Log a WARN message.
Definition log/log.h:615
#define log_info(...)
Log an INFO message.
Definition log/log.h:602
#define NS_PER_MS_INT
Definition time.h:172
#define US_PER_MS_INT
Definition time.h:176
@ 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_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_ACIP_SETTINGS_ACK
Settings acknowledgment (Participant -> Initiator).
Definition packet.h:501
@ PACKET_TYPE_CLIENT_CAPABILITIES
Client reports terminal capabilities.
Definition packet.h:419
@ PACKET_TYPE_PING
Keepalive ping packet.
Definition packet.h:421
@ PACKET_TYPE_STREAM_STOP
Client stops sending media.
Definition packet.h:431
#define PIXEL_FORMAT_BGRA
BGRA pixel format.
#define PIXEL_FORMAT_RGB
Packed RGB24 pixel format used by ACIP image-frame senders.
#define PIXEL_FORMAT_BGR
BGR pixel format.
#define PIXEL_FORMAT_RGBA
Packed RGBA32 pixel format.
bool ringbuffer_write(ringbuffer_t *rb, const void *data)
Try to write an element to the ring buffer (non-blocking).
Definition ringbuffer.c:70
acip_transport_t * session_host_get_client_transport(session_host_t *host, uint32_t client_id)
Get the current transport for a specific client in the host.
Definition host.c:1704
struct acip_transport acip_transport_t
Forward declaration of ACIP transport.
Definition host.h:508
asciichat_error_t acip_render_settings_capabilities(acip_render_settings_t *state, const terminal_capabilities_packet_t *packet)
asciichat_error_t acip_settings_encode(const acip_settings_header_t *header, const char *json, uint8_t *buffer, size_t capacity, size_t *length)
asciichat_error_t acip_settings_request(acip_render_settings_t *state, const void *payload, size_t length, uint8_t *reply, size_t capacity, size_t *reply_length)
#define log_debug_every(interval_us, fmt,...)
Rate-limited DEBUG logging.
Definition log/log.h:743
asciichat_error_t acip_send_pong(acip_transport_t *transport)
Send pong packet.
Definition send.c:382
asciichat_error_t packet_send_via_transport(acip_transport_t *transport, packet_type_t type, const void *payload, size_t payload_len, uint32_t client_id)
Definition send.c:183
acip settings header keeps the values that move through the subsystem: schema, request,...
Describes one compressed image sent through ACIP. width, height, and pixel_format tell the decoder ho...
Definition packet.h:948
uint32_t pixel_format
Pixel format enum (1=RGB24, 2=RGBA32, 3=BGR24, 4=BGRA32).
Definition packet.h:954
uint32_t height
Image height in pixels.
Definition packet.h:952
uint32_t width
Image width in pixels.
Definition packet.h:950
int h
Image height in pixels (must be > 0).
rgb_pixel_t * pixels
Pixel data array (width * height RGB pixels, row-major order).
int w
Image width in pixels (must be > 0).
Keeps the host-side rendering history used to compare successive frames and stabilize output for conn...
Definition host.c:88
acip_render_settings_t rendering
Definition host.c:100
uint16_t render_height
Definition host.c:99
uint16_t render_width
Definition host.c:98
struct acip_transport * transport
Transport borrowed from the connection owner or browser bridge.
Definition host.c:106
Terminal capabilities packet structure (Packet Type 5).
Definition packet.h:1093

References acip_render_settings_capabilities(), acip_send_pong(), acip_settings_encode(), ACIP_SETTINGS_HEADER_SIZE, ACIP_SETTINGS_MAX_SIZE, acip_settings_request(), ASCIICHAT_OK, AUDIO_CHANNELS, AUDIO_SAMPLE_RATE, ERROR_AUDIO, ERROR_INVALID_PARAM, ERROR_INVALID_STATE, image::h, acip_render_settings_t::height, height, image_frame_packet_t::height, image_destroy(), image_new(), log_debug_every, log_info, log_warn, NET_TO_HOST_U16, NET_TO_HOST_U32, NS_PER_MS_INT, opus_codec_create_decoder_channels(), opus_codec_decode(), OPUS_FRAME_SAMPLES, packet_send_via_transport(), PACKET_TYPE_ACIP_SETTINGS_ACK, PACKET_TYPE_ACIP_SETTINGS_SYNC, PACKET_TYPE_AUDIO_OPUS_BATCH, PACKET_TYPE_CLIENT_CAPABILITIES, PACKET_TYPE_CLIENT_LEAVE, PACKET_TYPE_IMAGE_FRAME, PACKET_TYPE_PING, PACKET_TYPE_STREAM_START, PACKET_TYPE_STREAM_STOP, image_frame_packet_t::pixel_format, PIXEL_FORMAT_BGR, PIXEL_FORMAT_BGRA, PIXEL_FORMAT_RGB, PIXEL_FORMAT_RGBA, image::pixels, session_host_client_t::render_height, session_host_client_t::render_width, session_host_client_t::rendering, acip_render_settings_t::revision, ringbuffer_write(), session_host_get_client_transport(), SET_ERRNO, session_host_client_t::transport, US_PER_MS_INT, image::w, acip_render_settings_t::width, image_frame_packet_t::width, and width.

Referenced by BrowserHostBinding::receive().

◆ session_host_render_audio()

asciichat_error_t session_host_render_audio ( session_host_t * host)

Mix and distribute one 20 ms audio frame without starting a thread.

Definition at line 794 of file host.c.

794 {
795 if (!host || !host->initialized)
796 return SET_ERRNO(ERROR_INVALID_PARAM, "invalid host");
797 uint64_t mix_start = time_get_ns();
798 double total[(OPUS_FRAME_SAMPLES * AUDIO_CHANNELS)] = {0};
800 mutex_lock(&host->clients_mutex);
801 // Drain each source once. Keep unclipped samples so mix-minus can subtract
802 // the recipient before clipping without leaking its voice into the output.
803 for (int i = 0; i < host->max_clients; i++) {
804 session_host_client_t *client = &host->clients[i];
805 memset(client->audio_frame, 0, sizeof(client->audio_frame));
806 if (!client->active || !client->audio_active || !client->incoming_audio)
807 continue;
808 for (int j = 0; j < (OPUS_FRAME_SAMPLES * AUDIO_CHANNELS); j++) {
809 if (ringbuffer_read(client->incoming_audio, &client->audio_frame[j]))
810 total[j] += client->audio_frame[j];
811 }
812 }
813 for (int i = 0; i < host->max_clients; i++) {
814 session_host_client_t *client = &host->clients[i];
815 if (!client->active || !client->transport)
816 continue;
817 ASCII_OTEL_SCOPE(SERVER_AUDIO);
818 if (!client->opus_encoder)
820 if (!client->opus_encoder) {
821 result = SET_ERRNO(ERROR_AUDIO, "Failed to initialize participant encoder");
822 continue;
823 }
824 float mixed[(OPUS_FRAME_SAMPLES * AUDIO_CHANNELS)];
825 for (int j = 0; j < (OPUS_FRAME_SAMPLES * AUDIO_CHANNELS); j++) {
826 float sample = (float)(total[j] - client->audio_frame[j]);
827 mixed[j] = sample > 1.0f ? 1.0f : sample < -1.0f ? -1.0f : sample;
828 }
830 size_t opus_len = opus_codec_encode(client->opus_encoder, mixed, OPUS_FRAME_SAMPLES * AUDIO_CHANNELS, opus_buffer, sizeof(opus_buffer));
831 if (opus_len > 0) {
832 uint16_t frame_sizes[1] = {(uint16_t)opus_len};
833 asciichat_error_t sent = acip_send_audio_opus_batch(client->transport, opus_buffer, opus_len, frame_sizes, 1, AUDIO_SAMPLE_RATE, CLIENT_AUDIO_PIPELINE_FRAME_MS);
834 if (sent != ASCIICHAT_OK)
835 log_warn("Failed to send mixed audio to client %u", client->client_id);
836 }
837 }
838 mutex_unlock(&host->clients_mutex);
840#ifndef __EMSCRIPTEN__
841 float recorded[(OPUS_FRAME_SAMPLES * AUDIO_CHANNELS)];
842 for (int j = 0; j < (OPUS_FRAME_SAMPLES * AUDIO_CHANNELS); j++)
843 recorded[j] = total[j] > 1.0 ? 1.0f : total[j] < -1.0 ? -1.0f : (float)total[j];
845#endif
846 return result;
847}
#define CLIENT_AUDIO_PIPELINE_FRAME_MS
#define AUDIO_OPUS_SESSION_PACKET_CAPACITY
size_t opus_codec_encode(opus_codec_t *codec, const float *samples, int num_samples, uint8_t *out_data, size_t out_size)
Encode audio frame with Opus.
Definition opus.c:132
opus_codec_t * opus_codec_create_encoder_channels(opus_application_t application, int sample_rate, int bitrate, int channels)
Definition opus.c:39
@ OPUS_APPLICATION_AUDIO
General audio (optimized for music).
Definition opus.h:86
bool ringbuffer_read(ringbuffer_t *rb, void *data)
Try to read an element from the ring buffer (non-blocking).
Definition ringbuffer.c:92
void stats_duration_record(stats_scope_t *scope, stats_duration_id_t id, uint64_t elapsed_ns)
#define ASCII_OTEL_SCOPE(id)
Definition otel.h:77
void audio_recording_submit(audio_recording_source_t source, const float *samples, size_t count, uint64_t start_ns)
Definition recording.c:79
@ AUDIO_RECORDING_REMOTE
Definition recording.h:13
stats_scope_t * stats_runtime_scope(void)
Definition runtime.c:78
asciichat_error_t acip_send_audio_opus_batch(acip_transport_t *transport, const void *opus_data, size_t opus_len, const uint16_t *frame_sizes, uint32_t frame_count, uint32_t sample_rate, uint32_t frame_duration)
Send batched Opus-encoded audio frames.
Definition send.c:338
ringbuffer_t * incoming_audio
Incoming audio ringbuffer (written by receive loop, read by render thread).
Definition host.c:113
float audio_frame[(OPUS_FRAME_SAMPLES *AUDIO_CHANNELS)]
Definition host.c:116
opus_codec_t * opus_encoder
Definition host.c:115
uint32_t client_id
Definition host.c:90

References acip_send_audio_opus_batch(), session_host_client_t::active, ASCII_OTEL_SCOPE, ASCIICHAT_OK, session_host_client_t::audio_active, AUDIO_CHANNELS, session_host_client_t::audio_frame, AUDIO_OPUS_SESSION_PACKET_CAPACITY, AUDIO_RECORDING_REMOTE, audio_recording_submit(), AUDIO_SAMPLE_RATE, CLIENT_AUDIO_PIPELINE_FRAME_MS, session_host_client_t::client_id, ERROR_AUDIO, ERROR_INVALID_PARAM, session_host_client_t::incoming_audio, log_warn, OPUS_APPLICATION_AUDIO, opus_codec_create_encoder_channels(), opus_codec_encode(), session_host_client_t::opus_encoder, OPUS_FRAME_SAMPLES, ringbuffer_read(), SET_ERRNO, STATS_DURATION_AUDIO_MIX, stats_duration_record(), stats_runtime_scope(), and session_host_client_t::transport.

Referenced by BrowserHostBinding::renderAudio().

◆ session_host_render_video()

asciichat_error_t session_host_render_video ( session_host_t * host)

Run one shared host video cycle without starting a thread.

Definition at line 787 of file host.c.

787 {
788 if (!host || !host->initialized)
789 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid session host");
790 host_render_video(host, false);
791 return ASCIICHAT_OK;
792}

References ASCIICHAT_OK, ERROR_INVALID_PARAM, and SET_ERRNO.

Referenced by BrowserHostBinding::render().

◆ session_host_set_render_target()

asciichat_error_t session_host_set_render_target ( session_host_t * host,
const session_host_render_target_t * target )

Set local output on native or WASM hosts. NULL disables the local target. A client target is rendered once and delivered through its existing transport. A backend's context must outlive its registration.

Definition at line 1504 of file host.c.

1504 {
1505 if (!host || !host->initialized) {
1506 return SET_ERRNO(ERROR_INVALID_PARAM, "invalid host");
1507 }
1508 if (target) {
1509 bool has_backend = target->get_config || target->render_frame;
1510 bool complete_backend = target->get_config && target->render_frame;
1511 bool valid_target = target->client_id ? !has_backend : complete_backend;
1512 if (!valid_target)
1513 return SET_ERRNO(ERROR_INVALID_PARAM, "Local rendering requires a client or a complete display backend");
1514 }
1515
1516 mutex_lock(&host->clients_mutex);
1517 if (target && target->client_id) {
1518 bool found = false;
1519 for (int i = 0; i < host->max_clients; ++i)
1520 if (host->clients[i].active && host->clients[i].client_id == target->client_id)
1521 found = true;
1522 if (!found) {
1523 mutex_unlock(&host->clients_mutex);
1524 return SET_ERRNO(ERROR_NOT_FOUND, "Local display client not found: %u", target->client_id);
1525 }
1526 }
1527 host->render_target = target ? *target : (session_host_render_target_t){0};
1528 mutex_unlock(&host->clients_mutex);
1529 return ASCIICHAT_OK;
1530}
@ ERROR_NOT_FOUND
session host render target is intentionally a small, concrete record. Fields such as client_id,...
Definition host.h:452
void(* render_frame)(void *context, const char *frame, size_t size, int width, int height)
Definition host.h:455
asciichat_error_t(* get_config)(void *context, session_host_render_config_t *config)
Definition host.h:454

References ASCIICHAT_OK, session_host_render_target_t::client_id, ERROR_INVALID_PARAM, ERROR_NOT_FOUND, session_host_render_target_t::get_config, session_host_render_target_t::render_frame, and SET_ERRNO.

Referenced by BrowserHostBinding::setLocalClient().