fix: fence voice turns and playback; add opt-in face context

This commit is contained in:
zenord
2026-10-11 20:50:11 +08:00
parent 885bb3f2db
commit 9b428fb76f
46 changed files with 2306 additions and 32 deletions
+9
View File
@@ -0,0 +1,9 @@
package android.media.audiofx
class AcousticEchoCanceler {
companion object {
fun isAvailable() = false
fun create(id: Int): AcousticEchoCanceler? = null
}
fun setEnabled(enabled: Boolean) {}
fun release() {}
}
+60
View File
@@ -0,0 +1,60 @@
import android.media.AudioRecord
import com.gorios.lanvoice.audio.AudioCapture
import java.util.concurrent.CountDownLatch
import java.util.concurrent.TimeUnit
import java.util.concurrent.CopyOnWriteArrayList
fun main() {
for ((mode, phase) in listOf("init" to "initialization", "start" to "startRecording", "read" to "read")) {
AudioRecord.scenario = mode
val messages = CopyOnWriteArrayList<String>()
val failed = CountDownLatch(1)
val capture = AudioCapture()
capture.start(onFrame = { error("no PCM expected") }, onError = { failed.countDown() }, onDiagnostic = messages::add)
check(failed.await(2, TimeUnit.SECONDS))
check(messages.first() == "AudioRecord initialization started")
check(messages.last().startsWith("AudioRecord $phase failed:"))
check(messages.contains("AudioRecord initialization ok") == (mode != "init"))
check(messages.contains("AudioRecord startRecording ok") == (mode == "read"))
check(messages.none { "synthetic mic revocation" in it }) // exception type only
capture.stop()
capture.release()
val limit = System.nanoTime() + 2_000_000_000L
while (AudioRecord.activeDevices.get() != 0) { check(System.nanoTime() < limit); Thread.sleep(1) }
}
val capture = AudioCapture()
for ((mode, expectedCount) in listOf("partial" to 2, "fresh" to 1)) {
AudioRecord.scenario = mode
AudioRecord.partialReady = CountDownLatch(1)
AudioRecord.released = CountDownLatch(1)
val frames = CopyOnWriteArrayList<ByteArray>()
val errors = CopyOnWriteArrayList<Exception>()
capture.start(onFrame = frames::add, onError = errors::add)
check(AudioRecord.partialReady.await(2, TimeUnit.SECONDS))
capture.stop()
check(AudioRecord.released.await(2, TimeUnit.SECONDS))
check(errors.isEmpty())
check(frames.size == expectedCount) // 100B unfinished tail must not be sent
frames.forEachIndexed { index, bytes ->
check(bytes.size == 640)
check(bytes.contentEquals(ByteArray(640) { ((index * 640 + it) % 251).toByte() }))
}
if (frames.size > 1) check(frames[0] !== frames[1])
}
// Turn switch starts immediately while the previous recorder releases slowly.
AudioRecord.scenario = "fresh"
AudioRecord.partialReady = CountDownLatch(1)
AudioRecord.released = CountDownLatch(1)
capture.start({}, { throw it })
check(AudioRecord.partialReady.await(2, TimeUnit.SECONDS))
capture.stop()
AudioRecord.partialReady = CountDownLatch(1)
capture.start({}, { throw it })
check(AudioRecord.partialReady.await(2, TimeUnit.SECONDS))
capture.release()
check(AudioRecord.released.await(2, TimeUnit.SECONDS))
check(AudioRecord.overlaps.get() == 0) { "replacement AudioRecord must await old device release" }
check(AudioCapture.FRAME_BYTES == 640)
println("PASS: short/zero reads accumulated into exact independent 640B frames; stop discards trailing partial; restart starts fresh")
println("PASS: actual AudioCapture with Android media doubles: initialization/start/read success markers and failure stage, no raw exception content")
}
+50
View File
@@ -0,0 +1,50 @@
package android.media
object AudioFormat {
const val CHANNEL_IN_MONO = 16
const val ENCODING_PCM_16BIT = 2
}
class MediaRecorder { object AudioSource { const val VOICE_COMMUNICATION = 7 } }
class AudioRecord(source: Int, rate: Int, channel: Int, encoding: Int, buffer: Int) {
companion object {
const val STATE_INITIALIZED = 1
const val RECORDSTATE_RECORDING = 3
const val READ_BLOCKING = 0
val activeDevices = java.util.concurrent.atomic.AtomicInteger()
val overlaps = java.util.concurrent.atomic.AtomicInteger()
@Volatile var scenario = "init"
@Volatile var partialReady = java.util.concurrent.CountDownLatch(1)
@Volatile var released = java.util.concurrent.CountDownLatch(1)
fun getMinBufferSize(rate: Int, channel: Int, encoding: Int) = 640
}
init { if (activeDevices.incrementAndGet() > 1) overlaps.incrementAndGet() }
private val mode = scenario
val state = if (mode == "init") 0 else STATE_INITIALIZED
val recordingState = RECORDSTATE_RECORDING
val audioSessionId = 1
fun startRecording() { if (mode == "start") throw SecurityException("synthetic mic revocation") }
private val stopped = java.util.concurrent.CountDownLatch(1)
private val ready = partialReady
private val done = released
private val chunks = when (mode) {
"partial" -> listOf(2, 0, 126, 200, 312, 638, 2, 100)
"fresh" -> listOf(640, 100)
else -> emptyList()
}
private var chunk = 0
private var streamOffset = 0
fun read(frame: ByteArray, offset: Int, size: Int, mode: Int): Int {
if (chunks.isEmpty()) return -3
if (chunk == chunks.size) {
ready.countDown()
check(stopped.await(2, java.util.concurrent.TimeUnit.SECONDS))
return -3
}
val count = chunks[chunk++]
check(count <= size && offset + count <= frame.size)
repeat(count) { frame[offset + it] = ((streamOffset + it) % 251).toByte() }
streamOffset += count
return count
}
fun stop() { stopped.countDown() }
fun release() { Thread.sleep(20); activeDevices.decrementAndGet(); done.countDown() }
}
+5
View File
@@ -0,0 +1,5 @@
package android.os
object Process {
const val THREAD_PRIORITY_AUDIO = -16
fun setThreadPriority(priority: Int) {}
}