blob: a244c0fce159b09c79d96aac215ad6af261e1f38 [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 Sebechlebskya984ffb2024-02-01 09:12:37 +0100155 .setSensorReadoutTimestamp(
156 ANDROID_SENSOR_READOUT_TIMESTAMP_NOT_SUPPORTED)
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100157 .setAvailableFaceDetectModes({ANDROID_STATISTICS_FACE_DETECT_MODE_OFF})
Jan Sebechlebskyb0119fa2023-12-04 10:29:06 +0100158 .setAvailableMaxDigitalZoom(1.0)
159 .setControlAvailableModes({ANDROID_CONTROL_MODE_AUTO})
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100160 .setControlAfAvailableModes({ANDROID_CONTROL_AF_MODE_OFF})
Jan Sebechlebsky4425a732024-01-31 11:31:54 +0100161 .setControlAvailableSceneModes({ANDROID_CONTROL_SCENE_MODE_DISABLED})
162 .setControlAvailableEffects({ANDROID_CONTROL_EFFECT_MODE_OFF})
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100163 .setControlAeAvailableFpsRange(10, 30)
164 .setControlMaxRegions(0, 0, 0)
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100165 .setControlAfRegions({kDefaultEmptyControlRegion})
166 .setControlAeRegions({kDefaultEmptyControlRegion})
167 .setControlAwbRegions({kDefaultEmptyControlRegion})
168 .setControlAeCompensationRange(0, 1)
169 .setControlAeCompensationStep(camera_metadata_rational_t{0, 1})
Jan Sebechlebsky4425a732024-01-31 11:31:54 +0100170 .setControlAwbLockAvailable(false)
171 .setControlAeLockAvailable(false)
172 .setControlAvailableAwbModes({ANDROID_CONTROL_AWB_MODE_AUTO})
Jan Sebechlebsky6ab07fe2023-12-05 15:23:34 +0100173 .setControlZoomRatioRange(/*min=*/1.0, /*max=*/1.0)
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100174 .setMaxJpegSize(kMaxJpegSize)
Jan Sebechlebsky4425a732024-01-31 11:31:54 +0100175 .setSyncMaxLatency(ANDROID_SYNC_MAX_LATENCY_UNKNOWN)
176 .setAvailableRequestKeys({})
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100177 .setAvailableRequestKeys({ANDROID_CONTROL_AF_MODE})
178 .setAvailableResultKeys({ANDROID_CONTROL_AF_MODE})
179 .setAvailableCapabilities(
180 {ANDROID_REQUEST_AVAILABLE_CAPABILITIES_BACKWARD_COMPATIBLE})
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +0100181 .setAvailableCharacteristicKeys();
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100182
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +0100183 // Active array size must correspond to largest supported input resolution.
184 std::optional<Resolution> maxResolution =
185 getMaxResolution(supportedInputConfig);
186 if (!maxResolution.has_value()) {
187 return std::nullopt;
188 }
189 builder.setSensorActiveArraySize(0, 0, maxResolution->width,
190 maxResolution->height);
191
192 std::vector<MetadataBuilder::StreamConfiguration> outputConfigurations;
193
194 // TODO(b/301023410) Add also all "standard" resolutions we can rescale the
195 // streams to (all standard resolutions with same aspect ratio).
196
Biswarup Pal6152a302023-12-19 12:44:09 +0000197 std::map<Resolution, int> resolutionToMaxFpsMap =
198 getResolutionToMaxFpsMap(supportedInputConfig);
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +0100199
Biswarup Pal6152a302023-12-19 12:44:09 +0000200 // Add configurations for all unique input resolutions and output formats.
201 for (int32_t format : kOutputFormats) {
202 std::transform(
203 resolutionToMaxFpsMap.begin(), resolutionToMaxFpsMap.end(),
204 std::back_inserter(outputConfigurations), [format](const auto& entry) {
205 Resolution resolution = entry.first;
206 int maxFps = entry.second;
207 return MetadataBuilder::StreamConfiguration{
208 .width = resolution.width,
209 .height = resolution.height,
210 .format = format,
211 .minFrameDuration = std::chrono::nanoseconds(1s) / maxFps,
212 .minStallDuration = 0s};
213 });
214 }
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +0100215
216 ALOGV("Adding %zu output configurations", outputConfigurations.size());
217 builder.setAvailableOutputStreamConfigurations(outputConfigurations);
218
219 auto metadata = builder.build();
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100220 if (metadata == nullptr) {
221 ALOGE("Failed to build metadata!");
222 return CameraMetadata();
223 }
224
225 return std::move(*metadata);
226}
227
228} // namespace
229
230VirtualCameraDevice::VirtualCameraDevice(
Biswarup Pal6152a302023-12-19 12:44:09 +0000231 const uint32_t cameraId, const VirtualCameraConfiguration& configuration)
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100232 : mCameraId(cameraId),
Biswarup Pal6152a302023-12-19 12:44:09 +0000233 mVirtualCameraClientCallback(configuration.virtualCameraCallback),
234 mSupportedInputConfigurations(configuration.supportedStreamConfigs) {
235 std::optional<CameraMetadata> metadata = initCameraCharacteristics(
Biswarup Pal112458f2023-12-28 19:50:17 +0000236 mSupportedInputConfigurations, configuration.sensorOrientation,
237 configuration.lensFacing);
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +0100238 if (metadata.has_value()) {
239 mCameraCharacteristics = *metadata;
240 } else {
241 ALOGE(
242 "%s: Failed to initialize camera characteristic based on provided "
243 "configuration.",
244 __func__);
245 }
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100246}
247
248ndk::ScopedAStatus VirtualCameraDevice::getCameraCharacteristics(
249 CameraMetadata* _aidl_return) {
250 ALOGV("%s", __func__);
251 if (_aidl_return == nullptr) {
252 return cameraStatus(Status::ILLEGAL_ARGUMENT);
253 }
254
255 *_aidl_return = mCameraCharacteristics;
256 return ndk::ScopedAStatus::ok();
257}
258
259ndk::ScopedAStatus VirtualCameraDevice::getPhysicalCameraCharacteristics(
260 const std::string& in_physicalCameraId, CameraMetadata* _aidl_return) {
261 ALOGV("%s: physicalCameraId %s", __func__, in_physicalCameraId.c_str());
262 (void)_aidl_return;
263
264 // VTS tests expect this call to fail with illegal argument status for
265 // all publicly advertised camera ids.
266 // Because we don't support physical camera ids, we just always
267 // fail with illegal argument (there's no valid argument to provide).
268 return cameraStatus(Status::ILLEGAL_ARGUMENT);
269}
270
271ndk::ScopedAStatus VirtualCameraDevice::getResourceCost(
272 CameraResourceCost* _aidl_return) {
273 ALOGV("%s", __func__);
274 if (_aidl_return == nullptr) {
275 return cameraStatus(Status::ILLEGAL_ARGUMENT);
276 }
277 _aidl_return->resourceCost = 100; // ¯\_(ツ)_/¯
278 return ndk::ScopedAStatus::ok();
279}
280
281ndk::ScopedAStatus VirtualCameraDevice::isStreamCombinationSupported(
282 const StreamConfiguration& in_streams, bool* _aidl_return) {
283 ALOGV("%s", __func__);
284
285 if (_aidl_return == nullptr) {
286 return cameraStatus(Status::ILLEGAL_ARGUMENT);
287 }
288
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +0100289 *_aidl_return = isStreamCombinationSupported(in_streams);
290 return ndk::ScopedAStatus::ok();
291};
292
293bool VirtualCameraDevice::isStreamCombinationSupported(
294 const StreamConfiguration& streamConfiguration) const {
Jan Sebechlebsky39129f82024-01-19 16:42:11 +0100295 if (streamConfiguration.streams.empty()) {
296 ALOGE("%s: Querying empty configuration", __func__);
297 return false;
298 }
299
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +0100300 for (const Stream& stream : streamConfiguration.streams) {
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100301 ALOGV("%s: Configuration queried: %s", __func__, stream.toString().c_str());
302
303 if (stream.streamType == StreamType::INPUT) {
304 ALOGW("%s: Input stream type is not supported", __func__);
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +0100305 return false;
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100306 }
307
308 // TODO(b/301023410) remove hardcoded format checks, verify against configuration.
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +0100309 if (stream.rotation != StreamRotation::ROTATION_0 ||
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100310 (stream.format != PixelFormat::IMPLEMENTATION_DEFINED &&
311 stream.format != PixelFormat::YCBCR_420_888 &&
312 stream.format != PixelFormat::BLOB)) {
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +0100313 ALOGV("Unsupported output stream type");
314 return false;
315 }
316
317 auto matchesSupportedInputConfig =
318 [&stream](const SupportedStreamConfiguration& config) {
319 return stream.width == config.width && stream.height == config.height;
320 };
321 if (std::none_of(mSupportedInputConfigurations.begin(),
322 mSupportedInputConfigurations.end(),
323 matchesSupportedInputConfig)) {
324 ALOGV("Requested config doesn't match any supported input config");
325 return false;
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100326 }
327 }
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +0100328 return true;
329}
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100330
331ndk::ScopedAStatus VirtualCameraDevice::open(
332 const std::shared_ptr<ICameraDeviceCallback>& in_callback,
333 std::shared_ptr<ICameraDeviceSession>* _aidl_return) {
334 ALOGV("%s", __func__);
335
336 *_aidl_return = ndk::SharedRefBase::make<VirtualCameraSession>(
Jan Sebechlebsky0bb5e092023-12-08 16:17:54 +0100337 sharedFromThis(), in_callback, mVirtualCameraClientCallback);
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100338
339 return ndk::ScopedAStatus::ok();
340};
341
342ndk::ScopedAStatus VirtualCameraDevice::openInjectionSession(
343 const std::shared_ptr<ICameraDeviceCallback>& in_callback,
344 std::shared_ptr<ICameraInjectionSession>* _aidl_return) {
345 ALOGV("%s", __func__);
346
347 (void)in_callback;
348 (void)_aidl_return;
349 return cameraStatus(Status::OPERATION_NOT_SUPPORTED);
350}
351
352ndk::ScopedAStatus VirtualCameraDevice::setTorchMode(bool in_on) {
353 ALOGV("%s: on = %s", __func__, in_on ? "on" : "off");
354 return cameraStatus(Status::OPERATION_NOT_SUPPORTED);
355}
356
357ndk::ScopedAStatus VirtualCameraDevice::turnOnTorchWithStrengthLevel(
358 int32_t in_torchStrength) {
359 ALOGV("%s: torchStrength = %d", __func__, in_torchStrength);
360 return cameraStatus(Status::OPERATION_NOT_SUPPORTED);
361}
362
363ndk::ScopedAStatus VirtualCameraDevice::getTorchStrengthLevel(
364 int32_t* _aidl_return) {
365 (void)_aidl_return;
366 return cameraStatus(Status::OPERATION_NOT_SUPPORTED);
367}
368
369binder_status_t VirtualCameraDevice::dump(int fd, const char** args,
370 uint32_t numArgs) {
371 // TODO(b/301023410) Implement.
372 (void)fd;
373 (void)args;
374 (void)numArgs;
375 return STATUS_OK;
376}
377
378std::string VirtualCameraDevice::getCameraName() const {
379 return std::string(kDevicePathPrefix) + std::to_string(mCameraId);
380}
381
Jan Sebechlebsky0bb5e092023-12-08 16:17:54 +0100382std::shared_ptr<VirtualCameraDevice> VirtualCameraDevice::sharedFromThis() {
383 // SharedRefBase which BnCameraDevice inherits from breaks
384 // std::enable_shared_from_this. This is recommended replacement for
385 // shared_from_this() per documentation in binder_interface_utils.h.
386 return ref<VirtualCameraDevice>();
387}
388
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100389} // namespace virtualcamera
390} // namespace companion
391} // namespace android