Skip to content

ffmpeg: make avfoundation's wait for the first frame interruptible - #50

Merged
jcelerier merged 1 commit into
masterfrom
fix/ffmpeg-avfoundation-interrupt
Oct 19, 2026
Merged

jcelerier merged 1 commit into
masterfrom
fix/ffmpeg-avfoundation-interrupt

Conversation

@jcelerier

@jcelerier jcelerier commented Oct 19, 2026 •

Copy link
Copy Markdown
Member

On macOS, opening an avfoundation capture device that the process isn't allowed to use never returns. FFmpeg 9.0 (and FFmpeg master) waits in get_video_config() / get_audio_config() for the first frame:

while (ctx->frames_captured < 1) {
    CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.1, YES);
}

When camera or microphone access hasn't been granted, AVFoundation runs the session and simply never delivers a sample buffer: no error is reported. The loop never checks the interrupt callback, so callers can't time out or cancel. score's CameraInput already opens under a 2 s LibavTimeout, but that timeout can't fire here. The result is a hang on the GUI thread when playback starts with a camera device. Users of LivePose, a custom app built on score, hit this on every Mac.

This adds common/patches/ffmpeg/0002-avfoundation-interruptible-first-frame.patch, which checks s->interrupt_callback in both loops, and applies it in clone-ffmpeg.sh on macOS. The callback is called directly rather than through ff_check_interrupt(), because that function isn't exported from a shared libavformat. This keeps the patch upstreamable as is.

Note: .gitignore ignores *.*, so the patch is force-added, like 0001-rpi.patch.

Tested

On macmini-m1 (macOS 26.4.1), I used a minimal FFmpeg 9.0 build (--disable-everything --enable-indev=avfoundation) and a small harness. The harness opens camera 0 (OBS Virtual Camera) with a 3 s interrupt deadline. I ran it over ssh, where TCC refuses camera access without prompting (kTCCServiceCamera → authValue=0, attributed to sshd-keygen-wrapper):

result
FFmpeg 9.0 never returns; killed by a 15 s alarm
FFmpeg 9.0 + this patch Interrupted while waiting for the first video frame, avformat_open_input → EIO after 3.0 s

I didn't run a full SDK build. The patch applies with no fuzz to n9.0 and, unchanged, to FFmpeg master 71403a21b0 (which update/ffmpeg-master moves the SDK to).

Companion score PR: ossia/score#2354 (it checks camera authorization before opening, so it no longer depends on this patch to avoid the hang. This patch is the backstop for everything else: a refused microphone, a device that never sends frames).

🤖 Generated with Claude Code

Without camera or microphone access, AVFoundation runs the capture session
but never delivers a frame, and avf_read_header() spins forever waiting for
one -- ignoring the caller's interrupt callback, so the open cannot be timed
out or cancelled. score hit this as a hang when starting a camera that the
user had not allowed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@jcelerier
jcelerier merged commit 5ee98a6 into master Oct 19, 2026
0 of 8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant