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) checkApertureRuntime() 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.selection.textureSlot+1 }==GLFixture.rendered.map { it.first }) check(frames.map { it.selection.opacity }==GLFixture.rendered.map { it.second }) check(frames.last().mouth.phase=="idle") val before=frames.size;renderer.close();TestScheduler.advanceBy(1000) check(frames.size==before && CodecFixture.releases==1 && GLFixture.deleted==pack.textureImages.size+1 && 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") } @Suppress("UNCHECKED_CAST") private fun checkApertureRuntime() { val old=continuousFixture() val rows=(old["patches"] as List>).mapIndexed { index,p -> val levels=(0..4).map { level -> mapOf("levelIndex" to level,"aperture" to level/4f, "path" to "patches/$index-l$level.png","sha256" to runtimeSha) } p+mapOf("levels" to levels,"path" to levels[4]["path"]) } val root=old+mapOf("packId" to "a01-neutral-graph-v1-runtime-04","patches" to rows, "patchFormat" to mapOf("format" to "PNG RGBA 8bit","alphaEncoding" to "straight_unassociated", "apertureLevels" to listOf(0f,.25f,.5f,.75f,1f),"requiresRendererExtension" to true)) fun read(r: Map)=ContinuousAvatarManifest.read(r,runtimeSha,runtimeSha,true,"runtime04/") val pack=checkNotNull(read(root));check(pack.apertureMode && pack.textureImages.size==485) for(frame in 0..96) for(level in 0..4) { val selection=selectMouthPatch(pack,frame,level/4f) check(selection.textureSlot==frame*5+level && selection.levelIndex==level) check(selection.opacity==if(level==0) 0f else 1f) check(pack.textureImages[selection.textureSlot].path=="patches/$frame-l$level.png") } for((g,level) in listOf(0f to 0,.1249f to 0,.125f to 0,.1251f to 1,.25f to 1,.375f to 1,.3751f to 2,.5f to 2,.75f to 3,1f to 4)) check(selectMouthPatch(pack,0,g).levelIndex==level) for(g in listOf(Float.NaN,-.1f,1.1f)) check(runCatching { selectMouthPatch(pack,0,g) }.isFailure) check(runCatching { selectMouthPatch(pack,97,.5f) }.isFailure) val levels=rows[0]["levels"] as List> fun invalidFirst(row: Map) { check(read(root+("patches" to listOf(row)+rows.drop(1)))==null) } invalidFirst(rows[0]+("levels" to levels.dropLast(1))) invalidFirst(rows[0]+("levels" to listOf(levels[0]+("levelIndex" to 3))+levels.drop(1))) invalidFirst(rows[0]+("levels" to listOf(levels[0]+("aperture" to .2f))+levels.drop(1))) invalidFirst(rows[0]+("levels" to listOf(levels[0]+("path" to "../x.png"))+levels.drop(1))) invalidFirst(rows[0]+("levels" to listOf(levels[0]+("path" to levels[1]["path"]))+levels.drop(1))) invalidFirst(rows[0]+("width" to 129));invalidFirst(rows[0]+("x" to 479)) check(read(root+("patches" to rows.map { it+mapOf("width" to 128,"height" to 128) }))==null) // total RGBA cache cap check(read(root+("packId" to "a01-neutral-graph-v1-runtime-03"))==null) checkContinuousRenderer(pack) // real driver's chosen texture AND opacity at every present, through wrap/abort/teardown println("PASS: runtime04 strict97x5 levels/hash/path/bounds, nearest aperture ties, zero bypass/nonzero opacity1, cross-schema rejection, actual renderer selection/render opacity through97frame loop and lifecycle; no atlas used") }