feat: video record preview size
This commit is contained in:
1 parent
c504c2b6b9
commit
2b0599ecd5
3 files changed
+73
-30
No files matched your search
@@ -5428,7 +5428,7 @@ class Camera2Controller(private val context: Context) {
|
||||
val outputSize = _state.value.videoConfig.resolveOutputSize(
|
||||
_state.value.videoCapabilities.openGatePortraitAspectRatio
|
||||
)
|
||||
val cameraInputSize = _state.value.videoCapabilities.previewSizesByResolution[
|
||||
val cameraInputSize = _state.value.videoCapabilities.cameraInputSizesByResolution[
|
||||
_state.value.videoConfig.resolution
|
||||
] ?: _state.value.currentPreviewSize
|
||||
val isFrontCamera = isCurrentCameraFrontFacing()
|
||||
|
||||
@@ -13,6 +13,8 @@ import kotlin.math.abs
|
||||
object VideoCapabilitiesResolver {
|
||||
|
||||
private const val TAG = "VideoCapabilitiesResolver"
|
||||
private const val VIDEO_PREVIEW_SHORT_EDGE = 1080
|
||||
private const val VIDEO_PREVIEW_ASPECT_TOLERANCE = 0.01f
|
||||
|
||||
fun resolve(
|
||||
characteristics: CameraCharacteristics,
|
||||
@@ -30,7 +32,8 @@ object VideoCapabilitiesResolver {
|
||||
val openGateAspect = resolveOpenGatePortraitAspectRatio(activeArray, sensorOrientation)
|
||||
|
||||
val availableResolutions = VideoResolutionPreset.entries.filter { preset ->
|
||||
findBestOutputSize(recordingOutputSizes, preset, requestedConfig.aspectRatio, openGateAspect) != null
|
||||
findBestOutputSize(recordingOutputSizes, preset, requestedConfig.aspectRatio, openGateAspect) != null &&
|
||||
findBestOutputSize(previewOutputSizes, preset, requestedConfig.aspectRatio, openGateAspect) != null
|
||||
}
|
||||
|
||||
val resolvedResolution = requestedConfig.resolution.takeIf { availableResolutions.contains(it) }
|
||||
@@ -41,37 +44,42 @@ object VideoCapabilitiesResolver {
|
||||
findBestOutputSize(recordingOutputSizes, preset, requestedConfig.aspectRatio, openGateAspect)
|
||||
?.let { preset to it }
|
||||
}.toMap()
|
||||
val previewSizesByResolution = availableResolutions.associateWith { preset ->
|
||||
val cameraInputSizesByResolution = availableResolutions.mapNotNull { preset ->
|
||||
findBestOutputSize(previewOutputSizes, preset, requestedConfig.aspectRatio, openGateAspect)
|
||||
?: requestedConfig.resolution.resolveOutputSize(
|
||||
requestedConfig.aspectRatio.getPortraitAspectRatio(openGateAspect)
|
||||
)
|
||||
}
|
||||
?.let { preset to it }
|
||||
}.toMap()
|
||||
|
||||
val recordingSize = recordingSizesByResolution[resolvedResolution]
|
||||
?: resolvedResolution.resolveOutputSize(
|
||||
requestedConfig.aspectRatio.getPortraitAspectRatio(openGateAspect)
|
||||
)
|
||||
|
||||
val previewSize = findFixedPreviewOutputSize(
|
||||
outputSizes = previewOutputSizes,
|
||||
aspectRatio = requestedConfig.aspectRatio,
|
||||
openGatePortraitAspectRatio = openGateAspect
|
||||
) ?: VideoResolutionPreset.FHD_1080P.resolveOutputSize(
|
||||
requestedConfig.aspectRatio.getPortraitAspectRatio(openGateAspect)
|
||||
)
|
||||
|
||||
val previewSize = previewSizesByResolution[resolvedResolution]
|
||||
?: resolvedResolution.resolveOutputSize(requestedConfig.aspectRatio.getPortraitAspectRatio(openGateAspect))
|
||||
val cameraInputSize = cameraInputSizesByResolution[resolvedResolution] ?: previewSize
|
||||
|
||||
val availableFps = resolveAvailableFps(
|
||||
characteristics = characteristics,
|
||||
recordingSize = recordingSize,
|
||||
cameraInputSize = cameraInputSize,
|
||||
previewSize = previewSize
|
||||
)
|
||||
val resolvedFps = requestedConfig.fps.takeIf { availableFps.contains(it) }
|
||||
?: availableFps.firstOrNull()
|
||||
?: VideoFpsPreset.FPS_30
|
||||
|
||||
/*PLog.d(
|
||||
PLog.d(
|
||||
TAG,
|
||||
"Resolved video capabilities: resolution=${resolvedResolution.displayName}, " +
|
||||
"recording=${recordingSize.width}x${recordingSize.height}, " +
|
||||
"cameraInput=${cameraInputSize.width}x${cameraInputSize.height}, " +
|
||||
"preview=${previewSize.width}x${previewSize.height}, " +
|
||||
"fps=${availableFps.map { it.fps }}"
|
||||
)*/
|
||||
)
|
||||
|
||||
val linearTonemapSupported = availableTonemapModes.contains(CaptureRequest.TONEMAP_MODE_CONTRAST_CURVE) ||
|
||||
availableTonemapModes.contains(CaptureRequest.TONEMAP_MODE_GAMMA_VALUE)
|
||||
@@ -114,7 +122,7 @@ object VideoCapabilitiesResolver {
|
||||
availableAspectRatios = VideoAspectRatio.entries.toList(),
|
||||
availableLogProfiles = availableLogProfiles,
|
||||
availableBitrates = VideoBitratePreset.entries.toList(),
|
||||
previewSizesByResolution = previewSizesByResolution,
|
||||
cameraInputSizesByResolution = cameraInputSizesByResolution,
|
||||
recordingSizesByResolution = recordingSizesByResolution,
|
||||
openGatePortraitAspectRatio = openGateAspect,
|
||||
availableStabilizationModes = availableStabilizationModes,
|
||||
@@ -157,14 +165,48 @@ object VideoCapabilitiesResolver {
|
||||
.firstOrNull()
|
||||
}
|
||||
|
||||
private fun findFixedPreviewOutputSize(
|
||||
outputSizes: List<Size>,
|
||||
aspectRatio: VideoAspectRatio,
|
||||
openGatePortraitAspectRatio: Float
|
||||
): Size? {
|
||||
val validSizes = outputSizes
|
||||
.filter { it.width > 0 && it.height > 0 }
|
||||
.distinctBy { it.width to it.height }
|
||||
if (validSizes.isEmpty()) return null
|
||||
|
||||
val targetAspect = aspectRatio.getPortraitAspectRatio(openGatePortraitAspectRatio)
|
||||
val aspectMatchedSizes = validSizes.filter {
|
||||
abs(getPortraitAspectRatio(it) - targetAspect) <= VIDEO_PREVIEW_ASPECT_TOLERANCE
|
||||
}
|
||||
val candidates = aspectMatchedSizes.ifEmpty { validSizes }
|
||||
val sizesWithinLimit = candidates.filter {
|
||||
minOf(it.width, it.height) <= VIDEO_PREVIEW_SHORT_EDGE
|
||||
}
|
||||
|
||||
return if (sizesWithinLimit.isNotEmpty()) {
|
||||
sizesWithinLimit.sortedWith(
|
||||
compareByDescending<Size> { minOf(it.width, it.height) }
|
||||
.thenBy { abs(getPortraitAspectRatio(it) - targetAspect) }
|
||||
.thenByDescending { it.width.toLong() * it.height.toLong() }
|
||||
).first()
|
||||
} else {
|
||||
candidates.sortedWith(
|
||||
compareBy<Size> { minOf(it.width, it.height) }
|
||||
.thenBy { abs(getPortraitAspectRatio(it) - targetAspect) }
|
||||
.thenBy { it.width.toLong() * it.height.toLong() }
|
||||
).first()
|
||||
}
|
||||
}
|
||||
|
||||
private fun resolveAvailableFps(
|
||||
characteristics: CameraCharacteristics,
|
||||
recordingSize: Size,
|
||||
cameraInputSize: Size,
|
||||
previewSize: Size
|
||||
): List<VideoFpsPreset> {
|
||||
val outputRanges = characteristics.get(CameraCharacteristics.CONTROL_AE_AVAILABLE_TARGET_FPS_RANGES)
|
||||
?: emptyArray()
|
||||
val minFrameDurationNs = resolveMinFrameDurationNs(characteristics, recordingSize, previewSize)
|
||||
val minFrameDurationNs = resolveMinFrameDurationNs(characteristics, cameraInputSize, previewSize)
|
||||
val maxFpsByDuration = if (minFrameDurationNs > 0) {
|
||||
(1_000_000_000.0 / minFrameDurationNs.toDouble()).toInt()
|
||||
} else {
|
||||
@@ -188,7 +230,7 @@ object VideoCapabilitiesResolver {
|
||||
/*PLog.w(
|
||||
TAG,
|
||||
"Camera reports max $advertisedMaxFps fps only, keeping optimistic presets=${optimisticFps.map { it.fps }} " +
|
||||
"for recording=${recordingSize.width}x${recordingSize.height}"
|
||||
"for cameraInput=${cameraInputSize.width}x${cameraInputSize.height}"
|
||||
)*/
|
||||
return (strictAvailableFps + optimisticFps).distinctBy { it.fps }
|
||||
}
|
||||
@@ -217,24 +259,25 @@ object VideoCapabilitiesResolver {
|
||||
|
||||
private fun resolveMinFrameDurationNs(
|
||||
characteristics: CameraCharacteristics,
|
||||
recordingSize: Size,
|
||||
cameraInputSize: Size,
|
||||
previewSize: Size
|
||||
): Long {
|
||||
val streamConfigMap = characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP)
|
||||
if (streamConfigMap == null) return 0L
|
||||
|
||||
return queryMinFrameDurationNs(
|
||||
val cameraInputDurationNs = queryMinFrameDurationNs(
|
||||
streamConfigMap = streamConfigMap,
|
||||
outputClass = MediaRecorder::class.java,
|
||||
outputSize = recordingSize,
|
||||
label = "MediaRecorder"
|
||||
).takeIf { it > 0L }
|
||||
?: queryMinFrameDurationNs(
|
||||
streamConfigMap = streamConfigMap,
|
||||
outputClass = SurfaceTexture::class.java,
|
||||
outputSize = previewSize,
|
||||
label = "SurfaceTexture"
|
||||
)
|
||||
outputClass = SurfaceTexture::class.java,
|
||||
outputSize = cameraInputSize,
|
||||
label = "recording SurfaceTexture"
|
||||
)
|
||||
val previewDurationNs = queryMinFrameDurationNs(
|
||||
streamConfigMap = streamConfigMap,
|
||||
outputClass = SurfaceTexture::class.java,
|
||||
outputSize = previewSize,
|
||||
label = "preview SurfaceTexture"
|
||||
)
|
||||
return maxOf(cameraInputDurationNs, previewDurationNs)
|
||||
}
|
||||
|
||||
private fun <T> queryMinFrameDurationNs(
|
||||
|
||||
@@ -140,7 +140,7 @@ data class VideoCapabilities(
|
||||
val availableLogProfiles: List<VideoLogProfile> = listOf(VideoLogProfile.OFF),
|
||||
val availableBitrates: List<VideoBitratePreset> = VideoBitratePreset.entries.toList(),
|
||||
val availableCodecs: List<VideoCodec> = VideoCodec.entries.toList(),
|
||||
val previewSizesByResolution: Map<VideoResolutionPreset, Size> = emptyMap(),
|
||||
val cameraInputSizesByResolution: Map<VideoResolutionPreset, Size> = emptyMap(),
|
||||
val recordingSizesByResolution: Map<VideoResolutionPreset, Size> = emptyMap(),
|
||||
val openGatePortraitAspectRatio: Float = 3f / 4f,
|
||||
val availableStabilizationModes: List<VideoStabilizationMode> = emptyList(),
|
||||
|
||||
Reference in new issue
Block a user