ascii-chat
Binary v0.16.50-198-g78f51665f
Library lib/v0.3.2-2240-g78f51665f
Video chat in your terminal
Loading...
Searching...
No Matches
media.c
Go to the documentation of this file.
1
12
13#include <math.h>
14#include <options/registry/common.h>
15#include <options/registry/metadata.h>
17
18#include <options/parsers.h>
19#include <libplatform/memory.h>
20#include <string.h>
21#include <libflags/flags.h>
22
23// ============================================================================
24// Validators
25// ============================================================================
26
27#ifndef __EMSCRIPTEN__
28static const bool default_render_kitty_value = false;
29
30static bool validate_kitty_output(const void *data) {
31 const options_t *opts = data;
32 bool kitty = opts->render_kitty || strcmp(opts->render_file, "--kitty") == 0;
33 static const flags_option_spec_t render_file_schema = {.name = "render-file", .type = FLAGS_TYPE_FILE, .dash_behavior = FLAGS_DASH_STDOUT};
34 flags_stream_value_t render_stream = {0};
35 const bool renders_stdout = flags_classify_stream(&render_file_schema, opts->render_file, &render_stream) && render_stream.kind == FLAGS_STREAM_STDOUT;
36 if (kitty && !opts->snapshot_mode) {
37 SET_ERRNO(ERROR_USAGE, "Kitty graphics output requires --snapshot");
38 return false;
39 }
40 if (opts->render_kitty && (renders_stdout || strcmp(opts->render_file, "pipe:") == 0)) {
41 SET_ERRNO(ERROR_USAGE, "Kitty graphics and binary media cannot share stdout");
42 return false;
43 }
44 return true;
45}
46#else
47#define validate_kitty_output NULL
48#endif
49
50static bool validate_video_codec(const void *opts_struct) {
51 const options_t *opts = (const options_t *)opts_struct;
52 if (strcmp(opts->video_codec, "auto") == 0 || strcmp(opts->video_codec, "hevc") == 0 || strcmp(opts->video_codec, "h264") == 0 || strcmp(opts->video_codec, "raw") == 0)
53 return true;
54 SET_ERRNO(ERROR_USAGE, "--video-codec must be auto, hevc, h264, or raw");
55 return false;
56}
57
58static bool validate_render_font_size(const void *opts_struct) {
59 (void)opts_struct;
60 const options_t *opts = (const options_t *)opts_struct;
61 if (!isfinite(opts->render_font_size) || opts->render_font_size < 0.0) {
62 SET_ERRNO(ERROR_USAGE, "--render-font-size must be 0 (auto) or a positive finite size");
63 return false;
64 }
65 return true;
66}
67
68// CLI arguments append to one ordered list. Environment/config strings retain
69// their ordinary precedence and are used as a single-item fallback at capture.
70static bool parse_media_argument(const char *arg, void *field, bool url, char **error) {
71 options_t *opts = (options_t *)((char *)field - (url ? offsetof(options_t, media_url) : offsetof(options_t, media_file)));
73 if (result != ASCIICHAT_OK) {
74 if (error)
75 *error = platform_strdup("Invalid media artifact or playlist limit exceeded");
76 return false;
77 }
78 if (opts->media_playlist.count == 1) {
79 opts->media_file[0] = opts->media_url[0] = '\0';
81 }
82 return true;
83}
84static bool parse_media_file(const char *arg, void *field, char **error) {
85 return parse_media_argument(arg, field, false, error);
86}
87static bool parse_media_url(const char *arg, void *field, char **error) {
88 return parse_media_argument(arg, field, true, error);
89}
90
91// ============================================================================
92// MEDIA CATEGORY - Media file and stream options
93// ============================================================================
94// Media File Streaming Options
96 'f',
98 offsetof(options_t, media_file),
99 "",
100 0,
101 "Append a media file to the playback playlist (repeat --file/--url in order). Stream from media file or stdin "
102 "(use '-' for stdin). Supported formats: see man ffmpeg-formats; codecs: see man "
103 "ffmpeg-codecs.",
104 "MEDIA",
105 NULL,
106 false,
107 "ASCII_CHAT_FILE",
108 NULL,
109 parse_media_file,
110 false,
111 false,
113 {.input_type = OPTION_INPUT_FILEPATH},
114 NULL},
115 {"url",
116 'u',
118 offsetof(options_t, media_url),
119 "",
120 0,
121 "Append a network URL to the playback playlist (repeat --file/--url in order). Direct HTTP/HTTPS/RTSP streams use "
122 "FFmpeg; complex sites (YouTube, TikTok, etc.) use "
123 "yt-dlp. Supported formats: see man ffmpeg-formats; codecs: see man ffmpeg-codecs; sites: yt-dlp "
124 "https://github.com/yt-dlp/yt-dlp/blob/master/README.md#supported-sites.",
125 "MEDIA",
126 NULL,
127 false,
128 "ASCII_CHAT_URL",
129 NULL,
130 parse_media_url,
131 false,
132 false,
134 {0},
135 NULL},
136 {"loop",
137 'l',
139 offsetof(options_t, media_loop),
140 &default_media_loop_value,
141 sizeof(bool),
142 "Loop single media or repeat the complete playlist.",
143 "MEDIA",
144 NULL,
145 false,
146 "ASCII_CHAT_LOOP",
147 NULL,
148 NULL,
149 false,
150 false,
152 {0},
153 NULL},
154 {"pause",
155 '\0',
157 offsetof(options_t, pause),
158 &default_pause_value,
159 sizeof(bool),
160 "Start playback paused (toggle with spacebar, requires --file or --url).",
161 "MEDIA",
162 NULL,
163 false,
164 "ASCII_CHAT_PAUSE",
165 NULL,
166 NULL,
167 false,
168 false,
170 {0},
171 NULL},
172 {"seek",
173 's',
175 offsetof(options_t, media_seek_timestamp),
176 &default_media_seek_value,
177 sizeof(double),
178 "Seek to timestamp before playback (format: seconds, MM:SS, or HH:MM:SS.ms).",
179 "MEDIA",
180 NULL,
181 false,
182 "ASCII_CHAT_SEEK",
183 NULL,
185 false,
186 false,
188 {.examples = g_seek_examples, .input_type = OPTION_INPUT_STRING},
189 NULL},
190 {"yt-dlp-options",
191 '\0',
193 offsetof(options_t, yt_dlp_options),
194 NULL,
195 sizeof(((options_t *)0)->yt_dlp_options),
196 "Arbitrary yt-dlp options passed to the extraction subprocess for URL resolution. "
197 "Examples: --no-warnings or --proxy socks5://127.0.0.1:1080 or --cookies-from-browser=firefox.",
198 "MEDIA",
199 NULL,
200 false,
201 "ASCII_CHAT_YT_DLP_OPTIONS",
202 NULL,
203 NULL,
204 false,
205 false,
207 {0},
208 NULL},
209 {"video-codec",
210 '\0',
212 offsetof(options_t, video_codec),
213 "auto",
214 sizeof(((options_t *)0)->video_codec),
215 "Video codec: auto (default) tries HEVC, H.264, then raw RGB. Explicit hevc, h264, or raw never falls back.",
216 "MEDIA",
217 "CODEC",
218 false,
219 "ASCII_CHAT_VIDEO_CODEC",
220 validate_video_codec,
221 NULL,
222 false,
223 false,
225 {0},
226 NULL},
227
228#define RENDER_FILE_MODES (OPTION_MODE_CLIENT | OPTION_MODE_MIRROR | OPTION_MODE_DISCOVERY)
229
230#ifndef __EMSCRIPTEN__
231 {"render-kitty",
232 '\0',
234 offsetof(options_t, render_kitty),
235 &default_render_kitty_value,
236 sizeof(bool),
237 "Display a snapshot using Kitty graphics (requires --snapshot). Combine with --render-file to save it.",
238 "MEDIA",
239 NULL,
240 false,
241 "ASCII_CHAT_RENDER_KITTY",
242 validate_kitty_output,
243 NULL,
244 false,
245 false,
247 {0},
248 NULL},
249#endif
250
251 {"render-file",
252 '\0',
254 offsetof(options_t, render_file),
255 "",
256 sizeof(((options_t *)0)->render_file),
257 "Render ASCII frames to stdout or video/image file. Use '-' for stdout; extension determines format: "
258 ".mp4, .mov, .webm, .avi, .gif, .png, .jpg. Named files retain ASCII on stdout and show terminal "
259 "progress while finishing; redirect stdout to hide it.",
260 "MEDIA",
261 "PATH",
262 false,
263 "ASCII_CHAT_RENDER_FILE",
264 validate_kitty_output,
265 NULL,
266 false,
267 false,
269 {.input_type = OPTION_INPUT_FILEPATH},
270 NULL},
271
272 {"render-theme",
273 '\0',
275 offsetof(options_t, render_theme),
276 &default_render_theme_value,
277 sizeof(int),
278 "Terminal color scheme for rendered output: dark, light, auto.",
279 "MEDIA",
280 "THEME",
281 false,
282 "ASCII_CHAT_RENDER_THEME",
283 NULL,
285 false,
286 false,
288 {.enum_values = g_render_theme_values, .enum_descriptions = g_render_theme_descs, .input_type = OPTION_INPUT_ENUM},
289 NULL},
290
291 {"render-font",
292 '\0',
294 offsetof(options_t, render_font),
295 "",
296 sizeof(((options_t *)0)->render_font),
297 "Font for render-file output: bundled names (matrix or default), system font names, or absolute .ttf/.otf path. "
298 "Default: default (bundled DejaVu Sans Mono). "
299 "Examples: matrix, default, JetBrains Mono, Nerd Font Mono, /path/to/font.ttf.",
300 "MEDIA",
301 "FONT",
302 false,
303 "ASCII_CHAT_RENDER_FONT",
304 NULL,
305 NULL,
306 false,
307 false,
309 {.input_type = OPTION_INPUT_FILEPATH},
310 NULL},
311
312 {"render-font-size",
313 '\0',
315 offsetof(options_t, render_font_size),
316 &default_render_font_size_value,
317 sizeof(double),
318 "Font size in points for render-file output (default: 0, auto-scales video; positive fractional sizes supported).",
319 "MEDIA",
320 "SIZE",
321 false,
322 "ASCII_CHAT_RENDER_FONT_SIZE",
323 validate_render_font_size,
324 NULL,
325 false,
326 false,
328 {.input_type = OPTION_INPUT_NUMERIC},
329 NULL},
330
#define SET_ERRNO(code,...)
@ OPTION_INPUT_STRING
Free-form text input.
Definition builder.h:189
@ OPTION_INPUT_NUMERIC
Numeric value with optional min/max/step.
Definition builder.h:188
@ OPTION_INPUT_FILEPATH
File path completion.
Definition builder.h:190
@ OPTION_INPUT_ENUM
Choose from fixed set of enum values.
Definition builder.h:187
@ OPTION_TYPE_DOUBLE
Floating point (–ratio 1.5).
Definition builder.h:174
@ OPTION_TYPE_STRING
String value (–name foo).
Definition builder.h:173
@ OPTION_TYPE_CALLBACK
Custom parser function.
Definition builder.h:175
@ OPTION_TYPE_BOOL
Boolean flag (–flag, no value).
Definition builder.h:171
#define SAFE_STRNCPY(dst, src, size)
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:48
@ ASCIICHAT_OK
Definition error_codes.h:50
@ ERROR_USAGE
Definition error_codes.h:52
struct options_state options_t
Immutable, RCU-published snapshot of the application's runtime configuration.
#define OPTIONS_BUFF_SIZE
Buffer size for option string values.
Definition options.h:305
@ OPTION_MODE_CLIENT
Client mode (bit 1).
Definition options.h:936
@ OPTION_MODE_DISCOVERY
Discovery mode (bit 4).
Definition options.h:939
@ OPTION_MODE_MIRROR
Mirror mode (bit 2).
Definition options.h:937
const registry_entry_t g_media_entries[]
Definition media.c:95
#define REGISTRY_TERMINATOR()
Initialize a terminator entry (sentinel value for array end) Uses designated initializers to properly...
🔢 Mathematical Utility Functions
#define RENDER_FILE_MODES
const char * g_render_theme_values[]
Definition metadata.c:111
const char * g_seek_examples[]
Definition metadata.c:151
const char * g_render_theme_descs[]
Definition metadata.c:113
bool parse_timestamp(const char *arg, void *dest, char **error_msg)
Custom parser for –seek flag.
Definition parsers.c:938
bool parse_render_theme(const char *arg, void *dest, char **error_msg)
Parse render theme option for pixel rendering.
Definition parsers.c:282
Custom option parsers for enum types.
asciichat_error_t media_playlist_append(media_playlist_t *list, const char *path, bool url)
Definition playlist.c:7
char * platform_strdup(const char *s)
🔤 String Manipulation and Shell Escaping Utilities
char paths[64][256]
Definition playlist.h:14
bool snapshot_mode
Snapshot mode (one frame and exit).
Definition options.h:1142
char media_url[256]
Network URL (HTTP/HTTPS/YouTube/RTSP) - takes priority over media_file.
Definition options.h:1072
bool render_kitty
Display a snapshot with Kitty graphics.
Definition options.h:1251
media_playlist_t media_playlist
Ordered CLI artifacts shared with live media input.
Definition options.h:1070
char render_file[256]
Output file path (e.g. output.mp4).
Definition options.h:1249
char video_codec[64]
Video codec (auto, hevc, h264, raw) - default: auto.
Definition options.h:1135
char media_file[256]
Media file path or "-" for stdin.
Definition options.h:1071
double render_font_size
Font size in points (0 selects an automatic size).
Definition options.h:1255
Registry entry - stores option definition with mode bitmask and metadata.