Skip to content

fix(android): synchronize viewfinder inversion with mirror stream state - #65

Merged
kvm404 merged 1 commit into
mainfrom
fix/mirror-preview-sync
Sep 8, 2026
Merged

kvm404 merged 1 commit into
mainfrom
fix/mirror-preview-sync

Conversation

@kvm404

@kvm404 kvm404 commented Sep 8, 2026

Copy link
Copy Markdown
Owner

Summary

Resolves an inversion defect where the phone's live viewfinder and RTP/H.264 stream mirror states fell out of sync when using the front-facing camera.

Problem Description

CameraX PreviewView naturally renders front-facing camera feeds mirrored (selfie mode) and rear camera feeds unmirrored.

Previously, MainActivity.updateMirrorUi evaluated:

val shouldInvertView = if (isFront) !isMirrored else isMirrored
binding.livePreview.scaleX = if (shouldInvertView) -1f else 1f

This inverted logic caused:

  1. Mirror OFF (isMirrored = false): scaleX = -1f (front viewfinder was inverted, desynced from natural preview and stream).
  2. Mirror ON (isMirrored = true): scaleX = 1f (front viewfinder was un-inverted, opposite of stream flip).

Solution

  1. Centralized Viewfinder Scaling: Implemented CameraMirror.viewfinderScaleX(isMirrored: Boolean): Float:
    • isMirrored = false -> scaleX = 1f (natural CameraX orientation for both front and back lenses)
    • isMirrored = true -> scaleX = -1f (horizontal flip matching stream mirror transform)
  2. Simplified UI Binding: Updated MainActivity.updateMirrorUi to bind directly to CameraMirror.viewfinderScaleX(isMirrored).
  3. Unit Tests: Added full test coverage in CameraMirrorTest.kt verifying static scale logic and instance state transitions.

Verification

  • Android Unit Tests: JAVA_HOME=/usr/lib/jvm/java-17-openjdk ./gradlew test --rerun-tasks PASSED (51/51 tasks executed).
  • Linux CLI Tests: go test ./... PASSED.
  • Git Hygiene: Clean commit on fix/mirror-preview-sync.

Previously, MainActivity.updateMirrorUi inverted the viewfinder preview logic for front-facing cameras (shouldInvertView = if (isFront) !isMirrored else isMirrored). This caused an inversion defect where the front camera preview displayed inverted (-1f) when mirror mode was OFF and un-inverted (1f) when mirror mode was ON, creating a desync between the live viewfinder and stream mirror state.

CameraX PreviewView naturally renders the front camera in selfie orientation and the back camera unmirrored. Applying viewfinderScaleX directly ensures:
- Mirror OFF (isMirrored = false): scaleX = 1f (CameraX natural orientation)
- Mirror ON (isMirrored = true): scaleX = -1f (inverted preview matching stream flip)

Consolidate the viewfinder scale calculation in CameraMirror.viewfinderScaleX() and add unit test coverage in CameraMirrorTest.
@kvm404
kvm404 merged commit e60c414 into main Sep 8, 2026
1 check passed
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