blob: 724ec627dedd3ee31a817334df847959c110f6bc [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
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +010018#define LOG_TAG "VirtualCameraService"
19#include "VirtualCameraService.h"
20
Jan Sebechlebsky773c0142024-03-25 12:17:05 +010021#include <algorithm>
Jan Sebechlebskyb36090e2024-04-11 09:19:33 +020022#include <array>
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +010023#include <cinttypes>
24#include <cstdint>
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +010025#include <memory>
26#include <mutex>
Jan Sebechlebsky773c0142024-03-25 12:17:05 +010027#include <optional>
28#include <regex>
29#include <variant>
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +010030
31#include "VirtualCameraDevice.h"
32#include "VirtualCameraProvider.h"
Jan Sebechlebsky288900f2024-05-24 14:47:54 +020033#include "VirtualCameraTestInstance.h"
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +010034#include "aidl/android/companion/virtualcamera/Format.h"
Jan Sebechlebsky773c0142024-03-25 12:17:05 +010035#include "aidl/android/companion/virtualcamera/LensFacing.h"
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +010036#include "aidl/android/companion/virtualcamera/VirtualCameraConfiguration.h"
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +010037#include "android/binder_auto_utils.h"
Jan Sebechlebsky288900f2024-05-24 14:47:54 +020038#include "android/binder_interface_utils.h"
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +010039#include "android/binder_libbinder.h"
Jan Sebechlebsky773c0142024-03-25 12:17:05 +010040#include "android/binder_status.h"
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +010041#include "binder/Status.h"
Jan Sebechlebskyb36090e2024-04-11 09:19:33 +020042#include "fmt/format.h"
43#include "util/EglDisplayContext.h"
44#include "util/EglUtil.h"
Jan Sebechlebskyde6f16f2023-11-29 09:27:36 +010045#include "util/Permissions.h"
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +010046#include "util/Util.h"
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +010047
48using ::android::binder::Status;
49
50namespace android {
51namespace companion {
52namespace virtualcamera {
53
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +010054using ::aidl::android::companion::virtualcamera::Format;
Biswarup Pal112458f2023-12-28 19:50:17 +000055using ::aidl::android::companion::virtualcamera::LensFacing;
Biswarup Pal6152a302023-12-19 12:44:09 +000056using ::aidl::android::companion::virtualcamera::SensorOrientation;
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +010057using ::aidl::android::companion::virtualcamera::SupportedStreamConfiguration;
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +010058using ::aidl::android::companion::virtualcamera::VirtualCameraConfiguration;
59
Marvin Ramina8196132024-03-15 15:55:22 +000060// TODO(b/301023410) Make camera id range configurable / dynamic
61// based on already registered devices.
62std::atomic_int VirtualCameraService::sNextId{1000};
63
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +010064namespace {
65
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +010066constexpr int kVgaWidth = 640;
67constexpr int kVgaHeight = 480;
Biswarup Pal6152a302023-12-19 12:44:09 +000068constexpr int kMaxFps = 60;
Jan Sebechlebsky34469e42024-05-29 10:56:12 +020069constexpr int kTestCameraDefaultInputFps = 30;
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +010070constexpr char kEnableTestCameraCmd[] = "enable_test_camera";
71constexpr char kDisableTestCameraCmd[] = "disable_test_camera";
Jan Sebechlebsky773c0142024-03-25 12:17:05 +010072constexpr char kHelp[] = "help";
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +010073constexpr char kShellCmdHelp[] = R"(
Jan Sebechlebsky773c0142024-03-25 12:17:05 +010074Usage:
75 cmd virtual_camera command [--option=value]
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +010076Available commands:
77 * enable_test_camera
Jan Sebechlebsky773c0142024-03-25 12:17:05 +010078 Options:
79 --camera_id=(ID) - override numerical ID for test camera instance
80 --lens_facing=(front|back|external) - specifies lens facing for test camera instance
Jan Sebechlebsky34469e42024-05-29 10:56:12 +020081 --input_fps=(fps) - specify input fps for test camera, valid values are from 1 to 1000
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +010082 * disable_test_camera
83)";
Jan Sebechlebskyde6f16f2023-11-29 09:27:36 +010084constexpr char kCreateVirtualDevicePermission[] =
85 "android.permission.CREATE_VIRTUAL_DEVICE";
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +010086
Jan Sebechlebskyb36090e2024-04-11 09:19:33 +020087constexpr std::array<const char*, 3> kRequiredEglExtensions = {
88 "GL_OES_EGL_image_external",
89 "GL_OES_EGL_image_external_essl3",
90 "GL_EXT_YUV_target",
91};
92
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +010093ndk::ScopedAStatus validateConfiguration(
94 const VirtualCameraConfiguration& configuration) {
95 if (configuration.supportedStreamConfigs.empty()) {
96 ALOGE("%s: No supported input configuration specified", __func__);
97 return ndk::ScopedAStatus::fromServiceSpecificError(
98 Status::EX_ILLEGAL_ARGUMENT);
99 }
100
Jan Sebechlebsky288900f2024-05-24 14:47:54 +0200101 if (configuration.virtualCameraCallback == nullptr) {
102 ALOGE("%s: Input configuration is missing virtual camera callback",
103 __func__);
104 return ndk::ScopedAStatus::fromServiceSpecificError(
105 Status::EX_ILLEGAL_ARGUMENT);
106 }
107
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +0100108 for (const SupportedStreamConfiguration& config :
109 configuration.supportedStreamConfigs) {
110 if (!isFormatSupportedForInput(config.width, config.height,
Biswarup Pal6152a302023-12-19 12:44:09 +0000111 config.pixelFormat, config.maxFps)) {
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +0100112 ALOGE("%s: Requested unsupported input format: %d x %d (%d)", __func__,
113 config.width, config.height, static_cast<int>(config.pixelFormat));
114 return ndk::ScopedAStatus::fromServiceSpecificError(
115 Status::EX_ILLEGAL_ARGUMENT);
116 }
117 }
Biswarup Pal6152a302023-12-19 12:44:09 +0000118
119 if (configuration.sensorOrientation != SensorOrientation::ORIENTATION_0 &&
120 configuration.sensorOrientation != SensorOrientation::ORIENTATION_90 &&
121 configuration.sensorOrientation != SensorOrientation::ORIENTATION_180 &&
122 configuration.sensorOrientation != SensorOrientation::ORIENTATION_270) {
123 return ndk::ScopedAStatus::fromServiceSpecificError(
124 Status::EX_ILLEGAL_ARGUMENT);
125 }
126
Biswarup Pal112458f2023-12-28 19:50:17 +0000127 if (configuration.lensFacing != LensFacing::FRONT &&
128 configuration.lensFacing != LensFacing::BACK &&
129 configuration.lensFacing != LensFacing::EXTERNAL) {
130 return ndk::ScopedAStatus::fromServiceSpecificError(
131 Status::EX_ILLEGAL_ARGUMENT);
132 }
133
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +0100134 return ndk::ScopedAStatus::ok();
135}
136
Jan Sebechlebsky773c0142024-03-25 12:17:05 +0100137enum class Command {
138 ENABLE_TEST_CAMERA,
139 DISABLE_TEST_CAMERA,
140 HELP,
141};
142
143struct CommandWithOptions {
144 Command command;
145 std::map<std::string, std::string> optionToValueMap;
146};
147
148std::optional<int> parseInt(const std::string& s) {
149 if (!std::all_of(s.begin(), s.end(), [](char c) { return std::isdigit(c); })) {
150 return std::nullopt;
151 }
152 int ret = atoi(s.c_str());
153 return ret > 0 ? std::optional(ret) : std::nullopt;
154}
155
156std::optional<LensFacing> parseLensFacing(const std::string& s) {
157 static const std::map<std::string, LensFacing> strToLensFacing{
158 {"front", LensFacing::FRONT},
159 {"back", LensFacing::BACK},
160 {"external", LensFacing::EXTERNAL}};
161 auto it = strToLensFacing.find(s);
162 return it == strToLensFacing.end() ? std::nullopt : std::optional(it->second);
163}
164
165std::variant<CommandWithOptions, std::string> parseCommand(
166 const char** args, const uint32_t numArgs) {
167 static const std::regex optionRegex("^--(\\w+)(?:=(.+))?$");
168 static const std::map<std::string, Command> strToCommand{
169 {kHelp, Command::HELP},
170 {kEnableTestCameraCmd, Command::ENABLE_TEST_CAMERA},
171 {kDisableTestCameraCmd, Command::DISABLE_TEST_CAMERA}};
172
173 if (numArgs < 1) {
174 return CommandWithOptions{.command = Command::HELP};
175 }
176
177 // We interpret the first argument as command;
178 auto it = strToCommand.find(args[0]);
179 if (it == strToCommand.end()) {
180 return "Unknown command: " + std::string(args[0]);
181 }
182
183 CommandWithOptions cmd{.command = it->second};
184
185 for (int i = 1; i < numArgs; i++) {
186 std::cmatch cm;
187 if (!std::regex_match(args[i], cm, optionRegex)) {
188 return "Not an option: " + std::string(args[i]);
189 }
190
191 cmd.optionToValueMap[cm[1]] = cm[2];
192 }
193
194 return cmd;
195};
196
Jan Sebechlebskyb36090e2024-04-11 09:19:33 +0200197ndk::ScopedAStatus verifyRequiredEglExtensions() {
198 EglDisplayContext context;
199 for (const char* eglExtension : kRequiredEglExtensions) {
200 if (!isGlExtensionSupported(eglExtension)) {
201 ALOGE("%s not supported", eglExtension);
202 return ndk::ScopedAStatus::fromExceptionCodeWithMessage(
203 EX_UNSUPPORTED_OPERATION,
204 fmt::format(
205 "Cannot create virtual camera, because required EGL extension {} "
206 "is not supported on this system",
207 eglExtension)
208 .c_str());
209 }
210 }
211 return ndk::ScopedAStatus::ok();
212}
213
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100214} // namespace
215
216VirtualCameraService::VirtualCameraService(
Jan Sebechlebskyde6f16f2023-11-29 09:27:36 +0100217 std::shared_ptr<VirtualCameraProvider> virtualCameraProvider,
218 const PermissionsProxy& permissionProxy)
219 : mVirtualCameraProvider(virtualCameraProvider),
220 mPermissionProxy(permissionProxy) {
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100221}
222
223ndk::ScopedAStatus VirtualCameraService::registerCamera(
224 const ::ndk::SpAIBinder& token,
Biswarup Pal37a75182024-01-16 15:53:35 +0000225 const VirtualCameraConfiguration& configuration, const int32_t deviceId,
226 bool* _aidl_return) {
227 return registerCamera(token, configuration, sNextId++, deviceId, _aidl_return);
Marvin Ramina8196132024-03-15 15:55:22 +0000228}
229
230ndk::ScopedAStatus VirtualCameraService::registerCamera(
231 const ::ndk::SpAIBinder& token,
232 const VirtualCameraConfiguration& configuration, const int cameraId,
Biswarup Pal37a75182024-01-16 15:53:35 +0000233 const int32_t deviceId, bool* _aidl_return) {
Jan Sebechlebskyde6f16f2023-11-29 09:27:36 +0100234 if (!mPermissionProxy.checkCallingPermission(kCreateVirtualDevicePermission)) {
235 ALOGE("%s: caller (pid %d, uid %d) doesn't hold %s permission", __func__,
236 getpid(), getuid(), kCreateVirtualDevicePermission);
237 return ndk::ScopedAStatus::fromExceptionCode(EX_SECURITY);
238 }
239
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100240 if (_aidl_return == nullptr) {
241 return ndk::ScopedAStatus::fromServiceSpecificError(
242 Status::EX_ILLEGAL_ARGUMENT);
243 }
Jan Sebechlebskyde6f16f2023-11-29 09:27:36 +0100244
Jan Sebechlebskyb36090e2024-04-11 09:19:33 +0200245 if (mVerifyEglExtensions) {
246 auto status = verifyRequiredEglExtensions();
247 if (!status.isOk()) {
248 *_aidl_return = false;
249 return status;
250 }
251 }
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100252
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +0100253 auto status = validateConfiguration(configuration);
254 if (!status.isOk()) {
255 *_aidl_return = false;
256 return status;
257 }
258
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100259 std::lock_guard lock(mLock);
260 if (mTokenToCameraName.find(token) != mTokenToCameraName.end()) {
261 ALOGE(
262 "Attempt to register camera corresponding to already registered binder "
263 "token: "
264 "0x%" PRIxPTR,
265 reinterpret_cast<uintptr_t>(token.get()));
266 *_aidl_return = false;
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +0100267 return ndk::ScopedAStatus::ok();
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100268 }
269
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100270 std::shared_ptr<VirtualCameraDevice> camera =
Biswarup Pal37a75182024-01-16 15:53:35 +0000271 mVirtualCameraProvider->createCamera(configuration, cameraId, deviceId);
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100272 if (camera == nullptr) {
273 ALOGE("Failed to create camera for binder token 0x%" PRIxPTR,
274 reinterpret_cast<uintptr_t>(token.get()));
275 *_aidl_return = false;
276 return ndk::ScopedAStatus::fromServiceSpecificError(
277 Status::EX_SERVICE_SPECIFIC);
278 }
279
280 mTokenToCameraName[token] = camera->getCameraName();
Jan Sebechlebskyb36090e2024-04-11 09:19:33 +0200281 *_aidl_return = true;
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100282 return ndk::ScopedAStatus::ok();
283}
284
285ndk::ScopedAStatus VirtualCameraService::unregisterCamera(
286 const ::ndk::SpAIBinder& token) {
Jan Sebechlebskyde6f16f2023-11-29 09:27:36 +0100287 if (!mPermissionProxy.checkCallingPermission(kCreateVirtualDevicePermission)) {
288 ALOGE("%s: caller (pid %d, uid %d) doesn't hold %s permission", __func__,
289 getpid(), getuid(), kCreateVirtualDevicePermission);
290 return ndk::ScopedAStatus::fromExceptionCode(EX_SECURITY);
291 }
292
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100293 std::lock_guard lock(mLock);
294
295 auto it = mTokenToCameraName.find(token);
296 if (it == mTokenToCameraName.end()) {
297 ALOGE(
298 "Attempt to unregister camera corresponding to unknown binder token: "
299 "0x%" PRIxPTR,
300 reinterpret_cast<uintptr_t>(token.get()));
301 return ndk::ScopedAStatus::ok();
302 }
303
304 mVirtualCameraProvider->removeCamera(it->second);
305
Tony Guo6cbe11b2024-03-17 02:34:23 +0000306 mTokenToCameraName.erase(it);
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100307 return ndk::ScopedAStatus::ok();
308}
309
Biswarup Pal68137fc2023-11-24 18:06:54 +0000310ndk::ScopedAStatus VirtualCameraService::getCameraId(
Marvin Ramina8196132024-03-15 15:55:22 +0000311 const ::ndk::SpAIBinder& token, int32_t* _aidl_return) {
Jan Sebechlebskyde6f16f2023-11-29 09:27:36 +0100312 if (!mPermissionProxy.checkCallingPermission(kCreateVirtualDevicePermission)) {
313 ALOGE("%s: caller (pid %d, uid %d) doesn't hold %s permission", __func__,
314 getpid(), getuid(), kCreateVirtualDevicePermission);
315 return ndk::ScopedAStatus::fromExceptionCode(EX_SECURITY);
316 }
317
Biswarup Pal68137fc2023-11-24 18:06:54 +0000318 if (_aidl_return == nullptr) {
319 return ndk::ScopedAStatus::fromServiceSpecificError(
Marvin Ramina8196132024-03-15 15:55:22 +0000320 Status::EX_ILLEGAL_ARGUMENT);
Biswarup Pal68137fc2023-11-24 18:06:54 +0000321 }
322
323 auto camera = getCamera(token);
324 if (camera == nullptr) {
325 ALOGE(
326 "Attempt to get camera id corresponding to unknown binder token: "
327 "0x%" PRIxPTR,
328 reinterpret_cast<uintptr_t>(token.get()));
329 return ndk::ScopedAStatus::ok();
330 }
331
332 *_aidl_return = camera->getCameraId();
333
334 return ndk::ScopedAStatus::ok();
335}
336
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100337std::shared_ptr<VirtualCameraDevice> VirtualCameraService::getCamera(
338 const ::ndk::SpAIBinder& token) {
339 if (token == nullptr) {
340 return nullptr;
341 }
342
343 std::lock_guard lock(mLock);
344 auto it = mTokenToCameraName.find(token);
345 if (it == mTokenToCameraName.end()) {
346 return nullptr;
347 }
348
349 return mVirtualCameraProvider->getCamera(it->second);
350}
351
Jan Sebechlebsky773c0142024-03-25 12:17:05 +0100352binder_status_t VirtualCameraService::handleShellCommand(int, int out, int err,
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100353 const char** args,
354 uint32_t numArgs) {
355 if (numArgs <= 0) {
356 dprintf(out, kShellCmdHelp);
Jan Sebechlebsky76d7e212023-11-28 14:10:25 +0100357 fsync(out);
358 return STATUS_OK;
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100359 }
360
Jan Sebechlebsky773c0142024-03-25 12:17:05 +0100361 auto isNullptr = [](const char* ptr) { return ptr == nullptr; };
362 if (args == nullptr || std::any_of(args, args + numArgs, isNullptr)) {
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100363 return STATUS_BAD_VALUE;
364 }
Marvin Ramina8196132024-03-15 15:55:22 +0000365
Jan Sebechlebsky773c0142024-03-25 12:17:05 +0100366 std::variant<CommandWithOptions, std::string> cmdOrErrorMessage =
367 parseCommand(args, numArgs);
368 if (std::holds_alternative<std::string>(cmdOrErrorMessage)) {
369 dprintf(err, "Error: %s\n",
370 std::get<std::string>(cmdOrErrorMessage).c_str());
371 return STATUS_BAD_VALUE;
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100372 }
373
Jan Sebechlebsky773c0142024-03-25 12:17:05 +0100374 const CommandWithOptions& cmd =
375 std::get<CommandWithOptions>(cmdOrErrorMessage);
376 binder_status_t status = STATUS_OK;
377 switch (cmd.command) {
378 case Command::HELP:
379 dprintf(out, kShellCmdHelp);
380 break;
381 case Command::ENABLE_TEST_CAMERA:
382 status = enableTestCameraCmd(out, err, cmd.optionToValueMap);
383 break;
384 case Command::DISABLE_TEST_CAMERA:
385 disableTestCameraCmd(out);
386 break;
387 }
388
389 fsync(err);
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100390 fsync(out);
Jan Sebechlebsky773c0142024-03-25 12:17:05 +0100391 return status;
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100392}
393
Jan Sebechlebsky773c0142024-03-25 12:17:05 +0100394binder_status_t VirtualCameraService::enableTestCameraCmd(
395 const int out, const int err,
396 const std::map<std::string, std::string>& options) {
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100397 if (mTestCameraToken != nullptr) {
Jan Sebechlebsky773c0142024-03-25 12:17:05 +0100398 dprintf(out, "Test camera is already enabled (%s).\n",
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100399 getCamera(mTestCameraToken)->getCameraName().c_str());
Jan Sebechlebsky773c0142024-03-25 12:17:05 +0100400 return STATUS_OK;
401 }
402
403 std::optional<int> cameraId;
404 auto it = options.find("camera_id");
405 if (it != options.end()) {
406 cameraId = parseInt(it->second);
407 if (!cameraId.has_value()) {
408 dprintf(err, "Invalid camera_id: %s\n, must be number > 0",
409 it->second.c_str());
410 return STATUS_BAD_VALUE;
411 }
412 }
413
414 std::optional<LensFacing> lensFacing;
415 it = options.find("lens_facing");
416 if (it != options.end()) {
417 lensFacing = parseLensFacing(it->second);
418 if (!lensFacing.has_value()) {
419 dprintf(err, "Invalid lens_facing: %s\n, must be front|back|external",
420 it->second.c_str());
421 return STATUS_BAD_VALUE;
422 }
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100423 }
424
Jan Sebechlebsky34469e42024-05-29 10:56:12 +0200425 std::optional<int> inputFps;
426 it = options.find("input_fps");
427 if (it != options.end()) {
428 inputFps = parseInt(it->second);
429 if (!inputFps.has_value() || inputFps.value() < 1 ||
430 inputFps.value() > 1000) {
431 dprintf(err, "Invalid input fps: %s\n, must be integer in <1,1000> range.",
432 it->second.c_str());
433 return STATUS_BAD_VALUE;
434 }
435 }
436
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100437 sp<BBinder> token = sp<BBinder>::make();
438 mTestCameraToken.set(AIBinder_fromPlatformBinder(token));
439
440 bool ret;
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +0100441 VirtualCameraConfiguration configuration;
Biswarup Pal6152a302023-12-19 12:44:09 +0000442 configuration.supportedStreamConfigs.push_back({.width = kVgaWidth,
443 .height = kVgaHeight,
Jan Sebechlebsky288900f2024-05-24 14:47:54 +0200444 Format::RGBA_8888,
Biswarup Pal6152a302023-12-19 12:44:09 +0000445 .maxFps = kMaxFps});
Jan Sebechlebsky773c0142024-03-25 12:17:05 +0100446 configuration.lensFacing = lensFacing.value_or(LensFacing::EXTERNAL);
Jan Sebechlebsky288900f2024-05-24 14:47:54 +0200447 configuration.virtualCameraCallback =
Jan Sebechlebsky34469e42024-05-29 10:56:12 +0200448 ndk::SharedRefBase::make<VirtualCameraTestInstance>(
449 inputFps.value_or(kTestCameraDefaultInputFps));
Jan Sebechlebsky773c0142024-03-25 12:17:05 +0100450 registerCamera(mTestCameraToken, configuration, cameraId.value_or(sNextId++),
Biswarup Pal37a75182024-01-16 15:53:35 +0000451 kDefaultDeviceId, &ret);
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100452 if (ret) {
Jan Sebechlebsky773c0142024-03-25 12:17:05 +0100453 dprintf(out, "Successfully registered test camera %s\n",
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100454 getCamera(mTestCameraToken)->getCameraName().c_str());
455 } else {
Jan Sebechlebsky773c0142024-03-25 12:17:05 +0100456 dprintf(err, "Failed to create test camera\n");
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100457 }
Jan Sebechlebsky773c0142024-03-25 12:17:05 +0100458 return STATUS_OK;
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100459}
460
461void VirtualCameraService::disableTestCameraCmd(const int out) {
462 if (mTestCameraToken == nullptr) {
463 dprintf(out, "Test camera is not registered.");
464 }
465 unregisterCamera(mTestCameraToken);
466 mTestCameraToken.set(nullptr);
467}
468
469} // namespace virtualcamera
470} // namespace companion
471} // namespace android