From 17ae5fae764839ac4ed3c47db73190c8a968f35a Mon Sep 17 00:00:00 2001 From: Marc Rousavy Date: Mon, 15 Jan 2024 10:30:13 +0100 Subject: [PATCH] fix: Fix `CamcorderProfile` out of range error (#2389) * fix: Fix `CamcorderProfile` out of range error * fix: Also take `cameraId` into account --- .../RecordingSession+getRecommendedBitRate.kt | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/package/android/src/main/java/com/mrousavy/camera/extensions/RecordingSession+getRecommendedBitRate.kt b/package/android/src/main/java/com/mrousavy/camera/extensions/RecordingSession+getRecommendedBitRate.kt index 9a4984b..ed6cee2 100644 --- a/package/android/src/main/java/com/mrousavy/camera/extensions/RecordingSession+getRecommendedBitRate.kt +++ b/package/android/src/main/java/com/mrousavy/camera/extensions/RecordingSession+getRecommendedBitRate.kt @@ -23,7 +23,7 @@ fun RecordingSession.getRecommendedBitRate(fps: Int, codec: VideoCodec, hdr: Boo val targetResolution = size val encoder = codec.toVideoEncoder() val bitDepth = if (hdr) 10 else 8 - val quality = findClosestCamcorderProfileQuality(targetResolution) + val quality = findClosestCamcorderProfileQuality(cameraId, targetResolution) Log.i("CamcorderProfile", "Closest matching CamcorderProfile: $quality") var recommendedProfile: RecommendedProfile? = null @@ -93,10 +93,19 @@ private fun getResolutionForCamcorderProfileQuality(camcorderProfile: Int): Int else -> throw Error("Invalid CamcorderProfile \"$camcorderProfile\"!") } -private fun findClosestCamcorderProfileQuality(resolution: Size): Int { +private fun findClosestCamcorderProfileQuality(cameraId: String, resolution: Size): Int { // Iterate through all available CamcorderProfiles and find the one that matches the closest val targetResolution = resolution.width * resolution.height - val closestProfile = (CamcorderProfile.QUALITY_QCIF..CamcorderProfile.QUALITY_8KUHD).minBy { profile -> + val cameraIdInt = cameraId.toIntOrNull() + + val profiles = (CamcorderProfile.QUALITY_QCIF..CamcorderProfile.QUALITY_8KUHD).filter { profile -> + if (cameraIdInt != null) { + return@filter CamcorderProfile.hasProfile(cameraIdInt, profile) + } else { + return@filter CamcorderProfile.hasProfile(profile) + } + } + val closestProfile = profiles.minBy { profile -> val currentResolution = getResolutionForCamcorderProfileQuality(profile) return@minBy abs(currentResolution - targetResolution) }