From 9e1be0dcd88ecd976321506f1ce34d60f511adc3 Mon Sep 17 00:00:00 2001 From: Ryan Doupe Date: Sat, 24 Oct 2026 23:01:03 -0400 Subject: [PATCH 1/2] test: WebAudio after the audio service dies When Chromium's audio service process dies, a page's AudioContext fires error and is suspended. Nothing resumes it, so a browser source with "Control audio via OBS" delivers no audio until the page reloads. tests/audio_recovery.py plays a local WebAudio tone through real browser sources, SIGKILLs only this run's audio service process and requires packets again in the fifth second after the kill: one kill, three kills on a page that replaces and freezes globals, a context the page suspended itself (must stay suspended), and two sources. All four scenarios fail on master: 0 packets after the kill. Refs obs-rust/obs-studio#2328 --- tests/README.md | 24 ++++ tests/audio-recovery.c | 131 ++++++++++++++++++++++ tests/audio-recovery.html | 109 ++++++++++++++++++ tests/audio_recovery.py | 226 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 490 insertions(+) create mode 100644 tests/audio-recovery.c create mode 100644 tests/audio-recovery.html create mode 100644 tests/audio_recovery.py diff --git a/tests/README.md b/tests/README.md index 1bde383..0e6187f 100644 --- a/tests/README.md +++ b/tests/README.md @@ -88,3 +88,27 @@ Linux x86_64 (Debian 13 LXC container, kernel 7.0.14, CEF 7871 namespace sandbox for every helper state; as a normal user with `RestrictNamespaces=~user`, off without a helper or with a helper that is not setuid, and the setuid helper with a helper that is setuid root. + +## WebAudio after the audio service dies + +`audio_recovery.py` compiles `audio-recovery.c` against the actual libobs and +plays `audio-recovery.html` (a WebAudio tone) through real browser sources +with "Control audio via OBS". Once packets flow it SIGKILLs only this run's +Chromium audio service process, found through the harness's process tree, +and requires packets again in the fifth second after the kill. Scenarios: +`plain` (also checks that the page sees an unchanged `AudioContext`), +`repeated` (three kills, on a page that replaces and freezes the globals a +recovery script might use), `page-suspend` (a context the page suspended +itself stays suspended) and `two-sources`. Run it with an owned Xvfb and +dbus session, no audio server and a fresh HOME, for example inside +`unshare -rn` with `PULSE_SERVER=unix:/nonexistent`: + +```sh +uv run --no-project python tests/audio_recovery.py \ + --include-dir /path/to/obs/libobs --include-dir /path/to/generated/config \ + --library-dir /path/to/staged/lib --module /path/to/staged/obs-browser.so \ + --data /path/to/obs-browser/data --libobs-data /path/to/staged/libobs/data \ + --output-dir /path/to/new-output [--scenario plain ...] +``` + +Linux only; it needs CEF and kills a process, so CI does not run it. diff --git a/tests/audio-recovery.c b/tests/audio-recovery.c new file mode 100644 index 0000000..41b9e00 --- /dev/null +++ b/tests/audio-recovery.c @@ -0,0 +1,131 @@ +/* Play a local WebAudio page through real browser sources with "Control + * audio via OBS" and report how many audio packets each source delivers. + * Prints "COUNTS ..." (cumulative) every 100 ms and forwards the + * OBS log (including the page's console.error lines) as "LOG ". + * Runs until stdin is closed or a line is read. */ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define MAX_SOURCES 4 + +static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER; +static long packets[MAX_SOURCES]; + +static void count_audio(void *param, obs_source_t *source, const struct audio_data *audio, bool muted) +{ + (void)source; + (void)audio; + (void)muted; + pthread_mutex_lock(&lock); + packets[(size_t)param]++; + pthread_mutex_unlock(&lock); +} + +static void forward_log(int level, const char *format, va_list args, void *param) +{ + (void)level; + (void)param; + char line[4096]; + vsnprintf(line, sizeof(line), format, args); + pthread_mutex_lock(&lock); + printf("LOG %s\n", line); + fflush(stdout); + pthread_mutex_unlock(&lock); +} + +int main(int argc, char **argv) +{ + if (argc < 7) { + fprintf(stderr, "usage: %s MODULE DATA LIBOBS_DATA CONFIG PAGE SOURCES\n", argv[0]); + return 2; + } + int count = atoi(argv[6]); + if (count < 1 || count > MAX_SOURCES) + return 2; + base_set_log_handler(forward_log, NULL); + + Display *display = XOpenDisplay(NULL); + if (!display) + return 3; + obs_set_nix_platform(OBS_NIX_PLATFORM_X11_EGL); + obs_set_nix_platform_display(display); + if (!obs_startup("en-US", argv[4], NULL)) + return 4; + /* A staged tree outside the install prefix needs its libobs effects. */ +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wdeprecated-declarations" + obs_add_data_path(argv[3]); +#pragma GCC diagnostic pop + + struct obs_audio_info audio = {.samples_per_sec = 48000, .speakers = SPEAKERS_STEREO}; + if (!obs_reset_audio(&audio)) + return 5; + struct obs_video_info video = { + .graphics_module = "libobs-opengl", + .fps_num = 30, + .fps_den = 1, + .base_width = 320, + .base_height = 180, + .output_width = 320, + .output_height = 180, + .output_format = VIDEO_FORMAT_NV12, + .gpu_conversion = true, + .colorspace = VIDEO_CS_709, + .range = VIDEO_RANGE_PARTIAL, + .scale_type = OBS_SCALE_BICUBIC, + }; + if (obs_reset_video(&video) != OBS_VIDEO_SUCCESS) + return 6; + + obs_module_t *module = NULL; + if (obs_open_module(&module, argv[1], argv[2]) != MODULE_SUCCESS || !obs_init_module(module)) + return 7; + obs_post_load_modules(); + + obs_source_t *sources[MAX_SOURCES] = {0}; + for (int i = 0; i < count; i++) { + char name[32]; + snprintf(name, sizeof(name), "Audio page %d", i); + obs_data_t *settings = obs_data_create(); + obs_data_set_bool(settings, "is_local_file", true); + obs_data_set_string(settings, "local_file", argv[5]); + obs_data_set_bool(settings, "reroute_audio", true); + obs_data_set_int(settings, "width", 160); + obs_data_set_int(settings, "height", 90); + sources[i] = obs_source_create("browser_source", name, settings, NULL); + obs_data_release(settings); + if (!sources[i]) + return 8; + obs_source_add_audio_capture_callback(sources[i], count_audio, (void *)(size_t)i); + obs_set_output_source((uint32_t)i, sources[i]); + } + + struct pollfd input = {.fd = STDIN_FILENO, .events = POLLIN}; + while (poll(&input, 1, 100) == 0) { + pthread_mutex_lock(&lock); + printf("COUNTS"); + for (int i = 0; i < count; i++) + printf(" %ld", packets[i]); + printf("\n"); + fflush(stdout); + pthread_mutex_unlock(&lock); + } + + for (int i = 0; i < count; i++) { + obs_set_output_source((uint32_t)i, NULL); + obs_source_remove_audio_capture_callback(sources[i], count_audio, (void *)(size_t)i); + obs_source_release(sources[i]); + } + obs_shutdown(); + XCloseDisplay(display); + puts("DONE"); + return 0; +} diff --git a/tests/audio-recovery.html b/tests/audio-recovery.html new file mode 100644 index 0000000..ac857c2 --- /dev/null +++ b/tests/audio-recovery.html @@ -0,0 +1,109 @@ + + + + + + diff --git a/tests/audio_recovery.py b/tests/audio_recovery.py new file mode 100644 index 0000000..592957a --- /dev/null +++ b/tests/audio_recovery.py @@ -0,0 +1,226 @@ +#!/usr/bin/env -S uv run --script +"""Browser source WebAudio after Chromium's audio service dies (Linux, real CEF). + +Each scenario starts the real module in a fresh process and profile, with +browser sources that play a local WebAudio page with "Control audio via +OBS". Once audio packets flow, it SIGKILLs only this run's Chromium audio +service process (found through the harness's process tree) and requires +the packets to flow again within 5 seconds. +""" +import argparse +import os +from pathlib import Path +import queue +import signal +import subprocess +import threading +import time + +AUDIO_SERVICE = b"--utility-sub-type=audio.mojom.AudioService" +RECOVERY_SECONDS = 5.0 + + +class Failure(Exception): + pass + +parser = argparse.ArgumentParser() +parser.add_argument("--include-dir", type=Path, action="append", required=True) +parser.add_argument("--library-dir", type=Path, action="append", required=True) +parser.add_argument("--module", type=Path, required=True) +parser.add_argument("--data", type=Path, required=True) +parser.add_argument("--libobs-data", type=Path, required=True) +parser.add_argument("--output-dir", type=Path, required=True) +parser.add_argument("--compiler", default="cc") +parser.add_argument("--scenario", action="append", help="run only these scenarios") +args = parser.parse_args() +output = args.output_dir.resolve() +output.mkdir(parents=True, exist_ok=True) +executable = output / "audio-recovery" +here = Path(__file__).resolve().parent +command = [args.compiler, "-DHAVE_OBSCONFIG_H"] +for directory in args.include_dir: + command.append("-I" + str(directory.resolve())) +command.append(str(here / "audio-recovery.c")) +for directory in args.library_dir: + command.extend(["-L" + str(directory.resolve()), "-Wl,-rpath," + str(directory.resolve())]) +command.extend(["-lobs", "-lX11", "-lpthread", "-o", str(executable)]) +subprocess.run(command, check=True) + + +def descendants(root): + children = {} + for entry in Path("/proc").iterdir(): + if not entry.name.isdigit(): + continue + try: + stat = (entry / "stat").read_text() + except OSError: + continue + parent = int(stat.rsplit(")", 1)[1].split()[1]) + children.setdefault(parent, []).append(int(entry.name)) + found, pending = [], [root] + while pending: + for child in children.get(pending.pop(), []): + found.append(child) + pending.append(child) + return found + + +def audio_services(root): + services = [] + for pid in descendants(root): + try: + if AUDIO_SERVICE in Path(f"/proc/{pid}/cmdline").read_bytes(): + services.append(pid) + except OSError: + pass + return services + + +class Run: + def __init__(self, name, mode, sources): + self.name = name + self.sources = sources + page = output / (name + ".html") + page.write_text((here / "audio-recovery.html").read_text().replace("@MODE@", mode)) + config = output / (name + "-config") + config.mkdir() # Each process needs a fresh CEF profile. + self.log = open(output / (name + ".log"), "w") + self.lines = queue.Queue() + self.samples = [] # (monotonic time, cumulative packets per source) + self.states = [] # (monotonic time, page state line) + self.compat = None + self.process = subprocess.Popen( + [str(executable), str(args.module.resolve()), str(args.data.resolve()), + str(args.libobs_data.resolve()) + "/", str(config), str(page), str(sources)], + stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) + threading.Thread(target=self.read, daemon=True).start() + + def read(self): + for raw in self.process.stdout: + now = time.monotonic() + line = raw.decode(errors="replace").rstrip("\n") + if line.startswith("COUNTS "): + self.samples.append((now, [int(n) for n in line.split()[1:]])) + continue + self.log.write(f"[{now:.3f}] {line}\n") + if "audio-test state " in line: + self.states.append((now, line.split("audio-test state ", 1)[1].split(" (")[0])) + if "audio-test compat " in line: + self.compat = line.split("audio-test compat ", 1)[1].split(" (")[0] + self.lines.put(None) + + def packets(self, start, end): + """Packets per source that arrived between two monotonic times.""" + def at(moment): + before = [counts for when, counts in self.samples if when <= moment] + return before[-1] if before else [0] * self.sources + first, last = at(start), at(end) + return [b - a for a, b in zip(first, last)] + + def first_packet(self, after): + """Time of the first sample after `after` in which every source got packets.""" + previous = None + for when, counts in self.samples: + if when > after and previous and all(b > a for a, b in zip(previous, counts)): + return when + previous = counts + return float("nan") + + def wait(self, condition, seconds, what): + deadline = time.monotonic() + seconds + while time.monotonic() < deadline: + if self.process.poll() is not None: + self.fail(f"harness exited ({self.process.returncode}) while waiting for {what}") + if condition(): + return + time.sleep(0.1) + self.fail(f"timed out after {seconds} s waiting for {what}") + + def flowing(self): + now = time.monotonic() + return all(n > 0 for n in self.packets(now - 1.0, now)) + + def kill_audio_service(self): + self.wait(lambda: audio_services(self.process.pid), 10, "the audio service process") + pids = audio_services(self.process.pid) + for pid in pids: + os.kill(pid, signal.SIGKILL) + self.log.write(f"[{time.monotonic():.3f}] TEST killed audio service {pids}\n") + return time.monotonic() + + def fail(self, message): + self.stop() + raise Failure(f"{message} (log: {self.log.name})") + + def stop(self): + if self.process.poll() is None: + try: + self.process.stdin.write(b"q\n") + self.process.stdin.close() + self.process.wait(timeout=30) + except (OSError, subprocess.TimeoutExpired): + self.process.kill() + self.process.wait() + self.lines.get(timeout=10) + self.log.close() + + +def recover(run, kills): + for attempt in range(1, kills + 1): + run.wait(run.flowing, 30, "audio packets from every source") + killed = run.kill_audio_service() + time.sleep(RECOVERY_SECONDS) + late = run.packets(killed + RECOVERY_SECONDS - 1.0, killed + RECOVERY_SECONDS) + if not all(n > 0 for n in late): + run.fail(f"kill {attempt}: packets per source in the 5th second after the kill: {late}") + print(f"{run.name}: kill {attempt}: audio resumed, packets in the 5th second: {late}, " + f"first packet {run.first_packet(killed + 1.0) - killed:.1f} s after the kill") + return killed + + +def plain(): + run = Run("plain", "plain", 1) + run.wait(lambda: run.compat is not None, 30, "the compatibility report") + if run.compat != "ok": + run.fail("page sees a changed AudioContext: " + run.compat) + recover(run, 1) + run.stop() + + +def repeated(): + run = Run("repeated", "hostile", 1) + recover(run, 3) + run.stop() + + +def page_suspend(): + run = Run("page-suspend", "suspend", 1) + run.wait(lambda: run.states and run.states[-1][1] == "running suspended", 30, + "the page to suspend its second context") + killed = recover(run, 1) + after = [state for when, state in run.states if when >= killed] + if any(state.split()[1] != "suspended" for state in after): + run.fail("the context the page suspended was resumed: " + ", ".join(after)) + if run.states[-1][1] != "running suspended": + run.fail("states after the kill: " + run.states[-1][1]) + run.stop() + + +def two_sources(): + run = Run("two-sources", "plain", 2) + recover(run, 1) + run.stop() + + +scenarios = {"plain": plain, "repeated": repeated, "page-suspend": page_suspend, "two-sources": two_sources} +failed = [] +for name in args.scenario or scenarios: + try: + scenarios[name]() + print(name + ": passed") + except Failure as failure: + print(f"{name}: FAILED: {failure}") + failed.append(name) +if failed: + raise SystemExit("failed: " + ", ".join(failed)) From 654d2b9b6f6a193407e33970004cbdc39bf8c6a2 Mon Sep 17 00:00:00 2001 From: Ryan Doupe Date: Sat, 24 Oct 2026 23:13:15 -0400 Subject: [PATCH 2/2] Resume WebAudio after the audio service dies When Chromium's audio service process dies (a crash, or on Windows the hang monitor ending it), every running AudioContext fires "error" and is suspended. Nothing resumes it, so the source stays silent until the page reloads (obsproject/obs-studio#10586). OnContextCreated now runs a script in every frame before the page's own scripts. It wraps AudioContext (and webkitAudioContext, where present) in a Proxy, so pages still see the native constructor, prototype, name and methods, and watches each context's error event. A context the page wanted running is resumed; one the page suspended itself is left alone (suspend() is wrapped to track that). OfflineAudioContext is untouched. The script uses only what it captured before the page ran, so pages that replace or freeze globals do not break it. The first resume waits 3 s: with "Control audio via OBS", CEF keeps the dead capture stream until the page has been silent for 2 s, and a context resumed sooner keeps playing into it. Later attempts retry every 1 to 2 s. tests/audio_recovery.py: 4 of 4 scenarios pass; audio is back 3.1 to 3.2 s after each kill. Fixes obs-rust/obs-studio#2328 --- browser-app.cpp | 124 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 124 insertions(+) diff --git a/browser-app.cpp b/browser-app.cpp index 9aa2e90..1dcfe73 100644 --- a/browser-app.cpp +++ b/browser-app.cpp @@ -44,6 +44,125 @@ constexpr bool kIsPlatformWindows = true; constexpr bool kIsPlatformWindows = false; #endif +/* Runs in every frame before the page's own scripts. When Chromium's audio + * service process dies (a crash, or the hang monitor ending it), each + * running AudioContext fires "error" and is suspended, and nothing resumes + * it: the source stays silent until the page reloads. Resume such contexts, + * unless the page suspended them itself. Uses only what it captures here, + * so pages that later replace or freeze globals do not break it; the page + * still sees the native constructor, prototype and methods. */ +static const char audioContextRecoveryScript[] = R"js((() => { + 'use strict'; + const apply = Reflect.apply, construct = Reflect.construct; + const defineProperty = Object.defineProperty, getDescriptor = Object.getOwnPropertyDescriptor; + const getPrototype = Object.getPrototypeOf; + const addListener = EventTarget.prototype.addEventListener; + const then = Promise.prototype.then; + const later = setTimeout, global = globalThis; + const records = new WeakMap(), getRecord = WeakMap.prototype.get, setRecord = WeakMap.prototype.set; + const ProxyClass = Proxy; + const ignore = () => {}; + /* Wait 3 s before the first resume: with "Control audio via OBS", CEF + * keeps the capture stream of the dead audio service until the page has + * been silent for 2 s, and only captures again after that. */ + const delays = [3000, 1000, 2000, 2000, 2000, 2000, 2000, 2000]; + const wrapped = new Map(), getWrapped = Map.prototype.get, setWrapped = Map.prototype.set; + + function findGetter(object, name) { + for (; object; object = getPrototype(object)) { + const descriptor = getDescriptor(object, name); + if (descriptor) + return descriptor.get; + } + return undefined; + } + + function wrap(Native) { + const existing = apply(getWrapped, wrapped, [Native]); + if (existing) + return existing; + const prototype = Native.prototype; + const stateGetter = findGetter(prototype, 'state'); + const nativeResume = prototype.resume, nativeSuspend = prototype.suspend; + if (typeof stateGetter !== 'function' || typeof nativeResume !== 'function' || + typeof nativeSuspend !== 'function') + return Native; + const state = (context) => apply(stateGetter, context, []); + + /* One chain of attempts per error; a later error starts a new chain + * and ends the older one. */ + function attempt(context, record, chain, index) { + if (chain !== record.chain || !record.wanted || index >= delays.length) + return; + const current = state(context); + if (current === 'running' || current === 'closed') + return; + apply(then, apply(nativeResume, context, []), [ignore, ignore]); + schedule(context, record, chain, index + 1); + } + + function schedule(context, record, chain, index) { + if (index < delays.length) + apply(later, global, [() => attempt(context, record, chain, index), delays[index]]); + } + + function track(context) { + const record = {wanted: state(context) === 'running', pendingSuspends: 0, chain: 0}; + apply(setRecord, records, [context, record]); + apply(addListener, context, ['statechange', () => { + const current = state(context); + if (current === 'running' && !record.pendingSuspends) + record.wanted = true; + else if (current === 'closed') + record.wanted = false; + }]); + apply(addListener, context, ['error', () => { + if (record.wanted) + schedule(context, record, ++record.chain, 0); + }]); + } + + const Wrapped = new ProxyClass(Native, { + construct(target, args, newTarget) { + const context = construct(target, args, newTarget); + track(context); + return context; + }, + }); + const suspend = new ProxyClass(nativeSuspend, { + apply(target, self, args) { + const record = apply(getRecord, records, [self]); + const result = apply(target, self, args); + if (record) { + record.wanted = false; + record.pendingSuspends++; + const settled = () => record.pendingSuspends--; + apply(then, result, [settled, settled]); + } + return result; + }, + }); + const suspendDescriptor = getDescriptor(prototype, 'suspend'); + if (suspendDescriptor && suspendDescriptor.value === nativeSuspend) + defineProperty(prototype, 'suspend', {...suspendDescriptor, value: suspend}); + const constructorDescriptor = getDescriptor(prototype, 'constructor'); + if (constructorDescriptor && constructorDescriptor.value === Native) + defineProperty(prototype, 'constructor', {...constructorDescriptor, value: Wrapped}); + apply(setWrapped, wrapped, [Native, Wrapped]); + return Wrapped; + } + + for (const name of ['AudioContext', 'webkitAudioContext']) { + try { + const descriptor = getDescriptor(global, name); + if (descriptor && typeof descriptor.value === 'function') + defineProperty(global, name, {...descriptor, value: wrap(descriptor.value)}); + } catch (error) { + // Leave this name as the browser made it. + } + } +})();)js"; + CefRefPtr BrowserApp::GetRenderProcessHandler() { return this; @@ -199,6 +318,11 @@ void BrowserApp::OnContextCreated(CefRefPtr browser, CefRefPtrSetValue(name, func, V8_PROPERTY_ATTRIBUTE_NONE); } + /* If the script fails, the page keeps the browser's own AudioContext. */ + CefRefPtr result; + CefRefPtr exception; + context->Eval(audioContextRecoveryScript, CefString(), 0, result, exception); + UNUSED_PARAMETER(browser); }