blob: 0657d8119a7343f7cc0c6740cfb0f0f0fd9f7440 [file] [log] [blame]
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +01001/*
2 * Copyright (C) 2023 The Android Open Source Project
3 *
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"
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +010031#include "aidl/android/hardware/camera/common/Status.h"
32#include "aidl/android/hardware/camera/device/CameraMetadata.h"
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +010033#include "aidl/android/hardware/camera/device/StreamConfiguration.h"
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +010034#include "android/binder_auto_utils.h"
35#include "android/binder_status.h"
36#include "log/log.h"
37#include "system/camera_metadata.h"
38#include "util/MetadataBuilder.h"
39#include "util/Util.h"
40
41namespace android {
42namespace companion {
43namespace virtualcamera {
44
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +010045using ::aidl::android::companion::virtualcamera::Format;
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +010046using ::aidl::android::companion::virtualcamera::IVirtualCameraCallback;
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +010047using ::aidl::android::companion::virtualcamera::SupportedStreamConfiguration;
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +010048using ::aidl::android::hardware::camera::common::CameraResourceCost;
49using ::aidl::android::hardware::camera::common::Status;
50using ::aidl::android::hardware::camera::device::CameraMetadata;
51using ::aidl::android::hardware::camera::device::ICameraDeviceCallback;
52using ::aidl::android::hardware::camera::device::ICameraDeviceSession;
53using ::aidl::android::hardware::camera::device::ICameraInjectionSession;
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +010054using ::aidl::android::hardware::camera::device::Stream;
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +010055using ::aidl::android::hardware::camera::device::StreamConfiguration;
56using ::aidl::android::hardware::camera::device::StreamRotation;
57using ::aidl::android::hardware::camera::device::StreamType;
58using ::aidl::android::hardware::graphics::common::PixelFormat;
59
60namespace {
61
62using namespace std::chrono_literals;
63
64// Prefix of camera name - "device@1.1/virtual/{numerical_id}"
65const char* kDevicePathPrefix = "device@1.1/virtual/";
66
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +010067constexpr std::chrono::nanoseconds kMinFrameDuration30Fps = 1s / 30;
68constexpr int32_t kMaxJpegSize = 3 * 1024 * 1024 /*3MiB*/;
69
70constexpr MetadataBuilder::ControlRegion kDefaultEmptyControlRegion{};
71
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +010072struct Resolution {
73 Resolution(const int w, const int h) : width(w), height(h) {
74 }
75
76 bool operator<(const Resolution& other) const {
77 return width * height < other.width * other.height;
78 }
79
80 bool operator==(const Resolution& other) const {
81 return width == other.width && height == other.height;
82 }
83
84 const int width;
85 const int height;
86};
87
88std::optional<Resolution> getMaxResolution(
89 const std::vector<SupportedStreamConfiguration>& configs) {
90 auto itMax = std::max_element(configs.begin(), configs.end(),
91 [](const SupportedStreamConfiguration& a,
92 const SupportedStreamConfiguration& b) {
93 return a.width * b.height < a.width * b.height;
94 });
95 if (itMax == configs.end()) {
96 ALOGE(
97 "%s: empty vector of supported configurations, cannot find largest "
98 "resolution.",
99 __func__);
100 return std::nullopt;
101 }
102
103 return Resolution(itMax->width, itMax->height);
104}
105
106std::set<Resolution> getUniqueResolutions(
107 const std::vector<SupportedStreamConfiguration>& configs) {
108 std::set<Resolution> uniqueResolutions;
109 std::transform(configs.begin(), configs.end(),
110 std::inserter(uniqueResolutions, uniqueResolutions.begin()),
111 [](const SupportedStreamConfiguration& config) {
112 return Resolution(config.width, config.height);
113 });
114 return uniqueResolutions;
115}
116
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100117// TODO(b/301023410) - Populate camera characteristics according to camera configuration.
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +0100118std::optional<CameraMetadata> initCameraCharacteristics(
119 const std::vector<SupportedStreamConfiguration>& supportedInputConfig) {
120 if (!std::all_of(supportedInputConfig.begin(), supportedInputConfig.end(),
121 [](const SupportedStreamConfiguration& config) {
122 return config.pixelFormat == Format::YUV_420_888;
123 })) {
124 ALOGE("%s: input configuration contains unsupported pixel format", __func__);
125 return std::nullopt;
126 }
127
128 MetadataBuilder builder =
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100129 MetadataBuilder()
130 .setSupportedHardwareLevel(
131 ANDROID_INFO_SUPPORTED_HARDWARE_LEVEL_EXTERNAL)
132 .setFlashAvailable(false)
133 .setLensFacing(ANDROID_LENS_FACING_EXTERNAL)
134 .setSensorOrientation(0)
135 .setAvailableFaceDetectModes({ANDROID_STATISTICS_FACE_DETECT_MODE_OFF})
Jan Sebechlebskyb0119fa2023-12-04 10:29:06 +0100136 .setAvailableMaxDigitalZoom(1.0)
137 .setControlAvailableModes({ANDROID_CONTROL_MODE_AUTO})
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100138 .setControlAfAvailableModes({ANDROID_CONTROL_AF_MODE_OFF})
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100139 .setControlAeAvailableFpsRange(10, 30)
140 .setControlMaxRegions(0, 0, 0)
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100141 .setControlAfRegions({kDefaultEmptyControlRegion})
142 .setControlAeRegions({kDefaultEmptyControlRegion})
143 .setControlAwbRegions({kDefaultEmptyControlRegion})
144 .setControlAeCompensationRange(0, 1)
145 .setControlAeCompensationStep(camera_metadata_rational_t{0, 1})
Jan Sebechlebsky6ab07fe2023-12-05 15:23:34 +0100146 .setControlZoomRatioRange(/*min=*/1.0, /*max=*/1.0)
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100147 .setMaxJpegSize(kMaxJpegSize)
148 .setAvailableRequestKeys({ANDROID_CONTROL_AF_MODE})
149 .setAvailableResultKeys({ANDROID_CONTROL_AF_MODE})
150 .setAvailableCapabilities(
151 {ANDROID_REQUEST_AVAILABLE_CAPABILITIES_BACKWARD_COMPATIBLE})
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +0100152 .setAvailableCharacteristicKeys();
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100153
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +0100154 // Active array size must correspond to largest supported input resolution.
155 std::optional<Resolution> maxResolution =
156 getMaxResolution(supportedInputConfig);
157 if (!maxResolution.has_value()) {
158 return std::nullopt;
159 }
160 builder.setSensorActiveArraySize(0, 0, maxResolution->width,
161 maxResolution->height);
162
163 std::vector<MetadataBuilder::StreamConfiguration> outputConfigurations;
164
165 // TODO(b/301023410) Add also all "standard" resolutions we can rescale the
166 // streams to (all standard resolutions with same aspect ratio).
167
168 // Add IMPLEMENTATION_DEFINED format for all supported input resolutions.
169 std::set<Resolution> uniqueResolutions =
170 getUniqueResolutions(supportedInputConfig);
171 std::transform(
172 uniqueResolutions.begin(), uniqueResolutions.end(),
173 std::back_inserter(outputConfigurations),
174 [](const Resolution& resolution) {
175 return MetadataBuilder::StreamConfiguration{
176 .width = resolution.width,
177 .height = resolution.height,
178 .format = ANDROID_SCALER_AVAILABLE_FORMATS_IMPLEMENTATION_DEFINED,
179 .minFrameDuration = kMinFrameDuration30Fps,
180 .minStallDuration = 0s};
181 });
182
183 // Add all supported configuration with explicit pixel format.
184 std::transform(supportedInputConfig.begin(), supportedInputConfig.end(),
185 std::back_inserter(outputConfigurations),
186 [](const SupportedStreamConfiguration& config) {
187 return MetadataBuilder::StreamConfiguration{
188 .width = config.width,
189 .height = config.height,
190 .format = static_cast<int>(config.pixelFormat),
191 .minFrameDuration = kMinFrameDuration30Fps,
192 .minStallDuration = 0s};
193 });
194
195 // TODO(b/301023410) We currently don't support rescaling for still capture,
196 // so only announce BLOB support for formats exactly matching the input.
197 std::transform(uniqueResolutions.begin(), uniqueResolutions.end(),
198 std::back_inserter(outputConfigurations),
199 [](const Resolution& resolution) {
200 return MetadataBuilder::StreamConfiguration{
201 .width = resolution.width,
202 .height = resolution.height,
203 .format = ANDROID_SCALER_AVAILABLE_FORMATS_BLOB,
204 .minFrameDuration = kMinFrameDuration30Fps,
205 .minStallDuration = 0s};
206 });
207
208 ALOGV("Adding %zu output configurations", outputConfigurations.size());
209 builder.setAvailableOutputStreamConfigurations(outputConfigurations);
210
211 auto metadata = builder.build();
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100212 if (metadata == nullptr) {
213 ALOGE("Failed to build metadata!");
214 return CameraMetadata();
215 }
216
217 return std::move(*metadata);
218}
219
220} // namespace
221
222VirtualCameraDevice::VirtualCameraDevice(
223 const uint32_t cameraId,
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +0100224 const std::vector<SupportedStreamConfiguration>& supportedInputConfig,
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100225 std::shared_ptr<IVirtualCameraCallback> virtualCameraClientCallback)
226 : mCameraId(cameraId),
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +0100227 mVirtualCameraClientCallback(virtualCameraClientCallback),
228 mSupportedInputConfigurations(supportedInputConfig) {
229 std::optional<CameraMetadata> metadata =
230 initCameraCharacteristics(mSupportedInputConfigurations);
231 if (metadata.has_value()) {
232 mCameraCharacteristics = *metadata;
233 } else {
234 ALOGE(
235 "%s: Failed to initialize camera characteristic based on provided "
236 "configuration.",
237 __func__);
238 }
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100239}
240
241ndk::ScopedAStatus VirtualCameraDevice::getCameraCharacteristics(
242 CameraMetadata* _aidl_return) {
243 ALOGV("%s", __func__);
244 if (_aidl_return == nullptr) {
245 return cameraStatus(Status::ILLEGAL_ARGUMENT);
246 }
247
248 *_aidl_return = mCameraCharacteristics;
249 return ndk::ScopedAStatus::ok();
250}
251
252ndk::ScopedAStatus VirtualCameraDevice::getPhysicalCameraCharacteristics(
253 const std::string& in_physicalCameraId, CameraMetadata* _aidl_return) {
254 ALOGV("%s: physicalCameraId %s", __func__, in_physicalCameraId.c_str());
255 (void)_aidl_return;
256
257 // VTS tests expect this call to fail with illegal argument status for
258 // all publicly advertised camera ids.
259 // Because we don't support physical camera ids, we just always
260 // fail with illegal argument (there's no valid argument to provide).
261 return cameraStatus(Status::ILLEGAL_ARGUMENT);
262}
263
264ndk::ScopedAStatus VirtualCameraDevice::getResourceCost(
265 CameraResourceCost* _aidl_return) {
266 ALOGV("%s", __func__);
267 if (_aidl_return == nullptr) {
268 return cameraStatus(Status::ILLEGAL_ARGUMENT);
269 }
270 _aidl_return->resourceCost = 100; // ¯\_(ツ)_/¯
271 return ndk::ScopedAStatus::ok();
272}
273
274ndk::ScopedAStatus VirtualCameraDevice::isStreamCombinationSupported(
275 const StreamConfiguration& in_streams, bool* _aidl_return) {
276 ALOGV("%s", __func__);
277
278 if (_aidl_return == nullptr) {
279 return cameraStatus(Status::ILLEGAL_ARGUMENT);
280 }
281
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +0100282 *_aidl_return = isStreamCombinationSupported(in_streams);
283 return ndk::ScopedAStatus::ok();
284};
285
286bool VirtualCameraDevice::isStreamCombinationSupported(
287 const StreamConfiguration& streamConfiguration) const {
288 for (const Stream& stream : streamConfiguration.streams) {
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100289 ALOGV("%s: Configuration queried: %s", __func__, stream.toString().c_str());
290
291 if (stream.streamType == StreamType::INPUT) {
292 ALOGW("%s: Input stream type is not supported", __func__);
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +0100293 return false;
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100294 }
295
296 // TODO(b/301023410) remove hardcoded format checks, verify against configuration.
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +0100297 if (stream.rotation != StreamRotation::ROTATION_0 ||
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100298 (stream.format != PixelFormat::IMPLEMENTATION_DEFINED &&
299 stream.format != PixelFormat::YCBCR_420_888 &&
300 stream.format != PixelFormat::BLOB)) {
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +0100301 ALOGV("Unsupported output stream type");
302 return false;
303 }
304
305 auto matchesSupportedInputConfig =
306 [&stream](const SupportedStreamConfiguration& config) {
307 return stream.width == config.width && stream.height == config.height;
308 };
309 if (std::none_of(mSupportedInputConfigurations.begin(),
310 mSupportedInputConfigurations.end(),
311 matchesSupportedInputConfig)) {
312 ALOGV("Requested config doesn't match any supported input config");
313 return false;
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100314 }
315 }
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +0100316 return true;
317}
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100318
319ndk::ScopedAStatus VirtualCameraDevice::open(
320 const std::shared_ptr<ICameraDeviceCallback>& in_callback,
321 std::shared_ptr<ICameraDeviceSession>* _aidl_return) {
322 ALOGV("%s", __func__);
323
324 *_aidl_return = ndk::SharedRefBase::make<VirtualCameraSession>(
Jan Sebechlebsky0bb5e092023-12-08 16:17:54 +0100325 sharedFromThis(), in_callback, mVirtualCameraClientCallback);
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100326
327 return ndk::ScopedAStatus::ok();
328};
329
330ndk::ScopedAStatus VirtualCameraDevice::openInjectionSession(
331 const std::shared_ptr<ICameraDeviceCallback>& in_callback,
332 std::shared_ptr<ICameraInjectionSession>* _aidl_return) {
333 ALOGV("%s", __func__);
334
335 (void)in_callback;
336 (void)_aidl_return;
337 return cameraStatus(Status::OPERATION_NOT_SUPPORTED);
338}
339
340ndk::ScopedAStatus VirtualCameraDevice::setTorchMode(bool in_on) {
341 ALOGV("%s: on = %s", __func__, in_on ? "on" : "off");
342 return cameraStatus(Status::OPERATION_NOT_SUPPORTED);
343}
344
345ndk::ScopedAStatus VirtualCameraDevice::turnOnTorchWithStrengthLevel(
346 int32_t in_torchStrength) {
347 ALOGV("%s: torchStrength = %d", __func__, in_torchStrength);
348 return cameraStatus(Status::OPERATION_NOT_SUPPORTED);
349}
350
351ndk::ScopedAStatus VirtualCameraDevice::getTorchStrengthLevel(
352 int32_t* _aidl_return) {
353 (void)_aidl_return;
354 return cameraStatus(Status::OPERATION_NOT_SUPPORTED);
355}
356
357binder_status_t VirtualCameraDevice::dump(int fd, const char** args,
358 uint32_t numArgs) {
359 // TODO(b/301023410) Implement.
360 (void)fd;
361 (void)args;
362 (void)numArgs;
363 return STATUS_OK;
364}
365
366std::string VirtualCameraDevice::getCameraName() const {
367 return std::string(kDevicePathPrefix) + std::to_string(mCameraId);
368}
369
Jan Sebechlebsky0bb5e092023-12-08 16:17:54 +0100370std::shared_ptr<VirtualCameraDevice> VirtualCameraDevice::sharedFromThis() {
371 // SharedRefBase which BnCameraDevice inherits from breaks
372 // std::enable_shared_from_this. This is recommended replacement for
373 // shared_from_this() per documentation in binder_interface_utils.h.
374 return ref<VirtualCameraDevice>();
375}
376
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100377} // namespace virtualcamera
378} // namespace companion
379} // namespace android