blob: 55d4c69cbcc1e90fc74733b227e4dd580d43182f [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>
26#include <optional>
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +010027#include <string>
28
29#include "VirtualCameraSession.h"
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +010030#include "aidl/android/companion/virtualcamera/SupportedStreamConfiguration.h"
Biswarup Pal6152a302023-12-19 12:44:09 +000031#include "aidl/android/companion/virtualcamera/VirtualCameraConfiguration.h"
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +010032#include "aidl/android/hardware/camera/common/Status.h"
33#include "aidl/android/hardware/camera/device/CameraMetadata.h"
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +010034#include "aidl/android/hardware/camera/device/StreamConfiguration.h"
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +010035#include "android/binder_auto_utils.h"
36#include "android/binder_status.h"
37#include "log/log.h"
38#include "system/camera_metadata.h"
39#include "util/MetadataBuilder.h"
40#include "util/Util.h"
41
42namespace android {
43namespace companion {
44namespace virtualcamera {
45
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +010046using ::aidl::android::companion::virtualcamera::Format;
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +010047using ::aidl::android::companion::virtualcamera::IVirtualCameraCallback;
Biswarup Pal112458f2023-12-28 19:50:17 +000048using ::aidl::android::companion::virtualcamera::LensFacing;
Biswarup Pal6152a302023-12-19 12:44:09 +000049using ::aidl::android::companion::virtualcamera::SensorOrientation;
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +010050using ::aidl::android::companion::virtualcamera::SupportedStreamConfiguration;
Biswarup Pal6152a302023-12-19 12:44:09 +000051using ::aidl::android::companion::virtualcamera::VirtualCameraConfiguration;
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +010052using ::aidl::android::hardware::camera::common::CameraResourceCost;
53using ::aidl::android::hardware::camera::common::Status;
54using ::aidl::android::hardware::camera::device::CameraMetadata;
55using ::aidl::android::hardware::camera::device::ICameraDeviceCallback;
56using ::aidl::android::hardware::camera::device::ICameraDeviceSession;
57using ::aidl::android::hardware::camera::device::ICameraInjectionSession;
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +010058using ::aidl::android::hardware::camera::device::Stream;
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +010059using ::aidl::android::hardware::camera::device::StreamConfiguration;
60using ::aidl::android::hardware::camera::device::StreamRotation;
61using ::aidl::android::hardware::camera::device::StreamType;
62using ::aidl::android::hardware::graphics::common::PixelFormat;
63
64namespace {
65
66using namespace std::chrono_literals;
67
68// Prefix of camera name - "device@1.1/virtual/{numerical_id}"
69const char* kDevicePathPrefix = "device@1.1/virtual/";
70
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +010071constexpr int32_t kMaxJpegSize = 3 * 1024 * 1024 /*3MiB*/;
72
73constexpr MetadataBuilder::ControlRegion kDefaultEmptyControlRegion{};
74
Biswarup Pal6152a302023-12-19 12:44:09 +000075const std::array<int32_t, 3> kOutputFormats{
76 ANDROID_SCALER_AVAILABLE_FORMATS_IMPLEMENTATION_DEFINED,
77 ANDROID_SCALER_AVAILABLE_FORMATS_YCbCr_420_888,
78 ANDROID_SCALER_AVAILABLE_FORMATS_BLOB};
79
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +010080struct Resolution {
81 Resolution(const int w, const int h) : width(w), height(h) {
82 }
83
84 bool operator<(const Resolution& other) const {
85 return width * height < other.width * other.height;
86 }
87
88 bool operator==(const Resolution& other) const {
89 return width == other.width && height == other.height;
90 }
91
92 const int width;
93 const int height;
94};
95
96std::optional<Resolution> getMaxResolution(
97 const std::vector<SupportedStreamConfiguration>& configs) {
98 auto itMax = std::max_element(configs.begin(), configs.end(),
99 [](const SupportedStreamConfiguration& a,
100 const SupportedStreamConfiguration& b) {
101 return a.width * b.height < a.width * b.height;
102 });
103 if (itMax == configs.end()) {
104 ALOGE(
105 "%s: empty vector of supported configurations, cannot find largest "
106 "resolution.",
107 __func__);
108 return std::nullopt;
109 }
110
111 return Resolution(itMax->width, itMax->height);
112}
113
Biswarup Pal6152a302023-12-19 12:44:09 +0000114// Returns a map of unique resolution to maximum maxFps for all streams with
115// that resolution.
116std::map<Resolution, int> getResolutionToMaxFpsMap(
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +0100117 const std::vector<SupportedStreamConfiguration>& configs) {
Biswarup Pal6152a302023-12-19 12:44:09 +0000118 std::map<Resolution, int> resolutionToMaxFpsMap;
119
120 for (const SupportedStreamConfiguration& config : configs) {
121 Resolution resolution(config.width, config.height);
122 if (resolutionToMaxFpsMap.find(resolution) == resolutionToMaxFpsMap.end()) {
123 resolutionToMaxFpsMap[resolution] = config.maxFps;
124 } else {
125 int currentMaxFps = resolutionToMaxFpsMap[resolution];
126 resolutionToMaxFpsMap[resolution] = std::max(currentMaxFps, config.maxFps);
127 }
128 }
129
130 return resolutionToMaxFpsMap;
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +0100131}
132
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100133// TODO(b/301023410) - Populate camera characteristics according to camera configuration.
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +0100134std::optional<CameraMetadata> initCameraCharacteristics(
Biswarup Pal6152a302023-12-19 12:44:09 +0000135 const std::vector<SupportedStreamConfiguration>& supportedInputConfig,
Biswarup Pal112458f2023-12-28 19:50:17 +0000136 const SensorOrientation sensorOrientation, const LensFacing lensFacing) {
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +0100137 if (!std::all_of(supportedInputConfig.begin(), supportedInputConfig.end(),
138 [](const SupportedStreamConfiguration& config) {
Jan Sebechlebsky042d1fb2023-12-12 16:37:00 +0100139 return isFormatSupportedForInput(
Biswarup Pal6152a302023-12-19 12:44:09 +0000140 config.width, config.height, config.pixelFormat,
141 config.maxFps);
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +0100142 })) {
Jan Sebechlebsky042d1fb2023-12-12 16:37:00 +0100143 ALOGE("%s: input configuration contains unsupported format", __func__);
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +0100144 return std::nullopt;
145 }
146
147 MetadataBuilder builder =
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100148 MetadataBuilder()
149 .setSupportedHardwareLevel(
150 ANDROID_INFO_SUPPORTED_HARDWARE_LEVEL_EXTERNAL)
151 .setFlashAvailable(false)
Biswarup Pal112458f2023-12-28 19:50:17 +0000152 .setLensFacing(
153 static_cast<camera_metadata_enum_android_lens_facing>(lensFacing))
Biswarup Pal6152a302023-12-19 12:44:09 +0000154 .setSensorOrientation(static_cast<int32_t>(sensorOrientation))
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100155 .setAvailableFaceDetectModes({ANDROID_STATISTICS_FACE_DETECT_MODE_OFF})
Jan Sebechlebskyb0119fa2023-12-04 10:29:06 +0100156 .setAvailableMaxDigitalZoom(1.0)
157 .setControlAvailableModes({ANDROID_CONTROL_MODE_AUTO})
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100158 .setControlAfAvailableModes({ANDROID_CONTROL_AF_MODE_OFF})
Jan Sebechlebsky4425a732024-01-31 11:31:54 +0100159 .setControlAvailableSceneModes({ANDROID_CONTROL_SCENE_MODE_DISABLED})
160 .setControlAvailableEffects({ANDROID_CONTROL_EFFECT_MODE_OFF})
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100161 .setControlAeAvailableFpsRange(10, 30)
162 .setControlMaxRegions(0, 0, 0)
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100163 .setControlAfRegions({kDefaultEmptyControlRegion})
164 .setControlAeRegions({kDefaultEmptyControlRegion})
165 .setControlAwbRegions({kDefaultEmptyControlRegion})
166 .setControlAeCompensationRange(0, 1)
167 .setControlAeCompensationStep(camera_metadata_rational_t{0, 1})
Jan Sebechlebsky4425a732024-01-31 11:31:54 +0100168 .setControlAwbLockAvailable(false)
169 .setControlAeLockAvailable(false)
170 .setControlAvailableAwbModes({ANDROID_CONTROL_AWB_MODE_AUTO})
Jan Sebechlebsky6ab07fe2023-12-05 15:23:34 +0100171 .setControlZoomRatioRange(/*min=*/1.0, /*max=*/1.0)
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100172 .setMaxJpegSize(kMaxJpegSize)
Jan Sebechlebsky4425a732024-01-31 11:31:54 +0100173 .setSyncMaxLatency(ANDROID_SYNC_MAX_LATENCY_UNKNOWN)
174 .setAvailableRequestKeys({})
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100175 .setAvailableRequestKeys({ANDROID_CONTROL_AF_MODE})
176 .setAvailableResultKeys({ANDROID_CONTROL_AF_MODE})
177 .setAvailableCapabilities(
178 {ANDROID_REQUEST_AVAILABLE_CAPABILITIES_BACKWARD_COMPATIBLE})
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +0100179 .setAvailableCharacteristicKeys();
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100180
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +0100181 // Active array size must correspond to largest supported input resolution.
182 std::optional<Resolution> maxResolution =
183 getMaxResolution(supportedInputConfig);
184 if (!maxResolution.has_value()) {
185 return std::nullopt;
186 }
187 builder.setSensorActiveArraySize(0, 0, maxResolution->width,
188 maxResolution->height);
189
190 std::vector<MetadataBuilder::StreamConfiguration> outputConfigurations;
191
192 // TODO(b/301023410) Add also all "standard" resolutions we can rescale the
193 // streams to (all standard resolutions with same aspect ratio).
194
Biswarup Pal6152a302023-12-19 12:44:09 +0000195 std::map<Resolution, int> resolutionToMaxFpsMap =
196 getResolutionToMaxFpsMap(supportedInputConfig);
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +0100197
Biswarup Pal6152a302023-12-19 12:44:09 +0000198 // Add configurations for all unique input resolutions and output formats.
199 for (int32_t format : kOutputFormats) {
200 std::transform(
201 resolutionToMaxFpsMap.begin(), resolutionToMaxFpsMap.end(),
202 std::back_inserter(outputConfigurations), [format](const auto& entry) {
203 Resolution resolution = entry.first;
204 int maxFps = entry.second;
205 return MetadataBuilder::StreamConfiguration{
206 .width = resolution.width,
207 .height = resolution.height,
208 .format = format,
209 .minFrameDuration = std::chrono::nanoseconds(1s) / maxFps,
210 .minStallDuration = 0s};
211 });
212 }
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +0100213
214 ALOGV("Adding %zu output configurations", outputConfigurations.size());
215 builder.setAvailableOutputStreamConfigurations(outputConfigurations);
216
217 auto metadata = builder.build();
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100218 if (metadata == nullptr) {
219 ALOGE("Failed to build metadata!");
220 return CameraMetadata();
221 }
222
223 return std::move(*metadata);
224}
225
226} // namespace
227
228VirtualCameraDevice::VirtualCameraDevice(
Biswarup Pal6152a302023-12-19 12:44:09 +0000229 const uint32_t cameraId, const VirtualCameraConfiguration& configuration)
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100230 : mCameraId(cameraId),
Biswarup Pal6152a302023-12-19 12:44:09 +0000231 mVirtualCameraClientCallback(configuration.virtualCameraCallback),
232 mSupportedInputConfigurations(configuration.supportedStreamConfigs) {
233 std::optional<CameraMetadata> metadata = initCameraCharacteristics(
Biswarup Pal112458f2023-12-28 19:50:17 +0000234 mSupportedInputConfigurations, configuration.sensorOrientation,
235 configuration.lensFacing);
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +0100236 if (metadata.has_value()) {
237 mCameraCharacteristics = *metadata;
238 } else {
239 ALOGE(
240 "%s: Failed to initialize camera characteristic based on provided "
241 "configuration.",
242 __func__);
243 }
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100244}
245
246ndk::ScopedAStatus VirtualCameraDevice::getCameraCharacteristics(
247 CameraMetadata* _aidl_return) {
248 ALOGV("%s", __func__);
249 if (_aidl_return == nullptr) {
250 return cameraStatus(Status::ILLEGAL_ARGUMENT);
251 }
252
253 *_aidl_return = mCameraCharacteristics;
254 return ndk::ScopedAStatus::ok();
255}
256
257ndk::ScopedAStatus VirtualCameraDevice::getPhysicalCameraCharacteristics(
258 const std::string& in_physicalCameraId, CameraMetadata* _aidl_return) {
259 ALOGV("%s: physicalCameraId %s", __func__, in_physicalCameraId.c_str());
260 (void)_aidl_return;
261
262 // VTS tests expect this call to fail with illegal argument status for
263 // all publicly advertised camera ids.
264 // Because we don't support physical camera ids, we just always
265 // fail with illegal argument (there's no valid argument to provide).
266 return cameraStatus(Status::ILLEGAL_ARGUMENT);
267}
268
269ndk::ScopedAStatus VirtualCameraDevice::getResourceCost(
270 CameraResourceCost* _aidl_return) {
271 ALOGV("%s", __func__);
272 if (_aidl_return == nullptr) {
273 return cameraStatus(Status::ILLEGAL_ARGUMENT);
274 }
275 _aidl_return->resourceCost = 100; // ¯\_(ツ)_/¯
276 return ndk::ScopedAStatus::ok();
277}
278
279ndk::ScopedAStatus VirtualCameraDevice::isStreamCombinationSupported(
280 const StreamConfiguration& in_streams, bool* _aidl_return) {
281 ALOGV("%s", __func__);
282
283 if (_aidl_return == nullptr) {
284 return cameraStatus(Status::ILLEGAL_ARGUMENT);
285 }
286
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +0100287 *_aidl_return = isStreamCombinationSupported(in_streams);
288 return ndk::ScopedAStatus::ok();
289};
290
291bool VirtualCameraDevice::isStreamCombinationSupported(
292 const StreamConfiguration& streamConfiguration) const {
Jan Sebechlebsky39129f82024-01-19 16:42:11 +0100293 if (streamConfiguration.streams.empty()) {
294 ALOGE("%s: Querying empty configuration", __func__);
295 return false;
296 }
297
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +0100298 for (const Stream& stream : streamConfiguration.streams) {
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100299 ALOGV("%s: Configuration queried: %s", __func__, stream.toString().c_str());
300
301 if (stream.streamType == StreamType::INPUT) {
302 ALOGW("%s: Input stream type is not supported", __func__);
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +0100303 return false;
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100304 }
305
306 // TODO(b/301023410) remove hardcoded format checks, verify against configuration.
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +0100307 if (stream.rotation != StreamRotation::ROTATION_0 ||
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100308 (stream.format != PixelFormat::IMPLEMENTATION_DEFINED &&
309 stream.format != PixelFormat::YCBCR_420_888 &&
310 stream.format != PixelFormat::BLOB)) {
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +0100311 ALOGV("Unsupported output stream type");
312 return false;
313 }
314
315 auto matchesSupportedInputConfig =
316 [&stream](const SupportedStreamConfiguration& config) {
317 return stream.width == config.width && stream.height == config.height;
318 };
319 if (std::none_of(mSupportedInputConfigurations.begin(),
320 mSupportedInputConfigurations.end(),
321 matchesSupportedInputConfig)) {
322 ALOGV("Requested config doesn't match any supported input config");
323 return false;
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100324 }
325 }
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +0100326 return true;
327}
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100328
329ndk::ScopedAStatus VirtualCameraDevice::open(
330 const std::shared_ptr<ICameraDeviceCallback>& in_callback,
331 std::shared_ptr<ICameraDeviceSession>* _aidl_return) {
332 ALOGV("%s", __func__);
333
334 *_aidl_return = ndk::SharedRefBase::make<VirtualCameraSession>(
Jan Sebechlebsky0bb5e092023-12-08 16:17:54 +0100335 sharedFromThis(), in_callback, mVirtualCameraClientCallback);
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100336
337 return ndk::ScopedAStatus::ok();
338};
339
340ndk::ScopedAStatus VirtualCameraDevice::openInjectionSession(
341 const std::shared_ptr<ICameraDeviceCallback>& in_callback,
342 std::shared_ptr<ICameraInjectionSession>* _aidl_return) {
343 ALOGV("%s", __func__);
344
345 (void)in_callback;
346 (void)_aidl_return;
347 return cameraStatus(Status::OPERATION_NOT_SUPPORTED);
348}
349
350ndk::ScopedAStatus VirtualCameraDevice::setTorchMode(bool in_on) {
351 ALOGV("%s: on = %s", __func__, in_on ? "on" : "off");
352 return cameraStatus(Status::OPERATION_NOT_SUPPORTED);
353}
354
355ndk::ScopedAStatus VirtualCameraDevice::turnOnTorchWithStrengthLevel(
356 int32_t in_torchStrength) {
357 ALOGV("%s: torchStrength = %d", __func__, in_torchStrength);
358 return cameraStatus(Status::OPERATION_NOT_SUPPORTED);
359}
360
361ndk::ScopedAStatus VirtualCameraDevice::getTorchStrengthLevel(
362 int32_t* _aidl_return) {
363 (void)_aidl_return;
364 return cameraStatus(Status::OPERATION_NOT_SUPPORTED);
365}
366
367binder_status_t VirtualCameraDevice::dump(int fd, const char** args,
368 uint32_t numArgs) {
369 // TODO(b/301023410) Implement.
370 (void)fd;
371 (void)args;
372 (void)numArgs;
373 return STATUS_OK;
374}
375
376std::string VirtualCameraDevice::getCameraName() const {
377 return std::string(kDevicePathPrefix) + std::to_string(mCameraId);
378}
379
Jan Sebechlebsky0bb5e092023-12-08 16:17:54 +0100380std::shared_ptr<VirtualCameraDevice> VirtualCameraDevice::sharedFromThis() {
381 // SharedRefBase which BnCameraDevice inherits from breaks
382 // std::enable_shared_from_this. This is recommended replacement for
383 // shared_from_this() per documentation in binder_interface_utils.h.
384 return ref<VirtualCameraDevice>();
385}
386
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100387} // namespace virtualcamera
388} // namespace companion
389} // namespace android