|
|
|
@@ -0,0 +1,212 @@
|
|
|
|
|
import com.gorios.lanvoice.avatar.*
|
|
|
|
|
import com.gorios.lanvoice.audio.*
|
|
|
|
|
import com.gorios.lanvoice.net.*
|
|
|
|
|
import android.os.*
|
|
|
|
|
import android.media.*
|
|
|
|
|
import okhttp3.*
|
|
|
|
|
import okio.ByteString
|
|
|
|
|
import org.json.JSONObject
|
|
|
|
|
import java.nio.ByteBuffer
|
|
|
|
|
import java.nio.ByteOrder
|
|
|
|
|
|
|
|
|
|
private fun field(obj:Any,name:String)=obj.javaClass.getDeclaredField(name).apply { isAccessible=true }
|
|
|
|
|
private class WaitingSocket(val control:(JSONObject)->Unit):WebSocket {
|
|
|
|
|
val sent=mutableListOf<JSONObject>();var audioFrames=0;var cancelled=false
|
|
|
|
|
override fun request()=Request.Builder().url("https://localhost/ws/dialogue").build()
|
|
|
|
|
override fun queueSize()=0L
|
|
|
|
|
override fun send(text:String):Boolean { val value=JSONObject(text);sent.add(value);control(value);return true }
|
|
|
|
|
override fun send(bytes:ByteString):Boolean {audioFrames++;return true}
|
|
|
|
|
override fun close(code:Int,reason:String?)=true
|
|
|
|
|
override fun cancel() {cancelled=true}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Real production session + protocol + view/driver/renderers; only Android I/O and server are doubles. */
|
|
|
|
|
private class WaitingRig(val pack:AvatarPack,val pending:Boolean=false) {
|
|
|
|
|
val index=checkNotNull(pack.emotionAction)
|
|
|
|
|
val binding=AvatarMediaBinding(true)
|
|
|
|
|
val trace=mutableListOf<Pair<Long,VideoPresentation>>()
|
|
|
|
|
var state=VoiceUiState();var turn=0L
|
|
|
|
|
lateinit var socket:WaitingSocket
|
|
|
|
|
lateinit var listener:WebSocketListener
|
|
|
|
|
val session=VoiceSession("localhost",5011,onState={
|
|
|
|
|
state=it;binding.beginTurn(it.turnId)
|
|
|
|
|
binding.intent(it.replyEmotion.takeIf { r->r.turnId!=null },it.playbackActivity?.audible==true && !it.actionActive && !it.waitingPoseActive)
|
|
|
|
|
},reviewedActionPack=if(pending) null else index.actionPack,pendingActionPackSha={index.actionPack!!.manifestSha},
|
|
|
|
|
actionPlayer=binding,waitingPlayer=binding,onPrepareExpression=binding::prepare,persona="liam",keepPermissionConnection=false)
|
|
|
|
|
val view:EmotionActionAvatarView
|
|
|
|
|
var running=true
|
|
|
|
|
init {
|
|
|
|
|
CodecFixture.reset();CodecFixture.width=720;CodecFixture.height=1280;CodecFixture.emitNativeSurface=true;MediaPlayer.created.clear()
|
|
|
|
|
index.packages.values.forEach { CodecFixture.timelines[it.root+it.body.path]=it.body.ptsUs }
|
|
|
|
|
index.edges.mapNotNull { it.mouthRuntime }.forEach { CodecFixture.timelines[it.root+it.body.path]=it.body.ptsUs }
|
|
|
|
|
index.waiting!!.poses.forEach { CodecFixture.timelines[index.root+it.clip.path]=it.clip.ptsUs }
|
|
|
|
|
index.actionPack!!.assets.mapNotNull { it.media }.forEach { CodecFixture.timelines[it.root+it.path]=it.ptsUs;CodecFixture.audioPaths.add(it.root+it.path) }
|
|
|
|
|
socket=WaitingSocket { value -> Handler(Looper.getMainLooper()).postDelayed({
|
|
|
|
|
when(value.optString("type")) {
|
|
|
|
|
"hello"->event("hello",fields=mapOf("action_available" to !pending,"action_pending" to pending))
|
|
|
|
|
"client.capabilities.ready"->event("client.capabilities",fields=mapOf("action_available" to true,"action_pack_sha256" to index.actionPack.manifestSha))
|
|
|
|
|
"listen"->if(value.optString("state")=="start") {
|
|
|
|
|
turn++;event("abort",turn,mapOf("reason" to "listen_start"));event("listen",turn,mapOf("state" to "start"))
|
|
|
|
|
} else event("listen",turn,mapOf("state" to "stop"))
|
|
|
|
|
}
|
|
|
|
|
},1) }
|
|
|
|
|
val dialogue=field(session,"dialogue").get(session) as DialogueClient
|
|
|
|
|
field(dialogue,"socket").set(dialogue,socket)
|
|
|
|
|
session.foreground(false)
|
|
|
|
|
listener=dialogue.javaClass.getDeclaredMethod("listener",Long::class.javaPrimitiveType).apply {isAccessible=true}.invoke(dialogue,0L) as WebSocketListener
|
|
|
|
|
event("auth",fields=mapOf("state" to "ok"));TestScheduler.advanceBy(2)
|
|
|
|
|
check(state.connection==ConnectionStatus.CONNECTED)
|
|
|
|
|
val hello=socket.sent.single {it.optString("type")=="hello"}
|
|
|
|
|
check(hello.optString("persona")=="liam")
|
|
|
|
|
check(hello.optString(if(pending) "action_pending_pack_sha256" else "action_pack_sha256")==index.actionPack.manifestSha)
|
|
|
|
|
if(pending) {session.installReviewedActions(index.actionPack);TestScheduler.advanceBy(2)}
|
|
|
|
|
check(dialogue.actionNegotiation=="accepted") {"ready_ack_not_accepted"}
|
|
|
|
|
view=EmotionActionAvatarView(android.content.Context(),pack,binding,{trace.add(TestScheduler.now to it)})
|
|
|
|
|
view.render(true)
|
|
|
|
|
controlledChoices()
|
|
|
|
|
session.startVoice()
|
|
|
|
|
val mic=object:Runnable {override fun run() {
|
|
|
|
|
if(!running)return
|
|
|
|
|
if(field(session,"capturing").getBoolean(session)) AudioCapture.lastFrame?.invoke(ByteArray(640))
|
|
|
|
|
Handler(Looper.getMainLooper()).postDelayed(this,20)
|
|
|
|
|
}}
|
|
|
|
|
Handler(Looper.getMainLooper()).post(mic)
|
|
|
|
|
TestScheduler.advanceBy(2)
|
|
|
|
|
}
|
|
|
|
|
private fun controlledChoices() {
|
|
|
|
|
val driver=field(view,"driver").get(view) as ConversationMediaDriver
|
|
|
|
|
// Control the random input, never queued poses, selected targets or ownership.
|
|
|
|
|
var choice=0
|
|
|
|
|
field(driver,"chooseIdle").set(driver,{ count:Int->(choice++%4).also {check(count==4)} })
|
|
|
|
|
field(driver,"chooseIdleCycles").set(driver,{2});field(driver,"idleCycleGoal").setInt(driver,2)
|
|
|
|
|
}
|
|
|
|
|
fun returnFromBackground() {
|
|
|
|
|
val dialogue=field(session,"dialogue").get(session) as DialogueClient
|
|
|
|
|
// Transport adapter supplies a new authenticated socket, without opening a
|
|
|
|
|
// real health/WS request. Session/renderer ownership and waiting gates stay real.
|
|
|
|
|
field(dialogue,"authReceived").setBoolean(dialogue,false)
|
|
|
|
|
field(dialogue,"helloSent").setBoolean(dialogue,false)
|
|
|
|
|
field(dialogue,"pendingOffered").set(dialogue,null)
|
|
|
|
|
field(dialogue,"readyUpdateSent").setBoolean(dialogue,false)
|
|
|
|
|
socket=WaitingSocket { value -> Handler(Looper.getMainLooper()).postDelayed({
|
|
|
|
|
when(value.optString("type")) {
|
|
|
|
|
"hello"->event("hello",fields=mapOf("action_available" to true,"action_pending" to false))
|
|
|
|
|
"listen"->if(value.optString("state")=="start") {
|
|
|
|
|
turn++;event("abort",turn,mapOf("reason" to "listen_start"));event("listen",turn,mapOf("state" to "start"))
|
|
|
|
|
} else event("listen",turn,mapOf("state" to "stop"))
|
|
|
|
|
}
|
|
|
|
|
},1) }
|
|
|
|
|
field(dialogue,"socket").set(dialogue,socket)
|
|
|
|
|
val epoch=field(dialogue,"generation").getLong(dialogue)
|
|
|
|
|
listener=dialogue.javaClass.getDeclaredMethod("listener",Long::class.javaPrimitiveType).apply {isAccessible=true}.invoke(dialogue,epoch) as WebSocketListener
|
|
|
|
|
session.foreground(false);event("auth",fields=mapOf("state" to "ok"));TestScheduler.advanceBy(2)
|
|
|
|
|
check(state.connection==ConnectionStatus.CONNECTED && dialogue.actionNegotiation=="accepted")
|
|
|
|
|
view.render(true);controlledChoices();session.startVoice();TestScheduler.advanceBy(2)
|
|
|
|
|
}
|
|
|
|
|
fun event(type:String,id:Long?=null,fields:Map<String,Any> = emptyMap()) {
|
|
|
|
|
val value=JSONObject().put("type",type);if(id!=null)value.put("turn_id",id)
|
|
|
|
|
fields.forEach {(k,v)->value.put(k,v)}
|
|
|
|
|
listener.onMessage(socket,value.toString())
|
|
|
|
|
}
|
|
|
|
|
fun pcm(id:Long=turn) {
|
|
|
|
|
val bytes=ByteBuffer.allocate(12+1920).order(ByteOrder.BIG_ENDIAN)
|
|
|
|
|
.put('L'.code.toByte()).put('V'.code.toByte()).put(1.toByte()).put(2.toByte()).putInt(id.toInt()).putInt(1).put(ByteArray(1920)).array()
|
|
|
|
|
listener.onMessage(socket,ByteString.of(*bytes))
|
|
|
|
|
}
|
|
|
|
|
fun tts() {
|
|
|
|
|
event("tts",turn,mapOf("state" to "start"))
|
|
|
|
|
event("reply.emotion",turn,mapOf("seq" to 3,"emotion" to "happy","intensity" to 1,"source" to "model"))
|
|
|
|
|
pcm();event("tts",turn,mapOf("state" to "done"));TestScheduler.advanceBy(0)
|
|
|
|
|
}
|
|
|
|
|
fun playAndDrain() {
|
|
|
|
|
val before=AudioPlayback.enqueued;tts()
|
|
|
|
|
TestScheduler.until(10_000) {AudioPlayback.enqueued>before}
|
|
|
|
|
check(binding.currentPackageId=="neutral")
|
|
|
|
|
AudioPlayback.lastPlaying!!(turn,0,true);AudioPlayback.lastActivity!!(PlaybackActivity(turn,0,true,960,1920));TestScheduler.advanceBy(0)
|
|
|
|
|
check(state.voiceIndicator==VoiceIndicatorState.SPEAKING)
|
|
|
|
|
TestScheduler.advanceBy(100);AudioPlayback.lastDrained!!(turn,0);TestScheduler.advanceBy(200)
|
|
|
|
|
check(state.phase==VoicePhase.LISTENING)
|
|
|
|
|
check(socket.sent.any {it.optString("type")=="playback.drained" && it.getLong("turn_id")==turn})
|
|
|
|
|
}
|
|
|
|
|
fun close() {running=false;session.release();view.dispose();TestScheduler.advanceBy(100);AudioCapture.confirmStopsAutomatically=true}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fun main(args:Array<String>) {
|
|
|
|
|
android.content.Archive.zip=java.util.zip.ZipFile(args.single())
|
|
|
|
|
val pack=checkNotNull(AvatarPack.load(android.content.Context()))
|
|
|
|
|
check(pack.emotionAdmission=="accepted")
|
|
|
|
|
println("SESSION_LOADER|actual_apk=true|pin=${pack.emotionAction!!.actionPack!!.manifestSha}")
|
|
|
|
|
for(pending in listOf(false,true)) {
|
|
|
|
|
TestScheduler.reset();val rig=WaitingRig(pack,pending)
|
|
|
|
|
val start=TestScheduler.now
|
|
|
|
|
TestScheduler.advanceBy(120_000)
|
|
|
|
|
val phases=rig.trace.map {it.second.actual}
|
|
|
|
|
check(listOf("body_neutral","body_neutral-v03","pose_suit-adjust","pose_knuckle-check").all {it in phases})
|
|
|
|
|
check(phases.none {it=="media_failed"})
|
|
|
|
|
check(rig.trace.filter {it.second.actual.startsWith("pose_") && it.second.actual!="pose_return_a"}.size>=386)
|
|
|
|
|
check(rig.trace.count {it.second.actual=="pose_return_a"}>=2)
|
|
|
|
|
println("WAIT_120S|pending=$pending|ms=${TestScheduler.now-start}|neutral=${phases.count {it=="body_neutral"}}|variant=${phases.count {it=="body_neutral-v03"}}|suit=${phases.count {it=="pose_suit-adjust"}}|knuckle=${phases.count {it=="pose_knuckle-check"}}|returns=${phases.count {it=="pose_return_a"}}|controlled_rng=true|device=false")
|
|
|
|
|
TestScheduler.until(90_000) {rig.binding.currentPackageId=="neutral-v03"}
|
|
|
|
|
rig.playAndDrain()
|
|
|
|
|
check(rig.state.replyEmotion.turnId==null)
|
|
|
|
|
println("TALK_FROM_VARIANT|actual=neutral|model=happy|drained=true|closed_to_wait=true")
|
|
|
|
|
for(action in 0..1) {
|
|
|
|
|
val controller=field(rig.session,"actions").get(rig.session) as ActionController
|
|
|
|
|
field(controller,"choose").set(controller,{_:Int->0})
|
|
|
|
|
val id="replay_action_${action}_0123456789"
|
|
|
|
|
AudioCapture.confirmStopsAutomatically=false
|
|
|
|
|
val oldPlayers=MediaPlayer.created.size
|
|
|
|
|
rig.event("action.plan",rig.turn,mapOf("action_id" to id,"pack_sha256" to rig.index.actionPack!!.manifestSha,"ttl_ms" to 120_000))
|
|
|
|
|
TestScheduler.advanceBy(0);check(MediaPlayer.created.size==oldPlayers)
|
|
|
|
|
AudioCapture.lastStopped!!(true);TestScheduler.advanceBy(0);AudioCapture.confirmStopsAutomatically=true
|
|
|
|
|
val movie=MediaPlayer.created.last();movie.prepared!!(movie)
|
|
|
|
|
TestScheduler.until(20_000) {movie.started}
|
|
|
|
|
TestScheduler.advanceBy(8100);movie.completion!!(movie)
|
|
|
|
|
TestScheduler.until(10_000) {rig.socket.sent.any {it.optString("type")=="action.status" && it.optString("action_id")==id && it.optString("status")=="completed"}}
|
|
|
|
|
check(movie.released && rig.binding.currentPackageId=="neutral")
|
|
|
|
|
val status=rig.socket.sent.last {it.optString("type")=="action.status"}
|
|
|
|
|
check(status.optBoolean("mic_stopped") && status.optBoolean("video_ended") && status.optBoolean("audio_stopped") && status.optBoolean("idle_restored"))
|
|
|
|
|
val previous=rig.turn;rig.turn++
|
|
|
|
|
rig.event("abort",rig.turn,mapOf("reason" to "action_followup"));rig.event("speech.start",rig.turn,mapOf("origin" to "after_action"));TestScheduler.advanceBy(0)
|
|
|
|
|
rig.playAndDrain();val before=AudioPlayback.enqueued;rig.pcm(previous);TestScheduler.advanceBy(0);check(AudioPlayback.enqueued==before)
|
|
|
|
|
println("WS_DANCE_AFTER|pending=$pending|asset=${status.optString("asset_id")}|capture_ack=true|native_audio_end=true|restored_actual_A=true|after_turn=${rig.turn}|after_drained=true|old_pcm_rejected=true")
|
|
|
|
|
}
|
|
|
|
|
rig.close()
|
|
|
|
|
}
|
|
|
|
|
for(exit in listOf("complete","cancel","background")) {
|
|
|
|
|
TestScheduler.reset();val rig=WaitingRig(pack);AudioCapture.confirmStopsAutomatically=false
|
|
|
|
|
TestScheduler.until(90_000) {rig.state.waitingPoseActive}
|
|
|
|
|
check(rig.trace.none {it.second.actual.startsWith("pose_")})
|
|
|
|
|
val ack=checkNotNull(AudioCapture.lastStopped);val lateMic=checkNotNull(AudioCapture.lastFrame)
|
|
|
|
|
val audio=AudioPlayback.enqueued;val uplink=rig.socket.audioFrames;val oldTurn=rig.turn
|
|
|
|
|
lateMic(ByteArray(640));rig.pcm();rig.event("tts",rig.turn,mapOf("state" to "done"));rig.event("listen",rig.turn+1,mapOf("state" to "start"));TestScheduler.advanceBy(100)
|
|
|
|
|
check(rig.socket.audioFrames==uplink && AudioPlayback.enqueued==audio)
|
|
|
|
|
ack(true);TestScheduler.until(1000) {rig.trace.any {it.second.actual.startsWith("pose_")}}
|
|
|
|
|
repeat(40) {lateMic(ByteArray(640));rig.pcm(oldTurn)};TestScheduler.advanceBy(100)
|
|
|
|
|
check(rig.socket.audioFrames==uplink && AudioPlayback.enqueued==audio && rig.state.voiceIndicator==VoiceIndicatorState.IDLE)
|
|
|
|
|
when(exit) {
|
|
|
|
|
"complete"->{TestScheduler.until(10_000) {!rig.state.waitingPoseActive};AudioCapture.confirmStopsAutomatically=true;TestScheduler.advanceBy(1000);check(rig.state.voiceIndicator==VoiceIndicatorState.LISTENING)}
|
|
|
|
|
"cancel"->{rig.session.stopVoice();TestScheduler.advanceBy(1000);check(!rig.state.voiceActive && !rig.state.waitingPoseActive)}
|
|
|
|
|
"background"->{rig.session.background();rig.view.render(false);TestScheduler.advanceBy(1000);check(!rig.state.voiceActive && rig.socket.cancelled)}
|
|
|
|
|
}
|
|
|
|
|
ack(true);rig.pcm(oldTurn);TestScheduler.advanceBy(0);check(AudioPlayback.enqueued==audio)
|
|
|
|
|
if(exit!="complete") {
|
|
|
|
|
val oldSocket=rig.socket;val oldListener=rig.listener
|
|
|
|
|
AudioCapture.confirmStopsAutomatically=true
|
|
|
|
|
val before=rig.trace.size
|
|
|
|
|
if(exit=="cancel") rig.session.startVoice() else rig.returnFromBackground()
|
|
|
|
|
TestScheduler.until(90_000) {rig.trace.drop(before).any {it.second.actual=="pose_suit-adjust" || it.second.actual=="pose_knuckle-check"}}
|
|
|
|
|
check(rig.trace.drop(before).any {it.second.actual=="pose_suit-adjust" || it.second.actual=="pose_knuckle-check"})
|
|
|
|
|
if(exit=="background") {
|
|
|
|
|
oldListener.onMessage(oldSocket,"""{"type":"tts","state":"start","turn_id":$oldTurn}""")
|
|
|
|
|
TestScheduler.advanceBy(0);check(AudioPlayback.enqueued==audio)
|
|
|
|
|
}
|
|
|
|
|
TestScheduler.until(10_000) {!rig.state.waitingPoseActive}
|
|
|
|
|
TestScheduler.until(1_000) {rig.state.voiceIndicator==VoiceIndicatorState.LISTENING}
|
|
|
|
|
check(rig.state.voiceActive && rig.state.voiceIndicator==VoiceIndicatorState.LISTENING)
|
|
|
|
|
}
|
|
|
|
|
println("POSE_FENCES|exit=$exit|delayed_capture_ack=true|before_ack_pose_frames=0|late_mic_tts_listen_rejected=true|actual_restore=true|reschedules_after_restart=${exit!="complete"}|device=false")
|
|
|
|
|
rig.close()
|
|
|
|
|
}
|
|
|
|
|
println("PASS: actual APK production session/protocol/View/renderer 120s four-way waiting, dedicated TALK, delayed recorder ACK, old callback/cancel/background, ready+pending HELLO, both dance/completed/after_action; Android audio/codec/EGL/server doubled, no live inference")
|
|
|
|
|
}
|