Skip to content

Refactor and enable switching between cameras using physical id. #676

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Jun 1, 2025
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import androidx.camera.camera2.interop.ExperimentalCamera2Interop
import androidx.camera.core.Camera
import androidx.camera.core.UseCase
import androidx.lifecycle.LifecycleOwner
import io.livekit.android.room.track.video.CameraCapturerUtils.CameraDeviceInfo
import io.livekit.android.room.track.video.CameraCapturerUtils.findCamera
import io.livekit.android.room.track.video.CameraCapturerWithSize
import io.livekit.android.room.track.video.CameraEventsDispatchHandler
import io.livekit.android.util.FlowObservable
Expand All @@ -32,13 +34,13 @@ import kotlinx.coroutines.flow.StateFlow

@ExperimentalCamera2Interop
internal class CameraXCapturer(
context: Context,
private val enumerator: CameraXEnumerator,
private val lifecycleOwner: LifecycleOwner,
cameraName: String?,
private val cameraManager: CameraManager,
private var cameraDevice: CameraDeviceInfo,
eventsHandler: CameraVideoCapturer.CameraEventsHandler?,
private val useCases: Array<out UseCase> = emptyArray(),
var physicalCameraId: String? = null,
) : CameraCapturer(cameraName, eventsHandler, CameraXEnumerator(context, lifecycleOwner)) {
) : CameraCapturer(cameraDevice.deviceId, eventsHandler, enumerator) {

@FlowObservable
@get:FlowObservable
Expand Down Expand Up @@ -89,14 +91,21 @@ internal class CameraXCapturer(
applicationContext,
lifecycleOwner,
surfaceTextureHelper,
cameraName,
cameraDevice,
width,
height,
framerate,
useCases,
physicalCameraId,
)
}

override fun switchCamera(switchEventsHandler: CameraVideoCapturer.CameraSwitchHandler?, cameraName: String?) {
val device = enumerator.findCamera(cameraManager, cameraName)
if (device == null) return

cameraDevice = device
super.switchCamera(switchEventsHandler, this.cameraName)
}
}

@ExperimentalCamera2Interop
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@ import android.content.Context
import android.graphics.Rect
import android.graphics.SurfaceTexture
import android.hardware.camera2.CameraCharacteristics
import android.hardware.camera2.CameraManager
import android.os.Build
import android.os.Build.VERSION
import androidx.camera.camera2.interop.Camera2CameraInfo
import androidx.camera.camera2.interop.ExperimentalCamera2Interop
import androidx.camera.core.UseCase
import androidx.lifecycle.LifecycleOwner
import io.livekit.android.room.track.video.CameraCapturerUtils.findCamera

