blob: ed1e92c527cb5dfc965bba95b3447cb2a413e30b [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 +010084constexpr float kAspectRatioEpsilon = 0.05;
85
86const std::array<Resolution, 5> kStandardJpegThumbnailSizes{
87 Resolution(176, 144), Resolution(240, 144), Resolution(256, 144),
88 Resolution(240, 160), Resolution(240, 180)};
89
Jan Sebechlebsky8ae23592024-02-02 16:08:18 +010090const std::array<PixelFormat, 3> kOutputFormats{
91 PixelFormat::IMPLEMENTATION_DEFINED, PixelFormat::YCBCR_420_888,
92 PixelFormat::BLOB};
Biswarup Pal6152a302023-12-19 12:44:09 +000093
Jan Sebechlebsky4ce32082024-02-14 16:02:11 +010094bool 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
103std::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 Sebechlebsky8ae23592024-02-02 16:08:18 +0100123bool isSupportedOutputFormat(const PixelFormat pixelFormat) {
124 return std::find(kOutputFormats.begin(), kOutputFormats.end(), pixelFormat) !=
125 kOutputFormats.end();
126}
127
Jan Sebechlebskyc3e1a632024-02-06 14:19:05 +0100128std::vector<MetadataBuilder::FpsRange> fpsRangesForInputConfig(
129 const std::vector<SupportedStreamConfiguration>& configs) {
130 std::set<MetadataBuilder::FpsRange> availableRanges;
Jan Sebechlebskybb01c1d2024-02-12 11:41:37 +0100131
Jan Sebechlebskyc3e1a632024-02-06 14:19:05 +0100132 for (const SupportedStreamConfiguration& config : configs) {
Jan Sebechlebskybb01c1d2024-02-12 11:41:37 +0100133 availableRanges.insert({.minFps = kMinFps, .maxFps = config.maxFps});
Jan Sebechlebskyc3e1a632024-02-06 14:19:05 +0100134 availableRanges.insert({.minFps = config.maxFps, .maxFps = config.maxFps});
135 }
136
Jan Sebechlebskybb01c1d2024-02-12 11:41:37 +0100137 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 Sebechlebskyc3e1a632024-02-06 14:19:05 +0100145 return std::vector<MetadataBuilder::FpsRange>(availableRanges.begin(),
146 availableRanges.end());
147}
148
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +0100149std::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 Pal6152a302023-12-19 12:44:09 +0000167// Returns a map of unique resolution to maximum maxFps for all streams with
168// that resolution.
169std::map<Resolution, int> getResolutionToMaxFpsMap(
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +0100170 const std::vector<SupportedStreamConfiguration>& configs) {
Biswarup Pal6152a302023-12-19 12:44:09 +0000171 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 Sebechlebsky3b478c42023-11-23 13:15:56 +0100184}
185
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100186// TODO(b/301023410) - Populate camera characteristics according to camera configuration.
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +0100187std::optional<CameraMetadata> initCameraCharacteristics(
Biswarup Pal6152a302023-12-19 12:44:09 +0000188 const std::vector<SupportedStreamConfiguration>& supportedInputConfig,
Biswarup Pal112458f2023-12-28 19:50:17 +0000189 const SensorOrientation sensorOrientation, const LensFacing lensFacing) {
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +0100190 if (!std::all_of(supportedInputConfig.begin(), supportedInputConfig.end(),
191 [](const SupportedStreamConfiguration& config) {
Jan Sebechlebsky042d1fb2023-12-12 16:37:00 +0100192 return isFormatSupportedForInput(
Biswarup Pal6152a302023-12-19 12:44:09 +0000193 config.width, config.height, config.pixelFormat,
194 config.maxFps);
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +0100195 })) {
Jan Sebechlebsky042d1fb2023-12-12 16:37:00 +0100196 ALOGE("%s: input configuration contains unsupported format", __func__);
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +0100197 return std::nullopt;
198 }
199
200 MetadataBuilder builder =
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100201 MetadataBuilder()
202 .setSupportedHardwareLevel(
203 ANDROID_INFO_SUPPORTED_HARDWARE_LEVEL_EXTERNAL)
204 .setFlashAvailable(false)
Biswarup Pal112458f2023-12-28 19:50:17 +0000205 .setLensFacing(
206 static_cast<camera_metadata_enum_android_lens_facing>(lensFacing))
Biswarup Pal8ad8bc52024-02-08 13:41:44 +0000207 .setAvailableFocalLengths({VirtualCameraDevice::kFocalLength})
Biswarup Pal6152a302023-12-19 12:44:09 +0000208 .setSensorOrientation(static_cast<int32_t>(sensorOrientation))
Jan Sebechlebskya984ffb2024-02-01 09:12:37 +0100209 .setSensorReadoutTimestamp(
210 ANDROID_SENSOR_READOUT_TIMESTAMP_NOT_SUPPORTED)
Jan Sebechlebskyad8d35f2024-02-05 11:58:59 +0100211 .setSensorTimestampSource(ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE_UNKNOWN)
Biswarup Pald9be04d2024-01-31 14:35:15 +0000212 .setSensorPhysicalSize(36.0, 24.0)
Jan Sebechlebskyab378202024-02-26 10:50:42 +0100213 .setAvailableAberrationCorrectionModes(
214 {ANDROID_COLOR_CORRECTION_ABERRATION_MODE_OFF})
215 .setAvailableNoiseReductionModes({ANDROID_NOISE_REDUCTION_MODE_OFF})
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100216 .setAvailableFaceDetectModes({ANDROID_STATISTICS_FACE_DETECT_MODE_OFF})
Jan Sebechlebskybb01c1d2024-02-12 11:41:37 +0100217 .setAvailableTestPatternModes({ANDROID_SENSOR_TEST_PATTERN_MODE_OFF})
Jan Sebechlebskyb0119fa2023-12-04 10:29:06 +0100218 .setAvailableMaxDigitalZoom(1.0)
219 .setControlAvailableModes({ANDROID_CONTROL_MODE_AUTO})
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100220 .setControlAfAvailableModes({ANDROID_CONTROL_AF_MODE_OFF})
Jan Sebechlebsky4425a732024-01-31 11:31:54 +0100221 .setControlAvailableSceneModes({ANDROID_CONTROL_SCENE_MODE_DISABLED})
222 .setControlAvailableEffects({ANDROID_CONTROL_EFFECT_MODE_OFF})
Jan Sebechlebskybb01c1d2024-02-12 11:41:37 +0100223 .setControlAvailableVideoStabilizationModes(
224 {ANDROID_CONTROL_VIDEO_STABILIZATION_MODE_OFF})
Jan Sebechlebskyc3e1a632024-02-06 14:19:05 +0100225 .setControlAeAvailableModes({ANDROID_CONTROL_AE_MODE_ON})
226 .setControlAeAvailableAntibandingModes(
227 {ANDROID_CONTROL_AE_ANTIBANDING_MODE_AUTO})
228 .setControlAeAvailableFpsRanges(
229 fpsRangesForInputConfig(supportedInputConfig))
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100230 .setControlMaxRegions(0, 0, 0)
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100231 .setControlAfRegions({kDefaultEmptyControlRegion})
232 .setControlAeRegions({kDefaultEmptyControlRegion})
233 .setControlAwbRegions({kDefaultEmptyControlRegion})
Jan Sebechlebskyc3e1a632024-02-06 14:19:05 +0100234 .setControlAeCompensationRange(0, 0)
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100235 .setControlAeCompensationStep(camera_metadata_rational_t{0, 1})
Jan Sebechlebsky4425a732024-01-31 11:31:54 +0100236 .setControlAwbLockAvailable(false)
237 .setControlAeLockAvailable(false)
238 .setControlAvailableAwbModes({ANDROID_CONTROL_AWB_MODE_AUTO})
Jan Sebechlebsky6ab07fe2023-12-05 15:23:34 +0100239 .setControlZoomRatioRange(/*min=*/1.0, /*max=*/1.0)
Jan Sebechlebskyab378202024-02-26 10:50:42 +0100240 .setCroppingType(ANDROID_SCALER_CROPPING_TYPE_CENTER_ONLY)
Jan Sebechlebsky4ce32082024-02-14 16:02:11 +0100241 .setJpegAvailableThumbnailSizes(
242 getSupportedJpegThumbnailSizes(supportedInputConfig))
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100243 .setMaxJpegSize(kMaxJpegSize)
Jan Sebechlebskyab378202024-02-26 10:50:42 +0100244 .setMaxFaceCount(0)
Jan Sebechlebskybb01c1d2024-02-12 11:41:37 +0100245 .setMaxFrameDuration(kMaxFrameDuration)
Jan Sebechlebsky8ae23592024-02-02 16:08:18 +0100246 .setMaxNumberOutputStreams(
247 VirtualCameraDevice::kMaxNumberOfRawStreams,
248 VirtualCameraDevice::kMaxNumberOfProcessedStreams,
249 VirtualCameraDevice::kMaxNumberOfStallStreams)
Jan Sebechlebskyab378202024-02-26 10:50:42 +0100250 .setRequestPartialResultCount(1)
Jan Sebechlebskybb01c1d2024-02-12 11:41:37 +0100251 .setPipelineMaxDepth(kPipelineMaxDepth)
Jan Sebechlebsky4425a732024-01-31 11:31:54 +0100252 .setSyncMaxLatency(ANDROID_SYNC_MAX_LATENCY_UNKNOWN)
Jan Sebechlebsky4ce32082024-02-14 16:02:11 +0100253 .setAvailableRequestKeys({ANDROID_CONTROL_CAPTURE_INTENT,
254 ANDROID_CONTROL_AE_MODE,
255 ANDROID_CONTROL_AE_EXPOSURE_COMPENSATION,
256 ANDROID_CONTROL_AE_TARGET_FPS_RANGE,
257 ANDROID_CONTROL_AE_ANTIBANDING_MODE,
258 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
259 ANDROID_CONTROL_AF_TRIGGER,
260 ANDROID_CONTROL_AF_MODE,
261 ANDROID_CONTROL_AWB_MODE,
262 ANDROID_SCALER_CROP_REGION,
263 ANDROID_CONTROL_EFFECT_MODE,
264 ANDROID_CONTROL_MODE,
265 ANDROID_CONTROL_SCENE_MODE,
266 ANDROID_CONTROL_VIDEO_STABILIZATION_MODE,
267 ANDROID_CONTROL_ZOOM_RATIO,
268 ANDROID_STATISTICS_FACE_DETECT_MODE,
269 ANDROID_FLASH_MODE,
270 ANDROID_JPEG_AVAILABLE_THUMBNAIL_SIZES,
271 ANDROID_JPEG_QUALITY,
272 ANDROID_JPEG_THUMBNAIL_QUALITY})
Jan Sebechlebskyc3e1a632024-02-06 14:19:05 +0100273 .setAvailableResultKeys(
274 {ANDROID_CONTROL_AE_MODE, ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
275 ANDROID_CONTROL_AF_MODE, ANDROID_CONTROL_AWB_MODE,
Jan Sebechlebskybb01c1d2024-02-12 11:41:37 +0100276 ANDROID_CONTROL_EFFECT_MODE, ANDROID_CONTROL_MODE,
277 ANDROID_FLASH_MODE, ANDROID_FLASH_STATE,
Jan Sebechlebsky4ce32082024-02-14 16:02:11 +0100278 ANDROID_JPEG_AVAILABLE_THUMBNAIL_SIZES, ANDROID_JPEG_QUALITY,
279 ANDROID_JPEG_THUMBNAIL_QUALITY, ANDROID_SENSOR_TIMESTAMP,
280 ANDROID_LENS_FOCAL_LENGTH})
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100281 .setAvailableCapabilities(
Jan Sebechlebskyad8d35f2024-02-05 11:58:59 +0100282 {ANDROID_REQUEST_AVAILABLE_CAPABILITIES_BACKWARD_COMPATIBLE});
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100283
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +0100284 // Active array size must correspond to largest supported input resolution.
285 std::optional<Resolution> maxResolution =
286 getMaxResolution(supportedInputConfig);
287 if (!maxResolution.has_value()) {
288 return std::nullopt;
289 }
290 builder.setSensorActiveArraySize(0, 0, maxResolution->width,
291 maxResolution->height);
Biswarup Pald9be04d2024-01-31 14:35:15 +0000292 builder.setSensorPixelArraySize(maxResolution->width, maxResolution->height);
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +0100293
294 std::vector<MetadataBuilder::StreamConfiguration> outputConfigurations;
295
296 // TODO(b/301023410) Add also all "standard" resolutions we can rescale the
297 // streams to (all standard resolutions with same aspect ratio).
298
Biswarup Pal6152a302023-12-19 12:44:09 +0000299 std::map<Resolution, int> resolutionToMaxFpsMap =
300 getResolutionToMaxFpsMap(supportedInputConfig);
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +0100301
Biswarup Pal6152a302023-12-19 12:44:09 +0000302 // Add configurations for all unique input resolutions and output formats.
Jan Sebechlebsky8ae23592024-02-02 16:08:18 +0100303 for (const PixelFormat format : kOutputFormats) {
Biswarup Pal6152a302023-12-19 12:44:09 +0000304 std::transform(
305 resolutionToMaxFpsMap.begin(), resolutionToMaxFpsMap.end(),
306 std::back_inserter(outputConfigurations), [format](const auto& entry) {
307 Resolution resolution = entry.first;
308 int maxFps = entry.second;
309 return MetadataBuilder::StreamConfiguration{
310 .width = resolution.width,
311 .height = resolution.height,
Jan Sebechlebsky8ae23592024-02-02 16:08:18 +0100312 .format = static_cast<int32_t>(format),
Biswarup Pal6152a302023-12-19 12:44:09 +0000313 .minFrameDuration = std::chrono::nanoseconds(1s) / maxFps,
314 .minStallDuration = 0s};
315 });
316 }
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +0100317
318 ALOGV("Adding %zu output configurations", outputConfigurations.size());
319 builder.setAvailableOutputStreamConfigurations(outputConfigurations);
320
Jan Sebechlebskyad8d35f2024-02-05 11:58:59 +0100321 auto metadata = builder.setAvailableCharacteristicKeys().build();
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100322 if (metadata == nullptr) {
323 ALOGE("Failed to build metadata!");
324 return CameraMetadata();
325 }
326
327 return std::move(*metadata);
328}
329
330} // namespace
331
332VirtualCameraDevice::VirtualCameraDevice(
Biswarup Pal6152a302023-12-19 12:44:09 +0000333 const uint32_t cameraId, const VirtualCameraConfiguration& configuration)
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100334 : mCameraId(cameraId),
Biswarup Pal6152a302023-12-19 12:44:09 +0000335 mVirtualCameraClientCallback(configuration.virtualCameraCallback),
336 mSupportedInputConfigurations(configuration.supportedStreamConfigs) {
337 std::optional<CameraMetadata> metadata = initCameraCharacteristics(
Biswarup Pal112458f2023-12-28 19:50:17 +0000338 mSupportedInputConfigurations, configuration.sensorOrientation,
339 configuration.lensFacing);
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +0100340 if (metadata.has_value()) {
341 mCameraCharacteristics = *metadata;
342 } else {
343 ALOGE(
344 "%s: Failed to initialize camera characteristic based on provided "
345 "configuration.",
346 __func__);
347 }
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100348}
349
350ndk::ScopedAStatus VirtualCameraDevice::getCameraCharacteristics(
351 CameraMetadata* _aidl_return) {
352 ALOGV("%s", __func__);
353 if (_aidl_return == nullptr) {
354 return cameraStatus(Status::ILLEGAL_ARGUMENT);
355 }
356
357 *_aidl_return = mCameraCharacteristics;
358 return ndk::ScopedAStatus::ok();
359}
360
361ndk::ScopedAStatus VirtualCameraDevice::getPhysicalCameraCharacteristics(
362 const std::string& in_physicalCameraId, CameraMetadata* _aidl_return) {
363 ALOGV("%s: physicalCameraId %s", __func__, in_physicalCameraId.c_str());
364 (void)_aidl_return;
365
366 // VTS tests expect this call to fail with illegal argument status for
367 // all publicly advertised camera ids.
368 // Because we don't support physical camera ids, we just always
369 // fail with illegal argument (there's no valid argument to provide).
370 return cameraStatus(Status::ILLEGAL_ARGUMENT);
371}
372
373ndk::ScopedAStatus VirtualCameraDevice::getResourceCost(
374 CameraResourceCost* _aidl_return) {
375 ALOGV("%s", __func__);
376 if (_aidl_return == nullptr) {
377 return cameraStatus(Status::ILLEGAL_ARGUMENT);
378 }
379 _aidl_return->resourceCost = 100; // ¯\_(ツ)_/¯
380 return ndk::ScopedAStatus::ok();
381}
382
383ndk::ScopedAStatus VirtualCameraDevice::isStreamCombinationSupported(
384 const StreamConfiguration& in_streams, bool* _aidl_return) {
385 ALOGV("%s", __func__);
386
387 if (_aidl_return == nullptr) {
388 return cameraStatus(Status::ILLEGAL_ARGUMENT);
389 }
390
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +0100391 *_aidl_return = isStreamCombinationSupported(in_streams);
392 return ndk::ScopedAStatus::ok();
393};
394
395bool VirtualCameraDevice::isStreamCombinationSupported(
396 const StreamConfiguration& streamConfiguration) const {
Jan Sebechlebsky39129f82024-01-19 16:42:11 +0100397 if (streamConfiguration.streams.empty()) {
398 ALOGE("%s: Querying empty configuration", __func__);
399 return false;
400 }
401
Jan Sebechlebsky8ae23592024-02-02 16:08:18 +0100402 int numberOfProcessedStreams = 0;
403 int numberOfStallStreams = 0;
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +0100404 for (const Stream& stream : streamConfiguration.streams) {
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100405 ALOGV("%s: Configuration queried: %s", __func__, stream.toString().c_str());
406
407 if (stream.streamType == StreamType::INPUT) {
408 ALOGW("%s: Input stream type is not supported", __func__);
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +0100409 return false;
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100410 }
411
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +0100412 if (stream.rotation != StreamRotation::ROTATION_0 ||
Jan Sebechlebsky8ae23592024-02-02 16:08:18 +0100413 !isSupportedOutputFormat(stream.format)) {
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +0100414 ALOGV("Unsupported output stream type");
415 return false;
416 }
417
Jan Sebechlebsky8ae23592024-02-02 16:08:18 +0100418 if (stream.format == PixelFormat::BLOB) {
419 numberOfStallStreams++;
420 } else {
421 numberOfProcessedStreams++;
422 }
423
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +0100424 auto matchesSupportedInputConfig =
425 [&stream](const SupportedStreamConfiguration& config) {
426 return stream.width == config.width && stream.height == config.height;
427 };
428 if (std::none_of(mSupportedInputConfigurations.begin(),
429 mSupportedInputConfigurations.end(),
430 matchesSupportedInputConfig)) {
431 ALOGV("Requested config doesn't match any supported input config");
432 return false;
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100433 }
434 }
Jan Sebechlebsky8ae23592024-02-02 16:08:18 +0100435
436 if (numberOfProcessedStreams > kMaxNumberOfProcessedStreams) {
437 ALOGE("%s: %d processed streams exceeds the supported maximum of %d",
438 __func__, numberOfProcessedStreams, kMaxNumberOfProcessedStreams);
439 return false;
440 }
441
442 if (numberOfStallStreams > kMaxNumberOfStallStreams) {
443 ALOGE("%s: %d stall streams exceeds the supported maximum of %d", __func__,
444 numberOfStallStreams, kMaxNumberOfStallStreams);
445 return false;
446 }
447
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +0100448 return true;
449}
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100450
451ndk::ScopedAStatus VirtualCameraDevice::open(
452 const std::shared_ptr<ICameraDeviceCallback>& in_callback,
453 std::shared_ptr<ICameraDeviceSession>* _aidl_return) {
454 ALOGV("%s", __func__);
455
456 *_aidl_return = ndk::SharedRefBase::make<VirtualCameraSession>(
Jan Sebechlebsky0bb5e092023-12-08 16:17:54 +0100457 sharedFromThis(), in_callback, mVirtualCameraClientCallback);
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100458
459 return ndk::ScopedAStatus::ok();
460};
461
462ndk::ScopedAStatus VirtualCameraDevice::openInjectionSession(
463 const std::shared_ptr<ICameraDeviceCallback>& in_callback,
464 std::shared_ptr<ICameraInjectionSession>* _aidl_return) {
465 ALOGV("%s", __func__);
466
467 (void)in_callback;
468 (void)_aidl_return;
469 return cameraStatus(Status::OPERATION_NOT_SUPPORTED);
470}
471
472ndk::ScopedAStatus VirtualCameraDevice::setTorchMode(bool in_on) {
473 ALOGV("%s: on = %s", __func__, in_on ? "on" : "off");
474 return cameraStatus(Status::OPERATION_NOT_SUPPORTED);
475}
476
477ndk::ScopedAStatus VirtualCameraDevice::turnOnTorchWithStrengthLevel(
478 int32_t in_torchStrength) {
479 ALOGV("%s: torchStrength = %d", __func__, in_torchStrength);
480 return cameraStatus(Status::OPERATION_NOT_SUPPORTED);
481}
482
483ndk::ScopedAStatus VirtualCameraDevice::getTorchStrengthLevel(
484 int32_t* _aidl_return) {
485 (void)_aidl_return;
486 return cameraStatus(Status::OPERATION_NOT_SUPPORTED);
487}
488
489binder_status_t VirtualCameraDevice::dump(int fd, const char** args,
490 uint32_t numArgs) {
491 // TODO(b/301023410) Implement.
492 (void)fd;
493 (void)args;
494 (void)numArgs;
495 return STATUS_OK;
496}
497
498std::string VirtualCameraDevice::getCameraName() const {
499 return std::string(kDevicePathPrefix) + std::to_string(mCameraId);
500}
501
Jan Sebechlebskyc3e1a632024-02-06 14:19:05 +0100502const std::vector<SupportedStreamConfiguration>&
503VirtualCameraDevice::getInputConfigs() const {
504 return mSupportedInputConfigurations;
505}
506
Jan Sebechlebskybb01c1d2024-02-12 11:41:37 +0100507Resolution VirtualCameraDevice::getMaxInputResolution() const {
508 std::optional<Resolution> maxResolution =
509 getMaxResolution(mSupportedInputConfigurations);
510 if (!maxResolution.has_value()) {
511 ALOGE(
512 "%s: Cannot determine sensor size for virtual camera - input "
513 "configurations empty?",
514 __func__);
515 return Resolution(0, 0);
516 }
517 return maxResolution.value();
518}
519
Jan Sebechlebsky0bb5e092023-12-08 16:17:54 +0100520std::shared_ptr<VirtualCameraDevice> VirtualCameraDevice::sharedFromThis() {
521 // SharedRefBase which BnCameraDevice inherits from breaks
522 // std::enable_shared_from_this. This is recommended replacement for
523 // shared_from_this() per documentation in binder_interface_utils.h.
524 return ref<VirtualCameraDevice>();
525}
526
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100527} // namespace virtualcamera
528} // namespace companion
529} // namespace android