blob: ff53be5e838e7671182c8cc56ff654e0a4636e5b [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 Pald9be04d2024-01-31 14:35:15 +0000154 .setFocalLength(43.0)
Biswarup Pal6152a302023-12-19 12:44:09 +0000155 .setSensorOrientation(static_cast<int32_t>(sensorOrientation))
Jan Sebechlebskya984ffb2024-02-01 09:12:37 +0100156 .setSensorReadoutTimestamp(
157 ANDROID_SENSOR_READOUT_TIMESTAMP_NOT_SUPPORTED)
Biswarup Pald9be04d2024-01-31 14:35:15 +0000158 .setSensorPhysicalSize(36.0, 24.0)
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100159 .setAvailableFaceDetectModes({ANDROID_STATISTICS_FACE_DETECT_MODE_OFF})
Jan Sebechlebskyb0119fa2023-12-04 10:29:06 +0100160 .setAvailableMaxDigitalZoom(1.0)
161 .setControlAvailableModes({ANDROID_CONTROL_MODE_AUTO})
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100162 .setControlAfAvailableModes({ANDROID_CONTROL_AF_MODE_OFF})
Jan Sebechlebsky4425a732024-01-31 11:31:54 +0100163 .setControlAvailableSceneModes({ANDROID_CONTROL_SCENE_MODE_DISABLED})
164 .setControlAvailableEffects({ANDROID_CONTROL_EFFECT_MODE_OFF})
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100165 .setControlAeAvailableFpsRange(10, 30)
166 .setControlMaxRegions(0, 0, 0)
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100167 .setControlAfRegions({kDefaultEmptyControlRegion})
168 .setControlAeRegions({kDefaultEmptyControlRegion})
169 .setControlAwbRegions({kDefaultEmptyControlRegion})
170 .setControlAeCompensationRange(0, 1)
171 .setControlAeCompensationStep(camera_metadata_rational_t{0, 1})
Jan Sebechlebsky4425a732024-01-31 11:31:54 +0100172 .setControlAwbLockAvailable(false)
173 .setControlAeLockAvailable(false)
174 .setControlAvailableAwbModes({ANDROID_CONTROL_AWB_MODE_AUTO})
Jan Sebechlebsky6ab07fe2023-12-05 15:23:34 +0100175 .setControlZoomRatioRange(/*min=*/1.0, /*max=*/1.0)
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100176 .setMaxJpegSize(kMaxJpegSize)
Jan Sebechlebsky4425a732024-01-31 11:31:54 +0100177 .setSyncMaxLatency(ANDROID_SYNC_MAX_LATENCY_UNKNOWN)
178 .setAvailableRequestKeys({})
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100179 .setAvailableRequestKeys({ANDROID_CONTROL_AF_MODE})
180 .setAvailableResultKeys({ANDROID_CONTROL_AF_MODE})
181 .setAvailableCapabilities(
182 {ANDROID_REQUEST_AVAILABLE_CAPABILITIES_BACKWARD_COMPATIBLE})
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +0100183 .setAvailableCharacteristicKeys();
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100184
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +0100185 // Active array size must correspond to largest supported input resolution.
186 std::optional<Resolution> maxResolution =
187 getMaxResolution(supportedInputConfig);
188 if (!maxResolution.has_value()) {
189 return std::nullopt;
190 }
191 builder.setSensorActiveArraySize(0, 0, maxResolution->width,
192 maxResolution->height);
Biswarup Pald9be04d2024-01-31 14:35:15 +0000193 builder.setSensorPixelArraySize(maxResolution->width, maxResolution->height);
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +0100194
195 std::vector<MetadataBuilder::StreamConfiguration> outputConfigurations;
196
197 // TODO(b/301023410) Add also all "standard" resolutions we can rescale the
198 // streams to (all standard resolutions with same aspect ratio).
199
Biswarup Pal6152a302023-12-19 12:44:09 +0000200 std::map<Resolution, int> resolutionToMaxFpsMap =
201 getResolutionToMaxFpsMap(supportedInputConfig);
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +0100202
Biswarup Pal6152a302023-12-19 12:44:09 +0000203 // Add configurations for all unique input resolutions and output formats.
204 for (int32_t format : kOutputFormats) {
205 std::transform(
206 resolutionToMaxFpsMap.begin(), resolutionToMaxFpsMap.end(),
207 std::back_inserter(outputConfigurations), [format](const auto& entry) {
208 Resolution resolution = entry.first;
209 int maxFps = entry.second;
210 return MetadataBuilder::StreamConfiguration{
211 .width = resolution.width,
212 .height = resolution.height,
213 .format = format,
214 .minFrameDuration = std::chrono::nanoseconds(1s) / maxFps,
215 .minStallDuration = 0s};
216 });
217 }
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +0100218
219 ALOGV("Adding %zu output configurations", outputConfigurations.size());
220 builder.setAvailableOutputStreamConfigurations(outputConfigurations);
221
222 auto metadata = builder.build();
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100223 if (metadata == nullptr) {
224 ALOGE("Failed to build metadata!");
225 return CameraMetadata();
226 }
227
228 return std::move(*metadata);
229}
230
231} // namespace
232
233VirtualCameraDevice::VirtualCameraDevice(
Biswarup Pal6152a302023-12-19 12:44:09 +0000234 const uint32_t cameraId, const VirtualCameraConfiguration& configuration)
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100235 : mCameraId(cameraId),
Biswarup Pal6152a302023-12-19 12:44:09 +0000236 mVirtualCameraClientCallback(configuration.virtualCameraCallback),
237 mSupportedInputConfigurations(configuration.supportedStreamConfigs) {
238 std::optional<CameraMetadata> metadata = initCameraCharacteristics(
Biswarup Pal112458f2023-12-28 19:50:17 +0000239 mSupportedInputConfigurations, configuration.sensorOrientation,
240 configuration.lensFacing);
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +0100241 if (metadata.has_value()) {
242 mCameraCharacteristics = *metadata;
243 } else {
244 ALOGE(
245 "%s: Failed to initialize camera characteristic based on provided "
246 "configuration.",
247 __func__);
248 }
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100249}
250
251ndk::ScopedAStatus VirtualCameraDevice::getCameraCharacteristics(
252 CameraMetadata* _aidl_return) {
253 ALOGV("%s", __func__);
254 if (_aidl_return == nullptr) {
255 return cameraStatus(Status::ILLEGAL_ARGUMENT);
256 }
257
258 *_aidl_return = mCameraCharacteristics;
259 return ndk::ScopedAStatus::ok();
260}
261
262ndk::ScopedAStatus VirtualCameraDevice::getPhysicalCameraCharacteristics(
263 const std::string& in_physicalCameraId, CameraMetadata* _aidl_return) {
264 ALOGV("%s: physicalCameraId %s", __func__, in_physicalCameraId.c_str());
265 (void)_aidl_return;
266
267 // VTS tests expect this call to fail with illegal argument status for
268 // all publicly advertised camera ids.
269 // Because we don't support physical camera ids, we just always
270 // fail with illegal argument (there's no valid argument to provide).
271 return cameraStatus(Status::ILLEGAL_ARGUMENT);
272}
273
274ndk::ScopedAStatus VirtualCameraDevice::getResourceCost(
275 CameraResourceCost* _aidl_return) {
276 ALOGV("%s", __func__);
277 if (_aidl_return == nullptr) {
278 return cameraStatus(Status::ILLEGAL_ARGUMENT);
279 }
280 _aidl_return->resourceCost = 100; // ¯\_(ツ)_/¯
281 return ndk::ScopedAStatus::ok();
282}
283
284ndk::ScopedAStatus VirtualCameraDevice::isStreamCombinationSupported(
285 const StreamConfiguration& in_streams, bool* _aidl_return) {
286 ALOGV("%s", __func__);
287
288 if (_aidl_return == nullptr) {
289 return cameraStatus(Status::ILLEGAL_ARGUMENT);
290 }
291
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +0100292 *_aidl_return = isStreamCombinationSupported(in_streams);
293 return ndk::ScopedAStatus::ok();
294};
295
296bool VirtualCameraDevice::isStreamCombinationSupported(
297 const StreamConfiguration& streamConfiguration) const {
Jan Sebechlebsky39129f82024-01-19 16:42:11 +0100298 if (streamConfiguration.streams.empty()) {
299 ALOGE("%s: Querying empty configuration", __func__);
300 return false;
301 }
302
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +0100303 for (const Stream& stream : streamConfiguration.streams) {
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100304 ALOGV("%s: Configuration queried: %s", __func__, stream.toString().c_str());
305
306 if (stream.streamType == StreamType::INPUT) {
307 ALOGW("%s: Input stream type is not supported", __func__);
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +0100308 return false;
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100309 }
310
311 // TODO(b/301023410) remove hardcoded format checks, verify against configuration.
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +0100312 if (stream.rotation != StreamRotation::ROTATION_0 ||
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100313 (stream.format != PixelFormat::IMPLEMENTATION_DEFINED &&
314 stream.format != PixelFormat::YCBCR_420_888 &&
315 stream.format != PixelFormat::BLOB)) {
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +0100316 ALOGV("Unsupported output stream type");
317 return false;
318 }
319
320 auto matchesSupportedInputConfig =
321 [&stream](const SupportedStreamConfiguration& config) {
322 return stream.width == config.width && stream.height == config.height;
323 };
324 if (std::none_of(mSupportedInputConfigurations.begin(),
325 mSupportedInputConfigurations.end(),
326 matchesSupportedInputConfig)) {
327 ALOGV("Requested config doesn't match any supported input config");
328 return false;
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100329 }
330 }
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +0100331 return true;
332}
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100333
334ndk::ScopedAStatus VirtualCameraDevice::open(
335 const std::shared_ptr<ICameraDeviceCallback>& in_callback,
336 std::shared_ptr<ICameraDeviceSession>* _aidl_return) {
337 ALOGV("%s", __func__);
338
339 *_aidl_return = ndk::SharedRefBase::make<VirtualCameraSession>(
Jan Sebechlebsky0bb5e092023-12-08 16:17:54 +0100340 sharedFromThis(), in_callback, mVirtualCameraClientCallback);
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100341
342 return ndk::ScopedAStatus::ok();
343};
344
345ndk::ScopedAStatus VirtualCameraDevice::openInjectionSession(
346 const std::shared_ptr<ICameraDeviceCallback>& in_callback,
347 std::shared_ptr<ICameraInjectionSession>* _aidl_return) {
348 ALOGV("%s", __func__);
349
350 (void)in_callback;
351 (void)_aidl_return;
352 return cameraStatus(Status::OPERATION_NOT_SUPPORTED);
353}
354
355ndk::ScopedAStatus VirtualCameraDevice::setTorchMode(bool in_on) {
356 ALOGV("%s: on = %s", __func__, in_on ? "on" : "off");
357 return cameraStatus(Status::OPERATION_NOT_SUPPORTED);
358}
359
360ndk::ScopedAStatus VirtualCameraDevice::turnOnTorchWithStrengthLevel(
361 int32_t in_torchStrength) {
362 ALOGV("%s: torchStrength = %d", __func__, in_torchStrength);
363 return cameraStatus(Status::OPERATION_NOT_SUPPORTED);
364}
365
366ndk::ScopedAStatus VirtualCameraDevice::getTorchStrengthLevel(
367 int32_t* _aidl_return) {
368 (void)_aidl_return;
369 return cameraStatus(Status::OPERATION_NOT_SUPPORTED);
370}
371
372binder_status_t VirtualCameraDevice::dump(int fd, const char** args,
373 uint32_t numArgs) {
374 // TODO(b/301023410) Implement.
375 (void)fd;
376 (void)args;
377 (void)numArgs;
378 return STATUS_OK;
379}
380
381std::string VirtualCameraDevice::getCameraName() const {
382 return std::string(kDevicePathPrefix) + std::to_string(mCameraId);
383}
384
Jan Sebechlebsky0bb5e092023-12-08 16:17:54 +0100385std::shared_ptr<VirtualCameraDevice> VirtualCameraDevice::sharedFromThis() {
386 // SharedRefBase which BnCameraDevice inherits from breaks
387 // std::enable_shared_from_this. This is recommended replacement for
388 // shared_from_this() per documentation in binder_interface_utils.h.
389 return ref<VirtualCameraDevice>();
390}
391
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100392} // namespace virtualcamera
393} // namespace companion
394} // namespace android