Skip to content

feat(android): in-stream video mirror toggle with preview synchronization - #63

Merged
kvm404 merged 3 commits into
mainfrom
feat/video-mirror-toggle
Sep 4, 2026
Merged

kvm404 merged 3 commits into
mainfrom
feat/video-mirror-toggle

Conversation

@kvm404

@kvm404 kvm404 commented Sep 4, 2026

Copy link
Copy Markdown
Owner

Summary

Implements an in-stream "Mirror" toggle feature in the Android app (android/) allowing users to horizontally flip their streamed video and synchronized viewfinder preview on demand.

Key Highlights

  • Zero GC Allocations on Frame Path: FrameConverter.flipHorizontallyInPlace uses an in-place two-pointer byte reversal for Y, U, and V planes directly modifying the buffer with 0 heap allocations.
  • Preview Synchronization Math:
    CameraX's PreviewView automatically applies a mirror transformation to the front-facing camera for natural selfie orientation, but displays back-facing camera unmirrored:
    • Back Camera:
      • Unmirrored (isMirrored = false): PreviewView is normal (scaleX = 1f), stream is normal.
      • Mirrored (isMirrored = true): PreviewView inverted (scaleX = -1f), stream is horizontally flipped.
    • Front Camera:
      • Unmirrored (isMirrored = false): Stream has raw upright sensor pixels. To match the unmirrored stream, PreviewView's default selfie mirror must be inverted (scaleX = -1f).
      • Mirrored (isMirrored = true): Stream is horizontally flipped to look like a mirror. PreviewView's default selfie mirror already reflects this (scaleX = 1f).
    • Formula:
      val isFront = streamingService?.isFrontCamera() == true
      val shouldInvertView = if (isFront) !isMirrored else isMirrored
      binding.livePreview.scaleX = if (shouldInvertView) -1f else 1f
      binding.mirrorButton.isChecked = isMirrored
  • Per-Lens Preference Persistence:
    Mirrored state is stored in SharedPreferences using "camera_mirror_" + facing (camera_mirror_back / camera_mirror_front). Switching lenses automatically restores that lens's mirror preference and updates the UI and stream.
  • UI Layout Integrity:
    flipCameraButton and mirrorButton are wrapped in a horizontal LinearLayout with layout_width="0dp" and layout_weight="1" across portrait and landscape layouts, preserving layout rhythm.
  • Vector Asset & Localization:
    Added ic_mirror.xml (24x24dp vector styled consistently with ic_flip.xml) and string resources btn_mirror, btn_mirror_on, btn_mirror_off.

Verification & Benchmark

  • Comprehensive unit tests added to FrameConverterTest.kt:
    • Non-square YUV frames (8x4, 4x2) left/right column swap verification.
    • Double-flip round-trip assertion (assertArrayEquals against original arrays).
    • Timestamp and object identity preservation (assertSame).
    • Combination of 90° rotation followed by flipHorizontallyInPlace.
  • Automated Validation:
    • JAVA_HOME=/usr/lib/jvm/java-17-openjdk ./gradlew test: 18/18 test classes passing (154 tests total, 0 failures).
    • JAVA_HOME=/usr/lib/jvm/java-17-openjdk ./gradlew assembleDebug: SUCCESS (zero lint or resource errors).
    • JAVA_HOME=/usr/lib/jvm/java-17-openjdk ./gradlew check: SUCCESS (lint and checks passing cleanly).

Use PreviewView COMPATIBLE so viewfinder scaleX matches the RTP flip,
show Mirror On/Off on the button, and stack Flip/Mirror on the land rail.
Tighten rotate+flip chroma tests and extract CameraMirror for JVM coverage.
@kvm404
kvm404 merged commit 9f7b5c2 into main Sep 4, 2026
1 check passed
@kvm404 kvm404 mentioned this pull request Sep 4, 2026
1 of 3 tasks
@kvm404
kvm404 deleted the feat/video-mirror-toggle branch September 4, 2026 06:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant