ascii-chat
Binary v0.15.83-53-gb711d8720
Library lib/v0.3.2-701-gb711d8720
Video chat in your terminal
Loading...
Searching...
No Matches
src/session/h265/client.c
Go to the documentation of this file.
1
8
9#include "client.h"
11#include <ascii-chat/common.h>
13#include <string.h>
14#include <stdlib.h>
15
29
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}
67
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}
83
85 const char *source_location) {
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}
123
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}
140
146
148 uint32_t *avg_bitrate) {
149 if (!ctx || !ctx->encoder)
150 return;
151
152 h265_encoder_get_stats(ctx->encoder, total_frames, keyframes, avg_bitrate);
153}
const char * asciichat_error_format(const char *message, asciichat_error_t code)
#define SET_ERRNO(code,...)
⚙️ Common definitions, error codes, macros, and types shared throughout the application
Named object registry for debugging — log identifiable resource names.
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
void h265_encoder_get_stats(h265_encoder_t *encoder, uint64_t *total_frames, uint64_t *keyframes, uint32_t *avg_bitrate)
Definition encoder.c:533
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
void h265_encoder_request_keyframe(h265_encoder_t *encoder)
Definition encoder.c:439
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.
unsigned int height
Definition fft.c:23
unsigned int width
Definition fft.c:23
unsigned short uint16_t
Definition common.h:61
unsigned int uint32_t
Definition common.h:62
#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
#define SAFE_STRCPY(dest, dest_size, src)
Definition common.h:486
unsigned long long uint64_t
Definition common.h:63
unsigned char uint8_t
Definition common.h:60
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:44
@ ERROR_MEMORY
Definition error_codes.h:51
@ ASCIICHAT_OK
Definition error_codes.h:46
@ ERROR_MEDIA_INIT
Definition error_codes.h:65
@ ERROR_INTERNAL
Definition error_codes.h:87
#define log_error(...)
Log an ERROR message.
Definition log/log.h:634
#define log_info(...)
Log an INFO message.
Definition log/log.h:608
void h265_client_request_keyframe(h265_client_context_t *ctx)
asciichat_error_t h265_client_capture_and_encode(h265_client_context_t *ctx, uint8_t *output_buf, size_t *output_size)
void h265_client_context_destroy(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)
asciichat_error_t h265_client_init_media_source(h265_client_context_t *ctx, h265_media_source_t source_type, const char *source_location)
h265_client_context_t * h265_client_context_create(uint16_t width, uint16_t height)
struct h265_client_context h265_client_context_t
Client-side H.265 media capture and encoding pipeline.
@ H265_SOURCE_TEST_PATTERN
@ H265_SOURCE_WEBCAM
🔤 String Manipulation and Shell Escaping Utilities
h265_media_source_t source_type