Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 1 | /* |
Biswarup Pal | 6152a30 | 2023-12-19 12:44:09 +0000 | [diff] [blame] | 2 | * Copyright 2023 The Android Open Source Project |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 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 "VirtualCameraDevice" |
| 19 | #include "VirtualCameraDevice.h" |
| 20 | |
Jan Sebechlebsky | 3b478c4 | 2023-11-23 13:15:56 +0100 | [diff] [blame] | 21 | #include <algorithm> |
| 22 | #include <array> |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 23 | #include <chrono> |
| 24 | #include <cstdint> |
Jan Sebechlebsky | 3b478c4 | 2023-11-23 13:15:56 +0100 | [diff] [blame] | 25 | #include <iterator> |
Jan Sebechlebsky | c3e1a63 | 2024-02-06 14:19:05 +0100 | [diff] [blame] | 26 | #include <numeric> |
Jan Sebechlebsky | 3b478c4 | 2023-11-23 13:15:56 +0100 | [diff] [blame] | 27 | #include <optional> |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 28 | #include <string> |
Jan Sebechlebsky | c3e1a63 | 2024-02-06 14:19:05 +0100 | [diff] [blame] | 29 | #include <vector> |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 30 | |
| 31 | #include "VirtualCameraSession.h" |
Jan Sebechlebsky | 3b478c4 | 2023-11-23 13:15:56 +0100 | [diff] [blame] | 32 | #include "aidl/android/companion/virtualcamera/SupportedStreamConfiguration.h" |
Biswarup Pal | 6152a30 | 2023-12-19 12:44:09 +0000 | [diff] [blame] | 33 | #include "aidl/android/companion/virtualcamera/VirtualCameraConfiguration.h" |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 34 | #include "aidl/android/hardware/camera/common/Status.h" |
| 35 | #include "aidl/android/hardware/camera/device/CameraMetadata.h" |
Jan Sebechlebsky | 3b478c4 | 2023-11-23 13:15:56 +0100 | [diff] [blame] | 36 | #include "aidl/android/hardware/camera/device/StreamConfiguration.h" |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 37 | #include "android/binder_auto_utils.h" |
| 38 | #include "android/binder_status.h" |
| 39 | #include "log/log.h" |
| 40 | #include "system/camera_metadata.h" |
Jan Sebechlebsky | 4ce3208 | 2024-02-14 16:02:11 +0100 | [diff] [blame] | 41 | #include "util/MetadataUtil.h" |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 42 | #include "util/Util.h" |
| 43 | |
| 44 | namespace android { |
| 45 | namespace companion { |
| 46 | namespace virtualcamera { |
| 47 | |
Jan Sebechlebsky | 3b478c4 | 2023-11-23 13:15:56 +0100 | [diff] [blame] | 48 | using ::aidl::android::companion::virtualcamera::Format; |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 49 | using ::aidl::android::companion::virtualcamera::IVirtualCameraCallback; |
Biswarup Pal | 112458f | 2023-12-28 19:50:17 +0000 | [diff] [blame] | 50 | using ::aidl::android::companion::virtualcamera::LensFacing; |
Biswarup Pal | 6152a30 | 2023-12-19 12:44:09 +0000 | [diff] [blame] | 51 | using ::aidl::android::companion::virtualcamera::SensorOrientation; |
Jan Sebechlebsky | 3b478c4 | 2023-11-23 13:15:56 +0100 | [diff] [blame] | 52 | using ::aidl::android::companion::virtualcamera::SupportedStreamConfiguration; |
Biswarup Pal | 6152a30 | 2023-12-19 12:44:09 +0000 | [diff] [blame] | 53 | using ::aidl::android::companion::virtualcamera::VirtualCameraConfiguration; |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 54 | using ::aidl::android::hardware::camera::common::CameraResourceCost; |
| 55 | using ::aidl::android::hardware::camera::common::Status; |
| 56 | using ::aidl::android::hardware::camera::device::CameraMetadata; |
| 57 | using ::aidl::android::hardware::camera::device::ICameraDeviceCallback; |
| 58 | using ::aidl::android::hardware::camera::device::ICameraDeviceSession; |
| 59 | using ::aidl::android::hardware::camera::device::ICameraInjectionSession; |
Jan Sebechlebsky | 3b478c4 | 2023-11-23 13:15:56 +0100 | [diff] [blame] | 60 | using ::aidl::android::hardware::camera::device::Stream; |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 61 | using ::aidl::android::hardware::camera::device::StreamConfiguration; |
| 62 | using ::aidl::android::hardware::camera::device::StreamRotation; |
| 63 | using ::aidl::android::hardware::camera::device::StreamType; |
| 64 | using ::aidl::android::hardware::graphics::common::PixelFormat; |
| 65 | |
| 66 | namespace { |
| 67 | |
| 68 | using namespace std::chrono_literals; |
| 69 | |
| 70 | // Prefix of camera name - "device@1.1/virtual/{numerical_id}" |
| 71 | const char* kDevicePathPrefix = "device@1.1/virtual/"; |
| 72 | |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 73 | constexpr int32_t kMaxJpegSize = 3 * 1024 * 1024 /*3MiB*/; |
| 74 | |
Jan Sebechlebsky | bb01c1d | 2024-02-12 11:41:37 +0100 | [diff] [blame] | 75 | constexpr int32_t kMinFps = 15; |
| 76 | |
| 77 | constexpr std::chrono::nanoseconds kMaxFrameDuration = |
| 78 | std::chrono::duration_cast<std::chrono::nanoseconds>(1e9ns / kMinFps); |
| 79 | |
| 80 | constexpr uint8_t kPipelineMaxDepth = 2; |
| 81 | |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 82 | constexpr MetadataBuilder::ControlRegion kDefaultEmptyControlRegion{}; |
| 83 | |
Jan Sebechlebsky | 4ce3208 | 2024-02-14 16:02:11 +0100 | [diff] [blame] | 84 | constexpr float kAspectRatioEpsilon = 0.05; |
| 85 | |
| 86 | const std::array<Resolution, 5> kStandardJpegThumbnailSizes{ |
| 87 | Resolution(176, 144), Resolution(240, 144), Resolution(256, 144), |
| 88 | Resolution(240, 160), Resolution(240, 180)}; |
| 89 | |
Jan Sebechlebsky | 8ae2359 | 2024-02-02 16:08:18 +0100 | [diff] [blame] | 90 | const std::array<PixelFormat, 3> kOutputFormats{ |
| 91 | PixelFormat::IMPLEMENTATION_DEFINED, PixelFormat::YCBCR_420_888, |
| 92 | PixelFormat::BLOB}; |
Biswarup Pal | 6152a30 | 2023-12-19 12:44:09 +0000 | [diff] [blame] | 93 | |
Jan Sebechlebsky | 4ce3208 | 2024-02-14 16:02:11 +0100 | [diff] [blame] | 94 | bool isApproximatellySameAspectRatio(const Resolution r1, const Resolution r2) { |
| 95 | float aspectRatio1 = |
| 96 | static_cast<float>(r1.width) / static_cast<float>(r1.height); |
| 97 | float aspectRatio2 = |
| 98 | static_cast<float>(r2.width) / static_cast<float>(r2.height); |
| 99 | |
| 100 | return abs(aspectRatio1 - aspectRatio2) < kAspectRatioEpsilon; |
| 101 | } |
| 102 | |
| 103 | std::vector<Resolution> getSupportedJpegThumbnailSizes( |
| 104 | const std::vector<SupportedStreamConfiguration>& configs) { |
| 105 | auto isSupportedByAnyInputConfig = |
| 106 | [&configs](const Resolution thumbnailResolution) { |
| 107 | return std::any_of( |
| 108 | configs.begin(), configs.end(), |
| 109 | [thumbnailResolution](const SupportedStreamConfiguration& config) { |
| 110 | return isApproximatellySameAspectRatio( |
| 111 | thumbnailResolution, Resolution(config.width, config.height)); |
| 112 | }); |
| 113 | }; |
| 114 | |
| 115 | std::vector<Resolution> supportedThumbnailSizes({Resolution(0, 0)}); |
| 116 | std::copy_if(kStandardJpegThumbnailSizes.begin(), |
| 117 | kStandardJpegThumbnailSizes.end(), |
| 118 | std::back_insert_iterator(supportedThumbnailSizes), |
| 119 | isSupportedByAnyInputConfig); |
| 120 | return supportedThumbnailSizes; |
| 121 | } |
| 122 | |
Jan Sebechlebsky | 8ae2359 | 2024-02-02 16:08:18 +0100 | [diff] [blame] | 123 | bool isSupportedOutputFormat(const PixelFormat pixelFormat) { |
| 124 | return std::find(kOutputFormats.begin(), kOutputFormats.end(), pixelFormat) != |
| 125 | kOutputFormats.end(); |
| 126 | } |
| 127 | |
Jan Sebechlebsky | c3e1a63 | 2024-02-06 14:19:05 +0100 | [diff] [blame] | 128 | std::vector<MetadataBuilder::FpsRange> fpsRangesForInputConfig( |
| 129 | const std::vector<SupportedStreamConfiguration>& configs) { |
| 130 | std::set<MetadataBuilder::FpsRange> availableRanges; |
Jan Sebechlebsky | bb01c1d | 2024-02-12 11:41:37 +0100 | [diff] [blame] | 131 | |
Jan Sebechlebsky | c3e1a63 | 2024-02-06 14:19:05 +0100 | [diff] [blame] | 132 | for (const SupportedStreamConfiguration& config : configs) { |
Jan Sebechlebsky | bb01c1d | 2024-02-12 11:41:37 +0100 | [diff] [blame] | 133 | availableRanges.insert({.minFps = kMinFps, .maxFps = config.maxFps}); |
Jan Sebechlebsky | c3e1a63 | 2024-02-06 14:19:05 +0100 | [diff] [blame] | 134 | availableRanges.insert({.minFps = config.maxFps, .maxFps = config.maxFps}); |
| 135 | } |
| 136 | |
Jan Sebechlebsky | bb01c1d | 2024-02-12 11:41:37 +0100 | [diff] [blame] | 137 | if (std::any_of(configs.begin(), configs.end(), |
| 138 | [](const SupportedStreamConfiguration& config) { |
| 139 | return config.maxFps >= 30; |
| 140 | })) { |
| 141 | availableRanges.insert({.minFps = kMinFps, .maxFps = 30}); |
| 142 | availableRanges.insert({.minFps = 30, .maxFps = 30}); |
| 143 | } |
| 144 | |
Jan Sebechlebsky | c3e1a63 | 2024-02-06 14:19:05 +0100 | [diff] [blame] | 145 | return std::vector<MetadataBuilder::FpsRange>(availableRanges.begin(), |
| 146 | availableRanges.end()); |
| 147 | } |
| 148 | |
Jan Sebechlebsky | 3b478c4 | 2023-11-23 13:15:56 +0100 | [diff] [blame] | 149 | std::optional<Resolution> getMaxResolution( |
| 150 | const std::vector<SupportedStreamConfiguration>& configs) { |
| 151 | auto itMax = std::max_element(configs.begin(), configs.end(), |
| 152 | [](const SupportedStreamConfiguration& a, |
| 153 | const SupportedStreamConfiguration& b) { |
| 154 | return a.width * b.height < a.width * b.height; |
| 155 | }); |
| 156 | if (itMax == configs.end()) { |
| 157 | ALOGE( |
| 158 | "%s: empty vector of supported configurations, cannot find largest " |
| 159 | "resolution.", |
| 160 | __func__); |
| 161 | return std::nullopt; |
| 162 | } |
| 163 | |
| 164 | return Resolution(itMax->width, itMax->height); |
| 165 | } |
| 166 | |
Biswarup Pal | 6152a30 | 2023-12-19 12:44:09 +0000 | [diff] [blame] | 167 | // Returns a map of unique resolution to maximum maxFps for all streams with |
| 168 | // that resolution. |
| 169 | std::map<Resolution, int> getResolutionToMaxFpsMap( |
Jan Sebechlebsky | 3b478c4 | 2023-11-23 13:15:56 +0100 | [diff] [blame] | 170 | const std::vector<SupportedStreamConfiguration>& configs) { |
Biswarup Pal | 6152a30 | 2023-12-19 12:44:09 +0000 | [diff] [blame] | 171 | std::map<Resolution, int> resolutionToMaxFpsMap; |
| 172 | |
| 173 | for (const SupportedStreamConfiguration& config : configs) { |
| 174 | Resolution resolution(config.width, config.height); |
| 175 | if (resolutionToMaxFpsMap.find(resolution) == resolutionToMaxFpsMap.end()) { |
| 176 | resolutionToMaxFpsMap[resolution] = config.maxFps; |
| 177 | } else { |
| 178 | int currentMaxFps = resolutionToMaxFpsMap[resolution]; |
| 179 | resolutionToMaxFpsMap[resolution] = std::max(currentMaxFps, config.maxFps); |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | return resolutionToMaxFpsMap; |
Jan Sebechlebsky | 3b478c4 | 2023-11-23 13:15:56 +0100 | [diff] [blame] | 184 | } |
| 185 | |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 186 | // TODO(b/301023410) - Populate camera characteristics according to camera configuration. |
Jan Sebechlebsky | 3b478c4 | 2023-11-23 13:15:56 +0100 | [diff] [blame] | 187 | std::optional<CameraMetadata> initCameraCharacteristics( |
Biswarup Pal | 6152a30 | 2023-12-19 12:44:09 +0000 | [diff] [blame] | 188 | const std::vector<SupportedStreamConfiguration>& supportedInputConfig, |
Biswarup Pal | 112458f | 2023-12-28 19:50:17 +0000 | [diff] [blame] | 189 | const SensorOrientation sensorOrientation, const LensFacing lensFacing) { |
Jan Sebechlebsky | 3b478c4 | 2023-11-23 13:15:56 +0100 | [diff] [blame] | 190 | if (!std::all_of(supportedInputConfig.begin(), supportedInputConfig.end(), |
| 191 | [](const SupportedStreamConfiguration& config) { |
Jan Sebechlebsky | 042d1fb | 2023-12-12 16:37:00 +0100 | [diff] [blame] | 192 | return isFormatSupportedForInput( |
Biswarup Pal | 6152a30 | 2023-12-19 12:44:09 +0000 | [diff] [blame] | 193 | config.width, config.height, config.pixelFormat, |
| 194 | config.maxFps); |
Jan Sebechlebsky | 3b478c4 | 2023-11-23 13:15:56 +0100 | [diff] [blame] | 195 | })) { |
Jan Sebechlebsky | 042d1fb | 2023-12-12 16:37:00 +0100 | [diff] [blame] | 196 | ALOGE("%s: input configuration contains unsupported format", __func__); |
Jan Sebechlebsky | 3b478c4 | 2023-11-23 13:15:56 +0100 | [diff] [blame] | 197 | return std::nullopt; |
| 198 | } |
| 199 | |
| 200 | MetadataBuilder builder = |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 201 | MetadataBuilder() |
| 202 | .setSupportedHardwareLevel( |
| 203 | ANDROID_INFO_SUPPORTED_HARDWARE_LEVEL_EXTERNAL) |
| 204 | .setFlashAvailable(false) |
Biswarup Pal | 112458f | 2023-12-28 19:50:17 +0000 | [diff] [blame] | 205 | .setLensFacing( |
| 206 | static_cast<camera_metadata_enum_android_lens_facing>(lensFacing)) |
Biswarup Pal | 8ad8bc5 | 2024-02-08 13:41:44 +0000 | [diff] [blame] | 207 | .setAvailableFocalLengths({VirtualCameraDevice::kFocalLength}) |
Biswarup Pal | 6152a30 | 2023-12-19 12:44:09 +0000 | [diff] [blame] | 208 | .setSensorOrientation(static_cast<int32_t>(sensorOrientation)) |
Jan Sebechlebsky | a984ffb | 2024-02-01 09:12:37 +0100 | [diff] [blame] | 209 | .setSensorReadoutTimestamp( |
| 210 | ANDROID_SENSOR_READOUT_TIMESTAMP_NOT_SUPPORTED) |
Jan Sebechlebsky | ad8d35f | 2024-02-05 11:58:59 +0100 | [diff] [blame] | 211 | .setSensorTimestampSource(ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE_UNKNOWN) |
Biswarup Pal | d9be04d | 2024-01-31 14:35:15 +0000 | [diff] [blame] | 212 | .setSensorPhysicalSize(36.0, 24.0) |
Jan Sebechlebsky | ab37820 | 2024-02-26 10:50:42 +0100 | [diff] [blame] | 213 | .setAvailableAberrationCorrectionModes( |
| 214 | {ANDROID_COLOR_CORRECTION_ABERRATION_MODE_OFF}) |
| 215 | .setAvailableNoiseReductionModes({ANDROID_NOISE_REDUCTION_MODE_OFF}) |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 216 | .setAvailableFaceDetectModes({ANDROID_STATISTICS_FACE_DETECT_MODE_OFF}) |
Jan Sebechlebsky | bb01c1d | 2024-02-12 11:41:37 +0100 | [diff] [blame] | 217 | .setAvailableTestPatternModes({ANDROID_SENSOR_TEST_PATTERN_MODE_OFF}) |
Jan Sebechlebsky | b0119fa | 2023-12-04 10:29:06 +0100 | [diff] [blame] | 218 | .setAvailableMaxDigitalZoom(1.0) |
| 219 | .setControlAvailableModes({ANDROID_CONTROL_MODE_AUTO}) |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 220 | .setControlAfAvailableModes({ANDROID_CONTROL_AF_MODE_OFF}) |
Jan Sebechlebsky | 4425a73 | 2024-01-31 11:31:54 +0100 | [diff] [blame] | 221 | .setControlAvailableSceneModes({ANDROID_CONTROL_SCENE_MODE_DISABLED}) |
| 222 | .setControlAvailableEffects({ANDROID_CONTROL_EFFECT_MODE_OFF}) |
Jan Sebechlebsky | bb01c1d | 2024-02-12 11:41:37 +0100 | [diff] [blame] | 223 | .setControlAvailableVideoStabilizationModes( |
| 224 | {ANDROID_CONTROL_VIDEO_STABILIZATION_MODE_OFF}) |
Jan Sebechlebsky | c3e1a63 | 2024-02-06 14:19:05 +0100 | [diff] [blame] | 225 | .setControlAeAvailableModes({ANDROID_CONTROL_AE_MODE_ON}) |
| 226 | .setControlAeAvailableAntibandingModes( |
| 227 | {ANDROID_CONTROL_AE_ANTIBANDING_MODE_AUTO}) |
| 228 | .setControlAeAvailableFpsRanges( |
| 229 | fpsRangesForInputConfig(supportedInputConfig)) |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 230 | .setControlMaxRegions(0, 0, 0) |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 231 | .setControlAfRegions({kDefaultEmptyControlRegion}) |
| 232 | .setControlAeRegions({kDefaultEmptyControlRegion}) |
| 233 | .setControlAwbRegions({kDefaultEmptyControlRegion}) |
Jan Sebechlebsky | c3e1a63 | 2024-02-06 14:19:05 +0100 | [diff] [blame] | 234 | .setControlAeCompensationRange(0, 0) |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 235 | .setControlAeCompensationStep(camera_metadata_rational_t{0, 1}) |
Jan Sebechlebsky | 4425a73 | 2024-01-31 11:31:54 +0100 | [diff] [blame] | 236 | .setControlAwbLockAvailable(false) |
| 237 | .setControlAeLockAvailable(false) |
| 238 | .setControlAvailableAwbModes({ANDROID_CONTROL_AWB_MODE_AUTO}) |
Jan Sebechlebsky | 6ab07fe | 2023-12-05 15:23:34 +0100 | [diff] [blame] | 239 | .setControlZoomRatioRange(/*min=*/1.0, /*max=*/1.0) |
Jan Sebechlebsky | ab37820 | 2024-02-26 10:50:42 +0100 | [diff] [blame] | 240 | .setCroppingType(ANDROID_SCALER_CROPPING_TYPE_CENTER_ONLY) |
Jan Sebechlebsky | 4ce3208 | 2024-02-14 16:02:11 +0100 | [diff] [blame] | 241 | .setJpegAvailableThumbnailSizes( |
| 242 | getSupportedJpegThumbnailSizes(supportedInputConfig)) |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 243 | .setMaxJpegSize(kMaxJpegSize) |
Jan Sebechlebsky | ab37820 | 2024-02-26 10:50:42 +0100 | [diff] [blame] | 244 | .setMaxFaceCount(0) |
Jan Sebechlebsky | bb01c1d | 2024-02-12 11:41:37 +0100 | [diff] [blame] | 245 | .setMaxFrameDuration(kMaxFrameDuration) |
Jan Sebechlebsky | 8ae2359 | 2024-02-02 16:08:18 +0100 | [diff] [blame] | 246 | .setMaxNumberOutputStreams( |
| 247 | VirtualCameraDevice::kMaxNumberOfRawStreams, |
| 248 | VirtualCameraDevice::kMaxNumberOfProcessedStreams, |
| 249 | VirtualCameraDevice::kMaxNumberOfStallStreams) |
Jan Sebechlebsky | ab37820 | 2024-02-26 10:50:42 +0100 | [diff] [blame] | 250 | .setRequestPartialResultCount(1) |
Jan Sebechlebsky | bb01c1d | 2024-02-12 11:41:37 +0100 | [diff] [blame] | 251 | .setPipelineMaxDepth(kPipelineMaxDepth) |
Jan Sebechlebsky | 4425a73 | 2024-01-31 11:31:54 +0100 | [diff] [blame] | 252 | .setSyncMaxLatency(ANDROID_SYNC_MAX_LATENCY_UNKNOWN) |
Jan Sebechlebsky | 4be2bd0 | 2024-02-26 18:35:18 +0100 | [diff] [blame^] | 253 | .setAvailableRequestKeys({ANDROID_COLOR_CORRECTION_ABERRATION_MODE, |
| 254 | ANDROID_CONTROL_CAPTURE_INTENT, |
Jan Sebechlebsky | 4ce3208 | 2024-02-14 16:02:11 +0100 | [diff] [blame] | 255 | ANDROID_CONTROL_AE_MODE, |
| 256 | ANDROID_CONTROL_AE_EXPOSURE_COMPENSATION, |
| 257 | ANDROID_CONTROL_AE_TARGET_FPS_RANGE, |
| 258 | ANDROID_CONTROL_AE_ANTIBANDING_MODE, |
| 259 | ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER, |
| 260 | ANDROID_CONTROL_AF_TRIGGER, |
| 261 | ANDROID_CONTROL_AF_MODE, |
| 262 | ANDROID_CONTROL_AWB_MODE, |
| 263 | ANDROID_SCALER_CROP_REGION, |
| 264 | ANDROID_CONTROL_EFFECT_MODE, |
| 265 | ANDROID_CONTROL_MODE, |
| 266 | ANDROID_CONTROL_SCENE_MODE, |
| 267 | ANDROID_CONTROL_VIDEO_STABILIZATION_MODE, |
| 268 | ANDROID_CONTROL_ZOOM_RATIO, |
Jan Sebechlebsky | 4ce3208 | 2024-02-14 16:02:11 +0100 | [diff] [blame] | 269 | ANDROID_FLASH_MODE, |
| 270 | ANDROID_JPEG_AVAILABLE_THUMBNAIL_SIZES, |
| 271 | ANDROID_JPEG_QUALITY, |
Jan Sebechlebsky | 4be2bd0 | 2024-02-26 18:35:18 +0100 | [diff] [blame^] | 272 | ANDROID_JPEG_THUMBNAIL_QUALITY, |
| 273 | ANDROID_NOISE_REDUCTION_MODE, |
| 274 | ANDROID_STATISTICS_FACE_DETECT_MODE}) |
Jan Sebechlebsky | c3e1a63 | 2024-02-06 14:19:05 +0100 | [diff] [blame] | 275 | .setAvailableResultKeys( |
Jan Sebechlebsky | 4be2bd0 | 2024-02-26 18:35:18 +0100 | [diff] [blame^] | 276 | {ANDROID_COLOR_CORRECTION_ABERRATION_MODE, ANDROID_CONTROL_AE_MODE, |
| 277 | ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER, ANDROID_CONTROL_AF_MODE, |
| 278 | ANDROID_CONTROL_AWB_MODE, ANDROID_CONTROL_EFFECT_MODE, |
| 279 | ANDROID_CONTROL_MODE, ANDROID_FLASH_MODE, ANDROID_FLASH_STATE, |
Jan Sebechlebsky | 4ce3208 | 2024-02-14 16:02:11 +0100 | [diff] [blame] | 280 | ANDROID_JPEG_AVAILABLE_THUMBNAIL_SIZES, ANDROID_JPEG_QUALITY, |
Jan Sebechlebsky | 4be2bd0 | 2024-02-26 18:35:18 +0100 | [diff] [blame^] | 281 | ANDROID_JPEG_THUMBNAIL_QUALITY, ANDROID_LENS_FOCAL_LENGTH, |
| 282 | ANDROID_SENSOR_TIMESTAMP, ANDROID_NOISE_REDUCTION_MODE}) |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 283 | .setAvailableCapabilities( |
Jan Sebechlebsky | ad8d35f | 2024-02-05 11:58:59 +0100 | [diff] [blame] | 284 | {ANDROID_REQUEST_AVAILABLE_CAPABILITIES_BACKWARD_COMPATIBLE}); |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 285 | |
Jan Sebechlebsky | 3b478c4 | 2023-11-23 13:15:56 +0100 | [diff] [blame] | 286 | // Active array size must correspond to largest supported input resolution. |
| 287 | std::optional<Resolution> maxResolution = |
| 288 | getMaxResolution(supportedInputConfig); |
| 289 | if (!maxResolution.has_value()) { |
| 290 | return std::nullopt; |
| 291 | } |
| 292 | builder.setSensorActiveArraySize(0, 0, maxResolution->width, |
| 293 | maxResolution->height); |
Biswarup Pal | d9be04d | 2024-01-31 14:35:15 +0000 | [diff] [blame] | 294 | builder.setSensorPixelArraySize(maxResolution->width, maxResolution->height); |
Jan Sebechlebsky | 3b478c4 | 2023-11-23 13:15:56 +0100 | [diff] [blame] | 295 | |
| 296 | std::vector<MetadataBuilder::StreamConfiguration> outputConfigurations; |
| 297 | |
| 298 | // TODO(b/301023410) Add also all "standard" resolutions we can rescale the |
| 299 | // streams to (all standard resolutions with same aspect ratio). |
| 300 | |
Biswarup Pal | 6152a30 | 2023-12-19 12:44:09 +0000 | [diff] [blame] | 301 | std::map<Resolution, int> resolutionToMaxFpsMap = |
| 302 | getResolutionToMaxFpsMap(supportedInputConfig); |
Jan Sebechlebsky | 3b478c4 | 2023-11-23 13:15:56 +0100 | [diff] [blame] | 303 | |
Biswarup Pal | 6152a30 | 2023-12-19 12:44:09 +0000 | [diff] [blame] | 304 | // Add configurations for all unique input resolutions and output formats. |
Jan Sebechlebsky | 8ae2359 | 2024-02-02 16:08:18 +0100 | [diff] [blame] | 305 | for (const PixelFormat format : kOutputFormats) { |
Biswarup Pal | 6152a30 | 2023-12-19 12:44:09 +0000 | [diff] [blame] | 306 | std::transform( |
| 307 | resolutionToMaxFpsMap.begin(), resolutionToMaxFpsMap.end(), |
| 308 | std::back_inserter(outputConfigurations), [format](const auto& entry) { |
| 309 | Resolution resolution = entry.first; |
| 310 | int maxFps = entry.second; |
| 311 | return MetadataBuilder::StreamConfiguration{ |
| 312 | .width = resolution.width, |
| 313 | .height = resolution.height, |
Jan Sebechlebsky | 8ae2359 | 2024-02-02 16:08:18 +0100 | [diff] [blame] | 314 | .format = static_cast<int32_t>(format), |
Biswarup Pal | 6152a30 | 2023-12-19 12:44:09 +0000 | [diff] [blame] | 315 | .minFrameDuration = std::chrono::nanoseconds(1s) / maxFps, |
| 316 | .minStallDuration = 0s}; |
| 317 | }); |
| 318 | } |
Jan Sebechlebsky | 3b478c4 | 2023-11-23 13:15:56 +0100 | [diff] [blame] | 319 | |
| 320 | ALOGV("Adding %zu output configurations", outputConfigurations.size()); |
| 321 | builder.setAvailableOutputStreamConfigurations(outputConfigurations); |
| 322 | |
Jan Sebechlebsky | ad8d35f | 2024-02-05 11:58:59 +0100 | [diff] [blame] | 323 | auto metadata = builder.setAvailableCharacteristicKeys().build(); |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 324 | if (metadata == nullptr) { |
| 325 | ALOGE("Failed to build metadata!"); |
| 326 | return CameraMetadata(); |
| 327 | } |
| 328 | |
| 329 | return std::move(*metadata); |
| 330 | } |
| 331 | |
| 332 | } // namespace |
| 333 | |
| 334 | VirtualCameraDevice::VirtualCameraDevice( |
Biswarup Pal | 6152a30 | 2023-12-19 12:44:09 +0000 | [diff] [blame] | 335 | const uint32_t cameraId, const VirtualCameraConfiguration& configuration) |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 336 | : mCameraId(cameraId), |
Biswarup Pal | 6152a30 | 2023-12-19 12:44:09 +0000 | [diff] [blame] | 337 | mVirtualCameraClientCallback(configuration.virtualCameraCallback), |
| 338 | mSupportedInputConfigurations(configuration.supportedStreamConfigs) { |
| 339 | std::optional<CameraMetadata> metadata = initCameraCharacteristics( |
Biswarup Pal | 112458f | 2023-12-28 19:50:17 +0000 | [diff] [blame] | 340 | mSupportedInputConfigurations, configuration.sensorOrientation, |
| 341 | configuration.lensFacing); |
Jan Sebechlebsky | 3b478c4 | 2023-11-23 13:15:56 +0100 | [diff] [blame] | 342 | if (metadata.has_value()) { |
| 343 | mCameraCharacteristics = *metadata; |
| 344 | } else { |
| 345 | ALOGE( |
| 346 | "%s: Failed to initialize camera characteristic based on provided " |
| 347 | "configuration.", |
| 348 | __func__); |
| 349 | } |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | ndk::ScopedAStatus VirtualCameraDevice::getCameraCharacteristics( |
| 353 | CameraMetadata* _aidl_return) { |
| 354 | ALOGV("%s", __func__); |
| 355 | if (_aidl_return == nullptr) { |
| 356 | return cameraStatus(Status::ILLEGAL_ARGUMENT); |
| 357 | } |
| 358 | |
| 359 | *_aidl_return = mCameraCharacteristics; |
| 360 | return ndk::ScopedAStatus::ok(); |
| 361 | } |
| 362 | |
| 363 | ndk::ScopedAStatus VirtualCameraDevice::getPhysicalCameraCharacteristics( |
| 364 | const std::string& in_physicalCameraId, CameraMetadata* _aidl_return) { |
| 365 | ALOGV("%s: physicalCameraId %s", __func__, in_physicalCameraId.c_str()); |
| 366 | (void)_aidl_return; |
| 367 | |
| 368 | // VTS tests expect this call to fail with illegal argument status for |
| 369 | // all publicly advertised camera ids. |
| 370 | // Because we don't support physical camera ids, we just always |
| 371 | // fail with illegal argument (there's no valid argument to provide). |
| 372 | return cameraStatus(Status::ILLEGAL_ARGUMENT); |
| 373 | } |
| 374 | |
| 375 | ndk::ScopedAStatus VirtualCameraDevice::getResourceCost( |
| 376 | CameraResourceCost* _aidl_return) { |
| 377 | ALOGV("%s", __func__); |
| 378 | if (_aidl_return == nullptr) { |
| 379 | return cameraStatus(Status::ILLEGAL_ARGUMENT); |
| 380 | } |
| 381 | _aidl_return->resourceCost = 100; // ¯\_(ツ)_/¯ |
| 382 | return ndk::ScopedAStatus::ok(); |
| 383 | } |
| 384 | |
| 385 | ndk::ScopedAStatus VirtualCameraDevice::isStreamCombinationSupported( |
| 386 | const StreamConfiguration& in_streams, bool* _aidl_return) { |
| 387 | ALOGV("%s", __func__); |
| 388 | |
| 389 | if (_aidl_return == nullptr) { |
| 390 | return cameraStatus(Status::ILLEGAL_ARGUMENT); |
| 391 | } |
| 392 | |
Jan Sebechlebsky | 3b478c4 | 2023-11-23 13:15:56 +0100 | [diff] [blame] | 393 | *_aidl_return = isStreamCombinationSupported(in_streams); |
| 394 | return ndk::ScopedAStatus::ok(); |
| 395 | }; |
| 396 | |
| 397 | bool VirtualCameraDevice::isStreamCombinationSupported( |
| 398 | const StreamConfiguration& streamConfiguration) const { |
Jan Sebechlebsky | 39129f8 | 2024-01-19 16:42:11 +0100 | [diff] [blame] | 399 | if (streamConfiguration.streams.empty()) { |
| 400 | ALOGE("%s: Querying empty configuration", __func__); |
| 401 | return false; |
| 402 | } |
| 403 | |
Jan Sebechlebsky | 8ae2359 | 2024-02-02 16:08:18 +0100 | [diff] [blame] | 404 | int numberOfProcessedStreams = 0; |
| 405 | int numberOfStallStreams = 0; |
Jan Sebechlebsky | 3b478c4 | 2023-11-23 13:15:56 +0100 | [diff] [blame] | 406 | for (const Stream& stream : streamConfiguration.streams) { |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 407 | ALOGV("%s: Configuration queried: %s", __func__, stream.toString().c_str()); |
| 408 | |
| 409 | if (stream.streamType == StreamType::INPUT) { |
| 410 | ALOGW("%s: Input stream type is not supported", __func__); |
Jan Sebechlebsky | 3b478c4 | 2023-11-23 13:15:56 +0100 | [diff] [blame] | 411 | return false; |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 412 | } |
| 413 | |
Jan Sebechlebsky | 3b478c4 | 2023-11-23 13:15:56 +0100 | [diff] [blame] | 414 | if (stream.rotation != StreamRotation::ROTATION_0 || |
Jan Sebechlebsky | 8ae2359 | 2024-02-02 16:08:18 +0100 | [diff] [blame] | 415 | !isSupportedOutputFormat(stream.format)) { |
Jan Sebechlebsky | 3b478c4 | 2023-11-23 13:15:56 +0100 | [diff] [blame] | 416 | ALOGV("Unsupported output stream type"); |
| 417 | return false; |
| 418 | } |
| 419 | |
Jan Sebechlebsky | 8ae2359 | 2024-02-02 16:08:18 +0100 | [diff] [blame] | 420 | if (stream.format == PixelFormat::BLOB) { |
| 421 | numberOfStallStreams++; |
| 422 | } else { |
| 423 | numberOfProcessedStreams++; |
| 424 | } |
| 425 | |
Jan Sebechlebsky | 3b478c4 | 2023-11-23 13:15:56 +0100 | [diff] [blame] | 426 | auto matchesSupportedInputConfig = |
| 427 | [&stream](const SupportedStreamConfiguration& config) { |
| 428 | return stream.width == config.width && stream.height == config.height; |
| 429 | }; |
| 430 | if (std::none_of(mSupportedInputConfigurations.begin(), |
| 431 | mSupportedInputConfigurations.end(), |
| 432 | matchesSupportedInputConfig)) { |
| 433 | ALOGV("Requested config doesn't match any supported input config"); |
| 434 | return false; |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 435 | } |
| 436 | } |
Jan Sebechlebsky | 8ae2359 | 2024-02-02 16:08:18 +0100 | [diff] [blame] | 437 | |
| 438 | if (numberOfProcessedStreams > kMaxNumberOfProcessedStreams) { |
| 439 | ALOGE("%s: %d processed streams exceeds the supported maximum of %d", |
| 440 | __func__, numberOfProcessedStreams, kMaxNumberOfProcessedStreams); |
| 441 | return false; |
| 442 | } |
| 443 | |
| 444 | if (numberOfStallStreams > kMaxNumberOfStallStreams) { |
| 445 | ALOGE("%s: %d stall streams exceeds the supported maximum of %d", __func__, |
| 446 | numberOfStallStreams, kMaxNumberOfStallStreams); |
| 447 | return false; |
| 448 | } |
| 449 | |
Jan Sebechlebsky | 3b478c4 | 2023-11-23 13:15:56 +0100 | [diff] [blame] | 450 | return true; |
| 451 | } |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 452 | |
| 453 | ndk::ScopedAStatus VirtualCameraDevice::open( |
| 454 | const std::shared_ptr<ICameraDeviceCallback>& in_callback, |
| 455 | std::shared_ptr<ICameraDeviceSession>* _aidl_return) { |
| 456 | ALOGV("%s", __func__); |
| 457 | |
| 458 | *_aidl_return = ndk::SharedRefBase::make<VirtualCameraSession>( |
Jan Sebechlebsky | 0bb5e09 | 2023-12-08 16:17:54 +0100 | [diff] [blame] | 459 | sharedFromThis(), in_callback, mVirtualCameraClientCallback); |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 460 | |
| 461 | return ndk::ScopedAStatus::ok(); |
| 462 | }; |
| 463 | |
| 464 | ndk::ScopedAStatus VirtualCameraDevice::openInjectionSession( |
| 465 | const std::shared_ptr<ICameraDeviceCallback>& in_callback, |
| 466 | std::shared_ptr<ICameraInjectionSession>* _aidl_return) { |
| 467 | ALOGV("%s", __func__); |
| 468 | |
| 469 | (void)in_callback; |
| 470 | (void)_aidl_return; |
| 471 | return cameraStatus(Status::OPERATION_NOT_SUPPORTED); |
| 472 | } |
| 473 | |
| 474 | ndk::ScopedAStatus VirtualCameraDevice::setTorchMode(bool in_on) { |
| 475 | ALOGV("%s: on = %s", __func__, in_on ? "on" : "off"); |
| 476 | return cameraStatus(Status::OPERATION_NOT_SUPPORTED); |
| 477 | } |
| 478 | |
| 479 | ndk::ScopedAStatus VirtualCameraDevice::turnOnTorchWithStrengthLevel( |
| 480 | int32_t in_torchStrength) { |
| 481 | ALOGV("%s: torchStrength = %d", __func__, in_torchStrength); |
| 482 | return cameraStatus(Status::OPERATION_NOT_SUPPORTED); |
| 483 | } |
| 484 | |
| 485 | ndk::ScopedAStatus VirtualCameraDevice::getTorchStrengthLevel( |
| 486 | int32_t* _aidl_return) { |
| 487 | (void)_aidl_return; |
| 488 | return cameraStatus(Status::OPERATION_NOT_SUPPORTED); |
| 489 | } |
| 490 | |
| 491 | binder_status_t VirtualCameraDevice::dump(int fd, const char** args, |
| 492 | uint32_t numArgs) { |
| 493 | // TODO(b/301023410) Implement. |
| 494 | (void)fd; |
| 495 | (void)args; |
| 496 | (void)numArgs; |
| 497 | return STATUS_OK; |
| 498 | } |
| 499 | |
| 500 | std::string VirtualCameraDevice::getCameraName() const { |
| 501 | return std::string(kDevicePathPrefix) + std::to_string(mCameraId); |
| 502 | } |
| 503 | |
Jan Sebechlebsky | c3e1a63 | 2024-02-06 14:19:05 +0100 | [diff] [blame] | 504 | const std::vector<SupportedStreamConfiguration>& |
| 505 | VirtualCameraDevice::getInputConfigs() const { |
| 506 | return mSupportedInputConfigurations; |
| 507 | } |
| 508 | |
Jan Sebechlebsky | bb01c1d | 2024-02-12 11:41:37 +0100 | [diff] [blame] | 509 | Resolution VirtualCameraDevice::getMaxInputResolution() const { |
| 510 | std::optional<Resolution> maxResolution = |
| 511 | getMaxResolution(mSupportedInputConfigurations); |
| 512 | if (!maxResolution.has_value()) { |
| 513 | ALOGE( |
| 514 | "%s: Cannot determine sensor size for virtual camera - input " |
| 515 | "configurations empty?", |
| 516 | __func__); |
| 517 | return Resolution(0, 0); |
| 518 | } |
| 519 | return maxResolution.value(); |
| 520 | } |
| 521 | |
Jan Sebechlebsky | 0bb5e09 | 2023-12-08 16:17:54 +0100 | [diff] [blame] | 522 | std::shared_ptr<VirtualCameraDevice> VirtualCameraDevice::sharedFromThis() { |
| 523 | // SharedRefBase which BnCameraDevice inherits from breaks |
| 524 | // std::enable_shared_from_this. This is recommended replacement for |
| 525 | // shared_from_this() per documentation in binder_interface_utils.h. |
| 526 | return ref<VirtualCameraDevice>(); |
| 527 | } |
| 528 | |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 529 | } // namespace virtualcamera |
| 530 | } // namespace companion |
| 531 | } // namespace android |