mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-12-05 12:35:42 +00:00
Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2ed7afd672 |
No files matched your search
@@ -3,6 +3,7 @@ package com.droiddeck.launcher.audio;
|
|||||||
import android.os.Process;
|
import android.os.Process;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
|
import com.droiddeck.launcher.core.CappedLogWriter;
|
||||||
import com.droiddeck.launcher.core.FileUtils;
|
import com.droiddeck.launcher.core.FileUtils;
|
||||||
import com.droiddeck.launcher.core.SessionPart;
|
import com.droiddeck.launcher.core.SessionPart;
|
||||||
import com.droiddeck.launcher.core.HostProcess;
|
import com.droiddeck.launcher.core.HostProcess;
|
||||||
@@ -16,6 +17,7 @@ public class DirectAudioRelayComponent extends SessionPart {
|
|||||||
|
|
||||||
private final File socketPath;
|
private final File socketPath;
|
||||||
private File logFile;
|
private File logFile;
|
||||||
|
private volatile CappedLogWriter log;
|
||||||
private final File micFifoPath;
|
private final File micFifoPath;
|
||||||
private volatile int pid = -1;
|
private volatile int pid = -1;
|
||||||
private final Object micLock = new Object();
|
private final Object micLock = new Object();
|
||||||
@@ -77,24 +79,23 @@ public class DirectAudioRelayComponent extends SessionPart {
|
|||||||
}
|
}
|
||||||
ArrayList<String> env = new ArrayList<>();
|
ArrayList<String> env = new ArrayList<>();
|
||||||
env.add("HOME=" + app().getFilesDir());
|
env.add("HOME=" + app().getFilesDir());
|
||||||
java.io.PrintWriter out = null;
|
CappedLogWriter out = null;
|
||||||
if (logFile != null) {
|
if (logFile != null) {
|
||||||
try {
|
try {
|
||||||
out = new java.io.PrintWriter(new java.io.FileWriter(logFile, true));
|
out = new CappedLogWriter(logFile, true);
|
||||||
out.println("== DirectAudio relay helper starting"
|
out.line("== DirectAudio relay helper starting"
|
||||||
+ (withMic ? " with the microphone" : " (no microphone)"));
|
+ (withMic ? " with the microphone" : " (no microphone)"));
|
||||||
out.flush();
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
Log.w(TAG, "could not open " + logFile, e);
|
Log.w(TAG, "could not open " + logFile, e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
final java.io.PrintWriter log = out;
|
final CappedLogWriter helperLog = out;
|
||||||
|
log = out;
|
||||||
pid = HostProcess.start(command.toString(), env.toArray(new String[0]),
|
pid = HostProcess.start(command.toString(), env.toArray(new String[0]),
|
||||||
app().getFilesDir(), null,
|
app().getFilesDir(), null,
|
||||||
line -> {
|
line -> {
|
||||||
Log.i(TAG, line);
|
if (helperLog == null || helperLog.line(line)) Log.i(TAG, line);
|
||||||
if (withMic) trackMic(line);
|
if (withMic) trackMic(line);
|
||||||
if (log != null) synchronized (log) { log.println(line); log.flush(); }
|
|
||||||
});
|
});
|
||||||
Log.i(TAG, "started pid=" + pid + " socket=" + socketPath
|
Log.i(TAG, "started pid=" + pid + " socket=" + socketPath
|
||||||
+ (withMic ? " mic=" + micFifoPath : " (no microphone)"));
|
+ (withMic ? " mic=" + micFifoPath : " (no microphone)"));
|
||||||
@@ -141,6 +142,9 @@ public class DirectAudioRelayComponent extends SessionPart {
|
|||||||
Process.killProcess(pid);
|
Process.killProcess(pid);
|
||||||
pid = -1;
|
pid = -1;
|
||||||
}
|
}
|
||||||
|
CappedLogWriter current = log;
|
||||||
|
log = null;
|
||||||
|
if (current != null) current.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import android.content.Context;
|
|||||||
import android.os.Process;
|
import android.os.Process;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
|
import com.droiddeck.launcher.core.CappedLogWriter;
|
||||||
import com.droiddeck.launcher.core.FileUtils;
|
import com.droiddeck.launcher.core.FileUtils;
|
||||||
import com.droiddeck.launcher.core.SessionPart;
|
import com.droiddeck.launcher.core.SessionPart;
|
||||||
import com.droiddeck.launcher.core.HostProcess;
|
import com.droiddeck.launcher.core.HostProcess;
|
||||||
@@ -32,6 +33,7 @@ public class PulseAudioComponent extends SessionPart {
|
|||||||
private final File workingDir;
|
private final File workingDir;
|
||||||
private final File deviceControls;
|
private final File deviceControls;
|
||||||
private File logFile;
|
private File logFile;
|
||||||
|
private CappedLogWriter log;
|
||||||
private final String micFifoPath;
|
private final String micFifoPath;
|
||||||
private final HostLifecycle host = new HostLifecycle(Process::killProcess);
|
private final HostLifecycle host = new HostLifecycle(Process::killProcess);
|
||||||
|
|
||||||
@@ -191,7 +193,7 @@ public class PulseAudioComponent extends SessionPart {
|
|||||||
+ " --system=false --disable-shm=true --fail=false"
|
+ " --system=false --disable-shm=true --fail=false"
|
||||||
+ " -n --file=default.pa --daemonize=false --use-pid-file=false --exit-idle-time=-1"
|
+ " -n --file=default.pa --daemonize=false --use-pid-file=false --exit-idle-time=-1"
|
||||||
+ " --log-level=info";
|
+ " --log-level=info";
|
||||||
final java.io.PrintWriter out = openLog();
|
final CappedLogWriter out = openLog();
|
||||||
if (!host.launch(generation, () -> HostProcess.start(command, env.toArray(new String[0]), workingDir, status -> {
|
if (!host.launch(generation, () -> HostProcess.start(command, env.toArray(new String[0]), workingDir, status -> {
|
||||||
if (host.exited(generation)) {
|
if (host.exited(generation)) {
|
||||||
Log.w(TAG, "audio host exited with status " + status + "; restarting");
|
Log.w(TAG, "audio host exited with status " + status + "; restarting");
|
||||||
@@ -199,8 +201,7 @@ public class PulseAudioComponent extends SessionPart {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
line -> {
|
line -> {
|
||||||
Log.i(TAG, line);
|
if (out == null || out.line(line)) Log.i(TAG, line);
|
||||||
if (out != null) synchronized (out) { out.println(line); out.flush(); }
|
|
||||||
}))) return;
|
}))) return;
|
||||||
long token = host.token();
|
long token = host.token();
|
||||||
Thread guard = new Thread(() -> ensureClientSink(out, token), "pulse-sink-guard");
|
Thread guard = new Thread(() -> ensureClientSink(out, token), "pulse-sink-guard");
|
||||||
@@ -208,7 +209,7 @@ public class PulseAudioComponent extends SessionPart {
|
|||||||
guard.start();
|
guard.start();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void ensureClientSink(java.io.PrintWriter out, long token) {
|
private void ensureClientSink(CappedLogWriter out, long token) {
|
||||||
try {
|
try {
|
||||||
// Nothing that may block (a sink whose AAudio open hangs: a Galaxy S24 FE (US) hung the
|
// Nothing that may block (a sink whose AAudio open hangs: a Galaxy S24 FE (US) hung the
|
||||||
// daemon's start-up loading one from default.pa) is loaded by the daemon itself; the
|
// daemon's start-up loading one from default.pa) is loaded by the daemon itself; the
|
||||||
@@ -253,17 +254,17 @@ public class PulseAudioComponent extends SessionPart {
|
|||||||
Log.w(TAG, line);
|
Log.w(TAG, line);
|
||||||
}
|
}
|
||||||
Log.i(TAG, line);
|
Log.i(TAG, line);
|
||||||
if (out != null) synchronized (out) { out.println(line); out.flush(); }
|
if (out != null) out.line(line);
|
||||||
} catch (Throwable t) {
|
} catch (Throwable t) {
|
||||||
Log.w(TAG, "ensureClientSink failed", t);
|
Log.w(TAG, "ensureClientSink failed", t);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** A timestamped line in the session's audio log. */
|
/** A timestamped line in the session's audio log. */
|
||||||
private void note(java.io.PrintWriter out, String what) {
|
private void note(CappedLogWriter out, String what) {
|
||||||
String line = "== " + new java.text.SimpleDateFormat("HH:mm:ss.SSS", java.util.Locale.US).format(new java.util.Date()) + " " + what;
|
String line = "== " + new java.text.SimpleDateFormat("HH:mm:ss.SSS", java.util.Locale.US).format(new java.util.Date()) + " " + what;
|
||||||
Log.i(TAG, line);
|
Log.i(TAG, line);
|
||||||
if (out != null) synchronized (out) { out.println(line); out.flush(); }
|
if (out != null) out.line(line);
|
||||||
}
|
}
|
||||||
|
|
||||||
private String pactlFor(long token, String... args) {
|
private String pactlFor(long token, String... args) {
|
||||||
@@ -330,6 +331,7 @@ public class PulseAudioComponent extends SessionPart {
|
|||||||
@Override
|
@Override
|
||||||
public void stop() {
|
public void stop() {
|
||||||
host.stop();
|
host.stop();
|
||||||
|
closeLog();
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean setSinkSuspended(boolean suspended) {
|
public boolean setSinkSuspended(boolean suspended) {
|
||||||
@@ -380,19 +382,24 @@ public class PulseAudioComponent extends SessionPart {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private java.io.PrintWriter openLog() {
|
private synchronized CappedLogWriter openLog() {
|
||||||
|
closeLog();
|
||||||
if (logFile == null) return null;
|
if (logFile == null) return null;
|
||||||
try {
|
try {
|
||||||
java.io.PrintWriter w = new java.io.PrintWriter(new java.io.FileWriter(logFile, true));
|
log = new CappedLogWriter(logFile, true);
|
||||||
w.println("== PulseAudio daemon starting" + (micFifoPath != null ? " with a microphone source" : ""));
|
log.line("== PulseAudio daemon starting" + (micFifoPath != null ? " with a microphone source" : ""));
|
||||||
w.flush();
|
return log;
|
||||||
return w;
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
Log.w(TAG, "could not open " + logFile, e);
|
Log.w(TAG, "could not open " + logFile, e);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private synchronized void closeLog() {
|
||||||
|
if (log != null) log.close();
|
||||||
|
log = null;
|
||||||
|
}
|
||||||
|
|
||||||
private void copyFromLibraryDir() {
|
private void copyFromLibraryDir() {
|
||||||
String[] libs = {"libltdl.so", "libpulseaudio.so", "libpulse.so",
|
String[] libs = {"libltdl.so", "libpulseaudio.so", "libpulse.so",
|
||||||
"libpulsecommon-13.0.so", "libpulsecore-13.0.so", "libsndfile.so", "libffi.so"};
|
"libpulsecommon-13.0.so", "libpulsecore-13.0.so", "libsndfile.so", "libffi.so"};
|
||||||
|
|||||||
@@ -0,0 +1,59 @@
|
|||||||
|
package com.droiddeck.launcher.core
|
||||||
|
|
||||||
|
import java.io.BufferedWriter
|
||||||
|
import java.io.Closeable
|
||||||
|
import java.io.File
|
||||||
|
import java.io.FileOutputStream
|
||||||
|
import java.io.OutputStreamWriter
|
||||||
|
|
||||||
|
class CappedLogWriter @JvmOverloads constructor(
|
||||||
|
private val file: File,
|
||||||
|
append: Boolean,
|
||||||
|
private val limitBytes: Long = SESSION_LOG_LIMIT_BYTES,
|
||||||
|
private val tailLines: Int = TAIL_LINES,
|
||||||
|
) : Closeable {
|
||||||
|
private val out = BufferedWriter(OutputStreamWriter(FileOutputStream(file, append), Charsets.UTF_8))
|
||||||
|
private val tail = ArrayDeque<String>()
|
||||||
|
private var dropped = 0L
|
||||||
|
private var closed = false
|
||||||
|
|
||||||
|
@Synchronized
|
||||||
|
fun line(text: String): Boolean {
|
||||||
|
if (closed) return false
|
||||||
|
if (dropped == 0L && file.length() + text.toByteArray(Charsets.UTF_8).size + 1 <= limitBytes) {
|
||||||
|
runCatching { out.write(text); out.newLine(); out.flush() }
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
if (dropped == 0L) {
|
||||||
|
runCatching {
|
||||||
|
out.write("== this log reached its ${limitBytes / (1024 * 1024)} MB limit; later lines are dropped and only the last $tailLines are kept at the end")
|
||||||
|
out.newLine()
|
||||||
|
out.flush()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
dropped++
|
||||||
|
tail.addLast(text)
|
||||||
|
if (tail.size > tailLines) tail.removeFirst()
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
@Synchronized
|
||||||
|
override fun close() {
|
||||||
|
if (closed) return
|
||||||
|
closed = true
|
||||||
|
runCatching {
|
||||||
|
if (dropped > 0) {
|
||||||
|
out.write("== ${dropped - tail.size} lines dropped after the limit; the last ${tail.size} follow")
|
||||||
|
out.newLine()
|
||||||
|
tail.forEach { out.write(it); out.newLine() }
|
||||||
|
}
|
||||||
|
out.flush()
|
||||||
|
}
|
||||||
|
runCatching { out.close() }
|
||||||
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
const val SESSION_LOG_LIMIT_BYTES = 64L * 1024 * 1024
|
||||||
|
const val TAIL_LINES = 400
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3,7 +3,6 @@ package com.droiddeck.launcher.core
|
|||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.os.Process
|
import android.os.Process
|
||||||
import android.util.Log
|
import android.util.Log
|
||||||
import java.io.BufferedWriter
|
|
||||||
import java.io.File
|
import java.io.File
|
||||||
import java.io.FileOutputStream
|
import java.io.FileOutputStream
|
||||||
import java.nio.file.Files
|
import java.nio.file.Files
|
||||||
@@ -14,7 +13,7 @@ object SessionLogCapture {
|
|||||||
private const val TAG = "SessionLogCapture"
|
private const val TAG = "SessionLogCapture"
|
||||||
|
|
||||||
private var pid = -1
|
private var pid = -1
|
||||||
private var writer: BufferedWriter? = null
|
private var writer: CappedLogWriter? = null
|
||||||
private var target: File? = null
|
private var target: File? = null
|
||||||
private var syncer: Thread? = null
|
private var syncer: Thread? = null
|
||||||
|
|
||||||
@@ -22,26 +21,20 @@ object SessionLogCapture {
|
|||||||
fun start(target: File, sinceMillis: Long) {
|
fun start(target: File, sinceMillis: Long) {
|
||||||
stop()
|
stop()
|
||||||
try {
|
try {
|
||||||
val out = target.bufferedWriter()
|
val out = CappedLogWriter(target, false)
|
||||||
out.write("The app's own log for this session (logcat, this process only).\n")
|
out.line("The app's own log for this session (logcat, this process only).")
|
||||||
out.write("Lines the app wrote before the session folder existed are in logcat only.\n\n")
|
out.line("Lines the app wrote before the session folder existed are in logcat only.")
|
||||||
out.flush()
|
out.line("")
|
||||||
writer = out
|
writer = out
|
||||||
this.target = target
|
this.target = target
|
||||||
pid = HostProcess.start(
|
pid = HostProcess.start(
|
||||||
command(Process.myPid(), sinceMillis),
|
command(Process.myPid(), sinceMillis),
|
||||||
null, target.parentFile, null,
|
null, target.parentFile, null,
|
||||||
) { line ->
|
) { line ->
|
||||||
synchronized(this) {
|
synchronized(this) { writer?.line(line) }
|
||||||
try {
|
|
||||||
writer?.apply { write(line); newLine(); flush() }
|
|
||||||
} catch (e: Exception) {
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if (pid == -1) {
|
if (pid == -1) {
|
||||||
out.write("logcat could not be started; this ROM may not hand an app its own entries.\n")
|
out.line("logcat could not be started; this ROM may not hand an app its own entries.")
|
||||||
out.flush()
|
|
||||||
}
|
}
|
||||||
target.parentFile?.let { syncer = startSyncing(it) }
|
target.parentFile?.let { syncer = startSyncing(it) }
|
||||||
Log.i(TAG, "app log -> $target (logcat pid $pid)")
|
Log.i(TAG, "app log -> $target (logcat pid $pid)")
|
||||||
@@ -81,10 +74,7 @@ object SessionLogCapture {
|
|||||||
}
|
}
|
||||||
pid = -1
|
pid = -1
|
||||||
}
|
}
|
||||||
try {
|
writer?.close()
|
||||||
writer?.close()
|
|
||||||
} catch (e: Exception) {
|
|
||||||
}
|
|
||||||
writer = null
|
writer = null
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,79 @@
|
|||||||
|
package com.droiddeck.launcher.core
|
||||||
|
|
||||||
|
import org.junit.Assert.assertEquals
|
||||||
|
import org.junit.Assert.assertFalse
|
||||||
|
import org.junit.Assert.assertTrue
|
||||||
|
import org.junit.Rule
|
||||||
|
import org.junit.Test
|
||||||
|
import org.junit.rules.TemporaryFolder
|
||||||
|
|
||||||
|
class CappedLogWriterTest {
|
||||||
|
@get:Rule val folder = TemporaryFolder()
|
||||||
|
|
||||||
|
@Test fun aLogUnderTheLimitIsWrittenAsIs() {
|
||||||
|
val file = folder.newFile("app.log")
|
||||||
|
CappedLogWriter(file, false, 1024, 3).use { log ->
|
||||||
|
assertTrue(log.line("first"))
|
||||||
|
assertTrue(log.line("second"))
|
||||||
|
}
|
||||||
|
assertEquals(listOf("first", "second"), file.readLines())
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test fun aReconnectStormStopsAtTheLimitAndKeepsTheLastLines() {
|
||||||
|
val file = folder.newFile("audio.log")
|
||||||
|
val limit = 4096L
|
||||||
|
val log = CappedLogWriter(file, true, limit, 3)
|
||||||
|
var written = 0
|
||||||
|
for (n in 0 until 10_000) {
|
||||||
|
if (log.line("I: [linker64] client.c: Created $n \"Native client (UNIX socket client)\"")) written++
|
||||||
|
}
|
||||||
|
val capped = file.length()
|
||||||
|
assertTrue("file grew to $capped bytes", capped <= limit + 200)
|
||||||
|
assertFalse(log.line("after the limit"))
|
||||||
|
log.close()
|
||||||
|
val lines = file.readLines()
|
||||||
|
assertEquals(written, lines.count { it.startsWith("I: [linker64] client.c: Created ") } - 2)
|
||||||
|
assertTrue(lines.any { it.startsWith("== this log reached its") })
|
||||||
|
assertEquals("== ${10_000 + 1 - written - 3} lines dropped after the limit; the last 3 follow", lines[lines.size - 4])
|
||||||
|
assertEquals(
|
||||||
|
listOf(
|
||||||
|
"I: [linker64] client.c: Created 9998 \"Native client (UNIX socket client)\"",
|
||||||
|
"I: [linker64] client.c: Created 9999 \"Native client (UNIX socket client)\"",
|
||||||
|
"after the limit",
|
||||||
|
),
|
||||||
|
lines.takeLast(3),
|
||||||
|
)
|
||||||
|
assertTrue(file.length() < limit + 1024)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test fun twoWritersOnOneFileShareItsLimit() {
|
||||||
|
val file = folder.newFile("audio.log")
|
||||||
|
val daemon = CappedLogWriter(file, true, 2048, 2)
|
||||||
|
val relay = CappedLogWriter(file, true, 2048, 2)
|
||||||
|
repeat(2_000) { daemon.line("I: [linker64] protocol-native.c: Connection died.") }
|
||||||
|
assertFalse(relay.line("DA-Relay: mic enabled=1"))
|
||||||
|
assertTrue(file.length() <= 2048 + 200)
|
||||||
|
daemon.close()
|
||||||
|
relay.close()
|
||||||
|
assertEquals("DA-Relay: mic enabled=1", file.readLines().last())
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test fun appendKeepsWhatTheFileHeldAndOverwriteStartsFresh() {
|
||||||
|
val file = folder.newFile("audio.log")
|
||||||
|
file.writeText("== PulseAudio daemon starting\n")
|
||||||
|
CappedLogWriter(file, true, 1024, 2).use { it.line("restarted") }
|
||||||
|
assertEquals(listOf("== PulseAudio daemon starting", "restarted"), file.readLines())
|
||||||
|
CappedLogWriter(file, false, 1024, 2).use { it.line("new session") }
|
||||||
|
assertEquals(listOf("new session"), file.readLines())
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test fun linesAfterCloseAreIgnored() {
|
||||||
|
val file = folder.newFile("app.log")
|
||||||
|
val log = CappedLogWriter(file, false, 1024, 2)
|
||||||
|
log.line("kept")
|
||||||
|
log.close()
|
||||||
|
assertFalse(log.line("late"))
|
||||||
|
log.close()
|
||||||
|
assertEquals(listOf("kept"), file.readLines())
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user