ascii-chat 0.11.33
Video chat in your terminal
Loading...
Searching...
No Matches
src/session/h265/server.c
Go to the documentation of this file.
1
6#include "server.h"
8#include <ascii-chat/common.h>
11#include <string.h>
12#include <stdlib.h>
13
21
26
29 if (!ctx) {
30 SET_ERRNO(ERROR_MEMORY, "Failed to allocate server context");
31 return NULL;
32 }
33
34 for (int i = 0; i < MAX_CLIENTS; i++) {
35 ctx->clients[i].in_use = false;
36 }
37 ctx->active_client_count = 0;
38
39 log_info("H.265 server context created (max %d clients)", MAX_CLIENTS);
40 return ctx;
41}
42
44 if (!ctx)
45 return;
46
47 for (int i = 0; i < MAX_CLIENTS; i++) {
48 if (ctx->clients[i].in_use && ctx->clients[i].decoder) {
50 }
51 }
52 SAFE_FREE(ctx);
53}
54
56 if (!ctx) {
57 SET_ERRNO(ERROR_INTERNAL, "Invalid server context");
58 return NULL;
59 }
60
61 for (int i = 0; i < MAX_CLIENTS; i++) {
62 if (ctx->clients[i].in_use && ctx->clients[i].client_id == client_id) {
63 return (h265_server_client_t *)&ctx->clients[i];
64 }
65 }
66
67 for (int i = 0; i < MAX_CLIENTS; i++) {
68 if (!ctx->clients[i].in_use) {
70 if (!decoder) {
71 SET_ERRNO(ERROR_MEDIA_INIT, "Failed to create decoder for client %u", client_id);
72 return NULL;
73 }
74
75 ctx->clients[i].client_id = client_id;
76 ctx->clients[i].decoder = decoder;
77 ctx->clients[i].in_use = true;
78 ctx->clients[i].last_width = 0;
79 ctx->clients[i].last_height = 0;
81
82 log_debug("Created H.265 decoder for client %u", client_id);
83 return (h265_server_client_t *)&ctx->clients[i];
84 }
85 }
86
87 SET_ERRNO(ERROR_INTERNAL, "No free decoder slots (max %d clients)", MAX_CLIENTS);
88 return NULL;
89}
90
92 size_t packet_size, uint8_t *output_rgba, uint16_t *output_width,
93 uint16_t *output_height, size_t *output_size) {
94 if (!client || !h265_packet || !output_rgba || !output_width || !output_height || !output_size) {
95 return SET_ERRNO(ERROR_INTERNAL, "Invalid arguments to decode_and_convert");
96 }
97
98 h265_client_decoder_t *client_dec = (h265_client_decoder_t *)client;
99 if (!client_dec->decoder || !client_dec->in_use) {
100 return SET_ERRNO(ERROR_INTERNAL, "Invalid client decoder state");
101 }
102
103 uint8_t ascii_buf[256 * 64];
104 size_t ascii_size = sizeof(ascii_buf);
105
106 asciichat_error_t result =
107 h265_decode(client_dec->decoder, h265_packet, packet_size, output_width, output_height, ascii_buf, &ascii_size);
108
109 if (result != ASCIICHAT_OK) {
110 return result;
111 }
112
113 size_t required_rgba_size = (size_t)(*output_width) * (*output_height) * 4;
114 if (*output_size < required_rgba_size) {
115 *output_size = required_rgba_size;
116 return SET_ERRNO(ERROR_NETWORK_SIZE, "Output buffer too small: need %zu, have %zu", required_rgba_size,
117 *output_size);
118 }
119
120 for (size_t i = 0; i < ascii_size && i < (size_t)(*output_width) * (*output_height); i++) {
121 uint8_t val = ascii_buf[i];
122 output_rgba[i * 4 + 0] = val;
123 output_rgba[i * 4 + 1] = val;
124 output_rgba[i * 4 + 2] = val;
125 output_rgba[i * 4 + 3] = 255;
126 }
127
128 *output_size = required_rgba_size;
129 client_dec->last_width = *output_width;
130 client_dec->last_height = *output_height;
131
132 return ASCIICHAT_OK;
133}
134
136 if (!ctx)
137 return;
138
139 for (int i = 0; i < MAX_CLIENTS; i++) {
140 if (ctx->clients[i].in_use && ctx->clients[i].client_id == client_id) {
141 if (ctx->clients[i].decoder) {
143 ctx->clients[i].decoder = NULL;
144 }
145 ctx->clients[i].in_use = false;
146 ctx->active_client_count--;
147 log_debug("Removed H.265 decoder for client %u", client_id);
148 return;
149 }
150 }
151}
152
154 uint16_t *last_width, uint16_t *last_height) {
155 if (!client)
156 return;
157
158 h265_client_decoder_t *client_dec = (h265_client_decoder_t *)client;
159 if (!client_dec->decoder)
160 return;
161
162 h265_decoder_get_stats(client_dec->decoder, total_frames, keyframes, last_width, last_height);
163}
⚙️ Common definitions, error codes, macros, and types shared throughout the application
Named object registry for debugging — log identifiable resource names.
void h265_decoder_destroy(h265_decoder_t *decoder)
Definition decoder.c:90
h265_decoder_t * h265_decoder_create(void)
Definition decoder.c:31
asciichat_error_t h265_decode(h265_decoder_t *decoder, const uint8_t *encoded_packet, size_t packet_size, uint16_t *output_width, uint16_t *output_height, uint8_t *output_buf, size_t *output_size)
Definition decoder.c:109
void h265_decoder_get_stats(h265_decoder_t *decoder, uint64_t *total_frames, uint64_t *keyframes, uint16_t *last_width, uint16_t *last_height)
Definition decoder.c:187
HEVC/H.265 decoder for ASCII art frames using libde265.
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_CALLOC(count, size, cast)
Definition common.h:274
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_NETWORK_SIZE
Definition error_codes.h:82
@ ERROR_MEDIA_INIT
Definition error_codes.h:70
@ ERROR_INTERNAL
Definition error_codes.h:92
#define MAX_CLIENTS
Maximum possible clients (static array size) - actual runtime limit set by –max-clients (1-32)
Definition limits.h:26
#define log_info(...)
Log an INFO message.
Definition log/log.h:561
#define log_debug(...)
Log a DEBUG message.
Definition log/log.h:548
Application limits and constraints.
void h265_server_client_get_stats(h265_server_client_t *client, uint64_t *total_frames, uint64_t *keyframes, uint16_t *last_width, uint16_t *last_height)
h265_server_context_t * h265_server_context_create(void)
void h265_server_context_destroy(h265_server_context_t *ctx)
struct h265_server_context h265_server_context_t
void h265_server_remove_client(h265_server_context_t *ctx, uint32_t client_id)
asciichat_error_t h265_server_decode_and_convert(h265_server_client_t *client, const uint8_t *h265_packet, size_t packet_size, uint8_t *output_rgba, uint16_t *output_width, uint16_t *output_height, size_t *output_size)
h265_server_client_t * h265_server_get_client_decoder(h265_server_context_t *ctx, uint32_t client_id)
Server-side H.265 decoding pipeline for multi-client streams.
struct h265_server_client h265_server_client_t
h265_client_decoder_t clients[MAX_CLIENTS]