CameraXSession.kt
15.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
/*
* Copyright 2024-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.
* 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.CameraManager
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.os.Build
import android.os.Handler
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.CameraSelector
import androidx.camera.core.ExtendableBuilder
import androidx.camera.core.Preview
import androidx.camera.core.Preview.SurfaceProvider
import androidx.camera.core.UseCase
import androidx.camera.core.resolutionselector.ResolutionSelector
import androidx.camera.core.resolutionselector.ResolutionStrategy
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
/**
* @suppress
*/
@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 useCases: Array<out UseCase> = emptyArray(),
) : CameraSession {
private var state = SessionState.RUNNING
private var cameraThreadHandler: Handler = surfaceTextureHelper.handler
private lateinit var cameraProvider: ProcessCameraProvider
private lateinit var surfaceProvider: SurfaceProvider
var camera: Camera? = null
private set
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()
}
}
private val cameraDevice: CameraDeviceId
get() {
val cameraManager = context.getSystemService(Context.CAMERA_SERVICE) as CameraManager
return findCamera(cameraManager, cameraId)
?: throw IllegalArgumentException("Camera ID $cameraId not found")
}
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 ->
surface?.let {
request.provideSurface(it, helperExecutor) { }
} ?: request.willNotProvideSurface()
}
// Select camera by ID
val cameraSelector = CameraSelector.Builder()
.addCameraFilter { cameraInfo -> cameraInfo.filter { Camera2CameraInfo.from(it).cameraId == cameraDevice.deviceId } }
.build()
try {
ContextCompat.getMainExecutor(context).execute {
// Preview
val preview = Preview.Builder()
.setResolutionSelector(
ResolutionSelector.Builder()
.setResolutionStrategy(
ResolutionStrategy(
Size(captureFormat?.width ?: width, captureFormat?.height ?: height),
ResolutionStrategy.FALLBACK_RULE_CLOSEST_HIGHER_THEN_LOWER,
),
)
.build(),
)
.applyCameraSettings()
.build()
.also {
it.setSurfaceProvider(surfaceProvider)
}
// Unbind use cases before rebinding
cameraProvider.unbindAll()
// Bind use cases to camera
camera = cameraProvider.bindToLifecycle(
lifecycleOwner,
cameraSelector,
preview,
*useCases,
)
cameraThreadHandler.post {
sessionCallback.onDone(this@CameraXSession)
}
}
} catch (e: Exception) {
reportError("Failed to open camera: $e")
}
},
helperExecutor,
)
}
private fun <T> ExtendableBuilder<T>.applyCameraSettings(): ExtendableBuilder<T> {
val cameraExtender = Camera2Interop.Extender(this)
cameraDevice.physicalId?.let { physicalId ->
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
cameraExtender.setPhysicalCameraId(physicalId)
}
}
val captureFormat = this@CameraXSession.captureFormat ?: return this
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 -> {
}
}
return this
}
private fun stopInternal() {
Logging.d(TAG, "Stop internal")
checkIsOnCameraThread()
surfaceTextureHelper.stopListening()
if (surface != null) {
surface!!.release()
surface = null
}
ContextCompat.getMainExecutor(context).execute {
cameraProvider.unbindAll()
cameraThreadHandler.postAtFrontOfQueue {
events.onCameraClosed(this)
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 == cameraDevice.deviceId }
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
}
private data class CameraDeviceId(val deviceId: String, val physicalId: String?)
private fun findCamera(
cameraManager: CameraManager,
deviceId: String,
): CameraDeviceId? {
for (id in cameraManager.cameraIdList) {
// First check if deviceId is a direct logical camera ID
if (id == deviceId) return CameraDeviceId(id, null)
// Then check if deviceId is a physical camera ID in a logical camera
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
val characteristic = cameraManager.getCameraCharacteristics(id)
for (physicalId in characteristic.physicalCameraIds) {
if (deviceId == physicalId) {
return CameraDeviceId(id, physicalId)
}
}
}
}
return null
}
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
}
}