voelin-media: keep AMF's runtime from crashing the app during the probe

The desktop app died at startup under Xvfb with SIGSEGV in libamfrt64,
inside avcodec_open2 while the encoder probe opened h264_amf, hevc_amf
and av1_amf on parallel threads. Reproduced with the probe alone: 10 of
420 runs crashed with four to eight processes probing at once, every one
in AMF's process-wide AMFFactoryHelper (Init, or Terminate dlclose-ing a
library) while AMFContextImpl::InitVulkan created an encoder's device.
Serialising AMF opens within the process removed it: none of 600 runs
crashed, with the processes still probing together. So the race is
between threads of one process, not between processes.

Two changes:

- AMF encoders are opened and closed one at a time in the process. That
  fixes the race wherever AMF is used: the probe, simulcast layers
  opening together, and Windows, where AMF is the AMD encoder.

- On Linux AMF is not used unless VOELIN_FFMPEG_AMF=1. FfmpegEncoder::new
  refuses it with the reason, so neither the probe, nor the factories, nor
  a direct caller loads the runtime at all. VA-API drives the same VCN
  encoder there, measured as fast with less CPU, and only VA-API takes
  DMA-BUFs without a copy; AMF on Linux is AMD's closed runtime on top of
  Vulkan, and a library that is never loaded cannot crash the app.

Not done: running the self-tests in a child process. A clean child does
not reproduce the state of the process that opens the encoder afterwards
(here the race only showed under load), so it would pass a backend that
can still crash the app, and every binary's main would have to cooperate.

Tests: the AMF refusal through FfmpegEncoder::new, the probe reporting
the reason without libamfrt mapped into the process, and an ignored
stress test that opens every AMF encoder on parallel threads 40 times
(with VOELIN_FFMPEG_AMF=1 it passes; the probe loop it replaces crashed).

On this machine the automatic H.264 encoder changes from h264_amf to
h264_vaapi.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
selikhovandClaude Opus 5.5 committed 2026-10-03 07:19:41 +03:00
1 parent 5446cea567
commit 5de0627a45
3 files changed
+132 -2

No files matched your search

+21
View File
@@ -323,6 +323,26 @@ the software encoders above are used as before.
machine below this took `voelinctl stream encoders` from 1.77 s to
0.93-1.12 s, testing 12 backends instead of 20, and the report says "no
NVIDIA GPU in this machine" instead of a CUDA error code.
- AMF on Linux: not used unless `VOELIN_FFMPEG_AMF=1`. No AMF encoder is
made (`FfmpegEncoder::new` refuses it with the reason, so the probe, the
factories and direct callers alike), and AMD's runtime (`libamfrt64`)
is never loaded. VA-API drives the same VCN encoder there, as fast and
with less CPU in the measurements below, and only VA-API takes DMA-BUFs
without a copy; AMF on Linux is AMD's closed runtime running on Vulkan.
It crashed the desktop app during the startup probe: SIGSEGV in
`AMFFactoryHelper::Init` / `Terminate` (`libamfrt64`, inside
`avcodec_open2` creating AMF's Vulkan device), reproduced in 10 of 420
probes with several processes probing at once. The runtime's
process-wide factory helper loads and unloads libraries while each new
encoder initialises, so encoders opened on parallel threads (the probe's
self-tests, simulcast layers) pull libraries from under each other.
Wherever AMF is used (Windows, or Linux with the variable), AMF encoders
are now opened and closed one at a time in the process: none of 600
probes crashed that way, with up to eight processes probing together
(`amf_encoders_open_in_parallel`, `--ignored`, stresses it). A crash in
a vendor library still takes the process down; running the self-tests in
a child process was not done because a clean child does not reproduce
the state of the app that uses the encoder afterwards.
- Third-party output: FFmpeg's own log goes to `tracing`, but the libraries
behind the encoders write to standard error themselves — SVT-AV1 prints a
build banner and its allocation totals, AMD's AMF runtime prints
@@ -834,6 +854,7 @@ has not run on Windows yet.
| FFmpeg loader: sonames, missing FFmpeg, layout checks on a real release | `ffmpeg::sys` / `ffmpeg` unit tests; mirrors compared with offsets compiled from the 4.4-9.0 headers | tested (FFmpeg 6.1.1 on Ubuntu 24.04; FFmpeg 9.0.1 / libavutil 61 / libavcodec 63 on Arch, every offset compared with that release's own headers) |
| FFmpeg software encoders → our decoders: x264 → OpenH264, SVT-AV1 / rav1e / libaom → dav1d (PSNR > 28 dB, keyframes at start and on request, timestamps, bitrate change, size change, odd sizes, Constrained High / Baseline) | `tests/ffmpeg.rs` (`VOELIN_OPENH264_LIB`, `--features av1`) | tested |
| Self-test failures (NVENC without CUDA, Quick Sync without a session, VA-API without a render node, encoders not in the build) | `tests/ffmpeg.rs`, `voelinctl stream encoders` | tested: each fails alone with FFmpeg's reason |
| AMF never loaded on Linux by default; AMF opens serialised | `ffmpeg::encoder::tests::amf_is_opt_in_on_linux`, `tests/ffmpeg.rs` `probe_reports_every_backend` (no `libamfrt` in `/proc/self/maps`), `amf_encoders_open_in_parallel` (`--ignored`, with `VOELIN_FFMPEG_AMF=1`) | tested: the probe crash (SIGSEGV in `libamfrt64`, 10 of 420 concurrent probes under Xvfb) reproduced, then none in 600 with serialised opens |
| Encoder preference (auto, software, named, hardware off), report ranks | `codec::tests::encoder_preference` | tested |
| An encoder per codec viewers chose (made, dropped, stream codec skipped, preference change) | `voelin-core` `media::tests::an_encoder_per_codec_viewers_chose` | tested |
| Answer's codec reported to the streamer, H.264 level in the offer, HEVC offered | `voelin-stream` `peer::tests::streamer_learns_the_answered_codec`, `h264::tests` | tested (str0m on loopback) |