ascii-chat
Binary v0.16.114-28-gf5913657f
Library lib/v0.3.2-3302-gf5913657f
Video chat in your terminal
Loading...
Searching...
No Matches
client.c File Reference

This is the handoff point for h265 client context. Callers fill encoder, source_type, source_location, current_width before passing it on; consumers read the values during the operation and return ownership only where the member contract explicitly says they do. More...

Go to the source code of this file.

Data Structures

struct  h265_client_context

Typedefs

typedef struct h265_client_context h265_client_context_t

Functions

h265_client_context_t * h265_client_context_create (uint16_t width, uint16_t height)
void h265_client_context_destroy (h265_client_context_t *ctx)
asciichat_error_t h265_client_init_media_source (h265_client_context_t *ctx, h265_media_source_t source_type, const char *source_location)
asciichat_error_t h265_client_capture_and_encode (h265_client_context_t *ctx, uint8_t *output_buf, size_t *output_size)
void h265_client_request_keyframe (h265_client_context_t *ctx)
void h265_client_get_stats (h265_client_context_t *ctx, uint64_t *total_frames, uint64_t *keyframes, uint32_t *avg_bitrate)

Detailed Description

This is the handoff point for h265 client context. Callers fill encoder, source_type, source_location, current_width before passing it on; consumers read the values during the operation and return ownership only where the member contract explicitly says they do.

Client-side H.265 media capture and encoding pipeline

Definition in file src/session/h265/client.c.

Typedef Documentation

◆ h265_client_context_t

Function Documentation

◆ h265_client_capture_and_encode()

asciichat_error_t h265_client_capture_and_encode ( h265_client_context_t * ctx,
uint8_t * output_buf,
size_t * output_size )

Capture and encode the next frame Reads from the media source, converts to ASCII, and encodes to H.265

Parameters
ctxClient context
output_bufOutput buffer for H.265 packet
output_sizeInput: buffer size, Output: actual packet size
Returns
Error code (ASCIICHAT_OK on success)

The output packet includes: [flags: u8][width: u16][height: u16][x265_data...] as defined in h265_encoder

Definition at line 123 of file src/session/h265/client.c.

123 {
124 if (!ctx || !output_buf || !output_size) {
125 return SET_ERRNO(ERROR_INTERNAL, "Invalid arguments to capture_and_encode");
126 }
127
128 if (!ctx->encoder) {
129 return SET_ERRNO(ERROR_INTERNAL, "Encoder not initialized");
130 }
131
132 memset(ctx->frame_buf, 128, ctx->frame_buf_size);
133
134 asciichat_error_t result = h265_encode(ctx->encoder, ctx->current_width, ctx->current_height, ctx->frame_buf, output_buf, output_size);
135
136 return result;
137}
#define SET_ERRNO(code,...)
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
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:48
@ ERROR_INTERNAL
Definition error_codes.h:94

References h265_client_context::current_height, h265_client_context::current_width, h265_client_context::encoder, ERROR_INTERNAL, h265_client_context::frame_buf, h265_client_context::frame_buf_size, h265_encode(), and SET_ERRNO.

◆ h265_client_context_create()

h265_client_context_t * h265_client_context_create ( uint16_t width,
uint16_t height )

Create a client H.265 encoding context Initializes the encoder and media pipeline

Parameters
widthInitial frame width (characters)
heightInitial frame height (characters)
Returns
Context handle, or NULL on error

Definition at line 30 of file src/session/h265/client.c.

30 {
31 if (width == 0 || height == 0) {
32 SET_ERRNO(ERROR_MEDIA_INIT, "Frame dimensions must be non-zero");
33 return NULL;
34 }
35 if ((size_t)width * height > SIZE_MAX / 3) {
36 SET_ERRNO(ERROR_MEDIA_INIT, "RGB frame dimensions exceed addressable storage");
37 return NULL;
38 }
39
41 if (!ctx) {
42 SET_ERRNO(ERROR_MEMORY, "Failed to allocate client context");
43 return NULL;
44 }
45
47 if (!ctx->encoder) {
48 SET_ERRNO(ERROR_MEDIA_INIT, "Failed to create H.265 encoder");
49 SAFE_FREE(ctx);
50 return NULL;
51 }
52
53 ctx->current_width = width;
55 ctx->frame_buf_size = (size_t)width * height * 3;
57 if (!ctx->frame_buf) {
58 SET_ERRNO(ERROR_MEMORY, "Failed to allocate frame buffer");
60 SAFE_FREE(ctx);
61 return NULL;
62 }
63
64 log_info("H.265 client context created: %ux%u", width, height);
65 return ctx;
66}
h265_encoder_t * h265_encoder_create(uint16_t initial_width, uint16_t initial_height)
Definition encoder.c:85
void h265_encoder_destroy(h265_encoder_t *encoder)
Definition encoder.c:178
unsigned int height
Definition fft.c:24
unsigned int width
Definition fft.c:24
#define SAFE_FREE(ptr)
#define SAFE_MALLOC(size, cast)
#define SAFE_CALLOC(count, size, cast)
unsigned char uint8_t
@ ERROR_MEMORY
Definition error_codes.h:55
@ ERROR_MEDIA_INIT
Definition error_codes.h:70
#define log_info(...)
Log an INFO message.
Definition log/log.h:578
struct h265_client_context h265_client_context_t

