Headless runs never open an audio device and stop after --max-seconds

This commit is contained in:
CRTkafa 2026-10-15 22:24:28 +08:00
parent 0b3f810df4
commit a34524bb4c
2 changed files with 24 additions and 3 deletions

View file

@ -549,8 +549,13 @@ static void register_java() {
java_impl(AT, "write", [](JCall& c) -> JVal { java_impl(AT, "write", [](JCall& c) -> JVal {
auto arr = jget(c.args[0].i); auto arr = jget(c.args[0].i);
uint32_t off = c.args[1].i, n = c.args[2].i; uint32_t off = c.args[1].i, n = c.args[2].i;
if (!g_audio || !arr) return JVal::I(n); if (!arr) return JVal::I(n);
uint32_t es = jelem_size(arr->elem); uint32_t es = jelem_size(arr->elem);
if (!g_audio) { // no device (headless): block for the buffer's play time, like a real AudioTrack
g_audio_bytes += n * es;
std::this_thread::sleep_for(std::chrono::microseconds((uint64_t)n * es * 1000000 / (g_audio_rate * g_audio_channels * 2)));
return JVal::I(n);
}
g_audio_bytes += n * es; g_audio_bytes += n * es;
{ {
// peak level of this buffer, for diagnostics // peak level of this buffer, for diagnostics

View file

@ -45,6 +45,7 @@ struct Options {
bool skip_video = false; bool skip_video = false;
std::string control_dir; std::string control_dir;
bool check_present = false; bool check_present = false;
int max_seconds = 600;
}; };
static uint32_t native(const char* name) { static uint32_t native(const char* name) {
@ -141,6 +142,7 @@ int main(int argc, char** argv) {
else if (a == "--skip-video") opt.skip_video = true; else if (a == "--skip-video") opt.skip_video = true;
else if (a == "--control") opt.control_dir = next(); else if (a == "--control") opt.control_dir = next();
else if (a == "--check-present") opt.check_present = true; else if (a == "--check-present") opt.check_present = true;
else if (a == "--max-seconds") opt.max_seconds = atoi(next().c_str());
else if (a == "--gl-trace") gl_set_trace(true); else if (a == "--gl-trace") gl_set_trace(true);
} }
// Locate the game data: --game, next to the app bundle, or next to the executable. // Locate the game data: --game, next to the app bundle, or next to the executable.
@ -224,8 +226,22 @@ int main(int argc, char** argv) {
// Audio: 44.1 kHz stereo 16-bit, fed by the game's AudioTrack.write calls. // Audio: 44.1 kHz stereo 16-bit, fed by the game's AudioTrack.write calls.
const int rate = 44100, channels = 2, frames = 1024; const int rate = 44100, channels = 2, frames = 1024;
SDL_AudioSpec spec{SDL_AUDIO_S16LE, channels, rate}; SDL_AudioSpec spec{SDL_AUDIO_S16LE, channels, rate};
SDL_AudioStream* audio = SDL_OpenAudioDeviceStream(SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK, &spec, nullptr, nullptr); // Headless runs never open an audio device (tests must be silent); AudioTrack.write is then paced by time.
if (!audio) logf("audio unavailable: %s", SDL_GetError()); SDL_AudioStream* audio = nullptr;
if (!opt.headless) {
audio = SDL_OpenAudioDeviceStream(SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK, &spec, nullptr, nullptr);
if (!audio) logf("audio unavailable: %s", SDL_GetError());
} else {
logf("headless: audio output disabled");
// a headless run can never outlive its test: hard stop after --max-seconds (default 10 minutes)
int limit = opt.max_seconds;
std::thread([limit] {
std::this_thread::sleep_for(std::chrono::seconds(limit));
logf("headless run reached --max-seconds %d, exiting", limit);
fflush(nullptr);
_exit(4);
}).detach();
}
jni_set_audio_stream(audio); jni_set_audio_stream(audio);
jni_audio_config(rate, channels, frames); jni_audio_config(rate, channels, frames);