|
| 1 | +/* |
| 2 | + * Copyright 2025 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.firebase.ai.type |
| 18 | + |
| 19 | +import android.Manifest |
| 20 | +import android.graphics.Bitmap |
| 21 | +import android.graphics.BitmapFactory |
| 22 | +import android.graphics.ImageFormat |
| 23 | +import android.hardware.camera2.CameraCaptureSession |
| 24 | +import android.hardware.camera2.CameraDevice |
| 25 | +import android.hardware.camera2.CameraManager |
| 26 | +import android.media.ImageReader |
| 27 | +import android.os.Handler |
| 28 | +import android.os.Looper |
| 29 | +import androidx.annotation.RequiresPermission |
| 30 | +import java.io.ByteArrayOutputStream |
| 31 | +import kotlin.coroutines.resume |
| 32 | +import kotlin.coroutines.resumeWithException |
| 33 | +import kotlin.math.max |
| 34 | +import kotlinx.coroutines.CoroutineScope |
| 35 | +import kotlinx.coroutines.Dispatchers |
| 36 | +import kotlinx.coroutines.cancel |
| 37 | +import kotlinx.coroutines.channels.awaitClose |
| 38 | +import kotlinx.coroutines.flow.Flow |
| 39 | +import kotlinx.coroutines.flow.callbackFlow |
| 40 | +import kotlinx.coroutines.flow.emptyFlow |
| 41 | +import kotlinx.coroutines.launch |
| 42 | +import kotlinx.coroutines.suspendCancellableCoroutine |
| 43 | + |
| 44 | +/** |
| 45 | + * Helper class for streaming video from the camera. |
| 46 | + * |
| 47 | + * @see VideoHelper.build |
| 48 | + * @see LiveSession.startVideoConversation |
| 49 | + */ |
| 50 | +@PublicPreviewAPI |
| 51 | +internal class VideoHelper( |
| 52 | + private val cameraManager: CameraManager, |
| 53 | +) { |
| 54 | + private var cameraDevice: CameraDevice? = null |
| 55 | + private var imageReader: ImageReader? = null |
| 56 | + private var session: CameraCaptureSession? = null |
| 57 | + private val scope = CoroutineScope(Dispatchers.Default) |
| 58 | + |
| 59 | + private var released: Boolean = false |
| 60 | + |
| 61 | + /** |
| 62 | + * Release the system resources on the camera. |
| 63 | + * |
| 64 | + * Once a [VideoHelper] has been "released", it can _not_ be used again. |
| 65 | + * |
| 66 | + * This method can safely be called multiple times, as it won't do anything if this instance has |
| 67 | + * already been released. |
| 68 | + */ |
| 69 | + fun release() { |
| 70 | + if (released) return |
| 71 | + released = true |
| 72 | + |
| 73 | + session?.close() |
| 74 | + imageReader?.close() |
| 75 | + cameraDevice?.close() |
| 76 | + scope.cancel() |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Start perpetually streaming the camera, and return the bytes read in a flow. |
| 81 | + * |
| 82 | + * Returns an empty flow if this [VideoHelper] has been [released][release]. |
| 83 | + */ |
| 84 | + @RequiresPermission(Manifest.permission.CAMERA) |
| 85 | + suspend fun start(cameraId: String): Flow<ByteArray> { |
| 86 | + if (released) return emptyFlow() |
| 87 | + |
| 88 | + cameraDevice = openCamera(cameraId) |
| 89 | + val cameraDevice = cameraDevice ?: return emptyFlow() |
| 90 | + |
| 91 | + val characteristics = cameraManager.getCameraCharacteristics(cameraId) |
| 92 | + val streamConfigurationMap = |
| 93 | + characteristics.get( |
| 94 | + android.hardware.camera2.CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP |
| 95 | + ) |
| 96 | + val outputSizes = streamConfigurationMap?.getOutputSizes(ImageFormat.JPEG) |
| 97 | + val size = outputSizes?.maxByOrNull { it.width * it.height } ?: return emptyFlow() |
| 98 | + |
| 99 | + imageReader = ImageReader.newInstance(size.width, size.height, ImageFormat.JPEG, 1) |
| 100 | + val imageReader = imageReader ?: return emptyFlow() |
| 101 | + |
| 102 | + session = createCaptureSession(cameraDevice, imageReader) |
| 103 | + val session = session ?: return emptyFlow() |
| 104 | + |
| 105 | + val captureRequest = |
| 106 | + session.device.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW).apply { |
| 107 | + addTarget(imageReader.surface) |
| 108 | + } |
| 109 | + session.setRepeatingRequest(captureRequest.build(), null, null) |
| 110 | + |
| 111 | + return callbackFlow { |
| 112 | + val listener = |
| 113 | + ImageReader.OnImageAvailableListener { reader -> |
| 114 | + val image = reader.acquireLatestImage() |
| 115 | + if (image != null) { |
| 116 | + scope.launch { |
| 117 | + val buffer = image.planes[0].buffer |
| 118 | + val bytes = ByteArray(buffer.remaining()) |
| 119 | + buffer.get(bytes) |
| 120 | + image.close() |
| 121 | + |
| 122 | + val scaledBytes = scaleAndCompressImage(bytes) |
| 123 | + trySend(scaledBytes) |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + imageReader.setOnImageAvailableListener(listener, null) |
| 128 | + |
| 129 | + awaitClose { imageReader.setOnImageAvailableListener(null, null) } |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + private fun scaleAndCompressImage(bytes: ByteArray): ByteArray { |
| 134 | + val options = BitmapFactory.Options().apply { inJustDecodeBounds = true } |
| 135 | + BitmapFactory.decodeByteArray(bytes, 0, bytes.size, options) |
| 136 | + |
| 137 | + val width = options.outWidth |
| 138 | + val height = options.outHeight |
| 139 | + val largestDimension = max(width, height) |
| 140 | + |
| 141 | + var inSampleSize = 1 |
| 142 | + if (largestDimension > 2048) { |
| 143 | + val halfLargestDimension = largestDimension / 2 |
| 144 | + while ((halfLargestDimension / inSampleSize) >= 2048) { |
| 145 | + inSampleSize *= 2 |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + options.inSampleSize = inSampleSize |
| 150 | + options.inJustDecodeBounds = false |
| 151 | + var bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.size, options) |
| 152 | + |
| 153 | + val scaledWidth = bitmap.width |
| 154 | + val scaledHeight = bitmap.height |
| 155 | + val scaledLargestDimension = max(scaledWidth, scaledHeight) |
| 156 | + if (scaledLargestDimension > 2048) { |
| 157 | + val scaleFactor = 2048.0f / scaledLargestDimension |
| 158 | + val newWidth = (scaledWidth * scaleFactor).toInt() |
| 159 | + val newHeight = (scaledHeight * scaleFactor).toInt() |
| 160 | + bitmap = Bitmap.createScaledBitmap(bitmap, newWidth, newHeight, true) |
| 161 | + } |
| 162 | + |
| 163 | + val outputStream = ByteArrayOutputStream() |
| 164 | + bitmap.compress(Bitmap.CompressFormat.JPEG, 80, outputStream) |
| 165 | + return outputStream.toByteArray() |
| 166 | + } |
| 167 | + |
| 168 | + @RequiresPermission(Manifest.permission.CAMERA) |
| 169 | + private suspend fun openCamera(cameraId: String): CameraDevice = |
| 170 | + suspendCancellableCoroutine { cont -> |
| 171 | + val handler = Handler(Looper.getMainLooper()) |
| 172 | + cameraManager.openCamera( |
| 173 | + cameraId, |
| 174 | + object : CameraDevice.StateCallback() { |
| 175 | + override fun onOpened(camera: CameraDevice) { |
| 176 | + cont.resume(camera) |
| 177 | + } |
| 178 | + |
| 179 | + override fun onDisconnected(camera: CameraDevice) { |
| 180 | + camera.close() |
| 181 | + } |
| 182 | + |
| 183 | + override fun onError(camera: CameraDevice, error: Int) { |
| 184 | + camera.close() |
| 185 | + cont.resumeWithException(RuntimeException("Failed to open camera. Error: $error")) |
| 186 | + } |
| 187 | + }, |
| 188 | + handler |
| 189 | + ) |
| 190 | + } |
| 191 | + |
| 192 | + private suspend fun createCaptureSession( |
| 193 | + cameraDevice: CameraDevice, |
| 194 | + imageReader: ImageReader |
| 195 | + ): CameraCaptureSession = suspendCancellableCoroutine { cont -> |
| 196 | + cameraDevice.createCaptureSession( |
| 197 | + listOf(imageReader.surface), |
| 198 | + object : CameraCaptureSession.StateCallback() { |
| 199 | + override fun onConfigured(session: CameraCaptureSession) { |
| 200 | + cont.resume(session) |
| 201 | + } |
| 202 | + |
| 203 | + override fun onConfigureFailed(session: CameraCaptureSession) { |
| 204 | + cont.resumeWithException(RuntimeException("Failed to create capture session.")) |
| 205 | + } |
| 206 | + }, |
| 207 | + null |
| 208 | + ) |
| 209 | + } |
| 210 | + |
| 211 | + companion object { |
| 212 | + private val TAG = VideoHelper::class.java.simpleName |
| 213 | + |
| 214 | + /** |
| 215 | + * Creates an instance of [VideoHelper] with the camera manager initialized. |
| 216 | + * |
| 217 | + * A separate build method is necessary so that we can properly propagate the required manifest |
| 218 | + * permission, and throw exceptions when needed. |
| 219 | + */ |
| 220 | + @RequiresPermission(Manifest.permission.CAMERA) |
| 221 | + fun build(cameraManager: CameraManager): VideoHelper { |
| 222 | + return VideoHelper(cameraManager) |
| 223 | + } |
| 224 | + } |
| 225 | +} |
0 commit comments