ascii-chat
Binary v0.16.114-28-gf5913657f
Library lib/v0.3.2-3302-gf5913657f
Video chat in your terminal
Loading...
Searching...
No Matches
wasm/stubs/ffmpeg_decoder.c
Go to the documentation of this file.
1
11
12// clang-format off
13
14#ifdef __EMSCRIPTEN__
15
16#include <emscripten.h>
20#include <ascii-chat/log/log.h>
21#include <stdlib.h>
22#include <string.h>
23
24/* Full struct definition for the opaque ffmpeg_decoder_t */
25struct ffmpeg_decoder_t {
26 int decoder_id; // ffmpeg.wasm decoder instance ID
27 image_t *cached_frame; // Cached RGB frame for reuse
28 bool at_end; // EOF flag
29 double position; // Current playback position in seconds
30};
31
32/* ============================================================================
33 * JavaScript Bridges (EM_JS)
34 * ============================================================================ */
35
40EM_JS(int, js_ffmpeg_init, (), {
41 if (typeof Module !== 'undefined' && Module.FFmpeg) {
42 console.log('[WASM] ffmpeg.wasm already initialized');
43 return 1;
44 }
45 console.log('[WASM] Initializing ffmpeg.wasm');
46 // ffmpeg.wasm must be loaded by JavaScript before calling decoder functions
47 return 0; // Will be initialized by JavaScript
48});
49
54EM_JS(int, js_ffmpeg_decoder_create, (const char *path_cstr), {
55 if (!Module.FFmpeg || !Module.FFmpeg.isLoaded()) {
56 console.error('[WASM] ffmpeg.wasm not loaded');
57 return 0;
58 }
59
60 const path = UTF8ToString(path_cstr);
61 console.log('[WASM] Creating decoder for: ' + path);
62
63 try {
64 // This would be implemented by JavaScript ffmpeg.wasm wrapper
65 // Returns a decoder ID for future operations
66 if (typeof Module.ffmpegDecoderCreate === 'function') {
67 return Module.ffmpegDecoderCreate(path);
68 }
69 return 0;
70 } catch (err) {
71 console.error('[WASM] Decoder creation failed:', err);
72 return 0;
73 }
74});
75
79EM_JS(void, js_ffmpeg_decoder_destroy, (int decoder_id), {
80 if (!Module.FFmpeg || !Module.FFmpeg.isLoaded()) {
81 return;
82 }
83
84 try {
85 if (typeof Module.ffmpegDecoderDestroy === 'function') {
86 Module.ffmpegDecoderDestroy(decoder_id);
87 }
88 } catch (err) {
89 console.error('[WASM] Decoder destruction failed:', err);
90 }
91});
92
97EM_JS(int, js_ffmpeg_decoder_read_frame, (int decoder_id, uint8_t *out_rgba, int width, int height), {
98 if (!Module.FFmpeg || !Module.FFmpeg.isLoaded()) {
99 return 0;
100 }
101
102 try {
103 if (typeof Module.ffmpegDecoderReadFrame === 'function') {
104 const rgba_data = Module.ffmpegDecoderReadFrame(decoder_id, width, height);
105 if (rgba_data) {
106 // Copy RGBA data to WASM heap
107 const byteOffset = out_rgba;
108 for (let i = 0; i < rgba_data.length; i++) {
109 HEAPU8[byteOffset + i] = rgba_data[i];
110 }
111 return 1;
112 }
113 }
114 return 0;
115 } catch (err) {
116 console.error('[WASM] Frame read failed:', err);
117 return 0;
118 }
119});
120
124EM_JS(int, js_ffmpeg_decoder_at_end, (int decoder_id), {
125 try {
126 if (typeof Module.ffmpegDecoderAtEnd === 'function') {
127 return Module.ffmpegDecoderAtEnd(decoder_id) ? 1 : 0;
128 }
129 } catch (err) {
130 console.error('[WASM] at_end check failed:', err);
131 }
132 return 1;
133});
134
138EM_JS(int, js_ffmpeg_decoder_rewind, (int decoder_id), {
139 try {
140 if (typeof Module.ffmpegDecoderRewind === 'function') {
141 return Module.ffmpegDecoderRewind(decoder_id) ? 1 : 0;
142 }
143 } catch (err) {
144 console.error('[WASM] Rewind failed:', err);
145 }
146 return 0;
147});
148
152EM_JS(int, js_ffmpeg_decoder_has_audio, (int decoder_id), {
153 try {
154 if (typeof Module.ffmpegDecoderHasAudio === 'function') {
155 return Module.ffmpegDecoderHasAudio(decoder_id) ? 1 : 0;
156 }
157 } catch (err) {
158 console.error('[WASM] has_audio check failed:', err);
159 }
160 return 0;
161});
162
166EM_JS(double, js_ffmpeg_decoder_get_position, (int decoder_id), {
167 try {
168 if (typeof Module.ffmpegDecoderGetPosition === 'function') {
169 return Module.ffmpegDecoderGetPosition(decoder_id);
170 }
171 } catch (err) {
172 console.error('[WASM] get_position failed:', err);
173 }
174 return 0.0;
175});
176
177/* ============================================================================
178 * Public API Implementation
179 * ============================================================================ */
180
181ffmpeg_decoder_t *ffmpeg_decoder_create(const char *path) {
182 if (!path) {
183 return SET_ERRNO(ERROR_INVALID_PARAM, "Path must not be NULL"), NULL;
184 }
185
186 int decoder_id = js_ffmpeg_decoder_create(path);
187 if (decoder_id <= 0) {
188 return SET_ERRNO(ERROR_MEDIA_OPEN, "Failed to create ffmpeg decoder"), NULL;
189 }
190
191 ffmpeg_decoder_t *decoder = malloc(sizeof(ffmpeg_decoder_t));
192 if (!decoder) {
193 js_ffmpeg_decoder_destroy(decoder_id);
194 return SET_ERRNO(ERROR_MEMORY, "Failed to allocate decoder"), NULL;
195 }
196
197 decoder->decoder_id = decoder_id;
198 decoder->cached_frame = NULL;
199 decoder->at_end = false;
200 decoder->position = 0.0;
201
202 log_debug("WASM FFmpeg decoder created for: %s", path);
203 return decoder;
204}
205
206ffmpeg_decoder_t *ffmpeg_decoder_create_interruptible(const char *path, bool (*cancel)(void *), void *data) {
207 if (cancel && cancel(data)) return NULL;
209 if (decoder) ffmpeg_decoder_set_exit_callback(decoder, cancel, data);
210 return decoder;
211}
212
214 return SET_ERRNO(ERROR_NOT_SUPPORTED, "stdin decoder not supported in WASM"), NULL;
215}
216
218 if (!decoder)
219 return;
220
221 js_ffmpeg_decoder_destroy(decoder->decoder_id);
222
223 if (decoder->cached_frame) {
224 image_destroy(decoder->cached_frame);
225 }
226
227 free(decoder);
228 log_debug("WASM FFmpeg decoder destroyed");
229}
230
232 if (!decoder) {
233 return SET_ERRNO(ERROR_INVALID_PARAM, "Decoder must not be NULL");
234 }
235 // No prefetching in WASM - frames are read on-demand
236 return ASCIICHAT_OK;
237}
238
240 (void)decoder;
241}
242
243void ffmpeg_decoder_set_exit_callback(ffmpeg_decoder_t *decoder, bool (*should_exit)(void *user_data),
244 void *user_data) {
245 (void)decoder;
246 (void)should_exit;
247 (void)user_data;
248}
249
251 if (!decoder)
252 return NULL;
253
254 if (decoder->at_end)
255 return NULL;
256
257 // Allocate cached frame if not already done
258 if (!decoder->cached_frame) {
259 // TODO: Get actual dimensions from decoder
260 decoder->cached_frame = image_new(1920, 1080);
261 if (!decoder->cached_frame) {
262 return NULL;
263 }
264 }
265
266 // Allocate RGBA staging buffer
267 int width = decoder->cached_frame->w;
268 int height = decoder->cached_frame->h;
269 uint8_t *rgba_buf = malloc(width * height * 4);
270 if (!rgba_buf) {
271 return NULL;
272 }
273
274 // Read frame from ffmpeg.wasm
275 int result = js_ffmpeg_decoder_read_frame(decoder->decoder_id, rgba_buf, width, height);
276 if (result <= 0) {
277 decoder->at_end = true;
278 free(rgba_buf);
279 return NULL;
280 }
281
282 // Convert RGBA to RGB24
283 for (int i = 0; i < width * height; i++) {
284 decoder->cached_frame->pixels[i].r = rgba_buf[i * 4 + 0];
285 decoder->cached_frame->pixels[i].g = rgba_buf[i * 4 + 1];
286 decoder->cached_frame->pixels[i].b = rgba_buf[i * 4 + 2];
287 }
288
289 free(rgba_buf);
290 decoder->position = js_ffmpeg_decoder_get_position(decoder->decoder_id);
291 return decoder->cached_frame;
292}
293
295 if (!decoder)
296 return true;
297
298 if (decoder->at_end)
299 return true;
300
301 int result = js_ffmpeg_decoder_at_end(decoder->decoder_id);
302 decoder->at_end = (result != 0);
303 return decoder->at_end;
304}
305
307 if (!decoder) {
308 return SET_ERRNO(ERROR_INVALID_PARAM, "Decoder must not be NULL");
309 }
310
311 int result = js_ffmpeg_decoder_rewind(decoder->decoder_id);
312 if (result <= 0) {
313 return SET_ERRNO(ERROR_MEDIA_SEEK, "Failed to rewind decoder");
314 }
315
316 decoder->at_end = false;
317 decoder->position = 0.0;
318 return ASCIICHAT_OK;
319}
320
322 if (!decoder)
323 return false;
324
325 int result = js_ffmpeg_decoder_has_audio(decoder->decoder_id);
326 return (result != 0);
327}
328
330 if (!decoder)
331 return false;
332 return true; // Assume video if decoder was created
333}
334
336 if (!decoder)
337 return 0.0;
338
339 return js_ffmpeg_decoder_get_position(decoder->decoder_id);
340}
341
342size_t ffmpeg_decoder_read_audio_samples(ffmpeg_decoder_t *decoder, float *buffer, size_t num_samples) {
343 (void)decoder;
344 (void)buffer;
345 (void)num_samples;
346 // Audio decoding not yet implemented in ffmpeg.wasm bridge
347 return 0;
348}
349
351 (void)decoder;
352 (void)timestamp;
353 return SET_ERRNO(ERROR_MEDIA_SEEK, "Seeking not yet implemented in ffmpeg.wasm");
354}
355
357 (void)decoder;
358 return 0.0;
359}
360
362 (void)decoder;
363 return 24.0; // Default FPS
364}
365
366#endif // __EMSCRIPTEN__
367
368// clang-format on
Synchronized per-thread pending error stacks and value snapshots. SET_ERRNO pushes,...
#define SET_ERRNO(code,...)
🎞️ FFmpeg-based media decoder for video and audio streams
unsigned int height
Definition fft.c:24
unsigned int width
Definition fft.c:24
unsigned char uint8_t
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:48
@ ERROR_MEDIA_SEEK
Definition error_codes.h:73
@ ERROR_MEMORY
Definition error_codes.h:55
@ ASCIICHAT_OK
Definition error_codes.h:50
@ ERROR_INVALID_PARAM
@ ERROR_MEDIA_OPEN
Definition error_codes.h:71
@ ERROR_NOT_SUPPORTED
Definition error_codes.h:74
#define log_debug(...)
Log a DEBUG message.
Definition log/log.h:565
bool ffmpeg_decoder_at_end(ffmpeg_decoder_t *decoder)
Check if decoder reached end of stream.
double ffmpeg_decoder_get_position(ffmpeg_decoder_t *decoder)
Get current playback position in seconds.
void ffmpeg_decoder_stop_prefetch(ffmpeg_decoder_t *decoder)
Stop the background frame prefetching thread.
image_t * ffmpeg_decoder_read_video_frame(ffmpeg_decoder_t *decoder)
bool ffmpeg_decoder_has_video(ffmpeg_decoder_t *decoder)
Check if decoder has video stream.
double ffmpeg_decoder_get_video_fps(ffmpeg_decoder_t *decoder)
Get video frame rate.
asciichat_error_t ffmpeg_decoder_rewind(ffmpeg_decoder_t *decoder)
Seek to beginning of media.
bool ffmpeg_decoder_has_audio(ffmpeg_decoder_t *decoder)
Check if decoder has audio stream.
asciichat_error_t ffmpeg_decoder_start_prefetch(ffmpeg_decoder_t *decoder)
Start the background frame prefetching thread.
asciichat_error_t ffmpeg_decoder_seek_to_timestamp(ffmpeg_decoder_t *decoder, double timestamp_sec)
Seek to specific timestamp in media.
void ffmpeg_decoder_set_exit_callback(ffmpeg_decoder_t *decoder, bool(*should_exit_callback)(void *), void *user_data)
Set exit signal callback for graceful shutdown during I/O.
void ffmpeg_decoder_destroy(ffmpeg_decoder_t *decoder)
Destroy FFmpeg decoder and free resources.
ffmpeg_decoder_t * ffmpeg_decoder_create(const char *path)
Create FFmpeg decoder from file path.
size_t ffmpeg_decoder_read_audio_samples(ffmpeg_decoder_t *decoder, float *buffer, size_t num_samples)
Decode audio samples.
ffmpeg_decoder_t * ffmpeg_decoder_create_stdin(void)
Create FFmpeg decoder from stdin.
double ffmpeg_decoder_get_duration(ffmpeg_decoder_t *decoder)
Get media duration in seconds.
void image_destroy(image_t *image)
Destroy an image allocated with image_new().
image_t * image_new(size_t width, size_t height)
Create a new image with standard allocation.
📝 Logging API with multiple log levels and terminal output control
bool should_exit(void)
Definition main.c:260
ffmpeg_decoder_t * ffmpeg_decoder_create_interruptible(const char *path, bool(*cancel)(void *), void *data)
🔤 String Manipulation and Shell Escaping Utilities
FFmpeg decoder state for video and audio decoding.
EM_JS(void, js_console_log,(int level, const char *message), { const msg=UTF8ToString(message);switch(level) { case 0:case 1:case 2:console.log(msg);break;case 3:console.warn(msg);break;case 4:case 5:console.error(msg);break;default:console.log(msg);} })