Skip to content

A rootless Android app that boots Alpine Linux: run containers (Podman/Docker/LXC) and GUI desktop apps.

fix(x11): VNC handles ServerCutText/Bell/ColourMap; AudioStreamer reads aligned frames

ExTv3ef7a215eb87
diff --git a/app/src/main/java/com/excp/podroid/x11/AudioStreamer.kt b/app/src/main/java/com/excp/podroid/x11/AudioStreamer.kt
index ebfe84a..9c24f68 100644
--- a/app/src/main/java/com/excp/podroid/x11/AudioStreamer.kt
+++ b/app/src/main/java/com/excp/podroid/x11/AudioStreamer.kt
@@ -18,6 +18,7 @@ import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch
+import java.io.DataInputStream
import java.net.InetSocketAddress
import java.net.Socket
import kotlin.coroutines.coroutineContext
@@ -26,6 +27,12 @@ class AudioStreamer(private val host: String = "127.0.0.1") {
companion object {
private const val TAG = "AudioStreamer"
+ // 16-bit stereo PCM = 4 bytes / frame; 4096 is a multiple of 4 so
+ // readFully(buf, 0, BUF_BYTES) always yields whole frames. Mis-
+ // aligned reads (raw `read()` returning e.g. 4093 bytes) feed
+ // AudioTrack a half-frame, after which every subsequent sample is
+ // shifted by 1–3 bytes — the audible result was the clicking the
+ // user reported in Firefox video playback.
private const val BUF_BYTES = 4096
}
@@ -49,12 +56,18 @@ class AudioStreamer(private val host: String = "127.0.0.1") {
try {
Socket().use { s ->
s.connect(InetSocketAddress(host, X11Constants.AUDIO_PORT), 2000)
- val inp = s.getInputStream()
+ // Disable Nagle — audio frames are tiny and latency-
+ // sensitive; coalescing them adds jitter that AudioTrack
+ // resamples to fill, producing audible artefacts.
+ s.tcpNoDelay = true
+ val din = DataInputStream(s.getInputStream())
val buf = ByteArray(BUF_BYTES)
while (coroutineContext.isActive) {
- val n = inp.read(buf)
- if (n <= 0) break
- track.write(buf, 0, n)
+ // readFully → exactly BUF_BYTES (= integer frames).
+ // Throws EOFException on socket close → catch outer
+ // reconnects.
+ din.readFully(buf, 0, BUF_BYTES)
+ track.write(buf, 0, BUF_BYTES)
}
}
} catch (e: Exception) {
diff --git a/app/src/main/java/com/excp/podroid/x11/VncClient.kt b/app/src/main/java/com/excp/podroid/x11/VncClient.kt
index a98d24c..1590b87 100644
--- a/app/src/main/java/com/excp/podroid/x11/VncClient.kt
+++ b/app/src/main/java/com/excp/podroid/x11/VncClient.kt
@@ -124,8 +124,33 @@ object VncClient {
*/
fun readFramebufferUpdate(inp: InputStream, targetArgb: IntArray, stride: Int) {
val din = DataInputStream(inp)
- val msgType = din.readUnsignedByte()
- require(msgType == MSG_FRAMEBUFFER_UPDATE) { "unexpected msg $msgType" }
+ // RFB servers interleave non-rect messages (Bell, ColourMap, ClipBoard)
+ // with FramebufferUpdates. Older versions of this client `require`'d
+ // type==0 and bailed on type 3 (ServerCutText, clipboard share) which
+ // Xvnc sends opportunistically — that's the "unexpected msg 3" the
+ // user saw mid-Firefox-video. Skip the bytes for each non-fatal type
+ // and loop until a FramebufferUpdate actually arrives.
+ var msgType: Int
+ while (true) {
+ msgType = din.readUnsignedByte()
+ when (msgType) {
+ MSG_FRAMEBUFFER_UPDATE -> break // fall through to rect parsing
+ 1 -> { // SetColourMapEntries: pad(1) + first(2) + n(2) + n*6
+ din.skipBytes(1)
+ din.readUnsignedShort()
+ val n = din.readUnsignedShort()
+ din.skipBytes(n * 6)
+ }
+ 2 -> { /* Bell: no body */ }
+ 3 -> { // ServerCutText: pad(3) + length(4) + text[length]
+ din.skipBytes(3)
+ val len = din.readInt()
+ if (len in 0..(1 shl 20)) din.skipBytes(len)
+ else throw java.io.IOException("ServerCutText absurd length=$len")
+ }
+ else -> throw java.io.IOException("unexpected RFB server msg type $msgType")
+ }
+ }
din.skipBytes(1)
val numRects = din.readUnsignedShort()