Skip to content

Commit

Permalink
address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
davidjiagoogle committed Jan 29, 2025
1 parent 099d7cd commit 0f88329
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ private val QUALITY_RANGE_MAP = mapOf(
HD to Range.create(720, 1079),
SD to Range.create(241, 719)
)
private var zoomScaleMap = mutableMapOf<LensFacing, Float>()

context(CameraSessionContext)
internal suspend fun runSingleCameraSession(
Expand Down Expand Up @@ -321,20 +320,16 @@ internal suspend fun processTransientSettingEvents(
}

context(CameraSessionContext)
internal fun setZoomScale(camera: Camera, zoomScale: Float) {
internal fun setZoomScale(camera: Camera, zoomScaleRelative: Float) {
camera.cameraInfo.zoomState.value?.let { zoomState ->
transientSettings.value?.let { transientSettings ->
if (!zoomScaleMap.containsKey(transientSettings.primaryLensFacing)) {
zoomScaleMap[transientSettings.primaryLensFacing] = 1f
}
val oldScale = zoomScaleMap[transientSettings.primaryLensFacing]!!
val finalScale =
(oldScale * zoomScale).coerceIn(
(zoomScale.value * zoomScaleRelative).coerceIn(
zoomState.minZoomRatio,
zoomState.maxZoomRatio
)
camera.cameraControl.setZoomRatio(finalScale)
zoomScaleMap[transientSettings.primaryLensFacing] = finalScale
zoomScale.update { finalScale }
currentCameraState.update { old ->
old.copy(zoomScale = finalScale)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,6 @@ internal data class CameraSessionContext(
val videoCaptureControlEvents: Channel<VideoCaptureControlEvent>,
val currentCameraState: MutableStateFlow<CameraState>,
val surfaceRequests: MutableStateFlow<SurfaceRequest?>,
val transientSettings: StateFlow<TransientSessionSettings?>
val transientSettings: StateFlow<TransientSessionSettings?>,
var zoomScale: MutableStateFlow<Float>
)
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@ constructor(

private val _surfaceRequest = MutableStateFlow<SurfaceRequest?>(null)

private val _cameraSessionZoomScale = MutableStateFlow(1f)
private var prevCameraSessionLensFacing: LensFacing? = null

override fun getSurfaceRequest(): StateFlow<SurfaceRequest?> = _surfaceRequest.asStateFlow()

override suspend fun initialize(
Expand Down Expand Up @@ -351,6 +354,10 @@ constructor(
}
}.distinctUntilChanged()
.collectLatest { sessionSettings ->
if (transientSettings.value?.primaryLensFacing != prevCameraSessionLensFacing) {
_cameraSessionZoomScale.update { 1f }
}
prevCameraSessionLensFacing = transientSettings.value?.primaryLensFacing
coroutineScope {
with(
CameraSessionContext(
Expand All @@ -362,7 +369,8 @@ constructor(
videoCaptureControlEvents = videoCaptureControlEvents,
currentCameraState = _currentCameraState,
surfaceRequests = _surfaceRequest,
transientSettings = transientSettings
transientSettings = transientSettings,
zoomScale = _cameraSessionZoomScale
)
) {
try {
Expand Down

0 comments on commit 0f88329

Please # to comment.