davidliu

Potential fix for camera1 crash when getting dimensions after capture has started

@@ -209,6 +209,8 @@ constructor( @@ -209,6 +209,8 @@ constructor(
209 position = enumerator.getCameraPosition(targetDeviceName!!) 209 position = enumerator.getCameraPosition(targetDeviceName!!)
210 ) 210 )
211 if (targetVideoCapturer is Camera1Capturer) { 211 if (targetVideoCapturer is Camera1Capturer) {
  212 + // Cache supported capture formats ahead of time to avoid future camera locks.
  213 + Camera1Helper.getSupportedFormats(Camera1Helper.getCameraId(newOptions.deviceId))
212 return Pair( 214 return Pair(
213 Camera1CapturerWithSize(targetVideoCapturer, targetDeviceName), 215 Camera1CapturerWithSize(targetVideoCapturer, targetDeviceName),
214 newOptions 216 newOptions
@@ -10,13 +10,16 @@ internal class Camera1Helper { @@ -10,13 +10,16 @@ internal class Camera1Helper {
10 companion object { 10 companion object {
11 fun getCameraId(deviceName: String?) = Camera1Enumerator.getCameraIndex(deviceName) 11 fun getCameraId(deviceName: String?) = Camera1Enumerator.getCameraIndex(deviceName)
12 12
  13 + fun getSupportedFormats(cameraId: Int): List<CameraEnumerationAndroid.CaptureFormat> =
  14 + Camera1Enumerator.getSupportedFormats(cameraId)
  15 +
13 fun findClosestCaptureFormat( 16 fun findClosestCaptureFormat(
14 cameraId: Int, 17 cameraId: Int,
15 width: Int, 18 width: Int,
16 height: Int 19 height: Int
17 ): Size { 20 ): Size {
18 return CameraEnumerationAndroid.getClosestSupportedSize( 21 return CameraEnumerationAndroid.getClosestSupportedSize(
19 - Camera1Enumerator.getSupportedFormats(cameraId) 22 + getSupportedFormats(cameraId)
20 .map { Size(it.width, it.height) }, 23 .map { Size(it.width, it.height) },
21 width, 24 width,
22 height 25 height
@@ -11,13 +11,20 @@ import android.hardware.camera2.CameraManager @@ -11,13 +11,20 @@ import android.hardware.camera2.CameraManager
11 */ 11 */
12 internal class Camera2Helper { 12 internal class Camera2Helper {
13 companion object { 13 companion object {
  14 +
  15 + fun getSupportedFormats(
  16 + cameraManager: CameraManager,
  17 + cameraId: String?,
  18 + ): List<CameraEnumerationAndroid.CaptureFormat>? =
  19 + Camera2Enumerator.getSupportedFormats(cameraManager, cameraId)
  20 +
14 fun findClosestCaptureFormat( 21 fun findClosestCaptureFormat(
15 cameraManager: CameraManager, 22 cameraManager: CameraManager,
16 cameraId: String?, 23 cameraId: String?,
17 width: Int, 24 width: Int,
18 height: Int 25 height: Int
19 ): Size { 26 ): Size {
20 - val sizes = Camera2Enumerator.getSupportedFormats(cameraManager, cameraId) 27 + val sizes = getSupportedFormats(cameraManager, cameraId)
21 ?.map { Size(it.width, it.height) } 28 ?.map { Size(it.width, it.height) }
22 ?: emptyList() 29 ?: emptyList()
23 return CameraEnumerationAndroid.getClosestSupportedSize(sizes, width, height) 30 return CameraEnumerationAndroid.getClosestSupportedSize(sizes, width, height)