blob: d4c642342131b78a6cc0c60298aef25c363ec0af [file] [log] [blame]
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +01001/*
Biswarup Pal6152a302023-12-19 12:44:09 +00002 * Copyright 2023 The Android Open Source Project
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +01003 *
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 Sebechlebsky3b478c42023-11-23 13:15:56 +010021#include <algorithm>
22#include <array>
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +010023#include <chrono>
24#include <cstdint>
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +010025#include <iterator>
Jan Sebechlebskyc3e1a632024-02-06 14:19:05 +010026#include <numeric>
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +010027#include <optional>
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +010028#include <string>
Jan Sebechlebskyc3e1a632024-02-06 14:19:05 +010029#include <vector>
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +010030
31#include "VirtualCameraSession.h"
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +010032#include "aidl/android/companion/virtualcamera/SupportedStreamConfiguration.h"
Biswarup Pal6152a302023-12-19 12:44:09 +000033#include "aidl/android/companion/virtualcamera/VirtualCameraConfiguration.h"
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +010034#include "aidl/android/hardware/camera/common/Status.h"
35#include "aidl/android/hardware/camera/device/CameraMetadata.h"
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +010036#include "aidl/android/hardware/camera/device/StreamConfiguration.h"
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +010037#include "android/binder_auto_utils.h"
38#include "android/binder_status.h"
39#include "log/log.h"
40#include "system/camera_metadata.h"
Jan Sebechlebsky4ce32082024-02-14 16:02:11 +010041#include "util/MetadataUtil.h"
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +010042#include "util/Util.h"
43
44namespace android {
45namespace companion {
46namespace virtualcamera {
47
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +010048using ::aidl::android::companion::virtualcamera::Format;
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +010049using ::aidl::android::companion::virtualcamera::IVirtualCameraCallback;
Biswarup Pal112458f2023-12-28 19:50:17 +000050using ::aidl::android::companion::virtualcamera::LensFacing;
Biswarup Pal6152a302023-12-19 12:44:09 +000051using ::aidl::android::companion::virtualcamera::SensorOrientation;
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +010052using ::aidl::android::companion::virtualcamera::SupportedStreamConfiguration;
Biswarup Pal6152a302023-12-19 12:44:09 +000053using ::aidl::android::companion::virtualcamera::VirtualCameraConfiguration;
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +010054using ::aidl::android::hardware::camera::common::CameraResourceCost;
55using ::aidl::android::hardware::camera::common::Status;
56using ::aidl::android::hardware::camera::device::CameraMetadata;
57using ::aidl::android::hardware::camera::device::ICameraDeviceCallback;
58using ::aidl::android::hardware::camera::device::ICameraDeviceSession;
59using ::aidl::android::hardware::camera::device::ICameraInjectionSession;
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +010060using ::aidl::android::hardware::camera::device::Stream;
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +010061using ::aidl::android::hardware::camera::device::StreamConfiguration;
62using ::aidl::android::hardware::camera::device::StreamRotation;
63using ::aidl::android::hardware::camera::device::StreamType;
64using ::aidl::android::hardware::graphics::common::PixelFormat;
65
66namespace {
67
68using namespace std::chrono_literals;
69
70// Prefix of camera name - "device@1.1/virtual/{numerical_id}"
71const char* kDevicePathPrefix = "device@1.1/virtual/";
72
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +010073constexpr int32_t kMaxJpegSize = 3 * 1024 * 1024 /*3MiB*/;
74
Jan Sebechlebskybb01c1d2024-02-12 11:41:37 +010075constexpr int32_t kMinFps = 15;
76
77constexpr std::chrono::nanoseconds kMaxFrameDuration =
78 std::chrono::duration_cast<std::chrono::nanoseconds>(1e9ns / kMinFps);
79
80constexpr uint8_t kPipelineMaxDepth = 2;
81
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +010082constexpr MetadataBuilder::ControlRegion kDefaultEmptyControlRegion{};
83
Jan Sebechlebsky4ce32082024-02-14 16:02:11 +010084const std::array<Resolution, 5> kStandardJpegThumbnailSizes{
85 Resolution(176, 144), Resolution(240, 144), Resolution(256, 144),
86 Resolution(240, 160), Resolution(240, 180)};
87
Jan Sebechlebsky8ae23592024-02-02 16:08:18 +010088const std::array<PixelFormat, 3> kOutputFormats{
89 PixelFormat::IMPLEMENTATION_DEFINED, PixelFormat::YCBCR_420_888,
90 PixelFormat::BLOB};
Biswarup Pal6152a302023-12-19 12:44:09 +000091
Jan Sebechlebsky4c9bb1e2024-02-28 16:32:39 +010092// The resolutions below will used to extend the set of supported output formats.
93// All resolutions with lower pixel count and same aspect ratio as some supported
94// input resolution will be added to the set of supported output resolutions.
95const std::array<Resolution, 10> kOutputResolutions{
96 Resolution(320, 240), Resolution(640, 360), Resolution(640, 480),
97 Resolution(720, 480), Resolution(720, 576), Resolution(800, 600),
98 Resolution(1024, 576), Resolution(1280, 720), Resolution(1280, 960),
99 Resolution(1280, 1080),
100};
Jan Sebechlebsky4ce32082024-02-14 16:02:11 +0100101
102std::vector<Resolution> getSupportedJpegThumbnailSizes(
103 const std::vector<SupportedStreamConfiguration>& configs) {
104 auto isSupportedByAnyInputConfig =
105 [&configs](const Resolution thumbnailResolution) {
106 return std::any_of(
107 configs.begin(), configs.end(),
108 [thumbnailResolution](const SupportedStreamConfiguration& config) {
109 return isApproximatellySameAspectRatio(
110 thumbnailResolution, Resolution(config.width, config.height));
111 });
112 };
113
114 std::vector<Resolution> supportedThumbnailSizes({Resolution(0, 0)});
115 std::copy_if(kStandardJpegThumbnailSizes.begin(),
116 kStandardJpegThumbnailSizes.end(),
117 std::back_insert_iterator(supportedThumbnailSizes),
118 isSupportedByAnyInputConfig);
119 return supportedThumbnailSizes;
120}
121
Jan Sebechlebsky8ae23592024-02-02 16:08:18 +0100122bool isSupportedOutputFormat(const PixelFormat pixelFormat) {
123 return std::find(kOutputFormats.begin(), kOutputFormats.end(), pixelFormat) !=
124 kOutputFormats.end();
125}
126
Vadim Caen11dfd932024-03-05 09:57:20 +0100127std::vector<FpsRange> fpsRangesForInputConfig(
Jan Sebechlebskyc3e1a632024-02-06 14:19:05 +0100128 const std::vector<SupportedStreamConfiguration>& configs) {
Vadim Caen11dfd932024-03-05 09:57:20 +0100129 std::set<FpsRange> availableRanges;
Jan Sebechlebskybb01c1d2024-02-12 11:41:37 +0100130
Jan Sebechlebskyc3e1a632024-02-06 14:19:05 +0100131 for (const SupportedStreamConfiguration& config : configs) {
Jan Sebechlebskybb01c1d2024-02-12 11:41:37 +0100132 availableRanges.insert({.minFps = kMinFps, .maxFps = config.maxFps});
Jan Sebechlebskyc3e1a632024-02-06 14:19:05 +0100133 availableRanges.insert({.minFps = config.maxFps, .maxFps = config.maxFps});
134 }
135
Jan Sebechlebskybb01c1d2024-02-12 11:41:37 +0100136 if (std::any_of(configs.begin(), configs.end(),
137 [](const SupportedStreamConfiguration& config) {
138 return config.maxFps >= 30;
139 })) {
140 availableRanges.insert({.minFps = kMinFps, .maxFps = 30});
141 availableRanges.insert({.minFps = 30, .maxFps = 30});
142 }
143
Vadim Caen11dfd932024-03-05 09:57:20 +0100144 return std::vector<FpsRange>(availableRanges.begin(), availableRanges.end());
Jan Sebechlebskyc3e1a632024-02-06 14:19:05 +0100145}
146
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +0100147std::optional<Resolution> getMaxResolution(
148 const std::vector<SupportedStreamConfiguration>& configs) {
149 auto itMax = std::max_element(configs.begin(), configs.end(),
150 [](const SupportedStreamConfiguration& a,
151 const SupportedStreamConfiguration& b) {
152 return a.width * b.height < a.width * b.height;
153 });
154 if (itMax == configs.end()) {
155 ALOGE(
156 "%s: empty vector of supported configurations, cannot find largest "
157 "resolution.",
158 __func__);
159 return std::nullopt;
160 }
161
162 return Resolution(itMax->width, itMax->height);
163}
164
Biswarup Pal6152a302023-12-19 12:44:09 +0000165// Returns a map of unique resolution to maximum maxFps for all streams with
166// that resolution.
167std::map<Resolution, int> getResolutionToMaxFpsMap(
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +0100168 const std::vector<SupportedStreamConfiguration>& configs) {
Biswarup Pal6152a302023-12-19 12:44:09 +0000169 std::map<Resolution, int> resolutionToMaxFpsMap;
170
171 for (const SupportedStreamConfiguration& config : configs) {
172 Resolution resolution(config.width, config.height);
173 if (resolutionToMaxFpsMap.find(resolution) == resolutionToMaxFpsMap.end()) {
174 resolutionToMaxFpsMap[resolution] = config.maxFps;
175 } else {
176 int currentMaxFps = resolutionToMaxFpsMap[resolution];
177 resolutionToMaxFpsMap[resolution] = std::max(currentMaxFps, config.maxFps);
178 }
179 }
180
Jan Sebechlebsky4c9bb1e2024-02-28 16:32:39 +0100181 std::map<Resolution, int> additionalResolutionToMaxFpsMap;
182 // Add additional resolutions we can support by downscaling input streams with
183 // same aspect ratio.
184 for (const Resolution& outputResolution : kOutputResolutions) {
185 for (const auto& [resolution, maxFps] : resolutionToMaxFpsMap) {
186 if (resolutionToMaxFpsMap.find(outputResolution) !=
187 resolutionToMaxFpsMap.end()) {
188 // Resolution is already in the map, skip it.
189 continue;
190 }
191
192 if (outputResolution < resolution &&
193 isApproximatellySameAspectRatio(outputResolution, resolution)) {
194 // Lower resolution with same aspect ratio, we can achieve this by
195 // downscaling, let's add it to the map.
196 ALOGD(
197 "Extending set of output resolutions with %dx%d which has same "
198 "aspect ratio as supported input %dx%d.",
199 outputResolution.width, outputResolution.height, resolution.width,
200 resolution.height);
201 additionalResolutionToMaxFpsMap[outputResolution] = maxFps;
202 break;
203 }
204 }
205 }
206
207 // Add all resolution we can achieve by downscaling to the map.
208 resolutionToMaxFpsMap.insert(additionalResolutionToMaxFpsMap.begin(),
209 additionalResolutionToMaxFpsMap.end());
210
Biswarup Pal6152a302023-12-19 12:44:09 +0000211 return resolutionToMaxFpsMap;
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +0100212}
213
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100214// TODO(b/301023410) - Populate camera characteristics according to camera configuration.
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +0100215std::optional<CameraMetadata> initCameraCharacteristics(
Biswarup Pal6152a302023-12-19 12:44:09 +0000216 const std::vector<SupportedStreamConfiguration>& supportedInputConfig,
Biswarup Pal37a75182024-01-16 15:53:35 +0000217 const SensorOrientation sensorOrientation, const LensFacing lensFacing,
218 const int32_t deviceId) {
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +0100219 if (!std::all_of(supportedInputConfig.begin(), supportedInputConfig.end(),
220 [](const SupportedStreamConfiguration& config) {
Jan Sebechlebsky042d1fb2023-12-12 16:37:00 +0100221 return isFormatSupportedForInput(
Biswarup Pal6152a302023-12-19 12:44:09 +0000222 config.width, config.height, config.pixelFormat,
223 config.maxFps);
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +0100224 })) {
Jan Sebechlebsky042d1fb2023-12-12 16:37:00 +0100225 ALOGE("%s: input configuration contains unsupported format", __func__);
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +0100226 return std::nullopt;
227 }
228
229 MetadataBuilder builder =
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100230 MetadataBuilder()
231 .setSupportedHardwareLevel(
232 ANDROID_INFO_SUPPORTED_HARDWARE_LEVEL_EXTERNAL)
Biswarup Pal37a75182024-01-16 15:53:35 +0000233 .setDeviceId(deviceId)
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100234 .setFlashAvailable(false)
Biswarup Pal112458f2023-12-28 19:50:17 +0000235 .setLensFacing(
236 static_cast<camera_metadata_enum_android_lens_facing>(lensFacing))
Biswarup Pal8ad8bc52024-02-08 13:41:44 +0000237 .setAvailableFocalLengths({VirtualCameraDevice::kFocalLength})
Biswarup Pal6152a302023-12-19 12:44:09 +0000238 .setSensorOrientation(static_cast<int32_t>(sensorOrientation))
Jan Sebechlebskya984ffb2024-02-01 09:12:37 +0100239 .setSensorReadoutTimestamp(
240 ANDROID_SENSOR_READOUT_TIMESTAMP_NOT_SUPPORTED)
Jan Sebechlebskyad8d35f2024-02-05 11:58:59 +0100241 .setSensorTimestampSource(ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE_UNKNOWN)
Biswarup Pald9be04d2024-01-31 14:35:15 +0000242 .setSensorPhysicalSize(36.0, 24.0)
Jan Sebechlebskyab378202024-02-26 10:50:42 +0100243 .setAvailableAberrationCorrectionModes(
244 {ANDROID_COLOR_CORRECTION_ABERRATION_MODE_OFF})
245 .setAvailableNoiseReductionModes({ANDROID_NOISE_REDUCTION_MODE_OFF})
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100246 .setAvailableFaceDetectModes({ANDROID_STATISTICS_FACE_DETECT_MODE_OFF})
Vadim Caen51d1f5c2024-04-09 16:19:08 +0200247 .setAvailableStreamUseCases(
248 {ANDROID_SCALER_AVAILABLE_STREAM_USE_CASES_DEFAULT,
249 ANDROID_SCALER_AVAILABLE_STREAM_USE_CASES_PREVIEW,
250 ANDROID_SCALER_AVAILABLE_STREAM_USE_CASES_STILL_CAPTURE,
251 ANDROID_SCALER_AVAILABLE_STREAM_USE_CASES_VIDEO_RECORD,
252 ANDROID_SCALER_AVAILABLE_STREAM_USE_CASES_PREVIEW_VIDEO_STILL,
253 ANDROID_SCALER_AVAILABLE_STREAM_USE_CASES_VIDEO_CALL})
Jan Sebechlebskybb01c1d2024-02-12 11:41:37 +0100254 .setAvailableTestPatternModes({ANDROID_SENSOR_TEST_PATTERN_MODE_OFF})
Jan Sebechlebskyb0119fa2023-12-04 10:29:06 +0100255 .setAvailableMaxDigitalZoom(1.0)
256 .setControlAvailableModes({ANDROID_CONTROL_MODE_AUTO})
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100257 .setControlAfAvailableModes({ANDROID_CONTROL_AF_MODE_OFF})
Jan Sebechlebsky4425a732024-01-31 11:31:54 +0100258 .setControlAvailableSceneModes({ANDROID_CONTROL_SCENE_MODE_DISABLED})
259 .setControlAvailableEffects({ANDROID_CONTROL_EFFECT_MODE_OFF})
Jan Sebechlebskybb01c1d2024-02-12 11:41:37 +0100260 .setControlAvailableVideoStabilizationModes(
261 {ANDROID_CONTROL_VIDEO_STABILIZATION_MODE_OFF})
Jan Sebechlebskyc3e1a632024-02-06 14:19:05 +0100262 .setControlAeAvailableModes({ANDROID_CONTROL_AE_MODE_ON})
263 .setControlAeAvailableAntibandingModes(
264 {ANDROID_CONTROL_AE_ANTIBANDING_MODE_AUTO})
265 .setControlAeAvailableFpsRanges(
266 fpsRangesForInputConfig(supportedInputConfig))
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100267 .setControlMaxRegions(0, 0, 0)
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100268 .setControlAfRegions({kDefaultEmptyControlRegion})
269 .setControlAeRegions({kDefaultEmptyControlRegion})
270 .setControlAwbRegions({kDefaultEmptyControlRegion})
Jan Sebechlebskyc3e1a632024-02-06 14:19:05 +0100271 .setControlAeCompensationRange(0, 0)
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100272 .setControlAeCompensationStep(camera_metadata_rational_t{0, 1})
Jan Sebechlebsky4425a732024-01-31 11:31:54 +0100273 .setControlAwbLockAvailable(false)
274 .setControlAeLockAvailable(false)
275 .setControlAvailableAwbModes({ANDROID_CONTROL_AWB_MODE_AUTO})
Jan Sebechlebsky6ab07fe2023-12-05 15:23:34 +0100276 .setControlZoomRatioRange(/*min=*/1.0, /*max=*/1.0)
Jan Sebechlebskyab378202024-02-26 10:50:42 +0100277 .setCroppingType(ANDROID_SCALER_CROPPING_TYPE_CENTER_ONLY)
Jan Sebechlebsky4ce32082024-02-14 16:02:11 +0100278 .setJpegAvailableThumbnailSizes(
279 getSupportedJpegThumbnailSizes(supportedInputConfig))
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100280 .setMaxJpegSize(kMaxJpegSize)
Jan Sebechlebskyab378202024-02-26 10:50:42 +0100281 .setMaxFaceCount(0)
Jan Sebechlebskybb01c1d2024-02-12 11:41:37 +0100282 .setMaxFrameDuration(kMaxFrameDuration)
Jan Sebechlebsky8ae23592024-02-02 16:08:18 +0100283 .setMaxNumberOutputStreams(
284 VirtualCameraDevice::kMaxNumberOfRawStreams,
285 VirtualCameraDevice::kMaxNumberOfProcessedStreams,
286 VirtualCameraDevice::kMaxNumberOfStallStreams)
Jan Sebechlebskyab378202024-02-26 10:50:42 +0100287 .setRequestPartialResultCount(1)
Jan Sebechlebskybb01c1d2024-02-12 11:41:37 +0100288 .setPipelineMaxDepth(kPipelineMaxDepth)
Jan Sebechlebsky4425a732024-01-31 11:31:54 +0100289 .setSyncMaxLatency(ANDROID_SYNC_MAX_LATENCY_UNKNOWN)
Jan Sebechlebsky4be2bd02024-02-26 18:35:18 +0100290 .setAvailableRequestKeys({ANDROID_COLOR_CORRECTION_ABERRATION_MODE,
291 ANDROID_CONTROL_CAPTURE_INTENT,
Jan Sebechlebsky4ce32082024-02-14 16:02:11 +0100292 ANDROID_CONTROL_AE_MODE,
293 ANDROID_CONTROL_AE_EXPOSURE_COMPENSATION,
294 ANDROID_CONTROL_AE_TARGET_FPS_RANGE,
295 ANDROID_CONTROL_AE_ANTIBANDING_MODE,
296 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
297 ANDROID_CONTROL_AF_TRIGGER,
298 ANDROID_CONTROL_AF_MODE,
299 ANDROID_CONTROL_AWB_MODE,
300 ANDROID_SCALER_CROP_REGION,
301 ANDROID_CONTROL_EFFECT_MODE,
302 ANDROID_CONTROL_MODE,
303 ANDROID_CONTROL_SCENE_MODE,
304 ANDROID_CONTROL_VIDEO_STABILIZATION_MODE,
305 ANDROID_CONTROL_ZOOM_RATIO,
Jan Sebechlebsky4ce32082024-02-14 16:02:11 +0100306 ANDROID_FLASH_MODE,
307 ANDROID_JPEG_AVAILABLE_THUMBNAIL_SIZES,
Vadim Caend86a3a42024-03-14 16:38:11 +0100308 ANDROID_JPEG_ORIENTATION,
Jan Sebechlebsky4ce32082024-02-14 16:02:11 +0100309 ANDROID_JPEG_QUALITY,
Jan Sebechlebsky4be2bd02024-02-26 18:35:18 +0100310 ANDROID_JPEG_THUMBNAIL_QUALITY,
Vadim Caend86a3a42024-03-14 16:38:11 +0100311 ANDROID_JPEG_THUMBNAIL_SIZE,
Jan Sebechlebsky4be2bd02024-02-26 18:35:18 +0100312 ANDROID_NOISE_REDUCTION_MODE,
313 ANDROID_STATISTICS_FACE_DETECT_MODE})
Vadim Caen11dfd932024-03-05 09:57:20 +0100314 .setAvailableResultKeys({
315 ANDROID_COLOR_CORRECTION_ABERRATION_MODE,
316 ANDROID_CONTROL_AE_ANTIBANDING_MODE,
317 ANDROID_CONTROL_AE_EXPOSURE_COMPENSATION,
318 ANDROID_CONTROL_AE_LOCK,
319 ANDROID_CONTROL_AE_MODE,
320 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
321 ANDROID_CONTROL_AE_STATE,
322 ANDROID_CONTROL_AE_TARGET_FPS_RANGE,
323 ANDROID_CONTROL_AF_MODE,
324 ANDROID_CONTROL_AF_STATE,
325 ANDROID_CONTROL_AF_TRIGGER,
326 ANDROID_CONTROL_AWB_LOCK,
327 ANDROID_CONTROL_AWB_MODE,
328 ANDROID_CONTROL_AWB_STATE,
329 ANDROID_CONTROL_CAPTURE_INTENT,
330 ANDROID_CONTROL_EFFECT_MODE,
331 ANDROID_CONTROL_MODE,
332 ANDROID_CONTROL_SCENE_MODE,
333 ANDROID_CONTROL_VIDEO_STABILIZATION_MODE,
334 ANDROID_STATISTICS_FACE_DETECT_MODE,
335 ANDROID_FLASH_MODE,
336 ANDROID_FLASH_STATE,
337 ANDROID_JPEG_AVAILABLE_THUMBNAIL_SIZES,
338 ANDROID_JPEG_QUALITY,
339 ANDROID_JPEG_THUMBNAIL_QUALITY,
340 ANDROID_LENS_FOCAL_LENGTH,
341 ANDROID_LENS_OPTICAL_STABILIZATION_MODE,
342 ANDROID_NOISE_REDUCTION_MODE,
343 ANDROID_REQUEST_PIPELINE_DEPTH,
344 ANDROID_SENSOR_TIMESTAMP,
345 ANDROID_STATISTICS_HOT_PIXEL_MAP_MODE,
346 ANDROID_STATISTICS_LENS_SHADING_MAP_MODE,
347 ANDROID_STATISTICS_SCENE_FLICKER,
348 })
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100349 .setAvailableCapabilities(
Jan Sebechlebskyad8d35f2024-02-05 11:58:59 +0100350 {ANDROID_REQUEST_AVAILABLE_CAPABILITIES_BACKWARD_COMPATIBLE});
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100351
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +0100352 // Active array size must correspond to largest supported input resolution.
353 std::optional<Resolution> maxResolution =
354 getMaxResolution(supportedInputConfig);
355 if (!maxResolution.has_value()) {
356 return std::nullopt;
357 }
358 builder.setSensorActiveArraySize(0, 0, maxResolution->width,
359 maxResolution->height);
Biswarup Pald9be04d2024-01-31 14:35:15 +0000360 builder.setSensorPixelArraySize(maxResolution->width, maxResolution->height);
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +0100361
362 std::vector<MetadataBuilder::StreamConfiguration> outputConfigurations;
363
364 // TODO(b/301023410) Add also all "standard" resolutions we can rescale the
365 // streams to (all standard resolutions with same aspect ratio).
366
Biswarup Pal6152a302023-12-19 12:44:09 +0000367 std::map<Resolution, int> resolutionToMaxFpsMap =
368 getResolutionToMaxFpsMap(supportedInputConfig);
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +0100369
Biswarup Pal6152a302023-12-19 12:44:09 +0000370 // Add configurations for all unique input resolutions and output formats.
Jan Sebechlebsky8ae23592024-02-02 16:08:18 +0100371 for (const PixelFormat format : kOutputFormats) {
Biswarup Pal6152a302023-12-19 12:44:09 +0000372 std::transform(
373 resolutionToMaxFpsMap.begin(), resolutionToMaxFpsMap.end(),
374 std::back_inserter(outputConfigurations), [format](const auto& entry) {
375 Resolution resolution = entry.first;
376 int maxFps = entry.second;
377 return MetadataBuilder::StreamConfiguration{
378 .width = resolution.width,
379 .height = resolution.height,
Jan Sebechlebsky8ae23592024-02-02 16:08:18 +0100380 .format = static_cast<int32_t>(format),
Biswarup Pal6152a302023-12-19 12:44:09 +0000381 .minFrameDuration = std::chrono::nanoseconds(1s) / maxFps,
382 .minStallDuration = 0s};
383 });
384 }
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +0100385
386 ALOGV("Adding %zu output configurations", outputConfigurations.size());
387 builder.setAvailableOutputStreamConfigurations(outputConfigurations);
388
Jan Sebechlebskyad8d35f2024-02-05 11:58:59 +0100389 auto metadata = builder.setAvailableCharacteristicKeys().build();
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100390 if (metadata == nullptr) {
391 ALOGE("Failed to build metadata!");
392 return CameraMetadata();
393 }
394
395 return std::move(*metadata);
396}
397
398} // namespace
399
400VirtualCameraDevice::VirtualCameraDevice(
Biswarup Pal37a75182024-01-16 15:53:35 +0000401 const uint32_t cameraId, const VirtualCameraConfiguration& configuration,
402 int32_t deviceId)
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100403 : mCameraId(cameraId),
Biswarup Pal6152a302023-12-19 12:44:09 +0000404 mVirtualCameraClientCallback(configuration.virtualCameraCallback),
405 mSupportedInputConfigurations(configuration.supportedStreamConfigs) {
406 std::optional<CameraMetadata> metadata = initCameraCharacteristics(
Biswarup Pal112458f2023-12-28 19:50:17 +0000407 mSupportedInputConfigurations, configuration.sensorOrientation,
Biswarup Pal37a75182024-01-16 15:53:35 +0000408 configuration.lensFacing, deviceId);
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +0100409 if (metadata.has_value()) {
410 mCameraCharacteristics = *metadata;
411 } else {
412 ALOGE(
413 "%s: Failed to initialize camera characteristic based on provided "
414 "configuration.",
415 __func__);
416 }
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100417}
418
419ndk::ScopedAStatus VirtualCameraDevice::getCameraCharacteristics(
420 CameraMetadata* _aidl_return) {
421 ALOGV("%s", __func__);
422 if (_aidl_return == nullptr) {
423 return cameraStatus(Status::ILLEGAL_ARGUMENT);
424 }
425
426 *_aidl_return = mCameraCharacteristics;
427 return ndk::ScopedAStatus::ok();
428}
429
430ndk::ScopedAStatus VirtualCameraDevice::getPhysicalCameraCharacteristics(
431 const std::string& in_physicalCameraId, CameraMetadata* _aidl_return) {
432 ALOGV("%s: physicalCameraId %s", __func__, in_physicalCameraId.c_str());
433 (void)_aidl_return;
434
435 // VTS tests expect this call to fail with illegal argument status for
436 // all publicly advertised camera ids.
437 // Because we don't support physical camera ids, we just always
438 // fail with illegal argument (there's no valid argument to provide).
439 return cameraStatus(Status::ILLEGAL_ARGUMENT);
440}
441
442ndk::ScopedAStatus VirtualCameraDevice::getResourceCost(
443 CameraResourceCost* _aidl_return) {
444 ALOGV("%s", __func__);
445 if (_aidl_return == nullptr) {
446 return cameraStatus(Status::ILLEGAL_ARGUMENT);
447 }
448 _aidl_return->resourceCost = 100; // ¯\_(ツ)_/¯
449 return ndk::ScopedAStatus::ok();
450}
451
452ndk::ScopedAStatus VirtualCameraDevice::isStreamCombinationSupported(
453 const StreamConfiguration& in_streams, bool* _aidl_return) {
454 ALOGV("%s", __func__);
455
456 if (_aidl_return == nullptr) {
457 return cameraStatus(Status::ILLEGAL_ARGUMENT);
458 }
459
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +0100460 *_aidl_return = isStreamCombinationSupported(in_streams);
461 return ndk::ScopedAStatus::ok();
462};
463
464bool VirtualCameraDevice::isStreamCombinationSupported(
465 const StreamConfiguration& streamConfiguration) const {
Jan Sebechlebsky39129f82024-01-19 16:42:11 +0100466 if (streamConfiguration.streams.empty()) {
467 ALOGE("%s: Querying empty configuration", __func__);
468 return false;
469 }
470
Jan Sebechlebsky4c9bb1e2024-02-28 16:32:39 +0100471 const std::vector<Stream>& streams = streamConfiguration.streams;
472
473 Resolution firstStreamResolution(streams[0].width, streams[0].height);
474 auto isSameAspectRatioAsFirst = [firstStreamResolution](const Stream& stream) {
475 return isApproximatellySameAspectRatio(
476 firstStreamResolution, Resolution(stream.width, stream.height));
477 };
478 if (!std::all_of(streams.begin(), streams.end(), isSameAspectRatioAsFirst)) {
479 ALOGW(
480 "%s: Requested streams do not have same aspect ratio. Different aspect "
481 "ratios are currently "
482 "not supported by virtual camera. Stream configuration: %s",
483 __func__, streamConfiguration.toString().c_str());
484 return false;
485 }
486
Jan Sebechlebsky8ae23592024-02-02 16:08:18 +0100487 int numberOfProcessedStreams = 0;
488 int numberOfStallStreams = 0;
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +0100489 for (const Stream& stream : streamConfiguration.streams) {
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100490 ALOGV("%s: Configuration queried: %s", __func__, stream.toString().c_str());
491
492 if (stream.streamType == StreamType::INPUT) {
493 ALOGW("%s: Input stream type is not supported", __func__);
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +0100494 return false;
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100495 }
496
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +0100497 if (stream.rotation != StreamRotation::ROTATION_0 ||
Jan Sebechlebsky8ae23592024-02-02 16:08:18 +0100498 !isSupportedOutputFormat(stream.format)) {
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +0100499 ALOGV("Unsupported output stream type");
500 return false;
501 }
502
Jan Sebechlebsky8ae23592024-02-02 16:08:18 +0100503 if (stream.format == PixelFormat::BLOB) {
504 numberOfStallStreams++;
505 } else {
506 numberOfProcessedStreams++;
507 }
508
Jan Sebechlebsky4c9bb1e2024-02-28 16:32:39 +0100509 Resolution requestedResolution(stream.width, stream.height);
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +0100510 auto matchesSupportedInputConfig =
Jan Sebechlebsky4c9bb1e2024-02-28 16:32:39 +0100511 [requestedResolution](const SupportedStreamConfiguration& config) {
512 Resolution supportedInputResolution(config.width, config.height);
513 return requestedResolution <= supportedInputResolution &&
514 isApproximatellySameAspectRatio(requestedResolution,
515 supportedInputResolution);
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +0100516 };
517 if (std::none_of(mSupportedInputConfigurations.begin(),
518 mSupportedInputConfigurations.end(),
519 matchesSupportedInputConfig)) {
520 ALOGV("Requested config doesn't match any supported input config");
521 return false;
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100522 }
523 }
Jan Sebechlebsky8ae23592024-02-02 16:08:18 +0100524
525 if (numberOfProcessedStreams > kMaxNumberOfProcessedStreams) {
526 ALOGE("%s: %d processed streams exceeds the supported maximum of %d",
527 __func__, numberOfProcessedStreams, kMaxNumberOfProcessedStreams);
528 return false;
529 }
530
531 if (numberOfStallStreams > kMaxNumberOfStallStreams) {
532 ALOGE("%s: %d stall streams exceeds the supported maximum of %d", __func__,
533 numberOfStallStreams, kMaxNumberOfStallStreams);
534 return false;
535 }
536
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +0100537 return true;
538}
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100539
540ndk::ScopedAStatus VirtualCameraDevice::open(
541 const std::shared_ptr<ICameraDeviceCallback>& in_callback,
542 std::shared_ptr<ICameraDeviceSession>* _aidl_return) {
543 ALOGV("%s", __func__);
544
545 *_aidl_return = ndk::SharedRefBase::make<VirtualCameraSession>(
Jan Sebechlebsky0bb5e092023-12-08 16:17:54 +0100546 sharedFromThis(), in_callback, mVirtualCameraClientCallback);
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100547
548 return ndk::ScopedAStatus::ok();
549};
550
551ndk::ScopedAStatus VirtualCameraDevice::openInjectionSession(
552 const std::shared_ptr<ICameraDeviceCallback>& in_callback,
553 std::shared_ptr<ICameraInjectionSession>* _aidl_return) {
554 ALOGV("%s", __func__);
555
556 (void)in_callback;
557 (void)_aidl_return;
558 return cameraStatus(Status::OPERATION_NOT_SUPPORTED);
559}
560
561ndk::ScopedAStatus VirtualCameraDevice::setTorchMode(bool in_on) {
562 ALOGV("%s: on = %s", __func__, in_on ? "on" : "off");
563 return cameraStatus(Status::OPERATION_NOT_SUPPORTED);
564}
565
566ndk::ScopedAStatus VirtualCameraDevice::turnOnTorchWithStrengthLevel(
567 int32_t in_torchStrength) {
568 ALOGV("%s: torchStrength = %d", __func__, in_torchStrength);
569 return cameraStatus(Status::OPERATION_NOT_SUPPORTED);
570}
571
572ndk::ScopedAStatus VirtualCameraDevice::getTorchStrengthLevel(
573 int32_t* _aidl_return) {
574 (void)_aidl_return;
575 return cameraStatus(Status::OPERATION_NOT_SUPPORTED);
576}
577
578binder_status_t VirtualCameraDevice::dump(int fd, const char** args,
579 uint32_t numArgs) {
580 // TODO(b/301023410) Implement.
581 (void)fd;
582 (void)args;
583 (void)numArgs;
584 return STATUS_OK;
585}
586
587std::string VirtualCameraDevice::getCameraName() const {
588 return std::string(kDevicePathPrefix) + std::to_string(mCameraId);
589}
590
Jan Sebechlebskyc3e1a632024-02-06 14:19:05 +0100591const std::vector<SupportedStreamConfiguration>&
592VirtualCameraDevice::getInputConfigs() const {
593 return mSupportedInputConfigurations;
594}
595
Jan Sebechlebskybb01c1d2024-02-12 11:41:37 +0100596Resolution VirtualCameraDevice::getMaxInputResolution() const {
597 std::optional<Resolution> maxResolution =
598 getMaxResolution(mSupportedInputConfigurations);
599 if (!maxResolution.has_value()) {
600 ALOGE(
601 "%s: Cannot determine sensor size for virtual camera - input "
602 "configurations empty?",
603 __func__);
604 return Resolution(0, 0);
605 }
606 return maxResolution.value();
607}
608
Jan Sebechlebsky0bb5e092023-12-08 16:17:54 +0100609std::shared_ptr<VirtualCameraDevice> VirtualCameraDevice::sharedFromThis() {
610 // SharedRefBase which BnCameraDevice inherits from breaks
611 // std::enable_shared_from_this. This is recommended replacement for
612 // shared_from_this() per documentation in binder_interface_utils.h.
613 return ref<VirtualCameraDevice>();
614}
615
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100616} // namespace virtualcamera
617} // namespace companion
618} // namespace android