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