ascii-chat
Binary v0.16.50-198-g78f51665f
Library lib/v0.3.2-2240-g78f51665f
Video chat in your terminal
Loading...
Searching...
No Matches
lib/network/acip/client.c
Go to the documentation of this file.
1#include <ascii-chat/otel.h>
14
19#include <ascii-chat/buffer.h>
20#include <ascii-chat/log/log.h>
21#include <ascii-chat/common.h>
22#include <recast/defer.h>
27#include <string.h>
28
29// =============================================================================
30// Client Receive (from server)
31// =============================================================================
32
34 if (!transport || !out) {
35 log_error("[ACIP_RECV] ❌ INVALID_PARAMS: transport=%p, output=%p", (void *)transport, (void *)out);
36 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid transport or packet output");
37 }
38
39 *out = (packet_envelope_t){0};
40 if (!acip_transport_is_connected(transport) && !acip_transport_has_pending_data(transport))
41 return SET_ERRNO(ERROR_NETWORK, "Transport not connected");
42 asciichat_error_t result = packet_receive_via_transport(transport, &out->type, &out->data, &out->len, &out->allocated_buffer);
43 if (result == ASCIICHAT_OK && out->allocated_buffer)
44 out->allocated_size = buffer_capacity(out->allocated_buffer);
45 return result;
46}
47
49 ASCII_OTEL_SCOPE(ACIP_CLIENT_RECEIVE_AND_DISPATCH);
50 if (!callbacks)
51 return SET_ERRNO(ERROR_INVALID_PARAM, "Missing client packet callbacks");
52 packet_envelope_t envelope = {0};
53 asciichat_error_t result = acip_client_receive_packet(transport, &envelope);
54 if (result == ASCIICHAT_OK)
55 result = acip_client_dispatch_packet(transport, envelope.type, envelope.data, envelope.len, callbacks);
56 packet_envelope_release(&envelope);
57 return result;
58}
59
60asciichat_error_t acip_client_dispatch_packet(acip_transport_t *transport, packet_type_t type, const void *payload, size_t length, const acip_client_callbacks_t *callbacks) {
61 if (!transport || !callbacks || (!payload && length))
62 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid client packet dispatch input");
63 return acip_handle_client_packet(transport, type, payload, length, callbacks);
64}
65
67 if (!session)
68 return;
69 *session = (acip_client_session_t){.transport = transport};
70 if (callbacks)
71 session->callbacks = *callbacks;
72}
73
75 if (!session)
76 return SET_ERRNO(ERROR_INVALID_PARAM, "Missing ACIP client session");
77 return acip_client_receive_and_dispatch(session->transport, &session->callbacks);
78}
79
80asciichat_error_t acip_client_session_dispatch(acip_client_session_t *session, packet_type_t type, const void *payload, size_t length) {
81 if (!session)
82 return SET_ERRNO(ERROR_INVALID_PARAM, "Missing ACIP client session");
83 return acip_client_dispatch_packet(session->transport, type, payload, length, &session->callbacks);
84}
85
87 if (!session || !wire)
88 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid ACIP client wire packet");
89 packet_envelope_t envelope = {0};
90 asciichat_error_t result = packet_parse_envelope(wire, length, session->transport ? session->transport->crypto_ctx : NULL, &envelope);
91 if (result == ASCIICHAT_OK && !envelope.consumed)
92 result = acip_client_session_dispatch(session, envelope.type, envelope.data, envelope.len);
93 packet_envelope_release(&envelope);
94 return result;
95}
96
97// =============================================================================
98// Client Send (to server)
99// =============================================================================
100
102 ASCII_OTEL_SCOPE(ACIP_SEND_IMAGE_FRAME);
103 log_dev("★ ACIP_SEND_IMAGE_FRAME: Called with %ux%u, transport=%p, pixel_data=%p", width, height, (void *)transport, pixel_data);
104
105 if (!transport || !pixel_data) {
106 log_dev("★ ACIP_SEND_IMAGE_FRAME: Invalid params");
107 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid transport or pixel_data");
108 }
109
110 // Calculate pixel data size (3 bytes per pixel for RGB24)
111 size_t pixel_size = (size_t)width * height * 3;
112 log_dev("★ ACIP_SEND_IMAGE_FRAME: pixel_size=%zu", pixel_size);
113
114 // Create image frame packet header
116 header.width = HOST_TO_NET_U32(width);
117 header.height = HOST_TO_NET_U32(height);
118 header.pixel_format = HOST_TO_NET_U32(pixel_format);
119 header.compressed_size = 0;
120 header.checksum = 0;
121 header.timestamp = 0;
122
123 socket_buffer_t slices[] = {{&header, sizeof(header)}, {pixel_data, pixel_size}};
124 return packet_send_via_transportv(transport, PACKET_TYPE_IMAGE_FRAME, slices, 2, 0);
125}
126
141 ASCII_OTEL_SCOPE(ACIP_SEND_IMAGE_FRAME_H265);
142 if (!transport || !encoder || !pixel_data) {
143 log_debug("★ ACIP_SEND_IMAGE_FRAME_H265: Invalid params");
144 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid transport, encoder, or pixel_data");
145 }
146
147 // Allocate output buffer for H.265 encoding
148 // H.265 can produce output larger than input due to codec overhead
149 // Use 2x multiplier to account for worst-case frame content
150 size_t output_buffer_size = 5 + (width * height * 2);
151 log_dev("[ACIP_SEND_H265_1] About to allocate output buffer (%zu bytes)", output_buffer_size);
152 uint8_t *output_buffer = buffer_alloc(NULL, output_buffer_size);
153 log_dev("[ACIP_SEND_H265_2] ✅ Allocated output buffer");
154 if (!output_buffer) {
155 log_debug("★ ACIP_SEND_IMAGE_FRAME_H265: Failed to allocate output buffer");
156 return SET_ERRNO(ERROR_MEMORY, "Failed to allocate H.265 output buffer: %zu bytes", output_buffer_size);
157 }
158 defer(buffer_release(output_buffer));
159
160 // Admission expiry can invalidate a previously accepted reference chain.
161#ifndef __EMSCRIPTEN__
164#endif
165 size_t encoded_size = output_buffer_size;
166 log_dev("[ACIP_SEND_H265_3] Calling h265_encode");
167 asciichat_error_t encode_result = h265_encode(encoder, (uint16_t)width, (uint16_t)height, (const uint8_t *)pixel_data, output_buffer, &encoded_size);
168 log_dev("[ACIP_SEND_H265_4] h265_encode returned: %d, encoded_size=%zu", encode_result, encoded_size);
169
170 if (encode_result != ASCIICHAT_OK) {
171 log_error("H.265 encoding failed: %s", asciichat_error_format(NULL, encode_result));
172 return encode_result;
173 }
174
175 // h265_encode includes flags, dimensions, and the encoded picture already.
176 size_t total_size = encoded_size;
177 if (!total_size) {
178 return ASCIICHAT_OK;
179 }
180
181 log_debug("★ ACIP_SEND_IMAGE_FRAME_H265: Encoded %ux%u → %zu bytes", width, height, total_size);
182
183 // Send via transport
184 log_dev("[ACIP_SEND_H265_5] Calling packet_send_via_transport with %zu bytes", total_size);
185 asciichat_error_t result = packet_send_via_transport(transport, PACKET_TYPE_IMAGE_FRAME_H265, output_buffer, total_size, 0);
186 log_dev("[ACIP_SEND_H265_6] packet_send_via_transport returned: %d", result);
187
188 return result;
189}
190
192 ASCII_OTEL_SCOPE(ACIP_SEND_STREAM_START);
193 if (!transport) {
194 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid transport");
195 }
196
197 // Server expects uint32_t (4 bytes), not uint8_t
198 uint32_t stream_types_net = HOST_TO_NET_U32((uint32_t)stream_types);
199 return packet_send_via_transport(transport, PACKET_TYPE_STREAM_START, &stream_types_net, sizeof(stream_types_net), 0);
200}
201
203 ASCII_OTEL_SCOPE(ACIP_SEND_STREAM_STOP);
204 if (!transport) {
205 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid transport");
206 }
207
208 // Server expects uint32_t (4 bytes), not uint8_t
209 uint32_t stream_types_net = HOST_TO_NET_U32((uint32_t)stream_types);
210 return packet_send_via_transport(transport, PACKET_TYPE_STREAM_STOP, &stream_types_net, sizeof(stream_types_net), 0);
211}
212
213asciichat_error_t acip_send_capabilities(acip_transport_t *transport, const void *cap_data, size_t cap_len) {
214 if (!transport || !cap_data) {
215 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid transport or cap_data");
216 }
217
218 return packet_send_via_transport(transport, PACKET_TYPE_CLIENT_CAPABILITIES, cap_data, cap_len, 0);
219}
220
222 if (!transport || !version) {
223 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid transport or version");
224 }
225
226 return packet_send_via_transport(transport, PACKET_TYPE_PROTOCOL_VERSION, version, sizeof(*version), 0);
227}
const char * asciichat_error_format(const char *message, asciichat_error_t code)
#define SET_ERRNO(code,...)
bool acip_outbound_needs_keyframe(acip_transport_t *transport, uint16_t type, uint32_t id)
size_t buffer_capacity(const void *data)
Definition buffer.c:278
void * buffer_alloc(buffer_domain_t *domain, size_t size)
Definition buffer.c:272
#define buffer_release(data)
Definition buffer.h:99
asciichat_error_t h265_encode(h265_encoder_t *encoder, uint16_t width, uint16_t height, const uint8_t *ascii_data, uint8_t *output_buf, size_t *output_size)
Definition encoder.c:316
void h265_encoder_request_keyframe(h265_encoder_t *encoder)
Definition encoder.c:436
struct h265_encoder h265_encoder_t
h265 encoder is intentionally a small, concrete record. Fields such as codec_ctx, codec,...
HEVC/H.265 encoder for ASCII art frames using libx265.
🔄 Network byte order conversion helpers
#define HOST_TO_NET_U32(val)
Definition endian.h:70
unsigned int height
Definition fft.c:24
unsigned int width
Definition fft.c:24
unsigned short uint16_t
unsigned int uint32_t
unsigned char uint8_t
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:48
@ ERROR_NETWORK
Definition error_codes.h:76
@ ERROR_MEMORY
Definition error_codes.h:55
@ ASCIICHAT_OK
Definition error_codes.h:50
@ ERROR_INVALID_PARAM
#define log_dev(...)
Log a DEV message (most verbose, development only).
Definition log/log.h:575
#define log_error(...)
Log an ERROR message.
Definition log/log.h:628
#define log_debug(...)
Log a DEBUG message.
Definition log/log.h:589
packet_type_t
Network protocol packet type enumeration.
Definition packet.h:315
@ 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_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_CAPABILITIES
Client reports terminal capabilities.
Definition packet.h:419
@ PACKET_TYPE_STREAM_STOP
Client stops sending media.
Definition packet.h:431
struct acip_transport acip_transport_t
Forward declaration of ACIP transport.
Definition host.h:508
asciichat_error_t acip_handle_client_packet(acip_transport_t *transport, packet_type_t type, const void *payload, size_t payload_len, const acip_client_callbacks_t *callbacks)
Client-side packet handler callbacks.
Definition handlers.c:182
ACIP protocol packet handlers (transport-agnostic).
⚙️ Common definitions, error codes, macros, and types shared throughout the application
ACIP client-side protocol API.
asciichat_error_t acip_send_image_frame(acip_transport_t *transport, const void *pixel_data, uint32_t width, uint32_t height, uint32_t pixel_format)
Send image frame to server (client → server).
asciichat_error_t acip_send_stream_stop(acip_transport_t *transport, uint8_t stream_types)
Request to stop media streaming (client → server).
asciichat_error_t acip_client_receive_and_dispatch(acip_transport_t *transport, const acip_client_callbacks_t *callbacks)
Receive packet from server and dispatch to callbacks.
asciichat_error_t acip_send_stream_start(acip_transport_t *transport, uint8_t stream_types)
Request to start media streaming (client → server).
asciichat_error_t acip_send_capabilities(acip_transport_t *transport, const void *cap_data, size_t cap_len)
Send terminal capabilities to server (client → server).
asciichat_error_t acip_client_session_process_wire(acip_client_session_t *session, const void *wire, size_t length)
asciichat_error_t acip_send_protocol_version(acip_transport_t *transport, const protocol_version_packet_t *version)
Send protocol version to server (client → server).
asciichat_error_t acip_client_session_receive(acip_client_session_t *session)
void acip_client_session_init(acip_client_session_t *session, acip_transport_t *transport, const acip_client_callbacks_t *callbacks)
asciichat_error_t acip_send_image_frame_h265(acip_transport_t *transport, h265_encoder_t *encoder, const void *pixel_data, uint32_t width, uint32_t height)
Send H.265-encoded video frame to server.
asciichat_error_t acip_client_dispatch_packet(acip_transport_t *transport, packet_type_t type, const void *payload, size_t length, const acip_client_callbacks_t *callbacks)
asciichat_error_t acip_client_session_dispatch(acip_client_session_t *session, packet_type_t type, const void *payload, size_t length)
asciichat_error_t acip_client_receive_packet(acip_transport_t *transport, packet_envelope_t *out)
📝 Logging API with multiple log levels and terminal output control
#define ASCII_OTEL_SCOPE(id)
Definition otel.h:77
✅ Safe Integer Arithmetic and Overflow Detection
asciichat_error_t packet_parse_envelope(const void *wire, size_t length, void *crypto_ctx, packet_envelope_t *out)
Parse one complete wire packet using the shared client receive path.
Definition packet.c:686
void packet_envelope_release(packet_envelope_t *envelope)
Definition packet.c:711
static bool size_t gotel_query_t * out
Definition queries.h:8
asciichat_error_t packet_receive_via_transport(acip_transport_t *transport, packet_type_t *type, void **payload, size_t *payload_len, void **alloc_buffer)
Receive a packet via transport with header parsing and validation.
Definition send.c:192
asciichat_error_t packet_send_via_transportv(acip_transport_t *transport, packet_type_t type, const socket_buffer_t *payload, size_t count, uint32_t client_id)
Send arbitrary packet via transport (generic packet sender).
Definition send.c:150
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 shared/bidirectional packet sending functions.
🔤 String Manipulation and Shell Escaping Utilities
Client-side packet handler callbacks.
Definition handlers.h:55
crypto_context_t * crypto_ctx
Optional encryption context.
Describes one compressed image sent through ACIP. width, height, and pixel_format tell the decoder ho...
Definition packet.h:948
uint32_t width
Image width in pixels.
Definition packet.h:950
Packet envelope containing received packet data.
Definition packet.h:1204
Protocol version negotiation packet structure (Packet Type 1).
Definition packet.h:871