photos: show a motion photo's still until its clip plays
- Commit
7729e1a08685cbae3610a62db1e80934403f783b- Author
- Vayun Mathur <[email protected]>
- When
- 2026-12-05 16:11 UTC (11 hours ago)
- Parents
52268b5
Extract the viewer's media decision into the pure MotionPhotoViewer policy: a motion photo renders STILL until its extracted clip is actually playing, MOTION_CLIP only while playing with a file in hand, and VIDEO only for real video photos. PhotoDetailMedia now switches on that decision instead of an inline negated condition, so the still-first contract is unit-tested directly. Extend AvifMotionPhotoTest with a fixture shaped exactly like the camera's corrected mux output (primary av01 + ispe in the first mdat, XMP mime item in a second mdat, cdsc to the primary, MP4 trailer): still dimensions and the extracted clip both resolve from it, and the still alone resolves the same dimensions. Verified: ./gradlew :photos:lint :photos:lintVitalRelease green; :photos:testDevUnitTest 448 tests green; :photos:testDevUnitTestNative 415 tests with only the known pre-existing VaultViewerPageRealTest failure, proven identical on a clean origin/main run.
Diff
photos/AGENTS.md
diff --git a/photos/AGENTS.md b/photos/AGENTS.mdindex c0db88c63d..8fa7aa58f2 100644--- a/photos/AGENTS.md+++ b/photos/AGENTS.md@@ -73,6 +73,7 @@ _Install: ./install photos (dev by default)._ ### domain/ — OCR, motion-photo and face-grouping logic (pure) - com/vayunmathur/photos/domain/PhotoOcr.kt - com/vayunmathur/photos/domain/MotionPhotoXmp.kt — GCamera MicroVideo XMP parsing (clip length from EOF) for JPEG and AVIF motion photos+- com/vayunmathur/photos/domain/MotionPhotoViewer.kt — viewer media decision: a motion photo renders its still until its extracted clip is actually playing (plain videos always take the video branch) - com/vayunmathur/photos/domain/AvifMotionPhoto.kt — ISOBMFF XMP-item parsing + trailer extraction for AVIF motion photos - com/vayunmathur/photos/domain/FaceAlignment.kt — five-point ArcFace alignment - com/vayunmathur/photos/domain/FaceQuality.kt — which faces may form people
photos/src/main/java/com/vayunmathur/photos/domain/MotionPhotoViewer.kt
diff --git a/photos/src/main/java/com/vayunmathur/photos/domain/MotionPhotoViewer.kt b/photos/src/main/java/com/vayunmathur/photos/domain/MotionPhotoViewer.ktnew file mode 100644index 0000000000..2ebe19a02b--- /dev/null+++ b/photos/src/main/java/com/vayunmathur/photos/domain/MotionPhotoViewer.kt@@ -0,0 +1,30 @@+package com.vayunmathur.photos.domain++/** What one viewer page renders for a photo. */+enum class ViewerMedia {+ /** The still image (the default for every photo, motion or not). */+ STILL,++ /** The extracted motion-photo clip, only while playing. */+ MOTION_CLIP,++ /** A plain video photo. */+ VIDEO,+}++/**+ * Viewer media decision for one page. A motion photo is a still photo+ * first: it renders [ViewerMedia.STILL] until the user plays the clip+ * and extraction has produced a file, and returns to the still when+ * playback stops. (A regression here — routing a motion photo to the+ * video branch before play, or staying on it after — makes the viewer+ * show a black video surface instead of the photo.)+ */+object MotionPhotoViewer {+ fun media(hasVideoData: Boolean, motionPlaying: Boolean, hasMotionFile: Boolean): ViewerMedia =+ when {+ hasVideoData -> ViewerMedia.VIDEO+ motionPlaying && hasMotionFile -> ViewerMedia.MOTION_CLIP+ else -> ViewerMedia.STILL+ }+}
photos/src/main/java/com/vayunmathur/photos/ui/PhotoDetailMedia.kt
diff --git a/photos/src/main/java/com/vayunmathur/photos/ui/PhotoDetailMedia.kt b/photos/src/main/java/com/vayunmathur/photos/ui/PhotoDetailMedia.ktindex 5c03c67cd5..710d58e0c5 100644--- a/photos/src/main/java/com/vayunmathur/photos/ui/PhotoDetailMedia.kt+++ b/photos/src/main/java/com/vayunmathur/photos/ui/PhotoDetailMedia.kt@@ -22,6 +22,8 @@ import com.vayunmathur.library.image.compose.AsyncImage import com.vayunmathur.library.util.sharedContainer import com.vayunmathur.photos.data.Photo import com.vayunmathur.photos.domain.GainMapPlaybackMode+import com.vayunmathur.photos.domain.MotionPhotoViewer+import com.vayunmathur.photos.domain.ViewerMedia import com.vayunmathur.photos.platform.rememberUltraHdrPlayback import java.io.File @@ -55,7 +57,15 @@ fun PhotoDetailMedia( } else { GainMapPlaybackMode.SDR }- if (photo.videoData == null && !(motionPlaying && motionFile != null)) {+ // A motion photo shows its still until the clip is actually+ // playing (see MotionPhotoViewer); only a real video photo or an+ // actively-playing extracted clip takes a video surface.+ val media = MotionPhotoViewer.media(+ hasVideoData = photo.videoData != null,+ motionPlaying = motionPlaying,+ hasMotionFile = motionFile != null,+ )+ if (media == ViewerMedia.STILL) { val imageModifier = Modifier.fillMaxSize() .onGloballyPositioned { layoutCoordinates ->@@ -109,7 +119,7 @@ fun PhotoDetailMedia( ) } }- } else if (motionPlaying && motionFile != null) {+ } else if (media == ViewerMedia.MOTION_CLIP) { // Motion-photo playback. The seek bar is the frame inspector: // scrubbing pauses polling and seeks on release, like videos. // Autoplay is gated on the settled page, mirroring plain videos.
photos/src/test/java/com/vayunmathur/photos/domain/AvifMotionPhotoTest.kt
diff --git a/photos/src/test/java/com/vayunmathur/photos/domain/AvifMotionPhotoTest.kt b/photos/src/test/java/com/vayunmathur/photos/domain/AvifMotionPhotoTest.ktindex 16ecf1e302..9c4b7b54cb 100644--- a/photos/src/test/java/com/vayunmathur/photos/domain/AvifMotionPhotoTest.kt+++ b/photos/src/test/java/com/vayunmathur/photos/domain/AvifMotionPhotoTest.kt@@ -68,6 +68,72 @@ class AvifMotionPhotoTest { assertNull(AvifMotionPhoto.extractVideo(byteArrayOf(1, 2, 3))) } + /**+ * A file shaped exactly like the camera's corrected Rust mux+ * output: primary av01 item (id 1, ispe 64x48) in the first mdat,+ * XMP mime item (id 2) in a second trailing mdat with a cdsc+ * reference to the primary, then the MP4 trailer. The still's+ * dimensions and the clip must both resolve from it.+ */+ private fun muxShapedMotionAvif(mp4: ByteArray): ByteArray {+ val xmp = """<x:xmpmeta><rdf:RDF><rdf:Description GCamera:MotionPhoto="1" """ ++ """GCamera:MicroVideo="1" GCamera:MicroVideoOffset="${mp4.size}"/></rdf:RDF></x:xmpmeta>"""+ val xmpBytes = xmp.toByteArray(Charsets.UTF_8)+ val av1Bytes = byteArrayOf(0x12, 0x34, 0x56, 0x78) // stand-in primary bitstream+ fun build(primaryOffset: Int, xmpOffset: Int): ByteArray {+ val pitm = box("pitm", byteArrayOf(0, 0, 0, 0) + u16(1))+ val av01Infe = box(+ "infe",+ byteArrayOf(2, 0, 0, 0) + u16(1) + u16(0) + "av01".toByteArray() + byteArrayOf(0),+ )+ val mimeInfe = box(+ "infe",+ byteArrayOf(2, 0, 0, 0) + u16(2) + u16(0) + "mime".toByteArray() + byteArrayOf(0) ++ "application/rdf+xml".toByteArray() + byteArrayOf(0) + byteArrayOf(0),+ )+ val iinf = box("iinf", u32(0) + u16(2) + av01Infe + mimeInfe)+ // iloc v1, 4-byte offsets/lengths, one extent per item.+ val ilocPayload = byteArrayOf(1, 0, 0, 0) + byteArrayOf(0x44, 0) + u16(2) ++ u16(1) + u16(0) + u16(0) + u16(1) + u32(primaryOffset) + u32(av1Bytes.size) ++ u16(2) + u16(0) + u16(0) + u16(1) + u32(xmpOffset) + u32(xmpBytes.size)+ val iloc = box("iloc", ilocPayload)+ // cdsc payload is from/to ids only — no version/flags word.+ val cdsc = box("cdsc", u16(2) + u16(1) + u16(1))+ val iref = box("iref", u32(0) + cdsc)+ val ispe = box(+ "ispe",+ byteArrayOf(0, 0, 0, 0) + u32(64) + u32(48),+ )+ val ipco = box("ipco", ispe)+ val ipma = box(+ "ipma",+ byteArrayOf(0, 0, 0, 0) + u16(1) + u16(1) + byteArrayOf(1) + byteArrayOf(1),+ )+ val iprp = box("iprp", ipco + ipma)+ val meta = box("meta", u32(0) + pitm + iloc + iinf + iref + iprp)+ val ftyp = box("ftyp", "avif".toByteArray() + u32(0) + "avifmif1".toByteArray())+ return ftyp + meta + box("mdat", av1Bytes) + box("mdat", xmpBytes) + mp4+ }+ val probe = build(0, 0)+ val primaryOffset = probe.size - mp4.size - (8 + xmpBytes.size) - av1Bytes.size+ val xmpOffset = probe.size - mp4.size - xmpBytes.size+ return build(primaryOffset, xmpOffset)+ }++ @Test fun muxShapedFileYieldsStillDimensionsAndClip() {+ val bytes = muxShapedMotionAvif(mp4)+ // (a) The still resolves: primary ispe dimensions survive the+ // XMP item, the second mdat and the MP4 trailer.+ assertEquals(ImageDimensions(64, 48), ImageDimensionParser.forBytes(bytes))+ // And on the still alone (trailer removed), as a plain decoder+ // sees it: same dimensions, no motion parse required.+ val stillOnly = bytes.copyOf(bytes.size - mp4.size)+ assertEquals(ImageDimensions(64, 48), ImageDimensionParser.forBytes(stillOnly))+ // (b) The clip is the trailer, counted back from EOF.+ assertTrue(AvifMotionPhoto.extractVideo(bytes)!!.contentEquals(mp4))+ assertTrue(AvifMotionPhoto.parse(bytes) != null)+ }+ @Test fun motionVideoStartIsFromEndOfFile() { // MicroVideoOffset is the clip length (bytes from EOF), not an // absolute start: a 1000-byte file with a 200-byte clip starts at 800.
photos/src/test/java/com/vayunmathur/photos/domain/MotionPhotoViewerTest.kt
diff --git a/photos/src/test/java/com/vayunmathur/photos/domain/MotionPhotoViewerTest.kt b/photos/src/test/java/com/vayunmathur/photos/domain/MotionPhotoViewerTest.ktnew file mode 100644index 0000000000..eaafc420fb--- /dev/null+++ b/photos/src/test/java/com/vayunmathur/photos/domain/MotionPhotoViewerTest.kt@@ -0,0 +1,51 @@+package com.vayunmathur.photos.domain++import kotlin.test.Test+import kotlin.test.assertEquals++/** Tests for [MotionPhotoViewer]: a motion photo is a still first. */+class MotionPhotoViewerTest {++ @Test fun motionPhotoShowsStillBeforePlay() {+ // The reported regression shape: an AVIF motion photo opened in+ // the viewer must render its still, not a video surface, until+ // the user actually plays the clip.+ assertEquals(+ ViewerMedia.STILL,+ MotionPhotoViewer.media(hasVideoData = false, motionPlaying = false, hasMotionFile = false)+ )+ assertEquals(+ ViewerMedia.STILL,+ MotionPhotoViewer.media(hasVideoData = false, motionPlaying = false, hasMotionFile = true)+ )+ }++ @Test fun motionPhotoShowsClipOnlyWhilePlayingWithExtractedFile() {+ assertEquals(+ ViewerMedia.MOTION_CLIP,+ MotionPhotoViewer.media(hasVideoData = false, motionPlaying = true, hasMotionFile = true)+ )+ // Play requested but extraction has not produced a file yet:+ // stay on the still rather than a blank video surface.+ assertEquals(+ ViewerMedia.STILL,+ MotionPhotoViewer.media(hasVideoData = false, motionPlaying = true, hasMotionFile = false)+ )+ // Playback stopped: back to the still.+ assertEquals(+ ViewerMedia.STILL,+ MotionPhotoViewer.media(hasVideoData = false, motionPlaying = false, hasMotionFile = true)+ )+ }++ @Test fun plainVideoAlwaysTakesVideoBranch() {+ assertEquals(+ ViewerMedia.VIDEO,+ MotionPhotoViewer.media(hasVideoData = true, motionPlaying = false, hasMotionFile = false)+ )+ assertEquals(+ ViewerMedia.VIDEO,+ MotionPhotoViewer.media(hasVideoData = true, motionPlaying = true, hasMotionFile = true)+ )+ }+}