mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-29 17:45:41 +00:00
Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
14762b07d4 |
No files matched your search
@@ -148,7 +148,6 @@ struct slot {
|
||||
uint32_t fmt;
|
||||
int release_fd;
|
||||
int busy;
|
||||
uint64_t generation;
|
||||
};
|
||||
|
||||
struct layer {
|
||||
@@ -176,7 +175,6 @@ struct layer {
|
||||
int votes_rate;
|
||||
float fps_applied;
|
||||
int recreate_pending;
|
||||
int pending_callbacks;
|
||||
|
||||
int32_t ds_applied;
|
||||
|
||||
@@ -224,7 +222,6 @@ struct complete_ctx {
|
||||
ASurfaceControl *sc;
|
||||
int layer;
|
||||
int prev_slot;
|
||||
uint64_t prev_generation;
|
||||
void *prev_token;
|
||||
int retire;
|
||||
};
|
||||
@@ -291,16 +288,12 @@ static void on_complete(void *context, ASurfaceTransactionStats *stats) {
|
||||
pthread_mutex_lock(&g_lock);
|
||||
if (ctx->layer >= 0 && ctx->layer < SC_LAYER_COUNT && ctx->prev_slot >= 0 && ctx->prev_slot < POOL_MAX) {
|
||||
struct slot *s = &g_layers[ctx->layer].slots[ctx->prev_slot];
|
||||
if (s->generation == ctx->prev_generation) {
|
||||
int fd = (stats && ctx->sc) ? api.prevReleaseFence(stats, ctx->sc) : -1;
|
||||
if (s->release_fd >= 0) close(s->release_fd);
|
||||
s->release_fd = fd;
|
||||
s->busy = 0;
|
||||
}
|
||||
int fd = (stats && ctx->sc) ? api.prevReleaseFence(stats, ctx->sc) : -1;
|
||||
if (s->release_fd >= 0) close(s->release_fd);
|
||||
s->release_fd = fd;
|
||||
s->busy = 0;
|
||||
}
|
||||
if (g_pending_cb > 0) g_pending_cb--;
|
||||
if (ctx->layer >= 0 && ctx->layer < SC_LAYER_COUNT && g_layers[ctx->layer].pending_callbacks > 0)
|
||||
g_layers[ctx->layer].pending_callbacks--;
|
||||
pthread_mutex_unlock(&g_lock);
|
||||
if (ctx->prev_token) {
|
||||
int fd = (stats && ctx->sc) ? api.prevReleaseFence(stats, ctx->sc) : -1;
|
||||
@@ -315,15 +308,9 @@ static int add_complete_on(ASurfaceTransaction *tx, struct layer *l, ASurfaceCon
|
||||
struct complete_ctx *ctx = calloc(1, sizeof(*ctx));
|
||||
if (!ctx) return -1;
|
||||
ctx->sc = sc; ctx->layer = (int)(l - g_layers); ctx->prev_slot = prev_slot;
|
||||
if (prev_slot >= 0 && prev_slot < POOL_MAX) {
|
||||
pthread_mutex_lock(&g_lock);
|
||||
ctx->prev_generation = l->slots[prev_slot].generation;
|
||||
pthread_mutex_unlock(&g_lock);
|
||||
}
|
||||
ctx->prev_token = prev_token; ctx->retire = retire;
|
||||
pthread_mutex_lock(&g_lock);
|
||||
g_pending_cb++;
|
||||
l->pending_callbacks++;
|
||||
pthread_mutex_unlock(&g_lock);
|
||||
api.setOnComplete(tx, ctx, on_complete);
|
||||
return 0;
|
||||
@@ -410,16 +397,12 @@ static void retire_sc(struct layer *l) {
|
||||
ASurfaceTransaction *tx = api.txCreate();
|
||||
if (tx) {
|
||||
retire_ops(tx, l->sc);
|
||||
if (add_complete(tx, l, l->cur_slot, l->cur_token, 1) != 0) {
|
||||
api.txDelete(tx);
|
||||
droiddeck_log("layer", "%s: retirement deferred because its release callback could not be allocated", l->name);
|
||||
return;
|
||||
}
|
||||
if (add_complete(tx, l, l->cur_slot, l->cur_token, 1) != 0) api.release(l->sc);
|
||||
api.txApply(tx);
|
||||
api.txDelete(tx);
|
||||
} else {
|
||||
droiddeck_log("layer", "%s: retirement deferred because a transaction could not be allocated", l->name);
|
||||
return;
|
||||
if (l->cur_token) ahb_swapchain_layer_released(l->cur_token, -1);
|
||||
api.release(l->sc);
|
||||
}
|
||||
droiddeck_log("layer", "%s: SurfaceControl retired (window %p)", l->name, (void *)l->win);
|
||||
l->sc = NULL; l->win = NULL;
|
||||
@@ -438,28 +421,20 @@ static void drain_and_free_pools(void) {
|
||||
if (!pending || now_ns() > deadline) break;
|
||||
usleep(2000);
|
||||
}
|
||||
pthread_mutex_lock(&g_lock);
|
||||
for (int i = 0; i < SC_LAYER_COUNT; i++) {
|
||||
struct layer *l = &g_layers[i];
|
||||
pthread_mutex_lock(&g_lock);
|
||||
const int pending = l->pending_callbacks;
|
||||
if (pending || l->sc) {
|
||||
pthread_mutex_unlock(&g_lock);
|
||||
droiddeck_log("layer", "pool drain deferred for %s (%s)", l->name,
|
||||
pending ? "SurfaceControl callbacks are pending" : "SurfaceControl retirement is deferred");
|
||||
continue;
|
||||
}
|
||||
for (int j = 0; j < POOL_MAX; j++) {
|
||||
struct slot *s = &l->slots[j];
|
||||
struct slot *s = &g_layers[i].slots[j];
|
||||
if (s->release_fd >= 0) { struct pollfd p = {.fd = s->release_fd, .events = POLLIN}; poll(&p, 1, 100); close(s->release_fd); }
|
||||
if (s->img) vkp_image_destroy(s->img);
|
||||
if (s->ahb) AHardwareBuffer_release(s->ahb);
|
||||
memset(s, 0, sizeof(*s));
|
||||
s->release_fd = -1;
|
||||
}
|
||||
l->first_logged = 0;
|
||||
l->logged_w = l->logged_h = 0;
|
||||
pthread_mutex_unlock(&g_lock);
|
||||
g_layers[i].first_logged = 0;
|
||||
g_layers[i].logged_w = g_layers[i].logged_h = 0;
|
||||
}
|
||||
pthread_mutex_unlock(&g_lock);
|
||||
}
|
||||
|
||||
static int sniff_modifier(const struct droiddeck_native_handle *h, uint64_t *mod) {
|
||||
@@ -637,10 +612,7 @@ static int take_free_slot(struct layer *l, int w, int h, uint32_t fmt, int *wait
|
||||
if (pending < 0) pending = i;
|
||||
}
|
||||
if (idx < 0) idx = pending;
|
||||
if (idx >= 0) {
|
||||
fd = l->slots[idx].release_fd; l->slots[idx].release_fd = -1;
|
||||
l->slots[idx].generation++;
|
||||
}
|
||||
if (idx >= 0) { fd = l->slots[idx].release_fd; l->slots[idx].release_fd = -1; }
|
||||
pthread_mutex_unlock(&g_lock);
|
||||
if (idx < 0) return -1;
|
||||
if (fd >= 0 && fence_signalled(fd)) { close(fd); fd = -1; }
|
||||
@@ -648,8 +620,7 @@ static int take_free_slot(struct layer *l, int w, int h, uint32_t fmt, int *wait
|
||||
if (s->ahb && (s->w != w || s->h != h || s->fmt != fmt)) {
|
||||
if (fd >= 0) { close(fd); fd = -1; }
|
||||
vkp_image_destroy(s->img); AHardwareBuffer_release(s->ahb);
|
||||
const uint64_t generation = s->generation;
|
||||
memset(s, 0, sizeof(*s)); s->release_fd = -1; s->generation = generation;
|
||||
memset(s, 0, sizeof(*s)); s->release_fd = -1;
|
||||
}
|
||||
if (fd >= 0 && !vkp_can_wait_sync_fd()) {
|
||||
if (fence_wait(fd, 100) == 0) { slot_keep_fence(s, fd); return -1; }
|
||||
@@ -688,13 +659,7 @@ static int ensure_sc(struct layer *l) {
|
||||
if (load_api() != 0) return -1;
|
||||
ANativeWindow *win = vkp_window();
|
||||
if (!win) return -1;
|
||||
if (l->sc && l->win != win) {
|
||||
retire_sc(l);
|
||||
if (l->sc) {
|
||||
droiddeck_log("layer", "%s: keeping the old SurfaceControl until retirement completes", l->name);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
if (l->sc && l->win != win) retire_sc(l);
|
||||
if (!l->sc) {
|
||||
l->sc = api.createFromWindow(win, l->name);
|
||||
if (!l->sc) { droiddeck_log("error", "layer: ASurfaceControl_createFromWindow(%s) failed", l->name); return -1; }
|
||||
@@ -853,7 +818,6 @@ static ASurfaceControl *swap_sc_begin(struct layer *l) {
|
||||
if (!win || win != l->win) return NULL;
|
||||
ASurfaceControl *fresh = api.createFromWindow(win, l->name);
|
||||
if (!fresh) {
|
||||
l->recreate_pending = 1;
|
||||
droiddeck_log("error", "layer: %s: composition recovery could not create a new SurfaceControl", l->name);
|
||||
return NULL;
|
||||
}
|
||||
@@ -868,45 +832,24 @@ static ASurfaceControl *swap_sc_begin(struct layer *l) {
|
||||
return old;
|
||||
}
|
||||
|
||||
static void rollback_swap_sc(struct layer *l, ASurfaceControl *old, int was_shown,
|
||||
int32_t ds_applied, uint32_t md_applied, float hr_applied) {
|
||||
if (!old) return;
|
||||
ASurfaceControl *fresh = l->sc;
|
||||
l->sc = old;
|
||||
l->shown = was_shown;
|
||||
l->recreate_pending = 1;
|
||||
l->geo_valid = 0;
|
||||
l->fps_applied = -1.0f;
|
||||
l->ds_applied = ds_applied; l->md_applied = md_applied; l->hr_applied = hr_applied;
|
||||
if (fresh) api.release(fresh);
|
||||
}
|
||||
|
||||
static int present_slot(struct layer *l, int idx, const int r[8], const struct droiddeck_color *color,
|
||||
int translucent) {
|
||||
struct slot *s = &l->slots[idx];
|
||||
ASurfaceTransaction *tx = api.txCreate();
|
||||
if (!tx) return -1;
|
||||
const int was_shown = l->shown;
|
||||
const int32_t ds_applied = l->ds_applied;
|
||||
const uint32_t md_applied = l->md_applied;
|
||||
const float hr_applied = l->hr_applied;
|
||||
ASurfaceControl *old = swap_sc_begin(l);
|
||||
if (old) api.setZOrder(tx, l->sc, l->z);
|
||||
api.setBuffer(tx, l->sc, s->ahb, -1);
|
||||
if (api.setEnableBackPressure) api.setEnableBackPressure(tx, l->sc, true);
|
||||
api.setBufferTransparency(tx, l->sc,
|
||||
translucent ? ASC_TRANSPARENCY_TRANSLUCENT : ASC_TRANSPARENCY_OPAQUE);
|
||||
if (!l->shown) api.setVisibility(tx, l->sc, ASC_VISIBILITY_SHOW);
|
||||
if (old) retire_ops(tx, old);
|
||||
if (add_complete_on(tx, l, old ? old : l->sc, l->cur_slot, l->cur_token, old ? 1 : 0) != 0) {
|
||||
api.txDelete(tx);
|
||||
rollback_swap_sc(l, old, was_shown, ds_applied, md_applied, hr_applied);
|
||||
droiddeck_log("layer", "%s: frame transition skipped because its release callback could not be allocated", l->name);
|
||||
return -1;
|
||||
}
|
||||
apply_geometry(tx, l, r);
|
||||
apply_frame_rate(tx, l);
|
||||
apply_colour(tx, l, color);
|
||||
if (!l->shown) api.setVisibility(tx, l->sc, ASC_VISIBILITY_SHOW);
|
||||
if (old) retire_ops(tx, old);
|
||||
if (add_complete_on(tx, l, old ? old : l->sc, l->cur_slot, l->cur_token, old ? 1 : 0) != 0 && old)
|
||||
api.release(old);
|
||||
pthread_mutex_lock(&g_lock);
|
||||
s->busy = 1;
|
||||
pthread_mutex_unlock(&g_lock);
|
||||
@@ -961,27 +904,23 @@ int sc_layer_present_ahb(AHardwareBuffer *ahb, int w, int h, int acquire_fd, voi
|
||||
}
|
||||
ASurfaceTransaction *tx = api.txCreate();
|
||||
if (!tx) goto unavailable;
|
||||
const int was_shown = l->shown;
|
||||
const int32_t ds_applied = l->ds_applied;
|
||||
const uint32_t md_applied = l->md_applied;
|
||||
const float hr_applied = l->hr_applied;
|
||||
ASurfaceControl *old = swap_sc_begin(l);
|
||||
if (old) api.setZOrder(tx, l->sc, l->z);
|
||||
api.setBuffer(tx, l->sc, ahb, acquire_fd);
|
||||
if (api.setEnableBackPressure) api.setEnableBackPressure(tx, l->sc, true);
|
||||
api.setBufferTransparency(tx, l->sc, ASC_TRANSPARENCY_OPAQUE);
|
||||
apply_geometry(tx, l, r);
|
||||
apply_frame_rate(tx, l);
|
||||
apply_colour(tx, l, color);
|
||||
if (!l->shown) api.setVisibility(tx, l->sc, ASC_VISIBILITY_SHOW);
|
||||
if (old) retire_ops(tx, old);
|
||||
if (add_complete_on(tx, l, old ? old : l->sc, l->cur_slot,
|
||||
l->cur_token == token ? NULL : l->cur_token, old ? 1 : 0) != 0) {
|
||||
api.txDelete(tx);
|
||||
rollback_swap_sc(l, old, was_shown, ds_applied, md_applied, hr_applied);
|
||||
droiddeck_log("layer", "zero-copy: frame transition skipped because its release callback could not be allocated");
|
||||
if (old) api.release(old);
|
||||
l->ds_applied = -1; l->md_applied = 0; l->hr_applied = -1.0f;
|
||||
return -1;
|
||||
}
|
||||
apply_geometry(tx, l, r);
|
||||
apply_frame_rate(tx, l);
|
||||
apply_colour(tx, l, color);
|
||||
apply_present_time(tx, 1);
|
||||
api.txApply(tx);
|
||||
api.txDelete(tx);
|
||||
@@ -1228,26 +1167,15 @@ int sc_layer_present_overlay(struct vkp_image *src, const int geo[8], int transl
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int hide_layer(struct layer *l) {
|
||||
if (!l->sc || !l->shown || api.state != 1) return 0;
|
||||
static void hide_layer(struct layer *l) {
|
||||
if (!l->sc || !l->shown || api.state != 1) return;
|
||||
ASurfaceTransaction *tx = api.txCreate();
|
||||
if (!tx) return -1;
|
||||
api.setVisibility(tx, l->sc, ASC_VISIBILITY_HIDE);
|
||||
if (add_complete(tx, l, l->cur_slot, l->cur_token, 0) != 0) {
|
||||
/*
|
||||
* Keep the current buffer attached when its release callback cannot
|
||||
* be allocated. The hide can still apply without releasing the slot.
|
||||
*/
|
||||
api.txApply(tx);
|
||||
api.txDelete(tx);
|
||||
l->shown = 0;
|
||||
droiddeck_log("layer", "%s: hidden; buffer release deferred because its callback could not be allocated", l->name);
|
||||
return 0;
|
||||
}
|
||||
if (!tx) return;
|
||||
replace_with_blank(tx, l);
|
||||
api.setVisibility(tx, l->sc, ASC_VISIBILITY_HIDE);
|
||||
add_complete(tx, l, l->cur_slot, l->cur_token, 0);
|
||||
api.txApply(tx); api.txDelete(tx);
|
||||
l->shown = 0; l->cur_slot = -1; l->cur_token = NULL;
|
||||
return 0;
|
||||
}
|
||||
|
||||
void sc_layer_hide(void) {
|
||||
@@ -1255,20 +1183,17 @@ void sc_layer_hide(void) {
|
||||
int said = g_layers[SC_LAYER_GAME].shown || g_layers[SC_LAYER_OVERLAY].sc;
|
||||
if (g_layers[SC_LAYER_OVERLAY].sc) sc_layer_hide_overlay();
|
||||
if (g_layers[SC_LAYER_GAME].shown) hide_layer(&g_layers[SC_LAYER_GAME]);
|
||||
if (said && !g_layers[SC_LAYER_GAME].shown && !g_layers[SC_LAYER_OVERLAY].shown)
|
||||
droiddeck_log("layer", "layers hidden (scene is not a single fullscreen window)");
|
||||
if (said) droiddeck_log("layer", "layers hidden (scene is not a single fullscreen window)");
|
||||
}
|
||||
|
||||
void sc_layer_hide_overlay(void) {
|
||||
layers_init();
|
||||
struct layer *l = &g_layers[SC_LAYER_OVERLAY];
|
||||
if (!l->sc) return;
|
||||
if (l->shown && hide_layer(l) != 0) return;
|
||||
if (l->shown) hide_layer(l);
|
||||
retire_sc(l);
|
||||
if (!l->sc) {
|
||||
droiddeck_log("layer", "%s: gone (nothing is above the game any more)", l->name);
|
||||
if (g_layers[SC_LAYER_GAME].sc) g_layers[SC_LAYER_GAME].recreate_pending = 1;
|
||||
}
|
||||
droiddeck_log("layer", "%s: gone (nothing is above the game any more)", l->name);
|
||||
if (g_layers[SC_LAYER_GAME].sc) g_layers[SC_LAYER_GAME].recreate_pending = 1;
|
||||
}
|
||||
|
||||
void sc_layer_window_gone(void) {
|
||||
|
||||
@@ -18,15 +18,18 @@ public class DirectAudioRelayComponent extends SessionPart {
|
||||
private File logFile;
|
||||
private final File micFifoPath;
|
||||
private volatile int pid = -1;
|
||||
private final Object micLock = new Object();
|
||||
private final Object stateLock = new Object();
|
||||
private boolean micHot;
|
||||
private boolean outputStopped;
|
||||
private final File micControl;
|
||||
private final File outputControl;
|
||||
public java.util.function.Consumer<Boolean> onMicEnabled;
|
||||
|
||||
public DirectAudioRelayComponent(File socketPath, File micFifoPath) {
|
||||
this.socketPath = socketPath;
|
||||
this.micFifoPath = micFifoPath;
|
||||
this.micControl = micFifoPath == null ? null : new File(micFifoPath.getParentFile(), "mic-enabled");
|
||||
this.outputControl = new File(socketPath.getParentFile(), "output-enabled");
|
||||
}
|
||||
|
||||
public void setLogFile(File file) {
|
||||
@@ -36,9 +39,11 @@ public class DirectAudioRelayComponent extends SessionPart {
|
||||
@Override
|
||||
public void start() {
|
||||
stop();
|
||||
synchronized (micLock) {
|
||||
synchronized (stateLock) {
|
||||
micHot = false;
|
||||
outputStopped = false;
|
||||
}
|
||||
outputControl.delete();
|
||||
File binary = new File(app().getApplicationInfo().nativeLibraryDir, BINARY);
|
||||
if (!binary.isFile()) {
|
||||
Log.w(TAG, "helper missing at " + binary + "; DirectAudio will report itself unavailable");
|
||||
@@ -67,6 +72,7 @@ public class DirectAudioRelayComponent extends SessionPart {
|
||||
StringBuilder command = new StringBuilder(binary.getAbsolutePath());
|
||||
command.append(" --socket ").append(socketPath.getAbsolutePath());
|
||||
command.append(" --log");
|
||||
command.append(" --output-control ").append(outputControl.getAbsolutePath());
|
||||
final boolean withMic = micFifoPath != null && setMicEnabled(true);
|
||||
if (withMic) {
|
||||
command.append(" --mic-fifo ").append(micFifoPath.getAbsolutePath());
|
||||
@@ -93,6 +99,7 @@ public class DirectAudioRelayComponent extends SessionPart {
|
||||
app().getFilesDir(), null,
|
||||
line -> {
|
||||
Log.i(TAG, line);
|
||||
trackOutput(line);
|
||||
if (withMic) trackMic(line);
|
||||
if (log != null) synchronized (log) { log.println(line); log.flush(); }
|
||||
});
|
||||
@@ -101,9 +108,27 @@ public class DirectAudioRelayComponent extends SessionPart {
|
||||
}
|
||||
|
||||
public synchronized boolean setMicEnabled(boolean enabled) {
|
||||
if (micControl == null) return false;
|
||||
File next = new File(micControl.getPath() + ".new");
|
||||
return FileUtils.writeString(next, enabled ? "1" : "0") && next.renameTo(micControl);
|
||||
return micControl != null && writeControl(micControl, enabled);
|
||||
}
|
||||
|
||||
public boolean setOutputEnabled(boolean enabled) {
|
||||
synchronized (stateLock) {
|
||||
outputStopped = false;
|
||||
}
|
||||
return writeControl(outputControl, enabled);
|
||||
}
|
||||
|
||||
private static boolean writeControl(File control, boolean enabled) {
|
||||
File next = new File(control.getPath() + ".new");
|
||||
return FileUtils.writeString(next, enabled ? "1" : "0") && next.renameTo(control);
|
||||
}
|
||||
|
||||
private void trackOutput(String line) {
|
||||
if (!line.equals("DA-Relay: output enabled=0")) return;
|
||||
synchronized (stateLock) {
|
||||
outputStopped = true;
|
||||
stateLock.notifyAll();
|
||||
}
|
||||
}
|
||||
|
||||
private void trackMic(String line) {
|
||||
@@ -112,20 +137,28 @@ public class DirectAudioRelayComponent extends SessionPart {
|
||||
}
|
||||
boolean start = line.startsWith("DA-Relay: mic start ");
|
||||
if (!start && !line.startsWith("DA-Relay: mic stop ")) return;
|
||||
synchronized (micLock) {
|
||||
synchronized (stateLock) {
|
||||
micHot = start;
|
||||
micLock.notifyAll();
|
||||
stateLock.notifyAll();
|
||||
}
|
||||
}
|
||||
|
||||
public boolean awaitMicReleased(long timeoutMs) {
|
||||
return await(() -> !micHot, timeoutMs);
|
||||
}
|
||||
|
||||
public boolean awaitOutputStopped(long timeoutMs) {
|
||||
return pid == -1 || await(() -> outputStopped, timeoutMs);
|
||||
}
|
||||
|
||||
private boolean await(java.util.function.BooleanSupplier done, long timeoutMs) {
|
||||
long deadline = System.currentTimeMillis() + timeoutMs;
|
||||
synchronized (micLock) {
|
||||
while (micHot) {
|
||||
synchronized (stateLock) {
|
||||
while (!done.getAsBoolean()) {
|
||||
long left = deadline - System.currentTimeMillis();
|
||||
if (left <= 0) return false;
|
||||
try {
|
||||
micLock.wait(left);
|
||||
stateLock.wait(left);
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
return false;
|
||||
|
||||
@@ -903,16 +903,23 @@ class SessionService : Service() {
|
||||
listOfNotNull(BottomDesktop.suspendPid.takeIf { it > 1 })
|
||||
},
|
||||
suspendAudio = {
|
||||
val relay = components.firstNotNullOfOrNull { it as? DirectAudioRelayComponent }
|
||||
val relayOutputStopping = relay?.setOutputEnabled(false)
|
||||
if (relayOutputStopping == false) Log.w(TAG, "could not stop DirectAudio output")
|
||||
if (!pulse.setSinkSuspended(true)) Log.w(TAG, "could not suspend audio sink")
|
||||
if (!pulse.setMicSuspended(true)) Log.w(TAG, "could not suspend microphone source")
|
||||
val relay = components.firstNotNullOfOrNull { it as? DirectAudioRelayComponent }
|
||||
if (relay != null && !relay.awaitMicReleased(MIC_RELEASE_TIMEOUT_MS)) {
|
||||
Log.w(TAG, "microphone still open after ${MIC_RELEASE_TIMEOUT_MS} ms; suspending anyway")
|
||||
}
|
||||
if (relay != null && relayOutputStopping == true && !relay.awaitOutputStopped(OUTPUT_RELEASE_TIMEOUT_MS)) {
|
||||
Log.w(TAG, "DirectAudio output still started after ${OUTPUT_RELEASE_TIMEOUT_MS} ms; suspending anyway")
|
||||
}
|
||||
},
|
||||
resumeAudio = {
|
||||
if (!pulse.setSinkSuspended(false)) Log.w(TAG, "could not resume audio sink")
|
||||
if (!pulse.setMicSuspended(false)) Log.w(TAG, "could not resume microphone source")
|
||||
val relay = components.firstNotNullOfOrNull { it as? DirectAudioRelayComponent }
|
||||
if (relay?.setOutputEnabled(true) == false) Log.w(TAG, "could not resume DirectAudio output")
|
||||
},
|
||||
zramControl = File(filesDir, "${LinuxRuntime.DIR}/tmp/.dd-zram"),
|
||||
clockControl = File(filesDir, "${LinuxRuntime.DIR}/tmp/.dd-clock"),
|
||||
@@ -2651,6 +2658,7 @@ class SessionService : Service() {
|
||||
private const val TAG = "SessionService"
|
||||
private const val TRACER_NICE = -6
|
||||
private const val MIC_RELEASE_TIMEOUT_MS = 5_000L
|
||||
private const val OUTPUT_RELEASE_TIMEOUT_MS = 2_000L
|
||||
private const val TU_DEBUG_SWITCH = "Download/droiddeck-tu-debug"
|
||||
private const val GAME_PREFIX = "DROIDDECK_GAME_"
|
||||
private const val LEGACY_ENV_SWITCH = "Download/droiddeck-env"
|
||||
|
||||
@@ -0,0 +1,109 @@
|
||||
package com.droiddeck.launcher.audio
|
||||
|
||||
import org.junit.Assert.*
|
||||
import org.junit.Rule
|
||||
import org.junit.Test
|
||||
import org.junit.rules.TemporaryFolder
|
||||
import org.junit.runner.RunWith
|
||||
import org.robolectric.RobolectricTestRunner
|
||||
import org.robolectric.RuntimeEnvironment
|
||||
import org.robolectric.annotation.Config
|
||||
import org.robolectric.util.ReflectionHelpers
|
||||
import java.io.File
|
||||
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
@Config(sdk = [28])
|
||||
class DirectAudioOutputTest {
|
||||
@get:Rule val folder = TemporaryFolder()
|
||||
|
||||
@Test fun outputControlIsWrittenWithAndWithoutTheMicrophone() {
|
||||
for (fifo in listOf(null, File(folder.root, "mic.fifo"))) {
|
||||
val relay = DirectAudioRelayComponent(File(folder.root, "relay.sock"), fifo)
|
||||
val control = File(folder.root, "output-enabled")
|
||||
assertTrue(relay.setOutputEnabled(false))
|
||||
assertEquals("0", control.readText())
|
||||
assertTrue(relay.setOutputEnabled(true))
|
||||
assertEquals("1", control.readText())
|
||||
}
|
||||
}
|
||||
|
||||
@Test fun suspendWaitsForTheRelayToStopItsOutput() {
|
||||
val relay = DirectAudioRelayComponent(File(folder.root, "relay.sock"), File(folder.root, "mic.fifo"))
|
||||
ReflectionHelpers.setField(relay, "pid", 4242)
|
||||
assertTrue(relay.setOutputEnabled(false))
|
||||
assertFalse(relay.awaitOutputStopped(0))
|
||||
output(relay, "DA-Relay: mic stop (session toggle)")
|
||||
output(relay, "DA-Relay: output enabled=1")
|
||||
assertFalse(relay.awaitOutputStopped(0))
|
||||
output(relay, "DA-Relay: output enabled=0")
|
||||
assertTrue(relay.awaitOutputStopped(0))
|
||||
assertTrue(relay.setOutputEnabled(true))
|
||||
assertFalse(relay.awaitOutputStopped(0))
|
||||
}
|
||||
|
||||
@Test fun lateAcknowledgementReleasesAWaitingSuspend() {
|
||||
val relay = DirectAudioRelayComponent(File(folder.root, "relay.sock"), null)
|
||||
ReflectionHelpers.setField(relay, "pid", 4242)
|
||||
assertTrue(relay.setOutputEnabled(false))
|
||||
val ack = Thread {
|
||||
Thread.sleep(100)
|
||||
output(relay, "DA-Relay: output enabled=0")
|
||||
}
|
||||
ack.start()
|
||||
assertTrue(relay.awaitOutputStopped(5_000))
|
||||
ack.join()
|
||||
}
|
||||
|
||||
@Test fun outputAcknowledgementLeavesTheMicrophoneWait() {
|
||||
val relay = DirectAudioRelayComponent(File(folder.root, "relay.sock"), File(folder.root, "mic.fifo"))
|
||||
ReflectionHelpers.setField(relay, "pid", 4242)
|
||||
mic(relay, "DA-Relay: mic start (game wants it, 1 consumer(s))")
|
||||
output(relay, "DA-Relay: output enabled=0")
|
||||
assertFalse(relay.awaitMicReleased(0))
|
||||
mic(relay, "DA-Relay: mic stop (game)")
|
||||
assertTrue(relay.awaitMicReleased(0))
|
||||
}
|
||||
|
||||
@Test fun relayThatIsNotRunningDoesNotDelaySuspend() {
|
||||
val relay = DirectAudioRelayComponent(File(folder.root, "relay.sock"), null)
|
||||
assertTrue(relay.setOutputEnabled(false))
|
||||
assertTrue(relay.awaitOutputStopped(5_000))
|
||||
}
|
||||
|
||||
@Test fun startClearsAStaleStopAndPassesTheOutputControl() {
|
||||
val context = RuntimeEnvironment.getApplication()
|
||||
val info = context.applicationInfo
|
||||
val previous = info.nativeLibraryDir
|
||||
val nativeDir = folder.newFolder("native")
|
||||
val args = File(folder.root, "arguments")
|
||||
File(nativeDir, DirectAudioRelayComponent.BINARY).apply {
|
||||
writeText("#!/bin/sh\nprintf '%s\\n' \"\$@\" > '${args.path}.new'\nmv '${args.path}.new' '${args.path}'\n")
|
||||
setExecutable(true)
|
||||
}
|
||||
val control = File(folder.root, "output-enabled").apply { writeText("0") }
|
||||
val relay = DirectAudioRelayComponent(File(folder.root, "relay.sock"), null)
|
||||
relay.attach(context)
|
||||
try {
|
||||
info.nativeLibraryDir = nativeDir.path
|
||||
relay.start()
|
||||
val deadline = System.nanoTime() + 3_000_000_000L
|
||||
while (!args.isFile && System.nanoTime() < deadline) Thread.sleep(10)
|
||||
assertTrue("Playback helper did not start", args.isFile)
|
||||
assertFalse(control.exists())
|
||||
val command = args.readLines()
|
||||
val at = command.indexOf("--output-control")
|
||||
assertTrue(at >= 0)
|
||||
assertEquals(control.absolutePath, command[at + 1])
|
||||
assertFalse(command.contains("--mic-fifo"))
|
||||
} finally {
|
||||
info.nativeLibraryDir = previous
|
||||
}
|
||||
}
|
||||
|
||||
private fun output(relay: DirectAudioRelayComponent, line: String) = call(relay, "trackOutput", line)
|
||||
|
||||
private fun mic(relay: DirectAudioRelayComponent, line: String) = call(relay, "trackMic", line)
|
||||
|
||||
private fun call(relay: DirectAudioRelayComponent, method: String, line: String) =
|
||||
ReflectionHelpers.callInstanceMethod<Unit>(relay, method, ReflectionHelpers.ClassParameter.from(String::class.java, line))
|
||||
}
|
||||
@@ -11,6 +11,7 @@ case "$(uname -s)" in
|
||||
esac
|
||||
toolchain="$NDK/toolchains/llvm/prebuilt/$host/bin"
|
||||
patch -d "$source_dir" -p1 < "$repo_root/tools/directaudio/microphone-toggle.patch"
|
||||
patch -d "$source_dir" -p1 < "$repo_root/tools/directaudio/output-pause.patch"
|
||||
"$toolchain/aarch64-linux-android28-clang" -O2 -Wl,-z,max-page-size=16384 \
|
||||
"$source_dir/directaudio-relay.c" -o "$repo_root/app/src/main/jniLibs/arm64-v8a/libdirectaudiorelay.so" \
|
||||
-laaudio -llog -pthread
|
||||
|
||||
@@ -0,0 +1,127 @@
|
||||
--- a/directaudio-relay.c
|
||||
+++ b/directaudio-relay.c
|
||||
@@ -73,6 +73,9 @@
|
||||
static const char *g_mic_control;
|
||||
static int g_mic_enabled = 1;
|
||||
static pthread_mutex_t g_mic_operations = PTHREAD_MUTEX_INITIALIZER;
|
||||
+static const char *g_output_control;
|
||||
+static int g_output_enabled = 1;
|
||||
+static pthread_mutex_t g_output_operations = PTHREAD_MUTEX_INITIALIZER;
|
||||
|
||||
static void rlog(const char *fmt, ...)
|
||||
{
|
||||
@@ -495,8 +498,11 @@
|
||||
AAudioStream_getPerformanceMode(aq), c->share, AAudioStream_getSharingMode(aq),
|
||||
c->out->target_frames);
|
||||
|
||||
- r = AAudioStream_requestStart(aq);
|
||||
- if (r != AAUDIO_OK) { AAudioStream_close(aq); return r; }
|
||||
+ if (__atomic_load_n(&g_output_enabled, __ATOMIC_ACQUIRE))
|
||||
+ {
|
||||
+ r = AAudioStream_requestStart(aq);
|
||||
+ if (r != AAUDIO_OK) { AAudioStream_close(aq); return r; }
|
||||
+ }
|
||||
|
||||
__atomic_store_n(&c->last_cb_ns, now_ns(), __ATOMIC_SEQ_CST);
|
||||
c->wd_last_tick_ns = 0;
|
||||
@@ -517,6 +523,7 @@
|
||||
__atomic_store_n(&c->reopen_redo, 0, __ATOMIC_SEQ_CST);
|
||||
if (c->dead) break;
|
||||
|
||||
+ pthread_mutex_lock(&g_output_operations);
|
||||
if (open_output(c, &neu) == AAUDIO_OK)
|
||||
{
|
||||
pthread_mutex_lock(&c->lock);
|
||||
@@ -532,6 +539,7 @@
|
||||
AAudioStream_close(old);
|
||||
RLOG("[%d] reopened output", c->pid);
|
||||
}
|
||||
+ pthread_mutex_unlock(&g_output_operations);
|
||||
|
||||
if (__atomic_load_n(&c->reopen_redo, __ATOMIC_SEQ_CST)) { usleep(20000); continue; }
|
||||
__atomic_store_n(&c->reopen_running, 0, __ATOMIC_SEQ_CST);
|
||||
@@ -570,6 +578,7 @@
|
||||
|
||||
if (!c->watchdog) return;
|
||||
if (!__atomic_load_n(&c->aq, __ATOMIC_SEQ_CST)) return;
|
||||
+ if (!__atomic_load_n(&g_output_enabled, __ATOMIC_ACQUIRE)) return;
|
||||
|
||||
prev_tick = c->wd_last_tick_ns;
|
||||
c->wd_last_tick_ns = now;
|
||||
@@ -882,6 +891,39 @@
|
||||
REVENT("mic enabled=%d", enabled);
|
||||
}
|
||||
|
||||
+static void output_control_update(void)
|
||||
+{
|
||||
+ FILE *f;
|
||||
+ int enabled;
|
||||
+ struct client *c;
|
||||
+ if (!g_output_control) return;
|
||||
+ f = fopen(g_output_control, "r");
|
||||
+ enabled = !f || fgetc(f) != '0';
|
||||
+ if (f) fclose(f);
|
||||
+ if (enabled == __atomic_load_n(&g_output_enabled, __ATOMIC_ACQUIRE)) return;
|
||||
+ pthread_mutex_lock(&g_output_operations);
|
||||
+ __atomic_store_n(&g_output_enabled, enabled, __ATOMIC_RELEASE);
|
||||
+ pthread_mutex_lock(&g_clients_lock);
|
||||
+ for (c = g_clients; c; c = c->next)
|
||||
+ {
|
||||
+ AAudioStream *aq;
|
||||
+ if (c->dead) continue;
|
||||
+ pthread_mutex_lock(&c->lock);
|
||||
+ aq = c->aq;
|
||||
+ if (aq && enabled)
|
||||
+ {
|
||||
+ c->wd_last_tick_ns = 0;
|
||||
+ __atomic_store_n(&c->last_cb_ns, now_ns(), __ATOMIC_SEQ_CST);
|
||||
+ if (AAudioStream_requestStart(aq) != AAUDIO_OK) request_reopen(c, "restart failed");
|
||||
+ }
|
||||
+ else if (aq) AAudioStream_requestStop(aq);
|
||||
+ pthread_mutex_unlock(&c->lock);
|
||||
+ }
|
||||
+ pthread_mutex_unlock(&g_clients_lock);
|
||||
+ pthread_mutex_unlock(&g_output_operations);
|
||||
+ REVENT("output enabled=%d", enabled);
|
||||
+}
|
||||
+
|
||||
static void *mic_reopen_thread(void *unused)
|
||||
{
|
||||
(void)unused;
|
||||
@@ -1313,7 +1355,9 @@
|
||||
ack.status = AAUDIO_ERROR_NO_MEMORY;
|
||||
goto reply;
|
||||
}
|
||||
+ pthread_mutex_lock(&g_output_operations);
|
||||
r = open_output(c, &c->aq);
|
||||
+ pthread_mutex_unlock(&g_output_operations);
|
||||
if (r != AAUDIO_OK)
|
||||
{
|
||||
REVENT("[%d] output open failed: %d", c->pid, r);
|
||||
@@ -1383,6 +1427,7 @@
|
||||
|
||||
nanosleep(&iv, NULL);
|
||||
mic_control_update();
|
||||
+ output_control_update();
|
||||
if (++tick % 10) continue;
|
||||
|
||||
if (g_rt_path[0])
|
||||
@@ -1430,8 +1475,9 @@
|
||||
if (!strcmp(argv[i], "--socket") && i + 1 < argc) path = argv[++i];
|
||||
else if (!strcmp(argv[i], "--mic-fifo") && i + 1 < argc) fifo_path = argv[++i];
|
||||
else if (!strcmp(argv[i], "--mic-control") && i + 1 < argc) g_mic_control = argv[++i];
|
||||
+ else if (!strcmp(argv[i], "--output-control") && i + 1 < argc) g_output_control = argv[++i];
|
||||
else if (!strcmp(argv[i], "--log")) g_verbose = 1;
|
||||
- else { fprintf(stderr, "usage: directaudio-relay --socket <path> [--mic-fifo <path>] [--mic-control <path>] [--log]\n"); return 2; }
|
||||
+ else { fprintf(stderr, "usage: directaudio-relay --socket <path> [--mic-fifo <path>] [--mic-control <path>] [--output-control <path>] [--log]\n"); return 2; }
|
||||
}
|
||||
if (!path)
|
||||
{
|
||||
@@ -1459,6 +1505,7 @@
|
||||
|
||||
mic_control_update();
|
||||
REVENT("mic enabled=%d", __atomic_load_n(&g_mic_enabled, __ATOMIC_ACQUIRE));
|
||||
+ output_control_update();
|
||||
if (pthread_create(&th, NULL, housekeeping_thread, NULL) == 0) pthread_detach(th);
|
||||
if (fifo_path && fifo_setup(fifo_path) != 0)
|
||||
REVENT("mic-fifo: disabled");
|
||||
Reference in new issue
Block a user