|
|
|
@@ -3,7 +3,6 @@ package com.gorios.lanvoice.avatar
|
|
|
|
|
import com.gorios.lanvoice.diag.DiagnosticLog
|
|
|
|
|
import android.content.Context
|
|
|
|
|
import android.graphics.BitmapFactory
|
|
|
|
|
import android.graphics.Matrix
|
|
|
|
|
import android.graphics.SurfaceTexture
|
|
|
|
|
import android.media.MediaPlayer
|
|
|
|
|
import android.os.Handler
|
|
|
|
@@ -92,8 +91,7 @@ class EmotionActionAvatarView(context: Context,private val pack: AvatarPack,
|
|
|
|
|
}
|
|
|
|
|
private fun fitMovie() {
|
|
|
|
|
val view=textures[3];if(view.width<=0 || view.height<=0) return
|
|
|
|
|
val fit=fitVideo(view.width,view.height,480,848)
|
|
|
|
|
view.setTransform(Matrix().apply { setScale(fit.scaleX,fit.scaleY,view.width/2f,view.height/2f) })
|
|
|
|
|
movie?.resize(view.width,view.height)
|
|
|
|
|
}
|
|
|
|
|
private fun suspend() {
|
|
|
|
|
main.removeCallbacks(summary)
|
|
|
|
@@ -104,86 +102,109 @@ class EmotionActionAvatarView(context: Context,private val pack: AvatarPack,
|
|
|
|
|
override fun onDetachedFromWindow() { dispose();super.onDetachedFromWindow() }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Android's completion is combined with actual final TextureView PTS and release.
|
|
|
|
|
* No timer substitutes for MediaPlayer.stop/release or idle presentation. */
|
|
|
|
|
/** Video uses the existing controlled codec/compositor's decoded+latched source PTS.
|
|
|
|
|
* MediaPlayer supplies audio only: its TextureView timestamp is a presentation clock,
|
|
|
|
|
* not a source-frame index. Completion still requires final video, native audio end,
|
|
|
|
|
* actual resource release and the driver's reviewed return A. */
|
|
|
|
|
internal class AndroidMusicPort(private val context: Context,private val view: TextureView,private val diagnostic: (String)->Unit={}): MusicMediaPort {
|
|
|
|
|
private val main=Handler(Looper.getMainLooper())
|
|
|
|
|
private var player: MediaPlayer?=null
|
|
|
|
|
private var surface: Surface?=null
|
|
|
|
|
private var video: ContinuousBodyRenderer?=null
|
|
|
|
|
private var epoch=0L
|
|
|
|
|
private var media: ActionMedia?=null
|
|
|
|
|
private var firstFrame: (()->Unit)?=null
|
|
|
|
|
private var ready: (()->Unit)?=null
|
|
|
|
|
private var ended: (()->Unit)?=null
|
|
|
|
|
private var failed: (()->Unit)?=null
|
|
|
|
|
private var lastPts=-1L
|
|
|
|
|
private var lastIndex=-1
|
|
|
|
|
private var playing=false
|
|
|
|
|
private var audioReady=false
|
|
|
|
|
private var readySent=false
|
|
|
|
|
private var nativeEnded=false
|
|
|
|
|
private var videoEnded=false
|
|
|
|
|
private var startedMs=0L
|
|
|
|
|
private var faulted=false
|
|
|
|
|
private fun event(reason: String, timestampUs: Long?=null) {
|
|
|
|
|
val position=try { player?.currentPosition ?: -1 } catch(_: Exception) { -1 }
|
|
|
|
|
val message="event=$reason epoch=$epoch playing=$playing ended=$nativeEnded elapsed_ms=${SystemClock.elapsedRealtime()-startedMs} position_ms=$position pts_us=${timestampUs ?: lastPts} layer_alpha=${view.alpha}"
|
|
|
|
|
val message="event=$reason epoch=$epoch playing=$playing ended=$nativeEnded video_ended=$videoEnded elapsed_ms=${SystemClock.elapsedRealtime()-startedMs} position_ms=$position pts_us=$lastPts frame=$lastIndex output_us=${timestampUs ?: -1} layer_alpha=${view.alpha}"
|
|
|
|
|
diagnostic(message)
|
|
|
|
|
DiagnosticLog.state("music_native", "$epoch/$reason",message)
|
|
|
|
|
}
|
|
|
|
|
private fun fault(reason: String,timestampUs: Long?=null) {
|
|
|
|
|
private fun fault(reason: String) {
|
|
|
|
|
if(faulted) return
|
|
|
|
|
faulted=true;event(reason,timestampUs);failed?.invoke()
|
|
|
|
|
faulted=true;event(reason);failed?.invoke()
|
|
|
|
|
}
|
|
|
|
|
private fun announceReady() {
|
|
|
|
|
if(!faulted && audioReady && lastIndex==0 && !readySent) { readySent=true;event("prepared");ready?.invoke() }
|
|
|
|
|
}
|
|
|
|
|
override fun prepare(media: ActionMedia,ready: ()->Unit,firstFrame: ()->Unit,ended: ()->Unit,failed: ()->Unit) {
|
|
|
|
|
check(player==null);val token=++epoch
|
|
|
|
|
this.media=media;this.firstFrame=firstFrame;this.ended=ended;this.failed=failed;lastPts=-1;faulted=false;startedMs=SystemClock.elapsedRealtime();event("preparing")
|
|
|
|
|
check(player==null && video==null);val token=++epoch
|
|
|
|
|
this.media=media;this.ready=ready;this.firstFrame=firstFrame;this.ended=ended;this.failed=failed
|
|
|
|
|
lastPts=-1;lastIndex=-1;faulted=false;audioReady=false;readySent=false;videoEnded=false;nativeEnded=false
|
|
|
|
|
startedMs=SystemClock.elapsedRealtime();event("preparing")
|
|
|
|
|
try {
|
|
|
|
|
val s=Surface(checkNotNull(view.surfaceTexture));surface=s
|
|
|
|
|
val current=MediaPlayer();player=current;current.setSurface(s);current.isLooping=false
|
|
|
|
|
val body=ContinuousBodyAsset(media.path,media.sha256,media.width,media.height,media.ptsUs.size,
|
|
|
|
|
media.durationUs,media.ptsUs,media.fpsNumerator,media.fpsDenominator)
|
|
|
|
|
// Genuine body-only action: no registered mouth assets or conversation patch reuse.
|
|
|
|
|
val pack=ContinuousAvatarPack(body,emptyList(),emptyMap(),media.root,media.sha256)
|
|
|
|
|
video=ContinuousBodyRenderer(context,pack,Surface(checkNotNull(view.surfaceTexture)),view.width,view.height,
|
|
|
|
|
{ value,_-> if(token==epoch && !faulted) {
|
|
|
|
|
val key=value.key
|
|
|
|
|
if(key.loopOrdinal!=0L || key.index!=lastIndex+1 || (media.ptsUs.getOrNull(key.index)?.let { kotlin.math.abs(it-key.ptsUs)<=2 }!=true) || value.selection.opacity!=0f) fault("video_frame_invalid")
|
|
|
|
|
else {
|
|
|
|
|
lastIndex=key.index;lastPts=key.ptsUs
|
|
|
|
|
if(!playing) announceReady() else { if(key.index==media.ptsUs.lastIndex) event("final_codec_frame");video?.advance() }
|
|
|
|
|
}
|
|
|
|
|
} },{ _-> if(token==epoch) fault("video_decoder_or_gl") },automatic=false,mayLoop=false,
|
|
|
|
|
mouthEnabled=false,videoOnlyFromAv=true,ended={ if(token==epoch && !faulted) {
|
|
|
|
|
videoEnded=true;event("video_completed");finishIfReady()
|
|
|
|
|
main.postDelayed({ if(token==epoch && videoEnded && !nativeEnded && playing) fault("audio_end_missing") },2_000)
|
|
|
|
|
} })
|
|
|
|
|
val current=MediaPlayer();player=current;current.setSurface(null);current.isLooping=false
|
|
|
|
|
context.assets.openFd(media.root+media.path).use { current.setDataSource(it.fileDescriptor,it.startOffset,it.length) }
|
|
|
|
|
current.setOnPreparedListener { if(token==epoch) {
|
|
|
|
|
if(current.videoWidth!=media.width || current.videoHeight!=media.height) fault("dimensions_invalid") else { event("prepared");ready() }
|
|
|
|
|
} }
|
|
|
|
|
current.setOnCompletionListener { if(token==epoch && playing) {
|
|
|
|
|
playing=false;nativeEnded=true;event("native_completed");finishIfReady()
|
|
|
|
|
main.postDelayed({ if(token==epoch && nativeEnded) fault("native_ended_missing_final") },2_000) // Missing final-frame watchdog, no fabricated completion.
|
|
|
|
|
current.setOnPreparedListener { if(token==epoch && !faulted) { audioReady=true;event("audio_prepared");announceReady() } }
|
|
|
|
|
current.setOnCompletionListener { if(token==epoch && playing && !faulted) {
|
|
|
|
|
if(SystemClock.elapsedRealtime()-startedMs+50 < media.audioEndUs/1000) fault("audio_duration_early")
|
|
|
|
|
else { nativeEnded=true;event("native_completed");finishIfReady() }
|
|
|
|
|
// Missing video progress retains the decoder's bounded watchdog, never fabricates an end.
|
|
|
|
|
} }
|
|
|
|
|
current.setOnInfoListener { _,what,_->
|
|
|
|
|
if(token==epoch && what==MediaPlayer.MEDIA_INFO_VIDEO_RENDERING_START) event("native_rendering_start")
|
|
|
|
|
true // Native info is not a latched frame or completion proof.
|
|
|
|
|
false // Informational only; audio player owns no visible surface.
|
|
|
|
|
}
|
|
|
|
|
current.setOnErrorListener { _,_,_->if(token==epoch) fault("native_error");true }
|
|
|
|
|
current.prepareAsync()
|
|
|
|
|
} catch(_: Exception) { fault("prepare_failed") }
|
|
|
|
|
}
|
|
|
|
|
override fun start() { if(faulted) return;try { startedMs=SystemClock.elapsedRealtime();nativeEnded=false;playing=true;checkNotNull(player).start();event("started") } catch(_: Exception) { fault("start_failed") } }
|
|
|
|
|
fun latched(timestampNs: Long) {
|
|
|
|
|
if(faulted || (!playing && !nativeEnded)) return
|
|
|
|
|
val pts=timestampNs/1000;val timeline=media?.ptsUs ?: return
|
|
|
|
|
val found=timeline.binarySearch(pts)
|
|
|
|
|
val candidate=if(found>=0) found else (-found-1).coerceAtMost(timeline.lastIndex)
|
|
|
|
|
val index=listOf(candidate,(candidate-1).coerceAtLeast(0)).firstOrNull { kotlin.math.abs(timeline[it]-pts)<=2 }
|
|
|
|
|
if(index==null) { fault("timestamp_not_indexed",pts);return }
|
|
|
|
|
if(timeline[index]<lastPts) { fault("timestamp_regressed",pts);return }
|
|
|
|
|
if(lastPts<0) {
|
|
|
|
|
if(index!=0) { fault("first_frame_not_zero",pts);return }
|
|
|
|
|
event("first_frame_latched",pts)
|
|
|
|
|
firstFrame?.invoke();firstFrame=null
|
|
|
|
|
}
|
|
|
|
|
lastPts=timeline[index]
|
|
|
|
|
finishIfReady()
|
|
|
|
|
}
|
|
|
|
|
private fun finishIfReady() {
|
|
|
|
|
override fun start() {
|
|
|
|
|
if(faulted) return
|
|
|
|
|
try {
|
|
|
|
|
check(readySent && lastIndex==0 && !playing)
|
|
|
|
|
startedMs=SystemClock.elapsedRealtime();playing=true;checkNotNull(player).start();event("started")
|
|
|
|
|
// Frame0 is already decoded, input-latched, composited and held on this action surface.
|
|
|
|
|
event("first_codec_frame");firstFrame?.invoke();firstFrame=null;video?.advance()
|
|
|
|
|
} catch(_: Exception) { fault("start_failed") }
|
|
|
|
|
}
|
|
|
|
|
/** EGL output presentation timestamps are diagnostic only. They cannot index source media.
|
|
|
|
|
* Zero/old/epoch timestamps from SurfaceTexture updates never grant an entry or completion. */
|
|
|
|
|
fun latched(timestampNs: Long) { if(playing && !faulted) event("output_texture_updated",timestampNs/1000) }
|
|
|
|
|
fun resize(width: Int,height: Int) { video?.resize(width,height) }
|
|
|
|
|
private fun finishIfReady() {
|
|
|
|
|
val current=media ?: return
|
|
|
|
|
if(nativeEnded && lastPts==current.ptsUs.last()) {
|
|
|
|
|
nativeEnded=false
|
|
|
|
|
if(!faulted && playing && nativeEnded && videoEnded && lastIndex==current.ptsUs.lastIndex && kotlin.math.abs(lastPts-current.ptsUs.last())<=2) {
|
|
|
|
|
if(SystemClock.elapsedRealtime()-startedMs<(current.durationUs+999)/1000) fault("duration_early")
|
|
|
|
|
else { event("validated_end");ended?.invoke() }
|
|
|
|
|
else { playing=false;event("validated_end");ended?.invoke() }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
override fun close(released: ()->Unit) {
|
|
|
|
|
event("closing");epoch++;playing=false;nativeEnded=false;firstFrame=null;ended=null
|
|
|
|
|
val old=player
|
|
|
|
|
try { try { old?.stop() } catch(_: Exception) { } // release also stops a prepared/error player.
|
|
|
|
|
old?.release();player=null;surface?.release();surface=null;media=null
|
|
|
|
|
event("released");main.post { released() }
|
|
|
|
|
event("closing");epoch++;playing=false;nativeEnded=false;firstFrame=null;ready=null;ended=null
|
|
|
|
|
val old=player;val oldVideo=video;video=null
|
|
|
|
|
try {
|
|
|
|
|
try { old?.stop() } catch(_: Exception) { } // release also stops a prepared/error player.
|
|
|
|
|
old?.release();player=null;media=null;event("audio_released")
|
|
|
|
|
val finish={ event("released");released() }
|
|
|
|
|
oldVideo?.close(finish) ?: main.post(finish)
|
|
|
|
|
} catch(_: Exception) { event("release_failed");val callback=failed;failed=null;callback?.invoke() } // No false release acknowledgement.
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|