Skip to content

Commit 5003786

Browse files
Support for selecting cameras by their physical id (#668)
* Support selecting physical camera This commit adds support for selecting a specific physical camera when multiple physical cameras are available under a single logical camera. This is particularly relevant for devices with multi-camera systems. * Return early * Fix typo * Add comment * Fix comment * Make function private * add changeset --------- Co-authored-by: davidliu <davidliu@deviange.net>
1 parent 216ce0a commit 5003786

File tree

5 files changed

+50
-6
lines changed

5 files changed

+50
-6
lines changed

.changeset/sixty-snakes-own.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"client-sdk-android": minor
3+
---
4+
5+
CameraX: support for selecting cameras by their physical id

livekit-android-camerax/src/main/java/livekit/org/webrtc/CameraXCapturer.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2024 LiveKit, Inc.
2+
* Copyright 2024-2025 LiveKit, Inc.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -37,6 +37,7 @@ internal class CameraXCapturer(
3737
cameraName: String?,
3838
eventsHandler: CameraVideoCapturer.CameraEventsHandler?,
3939
private val useCases: Array<out UseCase> = emptyArray(),
40+
var physicalCameraId: String? = null,
4041
) : CameraCapturer(cameraName, eventsHandler, CameraXEnumerator(context, lifecycleOwner)) {
4142

4243
@FlowObservable
@@ -93,6 +94,7 @@ internal class CameraXCapturer(
9394
height,
9495
framerate,
9596
useCases,
97+
physicalCameraId,
9698
)
9799
}
98100
}

livekit-android-camerax/src/main/java/livekit/org/webrtc/CameraXEnumerator.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2024 LiveKit, Inc.
2+
* Copyright 2024-2025 LiveKit, Inc.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -35,13 +35,14 @@ class CameraXEnumerator(
3535
context: Context,
3636
private val lifecycleOwner: LifecycleOwner,
3737
private val useCases: Array<out UseCase> = emptyArray(),
38+
var physicalCameraId: String? = null,
3839
) : Camera2Enumerator(context) {
3940

4041
override fun createCapturer(
4142
deviceName: String?,
4243
eventsHandler: CameraVideoCapturer.CameraEventsHandler?,
4344
): CameraVideoCapturer {
44-
return CameraXCapturer(context, lifecycleOwner, deviceName, eventsHandler, useCases)
45+
return CameraXCapturer(context, lifecycleOwner, deviceName, eventsHandler, useCases, physicalCameraId)
4546
}
4647

4748
companion object {

livekit-android-camerax/src/main/java/livekit/org/webrtc/CameraXHelper.kt

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2023-2024 LiveKit, Inc.
2+
* Copyright 2023-2025 LiveKit, Inc.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,6 +18,7 @@ package livekit.org.webrtc
1818

1919
import android.content.Context
2020
import android.hardware.camera2.CameraManager
21+
import android.os.Build
2122
import androidx.camera.camera2.interop.ExperimentalCamera2Interop
2223
import androidx.camera.core.UseCase
2324
import androidx.lifecycle.Lifecycle
@@ -61,12 +62,21 @@ class CameraXHelper {
6162
eventsHandler: CameraEventsDispatchHandler,
6263
): VideoCapturer {
6364
val enumerator = provideEnumerator(context)
64-
val targetDeviceName = enumerator.findCamera(options.deviceId, options.position)
65+
val cameraManager = context.getSystemService(Context.CAMERA_SERVICE) as CameraManager
66+
val deviceId = options.deviceId
67+
var targetDeviceName: String? = null
68+
if (deviceId != null) {
69+
targetDeviceName = findCameraById(cameraManager, deviceId)
70+
}
71+
if (targetDeviceName == null) {
72+
// Fallback to enumerator.findCamera which can't find camera by physical id but it will choose the closest one.
73+
targetDeviceName = enumerator.findCamera(deviceId, options.position)
74+
}
6575
val targetVideoCapturer = enumerator.createCapturer(targetDeviceName, eventsHandler) as CameraXCapturer
6676

6777
return CameraXCapturerWithSize(
6878
targetVideoCapturer,
69-
context.getSystemService(Context.CAMERA_SERVICE) as CameraManager,
79+
cameraManager,
7080
targetDeviceName,
7181
eventsHandler,
7282
)
@@ -75,6 +85,23 @@ class CameraXHelper {
7585
override fun isSupported(context: Context): Boolean {
7686
return Camera2Enumerator.isSupported(context) && lifecycleOwner.lifecycle.currentState.isAtLeast(Lifecycle.State.INITIALIZED)
7787
}
88+
89+
private fun findCameraById(cameraManager: CameraManager, deviceId: String): String? {
90+
for (id in cameraManager.cameraIdList) {
91+
if (id == deviceId) return id // This means the provided id is logical id.
92+
93+
val characteristics = cameraManager.getCameraCharacteristics(id)
94+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
95+
val ids = characteristics.physicalCameraIds
96+
if (ids.contains(deviceId)) {
97+
// This means the provided id is physical id.
98+
enumerator?.physicalCameraId = deviceId
99+
return id // This is its logical id.
100+
}
101+
}
102+
}
103+
return null
104+
}
78105
}
79106

80107
private fun getSupportedFormats(

livekit-android-camerax/src/main/java/livekit/org/webrtc/CameraXSession.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import android.hardware.camera2.CameraMetadata.CONTROL_VIDEO_STABILIZATION_MODE_
2424
import android.hardware.camera2.CameraMetadata.LENS_OPTICAL_STABILIZATION_MODE_OFF
2525
import android.hardware.camera2.CameraMetadata.LENS_OPTICAL_STABILIZATION_MODE_ON
2626
import android.hardware.camera2.CaptureRequest
27+
import android.os.Build
2728
import android.os.Handler
2829
import android.util.Range
2930
import android.util.Size
@@ -61,6 +62,7 @@ internal constructor(
6162
private val height: Int,
6263
private val frameRate: Int,
6364
private val useCases: Array<out UseCase> = emptyArray(),
65+
var physicalCameraId: String? = null,
6466
) : CameraSession {
6567

6668
private var state = SessionState.RUNNING
@@ -206,6 +208,13 @@ internal constructor(
206208

207209
private fun <T> ExtendableBuilder<T>.applyCameraSettings(): ExtendableBuilder<T> {
208210
val cameraExtender = Camera2Interop.Extender(this)
211+
212+
physicalCameraId?.let { physicalId ->
213+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
214+
cameraExtender.setPhysicalCameraId(physicalId)
215+
}
216+
}
217+
209218
val captureFormat = this@CameraXSession.captureFormat ?: return this
210219
cameraExtender.setCaptureRequestOption(
211220
CaptureRequest.CONTROL_AE_TARGET_FPS_RANGE,

0 commit comments

Comments
 (0)