import com.gorios.lanvoice.avatar.* import com.gorios.lanvoice.diag.DiagnosticLog import android.os.TestScheduler import android.media.MediaPlayer import android.view.TextureView import android.graphics.SurfaceTexture fun playbackDiagnosticCheck() { var assertions=0 val asset=mediaFixtureIndex().actionPack!!.assets.first() // Exact device report: compositor output clock jumped from0 to47,741,893,941us. // Reproduce the old failure independently, then pass that same output sequence // through the production adapter while source codec frames remain exact. val reported=listOf(0L,47_741_893_941_000L,47_742_026_182_000L) check(reported.drop(1).none { ts->asset.media!!.ptsUs.any { kotlin.math.abs(it-ts/1000)<=2 } });assertions++ for(mode in listOf("report_clock","early_audio_end","native_error","wrong_codec_pts","missing_audio_end","cancel")) { TestScheduler.reset();android.media.CodecFixture.reset();MediaPlayer.created.clear() val media=asset.media!! android.media.CodecFixture.emitNativeSurface=true android.media.CodecFixture.timelines[media.root+media.path]=media.ptsUs android.media.CodecFixture.audioPaths.add(media.root+media.path) if(mode=="wrong_codec_pts") android.media.CodecFixture.timestampOffsetUs=10_000 val view=TextureView(android.content.Context());val messages=mutableListOf() val port=AndroidMusicPort(android.content.Context(),view,messages::add) var ready=false;var first=0;var ended=0;var failures=0 port.prepare(media,{ready=true},{first++},{ended++},{failures++}) val native=MediaPlayer.created.last();native.prepared!!(native) TestScheduler.until { ready || failures>0 } if(mode=="wrong_codec_pts") { check(!ready && failures==1 && messages.any { "event=video_decoder_or_gl " in it });assertions++ } else { check(ready && first==0 && native.videoSurface==null);assertions++ native.info!!(native,MediaPlayer.MEDIA_INFO_VIDEO_RENDERING_START,0);check(first==0);assertions++ port.start();check(first==1 && native.started);assertions++ reported.forEach(port::latched) check(failures==0 && ended==0);assertions++ when(mode) { "report_clock","early_audio_end" -> { if(mode=="early_audio_end") { native.completion!!(native);check(failures==1 && ended==0);assertions++ } else { TestScheduler.advanceBy(1200);check(ended==0);assertions++ native.completion!!(native) check(ended==1 && failures==0 && messages.any { "event=final_codec_frame " in it });assertions++ } } "native_error" -> { native.error!!(native,1,2);check(failures==1 && ended==0);assertions++ } "missing_audio_end" -> { TestScheduler.advanceBy(3300);check(failures==1 && ended==0);assertions++ } } } var release=0;port.close { release++ };TestScheduler.advanceBy(1) check(native.released && release==1 && messages.last().contains("event=released"));assertions++ native.prepared!!(native);native.info!!(native,3,0);native.completion!!(native);native.error!!(native,1,2) val prior=ended;reported.forEach(port::latched);TestScheduler.advanceBy(100) check(ended==prior && release==1);assertions++ } class Player: ActionPlayer { lateinit var end: (ActionEnd)->Unit;lateinit var failure: ()->Unit;lateinit var release: ()->Unit val ids=mutableListOf() override fun play(asset: ActionAsset,finished: (ActionEnd)->Unit,failed: ()->Unit) {ids.add(asset.id);end=finished;failure=failed} override fun stop(released: ()->Unit) {release=released} } val p=Player();var now=0L;val events=mutableListOf>() val demo=OfflineDancePreview(mediaFixtureIndex().actionPack,p,{now}) {a,s->events.add(a to s)} check(!demo.play("unknown") && p.ids.isEmpty());assertions++ check(demo.play("asset_01") && !demo.play("asset_02"));assertions++ val old=p.end;old(ActionEnd(true,true,true)) // Early end is failure, never completion. check(demo.active && events.none { it.second=="completed" });assertions++ p.release();check(!demo.active);assertions++ check(demo.play("asset_02"));assertions++ old(ActionEnd(true,true,true));check(demo.active);assertions++ now=1001;p.end(ActionEnd(true,true,true));check(!demo.active && events.last().second=="completed");assertions++ check(demo.play("asset_01"));assertions++ val stale=p.failure;demo.cancel("background");demo.cancel();check(!demo.play("asset_02"));assertions++ p.release();stale();check(!demo.active && events.last().second=="released");assertions++ check(demo.play("asset_02"));assertions++ p.failure();p.release();check(!demo.active && p.ids==listOf("asset_01","asset_02","asset_01","asset_02"));assertions++ // Local controls exercise the actual persistent View + renderer + native music callbacks. for(id in listOf("asset_01","asset_02")) { TestScheduler.reset();android.media.CodecFixture.reset();MediaPlayer.created.clear() android.media.CodecFixture.emitNativeSurface=true val index=mediaFixtureIndex() index.packages.values.forEach { android.media.CodecFixture.timelines[it.root+it.body.path]=it.body.ptsUs } index.edges.mapNotNull { it.mouthRuntime }.forEach { android.media.CodecFixture.timelines[it.root+it.body.path]=it.body.ptsUs } index.actionPack?.assets?.mapNotNull { it.media }?.forEach { android.media.CodecFixture.timelines[it.root+it.path]=it.ptsUs;android.media.CodecFixture.audioPaths.add(it.root+it.path) } val binding=AvatarMediaBinding();val view=EmotionActionAvatarView(android.content.Context(),AvatarPack(fallback="fixture.png",emotionAction=index),binding,{}) view.render(true);TestScheduler.advanceBy(1) val local=OfflineDancePreview(index.actionPack,binding,{TestScheduler.now}) check(local.play(id));assertions++ TestScheduler.until { MediaPlayer.created.isNotEmpty() } val native=MediaPlayer.created.last();native.prepared!!(native) TestScheduler.until { native.started } val texture=(view.javaClass.getDeclaredField("textures").apply { isAccessible=true }.get(view) as List).last() val start=TestScheduler.now val media=index.actionPack!!.assets.single { it.id==id }.media!! for(pts in media.ptsUs) { TestScheduler.advanceBy((start+pts/1000-TestScheduler.now).coerceAtLeast(0)) texture.surfaceTexture.timestamp=pts*1000 texture.surfaceTextureListener!!.onSurfaceTextureUpdated(texture.surfaceTexture) TestScheduler.advanceBy(1) check(texture.alpha==1f && local.active);assertions++ } TestScheduler.advanceBy((start+1000-TestScheduler.now).coerceAtLeast(0)) native.completion!!(native) TestScheduler.until { !local.active } check(native.released && texture.alpha==0f && binding.diagnosticState.contains("event=released"));assertions++ view.dispose();TestScheduler.advanceBy(50) } println("PASS: $assertions production native failure reasons/info-vs-latched/stale-callback/release and sensor-free offline dance lifecycle assertions; strict media gate retained, native Android/GPU not_run") }