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() 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() val errors = CopyOnWriteArrayList() capture.start(onFrame = frames::add, onError = errors::add) check(AudioRecord.partialReady.await(2, TimeUnit.SECONDS)) val confirmed = CountDownLatch(1) capture.stopAndConfirm { stopped -> check(stopped && AudioRecord.activeDevices.get()==0) confirmed.countDown() } check(confirmed.await(2, TimeUnit.SECONDS)) 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") }