Skip to content

Your Android phone and your Windows PC, working as one. Local-first, end-to-end encrypted, no account.

Webcam: offer 4K only when the camera can output it

Bee6651b155ffcc
diff --git a/CHANGELOG.md b/CHANGELOG.md
index aee13b5..d4c1599 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -56,6 +56,8 @@ change things that need a step from you, and its notes say so.
follows the resolution actually streaming, whichever side changed it (or
1080p when the phone can't do 4K); upright, the phone's buttons showed none
selected.
+- Phone as webcam offers 4K only when the camera in use can output it, not
+ just the encoder: switching to a front camera without 4K drops to 1080p.
- **Phone sound never falls back to the microphone.** On a phone that blocks
capturing what it plays, Elevated recorded the microphone instead and sent
it to the PC as the phone's sound. It now sends no sound.
diff --git a/android/app/src/main/kotlin/app/nectarlink/android/webcam/CameraEncoder.kt b/android/app/src/main/kotlin/app/nectarlink/android/webcam/CameraEncoder.kt
index e171388..f2b7410 100644
--- a/android/app/src/main/kotlin/app/nectarlink/android/webcam/CameraEncoder.kt
+++ b/android/app/src/main/kotlin/app/nectarlink/android/webcam/CameraEncoder.kt
@@ -86,12 +86,14 @@ internal class CameraEncoder(
}
private val displayManager = this.context.getSystemService(DisplayManager::class.java)
- val supports4k: Boolean = supports4kEncoder()
-
@Volatile private var running = true
@Volatile private var keyframeWanted = false
- @Volatile private var targetHeight: Int = normalizeHeight(initialHeight, supports4k)
@Volatile private var cameraFacing: String = if (initialCamera == "front") "front" else "back"
+
+ /** Whether the camera in use and the encoder can both do 4K. */
+ @Volatile var supports4k: Boolean = supports4k(this.context, cameraFacing)
+ private set
+ @Volatile private var targetHeight: Int = normalizeHeight(initialHeight, supports4k)
@Volatile private var activeCodec: ActiveCodec? = null
@Volatile private var currentSurfaceRotation: Int = readDisplayRotation()
@Volatile private var currentSensorRotation: Int = 0
@@ -204,13 +206,17 @@ internal class CameraEncoder(
main.post {
if (!running || cameraFacing == normalized) return@post
cameraFacing = normalized
+ // The other camera may not do 4K (front cameras often don't).
+ supports4k = supports4k(context, normalized)
+ targetHeight = normalizeHeight(targetHeight, supports4k)
bindCamera()
}
}
fun setResolutionHeight(height: Int) {
- val normalized = normalizeHeight(height, supports4k)
main.post {
+ // After any camera switch queued before it, which can change what 4K means.
+ val normalized = normalizeHeight(height, supports4k)
if (!running || targetHeight == normalized) return@post
targetHeight = normalized
bindCamera()
@@ -376,6 +382,7 @@ internal class CameraEncoder(
maxZoomRatio = maxZ,
hasFlash = hasFlash,
torchOn = false,
+ supports4k = supports4k,
)
}
}
@@ -622,6 +629,32 @@ internal class CameraEncoder(
/** Checks whether any hardware AVC encoder on this device supports 3840×2160. */
fun supports4kEncoder(): Boolean = cachedSupports4k
+ private val uhdCache = java.util.concurrent.ConcurrentHashMap<String, Boolean>()
+
+ /**
+ * Whether [camera] (`front` or `back`) can stream 4K: a camera facing
+ * that way outputs 3840 × 2160, and the encoder takes it.
+ */
+ fun supports4k(context: Context, camera: String): Boolean =
+ supports4kEncoder() && uhdCache.getOrPut(camera) {
+ runCatching {
+ val cm = context.getSystemService(CameraManager::class.java) ?: return@runCatching false
+ val wantedFacing = if (camera == "front") {
+ CameraCharacteristics.LENS_FACING_FRONT
+ } else {
+ CameraCharacteristics.LENS_FACING_BACK
+ }
+ cm.cameraIdList.any { id ->
+ val chars = cm.getCameraCharacteristics(id)
+ val sizes = chars.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP)
+ ?.getOutputSizes(android.graphics.SurfaceTexture::class.java)
+ .orEmpty()
+ chars.get(CameraCharacteristics.LENS_FACING) == wantedFacing &&
+ sizes.any { it.width >= 3840 && it.height >= 2160 }
+ }
+ }.getOrDefault(false)
+ }
+
/**
* Guarantees each H.264 access unit starts with an Annex B Access
* Unit Delimiter (`00 00 00 01 09 F0`) and that keyframes include
diff --git a/android/app/src/main/kotlin/app/nectarlink/android/webcam/WebcamService.kt b/android/app/src/main/kotlin/app/nectarlink/android/webcam/WebcamService.kt
index 9ed9e11..f9003c2 100644
--- a/android/app/src/main/kotlin/app/nectarlink/android/webcam/WebcamService.kt
+++ b/android/app/src/main/kotlin/app/nectarlink/android/webcam/WebcamService.kt
@@ -108,10 +108,10 @@ class WebcamService : LifecycleService() {
current = this
WebcamRequests.dismiss(this, targetPc)
- val supports4k = CameraEncoder.supports4kEncoder()
+ val normCamera = if (camera == "front") "front" else "back"
+ val supports4k = CameraEncoder.supports4k(this, normCamera)
val normHeight = CameraEncoder.normalizeHeight(height, supports4k)
val normWidth = CameraEncoder.widthForHeight(normHeight)
- val normCamera = if (camera == "front") "front" else "back"
val hasFlash = CameraEncoder.cameraHasFlash(this, normCamera)
val maxZoom = CameraEncoder.cameraMaxZoom(this, normCamera)
val initialZoom = _session.value.zoomRatio.coerceIn(1f, maxZoom)