ascii-chat 0.11.33
Video chat in your terminal
Loading...
Searching...
No Matches
src/session/h265/client.c
Go to the documentation of this file.
1
6#include "client.h"
8#include <ascii-chat/common.h>
10#include <string.h>
11#include <stdlib.h>
12
26
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}
60
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}
76
78 const char *source_location) {
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}
116
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}
133
139
141 uint32_t *avg_bitrate) {
142 if (!ctx || !ctx->encoder)
143 return;
144
145 h265_encoder_get_stats(ctx->encoder, total_frames, keyframes, avg_bitrate);
146}
⚙️ Common definitions, error codes, macros, and types shared throughout the application
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)
void h265_encoder_t
void h265_encoder_destroy(h265_encoder_t *encoder)
void h265_encoder_request_keyframe(h265_encoder_t *encoder)
Named object registry for debugging — log identifiable resource names.
void h265_encoder_get_stats(h265_encoder_t *encoder, uint64_t *total_frames, uint64_t *keyframes, uint32_t *avg_bitrate)
Definition encoder.c:464
HEVC/H.265 encoder for ASCII art frames using libx265.
unsigned short uint16_t
Definition common.h:57
unsigned int uint32_t
Definition common.h:58
#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
#define SAFE_STRCPY(dest, dest_size, src)
Definition common.h:471
unsigned long long uint64_t
Definition common.h:59
unsigned char uint8_t
Definition common.h:56
#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_MEMORY
Definition error_codes.h:56
@ ASCIICHAT_OK
Definition error_codes.h:51
@ ERROR_MEDIA_INIT
Definition error_codes.h:70
@ ERROR_INTERNAL
Definition error_codes.h:92
#define log_error(...)
Log an ERROR message.
Definition log/log.h:587
#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
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
h265_media_source_t source_type