Skip to content

Commit 45e6774

Browse files
committed
add setRequiredResolution method and sanity check on disable glresources
1 parent e6ee9b9 commit 45e6774

4 files changed

Lines changed: 29 additions & 4 deletions

File tree

encoder/src/main/java/com/pedro/encoder/input/sources/video/Camera2Source.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,4 +260,13 @@ class Camera2Source(context: Context): VideoSource() {
260260
fun getMaxSupportedFps(size: Size?, facing: CameraHelper.Facing = getCameraFacing()): Int {
261261
return camera.getSupportedFps(size, facing).maxOfOrNull { it.upper } ?: 30
262262
}
263+
264+
/**
265+
* Set the required resolution for the camera.
266+
* Must be called before prepareVideo or changeVideoSource. Otherwise it will be ignored.
267+
*/
268+
fun setRequiredResolution(size: Size?) {
269+
size?.let { checkResolutionSupported(it.width, it.height) }
270+
camera.setRequiredResolution(size)
271+
}
263272
}

encoder/src/main/java/com/pedro/encoder/input/video/Camera2ApiManager.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ class Camera2ApiManager(context: Context) : CameraDevice.StateCallback() {
100100
private var fps = 30
101101
private val semaphore = Semaphore(0)
102102
private var cameraCallbacks: CameraCallbacks? = null
103+
private var requiredSize: Size? = null
103104

104105
interface ImageCallback {
105106
fun onImageAvailable(image: Image)
@@ -118,7 +119,7 @@ class Camera2ApiManager(context: Context) : CameraDevice.StateCallback() {
118119
}
119120

120121
fun prepareCamera(surfaceTexture: SurfaceTexture, width: Int, height: Int, fps: Int) {
121-
val optimalResolution = getOptimalResolution(Size(width, height), getCameraResolutions(facing))
122+
val optimalResolution = requiredSize ?: getOptimalResolution(Size(width, height), getCameraResolutions(facing))
122123
Log.i(TAG, "optimal resolution set to: " + optimalResolution.width + "x" + optimalResolution.height)
123124
surfaceTexture.setDefaultBufferSize(optimalResolution.width, optimalResolution.height)
124125
this.surfaceEncoder = Surface(surfaceTexture)
@@ -220,6 +221,10 @@ class Camera2ApiManager(context: Context) : CameraDevice.StateCallback() {
220221
return characteristics.secureGet(CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL) ?: -1
221222
}
222223

224+
fun setRequiredResolution(size: Size?) {
225+
requiredSize = size
226+
}
227+
223228
fun openCameraBack() {
224229
openCameraFacing(Facing.BACK)
225230
}

encoder/src/main/java/com/pedro/encoder/utils/gl/GlUtil.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,9 @@ private static Bitmap flipVerticalBitmap(Bitmap bitmap, int width, int height) {
163163
}
164164

165165
public static void disableResources(int... vertex) {
166-
for (int v: vertex) GLES20.glDisableVertexAttribArray(v);
166+
for (int v: vertex) {
167+
if (v >= 0 && v < GLES20.GL_MAX_VERTEX_ATTRIBS) GLES20.glDisableVertexAttribArray(v);
168+
}
167169
GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, 0);
168170
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, 0);
169171
GLES20.glUseProgram(0);

extra-sources/src/main/java/com/pedro/extrasources/CameraXSource.kt

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,13 @@ class CameraXSource(
6565
private var autoExposureEnabled = false
6666
private var autoWhiteBalanceEnabled = false
6767
private var fingerSpacing = 0f
68+
private var requiredSize: Size? = null
6869

6970
override val lifecycle: Lifecycle = lifecycleRegistry
7071

7172
override fun create(width: Int, height: Int, fps: Int, rotation: Int): Boolean {
7273
val facing = if (facing == CameraSelector.LENS_FACING_BACK) CameraHelper.Facing.BACK else CameraHelper.Facing.FRONT
73-
val optimalResolution = getOptimalResolution(Size(width, height), getCameraResolutions(facing).toTypedArray())
74+
val optimalResolution = requiredSize ?: getOptimalResolution(Size(width, height), getCameraResolutions(facing).toTypedArray())
7475
preview = Preview.Builder()
7576
.setTargetFrameRate(Range(fps, fps))
7677
.setResolutionSelector(
@@ -89,7 +90,7 @@ class CameraXSource(
8990

9091
override fun start(surfaceTexture: SurfaceTexture) {
9192
val facing = if (facing == CameraSelector.LENS_FACING_BACK) CameraHelper.Facing.BACK else CameraHelper.Facing.FRONT
92-
val optimalResolution = getOptimalResolution(Size(width, height), getCameraResolutions(facing).toTypedArray())
93+
val optimalResolution = requiredSize ?: getOptimalResolution(Size(width, height), getCameraResolutions(facing).toTypedArray())
9394
surfaceTexture.setDefaultBufferSize(optimalResolution.width, optimalResolution.height)
9495
this.surfaceTexture = surfaceTexture
9596
lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_START)
@@ -343,4 +344,12 @@ class CameraXSource(
343344
if (autoWhiteBalanceEnabled) flags = flags or FocusMeteringAction.FLAG_AWB
344345
return flags
345346
}
347+
348+
/**
349+
* Set the required resolution for the camera.
350+
* Must be called before prepareVideo or changeVideoSource. Otherwise it will be ignored.
351+
*/
352+
fun setRequiredResolution(size: Size?) {
353+
requiredSize = size
354+
}
346355
}

0 commit comments

Comments
 (0)