import com.gorios.lanvoice.avatar.* import android.content.Context import android.graphics.* import android.media.CodecFixture import android.opengl.GLFixture import android.os.TestScheduler import android.view.Surface private val runtimeSha="b".repeat(64) fun continuousFixture(): Map { val ids=listOf("idle","opening","talk1","talk2","end-talk") return mapOf("schemaVersion" to 1,"packId" to "a01-neutral-graph-v1-runtime-03","identityId" to "a01","publishApproval" to false,"runtimeReady" to true, "body" to mapOf("path" to "body/idle.mp4","sha256" to runtimeSha,"width" to 480,"height" to 848,"frameCount" to 97, "durationUs" to 4041667,"fpsNumerator" to 24,"fpsDenominator" to 1,"hasAudio" to false, "frameDurationUsNumerator" to 1000000,"frameDurationUsDenominator" to 24), "patchFormat" to mapOf("format" to "PNG RGBA 8bit","alphaEncoding" to "straight_unassociated"), "patches" to (0..96).map { mapOf("frameIndex" to it,"ptsUs" to it*1000000L/24,"path" to "patches/$it.png","sha256" to runtimeSha, "x" to 211,"y" to 112,"width" to 40,"height" to 22) }, "mouthStates" to ids.map { id -> val bridge=id in setOf("opening","end-talk") val alpha=when(id) { "idle" -> List(12) { 0f } "opening" -> listOf(0f,.1f,.35f,.65f,.9f,1f) "end-talk" -> listOf(1f,.9f,.65f,.35f,.1f,0f) else -> listOf(1f,.8f,.4f,.1f,.4f,.8f,.8f,.4f,.1f,.4f,.8f,1f) } mapOf("id" to id,"durationMs" to if(bridge) 250 else 500,"frameCount" to alpha.size,"fpsNumerator" to 24,"fpsDenominator" to 1, "entryMouthAnchor" to if(id in setOf("idle","opening")) "A" else "B", "exitMouthAnchor" to if(id in setOf("idle","end-talk")) "A" else "B","globalAlphaByFrame" to alpha) },"stateRouting" to mapOf("idle" to listOf("idle","opening"),"opening" to listOf("talk1","talk2","end-talk"), "talk1" to listOf("talk1","talk2","end-talk"),"talk2" to listOf("talk1","talk2","end-talk"),"end-talk" to listOf("idle"))) } fun continuousPack(): ContinuousAvatarPack = checkNotNull(ContinuousAvatarManifest.read(continuousFixture(),runtimeSha,runtimeSha,true,"runtime/")) @Suppress("UNCHECKED_CAST") fun checkContinuous() { val root=continuousFixture();val pack=continuousPack() check(ContinuousAvatarManifest.read(root,runtimeSha,runtimeSha,false,"runtime/")==null) check(ContinuousAvatarManifest.read(root,"c".repeat(64),runtimeSha,true,"runtime/")==null) fun invalid(r: Map) { check(ContinuousAvatarManifest.read(r,runtimeSha,runtimeSha,true,"runtime/")==null) } val patches=root["patches"] as List> for(c in listOf(mapOf("x" to -1),mapOf("x" to 479),mapOf("width" to 129),mapOf("path" to "../secret.png"),mapOf("ptsUs" to 90000),mapOf("frameIndex" to 2),mapOf("y" to Double.NaN))) invalid(root+("patches" to listOf(patches.first()+c)+patches.drop(1))) invalid(root+("patches" to patches.drop(1))) invalid(root+("publishApproval" to true)) invalid(root+("patchFormat" to mapOf("format" to "PNG RGBA 8bit","alphaEncoding" to "premultiplied"))) val routing=root["stateRouting"] as Map invalid(root+("stateRouting" to routing+("end-talk" to listOf("talk1")))) val states=root["mouthStates"] as List> invalid(root+("mouthStates" to listOf(states.first()+("globalAlphaByFrame" to List(12){1f}))+states.drop(1))) val mouth=ContinuousMouth(pack.curves) {0} check(mouth.sample(0).alpha==0f) check(mouth.request(true,1,1));check(mouth.sample(100).phase=="opening") check(!mouth.request(false,0,99));check(!mouth.request(false,1,1)) check(mouth.sample(350).phase=="talk1") check(mouth.sample(850).phase=="talk2") check(mouth.request(false,2,1));check(!mouth.request(true,1,999)) check(mouth.sample(1000).phase=="talk2") // stop finishes current small unit check(mouth.sample(1350).phase=="end-talk") check(mouth.request(true,2,2)) check(mouth.sample(1500).phase=="end-talk") check(mouth.sample(1600).phase=="opening" && mouth.sample(1600).alpha==0f) val reversal=ContinuousMouth(pack.curves){0} reversal.request(true,1,1);reversal.sample(0);reversal.request(false,1,2) check(reversal.sample(250).phase=="end-talk");check(reversal.sample(500).phase=="idle") val seed=kotlin.random.Random(7);val random=ContinuousMouth(pack.curves,seed::nextInt) random.request(true,1,1);random.sample(0);var last="" repeat(20) { val p=random.sample(250+it*500L).phase;check(p in setOf("talk1","talk2") && p!=last);last=p } val gate=ContinuousFramePresenter(pack){0};gate.intent(true,1,1) val observed=mutableListOf() repeat(2) { loop -> repeat(97) { index -> val pts=index*1_000_000L/24;gate.decoded(pts) gate.present(pts*1000,(loop*97L+index)*42) { observed+=it } check(observed.last().key==BodyFrameKey(index,loop.toLong(),pts)) check(observed.last().patch.frameIndex==index) if(index==20) gate.intent(false,2,1) } gate.wrap() } gate.close();check(gate.present(0,9000){ error("stale callback drawn") }==null) check(runCatching { ContinuousFrameClock().decoded(41666) }.isFailure) val clock=ContinuousFrameClock();clock.decoded(0) check(runCatching { clock.latched(9999999) }.isFailure) check(runCatching { ContinuousFrameClock().wrap() }.isFailure) for((w,h) in listOf(480 to 848,1080 to 1920,1920 to 1080,800 to 1280,1200 to 800)) { val rect=bodyFit(w,h);check(rect.left>=0 && rect.top>=0 && rect.width<=w+.01f && rect.height<=h+.01f) check(kotlin.math.abs(rect.width/rect.height-480f/848)<.0001f) } val p=pack.patches.first();check(patchUv(p.x+20,p.y+11,p)==(.5f to .5f));check(patchUv(p.x-1,p.y,p)==null) for(b in listOf(0f,.123f,1f)) for(a in listOf(0f,.5f,1f)) check(composeMouthChannel(b,.8f,a,0f)==b) check(kotlin.math.abs(composeMouthChannel(.2f,.8f,.5f,.5f)-.35f)<.00001) checkContinuousRenderer(pack) println("PASS: continuous runtime strict hash/bounds/97frame/routing/alpha parser; independent mouth state/latest generation/abort/reversal/random continuation; actual decoded-latched frame+loop indexing; Fit/patch mapping/alpha-zero; atomic presenter lifecycle and failure fences") } private fun checkContinuousRenderer(pack: ContinuousAvatarPack) { fun reset() { TestScheduler.reset();CodecFixture.reset();GLFixture.reset();BitmapFactory.wrongSize=false;CodecFixture.frames=97;CodecFixture.fps=24 } fun rig(): Triple,MutableList> { val frames=mutableListOf();val errors=mutableListOf() CodecFixture.onRender={ pts -> SurfaceTexture.lastCreated?.emit(pts*1000) } val renderer=ContinuousBodyRenderer(Context(),pack,Surface(SurfaceTexture()),1080,1920,{f,_->frames+=f},{errors+=it}) return Triple(renderer,frames,errors) } reset();val (renderer,frames,errors)=rig() renderer.intent(true,1,1);TestScheduler.advanceBy(2000);renderer.intent(false,2,1) TestScheduler.advanceBy(2500) check(errors.isEmpty() && frames.size>97 && frames[97].key.index==0 && frames[97].key.loopOrdinal==1L) check(GLFixture.swaps==frames.size && GLFixture.draws==frames.size) check(frames.map { it.key.index+1 }==GLFixture.rendered.map { it.first }) check(frames.last().mouth.phase=="idle") val before=frames.size;renderer.close();TestScheduler.advanceBy(1000) check(frames.size==before && CodecFixture.releases==1 && GLFixture.deleted==98 && GLFixture.terminated==1) reset();val (timestamp,_,timestampErrors)=rig();CodecFixture.onRender={pts->SurfaceTexture.lastCreated?.emit(pts*1000+100000)} TestScheduler.advanceBy(100);check(timestampErrors.size==1);timestamp.close() reset();GLFixture.swapFails=true;val (swap,swapFrames,swapErrors)=rig();TestScheduler.advanceBy(100) check(swapFrames.isEmpty() && swapErrors.size==1 && CodecFixture.releases==1);swap.close() reset();BitmapFactory.wrongSize=true;val (size,sizeFrames,sizeErrors)=rig();TestScheduler.advanceBy(100) check(sizeFrames.isEmpty() && sizeErrors.size==1);size.close();BitmapFactory.wrongSize=false reset();CodecFixture.onRender=null;val (timeout,_,timeoutErrors)=rig();CodecFixture.onRender=null TestScheduler.advanceBy(2100);check(timeoutErrors.size==1);timeout.close() reset();val (cancel,cancelFrames,_)=rig();cancel.close();TestScheduler.advanceBy(100) check(cancelFrames.isEmpty()) println("PASS: real ContinuousBodyRenderer with deterministic Android MediaCodec/EGL doubles: one atomic draw/swap per decoded+latched body frame, 97frame wrap, patch selection, stop body continuity, timestamp/swap/size/timeout failures and resource teardown; actual GPU/device pending") }