diff --git a/package/android/src/main/java/com/mrousavy/camera/core/CameraError.kt b/package/android/src/main/java/com/mrousavy/camera/core/CameraError.kt index 6b216aa468..fa1b5acfc6 100644 --- a/package/android/src/main/java/com/mrousavy/camera/core/CameraError.kt +++ b/package/android/src/main/java/com/mrousavy/camera/core/CameraError.kt @@ -231,14 +231,15 @@ class DoNotDisturbBugError(cause: Throwable?) : "system", "do-not-disturb-bug", "The Camera Device could not be opened because of a bug in Android 9 (API 28) when do-not-disturb mode is enabled! " + - "Either update your Android version, or disable do-not-disturb.", + "Either update your Android version, or disable do-not-disturb.", cause ) class RecordingWhileFrameProcessingUnavailable : CameraError( "system", "recording-while-frame-processing-unavailable", - "Video Recordings are not possible with a Frame Processor running, because the device is running on API 22 or lower and ImageWriters are not available." + "Video Recordings are not possible with a Frame Processor running, " + + "because the device is running on API 22 or lower and ImageWriters are not available." ) class UnknownCameraError(cause: Throwable?) : CameraError("unknown", "unknown", cause?.message ?: "An unknown camera error occured.", cause) diff --git a/package/android/src/main/java/com/mrousavy/camera/core/FrameProcessorEffect.kt b/package/android/src/main/java/com/mrousavy/camera/core/FrameProcessorEffect.kt index 6431301764..f9eb2f60dc 100644 --- a/package/android/src/main/java/com/mrousavy/camera/core/FrameProcessorEffect.kt +++ b/package/android/src/main/java/com/mrousavy/camera/core/FrameProcessorEffect.kt @@ -11,7 +11,6 @@ import androidx.camera.core.CameraEffect import androidx.camera.core.SurfaceOutput import androidx.camera.core.SurfaceProcessor import androidx.camera.core.SurfaceRequest -import androidx.camera.core.impl.OutputSurface import com.mrousavy.camera.CameraView import com.mrousavy.camera.frameprocessor.Frame import com.mrousavy.camera.types.Orientation @@ -22,17 +21,20 @@ class FrameProcessorEffect( format: PixelFormat = PixelFormat.NATIVE, enableGpuBuffers: Boolean = false, callback: CameraSession.Callback, - targets: Int = VIDEO_CAPTURE, + targets: Int = PREVIEW ) : CameraEffect( targets, - TRANSFORMATION_CAMERA_AND_SURFACE_ROTATION, CameraQueues.videoQueue.executor, FrameProcessorSurfaceProcessor(format, enableGpuBuffers, callback), { error -> callback.onError(error) } ) { // CameraEffect holds the SurfaceProcessor - class FrameProcessorSurfaceProcessor(private val format: PixelFormat, private val enableGpuBuffers: Boolean, private val callback: CameraSession.Callback) : SurfaceProcessor { + class FrameProcessorSurfaceProcessor( + private val format: PixelFormat, + private val enableGpuBuffers: Boolean, + private val callback: CameraSession.Callback + ) : SurfaceProcessor { companion object { private const val TAG = "FrameProcessorEffect" private const val MAX_IMAGES = 3 @@ -50,7 +52,8 @@ class FrameProcessorEffect( if (currentImageReader != null && currentImageReader.width == requestedSize.width && currentImageReader.height == requestedSize.height && - currentImageReader.imageFormat == requestedFormat) { + currentImageReader.imageFormat == requestedFormat + ) { Log.i(TAG, "Current ImageReader matches those requirements, attempting to re-use it...") request.provideSurface(currentImageReader.surface, queue.executor) { result -> onImageReaderSurfaceClosed(currentImageReader, result.resultCode) @@ -69,23 +72,31 @@ class FrameProcessorEffect( } imageReader.setOnImageAvailableListener({ reader -> - try { - val image = reader.acquireLatestImage() ?: return@setOnImageAvailableListener - - val orientation = Orientation.PORTRAIT // TODO: orientation - val isMirrored = false // TODO: isMirrored - val frame = Frame(image, image.timestamp, orientation, isMirrored) - - frame.incrementRefCount() - try { - callback.onFrame(frame) - } finally { - frame.decrementRefCount() - } - } catch (e: Throwable) { - Log.e(TAG, "Failed to process image! ${e.message}", e) - callback.onError(e) - } + try { + val image = reader.acquireLatestImage() ?: return@setOnImageAvailableListener + + val orientation = Orientation.PORTRAIT // TODO: orientation + val isMirrored = false // TODO: isMirrored + val frame = Frame(image, image.timestamp, orientation, isMirrored) + + frame.incrementRefCount() + try { + callback.onFrame(frame) + + val imageWriter = imageWriter + if (imageWriter != null) { + Log.i(TAG, "Forwarding to ImageWriter...") + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { + imageWriter.queueInputImage(image) + } + } + } finally { + frame.decrementRefCount() + } + } catch (e: Throwable) { + Log.e(TAG, "Failed to process image! ${e.message}", e) + callback.onError(e) + } }, CameraQueues.videoQueue.handler) request.provideSurface(imageReader.surface, queue.executor) { result ->