import com.gorios.lanvoice.avatar.* import android.content.Context import android.graphics.SurfaceTexture import android.media.CodecFixture import android.os.* import android.view.Surface private class CodecRig(val clip: SafeAvatarClip = syntheticGraph().clips.getValue("idle"), val entry: AnchorInterval = clip.entryAnchors.single(), latch: Boolean = true, wrongTimestamp: Boolean = false) { val port = CodecAnchorVideoPort(Context(),Surface(SurfaceTexture()),"fixture/",Looper()) { w,h -> check(w==480 && h==848) } val frames=mutableListOf(); val failures=mutableListOf() var ended=0; var autoAdvance=false init { if (latch) CodecFixture.onRender = { pts -> Handler().post { port.textureLatched(pts*1000 + if (wrongTimestamp) 10_000 else 0) } } port.prepare(clip,entry,object : AnchorVideoPort.Listener { override fun frame(frame: DisplayedVideoFrame) { frames += frame; if (autoAdvance) port.advance() } override fun ended() { ended++ } override fun failed(reason: VideoFailure) { failures += reason } }) } fun first() { TestScheduler.until { frames.isNotEmpty() || failures.isNotEmpty() } } fun close() { port.close(); TestScheduler.advanceBy(10) } } fun checkCodecPort() { fun reset() { TestScheduler.reset(); CodecFixture.reset() } reset() val idle=syntheticGraph().clips.getValue("idle") val entry=AnchorInterval("mid","closed",450,500,9,10,AnchorReview.APPROVED) val staged=CodecRig(idle,entry); staged.first() check(staged.frames.single().frame==9 && CodecFixture.seeks.single()==450_000L) check(CodecFixture.rendered==listOf(450_000L)) // preroll outputs discarded until entry TestScheduler.advanceBy(1_000); check(staged.frames.size==1) // hidden phase never advances val before=SystemClock.elapsedRealtime(); staged.port.advance() check(staged.port.displayed()==null) // old texture is not a fresh sample during flight TestScheduler.until { staged.frames.size==2 } check(SystemClock.elapsedRealtime()-before >= 50 && staged.frames.last().frame==10) staged.autoAdvance=true; staged.port.advance() TestScheduler.until { staged.frames.any { it.loop==1L } } check(staged.frames.last().frame==0 && CodecFixture.flushes==1 && staged.failures.isEmpty()) staged.close(); val n=staged.frames.size; staged.port.textureLatched(0); TestScheduler.advanceBy(100) check(staged.frames.size==n && CodecFixture.releases==1) reset(); CodecFixture.fps=24; CodecFixture.frames=12 val c24=idle.copy(durationMs=500,frameCount=12,fpsNumerator=24, entryAnchors=listOf(AnchorInterval("entry","closed",0,42,0,1,AnchorReview.APPROVED)), safeExitWindows=listOf(AnchorInterval("exit","closed",458,500,11,12,AnchorReview.APPROVED)),mouthStateWindows=emptyList()) val fps24=CodecRig(c24); fps24.autoAdvance=true; fps24.first() TestScheduler.until { fps24.frames.any { it.loop==1L } } check(fps24.frames.take(12).map { it.frame }==(0..11).toList() && fps24.failures.isEmpty()) check(fps24.frames[11].positionMs==458L) fps24.close() reset(); val bridge=CodecRig(syntheticGraph().clips.getValue("opening")); bridge.autoAdvance=true; bridge.first() TestScheduler.until { bridge.ended==1 } check(bridge.frames.size==20 && bridge.frames.last().frame==19 && CodecFixture.flushes==0) check(bridge.port.displayed()?.frame==19); bridge.close() reset(); val timestamp=CodecRig(wrongTimestamp=true); timestamp.first() check(timestamp.failures==listOf(VideoFailure.TIMESTAMP)); timestamp.close() reset(); val noLatch=CodecRig(latch=false) TestScheduler.until { noLatch.failures.isNotEmpty() } check(noLatch.failures==listOf(VideoFailure.FRAME_TIMEOUT)); noLatch.close() reset(); CodecFixture.outputEnabled=false; val stuck=CodecRig() TestScheduler.until { stuck.failures.isNotEmpty() } check(stuck.failures==listOf(VideoFailure.FRAME_TIMEOUT)); stuck.close() reset(); CodecFixture.configureFails=true; val resource=CodecRig(); resource.first() check(resource.failures==listOf(VideoFailure.DECODE)); resource.close() reset(); CodecFixture.firstPtsUs=50_000; val origin=CodecRig(); origin.first() check(origin.failures==listOf(VideoFailure.DECODE)); origin.close() reset(); CodecFixture.timestampOffsetUs=10_000; val cfr=CodecRig(); cfr.first() check(cfr.failures==listOf(VideoFailure.FORMAT)); cfr.close() reset(); CodecFixture.frames=10; val truncated=CodecRig(); truncated.autoAdvance=true; truncated.first() TestScheduler.until { truncated.failures.isNotEmpty() } check(truncated.failures==listOf(VideoFailure.FORMAT)); truncated.close() reset(); val canceled=CodecRig(); canceled.close(); TestScheduler.advanceBy(10_000) check(canceled.frames.isEmpty() && canceled.failures.isEmpty()) println("PASS: production codec control with deterministic Android API doubles: seek/preroll, latched entry/held phase, in-flight sample fence, frame pacing, approved wrap, nonloop tail, timestamp/decode/resource/early-EOS errors, watchdogs and canceled callbacks; hardware decoder/device pending") }