56 lines
2.6 KiB
Kotlin
56 lines
2.6 KiB
Kotlin
import android.media.AudioTrack
|
|
import android.os.SystemClock
|
|
import com.gorios.lanvoice.audio.AudioPlayback
|
|
import com.gorios.lanvoice.net.DownlinkFrame
|
|
import java.util.concurrent.CopyOnWriteArrayList
|
|
|
|
fun awaitCheck(test: () -> Boolean) {
|
|
val deadline = System.nanoTime() + 3_000_000_000L
|
|
while (!test()) { check(System.nanoTime() < deadline) { "playback test timed out" }; Thread.sleep(2) }
|
|
}
|
|
fun main() {
|
|
val drains = CopyOnWriteArrayList<Long>()
|
|
val failures = CopyOnWriteArrayList<Exception>()
|
|
val progress = CopyOnWriteArrayList<String>()
|
|
val player = AudioPlayback({ turn, _ -> drains.add(turn) }, { _, e -> failures.add(e) }, { _, _, msg -> progress.add(msg) })
|
|
try {
|
|
player.beginTurn(1)
|
|
check(player.enqueue(DownlinkFrame(1, 1, ByteArray(1920))))
|
|
awaitCheck { (AudioTrack.latest?.samples ?: 0) == 960 }
|
|
check(drains.isEmpty()) // temporary empty software queue is not completion
|
|
player.endTurn(1)
|
|
awaitCheck { drains == listOf(1L) }
|
|
check(progress.last().contains("written=960 head=960"))
|
|
check(!player.enqueue(DownlinkFrame(1, 2, ByteArray(10))))
|
|
player.beginTurn(2)
|
|
check(player.enqueue(DownlinkFrame(2, 1, ByteArray(20)))) // below native startup threshold
|
|
player.endTurn(2)
|
|
awaitCheck { drains == listOf(1L, 2L) }
|
|
check(checkNotNull(AudioTrack.latest).samples == 320) // padded tail counted, partial writes handled
|
|
AudioTrack.advanceHead = false
|
|
player.beginTurn(3)
|
|
check(player.enqueue(DownlinkFrame(3, 1, ByteArray(1920))))
|
|
player.endTurn(3)
|
|
awaitCheck { checkNotNull(AudioTrack.latest).samples == 960 }
|
|
player.interrupt()
|
|
AudioTrack.advanceHead = true
|
|
Thread.sleep(30)
|
|
check(3L !in drains) // interrupted epoch cannot claim drained
|
|
AudioTrack.rejectWrites = true
|
|
player.beginTurn(4)
|
|
check(player.enqueue(DownlinkFrame(4, 1, ByteArray(1920))))
|
|
awaitCheck { failures.size == 1 }
|
|
check(4L !in drains)
|
|
AudioTrack.rejectWrites = false
|
|
AudioTrack.advanceHead = false
|
|
player.beginTurn(5)
|
|
check(player.enqueue(DownlinkFrame(5, 1, ByteArray(1920))))
|
|
player.endTurn(5)
|
|
awaitCheck { (AudioTrack.latest?.samples ?: 0) == 960 }
|
|
SystemClock.extra = 11_000
|
|
awaitCheck { failures.size == 2 }
|
|
check(5L !in drains)
|
|
println("PASS: real AudioPlayback partial/zero writes, explicit end + native head drain, short tail, interruption fence, write failure, stalled head watchdog; device doubles, no real speaker")
|
|
} finally { player.release() }
|
|
}
|