ascii-chat 0.11.33
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_context h265_client_context_t
 
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_context_t

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

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

117 {
118 if (!ctx || !output_buf || !output_size) {
119 return SET_ERRNO(ERROR_INTERNAL, "Invalid arguments to capture_and_encode");
120 }
121
122 if (!ctx->encoder) {
123 return SET_ERRNO(ERROR_INTERNAL, "Encoder not initialized");
124 }
125
126 memset(ctx->frame_buf, 128, ctx->frame_buf_size);
127
128 asciichat_error_t result =
129 h265_encode(ctx->encoder, ctx->current_width, ctx->current_height, ctx->frame_buf, output_buf, output_size);
130
131 return result;
132}
asciichat_error_t h265_encode(h265_encoder_t *encoder, uint16_t width, uint16_t height, const uint8_t *pixel_data, uint8_t *out_data, size_t *out_len)
#define SET_ERRNO(code, context_msg,...)
Set error code with custom context message and log it, returning the error code.
asciichat_error_t
Error and exit codes - unified status values (0-255)
Definition error_codes.h:49
@ ERROR_INTERNAL
Definition error_codes.h:92

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

27 {
28 if (width == 0 || height == 0) {
29 SET_ERRNO(ERROR_MEDIA_INIT, "Frame dimensions must be non-zero");
30 return NULL;
31 }
32
34 if (!ctx) {
35 SET_ERRNO(ERROR_MEMORY, "Failed to allocate client context");
36 return NULL;
37 }
38
39 ctx->encoder = h265_encoder_create(width, height);
40 if (!ctx->encoder) {
41 SET_ERRNO(ERROR_MEDIA_INIT, "Failed to create H.265 encoder");
42 SAFE_FREE(ctx);
43 return NULL;
44 }
45
46 ctx->current_width = width;
47 ctx->current_height = height;
48 ctx->frame_buf_size = width * height;
50 if (!ctx->frame_buf) {
51 SET_ERRNO(ERROR_MEMORY, "Failed to allocate frame buffer");
53 SAFE_FREE(ctx);
54 return NULL;
55 }
56
57 log_info("H.265 client context created: %ux%u", width, height);
58 return ctx;
59}
void h265_encoder_destroy(h265_encoder_t *encoder)
#define SAFE_FREE(ptr)
Definition common.h:376
#define SAFE_MALLOC(size, cast)
Definition common.h:264
#define SAFE_CALLOC(count, size, cast)
Definition common.h:274
unsigned char uint8_t
Definition common.h:56
@ ERROR_MEMORY
Definition error_codes.h:56
@ ERROR_MEDIA_INIT
Definition error_codes.h:70
#define log_info(...)
Log an INFO message.
Definition log/log.h:561
h265_encoder_t * h265_encoder_create(int width, int height, double fps)
Definition misc.c:105

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(), log_info, SAFE_CALLOC, SAFE_FREE, SAFE_MALLOC, and SET_ERRNO.

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

61 {
62 if (!ctx)
63 return;
64
65 if (ctx->encoder) {
67 }
68 if (ctx->frame_buf) {
69 SAFE_FREE(ctx->frame_buf);
70 }
71 if (ctx->source_location) {
73 }
74 SAFE_FREE(ctx);
75}

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

141 {
142 if (!ctx || !ctx->encoder)
143 return;
144
145 h265_encoder_get_stats(ctx->encoder, total_frames, keyframes, avg_bitrate);
146}
void h265_encoder_get_stats(h265_encoder_t *encoder, uint64_t *total_frames, uint64_t *keyframes, uint32_t *avg_bitrate)
Definition encoder.c:464

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

78 {
79 if (!ctx) {
80 return SET_ERRNO(ERROR_INTERNAL, "Invalid client context");
81 }
82
83 ctx->source_type = source_type;
84
85 if (source_location) {
86 size_t source_len = strlen(source_location) + 1;
87 ctx->source_location = SAFE_MALLOC(source_len, char *);
88 if (!ctx->source_location) {
89 return SET_ERRNO(ERROR_MEMORY, "Failed to allocate source location string");
90 }
91 asciichat_error_t strcpy_result = SAFE_STRCPY(ctx->source_location, source_len, source_location);
92 if (strcpy_result != ASCIICHAT_OK) {
93 log_error("Failed to copy source location string: %s", asciichat_error_string(strcpy_result));
95 return strcpy_result;
96 }
97 }
98
99 switch (source_type) {
101 log_info("H.265 client media source: test pattern");
102 break;
103 case H265_SOURCE_FILE:
104 log_info("H.265 client media source: file '%s'", source_location);
105 break;
106 case H265_SOURCE_URL:
107 log_info("H.265 client media source: URL '%s'", source_location);
108 break;
110 log_info("H.265 client media source: webcam '%s'", source_location);
111 break;
112 }
113
114 return ASCIICHAT_OK;
115}
#define SAFE_STRCPY(dest, dest_size, src)
Definition common.h:471
@ ASCIICHAT_OK
Definition error_codes.h:51
#define log_error(...)
Log an ERROR message.
Definition log/log.h:587
h265_media_source_t source_type

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

134 {
135 if (ctx && ctx->encoder) {
137 }
138}
void h265_encoder_request_keyframe(h265_encoder_t *encoder)

References h265_client_context::encoder, and h265_encoder_request_keyframe().