blob: 768dffb5e9f68c15bff3181cb6c80171d62c31dd [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 Sebechlebsky5cb39962023-11-22 17:33:07 +0100159 .setControlAeAvailableFpsRange(10, 30)
160 .setControlMaxRegions(0, 0, 0)
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100161 .setControlAfRegions({kDefaultEmptyControlRegion})
162 .setControlAeRegions({kDefaultEmptyControlRegion})
163 .setControlAwbRegions({kDefaultEmptyControlRegion})
164 .setControlAeCompensationRange(0, 1)
165 .setControlAeCompensationStep(camera_metadata_rational_t{0, 1})
Jan Sebechlebsky6ab07fe2023-12-05 15:23:34 +0100166 .setControlZoomRatioRange(/*min=*/1.0, /*max=*/1.0)
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100167 .setMaxJpegSize(kMaxJpegSize)
168 .setAvailableRequestKeys({ANDROID_CONTROL_AF_MODE})
169 .setAvailableResultKeys({ANDROID_CONTROL_AF_MODE})
170 .setAvailableCapabilities(
171 {ANDROID_REQUEST_AVAILABLE_CAPABILITIES_BACKWARD_COMPATIBLE})
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +0100172 .setAvailableCharacteristicKeys();
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100173
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +0100174 // Active array size must correspond to largest supported input resolution.
175 std::optional<Resolution> maxResolution =
176 getMaxResolution(supportedInputConfig);
177 if (!maxResolution.has_value()) {
178 return std::nullopt;
179 }
180 builder.setSensorActiveArraySize(0, 0, maxResolution->width,
181 maxResolution->height);
182
183 std::vector<MetadataBuilder::StreamConfiguration> outputConfigurations;
184
185 // TODO(b/301023410) Add also all "standard" resolutions we can rescale the
186 // streams to (all standard resolutions with same aspect ratio).
187
Biswarup Pal6152a302023-12-19 12:44:09 +0000188 std::map<Resolution, int> resolutionToMaxFpsMap =
189 getResolutionToMaxFpsMap(supportedInputConfig);
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +0100190
Biswarup Pal6152a302023-12-19 12:44:09 +0000191 // Add configurations for all unique input resolutions and output formats.
192 for (int32_t format : kOutputFormats) {
193 std::transform(
194 resolutionToMaxFpsMap.begin(), resolutionToMaxFpsMap.end(),
195 std::back_inserter(outputConfigurations), [format](const auto& entry) {
196 Resolution resolution = entry.first;
197 int maxFps = entry.second;
198 return MetadataBuilder::StreamConfiguration{
199 .width = resolution.width,
200 .height = resolution.height,
201 .format = format,
202 .minFrameDuration = std::chrono::nanoseconds(1s) / maxFps,
203 .minStallDuration = 0s};
204 });
205 }
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +0100206
207 ALOGV("Adding %zu output configurations", outputConfigurations.size());
208 builder.setAvailableOutputStreamConfigurations(outputConfigurations);
209
210 auto metadata = builder.build();
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100211 if (metadata == nullptr) {
212 ALOGE("Failed to build metadata!");
213 return CameraMetadata();
214 }
215
216 return std::move(*metadata);
217}
218
219} // namespace
220
221VirtualCameraDevice::VirtualCameraDevice(
Biswarup Pal6152a302023-12-19 12:44:09 +0000222 const uint32_t cameraId, const VirtualCameraConfiguration& configuration)
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100223 : mCameraId(cameraId),
Biswarup Pal6152a302023-12-19 12:44:09 +0000224 mVirtualCameraClientCallback(configuration.virtualCameraCallback),
225 mSupportedInputConfigurations(configuration.supportedStreamConfigs) {
226 std::optional<CameraMetadata> metadata = initCameraCharacteristics(
Biswarup Pal112458f2023-12-28 19:50:17 +0000227 mSupportedInputConfigurations, configuration.sensorOrientation,
228 configuration.lensFacing);
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +0100229 if (metadata.has_value()) {
230 mCameraCharacteristics = *metadata;
231 } else {
232 ALOGE(
233 "%s: Failed to initialize camera characteristic based on provided "
234 "configuration.",
235 __func__);
236 }
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100237}
238
239ndk::ScopedAStatus VirtualCameraDevice::getCameraCharacteristics(
240 CameraMetadata* _aidl_return) {
241 ALOGV("%s", __func__);
242 if (_aidl_return == nullptr) {
243 return cameraStatus(Status::ILLEGAL_ARGUMENT);
244 }
245
246 *_aidl_return = mCameraCharacteristics;
247 return ndk::ScopedAStatus::ok();
248}
249
250ndk::ScopedAStatus VirtualCameraDevice::getPhysicalCameraCharacteristics(
251 const std::string& in_physicalCameraId, CameraMetadata* _aidl_return) {
252 ALOGV("%s: physicalCameraId %s", __func__, in_physicalCameraId.c_str());
253 (void)_aidl_return;
254
255 // VTS tests expect this call to fail with illegal argument status for
256 // all publicly advertised camera ids.
257 // Because we don't support physical camera ids, we just always
258 // fail with illegal argument (there's no valid argument to provide).
259 return cameraStatus(Status::ILLEGAL_ARGUMENT);
260}
261
262ndk::ScopedAStatus VirtualCameraDevice::getResourceCost(
263 CameraResourceCost* _aidl_return) {
264 ALOGV("%s", __func__);
265 if (_aidl_return == nullptr) {
266 return cameraStatus(Status::ILLEGAL_ARGUMENT);
267 }
268 _aidl_return->resourceCost = 100; // ¯\_(ツ)_/¯
269 return ndk::ScopedAStatus::ok();
270}
271
272ndk::ScopedAStatus VirtualCameraDevice::isStreamCombinationSupported(
273 const StreamConfiguration& in_streams, bool* _aidl_return) {
274 ALOGV("%s", __func__);
275
276 if (_aidl_return == nullptr) {
277 return cameraStatus(Status::ILLEGAL_ARGUMENT);
278 }
279
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +0100280 *_aidl_return = isStreamCombinationSupported(in_streams);
281 return ndk::ScopedAStatus::ok();
282};
283
284bool VirtualCameraDevice::isStreamCombinationSupported(
285 const StreamConfiguration& streamConfiguration) const {
Jan Sebechlebsky39129f82024-01-19 16:42:11 +0100286 if (streamConfiguration.streams.empty()) {
287 ALOGE("%s: Querying empty configuration", __func__);
288 return false;
289 }
290
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +0100291 for (const Stream& stream : streamConfiguration.streams) {
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100292 ALOGV("%s: Configuration queried: %s", __func__, stream.toString().c_str());
293
294 if (stream.streamType == StreamType::INPUT) {
295 ALOGW("%s: Input stream type is not supported", __func__);
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +0100296 return false;
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100297 }
298
299 // TODO(b/301023410) remove hardcoded format checks, verify against configuration.
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +0100300 if (stream.rotation != StreamRotation::ROTATION_0 ||
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100301 (stream.format != PixelFormat::IMPLEMENTATION_DEFINED &&
302 stream.format != PixelFormat::YCBCR_420_888 &&
303 stream.format != PixelFormat::BLOB)) {
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +0100304 ALOGV("Unsupported output stream type");
305 return false;
306 }
307
308 auto matchesSupportedInputConfig =
309 [&stream](const SupportedStreamConfiguration& config) {
310 return stream.width == config.width && stream.height == config.height;
311 };
312 if (std::none_of(mSupportedInputConfigurations.begin(),
313 mSupportedInputConfigurations.end(),
314 matchesSupportedInputConfig)) {
315 ALOGV("Requested config doesn't match any supported input config");
316 return false;
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100317 }
318 }
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +0100319 return true;
320}
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100321
322ndk::ScopedAStatus VirtualCameraDevice::open(
323 const std::shared_ptr<ICameraDeviceCallback>& in_callback,
324 std::shared_ptr<ICameraDeviceSession>* _aidl_return) {
325 ALOGV("%s", __func__);
326
327 *_aidl_return = ndk::SharedRefBase::make<VirtualCameraSession>(
Jan Sebechlebsky0bb5e092023-12-08 16:17:54 +0100328 sharedFromThis(), in_callback, mVirtualCameraClientCallback);
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100329
330 return ndk::ScopedAStatus::ok();
331};
332
333ndk::ScopedAStatus VirtualCameraDevice::openInjectionSession(
334 const std::shared_ptr<ICameraDeviceCallback>& in_callback,
335 std::shared_ptr<ICameraInjectionSession>* _aidl_return) {
336 ALOGV("%s", __func__);
337
338 (void)in_callback;
339 (void)_aidl_return;
340 return cameraStatus(Status::OPERATION_NOT_SUPPORTED);
341}
342
343ndk::ScopedAStatus VirtualCameraDevice::setTorchMode(bool in_on) {
344 ALOGV("%s: on = %s", __func__, in_on ? "on" : "off");
345 return cameraStatus(Status::OPERATION_NOT_SUPPORTED);
346}
347
348ndk::ScopedAStatus VirtualCameraDevice::turnOnTorchWithStrengthLevel(
349 int32_t in_torchStrength) {
350 ALOGV("%s: torchStrength = %d", __func__, in_torchStrength);
351 return cameraStatus(Status::OPERATION_NOT_SUPPORTED);
352}
353
354ndk::ScopedAStatus VirtualCameraDevice::getTorchStrengthLevel(
355 int32_t* _aidl_return) {
356 (void)_aidl_return;
357 return cameraStatus(Status::OPERATION_NOT_SUPPORTED);
358}
359
360binder_status_t VirtualCameraDevice::dump(int fd, const char** args,
361 uint32_t numArgs) {
362 // TODO(b/301023410) Implement.
363 (void)fd;
364 (void)args;
365 (void)numArgs;
366 return STATUS_OK;
367}
368
369std::string VirtualCameraDevice::getCameraName() const {
370 return std::string(kDevicePathPrefix) + std::to_string(mCameraId);
371}
372
Jan Sebechlebsky0bb5e092023-12-08 16:17:54 +0100373std::shared_ptr<VirtualCameraDevice> VirtualCameraDevice::sharedFromThis() {
374 // SharedRefBase which BnCameraDevice inherits from breaks
375 // std::enable_shared_from_this. This is recommended replacement for
376 // shared_from_this() per documentation in binder_interface_utils.h.
377 return ref<VirtualCameraDevice>();
378}
379
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100380} // namespace virtualcamera
381} // namespace companion
382} // namespace android