Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
124 changes: 124 additions & 0 deletions browser-app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<CefRenderProcessHandler> BrowserApp::GetRenderProcessHandler()
{
return this;
Expand Down Expand Up @@ -199,6 +318,11 @@ void BrowserApp::OnContextCreated(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFr
obsStudioObj->SetValue(name, func, V8_PROPERTY_ATTRIBUTE_NONE);
}

/* If the script fails, the page keeps the browser's own AudioContext. */
CefRefPtr<CefV8Value> result;
CefRefPtr<CefV8Exception> exception;
context->Eval(audioContextRecoveryScript, CefString(), 0, result, exception);

UNUSED_PARAMETER(browser);
}

Expand Down
24 changes: 24 additions & 0 deletions tests/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
131 changes: 131 additions & 0 deletions tests/audio-recovery.c
Original file line number Diff line number Diff line change
@@ -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 <n0> <n1> ..." (cumulative) every 100 ms and forwards the
* OBS log (including the page's console.error lines) as "LOG <line>".
* Runs until stdin is closed or a line is read. */
#include <obs.h>
#include <obs-nix-platform.h>
#include <util/platform.h>
#include <X11/Xlib.h>
#include <poll.h>
#include <pthread.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#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;
}
Loading
Loading