Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2023 The Android Open Source Project |
| 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 | // #define LOG_NDEBUG 0 |
| 18 | #define LOG_TAG "VirtualCameraSession" |
| 19 | #include "VirtualCameraSession.h" |
| 20 | |
Jan Sebechlebsky | 39129f8 | 2024-01-19 16:42:11 +0100 | [diff] [blame] | 21 | #include <algorithm> |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 22 | #include <atomic> |
| 23 | #include <chrono> |
Jan Sebechlebsky | 4c9bb1e | 2024-02-28 16:32:39 +0100 | [diff] [blame] | 24 | #include <cmath> |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 25 | #include <cstddef> |
| 26 | #include <cstdint> |
| 27 | #include <cstring> |
| 28 | #include <map> |
| 29 | #include <memory> |
| 30 | #include <mutex> |
Jan Sebechlebsky | c3e1a63 | 2024-02-06 14:19:05 +0100 | [diff] [blame] | 31 | #include <numeric> |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 32 | #include <optional> |
| 33 | #include <tuple> |
| 34 | #include <unordered_set> |
| 35 | #include <utility> |
| 36 | #include <vector> |
| 37 | |
| 38 | #include "CameraMetadata.h" |
| 39 | #include "EGL/egl.h" |
Jan Sebechlebsky | 3b478c4 | 2023-11-23 13:15:56 +0100 | [diff] [blame] | 40 | #include "VirtualCameraDevice.h" |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 41 | #include "VirtualCameraRenderThread.h" |
| 42 | #include "VirtualCameraStream.h" |
Jan Sebechlebsky | 4c9bb1e | 2024-02-28 16:32:39 +0100 | [diff] [blame] | 43 | #include "aidl/android/companion/virtualcamera/SupportedStreamConfiguration.h" |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 44 | #include "aidl/android/hardware/camera/common/Status.h" |
| 45 | #include "aidl/android/hardware/camera/device/BufferCache.h" |
| 46 | #include "aidl/android/hardware/camera/device/BufferStatus.h" |
Jan Sebechlebsky | 4ce3208 | 2024-02-14 16:02:11 +0100 | [diff] [blame] | 47 | #include "aidl/android/hardware/camera/device/CameraMetadata.h" |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 48 | #include "aidl/android/hardware/camera/device/CaptureRequest.h" |
| 49 | #include "aidl/android/hardware/camera/device/HalStream.h" |
| 50 | #include "aidl/android/hardware/camera/device/NotifyMsg.h" |
Jan Sebechlebsky | c3e1a63 | 2024-02-06 14:19:05 +0100 | [diff] [blame] | 51 | #include "aidl/android/hardware/camera/device/RequestTemplate.h" |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 52 | #include "aidl/android/hardware/camera/device/ShutterMsg.h" |
Jan Sebechlebsky | 4c9bb1e | 2024-02-28 16:32:39 +0100 | [diff] [blame] | 53 | #include "aidl/android/hardware/camera/device/Stream.h" |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 54 | #include "aidl/android/hardware/camera/device/StreamBuffer.h" |
| 55 | #include "aidl/android/hardware/camera/device/StreamConfiguration.h" |
| 56 | #include "aidl/android/hardware/camera/device/StreamRotation.h" |
| 57 | #include "aidl/android/hardware/graphics/common/BufferUsage.h" |
| 58 | #include "aidl/android/hardware/graphics/common/PixelFormat.h" |
| 59 | #include "android/hardware_buffer.h" |
| 60 | #include "android/native_window_aidl.h" |
| 61 | #include "fmq/AidlMessageQueue.h" |
| 62 | #include "system/camera_metadata.h" |
| 63 | #include "ui/GraphicBuffer.h" |
| 64 | #include "util/EglDisplayContext.h" |
| 65 | #include "util/EglFramebuffer.h" |
| 66 | #include "util/EglProgram.h" |
| 67 | #include "util/JpegUtil.h" |
Jan Sebechlebsky | 4ce3208 | 2024-02-14 16:02:11 +0100 | [diff] [blame] | 68 | #include "util/MetadataUtil.h" |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 69 | #include "util/Util.h" |
| 70 | |
| 71 | namespace android { |
| 72 | namespace companion { |
| 73 | namespace virtualcamera { |
| 74 | |
| 75 | using ::aidl::android::companion::virtualcamera::Format; |
| 76 | using ::aidl::android::companion::virtualcamera::IVirtualCameraCallback; |
Jan Sebechlebsky | c3e1a63 | 2024-02-06 14:19:05 +0100 | [diff] [blame] | 77 | using ::aidl::android::companion::virtualcamera::SupportedStreamConfiguration; |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 78 | using ::aidl::android::hardware::camera::common::Status; |
| 79 | using ::aidl::android::hardware::camera::device::BufferCache; |
| 80 | using ::aidl::android::hardware::camera::device::CameraMetadata; |
| 81 | using ::aidl::android::hardware::camera::device::CameraOfflineSessionInfo; |
| 82 | using ::aidl::android::hardware::camera::device::CaptureRequest; |
| 83 | using ::aidl::android::hardware::camera::device::HalStream; |
| 84 | using ::aidl::android::hardware::camera::device::ICameraDeviceCallback; |
| 85 | using ::aidl::android::hardware::camera::device::ICameraOfflineSession; |
| 86 | using ::aidl::android::hardware::camera::device::RequestTemplate; |
| 87 | using ::aidl::android::hardware::camera::device::Stream; |
| 88 | using ::aidl::android::hardware::camera::device::StreamBuffer; |
| 89 | using ::aidl::android::hardware::camera::device::StreamConfiguration; |
| 90 | using ::aidl::android::hardware::camera::device::StreamRotation; |
| 91 | using ::aidl::android::hardware::common::fmq::MQDescriptor; |
| 92 | using ::aidl::android::hardware::common::fmq::SynchronizedReadWrite; |
| 93 | using ::aidl::android::hardware::graphics::common::BufferUsage; |
| 94 | using ::aidl::android::hardware::graphics::common::PixelFormat; |
| 95 | using ::android::base::unique_fd; |
| 96 | |
| 97 | namespace { |
| 98 | |
| 99 | using metadata_ptr = |
| 100 | std::unique_ptr<camera_metadata_t, void (*)(camera_metadata_t*)>; |
| 101 | |
| 102 | using namespace std::chrono_literals; |
| 103 | |
| 104 | // Size of request/result metadata fast message queue. |
| 105 | // Setting to 0 to always disables FMQ. |
Jan Sebechlebsky | 4ce3208 | 2024-02-14 16:02:11 +0100 | [diff] [blame] | 106 | constexpr size_t kMetadataMsgQueueSize = 0; |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 107 | |
| 108 | // Maximum number of buffers to use per single stream. |
Jan Sebechlebsky | 4ce3208 | 2024-02-14 16:02:11 +0100 | [diff] [blame] | 109 | constexpr size_t kMaxStreamBuffers = 2; |
| 110 | |
Jan Sebechlebsky | 4ce3208 | 2024-02-14 16:02:11 +0100 | [diff] [blame] | 111 | // Thumbnail size (0,0) correspods to disabling thumbnail. |
| 112 | const Resolution kDefaultJpegThumbnailSize(0, 0); |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 113 | |
Jan Sebechlebsky | c3e1a63 | 2024-02-06 14:19:05 +0100 | [diff] [blame] | 114 | camera_metadata_enum_android_control_capture_intent_t requestTemplateToIntent( |
| 115 | const RequestTemplate type) { |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 116 | switch (type) { |
| 117 | case RequestTemplate::PREVIEW: |
Jan Sebechlebsky | c3e1a63 | 2024-02-06 14:19:05 +0100 | [diff] [blame] | 118 | return ANDROID_CONTROL_CAPTURE_INTENT_PREVIEW; |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 119 | case RequestTemplate::STILL_CAPTURE: |
Jan Sebechlebsky | c3e1a63 | 2024-02-06 14:19:05 +0100 | [diff] [blame] | 120 | return ANDROID_CONTROL_CAPTURE_INTENT_STILL_CAPTURE; |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 121 | case RequestTemplate::VIDEO_RECORD: |
Jan Sebechlebsky | c3e1a63 | 2024-02-06 14:19:05 +0100 | [diff] [blame] | 122 | return ANDROID_CONTROL_CAPTURE_INTENT_VIDEO_RECORD; |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 123 | case RequestTemplate::VIDEO_SNAPSHOT: |
Jan Sebechlebsky | c3e1a63 | 2024-02-06 14:19:05 +0100 | [diff] [blame] | 124 | return ANDROID_CONTROL_CAPTURE_INTENT_VIDEO_SNAPSHOT; |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 125 | default: |
Jan Sebechlebsky | c3e1a63 | 2024-02-06 14:19:05 +0100 | [diff] [blame] | 126 | // Return PREVIEW by default |
| 127 | return ANDROID_CONTROL_CAPTURE_INTENT_PREVIEW; |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 128 | } |
Jan Sebechlebsky | c3e1a63 | 2024-02-06 14:19:05 +0100 | [diff] [blame] | 129 | } |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 130 | |
Jan Sebechlebsky | c3e1a63 | 2024-02-06 14:19:05 +0100 | [diff] [blame] | 131 | int getMaxFps(const std::vector<SupportedStreamConfiguration>& configs) { |
| 132 | return std::transform_reduce( |
| 133 | configs.begin(), configs.end(), 0, |
| 134 | [](const int a, const int b) { return std::max(a, b); }, |
| 135 | [](const SupportedStreamConfiguration& config) { return config.maxFps; }); |
| 136 | } |
| 137 | |
| 138 | CameraMetadata createDefaultRequestSettings( |
| 139 | const RequestTemplate type, |
| 140 | const std::vector<SupportedStreamConfiguration>& inputConfigs) { |
| 141 | int maxFps = getMaxFps(inputConfigs); |
| 142 | auto metadata = |
| 143 | MetadataBuilder() |
Jan Sebechlebsky | 4be2bd0 | 2024-02-26 18:35:18 +0100 | [diff] [blame] | 144 | .setAberrationCorrectionMode( |
| 145 | ANDROID_COLOR_CORRECTION_ABERRATION_MODE_OFF) |
Jan Sebechlebsky | c3e1a63 | 2024-02-06 14:19:05 +0100 | [diff] [blame] | 146 | .setControlCaptureIntent(requestTemplateToIntent(type)) |
| 147 | .setControlMode(ANDROID_CONTROL_MODE_AUTO) |
| 148 | .setControlAeMode(ANDROID_CONTROL_AE_MODE_ON) |
| 149 | .setControlAeExposureCompensation(0) |
Vadim Caen | 11dfd93 | 2024-03-05 09:57:20 +0100 | [diff] [blame] | 150 | .setControlAeTargetFpsRange(FpsRange{maxFps, maxFps}) |
Jan Sebechlebsky | c3e1a63 | 2024-02-06 14:19:05 +0100 | [diff] [blame] | 151 | .setControlAeAntibandingMode(ANDROID_CONTROL_AE_ANTIBANDING_MODE_AUTO) |
| 152 | .setControlAePrecaptureTrigger( |
| 153 | ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE) |
| 154 | .setControlAfTrigger(ANDROID_CONTROL_AF_TRIGGER_IDLE) |
Jan Sebechlebsky | bb01c1d | 2024-02-12 11:41:37 +0100 | [diff] [blame] | 155 | .setControlAfMode(ANDROID_CONTROL_AF_MODE_OFF) |
Jan Sebechlebsky | c3e1a63 | 2024-02-06 14:19:05 +0100 | [diff] [blame] | 156 | .setControlAwbMode(ANDROID_CONTROL_AWB_MODE_AUTO) |
Jan Sebechlebsky | bb01c1d | 2024-02-12 11:41:37 +0100 | [diff] [blame] | 157 | .setControlEffectMode(ANDROID_CONTROL_EFFECT_MODE_OFF) |
Jan Sebechlebsky | c3e1a63 | 2024-02-06 14:19:05 +0100 | [diff] [blame] | 158 | .setFaceDetectMode(ANDROID_STATISTICS_FACE_DETECT_MODE_OFF) |
| 159 | .setFlashMode(ANDROID_FLASH_MODE_OFF) |
Jan Sebechlebsky | bb01c1d | 2024-02-12 11:41:37 +0100 | [diff] [blame] | 160 | .setFlashState(ANDROID_FLASH_STATE_UNAVAILABLE) |
Jan Sebechlebsky | 4ce3208 | 2024-02-14 16:02:11 +0100 | [diff] [blame] | 161 | .setJpegQuality(VirtualCameraDevice::kDefaultJpegQuality) |
| 162 | .setJpegThumbnailQuality(VirtualCameraDevice::kDefaultJpegQuality) |
| 163 | .setJpegThumbnailSize(0, 0) |
Jan Sebechlebsky | 4be2bd0 | 2024-02-26 18:35:18 +0100 | [diff] [blame] | 164 | .setNoiseReductionMode(ANDROID_NOISE_REDUCTION_MODE_OFF) |
Jan Sebechlebsky | c3e1a63 | 2024-02-06 14:19:05 +0100 | [diff] [blame] | 165 | .build(); |
| 166 | if (metadata == nullptr) { |
| 167 | ALOGE("%s: Failed to construct metadata for default request type %s", |
| 168 | __func__, toString(type).c_str()); |
| 169 | return CameraMetadata(); |
| 170 | } else { |
| 171 | ALOGV("%s: Successfully created metadata for request type %s", __func__, |
| 172 | toString(type).c_str()); |
| 173 | } |
| 174 | return *metadata; |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | HalStream getHalStream(const Stream& stream) { |
| 178 | HalStream halStream; |
| 179 | halStream.id = stream.id; |
| 180 | halStream.physicalCameraId = stream.physicalCameraId; |
| 181 | halStream.maxBuffers = kMaxStreamBuffers; |
| 182 | |
| 183 | if (stream.format == PixelFormat::IMPLEMENTATION_DEFINED) { |
| 184 | // If format is implementation defined we need it to override |
| 185 | // it with actual format. |
| 186 | // TODO(b/301023410) Override with the format based on the |
| 187 | // camera configuration, once we support more formats. |
| 188 | halStream.overrideFormat = PixelFormat::YCBCR_420_888; |
| 189 | } else { |
| 190 | halStream.overrideFormat = stream.format; |
| 191 | } |
| 192 | halStream.overrideDataSpace = stream.dataSpace; |
| 193 | |
| 194 | halStream.producerUsage = BufferUsage::GPU_RENDER_TARGET; |
| 195 | halStream.supportOffline = false; |
| 196 | return halStream; |
| 197 | } |
| 198 | |
Jan Sebechlebsky | 39129f8 | 2024-01-19 16:42:11 +0100 | [diff] [blame] | 199 | Stream getHighestResolutionStream(const std::vector<Stream>& streams) { |
| 200 | return *(std::max_element(streams.begin(), streams.end(), |
| 201 | [](const Stream& a, const Stream& b) { |
| 202 | return a.width * a.height < b.width * b.height; |
| 203 | })); |
| 204 | } |
| 205 | |
Jan Sebechlebsky | 4c9bb1e | 2024-02-28 16:32:39 +0100 | [diff] [blame] | 206 | Resolution resolutionFromStream(const Stream& stream) { |
| 207 | return Resolution(stream.width, stream.height); |
| 208 | } |
| 209 | |
| 210 | Resolution resolutionFromInputConfig( |
| 211 | const SupportedStreamConfiguration& inputConfig) { |
| 212 | return Resolution(inputConfig.width, inputConfig.height); |
| 213 | } |
| 214 | |
| 215 | std::optional<SupportedStreamConfiguration> pickInputConfigurationForStreams( |
| 216 | const std::vector<Stream>& requestedStreams, |
| 217 | const std::vector<SupportedStreamConfiguration>& supportedInputConfigs) { |
| 218 | Stream maxResolutionStream = getHighestResolutionStream(requestedStreams); |
| 219 | Resolution maxResolution = resolutionFromStream(maxResolutionStream); |
| 220 | |
| 221 | // Find best fitting stream to satisfy all requested streams: |
| 222 | // Best fitting => same or higher resolution as input with lowest pixel count |
| 223 | // difference and same aspect ratio. |
| 224 | auto isBetterInputConfig = [maxResolution]( |
| 225 | const SupportedStreamConfiguration& configA, |
| 226 | const SupportedStreamConfiguration& configB) { |
| 227 | int maxResPixelCount = maxResolution.width * maxResolution.height; |
| 228 | int pixelCountDiffA = |
| 229 | std::abs((configA.width * configA.height) - maxResPixelCount); |
| 230 | int pixelCountDiffB = |
| 231 | std::abs((configB.width * configB.height) - maxResPixelCount); |
| 232 | |
| 233 | return pixelCountDiffA < pixelCountDiffB; |
| 234 | }; |
| 235 | |
| 236 | std::optional<SupportedStreamConfiguration> bestConfig; |
| 237 | for (const SupportedStreamConfiguration& inputConfig : supportedInputConfigs) { |
| 238 | Resolution inputConfigResolution = resolutionFromInputConfig(inputConfig); |
| 239 | if (inputConfigResolution < maxResolution || |
| 240 | !isApproximatellySameAspectRatio(inputConfigResolution, maxResolution)) { |
| 241 | // We don't want to upscale from lower resolution, or use different aspect |
| 242 | // ratio, skip. |
| 243 | continue; |
| 244 | } |
| 245 | |
| 246 | if (!bestConfig.has_value() || |
| 247 | isBetterInputConfig(inputConfig, bestConfig.value())) { |
| 248 | bestConfig = inputConfig; |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | return bestConfig; |
| 253 | } |
| 254 | |
Jan Sebechlebsky | 4ce3208 | 2024-02-14 16:02:11 +0100 | [diff] [blame] | 255 | RequestSettings createSettingsFromMetadata(const CameraMetadata& metadata) { |
| 256 | return RequestSettings{ |
| 257 | .jpegQuality = getJpegQuality(metadata).value_or( |
| 258 | VirtualCameraDevice::kDefaultJpegQuality), |
Vadim Caen | c0aff13 | 2024-03-12 17:20:07 +0100 | [diff] [blame] | 259 | .jpegOrientation = getJpegOrientation(metadata), |
Jan Sebechlebsky | 4ce3208 | 2024-02-14 16:02:11 +0100 | [diff] [blame] | 260 | .thumbnailResolution = |
| 261 | getJpegThumbnailSize(metadata).value_or(Resolution(0, 0)), |
| 262 | .thumbnailJpegQuality = getJpegThumbnailQuality(metadata).value_or( |
Vadim Caen | 11dfd93 | 2024-03-05 09:57:20 +0100 | [diff] [blame] | 263 | VirtualCameraDevice::kDefaultJpegQuality), |
| 264 | .fpsRange = getFpsRange(metadata), |
| 265 | .captureIntent = getCaptureIntent(metadata).value_or( |
Vadim Caen | c0aff13 | 2024-03-12 17:20:07 +0100 | [diff] [blame] | 266 | ANDROID_CONTROL_CAPTURE_INTENT_PREVIEW), |
Vadim Caen | 6a43beb | 2024-04-12 15:06:42 +0200 | [diff] [blame] | 267 | .gpsCoordinates = getGpsCoordinates(metadata), |
| 268 | .aePrecaptureTrigger = getPrecaptureTrigger(metadata)}; |
Jan Sebechlebsky | 4ce3208 | 2024-02-14 16:02:11 +0100 | [diff] [blame] | 269 | } |
| 270 | |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 271 | } // namespace |
| 272 | |
| 273 | VirtualCameraSession::VirtualCameraSession( |
Jan Sebechlebsky | 0bb5e09 | 2023-12-08 16:17:54 +0100 | [diff] [blame] | 274 | std::shared_ptr<VirtualCameraDevice> cameraDevice, |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 275 | std::shared_ptr<ICameraDeviceCallback> cameraDeviceCallback, |
| 276 | std::shared_ptr<IVirtualCameraCallback> virtualCameraClientCallback) |
Jan Sebechlebsky | 3b478c4 | 2023-11-23 13:15:56 +0100 | [diff] [blame] | 277 | : mCameraDevice(cameraDevice), |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 278 | mCameraDeviceCallback(cameraDeviceCallback), |
| 279 | mVirtualCameraClientCallback(virtualCameraClientCallback) { |
| 280 | mRequestMetadataQueue = std::make_unique<RequestMetadataQueue>( |
| 281 | kMetadataMsgQueueSize, false /* non blocking */); |
| 282 | if (!mRequestMetadataQueue->isValid()) { |
| 283 | ALOGE("%s: invalid request fmq", __func__); |
| 284 | } |
| 285 | |
| 286 | mResultMetadataQueue = std::make_shared<ResultMetadataQueue>( |
| 287 | kMetadataMsgQueueSize, false /* non blocking */); |
| 288 | if (!mResultMetadataQueue->isValid()) { |
| 289 | ALOGE("%s: invalid result fmq", __func__); |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | ndk::ScopedAStatus VirtualCameraSession::close() { |
| 294 | ALOGV("%s", __func__); |
| 295 | |
| 296 | if (mVirtualCameraClientCallback != nullptr) { |
| 297 | mVirtualCameraClientCallback->onStreamClosed(/*streamId=*/0); |
| 298 | } |
| 299 | |
Jan Sebechlebsky | b0d8cab | 2023-11-28 10:55:04 +0100 | [diff] [blame] | 300 | { |
| 301 | std::lock_guard<std::mutex> lock(mLock); |
| 302 | if (mRenderThread != nullptr) { |
| 303 | mRenderThread->stop(); |
| 304 | mRenderThread = nullptr; |
| 305 | } |
| 306 | } |
| 307 | |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 308 | mSessionContext.closeAllStreams(); |
| 309 | return ndk::ScopedAStatus::ok(); |
| 310 | } |
| 311 | |
| 312 | ndk::ScopedAStatus VirtualCameraSession::configureStreams( |
| 313 | const StreamConfiguration& in_requestedConfiguration, |
| 314 | std::vector<HalStream>* _aidl_return) { |
| 315 | ALOGV("%s: requestedConfiguration: %s", __func__, |
| 316 | in_requestedConfiguration.toString().c_str()); |
| 317 | |
| 318 | if (_aidl_return == nullptr) { |
| 319 | return cameraStatus(Status::ILLEGAL_ARGUMENT); |
| 320 | } |
| 321 | |
Jan Sebechlebsky | 0bb5e09 | 2023-12-08 16:17:54 +0100 | [diff] [blame] | 322 | std::shared_ptr<VirtualCameraDevice> virtualCamera = mCameraDevice.lock(); |
| 323 | if (virtualCamera == nullptr) { |
| 324 | ALOGW("%s: configure called on already unregistered camera", __func__); |
| 325 | return cameraStatus(Status::CAMERA_DISCONNECTED); |
| 326 | } |
| 327 | |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 328 | mSessionContext.removeStreamsNotInStreamConfiguration( |
| 329 | in_requestedConfiguration); |
| 330 | |
| 331 | auto& streams = in_requestedConfiguration.streams; |
| 332 | auto& halStreams = *_aidl_return; |
| 333 | halStreams.clear(); |
| 334 | halStreams.resize(in_requestedConfiguration.streams.size()); |
| 335 | |
Jan Sebechlebsky | 0bb5e09 | 2023-12-08 16:17:54 +0100 | [diff] [blame] | 336 | if (!virtualCamera->isStreamCombinationSupported(in_requestedConfiguration)) { |
Jan Sebechlebsky | 3b478c4 | 2023-11-23 13:15:56 +0100 | [diff] [blame] | 337 | ALOGE("%s: Requested stream configuration is not supported", __func__); |
| 338 | return cameraStatus(Status::ILLEGAL_ARGUMENT); |
| 339 | } |
| 340 | |
Jan Sebechlebsky | 4c9bb1e | 2024-02-28 16:32:39 +0100 | [diff] [blame] | 341 | sp<Surface> inputSurface = nullptr; |
| 342 | std::optional<SupportedStreamConfiguration> inputConfig; |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 343 | { |
| 344 | std::lock_guard<std::mutex> lock(mLock); |
| 345 | for (int i = 0; i < in_requestedConfiguration.streams.size(); ++i) { |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 346 | halStreams[i] = getHalStream(streams[i]); |
| 347 | if (mSessionContext.initializeStream(streams[i])) { |
| 348 | ALOGV("Configured new stream: %s", streams[i].toString().c_str()); |
| 349 | } |
| 350 | } |
| 351 | |
Jan Sebechlebsky | 4c9bb1e | 2024-02-28 16:32:39 +0100 | [diff] [blame] | 352 | inputConfig = pickInputConfigurationForStreams( |
| 353 | streams, virtualCamera->getInputConfigs()); |
| 354 | if (!inputConfig.has_value()) { |
| 355 | ALOGE( |
| 356 | "%s: Failed to pick any input configuration for stream configuration " |
| 357 | "request: %s", |
| 358 | __func__, in_requestedConfiguration.toString().c_str()); |
| 359 | return cameraStatus(Status::ILLEGAL_ARGUMENT); |
| 360 | } |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 361 | if (mRenderThread == nullptr) { |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 362 | mRenderThread = std::make_unique<VirtualCameraRenderThread>( |
Jan Sebechlebsky | 4c9bb1e | 2024-02-28 16:32:39 +0100 | [diff] [blame] | 363 | mSessionContext, resolutionFromInputConfig(*inputConfig), |
Jan Sebechlebsky | 288900f | 2024-05-24 14:47:54 +0200 | [diff] [blame^] | 364 | virtualCamera->getMaxInputResolution(), mCameraDeviceCallback); |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 365 | mRenderThread->start(); |
| 366 | inputSurface = mRenderThread->getInputSurface(); |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | if (mVirtualCameraClientCallback != nullptr && inputSurface != nullptr) { |
| 371 | // TODO(b/301023410) Pass streamId based on client input stream id once |
| 372 | // support for multiple input streams is implemented. For now we always |
| 373 | // create single texture. |
| 374 | mVirtualCameraClientCallback->onStreamConfigured( |
| 375 | /*streamId=*/0, aidl::android::view::Surface(inputSurface.get()), |
Jan Sebechlebsky | 4c9bb1e | 2024-02-28 16:32:39 +0100 | [diff] [blame] | 376 | inputConfig->width, inputConfig->height, inputConfig->pixelFormat); |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 377 | } |
| 378 | |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 379 | return ndk::ScopedAStatus::ok(); |
| 380 | } |
| 381 | |
| 382 | ndk::ScopedAStatus VirtualCameraSession::constructDefaultRequestSettings( |
| 383 | RequestTemplate in_type, CameraMetadata* _aidl_return) { |
| 384 | ALOGV("%s: type %d", __func__, static_cast<int32_t>(in_type)); |
| 385 | |
Jan Sebechlebsky | c3e1a63 | 2024-02-06 14:19:05 +0100 | [diff] [blame] | 386 | std::shared_ptr<VirtualCameraDevice> camera = mCameraDevice.lock(); |
| 387 | if (camera == nullptr) { |
| 388 | ALOGW( |
| 389 | "%s: constructDefaultRequestSettings called on already unregistered " |
| 390 | "camera", |
| 391 | __func__); |
| 392 | return cameraStatus(Status::CAMERA_DISCONNECTED); |
| 393 | } |
| 394 | |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 395 | switch (in_type) { |
| 396 | case RequestTemplate::PREVIEW: |
| 397 | case RequestTemplate::STILL_CAPTURE: |
Jan Sebechlebsky | b0119fa | 2023-12-04 10:29:06 +0100 | [diff] [blame] | 398 | case RequestTemplate::VIDEO_RECORD: |
| 399 | case RequestTemplate::VIDEO_SNAPSHOT: { |
Jan Sebechlebsky | c3e1a63 | 2024-02-06 14:19:05 +0100 | [diff] [blame] | 400 | *_aidl_return = |
| 401 | createDefaultRequestSettings(in_type, camera->getInputConfigs()); |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 402 | return ndk::ScopedAStatus::ok(); |
| 403 | } |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 404 | case RequestTemplate::MANUAL: |
| 405 | case RequestTemplate::ZERO_SHUTTER_LAG: |
| 406 | // Don't support VIDEO_SNAPSHOT, MANUAL, ZSL templates |
| 407 | return ndk::ScopedAStatus::fromServiceSpecificError( |
| 408 | static_cast<int32_t>(Status::ILLEGAL_ARGUMENT)); |
| 409 | ; |
| 410 | default: |
| 411 | ALOGE("%s: unknown request template type %d", __FUNCTION__, |
| 412 | static_cast<int>(in_type)); |
| 413 | return ndk::ScopedAStatus::fromServiceSpecificError( |
| 414 | static_cast<int32_t>(Status::ILLEGAL_ARGUMENT)); |
| 415 | ; |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | ndk::ScopedAStatus VirtualCameraSession::flush() { |
| 420 | ALOGV("%s", __func__); |
| 421 | std::lock_guard<std::mutex> lock(mLock); |
Jan Sebechlebsky | b0d8cab | 2023-11-28 10:55:04 +0100 | [diff] [blame] | 422 | if (mRenderThread != nullptr) { |
| 423 | mRenderThread->flush(); |
| 424 | } |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 425 | return ndk::ScopedAStatus::ok(); |
| 426 | } |
| 427 | |
| 428 | ndk::ScopedAStatus VirtualCameraSession::getCaptureRequestMetadataQueue( |
| 429 | MQDescriptor<int8_t, SynchronizedReadWrite>* _aidl_return) { |
| 430 | ALOGV("%s", __func__); |
| 431 | *_aidl_return = mRequestMetadataQueue->dupeDesc(); |
| 432 | return ndk::ScopedAStatus::ok(); |
| 433 | } |
| 434 | |
| 435 | ndk::ScopedAStatus VirtualCameraSession::getCaptureResultMetadataQueue( |
| 436 | MQDescriptor<int8_t, SynchronizedReadWrite>* _aidl_return) { |
| 437 | ALOGV("%s", __func__); |
| 438 | *_aidl_return = mResultMetadataQueue->dupeDesc(); |
| 439 | return ndk::ScopedAStatus::ok(); |
| 440 | } |
| 441 | |
| 442 | ndk::ScopedAStatus VirtualCameraSession::isReconfigurationRequired( |
| 443 | const CameraMetadata& in_oldSessionParams, |
| 444 | const CameraMetadata& in_newSessionParams, bool* _aidl_return) { |
| 445 | ALOGV("%s: oldSessionParams: %s newSessionParams: %s", __func__, |
| 446 | in_newSessionParams.toString().c_str(), |
| 447 | in_oldSessionParams.toString().c_str()); |
| 448 | |
| 449 | if (_aidl_return == nullptr) { |
| 450 | return ndk::ScopedAStatus::fromServiceSpecificError( |
| 451 | static_cast<int32_t>(Status::ILLEGAL_ARGUMENT)); |
| 452 | } |
| 453 | |
| 454 | *_aidl_return = true; |
| 455 | return ndk::ScopedAStatus::ok(); |
| 456 | } |
| 457 | |
| 458 | ndk::ScopedAStatus VirtualCameraSession::processCaptureRequest( |
| 459 | const std::vector<CaptureRequest>& in_requests, |
| 460 | const std::vector<BufferCache>& in_cachesToRemove, int32_t* _aidl_return) { |
| 461 | ALOGV("%s", __func__); |
| 462 | |
| 463 | if (!in_cachesToRemove.empty()) { |
| 464 | mSessionContext.removeBufferCaches(in_cachesToRemove); |
| 465 | } |
| 466 | |
| 467 | for (const auto& captureRequest : in_requests) { |
| 468 | auto status = processCaptureRequest(captureRequest); |
| 469 | if (!status.isOk()) { |
| 470 | return status; |
| 471 | } |
| 472 | } |
| 473 | *_aidl_return = in_requests.size(); |
| 474 | return ndk::ScopedAStatus::ok(); |
| 475 | } |
| 476 | |
| 477 | ndk::ScopedAStatus VirtualCameraSession::signalStreamFlush( |
| 478 | const std::vector<int32_t>& in_streamIds, int32_t in_streamConfigCounter) { |
| 479 | ALOGV("%s", __func__); |
| 480 | |
| 481 | (void)in_streamIds; |
| 482 | (void)in_streamConfigCounter; |
| 483 | return ndk::ScopedAStatus::ok(); |
| 484 | } |
| 485 | |
| 486 | ndk::ScopedAStatus VirtualCameraSession::switchToOffline( |
| 487 | const std::vector<int32_t>& in_streamsToKeep, |
| 488 | CameraOfflineSessionInfo* out_offlineSessionInfo, |
| 489 | std::shared_ptr<ICameraOfflineSession>* _aidl_return) { |
| 490 | ALOGV("%s", __func__); |
| 491 | |
| 492 | (void)in_streamsToKeep; |
| 493 | (void)out_offlineSessionInfo; |
| 494 | |
| 495 | if (_aidl_return == nullptr) { |
| 496 | return ndk::ScopedAStatus::fromServiceSpecificError( |
| 497 | static_cast<int32_t>(Status::ILLEGAL_ARGUMENT)); |
| 498 | } |
| 499 | |
| 500 | *_aidl_return = nullptr; |
| 501 | return cameraStatus(Status::OPERATION_NOT_SUPPORTED); |
| 502 | } |
| 503 | |
| 504 | ndk::ScopedAStatus VirtualCameraSession::repeatingRequestEnd( |
| 505 | int32_t in_frameNumber, const std::vector<int32_t>& in_streamIds) { |
| 506 | ALOGV("%s", __func__); |
| 507 | (void)in_frameNumber; |
| 508 | (void)in_streamIds; |
| 509 | return ndk::ScopedAStatus::ok(); |
| 510 | } |
| 511 | |
| 512 | std::set<int> VirtualCameraSession::getStreamIds() const { |
| 513 | return mSessionContext.getStreamIds(); |
| 514 | } |
| 515 | |
| 516 | ndk::ScopedAStatus VirtualCameraSession::processCaptureRequest( |
| 517 | const CaptureRequest& request) { |
Vadim Caen | 324fcfb | 2024-03-21 16:49:08 +0100 | [diff] [blame] | 518 | ALOGV("%s: request: %s", __func__, request.toString().c_str()); |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 519 | |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 520 | std::shared_ptr<ICameraDeviceCallback> cameraCallback = nullptr; |
Jan Sebechlebsky | 4ce3208 | 2024-02-14 16:02:11 +0100 | [diff] [blame] | 521 | RequestSettings requestSettings; |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 522 | { |
| 523 | std::lock_guard<std::mutex> lock(mLock); |
Jan Sebechlebsky | 4ce3208 | 2024-02-14 16:02:11 +0100 | [diff] [blame] | 524 | |
| 525 | // If metadata it empty, last received metadata applies, if it's non-empty |
| 526 | // update it. |
| 527 | if (!request.settings.metadata.empty()) { |
| 528 | mCurrentRequestMetadata = request.settings; |
| 529 | } |
| 530 | |
| 531 | // We don't have any metadata for this request - this means we received none |
| 532 | // in first request, this is an error state. |
| 533 | if (mCurrentRequestMetadata.metadata.empty()) { |
| 534 | return cameraStatus(Status::ILLEGAL_ARGUMENT); |
| 535 | } |
| 536 | |
| 537 | requestSettings = createSettingsFromMetadata(mCurrentRequestMetadata); |
| 538 | |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 539 | cameraCallback = mCameraDeviceCallback; |
| 540 | } |
| 541 | |
| 542 | if (cameraCallback == nullptr) { |
| 543 | ALOGE( |
| 544 | "%s: processCaptureRequest called, but there's no camera callback " |
| 545 | "configured", |
| 546 | __func__); |
| 547 | return cameraStatus(Status::INTERNAL_ERROR); |
| 548 | } |
| 549 | |
| 550 | if (!mSessionContext.importBuffersFromCaptureRequest(request)) { |
| 551 | ALOGE("Failed to import buffers from capture request."); |
| 552 | return cameraStatus(Status::INTERNAL_ERROR); |
| 553 | } |
| 554 | |
| 555 | std::vector<CaptureRequestBuffer> taskBuffers; |
| 556 | taskBuffers.reserve(request.outputBuffers.size()); |
| 557 | for (const StreamBuffer& streamBuffer : request.outputBuffers) { |
| 558 | taskBuffers.emplace_back(streamBuffer.streamId, streamBuffer.bufferId, |
| 559 | importFence(streamBuffer.acquireFence)); |
| 560 | } |
| 561 | |
| 562 | { |
| 563 | std::lock_guard<std::mutex> lock(mLock); |
| 564 | if (mRenderThread == nullptr) { |
| 565 | ALOGE( |
| 566 | "%s: processCaptureRequest (frameNumber %d)called before configure " |
| 567 | "(render thread not initialized)", |
| 568 | __func__, request.frameNumber); |
| 569 | return cameraStatus(Status::INTERNAL_ERROR); |
| 570 | } |
| 571 | mRenderThread->enqueueTask(std::make_unique<ProcessCaptureRequestTask>( |
Jan Sebechlebsky | 4ce3208 | 2024-02-14 16:02:11 +0100 | [diff] [blame] | 572 | request.frameNumber, taskBuffers, requestSettings)); |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 573 | } |
| 574 | |
| 575 | if (mVirtualCameraClientCallback != nullptr) { |
| 576 | auto status = mVirtualCameraClientCallback->onProcessCaptureRequest( |
| 577 | /*streamId=*/0, request.frameNumber); |
| 578 | if (!status.isOk()) { |
| 579 | ALOGE( |
| 580 | "Failed to invoke onProcessCaptureRequest client callback for frame " |
| 581 | "%d", |
| 582 | request.frameNumber); |
| 583 | } |
| 584 | } |
| 585 | |
| 586 | return ndk::ScopedAStatus::ok(); |
| 587 | } |
| 588 | |
| 589 | } // namespace virtualcamera |
| 590 | } // namespace companion |
| 591 | } // namespace android |