References h265_client_context::current_height, h265_client_context::current_width, h265_client_context::encoder, ERROR_MEDIA_INIT, ERROR_MEMORY, h265_client_context::frame_buf, h265_client_context::frame_buf_size, h265_encoder_create(), h265_encoder_destroy(), height, log_info, SAFE_CALLOC, SAFE_FREE, SAFE_MALLOC, SET_ERRNO, and width.

◆ h265_client_context_destroy()

void h265_client_context_destroy ( h265_client_context_t * ctx)

Destroy the client context and encoder

Parameters
ctxContext to destroy (may be NULL)

Definition at line 68 of file src/session/h265/client.c.

68 {
69 if (!ctx)
70 return;
71
72 if (ctx->encoder) {
74 }
75 if (ctx->frame_buf) {
76 SAFE_FREE(ctx->frame_buf);
77 }
78 if (ctx->source_location) {
80 }
81 SAFE_FREE(ctx);
82}

References h265_client_context::encoder, h265_client_context::frame_buf, h265_encoder_destroy(), SAFE_FREE, and h265_client_context::source_location.

◆ h265_client_get_stats()

void h265_client_get_stats ( h265_client_context_t * ctx,
uint64_t * total_frames,
uint64_t * keyframes,
uint32_t * avg_bitrate )

Get encoder statistics

Parameters
ctxClient context
total_framesOutput: total frames encoded
keyframesOutput: total keyframes encoded
avg_bitrateOutput: average bitrate in bits per second

Definition at line 145 of file src/session/h265/client.c.

145 {
146 if (!ctx || !ctx->encoder)
147 return;
148
149 h265_encoder_get_stats(ctx->encoder, total_frames, keyframes, avg_bitrate);
150}
void h265_encoder_get_stats(h265_encoder_t *encoder, uint64_t *total_frames, uint64_t *keyframes, uint32_t *avg_bitrate)
Definition encoder.c:530

References h265_client_context::encoder, and h265_encoder_get_stats().

◆ h265_client_init_media_source()

asciichat_error_t h265_client_init_media_source ( h265_client_context_t * ctx,
h265_media_source_t source_type,
const char * source_location )

Initialize media source for the client encoder This prepares the media pipeline (opens file, connects to stream, etc.)

Parameters
ctxClient context
source_typeType of media source (file, URL, webcam, test pattern)
source_locationFile path, URL, or device path (NULL for test pattern)
Returns
Error code (ASCIICHAT_OK on success)

Examples: h265_client_init_media_source(ctx, H265_SOURCE_FILE, "/path/to/video.mp4") h265_client_init_media_source(ctx, H265_SOURCE_URL, "https://www.youtube.com/watch?v=...") h265_client_init_media_source(ctx, H265_SOURCE_WEBCAM, "/dev/video0") h265_client_init_media_source(ctx, H265_SOURCE_TEST_PATTERN, NULL)

Definition at line 84 of file src/session/h265/client.c.

84 {
85 if (!ctx) {
86 return SET_ERRNO(ERROR_INTERNAL, "Invalid client context");
87 }
88
89 ctx->source_type = source_type;
90
91 if (source_location) {
92 size_t source_len = strlen(source_location) + 1;
93 ctx->source_location = SAFE_MALLOC(source_len, char *);
94 if (!ctx->source_location) {
95 return SET_ERRNO(ERROR_MEMORY, "Failed to allocate source location string");
96 }
97 asciichat_error_t strcpy_result = SAFE_STRCPY(ctx->source_location, source_len, source_location);
98 if (strcpy_result != ASCIICHAT_OK) {
99 log_error("Failed to copy source location string: %s", asciichat_error_format(NULL, strcpy_result));
101 return strcpy_result;
102 }
103 }
104
105 switch (source_type) {
107 log_info("H.265 client media source: test pattern");
108 break;
109 case H265_SOURCE_FILE:
110 log_info("H.265 client media source: file '%s'", source_location);
111 break;
112 case H265_SOURCE_URL:
113 log_info("H.265 client media source: URL '%s'", source_location);
114 break;
116 log_info("H.265 client media source: webcam '%s'", source_location);
117 break;
118 }
119
120 return ASCIICHAT_OK;
121}
const char * asciichat_error_format(const char *message, asciichat_error_t code)
#define SAFE_STRCPY(dest, dest_size, src)
@ ASCIICHAT_OK
Definition error_codes.h:50
#define log_error(...)
Log an ERROR message.
Definition log/log.h:604
@ H265_SOURCE_TEST_PATTERN
@ H265_SOURCE_WEBCAM
h265_media_source_t source_type

References asciichat_error_format(), ASCIICHAT_OK, ERROR_INTERNAL, ERROR_MEMORY, H265_SOURCE_FILE, H265_SOURCE_TEST_PATTERN, H265_SOURCE_URL, H265_SOURCE_WEBCAM, log_error, log_info, SAFE_FREE, SAFE_MALLOC, SAFE_STRCPY, SET_ERRNO, h265_client_context::source_location, and h265_client_context::source_type.

◆ h265_client_request_keyframe()

void h265_client_request_keyframe ( h265_client_context_t * ctx)

Request a keyframe on the next encode Used for error recovery and stream synchronization

Parameters
ctxClient context

Definition at line 139 of file src/session/h265/client.c.

139 {
140 if (ctx && ctx->encoder) {
142 }
143}
void h265_encoder_request_keyframe(h265_encoder_t *encoder)
Definition encoder.c:436

References h265_client_context::encoder, and h265_encoder_request_keyframe().