--- a/fftools/ffmpeg.c +++ b/fftools/ffmpeg.c @@ -148,13 +148,26 @@ received_nb_signals++; term_exit_sigsafe(); if(received_nb_signals > 3) { +#ifndef MEDIABRIDGE_IN_PROCESS ret = write(2/*STDERR_FILENO*/, "Received > 3 system signals, hard exiting\n", strlen("Received > 3 system signals, hard exiting\n")); if (ret < 0) { /* Do nothing */ }; exit(123); +#else + (void)ret; /* in-process: never exit() the host app */ +#endif } } +#ifdef MEDIABRIDGE_IN_PROCESS +/* Cooperative cancel for the in-process wrapper: identical effect to Ctrl-C. + * Safe to call from any thread; decode_interrupt_cb picks the flags up. */ +void mediabridge_ffmpeg_cancel(void) +{ + sigterm_handler(SIGTERM); +} +#endif + #if HAVE_SETCONSOLECTRLHANDLER static BOOL WINAPI CtrlHandler(DWORD fdwCtrlType) { @@ -200,6 +213,11 @@ void term_init(void) { +#ifdef MEDIABRIDGE_IN_PROCESS + /* In-process: leave the host app's signal handlers and tty alone. + * Cancellation goes through mediabridge_ffmpeg_cancel() instead. */ + return; +#endif #if defined __linux__ struct sigaction action = {0}; action.sa_handler = sigterm_handler; @@ -944,13 +962,34 @@ #endif } +#ifdef MEDIABRIDGE_IN_PROCESS +int mediabridge_ffmpeg_main(int argc, char **argv) +#else int main(int argc, char **argv) +#endif { Scheduler *sch = NULL; int ret; BenchmarkTimeStamps ti; +#ifdef MEDIABRIDGE_IN_PROCESS + /* Re-entry hygiene: these file statics survive across runs in-process. + * CRITICAL: ffmpeg_cleanup() av_freep()s the input_files/output_files/ + * filtergraphs/decoders arrays but does NOT reset their nb_* counters — + * a second run's opt_map then dereferences input_files[0] with + * nb_input_files still set from run 1 → SIGSEGV (reproduced in the P0 + * harness). Zero the counters alongside the signal flags. */ + received_sigterm = 0; + received_nb_signals = 0; + atomic_store(&transcode_init_done, 0); + ffmpeg_exited = 0; + copy_ts_first_pts = AV_NOPTS_VALUE; + nb_input_files = 0; + nb_output_files = 0; + nb_filtergraphs = 0; + nb_decoders = 0; +#endif init_dynload(); setvbuf(stderr,NULL,_IONBF,0); /* win32 runtime needs this */