Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import com.pedro.encoder.input.video.Camera2ApiManager
import com.pedro.encoder.input.video.Camera2ApiManager.ImageCallback
import com.pedro.encoder.input.video.CameraCallbacks
import com.pedro.encoder.input.video.CameraHelper
import com.pedro.encoder.input.video.FrameCapturedCallback
import com.pedro.encoder.input.video.facedetector.FaceDetectorCallback

/**
Expand Down Expand Up @@ -167,6 +168,10 @@ class Camera2Source(context: Context): VideoSource() {
return if (isRunning()) camera.enableFaceDetection(callback) else false
}

fun enableFrameCaptureCallback(frameCapturedCallback: FrameCapturedCallback?) {
camera.enableFrameCaptureCallback(frameCapturedCallback)
}

fun disableFaceDetection() {
if (isRunning()) camera.disableFaceDetection()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ class Camera2ApiManager(context: Context) : CameraDevice.StateCallback() {
private var sensorOrientation = 0
private var faceSensorScale: Rect? = null
private var faceDetectorCallback: FaceDetectorCallback? = null
private var frameCapturedCallback: FrameCapturedCallback? = null
private var faceDetectionEnabled = false
private var faceDetectionMode = 0
private var imageReader: ImageReader? = null
Expand Down Expand Up @@ -156,7 +157,11 @@ class Camera2ApiManager(context: Context) : CameraDevice.StateCallback() {
try {
it.setRepeatingRequest(
captureRequest,
if (faceDetectionEnabled) cb else null, cameraHandler
if (faceDetectionEnabled || frameCapturedCallback != null){
cb
} else{
null
}, cameraHandler
)
} catch (e: IllegalStateException) {
reOpenCamera(cameraId)
Expand Down Expand Up @@ -296,7 +301,7 @@ class Camera2ApiManager(context: Context) : CameraDevice.StateCallback() {
try {
cameraCaptureSession.setRepeatingRequest(
builder.build(),
if (faceDetectionEnabled) cb else null, null
if (faceDetectionEnabled || frameCapturedCallback != null) cb else null, null
)
return true
} catch (e: Exception) {
Expand Down Expand Up @@ -630,6 +635,11 @@ class Camera2ApiManager(context: Context) : CameraDevice.StateCallback() {
return true
}


fun enableFrameCaptureCallback(frameCapturedCallback: FrameCapturedCallback?) {
this.frameCapturedCallback = frameCapturedCallback
}

fun disableFaceDetection() {
if (faceDetectionEnabled) {
faceDetectorCallback = null
Expand All @@ -646,9 +656,29 @@ class Camera2ApiManager(context: Context) : CameraDevice.StateCallback() {
}

private val cb: CameraCaptureSession.CaptureCallback = object : CameraCaptureSession.CaptureCallback() {
override fun onCaptureCompleted(session: CameraCaptureSession, request: CaptureRequest, result: TotalCaptureResult) {
override fun onCaptureCompleted(
session: CameraCaptureSession,
request: CaptureRequest,
result: TotalCaptureResult
) {
val faces = result.get(CaptureResult.STATISTICS_FACES) ?: return
faceDetectorCallback?.onGetFaces(mapCamera2Faces(faces), faceSensorScale, sensorOrientation)
faceDetectorCallback?.onGetFaces(
faces = mapCamera2Faces(faces = faces),
scaleSensor = faceSensorScale,
sensorOrientation = sensorOrientation
)
}

override fun onCaptureStarted(
session: CameraCaptureSession,
request: CaptureRequest,
timestamp: Long,
frameNumber: Long
) {
frameCapturedCallback?.onFrameCaptured(
frameNumber = frameNumber,
timestamp = timestamp
)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright (C) 2024 pedroSG94.
*
* 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 com.pedro.encoder.input.video

/*
This method is called when the camera device has started capturing the output image for the request,
at the beginning of image exposure, or when the camera device has started processing an input image
for a reprocess request.
For a regular capture request, this callback is invoked right as the capture of a frame begins,
so it is the most appropriate time for playing a shutter sound, or triggering UI indicators of capture.
The request that is being used for this capture is provided, along with the actual timestamp for
the start of exposure. For a reprocess request, this timestamp will be the input image's start of
exposure which matches the result timestamp field of the TotalCaptureResult that was used to create
the reprocess request. This timestamp matches the timestamps that will be included in the result
timestamp field, and in the buffers sent to each output Surface. These buffer timestamps are
accessible through, for example, Image.getTimestamp() or android.graphics.SurfaceTexture.getTimestamp().
The frame number included is equal to the frame number that will be included in CaptureResult.getFrameNumber.
*/

interface FrameCapturedCallback {
fun onFrameCaptured(frameNumber: Long, timestamp: Long)
}
5 changes: 5 additions & 0 deletions library/src/main/java/com/pedro/library/base/Camera2Base.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import com.pedro.encoder.input.video.CameraCallbacks;
import com.pedro.encoder.input.video.CameraHelper;
import com.pedro.encoder.input.video.CameraOpenException;
import com.pedro.encoder.input.video.FrameCapturedCallback;
import com.pedro.encoder.input.video.facedetector.FaceDetectorCallback;
import com.pedro.encoder.utils.CodecUtil;
import com.pedro.encoder.video.FormatVideoEncoder;
Expand Down Expand Up @@ -165,6 +166,10 @@ public boolean enableFaceDetection(FaceDetectorCallback faceDetectorCallback) {
return cameraManager.enableFaceDetection(faceDetectorCallback);
}

public void enableFrameCaptureCallback(FrameCapturedCallback frameCapturedCallback) {
cameraManager.enableFrameCaptureCallback(frameCapturedCallback);
}

public void disableFaceDetection() {
cameraManager.disableFaceDetection();
}
Expand Down