Android port (Adreno): Vulkan backend, GameActivity app, touch HUD, lifecycle, minimap quest goals
- host/android: Vulkan replay backend (nvn_vk.c) with pipeline/descriptor caching, ADPF, AChoreographer present link; Linux compat layer; Makefile.android (headless dmhost + APK lib) - android/app: GameActivity shell without Gradle (build_apk.sh), AAudio output, touch HUD port (touch_app.c + TouchOverlay.java, three layers: body / joystick / minimap goals), lifecycle save-on-background like iOS, art_from_ipa.py for touch / HUD / mapgoal art - touch: shared touch controls moved from ios/Sources to dm/touch - patch_nso.py Android ruleset, dmsh_vk.py shader pack, DM_TOUCH_PRODUCT defaults - hle_audio: AAudio resume waits through STARTING and reopens once on failure - .gitignore: local resources/ (original game data) never committed Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
5bc5a01087
commit
4d91f80bae
+133
-1
@@ -7,7 +7,28 @@
|
||||
// iOS defaults to real audio output (the product must have sound), tests that want silence set DM_AUDIO=0 explicitly; Mac defaults to the silent sink (replay / gate checks are silent), DM_AUDIO=1 for real audio output.
|
||||
// The contents of AudioOut / AudioOutBuffer / AudioOutParameter are read and written only through the interfaces below; the host defines its own internal layout and only writes the front part of the structs.
|
||||
#include "hle.h"
|
||||
#if defined(__APPLE__)
|
||||
#include <AudioToolbox/AudioToolbox.h>
|
||||
#elif defined(__ANDROID__)
|
||||
// Android:出声端 = AAudio 流(数据回调拉同一条待播队列);句柄借 AudioUnit 这个名字,开 / 起 / 停 / 关见下方 au_open 一节
|
||||
// Android: the output = an AAudio stream (its data callback pulls the same pending queue); the handle borrows the AudioUnit name, open / start / stop / close are in the au_open section below
|
||||
#include <aaudio/AAudio.h>
|
||||
typedef AAudioStream *AudioUnit;
|
||||
typedef int OSStatus;
|
||||
static OSStatus AudioOutputUnitStart(AudioUnit au);
|
||||
static OSStatus AudioOutputUnitStop(AudioUnit au);
|
||||
#define AudioUnitUninitialize(au) 0
|
||||
#define AudioComponentInstanceDispose(au) AAudioStream_close(au)
|
||||
#else
|
||||
// Linux:真出声后端没接,只有静音接收端;AudioUnit 句柄恒空,下列调用不会走到
|
||||
// Linux: no real audio backend, only the silent sink; the AudioUnit handle is always null and the calls below are never reached
|
||||
typedef void *AudioUnit;
|
||||
typedef int OSStatus;
|
||||
#define AudioOutputUnitStart(au) 0
|
||||
#define AudioOutputUnitStop(au) 0
|
||||
#define AudioUnitUninitialize(au) 0
|
||||
#define AudioComponentInstanceDispose(au) 0
|
||||
#endif
|
||||
#include <TargetConditionals.h>
|
||||
#include <dispatch/dispatch.h>
|
||||
#include <stdatomic.h>
|
||||
@@ -27,6 +48,11 @@ typedef struct {
|
||||
gbuf *rel[RELMAX]; _Atomic uint32_t rel_head, rel_tail; // 已释放:出声端写,游戏侧读 | Released: written by the output side, read by the game side
|
||||
uint64_t pos; // 当前块已播字节 | Bytes already played of the current block
|
||||
AudioUnit au;
|
||||
#if defined(__ANDROID__)
|
||||
int out_ch; // 流的声道数:设备收原声道数就等于 ch,否则 2(回调里自己下混) | the stream's channel count: equal to ch when the device takes the original count, otherwise 2 (downmixed in the callback)
|
||||
int16_t *mix; // 下混用的暂存(ch 声道) | scratch for downmixing (ch channels)
|
||||
_Atomic int lost; // 设备断开(换耳机 / 蓝牙):等普通线程重开 | device disconnected (headphones / Bluetooth switch): waiting for an ordinary thread to reopen
|
||||
#endif
|
||||
pthread_t null_th; _Atomic int null_run;
|
||||
dispatch_semaphore_t notify_sem;
|
||||
pthread_t notify_th; _Atomic int notify_run, notify_pending, notify_armed;
|
||||
@@ -89,6 +115,7 @@ static void pull(aout *a, uint8_t *dst, uint64_t n) {
|
||||
|
||||
static _Atomic uint64_t g_render_calls; // 声卡回调累计(生命周期验证:恢复后须继续增长) | Cumulative sound card callbacks (lifecycle verification: must keep growing after resume)
|
||||
uint64_t dm_audio_render_calls(void) { return atomic_load_explicit(&g_render_calls, memory_order_relaxed); }
|
||||
#if defined(__APPLE__)
|
||||
static OSStatus au_render(void *ctx, AudioUnitRenderActionFlags *f, const AudioTimeStamp *ts, UInt32 bus, UInt32 frames, AudioBufferList *bl) {
|
||||
(void)f; (void)ts; (void)bus; (void)frames;
|
||||
aout *a = ctx;
|
||||
@@ -115,6 +142,97 @@ static void au_open(aout *a) {
|
||||
AudioUnitSetProperty(a->au, kAudioUnitProperty_SetRenderCallback, kAudioUnitScope_Input, 0, &cb, sizeof cb) ||
|
||||
AudioUnitInitialize(a->au)) die("AudioUnit 设置失败");
|
||||
}
|
||||
#elif defined(__ANDROID__)
|
||||
// AAudio:48 kHz PCM16,先按游戏的声道数(常见 6)开;设备不收就开双声道,回调里按 ITU 系数把 5.1 下混成立体声
|
||||
// AAudio: 48 kHz PCM16, opened first with the game's channel count (usually 6); if the device refuses, open stereo and downmix 5.1 to stereo with ITU coefficients in the callback
|
||||
enum { MIX_FRAMES = 1024 };
|
||||
static void downmix(const int16_t *in, int16_t *out, int frames, int ch) {
|
||||
for (int i = 0; i < frames; i++, in += ch, out += 2) {
|
||||
float l = in[0], r = ch > 1 ? in[1] : in[0];
|
||||
if (ch >= 6) { float c = in[2] * .7071f; l += c + in[4] * .7071f; r += c + in[5] * .7071f; } // FL FR C LFE SL SR;低音声道不混 | FL FR C LFE SL SR; the LFE channel is not mixed in
|
||||
else if (ch >= 4) { l += in[2] * .7071f; r += in[3] * .7071f; }
|
||||
if (ch >= 4) { l *= .5858f; r *= .5858f; } // 1/(1+.7071+.7071):不削波 | 1/(1+.7071+.7071): no clipping
|
||||
out[0] = (int16_t)(l > 32767 ? 32767 : l < -32768 ? -32768 : l); out[1] = (int16_t)(r > 32767 ? 32767 : r < -32768 ? -32768 : r);
|
||||
}
|
||||
}
|
||||
static aaudio_data_callback_result_t au_render(AAudioStream *st, void *ctx, void *data, int32_t frames) {
|
||||
(void)st; aout *a = ctx;
|
||||
atomic_fetch_add_explicit(&g_render_calls, 1, memory_order_relaxed);
|
||||
if (a->out_ch == a->ch) { pull(a, data, (uint64_t)frames * 2 * a->ch); return AAUDIO_CALLBACK_RESULT_CONTINUE; }
|
||||
int16_t *out = data;
|
||||
while (frames > 0) { int n = frames < MIX_FRAMES ? frames : MIX_FRAMES;
|
||||
pull(a, (uint8_t *)a->mix, (uint64_t)n * 2 * a->ch); downmix(a->mix, out, n, a->ch);
|
||||
out += n * 2; frames -= n; }
|
||||
return AAUDIO_CALLBACK_RESULT_CONTINUE;
|
||||
}
|
||||
static void au_reopen(aout *a);
|
||||
static void *null_sink(void *p);
|
||||
// 设备断开(拔耳机、切蓝牙):AAudio 的回调线程里不许关流重开,交给一条普通线程 | device disconnected (headphones unplugged, Bluetooth switched): the stream must not be closed / reopened on AAudio's callback thread, hand it to an ordinary thread
|
||||
static void *au_reopen_th(void *p) { au_reopen(p); return NULL; }
|
||||
static void au_error(AAudioStream *st, void *ctx, aaudio_result_t err) {
|
||||
(void)st; aout *a = ctx;
|
||||
LOG("【红】AAudio 出错 %d(%s):换设备重开", err, AAudio_convertResultToText(err));
|
||||
if (atomic_exchange(&a->lost, 1)) return;
|
||||
pthread_t t; if (!pthread_create(&t, NULL, au_reopen_th, a)) pthread_detach(t);
|
||||
}
|
||||
static AAudioStream *au_stream(aout *a, int ch, int low_latency) {
|
||||
AAudioStreamBuilder *b; AAudioStream *st = NULL;
|
||||
if (AAudio_createStreamBuilder(&b) != AAUDIO_OK) return NULL;
|
||||
AAudioStreamBuilder_setDirection(b, AAUDIO_DIRECTION_OUTPUT);
|
||||
AAudioStreamBuilder_setSharingMode(b, AAUDIO_SHARING_MODE_SHARED);
|
||||
AAudioStreamBuilder_setSampleRate(b, RATE); AAudioStreamBuilder_setChannelCount(b, ch);
|
||||
AAudioStreamBuilder_setFormat(b, AAUDIO_FORMAT_PCM_I16);
|
||||
AAudioStreamBuilder_setUsage(b, AAUDIO_USAGE_GAME); AAudioStreamBuilder_setContentType(b, AAUDIO_CONTENT_TYPE_MUSIC);
|
||||
AAudioStreamBuilder_setPerformanceMode(b, low_latency ? AAUDIO_PERFORMANCE_MODE_LOW_LATENCY : AAUDIO_PERFORMANCE_MODE_NONE);
|
||||
AAudioStreamBuilder_setDataCallback(b, au_render, a); AAudioStreamBuilder_setErrorCallback(b, au_error, a);
|
||||
if (AAudioStreamBuilder_openStream(b, &st) != AAUDIO_OK) st = NULL;
|
||||
AAudioStreamBuilder_delete(b);
|
||||
// 系统可能悄悄改格式(重采样到别的率、改声道):不是要的就不用 | the system may silently change the format (resample to another rate, change channels): reject it if it is not what was asked for
|
||||
if (st && (AAudioStream_getSampleRate(st) != RATE || AAudioStream_getChannelCount(st) != ch || AAudioStream_getFormat(st) != AAUDIO_FORMAT_PCM_I16)) { AAudioStream_close(st); st = NULL; }
|
||||
return st;
|
||||
}
|
||||
static int au_try_open(aout *a) {
|
||||
for (int lat = 1; lat >= 0; lat--) {
|
||||
if ((a->au = au_stream(a, a->ch, lat))) { a->out_ch = a->ch; break; }
|
||||
if (a->ch != 2 && (a->au = au_stream(a, 2, lat))) { a->out_ch = 2; break; }
|
||||
}
|
||||
if (!a->au) return 0;
|
||||
if (a->out_ch != a->ch && !a->mix) a->mix = malloc((size_t)MIX_FRAMES * 2 * a->ch);
|
||||
int burst = AAudioStream_getFramesPerBurst(a->au);
|
||||
if (burst > 0) AAudioStream_setBufferSizeInFrames(a->au, burst * 4); // 4 个突发 ≈ 几毫秒:游戏自己有 FMOD 缓冲,这里少留防爆音 | 4 bursts ≈ a few ms: the game has its own FMOD buffering, keep a little here against crackles
|
||||
LOG("AAudio 打开:48 kHz PCM16 声道 %d(游戏 %d%s)突发 %d 帧 性能模式 %d", a->out_ch, a->ch, a->out_ch != a->ch ? ",下混" : "", burst, AAudioStream_getPerformanceMode(a->au));
|
||||
return 1;
|
||||
}
|
||||
static void au_open(aout *a) { if (!au_try_open(a)) LOG("【红】AAudio 打不开:用静音接收端"); }
|
||||
static OSStatus au_wait(AudioUnit au, aaudio_stream_state_t want) {
|
||||
aaudio_stream_state_t st = AAudioStream_getState(au), next;
|
||||
for (int i = 0; i < 10 && st != want; i++) { // 超时不算失败(回前台时 STARTING 常要一百多毫秒):最多等 1 秒 | a timeout is not a failure (STARTING often takes 100+ ms when returning to foreground): wait up to 1 s
|
||||
aaudio_result_t r = AAudioStream_waitForStateChange(au, st, &next, 100000000);
|
||||
if (r == AAUDIO_OK) st = next; else if (r == AAUDIO_ERROR_TIMEOUT) st = AAudioStream_getState(au); else break; }
|
||||
return st == want ? 0 : -1;
|
||||
}
|
||||
static OSStatus AudioOutputUnitStart(AudioUnit au) {
|
||||
aaudio_result_t r = AAudioStream_requestStart(au); OSStatus st = r == AAUDIO_OK ? au_wait(au, AAUDIO_STREAM_STATE_STARTED) : -1;
|
||||
if (st) LOG("AAudio 启动失败:%s,流状态 %s", AAudio_convertResultToText(r), AAudio_convertStreamStateToText(AAudioStream_getState(au)));
|
||||
return st;
|
||||
}
|
||||
// 停要等回调真停:之后静音接收端接手同一条队列,两边不能同时拉 | stopping waits until callbacks have really stopped: afterwards the silent sink takes over the same queue, the two must not pull at once
|
||||
static OSStatus AudioOutputUnitStop(AudioUnit au) { return AAudioStream_requestStop(au) == AAUDIO_OK ? au_wait(au, AAUDIO_STREAM_STATE_STOPPED) : -1; }
|
||||
static void au_reopen(aout *a) {
|
||||
pthread_mutex_lock(&g_aos_lock);
|
||||
int live = 0; for (int i = 0; i < AOMAX; i++) live |= g_aos[i] == a;
|
||||
if (live && a->au) {
|
||||
AAudioStream_close(a->au); a->au = NULL; // 断开的流回调已停 | callbacks of a disconnected stream have stopped
|
||||
if (!au_try_open(a)) { LOG("【红】AAudio 重开失败:改用静音接收端");
|
||||
if (atomic_load(&a->state) == STATE_STARTED && !g_suspended) { atomic_store(&a->null_run, 1); pthread_create(&a->null_th, NULL, null_sink, a); } }
|
||||
else if (atomic_load(&a->state) == STATE_STARTED && !g_suspended && AudioOutputUnitStart(a->au)) LOG("【红】AAudio 重开后启动失败");
|
||||
}
|
||||
atomic_store(&a->lost, 0);
|
||||
pthread_mutex_unlock(&g_aos_lock);
|
||||
}
|
||||
#else
|
||||
static void au_open(aout *a) { (void)a; die("Linux 宿主还没有真出声后端:用 DM_AUDIO=0(静音接收端)"); }
|
||||
#endif
|
||||
// 静音接收端:每 5 ms 按 48 kHz 实时消耗
|
||||
// Silent sink: consumes in real time at 48 kHz every 5 ms
|
||||
static void *null_sink(void *p) {
|
||||
@@ -126,7 +244,12 @@ static void *null_sink(void *p) {
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
static int real_audio(void) { const char *e = getenv("DM_AUDIO"); return e ? atoi(e) != 0 : TARGET_OS_IPHONE; }
|
||||
#if defined(__ANDROID__)
|
||||
#define DM_AUDIO_DEFAULT 1 // Android App 同 iOS:缺省真出声 | Android app, like iOS: real audio by default
|
||||
#else
|
||||
#define DM_AUDIO_DEFAULT TARGET_OS_IPHONE
|
||||
#endif
|
||||
static int real_audio(void) { const char *e = getenv("DM_AUDIO"); return e ? atoi(e) != 0 : DM_AUDIO_DEFAULT; }
|
||||
|
||||
// AudioOutParameter = { int32 sampleRate; uint16 channelCount; uint16 _r; ... },0 = 系统默认(48 kHz / 2 声道)
|
||||
// AudioOutParameter = { int32 sampleRate; uint16 channelCount; uint16 _r; ... }, 0 = system default (48 kHz / 2 channels)
|
||||
@@ -187,6 +310,9 @@ HLE_FORCE("_ZN2nn5audio13CloseAudioOutEPNS0_8AudioOutE", "关闭音频输出", v
|
||||
for (int i = 0; i < AOMAX; i++) if (g_aos[i] == a) g_aos[i] = NULL;
|
||||
pthread_mutex_unlock(&g_aos_lock);
|
||||
if (a->au) { AudioUnitUninitialize(a->au); AudioComponentInstanceDispose(a->au); }
|
||||
#if defined(__ANDROID__)
|
||||
free(a->mix);
|
||||
#endif
|
||||
dispatch_release(a->notify_sem);
|
||||
pthread_mutex_destroy(&a->m); free(a);
|
||||
}
|
||||
@@ -242,6 +368,12 @@ void dm_audio_suspend(int on) {
|
||||
if (on) { AudioOutputUnitStop(a->au); atomic_store_explicit(&a->null_run, 1, memory_order_release); pthread_create(&a->null_th, NULL, null_sink, a); }
|
||||
else { atomic_store_explicit(&a->null_run, 0, memory_order_release); pthread_join(a->null_th, NULL);
|
||||
OSStatus st = AudioOutputUnitStart(a->au);
|
||||
#if defined(__ANDROID__)
|
||||
if (st) { // 后台期间流被系统收走(断开 / 起不来):关掉重开一次再起 | the stream was taken away by the system while in the background (disconnected / won't start): close, reopen once and start again
|
||||
AAudioStream_close(a->au); a->au = NULL;
|
||||
st = au_try_open(a) ? AudioOutputUnitStart(a->au) : -1;
|
||||
LOG("AAudio 回前台重开%s", st ? "仍失败" : "后起来了"); }
|
||||
#endif
|
||||
if (st) { // 会话还被别人占着(通话没挂就切回游戏、打断结束时人还在别的 App):不致命,继续由静音接收端顶着,等下一次恢复再试 | The session is still held by someone else (switched back to the game with a call not yet hung up, or the user is still in another app when the interruption ends): not fatal, the silent sink keeps covering, and we retry at the next resume
|
||||
LOG("【红】AudioUnit 恢复失败(状态 %d):保持静音接收端,等下次恢复重试", (int)st);
|
||||
atomic_store_explicit(&a->null_run, 1, memory_order_release); pthread_create(&a->null_th, NULL, null_sink, a); failed = 1; } }
|
||||
|
||||
Reference in New Issue
Block a user