Skip to content

obs-media-io: real-time audio thread on Linux through the Realtime portal or rtkit (Refs #5770) - #5781

Merged
vpacher merged 3 commits into
masterfrom
fix/5770-linux-realtime-audio-thread
Nov 26, 2026
Merged

vpacher merged 3 commits into
masterfrom
fix/5770-linux-realtime-audio-thread

Conversation

@vpacher

@vpacher vpacher commented Nov 26, 2026 •

Copy link
Copy Markdown
Member

Refs #5770. Part of #5768 (thread priority on Linux and macOS, discussion #5458).

Change

On Linux, the audio thread now asks for real-time priority when it starts, as the Windows audio thread joins MMCSS "Audio".

  • Order (as PipeWire's module-rt):

    1. SCHED_RR directly, when RLIMIT_RTPRIO allows it;
    2. the org.freedesktop.portal.Realtime portal on the session bus (also covers Flatpak);
    3. rtkit on the system bus;
    4. fallback: high priority (nice -11, or the service's MinNiceLevel if that is higher).

    Priority 20, clamped to RLIMIT_RTPRIO or MaxRealtimePriority. A request that the service cannot grant (limit 0) is not sent.

  • RLIMIT_RTTIME and the guard. The portal and rtkit grant real time only with a process-wide RLIMIT_RTTIME. os_sleepto_ns_fast returns at once when the target has passed, so after a long stall the audio thread mixes many ticks without blocking. Without protection, the kernel would send SIGXCPU, then SIGKILL. Before any real-time request (the direct path included, because an in-process PipeWire client can set the limit too):

    • the hard limit is rtkit's RTTimeUSecMax, the soft limit half of it;
    • a SIGXCPU handler puts the registered audio threads back to SCHED_OTHER|SCHED_RESET_ON_FORK, then calls the previous handler, if any.

    OBS loses real-time priority but keeps running.

  • Cost: one probe per process, cached. D-Bus calls have a 500 ms timeout, and a bus whose socket is missing is not connected (no autolaunch), so Tier 2 tests and headless runs return at once.

  • Log: one line when the audio thread starts (for example audio thread: real-time priority 20 through rtkit, or default priority (...) with the reason for each method), and one warning if the guard demotes the thread.

Structure:

  • obs-media-io/src/thread_priority.rs: the host-neutral decision, behind a RealtimeHost trait for every side effect.
  • obs-media-io/src/platform/linux.rs: the Linux host (rlimits, scheduler, SIGXCPU guard). AudioThreadClass moves out of unix.rs on Linux; macOS and the BSDs keep the empty stub (macOS: perf: obs-media-io: time-constraint policy for the audio thread on macOS #5771).
  • obs-util/src/ffi/dbus_realtime.rs: GIO calls, gio-gated like the screensaver inhibit code. obs-media-io gets a gio feature, which libobs-rust's gio feature turns on.

RED → GREEN

  • RED b594343: 13 Tier 1 tests in tests/audio/thread_priority.rs, against a fake host that records every call. They cover:

    • the order and the clamps;
    • the CPU-time limit and the guard before any real-time request, the direct path included;
    • no real-time request without the guard or the limit;
    • the high-priority fallback, and no request a service cannot grant;
    • one probe per process;
    • the log line.

    All 13 fail at todo!().

  • GREEN af34772: all pass. Media-io harness 416/416 on macOS, and on Linux with and without the gio feature. Clippy is clean in both feature sets, and ./lint passes (Linux).

Tier 2 and Tier 3 do not apply: there is no C original. Tier 2 builds hybrid libobs with gio on, so every audio test there makes the probe once (no bus in the container, so it returns at once).

Real check (Linux desktop, Arch, kernel 7.2)

A local probe: the media-io test binary with gio, which starts a real audio thread through the public audio-output API. It ran as a user-manager unit, like a desktop app. It is not the OBS app, which is why this PR says Refs.

Case Log line Audio thread
Portal present, rtkit not installed default priority (the Realtime portal: no real-time priority allowed; rtkit: ...ServiceUnknown...) SCHED_OTHER
rtkit installed (the portal still reports 0) real-time priority 20 through rtkit (the Realtime portal: no real-time priority allowed) SCHED_RR|SCHED_RESET_ON_FORK, 20
Portal sees rtkit real-time priority 20 through the Realtime portal SCHED_RR|SCHED_RESET_ON_FORK, 20
As above, 150 µs work per tick, SIGSTOP 60 s, SIGCONT demotion warning; the thread catches up 3000 ticks and runs on; exit 0 SCHED_OTHER|SCHED_RESET_ON_FORK

The stall check found a bug before this PR. A demotion to plain SCHED_OTHER fails with EPERM, because clearing SCHED_RESET_ON_FORK needs CAP_SYS_NICE. The thread then stayed real-time and the process was killed at the hard limit (exit 137). The handler now keeps the flag.

From an SSH session, rtkit answers AccessDenied: its polkit policy has allow_any=no, so remote sessions are refused. A remote or headless start therefore logs default priority with that reason.

Not checked:

Test cost

13 new tests with a fake host: each takes under 1 ms, uses no threads of its own, and touches no process state.

Ledger

docs/features.md, Audio: "Real-time audio thread".

🤖 Generated with Claude Code

vpacher and others added 2 commits November 26, 2026 13:51
…ks (RED, Refs #5770)

Tier 1 against a fake host: direct SCHED_RR, then the Realtime portal,
then rtkit, then high priority; RLIMIT_RTTIME and the SIGXCPU guard before
any real-time request; no request a service cannot grant; one probe per
process; the startup log line. The core is todo!() here.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…rtal or rtkit (Refs #5770)

The audio thread asks, in PipeWire module-rt's order, for SCHED_RR
directly (RLIMIT_RTPRIO), then through the Realtime portal (also covers
Flatpak), then rtkit, and falls back to high priority. A request the
service cannot grant is skipped; one probe per process is cached.

Before any real-time request the process gets RLIMIT_RTTIME (soft half of
rtkit's maximum) and a SIGXCPU guard that demotes the audio thread to
SCHED_OTHER|SCHED_RESET_ON_FORK, so a catch-up burst after a long stall
costs real-time priority instead of OBS being killed at the hard limit.
The audio thread logs the result once at start, and the demotion once.

D-Bus goes through GIO (obs-util ffi::dbus_realtime, gio feature) with a
500 ms timeout and no bus autolaunch.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@vpacher
vpacher requested a review from zackees as a code owner November 26, 2026 12:58
@vpacher
vpacher enabled auto-merge November 26, 2026 13:01
@vpacher
vpacher added this pull request to the merge queue Nov 26, 2026
Merged via the queue into master with commit cef8f1d Nov 26, 2026
47 checks passed
@vpacher
vpacher deleted the fix/5770-linux-realtime-audio-thread branch November 26, 2026 13:52
KelboLive pushed a commit to KelboLive/obs-studio that referenced this pull request Nov 29, 2026
…rust#5790) (obs-rust#5791)

libc::gettid is a glibc 2.30 wrapper, so libobs.so stopped linking on the
AppImage's manylinux_2_28 image (glibc 2.28): the scheduled release run's
AppImage build fails with "libobs.so.30: undefined reference to `gettid'"
since obs-rust#5781. The raw syscall needs no glibc wrapper.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Ci-Attestation: {"at":1795725241,"gate":"data/all/compatibility","host":"linux-x86_64","key":"","lane":"client-checks","parents":["529d2d9fd151535e37f3b482df01c7b2dfe71667"],"secs":234,"stamp":"3bcefdc08b2120936926609bb273d4f6","tree":"c684dbbee78fd6f4fb56ec0f49a847219e7ffb7d","v":1,"via":"run"}
Ci-Attestation: {"at":1795725241,"gate":"data/all/flatpak-manifest","host":"linux-x86_64","key":"","lane":"client-checks","parents":["529d2d9fd151535e37f3b482df01c7b2dfe71667"],"secs":234,"stamp":"b001a04d767ec0f66a57a311853759fb","tree":"c684dbbee78fd6f4fb56ec0f49a847219e7ffb7d","v":1,"via":"run"}
Ci-Attestation: {"at":1795725241,"gate":"data/all/qt-xml","host":"linux-x86_64","key":"","lane":"client-checks","parents":["529d2d9fd151535e37f3b482df01c7b2dfe71667"],"secs":234,"stamp":"3fc2a7930fcdfc65e1271469b746c849","tree":"c684dbbee78fd6f4fb56ec0f49a847219e7ffb7d","v":1,"via":"run"}
Ci-Attestation: {"at":1795725241,"gate":"data/all/services-schema","host":"linux-x86_64","key":"","lane":"client-checks","parents":["529d2d9fd151535e37f3b482df01c7b2dfe71667"],"secs":234,"stamp":"500b8d807b91ff85d3f2e7c326d7e003","tree":"c684dbbee78fd6f4fb56ec0f49a847219e7ffb7d","v":1,"via":"run"}
Ci-Attestation: {"at":1795725004,"gate":"general/all/client-lint","host":"linux-x86_64","key":"e0cfb0721679f88a05ab7250db0b650ff9983ba8444ba9d5d4aaf9723005fd1f","lane":"client-lint","parents":["529d2d9fd151535e37f3b482df01c7b2dfe71667"],"secs":218,"stamp":"c8fa83b578c9995321946acacdce8b20","tree":"c684dbbee78fd6f4fb56ec0f49a847219e7ffb7d","v":1,"via":"run"}
Ci-Attestation: {"at":1795725241,"gate":"python/all/self-test","host":"linux-x86_64","key":"","lane":"client-checks","parents":["529d2d9fd151535e37f3b482df01c7b2dfe71667"],"secs":234,"stamp":"bedfc9bd7635f1817e776286113346c0","tree":"c684dbbee78fd6f4fb56ec0f49a847219e7ffb7d","v":1,"via":"run"}
Ci-Attestation: {"at":1795725005,"gate":"rust/aarch64-apple-darwin/client-clippy","host":"linux-x86_64","key":"e0cfb0721679f88a05ab7250db0b650ff9983ba8444ba9d5d4aaf9723005fd1f","lane":"client-lint","parents":["529d2d9fd151535e37f3b482df01c7b2dfe71667"],"secs":218,"stamp":"ef3052f9bcfafb33f20fb88d9e391272","tree":"c684dbbee78fd6f4fb56ec0f49a847219e7ffb7d","v":1,"via":"run"}
Ci-Attestation: {"at":1795725005,"gate":"rust/aarch64-apple-darwin/client-dylint","host":"linux-x86_64","key":"e0cfb0721679f88a05ab7250db0b650ff9983ba8444ba9d5d4aaf9723005fd1f","lane":"client-lint","parents":["529d2d9fd151535e37f3b482df01c7b2dfe71667"],"secs":218,"stamp":"117ebdfb54d7b6ea0cc134c5b27d67ce","tree":"c684dbbee78fd6f4fb56ec0f49a847219e7ffb7d","v":1,"via":"run"}
Ci-Attestation: {"at":1795725005,"gate":"rust/aarch64-pc-windows-msvc/client-clippy","host":"linux-x86_64","key":"e0cfb0721679f88a05ab7250db0b650ff9983ba8444ba9d5d4aaf9723005fd1f","lane":"client-lint","parents":["529d2d9fd151535e37f3b482df01c7b2dfe71667"],"secs":218,"stamp":"7c9893e3926cd66e4707fdbec204e59b","tree":"c684dbbee78fd6f4fb56ec0f49a847219e7ffb7d","v":1,"via":"run"}
Ci-Attestation: {"at":1795725005,"gate":"rust/aarch64-pc-windows-msvc/client-dylint","host":"linux-x86_64","key":"e0cfb0721679f88a05ab7250db0b650ff9983ba8444ba9d5d4aaf9723005fd1f","lane":"client-lint","parents":["529d2d9fd151535e37f3b482df01c7b2dfe71667"],"secs":218,"stamp":"063189a768b3978195af9397c4157ba9","tree":"c684dbbee78fd6f4fb56ec0f49a847219e7ffb7d","v":1,"via":"run"}
Ci-Attestation: {"at":1795725005,"gate":"rust/aarch64-unknown-linux-gnu/client-clippy","host":"linux-x86_64","key":"e0cfb0721679f88a05ab7250db0b650ff9983ba8444ba9d5d4aaf9723005fd1f","lane":"client-lint","parents":["529d2d9fd151535e37f3b482df01c7b2dfe71667"],"secs":218,"stamp":"dba0c77a70773b822e994dd05a86750d","tree":"c684dbbee78fd6f4fb56ec0f49a847219e7ffb7d","v":1,"via":"run"}
Ci-Attestation: {"at":1795725005,"gate":"rust/aarch64-unknown-linux-gnu/client-dylint","host":"linux-x86_64","key":"e0cfb0721679f88a05ab7250db0b650ff9983ba8444ba9d5d4aaf9723005fd1f","lane":"client-lint","parents":["529d2d9fd151535e37f3b482df01c7b2dfe71667"],"secs":218,"stamp":"95ea859dcd2fc68b035871ef8a5fac54","tree":"c684dbbee78fd6f4fb56ec0f49a847219e7ffb7d","v":1,"via":"run"}
Ci-Attestation: {"at":1795725005,"gate":"rust/x86_64-apple-darwin/client-clippy","host":"linux-x86_64","key":"e0cfb0721679f88a05ab7250db0b650ff9983ba8444ba9d5d4aaf9723005fd1f","lane":"client-lint","parents":["529d2d9fd151535e37f3b482df01c7b2dfe71667"],"secs":218,"stamp":"56e9ea1533efe393b85cd4d6cbb00464","tree":"c684dbbee78fd6f4fb56ec0f49a847219e7ffb7d","v":1,"via":"run"}
Ci-Attestation: {"at":1795725005,"gate":"rust/x86_64-apple-darwin/client-dylint","host":"linux-x86_64","key":"e0cfb0721679f88a05ab7250db0b650ff9983ba8444ba9d5d4aaf9723005fd1f","lane":"client-lint","parents":["529d2d9fd151535e37f3b482df01c7b2dfe71667"],"secs":218,"stamp":"7e864f82a5a6e4cb234f1839a291acce","tree":"c684dbbee78fd6f4fb56ec0f49a847219e7ffb7d","v":1,"via":"run"}
Ci-Attestation: {"at":1795725005,"gate":"rust/x86_64-pc-windows-msvc/client-clippy","host":"linux-x86_64","key":"e0cfb0721679f88a05ab7250db0b650ff9983ba8444ba9d5d4aaf9723005fd1f","lane":"client-lint","parents":["529d2d9fd151535e37f3b482df01c7b2dfe71667"],"secs":218,"stamp":"ec5e6f18cf18facecbcf9921874573c4","tree":"c684dbbee78fd6f4fb56ec0f49a847219e7ffb7d","v":1,"via":"run"}
Ci-Attestation: {"at":1795725005,"gate":"rust/x86_64-pc-windows-msvc/client-dylint","host":"linux-x86_64","key":"e0cfb0721679f88a05ab7250db0b650ff9983ba8444ba9d5d4aaf9723005fd1f","lane":"client-lint","parents":["529d2d9fd151535e37f3b482df01c7b2dfe71667"],"secs":218,"stamp":"bccfae2a0921ead89f35c14ac662a97a","tree":"c684dbbee78fd6f4fb56ec0f49a847219e7ffb7d","v":1,"via":"run"}
Ci-Attestation: {"at":1795725004,"gate":"rust/x86_64-unknown-linux-gnu/client-clippy","host":"linux-x86_64","key":"e0cfb0721679f88a05ab7250db0b650ff9983ba8444ba9d5d4aaf9723005fd1f","lane":"client-lint","parents":["529d2d9fd151535e37f3b482df01c7b2dfe71667"],"secs":218,"stamp":"620559cd4e3b8a629dae336e4917c790","tree":"c684dbbee78fd6f4fb56ec0f49a847219e7ffb7d","v":1,"via":"run"}
Ci-Attestation: {"at":1795725005,"gate":"rust/x86_64-unknown-linux-gnu/client-dylint","host":"linux-x86_64","key":"e0cfb0721679f88a05ab7250db0b650ff9983ba8444ba9d5d4aaf9723005fd1f","lane":"client-lint","parents":["529d2d9fd151535e37f3b482df01c7b2dfe71667"],"secs":218,"stamp":"069f76ba0067f5f2cc78340fac03884d","tree":"c684dbbee78fd6f4fb56ec0f49a847219e7ffb7d","v":1,"via":"run"}
Ci-Attestation: {"at":1795725867,"gate":"rust/x86_64-unknown-linux-gnu/client-test","host":"linux-x86_64","key":"950b476384f27bc4f6597382dba91cea1abac64387df2b42ed1bb76c87ab7ddd","lane":"client-test-x86_64-unknown-linux-gnu","parents":["529d2d9fd151535e37f3b482df01c7b2dfe71667"],"secs":625,"stamp":"6bc187e2f4af66126b5216fb6ef6e2e5","tree":"c684dbbee78fd6f4fb56ec0f49a847219e7ffb7d","v":1,"via":"run"}

Client-Checks: {"checks":{"base":"529d2d9fd151535e37f3b482df01c7b2dfe71667","coverage":"all-tracked-applicable-files","diff":["rust/obs-media-io/src/platform/linux.rs"],"head":"e12a6e5e776d2d5d5f9ebab8874731bf2cba1eef","tools":{"orchestrator-python":"3.13.16 (main, Oct 14 2026, 01:04:01) [Clang 23.1.3 ]","schema-dependencies":"3.13.16 (main, Oct 14 2026, 01:04:01) [Clang 23.1.3 ]\n{'jsonschema': '4.26.0', 'json_source_map': '1.0.5'}","uv":"uv 0.13.5 (x86_64-unknown-linux-gnu)","xmllint":"xmllint: using libxml version 21504-GITv2.15.4\n   compiled with: Threads Tree Output Push Reader Patterns Writer SAXv1 DTDValid HTML C14N Catalog XPath XPointer XInclude Iconv ICU ISO8859X Regexps Automata RelaxNG Schemas Schematron Modules Debug Zlib"},"tracked-digest":"3111c3be86c17d67ad7a37f26814a652443330b6e5a0d5c8dbd7eb3cc5222b43","tree":"c684dbbee78fd6f4fb56ec0f49a847219e7ffb7d"}}
KelboLive pushed a commit to KelboLive/obs-studio that referenced this pull request Nov 29, 2026
…bs-rust#5819)

* ci(sanitizers): require TSan not to sleep at exit (RED, Refs obs-rust#5814)

The harness test requires atexit_sleep_ms=0 in TSAN_OPTIONS. It fails on
master, where TSan sleeps 1 s at exit while GLib's GDBus worker thread
runs.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* ci(sanitizers): stop TSan's 1 s sleep at exit (GREEN, Fixes obs-rust#5814)

TSan sleeps atexit_sleep_ms (default 1000) at exit while another thread
runs. Since obs-rust#5781, every test process whose audio thread asks D-Bus for
real-time priority keeps GLib's GDBus worker thread until exit, so each
waited 1 s in the nightly tsan job. The OBS threads stop in
obs_shutdown(); the threads left at exit belong to GLib (suppressed in
tsan.supp) and llvmpipe.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

1 participant