fix(android): stop the rotation cross-fade replaying the old video frame
Rotating with native video on shows the previous frame flashing in what become
the letterbox bars. It reads as a TextureView artefact — the view retains its
last frame, so between the rotation and fitSurfaceToScreen() landing that frame
sits at the old size — and two fixes were built on that reading:
1. reveal after two postOnAnimation hops. An animation frame is not a video
frame; at 24fps the next decoded frame can be several vsyncs away.
2. reveal on onSurfaceTextureUpdated, i.e. when a real frame lands. This meant
owning the SurfaceTextureListener and handing ExoPlayer the Surface directly
instead of via setVideoTextureView, which installs its own and leaves us
blind to frame arrival.
Neither stopped the flash. The mechanism is the WINDOW's rotation animation:
Android cross-fades a screenshot of the old orientation, that screenshot holds
the old video frame at the old size, and nothing at the TextureView level can
reach it. The app cannot pre-empt the screenshot either — onConfigurationChanged
fires after it is taken.
So the animation itself has to go: ROTATION_ANIMATION_JUMPCUT. That was accepted
and silently ignored, and the platform said why out loud —
"VRI[MainActivity]: setLayoutParams: not fullscreen" — because the attribute is
honoured only for a fullscreen window. FLAG_FULLSCREEN is therefore set with it,
scoped to while native compositing is active so the rest of the app keeps its
normal animation. After the change that complaint is gone from logcat.
The frame-arrival reveal is kept: it replaces a fixed-timeout guess with a real
signal, and its timeout is required rather than defensive — a resize while paused
means no new frame is ever coming, and revealing a stale frame beats a
permanently black player.
NOT CONFIRMED FIXED on device. The forced-rotation harness
(settings put system user_rotation) proved unreliable here, and screenrecord
fixes its canvas at start, so a rotation inside a recording never changes frame
dimensions — which defeated two separate attempts to measure this. DR-194 is
recorded as "Needs device verification" rather than Done.
This commit is contained in:
@@ -191,6 +191,37 @@ class MainActivity : TauriActivity() {
|
||||
super.onDestroy()
|
||||
}
|
||||
|
||||
/**
|
||||
* Rotation (and any other config change this Activity handles itself).
|
||||
*
|
||||
* Two things have to happen here rather than later, and both are about the
|
||||
* *previous* video frame surviving the transition:
|
||||
*
|
||||
* - The video view is hidden until a new frame arrives. The equivalent call
|
||||
* in `fitSurfaceToScreen` runs from the content view's layout listener,
|
||||
* which is after the rotation — by then the stale frame has been on screen
|
||||
* for the whole transition.
|
||||
* - The window's rotation animation is a **cross-fade of a screenshot** of
|
||||
* the old orientation, and that screenshot contains the old video frame at
|
||||
* the old size. No amount of TextureView bookkeeping can touch it, which is
|
||||
* why hiding on frame-arrival alone did not stop the flash. `JUMPCUT` drops
|
||||
* the cross-fade, so there is no old frame to fade through; it is set only
|
||||
* while native compositing is active (see setTransparent) so the rest of
|
||||
* the app keeps the normal animation.
|
||||
*
|
||||
* TRACES: UR-003, UR-066 | DR-194
|
||||
*/
|
||||
override fun onConfigurationChanged(newConfig: android.content.res.Configuration) {
|
||||
super.onConfigurationChanged(newConfig)
|
||||
try {
|
||||
if (com.dtourolle.jellytau.player.JellyTauPlayer.isInitialized()) {
|
||||
com.dtourolle.jellytau.player.JellyTauPlayer.getInstance().hideUntilFreshFrame()
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
android.util.Log.w("MainActivity", "hideUntilFreshFrame on config change failed", e)
|
||||
}
|
||||
}
|
||||
|
||||
override fun onPictureInPictureModeChanged(
|
||||
isInPictureInPictureMode: Boolean,
|
||||
newConfig: android.content.res.Configuration
|
||||
@@ -381,6 +412,34 @@ class MainActivity : TauriActivity() {
|
||||
window.setBackgroundDrawable(
|
||||
android.graphics.drawable.ColorDrawable(color)
|
||||
)
|
||||
// Drop the rotation cross-fade while a native video surface is
|
||||
// composited behind the page. The animation fades a *screenshot* of
|
||||
// the old orientation, which still holds the previous video frame at
|
||||
// the old size — that is the "previous frame flashing in the black
|
||||
// bars", and it lives in the window animation rather than in
|
||||
// anything the TextureView owns. (DR-194)
|
||||
val attrs = window.attributes
|
||||
attrs.rotationAnimation = if (transparent) {
|
||||
android.view.WindowManager.LayoutParams.ROTATION_ANIMATION_JUMPCUT
|
||||
} else {
|
||||
android.view.WindowManager.LayoutParams.ROTATION_ANIMATION_ROTATE
|
||||
}
|
||||
window.attributes = attrs
|
||||
// `rotationAnimation` is honoured only for a **fullscreen** window —
|
||||
// the platform says so out loud, logging
|
||||
// "VRI[MainActivity]: setLayoutParams: not fullscreen" when the
|
||||
// attribute is set on ours, and then animating normally regardless.
|
||||
// Without this the JUMPCUT above is accepted and ignored, and the
|
||||
// cross-fade keeps showing the old orientation's screenshot, stale
|
||||
// video frame and all. FLAG_FULLSCREEN is deprecated for *hiding
|
||||
// system bars* (immersive mode does that, on player entry), but it
|
||||
// is still what marks the window fullscreen for this decision.
|
||||
@Suppress("DEPRECATION")
|
||||
if (transparent) {
|
||||
window.addFlags(android.view.WindowManager.LayoutParams.FLAG_FULLSCREEN)
|
||||
} else {
|
||||
window.clearFlags(android.view.WindowManager.LayoutParams.FLAG_FULLSCREEN)
|
||||
}
|
||||
android.util.Log.d("MainActivity", "WebView transparent = $transparent")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -38,6 +38,12 @@ class JellyTauPlayer(private val appContext: Context) {
|
||||
/** AudioEffect priority. Positive = higher priority than the default. */
|
||||
private const val EFFECT_PRIORITY = 1000
|
||||
|
||||
/**
|
||||
* How long to wait for a fresh frame after a resize before revealing the
|
||||
* view anyway. Playback may be paused, in which case no frame is coming.
|
||||
*/
|
||||
private const val FRESH_FRAME_TIMEOUT_MS = 400L
|
||||
|
||||
/**
|
||||
* Canonical 10-band ISO centre frequencies (Hz), mirroring EQ_BANDS in
|
||||
* settings.rs. Kept in sync deliberately: Rust owns the band layout, this
|
||||
@@ -226,6 +232,16 @@ class JellyTauPlayer(private val appContext: Context) {
|
||||
|
||||
/** TextureView for video playback — see getOrCreateSurfaceView() for why. */
|
||||
private var videoView: TextureView? = null
|
||||
|
||||
/** The Surface handed to ExoPlayer, owned here rather than by the player. */
|
||||
private var videoSurface: android.view.Surface? = null
|
||||
|
||||
/**
|
||||
* True while the view is hidden waiting for a new frame after a resize.
|
||||
* See fitSurfaceToScreen (DR-194).
|
||||
*/
|
||||
@Volatile
|
||||
private var awaitingFreshFrame = false
|
||||
/** Last reported video frame size, used to fit the surface to the screen preserving aspect ratio */
|
||||
private var videoWidth: Int = 0
|
||||
private var videoHeight: Int = 0
|
||||
@@ -1123,9 +1139,57 @@ class JellyTauPlayer(private val appContext: Context) {
|
||||
// The view is opaque where video is drawn; the WebView above it
|
||||
// is what supplies transparency, exactly as before.
|
||||
isOpaque = true
|
||||
|
||||
// Own the listener rather than calling `setVideoTextureView`,
|
||||
// which installs ExoPlayer's own and leaves us blind to frame
|
||||
// arrival. `onSurfaceTextureUpdated` is the only honest signal
|
||||
// that a NEW frame has landed in the texture, and that is
|
||||
// precisely what the letterbox artefact waits on — see
|
||||
// fitSurfaceToScreen. Handing ExoPlayer the Surface directly is
|
||||
// the same wiring `setVideoTextureView` does internally.
|
||||
//
|
||||
// TRACES: UR-003, UR-004 | DR-194
|
||||
surfaceTextureListener = object : TextureView.SurfaceTextureListener {
|
||||
override fun onSurfaceTextureAvailable(
|
||||
texture: android.graphics.SurfaceTexture,
|
||||
width: Int,
|
||||
height: Int
|
||||
) {
|
||||
videoSurface?.release()
|
||||
videoSurface = android.view.Surface(texture)
|
||||
exoPlayer.setVideoSurface(videoSurface)
|
||||
android.util.Log.d("JellyTauPlayer", "Video surface attached to ExoPlayer")
|
||||
}
|
||||
|
||||
override fun onSurfaceTextureSizeChanged(
|
||||
texture: android.graphics.SurfaceTexture,
|
||||
width: Int,
|
||||
height: Int
|
||||
) {
|
||||
}
|
||||
|
||||
override fun onSurfaceTextureDestroyed(
|
||||
texture: android.graphics.SurfaceTexture
|
||||
): Boolean {
|
||||
exoPlayer.setVideoSurface(null)
|
||||
videoSurface?.release()
|
||||
videoSurface = null
|
||||
return true
|
||||
}
|
||||
|
||||
override fun onSurfaceTextureUpdated(
|
||||
texture: android.graphics.SurfaceTexture
|
||||
) {
|
||||
// A genuinely new frame is now in the texture, so
|
||||
// whatever was retained from before the resize is gone.
|
||||
if (awaitingFreshFrame) {
|
||||
awaitingFreshFrame = false
|
||||
videoView?.alpha = 1f
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
exoPlayer.setVideoTextureView(videoView)
|
||||
android.util.Log.d("JellyTauPlayer", "Video TextureView created and attached to ExoPlayer")
|
||||
android.util.Log.d("JellyTauPlayer", "Video TextureView created")
|
||||
}
|
||||
return videoView!!.hashCode()
|
||||
}
|
||||
@@ -1194,6 +1258,31 @@ class JellyTauPlayer(private val appContext: Context) {
|
||||
* pillarbox). A raw SurfaceView with MATCH_PARENT otherwise stretches the
|
||||
* video to the surface bounds, which crops the bottom on rotation.
|
||||
*/
|
||||
/**
|
||||
* Hide the video view now, and keep it hidden until a genuinely new frame
|
||||
* arrives (or the timeout fires).
|
||||
*
|
||||
* Called from `MainActivity.onConfigurationChanged`, i.e. at the *start* of a
|
||||
* rotation. [fitSurfaceToScreen] is too late for this: it runs from the
|
||||
* content view's layout listener, after the rotation has already happened,
|
||||
* so the stale frame has been on screen for the whole transition by then.
|
||||
*
|
||||
* TRACES: UR-003, UR-066 | DR-194
|
||||
*/
|
||||
fun hideUntilFreshFrame() {
|
||||
mainHandler.post {
|
||||
val view = videoView ?: return@post
|
||||
awaitingFreshFrame = true
|
||||
view.alpha = 0f
|
||||
mainHandler.postDelayed({
|
||||
if (awaitingFreshFrame) {
|
||||
awaitingFreshFrame = false
|
||||
videoView?.alpha = 1f
|
||||
}
|
||||
}, FRESH_FRAME_TIMEOUT_MS)
|
||||
}
|
||||
}
|
||||
|
||||
fun fitSurfaceToScreen() {
|
||||
mainHandler.post {
|
||||
val view = videoView ?: return@post
|
||||
@@ -1229,38 +1318,47 @@ class JellyTauPlayer(private val appContext: Context) {
|
||||
lp.gravity = android.view.Gravity.CENTER
|
||||
}
|
||||
|
||||
// Hide the view across a resize, and reveal it once the new bounds
|
||||
// hold a freshly drawn frame.
|
||||
// Hide the view across a resize, and reveal it when a genuinely NEW
|
||||
// video frame lands in the texture.
|
||||
//
|
||||
// A TextureView retains its last frame. Between a rotation and this
|
||||
// re-fit landing, that retained frame is stretched across the OLD
|
||||
// rect — which is larger than the new one along at least one axis —
|
||||
// so the previous frame flashes in what should be the letterbox
|
||||
// bars. Nothing is wrong with the video; it is one or two frames of
|
||||
// stale texture at a stale size.
|
||||
// rect — larger than the new one along at least one axis — so the
|
||||
// previous frame flashes in what should be the letterbox bars.
|
||||
//
|
||||
// Two `postOnAnimation` hops rather than one: the first runs after
|
||||
// layout has been applied, the second after a frame has actually
|
||||
// been drawn into the new bounds, which is the thing worth waiting
|
||||
// for. Scoped to an actual size change so steady-state playback
|
||||
// never touches alpha.
|
||||
// Waiting a fixed number of animation frames does NOT fix it, which
|
||||
// the first attempt at this proved on device: an animation frame is
|
||||
// not a video frame, and at 24fps the next decoded frame can be
|
||||
// several vsyncs away. The tell was that pausing and playing cleared
|
||||
// the artefact by hand — that forces a fresh frame, which is the
|
||||
// real precondition. So the reveal is driven by
|
||||
// `onSurfaceTextureUpdated` instead.
|
||||
//
|
||||
// The timeout is not belt-and-braces, it is required: if playback is
|
||||
// paused when the resize happens, no new frame is coming and the
|
||||
// video would stay invisible forever. Revealing a stale frame after
|
||||
// a beat is strictly better than a permanently black player.
|
||||
//
|
||||
// Scoped to an actual size change so steady-state playback never
|
||||
// touches alpha.
|
||||
//
|
||||
// TRACES: UR-003, UR-066 | DR-194
|
||||
val sizeChanged = lp.width != targetW || lp.height != targetH
|
||||
if (sizeChanged) {
|
||||
awaitingFreshFrame = true
|
||||
view.alpha = 0f
|
||||
mainHandler.postDelayed({
|
||||
if (awaitingFreshFrame) {
|
||||
awaitingFreshFrame = false
|
||||
videoView?.alpha = 1f
|
||||
}
|
||||
}, FRESH_FRAME_TIMEOUT_MS)
|
||||
}
|
||||
|
||||
lp.width = targetW
|
||||
lp.height = targetH
|
||||
view.layoutParams = lp
|
||||
view.requestLayout()
|
||||
|
||||
if (sizeChanged) {
|
||||
view.postOnAnimation {
|
||||
view.postOnAnimation { view.alpha = 1f }
|
||||
}
|
||||
}
|
||||
android.util.Log.d(
|
||||
"JellyTauPlayer",
|
||||
"Video surface fitted to ${targetW}x${targetH} (video ${videoWidth}x${videoHeight}, avail ${availW}x${availH})"
|
||||
|
||||
Reference in New Issue
Block a user