fix: Fix CamcorderProfile out of range error (#2389)

* fix: Fix `CamcorderProfile` out of range error

* fix: Also take `cameraId` into account
This commit is contained in:
Marc Rousavy 2024-01-15 10:30:13 +01:00 committed by GitHub
parent ec2f8c258d
commit 17ae5fae76
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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)
}