Modern Apps git

← Modern-Apps.git

library/media: remux failure and Kaiser-window coverage (#tests)

Commit
e1552836d71e6b85cf599a9205fba3fd884a639c
Author
Vayun Mathur <[email protected]>
When
2026-11-16 14:07 UTC (19 days ago)
Parents
6f6125b
OpusRemuxer.remux was never called by unit tests: add the no-track
failure contract (null result, temp files deleted for garbage,
empty and fake-EBML inputs). The stream-copy happy path needs a
platform MediaMuxer and is covered on device by CodecPipelineTest.

PolyphaseResampler's Kaiser design function is only ever invoked
at 80 dB in production, leaving its 21..50 dB and <21 dB regimes
unreachable; pin all three regimes plus sinc edge cases against
the published Kaiser formulas via reflection.

Coverage (createMergedCoverageReport, instruction):
library/media 58.0% (2942/5076) -> 60.6% (3074/5076).

Ceiling: the remaining ~2000 missed instructions are the
MediaCodec/MediaExtractor/MediaMuxer pump (OpusPump 952,
OpusTranscoder codec paths, OpusRemuxer mux paths). Robolectric
shadow codecs do not emit encoder output the pump can drain
(verified experimentally: sink stays empty, encoded==0), so the
happy path stays with the on-device CodecPipelineTest.

Verified: :library:media:testDebugUnitTest, detekt and lint green
without the ignore-failures init script.

Diff

library/media/src/test/java/com/vayunmathur/library/media/MediaRemuxTest.kt
diff --git a/library/media/src/test/java/com/vayunmathur/library/media/MediaRemuxTest.kt b/library/media/src/test/java/com/vayunmathur/library/media/MediaRemuxTest.ktnew file mode 100644index 0000000000..f2f0f65589--- /dev/null+++ b/library/media/src/test/java/com/vayunmathur/library/media/MediaRemuxTest.kt@@ -0,0 +1,78 @@+package com.vayunmathur.library.media++import android.content.Context+import androidx.test.core.app.ApplicationProvider+import kotlin.math.PI+import kotlin.math.abs+import kotlin.math.pow+import kotlin.test.Test+import kotlin.test.assertEquals+import kotlin.test.assertNull+import kotlin.test.assertTrue+import org.junit.runner.RunWith+import org.robolectric.RobolectricTestRunner++/**+ * Remux failure contract plus the resampler's Kaiser-window math.+ *+ * Under Robolectric the extractor finds no tracks in any input, which is+ * exactly the "no readable Opus track" case [OpusRemuxer.remux] must answer+ * with null (callers keep the original download bytes). The happy-path+ * stream copy needs a platform MediaMuxer and runs on device.+ */+@RunWith(RobolectricTestRunner::class)+class MediaRemuxTest {++    private val context: Context get() = ApplicationProvider.getApplicationContext()++    private fun remuxTempFiles(): List<String> =+        context.cacheDir.listFiles()+            ?.filter { it.name.startsWith("mb-remux-") }+            ?.map { it.name }+            .orEmpty()++    @Test+    fun remuxOfGarbageReturnsNullAndCleansTempFiles() {+        val before = remuxTempFiles()+        assertNull(OpusRemuxer.remux(context, ByteArray(512) { 0x55 }))+        assertEquals(before, remuxTempFiles(), "temp files must be deleted on every path")+    }++    @Test+    fun remuxOfEmptyInputReturnsNull() {+        assertNull(OpusRemuxer.remux(context, ByteArray(0)))+    }++    @Test+    fun remuxOfFakeWebmHeaderReturnsNull() {+        // EBML magic + a plausible-looking but trackless body.+        val fake = byteArrayOf(0x1A, 0x45, 0xDF.toByte(), 0xA3.toByte()) + ByteArray(256) { it.toByte() }+        assertNull(OpusRemuxer.remux(context, fake))+    }++    @Test+    fun kaiserBetaFollowsTheThreeRegimeFormula() {+        // The production filter always designs for 80 dB, so the 21..50 dB+        // and <21 dB regimes of Kaiser's formula are only reachable by+        // invoking the (private) design function directly. The values are+        // the published Kaiser estimates, not snapshots of the code.+        val companion = PolyphaseResampler::class.java.getDeclaredField("Companion")+            .apply { isAccessible = true }.get(null)+        val beta = companion.javaClass.getDeclaredMethod("kaiserBeta", Double::class.javaPrimitiveType)+            .apply { isAccessible = true }++        fun betaAt(db: Double): Double = beta.invoke(companion, db) as Double++        assertEquals(0.0, betaAt(10.0), 1e-12)+        assertEquals(0.0, betaAt(21.0), 1e-12)+        val mid = 0.5842 * (30.0 - 21.0).pow(0.4) + 0.07886 * (30.0 - 21.0)+        assertEquals(mid, betaAt(30.0), 1e-9)+        assertTrue(betaAt(50.0) > betaAt(30.0), "beta must grow with attenuation")+        assertEquals(0.1102 * (80.0 - 8.7), betaAt(80.0), 1e-9)+        // sinc(0) is the removable-singularity case.+        val sinc = companion.javaClass.getDeclaredMethod("sinc", Double::class.javaPrimitiveType)+            .apply { isAccessible = true }+        assertEquals(1.0, sinc.invoke(companion, 0.0) as Double, 1e-12)+        assertTrue(abs((sinc.invoke(companion, 0.5) as Double) - 2.0 / PI) < 1e-12)+    }+}