package link.roadio.riff.indexer.android.level2 import android.os.Build import androidx.test.ext.junit.runners.AndroidJUnit4 import androidx.test.platform.app.InstrumentationRegistry import link.roadio.riff.indexer.core.CancellationSignal import link.roadio.riff.indexer.core.ipc.reliable.DecoderAttemptObserver import link.roadio.riff.indexer.core.ipc.reliable.DecoderPlan import link.roadio.riff.indexer.core.ipc.reliable.DecoderSelection import link.roadio.riff.indexer.core.ipc.reliable.DecoderWorkload import link.roadio.riff.indexer.core.model.SourceMedia import org.json.JSONArray import org.json.JSONObject import org.junit.Assert.assertArrayEquals import org.junit.Assert.assertEquals import org.junit.Assert.assertNotNull import org.junit.Assert.assertSame import org.junit.Assert.assertThrows import org.junit.Assert.assertTrue import org.junit.Test import org.junit.runner.RunWith import java.io.File import java.util.concurrent.CancellationException /** Changes AU grouping on real warm sessions and compares every prepared PCM sample with one AU. */ @RunWith(AndroidJUnit4::class) class DecoderBatchTransitionInstrumentedTest { private val instrumentation = InstrumentationRegistry.getInstrumentation() private val context = instrumentation.targetContext @Test fun cancelledDecodeDoesNotOpenItsSource() { val cancelled = object : CancellationSignal { override val cancelled = true override fun ensureAvailable(): Unit = throw CancellationException("Cancelled before source inspection") } AndroidAudioExcerptDecoder(context).use { decoder -> assertThrows(CancellationException::class.java) { decoder.decode( SourceMedia("content://riff.invalid.source/missing", "missing"), HINT, true, true, cancelled, ) } } } @Test fun mp3WindowChangesKeepWarmSessionsAndExactPcm() = verify("mp3") @Test fun aacWindowChangesKeepWarmSessionsAndExactPcm() = verify("m4a") @Test fun opusWindowChangesKeepWarmSessionsAndExactPcm() = verify("opus") private fun verify(extension: String) { val fixture = File(context.cacheDir, "window-transition." + extension) try { // AAC fixture disables encoder PNS: its synthesis state can vary even across one-AU reuses. instrumentation.context.assets.open("decoder-recovery/fixture." + extension).use { input -> fixture.outputStream().use { output -> input.copyTo(output) } } val source = SourceMedia(fixture.absolutePath, fixture.name) val selected = AndroidDecoderAutotuner(context).select(source, HINT, Available) var current = selected.copy(maxAccessUnits = 1) val observer = object : DecoderAttemptObserver { override val ownsPlans = true override fun select( decoderHint: Long, workload: DecoderWorkload, maximumAccessUnits: Int, parallelism: Int, ): DecoderSelection = DecoderSelection(current, false) } val sizes = if (InstrumentationRegistry.getArguments().getString("riffTransitionSingleControl") == "true") { List(8) { 1 } } else if (selected.batchApi == DecoderBatchApi.SINGLE) { listOf(1, 1, 1) } else { listOf(1, 2, 3, 4, 3, 2, 1, 2) } val events = mutableListOf() AndroidAudioExcerptDecoder(context, onBatchSizeChanged = events::add).use { decoder -> val sessionsField = decoder.javaClass.getDeclaredField("codecSessions").apply { isAccessible = true } var reference: FloatArray? = null var warmSessions: List<*>? = null sizes.forEach { size -> current = selected.copy(maxAccessUnits = size) val decoded = decoder.decode( source = source, decoderHint = HINT, needsLoudness = true, needsAi = true, cancellation = Available, attempts = observer, ) assertTrue("AI PCM must remain usable", decoded.aiUsable) assertTrue("Loudness must remain finite", decoded.integratedLufs?.isFinite() == true) if (reference == null) { reference = decoded.pcm } else { assertArrayEquals("PCM for " + extension + " at " + size + " AU", reference, decoded.pcm, 0f) } val sessions = (sessionsField.get(decoder) as Array<*>).toList() assertEquals(3, sessions.size) sessions.forEach { assertNotNull(it) } warmSessions?.forEachIndexed { index, session -> assertSame("AU-only changes must retain session " + index, session, sessions[index]) } warmSessions = sessions assertEquals(size, events.last()) assertTrue("No release gap between AU sizes", events.none { it == null }) } } assertEquals(null, events.last()) val proof = JSONObject() .put("api", Build.VERSION.SDK_INT) .put("format", extension) .put("codec", selected.codecName) .put("input_api", selected.batchApi.name) .put("sizes", JSONArray(sizes)) .put("exact_pcm", true) .put("warm_sessions_retained", true) .put("closed_status_cleared", true) File(context.filesDir, "decoder-transition-" + extension + ".json").writeText(proof.toString(2)) } finally { fixture.delete() } } private object Available : CancellationSignal { override val cancelled = false override fun ensureAvailable() = Unit } private companion object { const val HINT = 0x741L } }