/**
* @suppress
Expand All @@ -35,14 +37,15 @@ class CameraXEnumerator(
context: Context,
private val lifecycleOwner: LifecycleOwner,
private val useCases: Array<out UseCase> = emptyArray(),
var physicalCameraId: String? = null,
private val cameraManager: CameraManager = context.getSystemService(Context.CAMERA_SERVICE) as CameraManager,
) : Camera2Enumerator(context) {

override fun createCapturer(
deviceName: String?,
eventsHandler: CameraVideoCapturer.CameraEventsHandler?,
): CameraVideoCapturer {
return CameraXCapturer(context, lifecycleOwner, deviceName, eventsHandler, useCases, physicalCameraId)
val deviceInfo = findCamera(cameraManager!!, deviceName, fallback = true)!!
return CameraXCapturer(this, lifecycleOwner, cameraManager!!, deviceInfo, eventsHandler, useCases)
Copy link
Contributor Author

@KasemJaffer KasemJaffer May 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Passing this instead of creating it again inside CameraXCapturer. From my testing I didn't see any problems with this change. Let me know if i'm missing anything.

}

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package livekit.org.webrtc

import android.content.Context
import android.hardware.camera2.CameraManager
import android.os.Build
import androidx.camera.camera2.interop.ExperimentalCamera2Interop
import androidx.camera.core.UseCase
import androidx.lifecycle.Lifecycle
Expand Down Expand Up @@ -63,15 +62,10 @@ class CameraXHelper {
): VideoCapturer {
val enumerator = provideEnumerator(context)
val cameraManager = context.getSystemService(Context.CAMERA_SERVICE) as CameraManager
val deviceId = options.deviceId
var targetDeviceName: String? = null
if (deviceId != null) {
targetDeviceName = findCameraById(cameraManager, deviceId)
}
if (targetDeviceName == null) {
// Fallback to enumerator.findCamera which can't find camera by physical id but it will choose the closest one.
targetDeviceName = enumerator.findCamera(deviceId, options.position)
}

val targetDevice = enumerator.findCamera(cameraManager, options.deviceId, options.position)
val targetDeviceName = targetDevice?.physicalId ?: targetDevice?.deviceId

val targetVideoCapturer = enumerator.createCapturer(targetDeviceName, eventsHandler) as CameraXCapturer

return CameraXCapturerWithSize(
Expand All @@ -85,23 +79,6 @@ class CameraXHelper {
override fun isSupported(context: Context): Boolean {
return Camera2Enumerator.isSupported(context) && lifecycleOwner.lifecycle.currentState.isAtLeast(Lifecycle.State.INITIALIZED)
}

private fun findCameraById(cameraManager: CameraManager, deviceId: String): String? {
for (id in cameraManager.cameraIdList) {
if (id == deviceId) return id // This means the provided id is logical id.

val characteristics = cameraManager.getCameraCharacteristics(id)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
val ids = characteristics.physicalCameraIds
if (ids.contains(deviceId)) {
// This means the provided id is physical id.
enumerator?.physicalCameraId = deviceId
return id // This is its logical id.
}
}
}
return null
}
}

private fun getSupportedFormats(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import androidx.camera.core.resolutionselector.ResolutionStrategy
import androidx.camera.lifecycle.ProcessCameraProvider
import androidx.core.content.ContextCompat
import androidx.lifecycle.LifecycleOwner
import io.livekit.android.room.track.video.CameraCapturerUtils
import livekit.org.webrtc.CameraEnumerationAndroid.CaptureFormat
import java.util.concurrent.Executor
import java.util.concurrent.TimeUnit
Expand All @@ -57,12 +58,11 @@ internal constructor(
private val context: Context,
private val lifecycleOwner: LifecycleOwner,
private val surfaceTextureHelper: SurfaceTextureHelper,
private val cameraId: String,
private val cameraDevice: CameraCapturerUtils.CameraDeviceInfo,
private val width: Int,
private val height: Int,
private val frameRate: Int,
private val useCases: Array<out UseCase> = emptyArray(),
var physicalCameraId: String? = null,
) : CameraSession {

private var state = SessionState.RUNNING
Expand Down Expand Up @@ -120,7 +120,7 @@ internal constructor(
}

override fun stop() {
Logging.d(TAG, "Stop cameraX session on camera $cameraId")
Logging.d(TAG, "Stop cameraX session on camera $cameraDevice")
checkIsOnCameraThread()
if (state != SessionState.STOPPED) {
val stopStartTime = System.nanoTime()
Expand All @@ -133,7 +133,7 @@ internal constructor(

private fun openCamera() {
checkIsOnCameraThread()
Logging.d(TAG, "Opening camera $cameraId")
Logging.d(TAG, "Opening camera $cameraDevice")
events.onCameraOpening()
val cameraProviderFuture = ProcessCameraProvider.getInstance(context)
val helperExecutor = Executor { command ->
Expand All @@ -160,7 +160,7 @@ internal constructor(

// Select camera by ID
val cameraSelector = CameraSelector.Builder()
.addCameraFilter { cameraInfo -> cameraInfo.filter { Camera2CameraInfo.from(it).cameraId == cameraId } }
.addCameraFilter { cameraInfo -> cameraInfo.filter { Camera2CameraInfo.from(it).cameraId == cameraDevice.deviceId } }
.build()

try {
Expand Down Expand Up @@ -209,7 +209,7 @@ internal constructor(
private fun <T> ExtendableBuilder<T>.applyCameraSettings(): ExtendableBuilder<T> {
val cameraExtender = Camera2Interop.Extender(this)

physicalCameraId?.let { physicalId ->
cameraDevice.physicalId?.let { physicalId ->
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
cameraExtender.setPhysicalCameraId(physicalId)
}
Expand Down Expand Up @@ -275,7 +275,7 @@ internal constructor(
}

private fun obtainCameraConfiguration() {
val camera = cameraProvider.availableCameraInfos.map { Camera2CameraInfo.from(it) }.first { it.cameraId == cameraId }
val camera = cameraProvider.availableCameraInfos.map { Camera2CameraInfo.from(it) }.first { it.cameraId == cameraDevice.deviceId }

cameraOrientation = camera.getCameraCharacteristic(CameraCharacteristics.SENSOR_ORIENTATION) ?: -1
isCameraFrontFacing = camera.getCameraCharacteristic(CameraCharacteristics.LENS_FACING) == CameraMetadata.LENS_FACING_FRONT
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2023-2024 LiveKit, Inc.
* Copyright 2023-2025 LiveKit, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -19,6 +19,7 @@ package io.livekit.android.room.track
import android.Manifest
import android.content.Context
import android.content.pm.PackageManager
import android.hardware.camera2.CameraManager
import androidx.core.content.ContextCompat
import dagger.assisted.Assisted
import dagger.assisted.AssistedFactory
Expand All @@ -27,9 +28,9 @@ import io.livekit.android.memory.CloseableManager
import io.livekit.android.memory.SurfaceTextureHelperCloser
import io.livekit.android.room.DefaultsManager
import io.livekit.android.room.track.video.CameraCapturerUtils
import io.livekit.android.room.track.video.CameraCapturerUtils.CameraDeviceInfo
import io.livekit.android.room.track.video.CameraCapturerUtils.createCameraEnumerator
import io.livekit.android.room.track.video.CameraCapturerUtils.findCamera
import io.livekit.android.room.track.video.CameraCapturerUtils.getCameraPosition
import io.livekit.android.room.track.video.CameraCapturerWithSize
import io.livekit.android.room.track.video.CaptureDispatchObserver
import io.livekit.android.room.track.video.ScaleCropVideoProcessor
Expand Down Expand Up @@ -179,26 +180,29 @@ constructor(
return
}

var targetDeviceId: String? = null
var targetDevice: CameraDeviceInfo? = null
val enumerator = createCameraEnumerator(context)
val cameraManager = context.getSystemService(Context.CAMERA_SERVICE) as CameraManager
if (deviceId != null || position != null) {
targetDeviceId = enumerator.findCamera(deviceId, position, fallback = false)
targetDevice = enumerator.findCamera(cameraManager, deviceId, position, fallback = false)
}

if (targetDeviceId == null) {
if (targetDevice == null) {
val deviceNames = enumerator.deviceNames
if (deviceNames.size < 2) {
LKLog.w { "No available cameras to switch to!" }
return
}
val currentIndex = deviceNames.indexOf(options.deviceId)
targetDeviceId = deviceNames[(currentIndex + 1) % deviceNames.size]
val targetDeviceId = deviceNames[(currentIndex + 1) % deviceNames.size]
targetDevice = enumerator.findCamera(cameraManager, targetDeviceId, fallback = false)
}

val targetDeviceId = targetDevice?.physicalId ?: targetDevice?.deviceId
fun updateCameraOptions() {
val newOptions = options.copy(
deviceId = targetDeviceId,
position = enumerator.getCameraPosition(targetDeviceId),
position = targetDevice?.position,
)
options = newOptions
}
Expand Down Expand Up @@ -243,7 +247,7 @@ constructor(
LKLog.w { "switching camera failed: $errorDescription" }
}
}
if (targetDeviceId == null) {
if (targetDevice == null) {
LKLog.w { "No target camera found!" }
return
} else {
Expand Down
Loading
Loading