blob: e21afb76cddc9ec9750325ef75cbd7732d193ccf [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})
136 .setControlAfAvailableModes({ANDROID_CONTROL_AF_MODE_OFF})
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100137 .setControlAeAvailableFpsRange(10, 30)
138 .setControlMaxRegions(0, 0, 0)
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100139 .setControlAfRegions({kDefaultEmptyControlRegion})
140 .setControlAeRegions({kDefaultEmptyControlRegion})
141 .setControlAwbRegions({kDefaultEmptyControlRegion})
142 .setControlAeCompensationRange(0, 1)
143 .setControlAeCompensationStep(camera_metadata_rational_t{0, 1})
144 .setMaxJpegSize(kMaxJpegSize)
145 .setAvailableRequestKeys({ANDROID_CONTROL_AF_MODE})
146 .setAvailableResultKeys({ANDROID_CONTROL_AF_MODE})
147 .setAvailableCapabilities(
148 {ANDROID_REQUEST_AVAILABLE_CAPABILITIES_BACKWARD_COMPATIBLE})
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +0100149 .setAvailableCharacteristicKeys();
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100150
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +0100151 // Active array size must correspond to largest supported input resolution.
152 std::optional<Resolution> maxResolution =
153 getMaxResolution(supportedInputConfig);
154 if (!maxResolution.has_value()) {
155 return std::nullopt;
156 }
157 builder.setSensorActiveArraySize(0, 0, maxResolution->width,
158 maxResolution->height);
159
160 std::vector<MetadataBuilder::StreamConfiguration> outputConfigurations;
161
162 // TODO(b/301023410) Add also all "standard" resolutions we can rescale the
163 // streams to (all standard resolutions with same aspect ratio).
164
165 // Add IMPLEMENTATION_DEFINED format for all supported input resolutions.
166 std::set<Resolution> uniqueResolutions =
167 getUniqueResolutions(supportedInputConfig);
168 std::transform(
169 uniqueResolutions.begin(), uniqueResolutions.end(),
170 std::back_inserter(outputConfigurations),
171 [](const Resolution& resolution) {
172 return MetadataBuilder::StreamConfiguration{
173 .width = resolution.width,
174 .height = resolution.height,
175 .format = ANDROID_SCALER_AVAILABLE_FORMATS_IMPLEMENTATION_DEFINED,
176 .minFrameDuration = kMinFrameDuration30Fps,
177 .minStallDuration = 0s};
178 });
179
180 // Add all supported configuration with explicit pixel format.
181 std::transform(supportedInputConfig.begin(), supportedInputConfig.end(),
182 std::back_inserter(outputConfigurations),
183 [](const SupportedStreamConfiguration& config) {
184 return MetadataBuilder::StreamConfiguration{
185 .width = config.width,
186 .height = config.height,
187 .format = static_cast<int>(config.pixelFormat),
188 .minFrameDuration = kMinFrameDuration30Fps,
189 .minStallDuration = 0s};
190 });
191
192 // TODO(b/301023410) We currently don't support rescaling for still capture,
193 // so only announce BLOB support for formats exactly matching the input.
194 std::transform(uniqueResolutions.begin(), uniqueResolutions.end(),
195 std::back_inserter(outputConfigurations),
196 [](const Resolution& resolution) {
197 return MetadataBuilder::StreamConfiguration{
198 .width = resolution.width,
199 .height = resolution.height,
200 .format = ANDROID_SCALER_AVAILABLE_FORMATS_BLOB,
201 .minFrameDuration = kMinFrameDuration30Fps,
202 .minStallDuration = 0s};
203 });
204
205 ALOGV("Adding %zu output configurations", outputConfigurations.size());
206 builder.setAvailableOutputStreamConfigurations(outputConfigurations);
207
208 auto metadata = builder.build();
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100209 if (metadata == nullptr) {
210 ALOGE("Failed to build metadata!");
211 return CameraMetadata();
212 }
213
214 return std::move(*metadata);
215}
216
217} // namespace
218
219VirtualCameraDevice::VirtualCameraDevice(
220 const uint32_t cameraId,
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +0100221 const std::vector<SupportedStreamConfiguration>& supportedInputConfig,
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100222 std::shared_ptr<IVirtualCameraCallback> virtualCameraClientCallback)
223 : mCameraId(cameraId),
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +0100224 mVirtualCameraClientCallback(virtualCameraClientCallback),
225 mSupportedInputConfigurations(supportedInputConfig) {
226 std::optional<CameraMetadata> metadata =
227 initCameraCharacteristics(mSupportedInputConfigurations);
228 if (metadata.has_value()) {
229 mCameraCharacteristics = *metadata;
230 } else {
231 ALOGE(
232 "%s: Failed to initialize camera characteristic based on provided "
233 "configuration.",
234 __func__);
235 }
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100236}
237
238ndk::ScopedAStatus VirtualCameraDevice::getCameraCharacteristics(
239 CameraMetadata* _aidl_return) {
240 ALOGV("%s", __func__);
241 if (_aidl_return == nullptr) {
242 return cameraStatus(Status::ILLEGAL_ARGUMENT);
243 }
244
245 *_aidl_return = mCameraCharacteristics;
246 return ndk::ScopedAStatus::ok();
247}
248
249ndk::ScopedAStatus VirtualCameraDevice::getPhysicalCameraCharacteristics(
250 const std::string& in_physicalCameraId, CameraMetadata* _aidl_return) {
251 ALOGV("%s: physicalCameraId %s", __func__, in_physicalCameraId.c_str());
252 (void)_aidl_return;
253
254 // VTS tests expect this call to fail with illegal argument status for
255 // all publicly advertised camera ids.
256 // Because we don't support physical camera ids, we just always
257 // fail with illegal argument (there's no valid argument to provide).
258 return cameraStatus(Status::ILLEGAL_ARGUMENT);
259}
260
261ndk::ScopedAStatus VirtualCameraDevice::getResourceCost(
262 CameraResourceCost* _aidl_return) {
263 ALOGV("%s", __func__);
264 if (_aidl_return == nullptr) {
265 return cameraStatus(Status::ILLEGAL_ARGUMENT);
266 }
267 _aidl_return->resourceCost = 100; // ¯\_(ツ)_/¯
268 return ndk::ScopedAStatus::ok();
269}
270
271ndk::ScopedAStatus VirtualCameraDevice::isStreamCombinationSupported(
272 const StreamConfiguration& in_streams, bool* _aidl_return) {
273 ALOGV("%s", __func__);
274
275 if (_aidl_return == nullptr) {
276 return cameraStatus(Status::ILLEGAL_ARGUMENT);
277 }
278
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +0100279 *_aidl_return = isStreamCombinationSupported(in_streams);
280 return ndk::ScopedAStatus::ok();
281};
282
283bool VirtualCameraDevice::isStreamCombinationSupported(
284 const StreamConfiguration& streamConfiguration) const {
285 for (const Stream& stream : streamConfiguration.streams) {
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100286 ALOGV("%s: Configuration queried: %s", __func__, stream.toString().c_str());
287
288 if (stream.streamType == StreamType::INPUT) {
289 ALOGW("%s: Input stream type is not supported", __func__);
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +0100290 return false;
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100291 }
292
293 // TODO(b/301023410) remove hardcoded format checks, verify against configuration.
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +0100294 if (stream.rotation != StreamRotation::ROTATION_0 ||
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100295 (stream.format != PixelFormat::IMPLEMENTATION_DEFINED &&
296 stream.format != PixelFormat::YCBCR_420_888 &&
297 stream.format != PixelFormat::BLOB)) {
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +0100298 ALOGV("Unsupported output stream type");
299 return false;
300 }
301
302 auto matchesSupportedInputConfig =
303 [&stream](const SupportedStreamConfiguration& config) {
304 return stream.width == config.width && stream.height == config.height;
305 };
306 if (std::none_of(mSupportedInputConfigurations.begin(),
307 mSupportedInputConfigurations.end(),
308 matchesSupportedInputConfig)) {
309 ALOGV("Requested config doesn't match any supported input config");
310 return false;
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100311 }
312 }
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +0100313 return true;
314}
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100315
316ndk::ScopedAStatus VirtualCameraDevice::open(
317 const std::shared_ptr<ICameraDeviceCallback>& in_callback,
318 std::shared_ptr<ICameraDeviceSession>* _aidl_return) {
319 ALOGV("%s", __func__);
320
321 *_aidl_return = ndk::SharedRefBase::make<VirtualCameraSession>(
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +0100322 *this, in_callback, mVirtualCameraClientCallback);
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100323
324 return ndk::ScopedAStatus::ok();
325};
326
327ndk::ScopedAStatus VirtualCameraDevice::openInjectionSession(
328 const std::shared_ptr<ICameraDeviceCallback>& in_callback,
329 std::shared_ptr<ICameraInjectionSession>* _aidl_return) {
330 ALOGV("%s", __func__);
331
332 (void)in_callback;
333 (void)_aidl_return;
334 return cameraStatus(Status::OPERATION_NOT_SUPPORTED);
335}
336
337ndk::ScopedAStatus VirtualCameraDevice::setTorchMode(bool in_on) {
338 ALOGV("%s: on = %s", __func__, in_on ? "on" : "off");
339 return cameraStatus(Status::OPERATION_NOT_SUPPORTED);
340}
341
342ndk::ScopedAStatus VirtualCameraDevice::turnOnTorchWithStrengthLevel(
343 int32_t in_torchStrength) {
344 ALOGV("%s: torchStrength = %d", __func__, in_torchStrength);
345 return cameraStatus(Status::OPERATION_NOT_SUPPORTED);
346}
347
348ndk::ScopedAStatus VirtualCameraDevice::getTorchStrengthLevel(
349 int32_t* _aidl_return) {
350 (void)_aidl_return;
351 return cameraStatus(Status::OPERATION_NOT_SUPPORTED);
352}
353
354binder_status_t VirtualCameraDevice::dump(int fd, const char** args,
355 uint32_t numArgs) {
356 // TODO(b/301023410) Implement.
357 (void)fd;
358 (void)args;
359 (void)numArgs;
360 return STATUS_OK;
361}
362
363std::string VirtualCameraDevice::getCameraName() const {
364 return std::string(kDevicePathPrefix) + std::to_string(mCameraId);
365}
366
367} // namespace virtualcamera
368} // namespace companion
369} // namespace android