ascii-chat
Binary v0.15.83-53-gb711d8720
Library lib/v0.3.2-701-gb711d8720
Video chat in your terminal
Loading...
Searching...
No Matches
client.h File Reference

Client-side H.265 media capture and encoding pipeline. More...

Go to the source code of this file.

Typedefs

typedef struct h265_client_encoder h265_client_encoder_t

Enumerations

enum  h265_media_source_t { H265_SOURCE_TEST_PATTERN = 0 , H265_SOURCE_FILE = 1 , H265_SOURCE_URL = 2 , H265_SOURCE_WEBCAM = 3 }

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

Client-side H.265 media capture and encoding pipeline.

The client encodes media from various sources into H.265 and sends encoded frames to the server:

  • Test pattern: Procedurally generated color grids
  • File: Local video files (MP4, MKV, AVI, etc.) via FFmpeg
  • URL: Remote streams (HTTP, RTSP, HLS, DASH, YouTube, etc.) via FFmpeg + yt-dlp
  • Webcam: Live camera input via PortAudio/libwebsockets

Each media source is decoded to RGBA, then:

  1. Converted to ASCII art via color-to-character mapping
  2. Encoded to H.265 with h265_encoder
  3. Sent to server in H.265 packets

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

Typedef Documentation

◆ h265_client_encoder_t

typedef struct h265_client_encoder h265_client_encoder_t

Definition at line 26 of file src/session/h265/client.h.

Enumeration Type Documentation

◆ h265_media_source_t

Media source type

Enumerator
H265_SOURCE_TEST_PATTERN 
H265_SOURCE_FILE 
H265_SOURCE_URL 
H265_SOURCE_WEBCAM 

Definition at line 31 of file src/session/h265/client.h.

31 {
32 H265_SOURCE_TEST_PATTERN = 0, // Procedural test pattern
33 H265_SOURCE_FILE = 1, // Local file
34 H265_SOURCE_URL = 2, // Remote URL (with yt-dlp support)
35 H265_SOURCE_WEBCAM = 3, // Live camera
@ H265_SOURCE_TEST_PATTERN
@ H265_SOURCE_WEBCAM

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 124 of file src/session/h265/client.c.

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

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:86
void h265_encoder_destroy(h265_encoder_t *encoder)
Definition encoder.c:179
unsigned int height
Definition fft.c:23
unsigned int width
Definition fft.c:23
#define SAFE_FREE(ptr)
Definition common.h:377
#define SAFE_MALLOC(size, cast)
Definition common.h:265
#define SAFE_CALLOC(count, size, cast)
Definition common.h:275
unsigned char uint8_t
Definition common.h:60
@ ERROR_MEMORY
Definition error_codes.h:51
@ ERROR_MEDIA_INIT
Definition error_codes.h:65
#define log_info(...)
Log an INFO message.
Definition log/log.h:608
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 147 of file src/session/h265/client.c.

148 {
149 if (!ctx || !ctx->encoder)
150 return;
151
152 h265_encoder_get_stats(ctx->encoder, total_frames, keyframes, avg_bitrate);
153}
void h265_encoder_get_stats(h265_encoder_t *encoder, uint64_t *total_frames, uint64_t *keyframes, uint32_t *avg_bitrate)
Definition encoder.c:533

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.

85 {
86 if (!ctx) {
87 return SET_ERRNO(ERROR_INTERNAL, "Invalid client context");
88 }
89
90 ctx->source_type = source_type;
91
92 if (source_location) {
93 size_t source_len = strlen(source_location) + 1;
94 ctx->source_location = SAFE_MALLOC(source_len, char *);
95 if (!ctx->source_location) {
96 return SET_ERRNO(ERROR_MEMORY, "Failed to allocate source location string");
97 }
98 asciichat_error_t strcpy_result = SAFE_STRCPY(ctx->source_location, source_len, source_location);
99 if (strcpy_result != ASCIICHAT_OK) {
100 log_error("Failed to copy source location string: %s", asciichat_error_format(NULL, strcpy_result));
102 return strcpy_result;
103 }
104 }
105
106 switch (source_type) {
108 log_info("H.265 client media source: test pattern");
109 break;
110 case H265_SOURCE_FILE:
111 log_info("H.265 client media source: file '%s'", source_location);
112 break;
113 case H265_SOURCE_URL:
114 log_info("H.265 client media source: URL '%s'", source_location);
115 break;
117 log_info("H.265 client media source: webcam '%s'", source_location);
118 break;
119 }
120
121 return ASCIICHAT_OK;
122}
const char * asciichat_error_format(const char *message, asciichat_error_t code)
#define SAFE_STRCPY(dest, dest_size, src)
Definition common.h:486
@ ASCIICHAT_OK
Definition error_codes.h:46
#define log_error(...)
Log an ERROR message.
Definition log/log.h:634
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 141 of file src/session/h265/client.c.

141 {
142 if (ctx && ctx->encoder) {
144 }
145}
void h265_encoder_request_keyframe(h265_encoder_t *encoder)
Definition encoder.c:439

References h265_client_context::encoder, and h265_encoder_request_keyframe().