jfilo-ebay
Committed by GitHub

CameraX integration (#421)

* Allow registration of external cameras implementations

* CameraX sample integration

* create livekit-android-camerax module

* spotless

---------

Co-authored-by: davidliu <davidliu@deviange.net>
... ... @@ -3,9 +3,10 @@ webrtc = "114.5735.09"
androidJainSipRi = "1.3.0-91"
androidx-activity = "1.9.0"
androidx-camera = "1.3.3"
androidx-core = "1.13.1"
androidx-fragment = "1.5.1"
androidx-lifecycle = "2.5.1"
androidx-lifecycle = "2.8.0"
audioswitch = "89582c47c9a04c62f90aa5e57251af4800a62c9a"
autoService = '1.0.1'
coroutines = "1.6.0"
... ... @@ -28,6 +29,12 @@ viewpager2 = "1.0.0"
[libraries]
android-jain-sip-ri = { module = "javax.sip:android-jain-sip-ri", version.ref = "androidJainSipRi" }
androidx-activity-ktx = { module = "androidx.activity:activity-ktx", version.ref = "androidx-activity" }
androidx-camera-core = { module = "androidx.camera:camera-core", version.ref = "androidx-camera" }
androidx-camera-camera2 = { module = "androidx.camera:camera-camera2", version.ref = "androidx-camera" }
androidx-camera-extensions = { module = "androidx.camera:camera-extensions", version.ref = "androidx-camera" }
androidx-camera-lifecycle = { module = "androidx.camera:camera-lifecycle", version.ref = "androidx-camera" }
androidx-camera-video = { module = "androidx.camera:camera-video", version.ref = "androidx-camera" }
androidx-camera-view = { module = "androidx.camera:camera-view", version.ref = "androidx-camera" }
androidx-core = { module = "androidx.core:core", version.ref = "androidx-core" }
androidx-core-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "androidx-core" }
androidx-fragment-ktx = { module = "androidx.fragment:fragment-ktx", version.ref = "androidx-fragment" }
... ... @@ -38,8 +45,9 @@ dagger-lib = { module = "com.google.dagger:dagger", version.ref = "dagger" }
dagger-compiler = { module = "com.google.dagger:dagger-compiler", version.ref = "dagger" }
groupie = { module = "com.github.lisawray.groupie:groupie", version.ref = "groupie" }
groupie-viewbinding = { module = "com.github.lisawray.groupie:groupie-viewbinding", version.ref = "groupie" }
androidx-lifecycle-runtime-ktx = { module = "androidx.lifecycle:lifecycle-runtime-ktx", version.ref = "androidx-lifecycle" }
androidx-lifecycle-common-java8 = { module = "androidx.lifecycle:lifecycle-common-java8", version.ref = "androidx-lifecycle" }
androidx-lifecycle-runtime-ktx = { module = "androidx.lifecycle:lifecycle-runtime-ktx", version.ref = "androidx-lifecycle" }
androidx-lifecycle-process = { module = "androidx.lifecycle:lifecycle-process", version.ref = "androidx-lifecycle" }
androidx-lifecycle-viewmodel-ktx = { module = "androidx.lifecycle:lifecycle-viewmodel-ktx", version.ref = "androidx-lifecycle" }
kotlinx-serialization-json = { module = "org.jetbrains.kotlinx:kotlinx-serialization-json", version.ref = "kotlinx-serialization" }
leakcanary-android = { module = "com.squareup.leakcanary:leakcanary-android", version.ref = "leakcanaryAndroid" }
... ...
/build
\ No newline at end of file
... ...
plugins {
id "org.jetbrains.dokka"
id 'com.android.library'
id 'kotlin-android'
id 'kotlin-kapt'
}
android {
namespace 'io.livekit.android.camerax'
compileSdkVersion androidSdk.compileVersion
defaultConfig {
minSdkVersion androidSdk.minVersion
targetSdkVersion androidSdk.targetVersion
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles "consumer-rules.pro"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility java_version
targetCompatibility java_version
}
kotlinOptions {
freeCompilerArgs = ["-Xinline-classes", "-opt-in=kotlin.RequiresOptIn"]
jvmTarget = java_version
}
testOptions {
unitTests {
includeAndroidResources = true
}
}
}
dokkaHtml {
moduleName.set("livekit-android-test")
dokkaSourceSets {
configureEach {
skipEmptyPackages.set(true)
includeNonPublic.set(false)
includes.from("module.md")
displayName.set("LiveKit CameraX")
sourceLink {
localDirectory.set(file("src/main/java"))
// URL showing where the source code can be accessed through the web browser
remoteUrl.set(new URL(
"https://github.com/livekit/client-sdk-android/tree/master/livekit-android-camerax/src/main/java"))
// Suffix which is used to append the line number to the URL. Use #L for GitHub
remoteLineSuffix.set("#L")
}
perPackageOption {
matchingRegex.set(".*\\.dagger.*")
suppress.set(true)
}
perPackageOption {
matchingRegex.set(".*\\.util.*")
suppress.set(true)
}
}
}
}
dependencies {
implementation(project(":livekit-android-sdk"))
implementation libs.timber
implementation libs.coroutines.lib
implementation libs.androidx.annotation
api libs.androidx.camera.core
api libs.androidx.camera.camera2
api libs.androidx.camera.lifecycle
testImplementation libs.junit
testImplementation libs.robolectric
androidTestImplementation libs.androidx.test.junit
androidTestImplementation libs.espresso
}
tasks.withType(Test).configureEach {
systemProperty "robolectric.logging.enabled", true
}
apply from: rootProject.file('gradle/gradle-mvn-push.gradle')
afterEvaluate {
publishing {
publications {
// Creates a Maven publication called "release".
release(MavenPublication) {
// Applies the component for the release build variant.
from components.release
// You can then customize attributes of the publication as shown below.
groupId = GROUP
artifactId = POM_ARTIFACT_ID
version = VERSION_NAME
}
}
}
}
... ...
POM_NAME=CameraX Support for LiveKit Android SDK
POM_ARTIFACT_ID=livekit-android-camerax
POM_PACKAGING=aar
... ...
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html
# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}
# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable
# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
\ No newline at end of file
... ...
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
</manifest>
... ...
/*
* Copyright 2024 LiveKit, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package livekit.org.webrtc
import android.content.Context
import android.hardware.camera2.CameraManager
import androidx.camera.camera2.interop.ExperimentalCamera2Interop
import androidx.lifecycle.LifecycleOwner
import io.livekit.android.room.track.video.CameraCapturerWithSize
import io.livekit.android.room.track.video.CameraEventsDispatchHandler
@ExperimentalCamera2Interop
internal class CameraXCapturer(
context: Context,
private val lifecycleOwner: LifecycleOwner,
cameraName: String?,
eventsHandler: CameraVideoCapturer.CameraEventsHandler?,
) : CameraCapturer(cameraName, eventsHandler, Camera2Enumerator(context)) {
var cameraControlListener: CameraXSession.CameraControlListener? = null
override fun createCameraSession(
createSessionCallback: CameraSession.CreateSessionCallback,
events: CameraSession.Events,
applicationContext: Context,
surfaceTextureHelper: SurfaceTextureHelper,
cameraName: String,
width: Int,
height: Int,
framerate: Int,
) {
CameraXSession(
createSessionCallback,
events,
applicationContext,
lifecycleOwner,
surfaceTextureHelper,
cameraName,
width,
height,
framerate,
cameraControlListener,
)
}
}
@ExperimentalCamera2Interop
internal class CameraXCapturerWithSize(
private val capturer: CameraXCapturer,
private val cameraManager: CameraManager,
private val deviceName: String?,
cameraEventsDispatchHandler: CameraEventsDispatchHandler,
) : CameraCapturerWithSize(cameraEventsDispatchHandler), CameraVideoCapturer by capturer {
override fun findCaptureFormat(width: Int, height: Int): Size {
return CameraXHelper.findClosestCaptureFormat(cameraManager, deviceName, width, height)
}
}
... ...
/*
* Copyright 2024 LiveKit, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package livekit.org.webrtc
import android.content.Context
import android.graphics.Rect
import android.graphics.SurfaceTexture
import android.hardware.camera2.CameraCharacteristics
import android.os.Build
import android.os.Build.VERSION
import androidx.camera.camera2.interop.Camera2CameraInfo
import androidx.camera.camera2.interop.ExperimentalCamera2Interop
import androidx.lifecycle.LifecycleOwner
@ExperimentalCamera2Interop
class CameraXEnumerator(
context: Context,
private val lifecycleOwner: LifecycleOwner,
) : Camera2Enumerator(context) {
override fun createCapturer(
deviceName: String?,
eventsHandler: CameraVideoCapturer.CameraEventsHandler?,
): CameraVideoCapturer {
return CameraXCapturer(context, lifecycleOwner, deviceName, eventsHandler)
}
companion object {
fun getSupportedSizes(camera: Camera2CameraInfo): List<Size> {
val streamMap = camera.getCameraCharacteristic(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP)
val supportLevel = camera.getCameraCharacteristic(CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL)
val nativeSizes = streamMap!!.getOutputSizes(SurfaceTexture::class.java)
val sizes = convertSizes(nativeSizes)
val activeArraySize: Rect? = camera.getCameraCharacteristic(CameraCharacteristics.SENSOR_INFO_ACTIVE_ARRAY_SIZE)
return if (VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP_MR1 &&
supportLevel == CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL_LEGACY &&
activeArraySize != null
) {
val filteredSizes = ArrayList<Size>()
for (size in sizes) {
if (activeArraySize.width() * size.height == activeArraySize.height() * size.width) {
filteredSizes.add(size)
}
}
filteredSizes
} else {
sizes
}
}
private fun convertSizes(cameraSizes: Array<android.util.Size>): List<Size> {
val sizes: MutableList<Size> = ArrayList()
for (size in cameraSizes) {
sizes.add(Size(size.width, size.height))
}
return sizes
}
}
}
... ...
/*
* Copyright 2023-2024 LiveKit, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package livekit.org.webrtc
import android.content.Context
import android.hardware.camera2.CameraManager
import androidx.camera.camera2.interop.ExperimentalCamera2Interop
import androidx.lifecycle.LifecycleOwner
import io.livekit.android.room.track.LocalVideoTrackOptions
import io.livekit.android.room.track.video.CameraCapturerUtils
import io.livekit.android.room.track.video.CameraCapturerUtils.findCamera
import io.livekit.android.room.track.video.CameraEventsDispatchHandler
class CameraXHelper {
companion object {
@ExperimentalCamera2Interop
fun getCameraProvider(
lifecycleOwner: LifecycleOwner,
controlListener: CameraXSession.CameraControlListener?,
) = object : CameraCapturerUtils.CameraProvider {
private var enumerator: CameraXEnumerator? = null
override val cameraVersion = 3
override fun provideEnumerator(context: Context): CameraXEnumerator =
enumerator ?: CameraXEnumerator(context, lifecycleOwner).also {
enumerator = it
}
override fun provideCapturer(
context: Context,
options: LocalVideoTrackOptions,
eventsHandler: CameraEventsDispatchHandler,
): VideoCapturer {
val enumerator = provideEnumerator(context)
val targetDeviceName = enumerator.findCamera(options.deviceId, options.position)
val targetVideoCapturer = enumerator.createCapturer(targetDeviceName, eventsHandler) as CameraXCapturer
controlListener?.let {
targetVideoCapturer.cameraControlListener = it
}
return CameraXCapturerWithSize(
targetVideoCapturer,
context.getSystemService(Context.CAMERA_SERVICE) as CameraManager,
targetDeviceName,
eventsHandler,
)
}
override fun isSupported(context: Context) = Camera2Enumerator.isSupported(context)
}
private fun getSupportedFormats(
cameraManager: CameraManager,
cameraId: String?,
): List<CameraEnumerationAndroid.CaptureFormat>? =
Camera2Enumerator.getSupportedFormats(cameraManager, cameraId)
fun findClosestCaptureFormat(
cameraManager: CameraManager,
cameraId: String?,
width: Int,
height: Int,
): Size {
val sizes = getSupportedFormats(cameraManager, cameraId)
?.map { Size(it.width, it.height) }
?: emptyList()
return CameraEnumerationAndroid.getClosestSupportedSize(sizes, width, height)
}
}
}
... ...
/*
* Copyright 2024 LiveKit, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package livekit.org.webrtc
import android.content.Context
import android.hardware.camera2.CameraCharacteristics
import android.hardware.camera2.CameraMetadata
import android.hardware.camera2.CameraMetadata.CONTROL_VIDEO_STABILIZATION_MODE_OFF
import android.hardware.camera2.CameraMetadata.CONTROL_VIDEO_STABILIZATION_MODE_ON
import android.hardware.camera2.CameraMetadata.LENS_OPTICAL_STABILIZATION_MODE_OFF
import android.hardware.camera2.CameraMetadata.LENS_OPTICAL_STABILIZATION_MODE_ON
import android.hardware.camera2.CaptureRequest
import android.util.Range
import android.util.Size
import android.view.Surface
import androidx.camera.camera2.interop.Camera2CameraInfo
import androidx.camera.camera2.interop.Camera2Interop
import androidx.camera.core.Camera
import androidx.camera.core.CameraControl
import androidx.camera.core.CameraSelector
import androidx.camera.core.ImageAnalysis
import androidx.camera.core.Preview
import androidx.camera.core.Preview.SurfaceProvider
import androidx.camera.lifecycle.ProcessCameraProvider
import androidx.core.content.ContextCompat
import androidx.lifecycle.LifecycleOwner
import livekit.org.webrtc.CameraEnumerationAndroid.CaptureFormat
import java.util.concurrent.Executor
import java.util.concurrent.TimeUnit
@androidx.camera.camera2.interop.ExperimentalCamera2Interop
class CameraXSession
internal constructor(
private val sessionCallback: CameraSession.CreateSessionCallback,
private val events: CameraSession.Events,
private val context: Context,
private val lifecycleOwner: LifecycleOwner,
private val surfaceTextureHelper: SurfaceTextureHelper,
private val cameraId: String,
private val width: Int,
private val height: Int,
private val frameRate: Int,
private val cameraControlListener: CameraControlListener? = null,
) : CameraSession {
private var state = SessionState.RUNNING
private var cameraThreadHandler = surfaceTextureHelper.handler
private lateinit var cameraProvider: ProcessCameraProvider
private lateinit var surfaceProvider: SurfaceProvider
private var camera: Camera? = null
private var surface: Surface? = null
private var cameraOrientation: Int = 0
private var isCameraFrontFacing: Boolean = true
private var firstFrameReported = false
private val constructionTimeNs = System.nanoTime()
private var fpsUnitFactor = 1
private var captureFormat: CaptureFormat? = null
private var stabilizationMode = StabilizationMode.NONE
private var surfaceTextureListener = { frame: VideoFrame ->
checkIsOnCameraThread()
if (state != SessionState.RUNNING) {
Logging.d(TAG, "Texture frame captured but camera is no longer running.")
} else {
if (!firstFrameReported) {
firstFrameReported = true
val startTimeMs = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - constructionTimeNs).toInt()
cameraXStartTimeMsHistogram.addSample(startTimeMs)
}
// Undo the mirror that the OS "helps" us with.
// http://developer.android.com/reference/android/hardware/Camera.html#setDisplayOrientation(int)
// Also, undo camera orientation, we report it as rotation instead.
val modifiedFrame = VideoFrame(
CameraSession.createTextureBufferWithModifiedTransformMatrix(
frame.buffer as TextureBufferImpl,
isCameraFrontFacing,
-cameraOrientation,
),
getFrameOrientation(),
frame.timestampNs,
)
events.onFrameCaptured(this@CameraXSession, modifiedFrame)
modifiedFrame.release()
}
}
init {
cameraThreadHandler.post {
start()
}
}
private fun start() {
checkIsOnCameraThread()
Logging.d(TAG, "start")
surfaceTextureHelper.startListening(surfaceTextureListener)
openCamera()
}
override fun stop() {
Logging.d(TAG, "Stop cameraX session on camera $cameraId")
checkIsOnCameraThread()
if (state != SessionState.STOPPED) {
val stopStartTime = System.nanoTime()
state = SessionState.STOPPED
stopInternal()
val stopTimeMs = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - stopStartTime).toInt()
cameraXStopTimeMsHistogram.addSample(stopTimeMs)
}
}
private fun openCamera() {
checkIsOnCameraThread()
Logging.d(TAG, "Opening camera $cameraId")
events.onCameraOpening()
val cameraProviderFuture = ProcessCameraProvider.getInstance(context)
val helperExecutor = Executor { command ->
surfaceTextureHelper.handler.let {
if (it.looper.thread.isAlive) {
it.post(command)
}
}
}
cameraProviderFuture.addListener(
{
// Used to bind the lifecycle of cameras to the lifecycle owner
cameraProvider = cameraProviderFuture.get()
obtainCameraConfiguration()
surfaceTextureHelper.setTextureSize(captureFormat?.width ?: width, captureFormat?.height ?: height)
surface = Surface(surfaceTextureHelper.surfaceTexture)
surfaceProvider = SurfaceProvider { request ->
request.provideSurface(surface!!, helperExecutor) { }
}
// Set image analysis - camera params
val imageAnalysis = setImageAnalysis()
// Select camera by ID
val cameraSelector = CameraSelector.Builder()
.addCameraFilter { cameraInfo -> cameraInfo.filter { Camera2CameraInfo.from(it).cameraId == cameraId } }
.build()
try {
ContextCompat.getMainExecutor(context).execute {
// Preview
val preview = Preview.Builder()
.build()
.also {
it.setSurfaceProvider(surfaceProvider)
}
// Unbind use cases before rebinding
cameraProvider.unbindAll()
// Bind use cases to camera
camera = cameraProvider.bindToLifecycle(lifecycleOwner, cameraSelector, imageAnalysis, preview).apply {
cameraControlListener?.onCameraControlAvailable(this.cameraControl)
}
}
sessionCallback.onDone(this@CameraXSession)
} catch (e: Exception) {
reportError("Failed to open camera: $e")
}
},
helperExecutor,
)
}
private fun setImageAnalysis() = ImageAnalysis.Builder()
.setTargetResolution(Size(captureFormat?.width ?: width, captureFormat?.height ?: height))
.setBackpressureStrategy(ImageAnalysis.STRATEGY_KEEP_ONLY_LATEST).apply {
val cameraExtender = Camera2Interop.Extender(this)
captureFormat?.let { captureFormat ->
cameraExtender.setCaptureRequestOption(
CaptureRequest.CONTROL_AE_TARGET_FPS_RANGE,
Range(
captureFormat.framerate.min / fpsUnitFactor,
captureFormat.framerate.max / fpsUnitFactor,
),
)
cameraExtender.setCaptureRequestOption(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON)
cameraExtender.setCaptureRequestOption(CaptureRequest.CONTROL_AE_LOCK, false)
when (stabilizationMode) {
StabilizationMode.OPTICAL -> {
cameraExtender.setCaptureRequestOption(CaptureRequest.LENS_OPTICAL_STABILIZATION_MODE, LENS_OPTICAL_STABILIZATION_MODE_ON)
cameraExtender.setCaptureRequestOption(CaptureRequest.CONTROL_VIDEO_STABILIZATION_MODE, CONTROL_VIDEO_STABILIZATION_MODE_OFF)
}
StabilizationMode.VIDEO -> {
cameraExtender.setCaptureRequestOption(CaptureRequest.CONTROL_VIDEO_STABILIZATION_MODE, CONTROL_VIDEO_STABILIZATION_MODE_ON)
cameraExtender.setCaptureRequestOption(CaptureRequest.LENS_OPTICAL_STABILIZATION_MODE, LENS_OPTICAL_STABILIZATION_MODE_OFF)
}
else -> Unit
}
}
}
.build()
private fun stopInternal() {
Logging.d(TAG, "Stop internal")
checkIsOnCameraThread()
surfaceTextureHelper.stopListening()
if (surface != null) {
surface!!.release()
surface = null
}
ContextCompat.getMainExecutor(context).execute {
cameraProvider.unbindAll()
}
Logging.d(TAG, "Stop done")
}
private fun reportError(error: String) {
checkIsOnCameraThread()
Logging.e(TAG, "Error: $error")
val startFailure = camera == null && state != SessionState.STOPPED
state = SessionState.STOPPED
stopInternal()
if (startFailure) {
sessionCallback.onFailure(CameraSession.FailureType.ERROR, error)
} else {
events.onCameraError(this, error)
}
}
private fun obtainCameraConfiguration() {
val camera = cameraProvider.availableCameraInfos.map { Camera2CameraInfo.from(it) }.first { it.cameraId == cameraId }
cameraOrientation = camera.getCameraCharacteristic(CameraCharacteristics.SENSOR_ORIENTATION) ?: -1
isCameraFrontFacing = camera.getCameraCharacteristic(CameraCharacteristics.LENS_FACING) == CameraMetadata.LENS_FACING_FRONT
findCaptureFormat(camera)
findStabilizationMode(camera)
}
private fun findCaptureFormat(camera: Camera2CameraInfo) {
val fpsRanges = camera.getCameraCharacteristic(CameraCharacteristics.CONTROL_AE_AVAILABLE_TARGET_FPS_RANGES)
fpsUnitFactor = Camera2Enumerator.getFpsUnitFactor(fpsRanges)
val framerateRanges = Camera2Enumerator.convertFramerates(fpsRanges, fpsUnitFactor)
val sizes = CameraXEnumerator.getSupportedSizes(camera)
Logging.d(TAG, "Available preview sizes: $sizes")
Logging.d(TAG, "Available fps ranges: $framerateRanges")
if (framerateRanges.isEmpty() || sizes.isEmpty()) {
reportError("No supported capture formats.")
return
}
val bestFpsRange = CameraEnumerationAndroid.getClosestSupportedFramerateRange(framerateRanges, frameRate)
val bestSize = CameraEnumerationAndroid.getClosestSupportedSize(sizes, width, height)
CameraEnumerationAndroid.reportCameraResolution(cameraXResolutionHistogram, bestSize)
captureFormat = CaptureFormat(bestSize.width, bestSize.height, bestFpsRange)
Logging.d(TAG, "Using capture format: $captureFormat")
}
private fun findStabilizationMode(camera: Camera2CameraInfo) {
val availableOpticalStabilization: IntArray? = camera.getCameraCharacteristic(CameraCharacteristics.LENS_INFO_AVAILABLE_OPTICAL_STABILIZATION)
if (availableOpticalStabilization?.contains(LENS_OPTICAL_STABILIZATION_MODE_ON) == true) {
stabilizationMode = StabilizationMode.OPTICAL
} else {
val availableVideoStabilization: IntArray? = camera.getCameraCharacteristic(CameraCharacteristics.LENS_INFO_AVAILABLE_OPTICAL_STABILIZATION)
if (availableVideoStabilization?.contains(CONTROL_VIDEO_STABILIZATION_MODE_ON) == true) {
stabilizationMode = StabilizationMode.VIDEO
}
}
}
private fun checkIsOnCameraThread() {
check(Thread.currentThread() === cameraThreadHandler.looper.thread) { "Wrong thread" }
}
private fun getFrameOrientation(): Int {
var rotation = CameraSession.getDeviceOrientation(context)
if (!isCameraFrontFacing) {
rotation = 360 - rotation
}
return (cameraOrientation + rotation) % 360
}
companion object {
private const val TAG = "CameraXSession"
private val cameraXStartTimeMsHistogram = Histogram.createCounts("WebRTC.Android.CameraX.StartTimeMs", 1, 10000, 50)
private val cameraXStopTimeMsHistogram = Histogram.createCounts("WebRTC.Android.CameraX.StopTimeMs", 1, 10000, 50)
private val cameraXResolutionHistogram = Histogram.createEnumeration("WebRTC.Android.CameraX.Resolution", CameraEnumerationAndroid.COMMON_RESOLUTIONS.size)
}
internal enum class SessionState {
RUNNING, STOPPED
}
internal enum class StabilizationMode {
OPTICAL, VIDEO, NONE
}
interface CameraControlListener {
fun onCameraControlAvailable(control: CameraControl)
}
}
... ...
... ... @@ -58,6 +58,7 @@ dependencies {
// If building the sample app outside the context of this repo, replace the following with:
// api "io.livekit:livekit-android:<version>"
api project(":livekit-android-sdk")
api project(":livekit-android-camerax")
api libs.androidx.core.ktx
api libs.appcompat
... ... @@ -67,6 +68,7 @@ dependencies {
api libs.androidx.lifecycle.runtime.ktx
api libs.androidx.lifecycle.viewmodel.ktx
api libs.androidx.lifecycle.common.java8
api libs.androidx.lifecycle.process
api libs.protobuf.javalite
api libs.androidx.preference.ktx
// debugImplementation because LeakCanary should only run in debug builds.
... ...
... ... @@ -39,6 +39,14 @@ dependencies {
// as well as classes common to both sample apps.
implementation project(":sample-app-common")
def camerax_version = "1.2.2"
implementation "androidx.camera:camera-core:${camerax_version}"
implementation "androidx.camera:camera-camera2:${camerax_version}"
implementation "androidx.camera:camera-lifecycle:${camerax_version}"
implementation "androidx.camera:camera-video:${camerax_version}"
implementation "androidx.camera:camera-view:${camerax_version}"
implementation "androidx.camera:camera-extensions:${camerax_version}"
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation libs.coroutines.lib
... ...
... ... @@ -26,9 +26,11 @@ import android.widget.Toast
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import androidx.camera.core.CameraControl
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import com.xwray.groupie.GroupieAdapter
import io.livekit.android.room.track.video.CameraCapturerUtils
import io.livekit.android.sample.common.R
import io.livekit.android.sample.databinding.CallActivityBinding
import io.livekit.android.sample.dialog.showAudioProcessorSwitchDialog
... ... @@ -37,9 +39,14 @@ import io.livekit.android.sample.dialog.showSelectAudioDeviceDialog
import io.livekit.android.sample.model.StressTest
import kotlinx.coroutines.flow.collectLatest
import kotlinx.parcelize.Parcelize
import livekit.org.webrtc.CameraXHelper
import livekit.org.webrtc.CameraXSession
class CallActivity : AppCompatActivity() {
private var cameraProvider: CameraCapturerUtils.CameraProvider? = null
private var cameraControl: CameraControl? = null
private val viewModel: CallViewModel by viewModelByFactory {
val args = intent.getParcelableExtra<BundleArgs>(KEY_ARGS)
?: throw NullPointerException("args is null!")
... ... @@ -66,6 +73,7 @@ class CallActivity : AppCompatActivity() {
viewModel.startScreenCapture(data)
}
@androidx.camera.camera2.interop.ExperimentalCamera2Interop
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
... ... @@ -73,6 +81,22 @@ class CallActivity : AppCompatActivity() {
setContentView(binding.root)
val controlListener = object : CameraXSession.CameraControlListener {
override fun onCameraControlAvailable(control: CameraControl) {
cameraControl = control
}
}
CameraXHelper.getCameraProvider(
this,
controlListener,
).let {
if (it.isSupported(this@CallActivity)) {
CameraCapturerUtils.registerCameraProvider(it)
cameraProvider = it
}
}
// Audience row setup
val audienceAdapter = GroupieAdapter()
binding.audienceRow.apply {
... ... @@ -216,6 +240,9 @@ class CallActivity : AppCompatActivity() {
override fun onDestroy() {
binding.audienceRow.adapter = null
binding.speakerView.adapter = null
cameraProvider?.let {
CameraCapturerUtils.unregisterCameraProvider(it)
}
super.onDestroy()
}
... ...
... ... @@ -12,3 +12,4 @@ include ':sample-app-basic'
include ':sample-app-record-local'
include ':examples:selfie-segmentation'
include ':livekit-android-test'
include ':livekit-android-camerax'
... ...