Commit 5a6d53892a for ffmpeg
commit 5a6d53892a3b483595af72a5ac19c87597a6f5a7
Author: Jun Zhao <barryjzhao@tencent.com>
Date: Fri Dec 4 23:26:41 2026 +0800
fftools/ffmpeg_demux: interrupt stalled AVFoundation reads on shutdown
Let AVFoundation probing and capture wait for the next frame by
default. Check scheduler shutdown as well as signals in the input
interrupt callback, and stop the demux loop when a read is interrupted
or returns EAGAIN during shutdown.
Check for cancellation after stream probing so an interrupt does not
proceed to output setup and truncate an existing file with -y.
Report a signal during probing with exit status 255 so callers can
distinguish cancellation from a successful conversion.
Refs #26556.
Signed-off-by: Jun Zhao <barryjzhao@tencent.com>
diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c
index 186b549102..563d4a8451 100644
--- a/fftools/ffmpeg.c
+++ b/fftools/ffmpeg.c
@@ -1059,7 +1059,7 @@ int main(int argc, char **argv)
finish:
if (ret == AVERROR_EXIT)
- ret = 0;
+ ret = received_nb_signals ? 255 : 0;
ffmpeg_cleanup(ret);
diff --git a/fftools/ffmpeg_demux.c b/fftools/ffmpeg_demux.c
index 01301e073e..1bf502bc7c 100644
--- a/fftools/ffmpeg_demux.c
+++ b/fftools/ffmpeg_demux.c
@@ -866,6 +866,11 @@ static int input_thread(void *arg)
ret = av_read_frame(f->ctx, dt.pkt_demux);
+ if ((ret == AVERROR(EAGAIN) || ret == AVERROR_EXIT) &&
+ sch_is_stopping(d->sch)) {
+ ret = AVERROR_EXIT;
+ break;
+ }
if (ret == AVERROR(EAGAIN)) {
av_usleep(10000);
continue;
@@ -2316,6 +2321,11 @@ static Demuxer *demux_alloc(void)
return d;
}
+static int input_interrupt_cb(void *opaque)
+{
+ return sch_is_stopping(opaque) || int_cb.callback(int_cb.opaque);
+}
+
int ifile_open(const OptionsContext *o, const char *filename, Scheduler *sch)
{
Demuxer *d;
@@ -2452,10 +2462,15 @@ int ifile_open(const OptionsContext *o, const char *filename, Scheduler *sch)
ic->subtitle_codec_id = subtitle_codec_name ? ic->subtitle_codec->id : AV_CODEC_ID_NONE;
ic->data_codec_id = data_codec_name ? ic->data_codec->id : AV_CODEC_ID_NONE;
- ic->flags |= AVFMT_FLAG_NONBLOCK;
+ if (file_iformat && !strcmp(file_iformat->name, "avfoundation")) {
+ // Capture callbacks wake blocking reads when a frame is ready.
+ ic->interrupt_callback = (AVIOInterruptCB){ input_interrupt_cb, sch };
+ } else {
+ ic->flags |= AVFMT_FLAG_NONBLOCK;
+ ic->interrupt_callback = int_cb;
+ }
if (o->bitexact)
ic->flags |= AVFMT_FLAG_BITEXACT;
- ic->interrupt_callback = int_cb;
if (!av_dict_get(o->g->format_opts, "scan_all_pmts", NULL, AV_DICT_MATCH_CASE)) {
av_dict_set(&o->g->format_opts, "scan_all_pmts", "1", AV_DICT_DONT_OVERWRITE);
@@ -2511,6 +2526,9 @@ int ifile_open(const OptionsContext *o, const char *filename, Scheduler *sch)
av_dict_free(&opts[i]);
av_freep(&opts);
+ if (int_cb.callback(int_cb.opaque))
+ return AVERROR_EXIT;
+
if (ret < 0) {
av_log(d, AV_LOG_FATAL, "could not find codec parameters\n");
if (ic->nb_streams == 0)
diff --git a/fftools/ffmpeg_sched.c b/fftools/ffmpeg_sched.c
index fe64eb6557..3c440f3703 100644
--- a/fftools/ffmpeg_sched.c
+++ b/fftools/ffmpeg_sched.c
@@ -2797,6 +2797,11 @@ static int task_stop(Scheduler *sch, SchTask *task)
return (intptr_t)thread_ret;
}
+int sch_is_stopping(const Scheduler *sch)
+{
+ return atomic_load(&sch->terminate);
+}
+
int sch_stop(Scheduler *sch, int64_t *finish_ts)
{
int ret = 0, err;
diff --git a/fftools/ffmpeg_sched.h b/fftools/ffmpeg_sched.h
index 26f8fb6d94..c83ff2cc1d 100644
--- a/fftools/ffmpeg_sched.h
+++ b/fftools/ffmpeg_sched.h
@@ -135,6 +135,7 @@ void sch_free(Scheduler **sch);
int sch_start(Scheduler *sch);
int sch_stop(Scheduler *sch, int64_t *finish_ts);
+int sch_is_stopping(const Scheduler *sch);
/**
* Wait until transcoding terminates or the specified timeout elapses.