camera: allow user to choose codecs #322
- Commit
7213de0a0325d9a56ca8277cd75e73680a3f760b- Author
- Vayun Mathur <[email protected]>
- When
- 2026-07-22 06:52 UTC (4 months ago)
- Parents
6c9faec
Diff
camera/src/main/java/com/vayunmathur/camera/ui/SettingsPage.kt
diff --git a/camera/src/main/java/com/vayunmathur/camera/ui/SettingsPage.kt b/camera/src/main/java/com/vayunmathur/camera/ui/SettingsPage.ktindex dd9d61c8a8..28c2fe9585 100644--- a/camera/src/main/java/com/vayunmathur/camera/ui/SettingsPage.kt+++ b/camera/src/main/java/com/vayunmathur/camera/ui/SettingsPage.kt@@ -22,6 +22,9 @@ import com.vayunmathur.library.ui.TopAppBar import androidx.compose.runtime.Composable import androidx.compose.runtime.collectAsState import androidx.compose.runtime.getValue+import androidx.compose.runtime.mutableStateOf+import androidx.compose.runtime.remember+import androidx.compose.runtime.setValue import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.platform.LocalContext@@ -30,6 +33,13 @@ import androidx.compose.ui.unit.dp import androidx.core.content.ContextCompat import com.vayunmathur.camera.R import com.vayunmathur.camera.util.CameraViewModel+import com.vayunmathur.camera.util.CodecSupport+import com.vayunmathur.camera.util.VideoCodec+import com.vayunmathur.library.ui.DropdownMenu+import com.vayunmathur.library.ui.DropdownMenuItem+import com.vayunmathur.library.ui.ExposedDropdownMenuBox+import com.vayunmathur.library.ui.ExposedDropdownMenuDefaults+import com.vayunmathur.library.ui.OutlinedTextField import com.vayunmathur.library.ui.IconNavigation import com.vayunmathur.library.util.NavBackStack import com.vayunmathur.library.util.NavKey@@ -38,6 +48,7 @@ import com.vayunmathur.library.util.NavKey @Composable fun <T : NavKey> SettingsPage(backStack: NavBackStack<T>, viewModel: CameraViewModel) { val locationEnabled by viewModel.locationEnabled.collectAsState()+ val videoCodec by viewModel.videoCodec.collectAsState() val context = LocalContext.current val locationPermissionLauncher = rememberLauncherForActivityResult(@@ -62,6 +73,58 @@ fun <T : NavKey> SettingsPage(backStack: NavBackStack<T>, viewModel: CameraViewM .verticalScroll(rememberScrollState()) .padding(16.dp) ) {+ val availableCodecs = remember {+ buildList {+ add(VideoCodec.AVC)+ if (CodecSupport.isHevcEncoderAvailable) add(VideoCodec.HEVC)+ if (CodecSupport.isHardwareAv1EncoderAvailable) add(VideoCodec.AV1)+ }+ }+ if (availableCodecs.size > 1) {+ SettingsSection(stringResource(R.string.settings_video_codec)) {+ var expanded by remember { mutableStateOf(false) }+ ExposedDropdownMenuBox(+ expanded = expanded,+ onExpandedChange = { expanded = it }+ ) {+ OutlinedTextField(+ value = "${videoCodec.label} — ${videoCodec.description}",+ onValueChange = {},+ readOnly = true,+ trailingIcon = { ExposedDropdownMenuDefaults.TrailingIcon(expanded = expanded) },+ modifier = Modifier+ .fillMaxWidth()+ .padding(horizontal = 8.dp)+ .menuAnchor(),+ label = { Text(stringResource(R.string.settings_video_codec_label)) }+ )+ ExposedDropdownMenu(+ expanded = expanded,+ onDismissRequest = { expanded = false }+ ) {+ availableCodecs.forEach { codec ->+ DropdownMenuItem(+ text = {+ Column {+ Text(codec.label)+ Text(+ codec.description,+ style = MaterialTheme.typography.bodySmall,+ color = MaterialTheme.colorScheme.onSurfaceVariant+ )+ }+ },+ onClick = {+ viewModel.setVideoCodec(codec)+ expanded = false+ }+ )+ }+ }+ }+ }+ }+ SettingsSection(stringResource(R.string.settings_location)) { Row( modifier = Modifier.fillMaxWidth().padding(horizontal = 8.dp),
camera/src/main/java/com/vayunmathur/camera/util/CameraViewModel.kt
diff --git a/camera/src/main/java/com/vayunmathur/camera/util/CameraViewModel.kt b/camera/src/main/java/com/vayunmathur/camera/util/CameraViewModel.ktindex b2652bee51..12858d79b6 100644--- a/camera/src/main/java/com/vayunmathur/camera/util/CameraViewModel.kt+++ b/camera/src/main/java/com/vayunmathur/camera/util/CameraViewModel.kt@@ -69,6 +69,11 @@ enum class CameraMode { PHOTO, PORTRAIT, PANORAMA, PHOTOSPHERE, VIDEO, SLOW_MO, enum class FlashMode { ON, OFF, AUTO } enum class TimerDuration(val seconds: Int) { NONE(0), THREE(3), FIVE(5), TEN(10) } enum class AspectRatioOption(val label: String) { RATIO_16_9("16:9"), RATIO_4_3("4:3"), RATIO_1_1("1:1") }+enum class VideoCodec(val label: String, val description: String) {+ AVC("H.264 / AVC", "Most compatible"),+ HEVC("H.265 / HEVC", "More efficient"),+ AV1("AV1", "Most efficient"),+} /** Formats a zoom ratio for the zoom bar: ".5", "1x", "2x", or "1.5x". */ fun formatZoomLabel(ratio: Float): String = when {@@ -97,14 +102,6 @@ fun buildColorAdjustmentMatrix(warmth: Float, shadows: Float): ColorMatrix = Col class CameraViewModel(private val app: Application) : AndroidViewModel(app) { companion object {- /**- * AV1/Opus recording is disabled: CameraX's Recorder muxes an incomplete `av1C`- * (empty configOBUs — no sequence-header OBU) and Android raw Opus CSD, producing- * files the system thumbnailer and ExoPlayer can't decode (0x0 playback, no- * thumbnail) even on hardware that supports AV1. Falls back to the default- * H.264 + AAC. Flip to true to re-enable once the muxing writes a complete av1C.- */- private const val ENABLE_AV1_OPUS_RECORDING = false // Night-mode auto-detection tuning. Engage once average Y stays below ENGAGE for a few // frames; disengage once it climbs above DISENGAGE for a few frames. The gap between the@@ -205,6 +202,9 @@ class CameraViewModel(private val app: Application) : AndroidViewModel(app) { private val _aspectRatio = MutableStateFlow(AspectRatioOption.RATIO_4_3) val aspectRatio = _aspectRatio.asStateFlow() + private val _videoCodec = MutableStateFlow(VideoCodec.AVC)+ val videoCodec = _videoCodec.asStateFlow()+ private val _isRecording = MutableStateFlow(false) val isRecording = _isRecording.asStateFlow() @@ -423,6 +423,10 @@ class CameraViewModel(private val app: Application) : AndroidViewModel(app) { var recordingWithAv1: Boolean = false private set + /** True when the active video session is encoding HEVC/H.265. */+ var recordingWithHevc: Boolean = false+ private set+ fun setSloMoFps(fps: Int) { sloMoFps = fps }@@ -450,6 +454,9 @@ class CameraViewModel(private val app: Application) : AndroidViewModel(app) { ds.getString("camera_flash")?.let { _flashMode.value = FlashMode.valueOf(it) } ds.getString("camera_timer")?.let { _timerDuration.value = TimerDuration.valueOf(it) } ds.getString("camera_aspect_ratio")?.let { _aspectRatio.value = AspectRatioOption.valueOf(it) }+ ds.getString("camera_video_codec")?.let {+ _videoCodec.value = try { VideoCodec.valueOf(it) } catch (_: Exception) { VideoCodec.AVC }+ } ds.getString("camera_location")?.let { _locationEnabled.value = it.toBoolean() } ds.getString("camera_last_capture")?.let { _lastCaptureUri.value = Uri.parse(it) } ds.getString("camera_grid")?.let { _gridEnabled.value = it.toBoolean() }@@ -493,6 +500,11 @@ class CameraViewModel(private val app: Application) : AndroidViewModel(app) { viewModelScope.launch { ds.setString("camera_location", enabled.toString()) } } + fun setVideoCodec(codec: VideoCodec) {+ _videoCodec.value = codec+ viewModelScope.launch { ds.setString("camera_video_codec", codec.name) }+ }+ fun toggleGrid() { _gridEnabled.value = !_gridEnabled.value viewModelScope.launch { ds.setString("camera_grid", _gridEnabled.value.toString()) }@@ -1006,14 +1018,20 @@ class CameraViewModel(private val app: Application) : AndroidViewModel(app) { .build() val cameraInfo = provider.getCameraInfo(selector) - val useAv1 = ENABLE_AV1_OPUS_RECORDING &&+ val selectedCodec = _videoCodec.value+ // Respect user setting with fallback: AV1 > HEVC > AVC priority for availability check.+ // If selected codec isn't available, fall back to next best available.+ val useAv1 = selectedCodec == VideoCodec.AV1 && CodecSupport.isHardwareAv1EncoderAvailable && av1SupportedByCamera(cameraInfo)- val useOpus = ENABLE_AV1_OPUS_RECORDING && CodecSupport.isOpusEncoderAvailable+ val useHevc = !useAv1 && selectedCodec != VideoCodec.AVC &&+ CodecSupport.isHevcEncoderAvailable && hevcSupportedByCamera(cameraInfo)+ // All codecs use AAC audio. Opus disabled to avoid CameraX raw-Opus-CSD muxing bug.+ val useOpus = false // HLG10 (10-bit HDR) when the camera's video pipeline supports it. Uses the default // HEVC/H.264 codec (AV1 stays off). Preview and VideoCapture must share the dynamic // range or the bind fails, so both are gated together. val hlgSupported = hlgSupportedByCamera(cameraInfo)- Log.d("VideoSession", "Codec selection: av1=$useAv1, opus=$useOpus, hlg10=$hlgSupported")+ Log.d("VideoSession", "Codec selection: av1=$useAv1, hevc=$useHevc, opus=$useOpus, hlg10=$hlgSupported") // Cinematic mode enables video stabilization; all video modes always record at max // quality/fps (no UI picker).@@ -1032,7 +1050,7 @@ class CameraViewModel(private val app: Application) : AndroidViewModel(app) { owner.start() sessionLifecycleOwner = owner - fun bind(av1: Boolean, opus: Boolean, hlg: Boolean, snapshot: Boolean): Camera {+ fun bind(av1: Boolean, hevc: Boolean, opus: Boolean, hlg: Boolean, snapshot: Boolean): Camera { val dynamicRange = if (hlg) androidx.camera.core.DynamicRange.HLG_10_BIT else androidx.camera.core.DynamicRange.SDR val previewBuilder = Preview.Builder()@@ -1043,6 +1061,7 @@ class CameraViewModel(private val app: Application) : AndroidViewModel(app) { val recorderBuilder = Recorder.Builder() .setQualitySelector(qualitySelector) if (av1) recorderBuilder.setVideoMimeType(MediaFormat.MIMETYPE_VIDEO_AV1)+ else if (hevc) recorderBuilder.setVideoMimeType(MediaFormat.MIMETYPE_VIDEO_HEVC) if (opus) recorderBuilder.setAudioMimeType(MediaFormat.MIMETYPE_AUDIO_OPUS) val captureBuilder = VideoCapture.Builder(recorderBuilder.build()) .setMirrorMode(MirrorMode.MIRROR_MODE_ON_FRONT_ONLY)@@ -1052,6 +1071,7 @@ class CameraViewModel(private val app: Application) : AndroidViewModel(app) { capture.targetRotation = targetRotation videoCapture = capture recordingWithAv1 = av1+ recordingWithHevc = hevc return if (snapshot) { // Extra ImageCapture use case enables taking a still while recording (SDR JPEG). val still = ImageCapture.Builder()@@ -1080,7 +1100,7 @@ class CameraViewModel(private val app: Application) : AndroidViewModel(app) { for ((hlg, snapshot) in attempts) { try { provider.unbindAll()- bound = bind(useAv1, useOpus, hlg, snapshot)+ bound = bind(useAv1, useHevc, useOpus, hlg, snapshot) break } catch (e: Exception) { lastError = e@@ -1110,6 +1130,14 @@ class CameraViewModel(private val app: Application) : AndroidViewModel(app) { false } + private fun hevcSupportedByCamera(cameraInfo: androidx.camera.core.CameraInfo): Boolean = try {+ val caps = Recorder.getVideoCapabilities(cameraInfo, MediaFormat.MIMETYPE_VIDEO_HEVC)+ caps?.getSupportedQualities(androidx.camera.core.DynamicRange.SDR)?.isNotEmpty() == true+ } catch (e: Exception) {+ Log.w("VideoSession", "Could not query HEVC video capabilities", e)+ false+ }+ private fun hlgSupportedByCamera(cameraInfo: androidx.camera.core.CameraInfo): Boolean = try { Recorder.getVideoCapabilities(cameraInfo) .supportedDynamicRanges
camera/src/main/java/com/vayunmathur/camera/util/CodecSupport.kt
diff --git a/camera/src/main/java/com/vayunmathur/camera/util/CodecSupport.kt b/camera/src/main/java/com/vayunmathur/camera/util/CodecSupport.ktindex d7d21c025a..cfdeaee93f 100644--- a/camera/src/main/java/com/vayunmathur/camera/util/CodecSupport.kt+++ b/camera/src/main/java/com/vayunmathur/camera/util/CodecSupport.kt@@ -21,6 +21,11 @@ object CodecSupport { hasEncoder(MediaFormat.MIMETYPE_AUDIO_OPUS, requireHardware = false) } + /** An HEVC/H.265 (`video/hevc`) encoder exists on this device. */+ val isHevcEncoderAvailable: Boolean by lazy {+ hasEncoder(MediaFormat.MIMETYPE_VIDEO_HEVC, requireHardware = false)+ }+ private fun hasEncoder(mimeType: String, requireHardware: Boolean): Boolean { return try { MediaCodecList(MediaCodecList.REGULAR_CODECS).codecInfos.any { info ->
camera/src/main/res/values/strings.xml
diff --git a/camera/src/main/res/values/strings.xml b/camera/src/main/res/values/strings.xmlindex 1dee7d4010..5b7abbf1bc 100644--- a/camera/src/main/res/values/strings.xml+++ b/camera/src/main/res/values/strings.xml@@ -33,6 +33,9 @@ <string name="settings_location">Location</string> <string name="settings_location_description">Save location in photo metadata</string> + <string name="settings_video_codec">Video codec</string>+ <string name="settings_video_codec_label">Video codec</string>+ <string name="mode_portrait">Portrait</string> <string name="qr_result">QR Code</string>