Repository navigation
ffmpeg: make avfoundation's wait for the first frame interruptible - #50
Merged
Merged
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
On macOS, opening an
avfoundationcapture device that the process isn't allowed to use never returns. FFmpeg 9.0 (and FFmpeg master) waits inget_video_config()/get_audio_config()for the first frame: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
CameraInputalready opens under a 2 sLibavTimeout, 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 checkss->interrupt_callbackin both loops, and applies it inclone-ffmpeg.shon macOS. The callback is called directly rather than throughff_check_interrupt(), because that function isn't exported from a shared libavformat. This keeps the patch upstreamable as is.Note:
.gitignoreignores*.*, so the patch is force-added, like0001-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 tosshd-keygen-wrapper):Interrupted while waiting for the first video frame,avformat_open_input→EIOafter 3.0 sI 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