Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 1 | /* |
Biswarup Pal | 6152a30 | 2023-12-19 12:44:09 +0000 | [diff] [blame] | 2 | * Copyright 2023 The Android Open Source Project |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 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 |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 18 | #define LOG_TAG "VirtualCameraService" |
| 19 | #include "VirtualCameraService.h" |
| 20 | |
Jan Sebechlebsky | 773c014 | 2024-03-25 12:17:05 +0100 | [diff] [blame] | 21 | #include <algorithm> |
Jan Sebechlebsky | b36090e | 2024-04-11 09:19:33 +0200 | [diff] [blame^] | 22 | #include <array> |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 23 | #include <cinttypes> |
| 24 | #include <cstdint> |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 25 | #include <memory> |
| 26 | #include <mutex> |
Jan Sebechlebsky | 773c014 | 2024-03-25 12:17:05 +0100 | [diff] [blame] | 27 | #include <optional> |
| 28 | #include <regex> |
| 29 | #include <variant> |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 30 | |
| 31 | #include "VirtualCameraDevice.h" |
| 32 | #include "VirtualCameraProvider.h" |
Jan Sebechlebsky | 3b478c4 | 2023-11-23 13:15:56 +0100 | [diff] [blame] | 33 | #include "aidl/android/companion/virtualcamera/Format.h" |
Jan Sebechlebsky | 773c014 | 2024-03-25 12:17:05 +0100 | [diff] [blame] | 34 | #include "aidl/android/companion/virtualcamera/LensFacing.h" |
Jan Sebechlebsky | 3b478c4 | 2023-11-23 13:15:56 +0100 | [diff] [blame] | 35 | #include "aidl/android/companion/virtualcamera/VirtualCameraConfiguration.h" |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 36 | #include "android/binder_auto_utils.h" |
| 37 | #include "android/binder_libbinder.h" |
Jan Sebechlebsky | 773c014 | 2024-03-25 12:17:05 +0100 | [diff] [blame] | 38 | #include "android/binder_status.h" |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 39 | #include "binder/Status.h" |
Jan Sebechlebsky | b36090e | 2024-04-11 09:19:33 +0200 | [diff] [blame^] | 40 | #include "fmt/format.h" |
| 41 | #include "util/EglDisplayContext.h" |
| 42 | #include "util/EglUtil.h" |
Jan Sebechlebsky | de6f16f | 2023-11-29 09:27:36 +0100 | [diff] [blame] | 43 | #include "util/Permissions.h" |
Jan Sebechlebsky | 3b478c4 | 2023-11-23 13:15:56 +0100 | [diff] [blame] | 44 | #include "util/Util.h" |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 45 | |
| 46 | using ::android::binder::Status; |
| 47 | |
| 48 | namespace android { |
| 49 | namespace companion { |
| 50 | namespace virtualcamera { |
| 51 | |
Jan Sebechlebsky | 3b478c4 | 2023-11-23 13:15:56 +0100 | [diff] [blame] | 52 | using ::aidl::android::companion::virtualcamera::Format; |
Biswarup Pal | 112458f | 2023-12-28 19:50:17 +0000 | [diff] [blame] | 53 | using ::aidl::android::companion::virtualcamera::LensFacing; |
Biswarup Pal | 6152a30 | 2023-12-19 12:44:09 +0000 | [diff] [blame] | 54 | using ::aidl::android::companion::virtualcamera::SensorOrientation; |
Jan Sebechlebsky | 3b478c4 | 2023-11-23 13:15:56 +0100 | [diff] [blame] | 55 | using ::aidl::android::companion::virtualcamera::SupportedStreamConfiguration; |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 56 | using ::aidl::android::companion::virtualcamera::VirtualCameraConfiguration; |
| 57 | |
Marvin Ramin | a819613 | 2024-03-15 15:55:22 +0000 | [diff] [blame] | 58 | // TODO(b/301023410) Make camera id range configurable / dynamic |
| 59 | // based on already registered devices. |
| 60 | std::atomic_int VirtualCameraService::sNextId{1000}; |
| 61 | |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 62 | namespace { |
| 63 | |
Jan Sebechlebsky | 3b478c4 | 2023-11-23 13:15:56 +0100 | [diff] [blame] | 64 | constexpr int kVgaWidth = 640; |
| 65 | constexpr int kVgaHeight = 480; |
Biswarup Pal | 6152a30 | 2023-12-19 12:44:09 +0000 | [diff] [blame] | 66 | constexpr int kMaxFps = 60; |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 67 | constexpr int kDefaultDeviceId = 0; |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 68 | constexpr char kEnableTestCameraCmd[] = "enable_test_camera"; |
| 69 | constexpr char kDisableTestCameraCmd[] = "disable_test_camera"; |
Jan Sebechlebsky | 773c014 | 2024-03-25 12:17:05 +0100 | [diff] [blame] | 70 | constexpr char kHelp[] = "help"; |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 71 | constexpr char kShellCmdHelp[] = R"( |
Jan Sebechlebsky | 773c014 | 2024-03-25 12:17:05 +0100 | [diff] [blame] | 72 | Usage: |
| 73 | cmd virtual_camera command [--option=value] |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 74 | Available commands: |
| 75 | * enable_test_camera |
Jan Sebechlebsky | 773c014 | 2024-03-25 12:17:05 +0100 | [diff] [blame] | 76 | Options: |
| 77 | --camera_id=(ID) - override numerical ID for test camera instance |
| 78 | --lens_facing=(front|back|external) - specifies lens facing for test camera instance |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 79 | * disable_test_camera |
| 80 | )"; |
Jan Sebechlebsky | de6f16f | 2023-11-29 09:27:36 +0100 | [diff] [blame] | 81 | constexpr char kCreateVirtualDevicePermission[] = |
| 82 | "android.permission.CREATE_VIRTUAL_DEVICE"; |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 83 | |
Jan Sebechlebsky | b36090e | 2024-04-11 09:19:33 +0200 | [diff] [blame^] | 84 | constexpr std::array<const char*, 3> kRequiredEglExtensions = { |
| 85 | "GL_OES_EGL_image_external", |
| 86 | "GL_OES_EGL_image_external_essl3", |
| 87 | "GL_EXT_YUV_target", |
| 88 | }; |
| 89 | |
Jan Sebechlebsky | 3b478c4 | 2023-11-23 13:15:56 +0100 | [diff] [blame] | 90 | ndk::ScopedAStatus validateConfiguration( |
| 91 | const VirtualCameraConfiguration& configuration) { |
| 92 | if (configuration.supportedStreamConfigs.empty()) { |
| 93 | ALOGE("%s: No supported input configuration specified", __func__); |
| 94 | return ndk::ScopedAStatus::fromServiceSpecificError( |
| 95 | Status::EX_ILLEGAL_ARGUMENT); |
| 96 | } |
| 97 | |
| 98 | for (const SupportedStreamConfiguration& config : |
| 99 | configuration.supportedStreamConfigs) { |
| 100 | if (!isFormatSupportedForInput(config.width, config.height, |
Biswarup Pal | 6152a30 | 2023-12-19 12:44:09 +0000 | [diff] [blame] | 101 | config.pixelFormat, config.maxFps)) { |
Jan Sebechlebsky | 3b478c4 | 2023-11-23 13:15:56 +0100 | [diff] [blame] | 102 | ALOGE("%s: Requested unsupported input format: %d x %d (%d)", __func__, |
| 103 | config.width, config.height, static_cast<int>(config.pixelFormat)); |
| 104 | return ndk::ScopedAStatus::fromServiceSpecificError( |
| 105 | Status::EX_ILLEGAL_ARGUMENT); |
| 106 | } |
| 107 | } |
Biswarup Pal | 6152a30 | 2023-12-19 12:44:09 +0000 | [diff] [blame] | 108 | |
| 109 | if (configuration.sensorOrientation != SensorOrientation::ORIENTATION_0 && |
| 110 | configuration.sensorOrientation != SensorOrientation::ORIENTATION_90 && |
| 111 | configuration.sensorOrientation != SensorOrientation::ORIENTATION_180 && |
| 112 | configuration.sensorOrientation != SensorOrientation::ORIENTATION_270) { |
| 113 | return ndk::ScopedAStatus::fromServiceSpecificError( |
| 114 | Status::EX_ILLEGAL_ARGUMENT); |
| 115 | } |
| 116 | |
Biswarup Pal | 112458f | 2023-12-28 19:50:17 +0000 | [diff] [blame] | 117 | if (configuration.lensFacing != LensFacing::FRONT && |
| 118 | configuration.lensFacing != LensFacing::BACK && |
| 119 | configuration.lensFacing != LensFacing::EXTERNAL) { |
| 120 | return ndk::ScopedAStatus::fromServiceSpecificError( |
| 121 | Status::EX_ILLEGAL_ARGUMENT); |
| 122 | } |
| 123 | |
Jan Sebechlebsky | 3b478c4 | 2023-11-23 13:15:56 +0100 | [diff] [blame] | 124 | return ndk::ScopedAStatus::ok(); |
| 125 | } |
| 126 | |
Jan Sebechlebsky | 773c014 | 2024-03-25 12:17:05 +0100 | [diff] [blame] | 127 | enum class Command { |
| 128 | ENABLE_TEST_CAMERA, |
| 129 | DISABLE_TEST_CAMERA, |
| 130 | HELP, |
| 131 | }; |
| 132 | |
| 133 | struct CommandWithOptions { |
| 134 | Command command; |
| 135 | std::map<std::string, std::string> optionToValueMap; |
| 136 | }; |
| 137 | |
| 138 | std::optional<int> parseInt(const std::string& s) { |
| 139 | if (!std::all_of(s.begin(), s.end(), [](char c) { return std::isdigit(c); })) { |
| 140 | return std::nullopt; |
| 141 | } |
| 142 | int ret = atoi(s.c_str()); |
| 143 | return ret > 0 ? std::optional(ret) : std::nullopt; |
| 144 | } |
| 145 | |
| 146 | std::optional<LensFacing> parseLensFacing(const std::string& s) { |
| 147 | static const std::map<std::string, LensFacing> strToLensFacing{ |
| 148 | {"front", LensFacing::FRONT}, |
| 149 | {"back", LensFacing::BACK}, |
| 150 | {"external", LensFacing::EXTERNAL}}; |
| 151 | auto it = strToLensFacing.find(s); |
| 152 | return it == strToLensFacing.end() ? std::nullopt : std::optional(it->second); |
| 153 | } |
| 154 | |
| 155 | std::variant<CommandWithOptions, std::string> parseCommand( |
| 156 | const char** args, const uint32_t numArgs) { |
| 157 | static const std::regex optionRegex("^--(\\w+)(?:=(.+))?$"); |
| 158 | static const std::map<std::string, Command> strToCommand{ |
| 159 | {kHelp, Command::HELP}, |
| 160 | {kEnableTestCameraCmd, Command::ENABLE_TEST_CAMERA}, |
| 161 | {kDisableTestCameraCmd, Command::DISABLE_TEST_CAMERA}}; |
| 162 | |
| 163 | if (numArgs < 1) { |
| 164 | return CommandWithOptions{.command = Command::HELP}; |
| 165 | } |
| 166 | |
| 167 | // We interpret the first argument as command; |
| 168 | auto it = strToCommand.find(args[0]); |
| 169 | if (it == strToCommand.end()) { |
| 170 | return "Unknown command: " + std::string(args[0]); |
| 171 | } |
| 172 | |
| 173 | CommandWithOptions cmd{.command = it->second}; |
| 174 | |
| 175 | for (int i = 1; i < numArgs; i++) { |
| 176 | std::cmatch cm; |
| 177 | if (!std::regex_match(args[i], cm, optionRegex)) { |
| 178 | return "Not an option: " + std::string(args[i]); |
| 179 | } |
| 180 | |
| 181 | cmd.optionToValueMap[cm[1]] = cm[2]; |
| 182 | } |
| 183 | |
| 184 | return cmd; |
| 185 | }; |
| 186 | |
Jan Sebechlebsky | b36090e | 2024-04-11 09:19:33 +0200 | [diff] [blame^] | 187 | ndk::ScopedAStatus verifyRequiredEglExtensions() { |
| 188 | EglDisplayContext context; |
| 189 | for (const char* eglExtension : kRequiredEglExtensions) { |
| 190 | if (!isGlExtensionSupported(eglExtension)) { |
| 191 | ALOGE("%s not supported", eglExtension); |
| 192 | return ndk::ScopedAStatus::fromExceptionCodeWithMessage( |
| 193 | EX_UNSUPPORTED_OPERATION, |
| 194 | fmt::format( |
| 195 | "Cannot create virtual camera, because required EGL extension {} " |
| 196 | "is not supported on this system", |
| 197 | eglExtension) |
| 198 | .c_str()); |
| 199 | } |
| 200 | } |
| 201 | return ndk::ScopedAStatus::ok(); |
| 202 | } |
| 203 | |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 204 | } // namespace |
| 205 | |
| 206 | VirtualCameraService::VirtualCameraService( |
Jan Sebechlebsky | de6f16f | 2023-11-29 09:27:36 +0100 | [diff] [blame] | 207 | std::shared_ptr<VirtualCameraProvider> virtualCameraProvider, |
| 208 | const PermissionsProxy& permissionProxy) |
| 209 | : mVirtualCameraProvider(virtualCameraProvider), |
| 210 | mPermissionProxy(permissionProxy) { |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | ndk::ScopedAStatus VirtualCameraService::registerCamera( |
| 214 | const ::ndk::SpAIBinder& token, |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 215 | const VirtualCameraConfiguration& configuration, const int32_t deviceId, |
| 216 | bool* _aidl_return) { |
| 217 | return registerCamera(token, configuration, sNextId++, deviceId, _aidl_return); |
Marvin Ramin | a819613 | 2024-03-15 15:55:22 +0000 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | ndk::ScopedAStatus VirtualCameraService::registerCamera( |
| 221 | const ::ndk::SpAIBinder& token, |
| 222 | const VirtualCameraConfiguration& configuration, const int cameraId, |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 223 | const int32_t deviceId, bool* _aidl_return) { |
Jan Sebechlebsky | de6f16f | 2023-11-29 09:27:36 +0100 | [diff] [blame] | 224 | if (!mPermissionProxy.checkCallingPermission(kCreateVirtualDevicePermission)) { |
| 225 | ALOGE("%s: caller (pid %d, uid %d) doesn't hold %s permission", __func__, |
| 226 | getpid(), getuid(), kCreateVirtualDevicePermission); |
| 227 | return ndk::ScopedAStatus::fromExceptionCode(EX_SECURITY); |
| 228 | } |
| 229 | |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 230 | if (_aidl_return == nullptr) { |
| 231 | return ndk::ScopedAStatus::fromServiceSpecificError( |
| 232 | Status::EX_ILLEGAL_ARGUMENT); |
| 233 | } |
Jan Sebechlebsky | de6f16f | 2023-11-29 09:27:36 +0100 | [diff] [blame] | 234 | |
Jan Sebechlebsky | b36090e | 2024-04-11 09:19:33 +0200 | [diff] [blame^] | 235 | if (mVerifyEglExtensions) { |
| 236 | auto status = verifyRequiredEglExtensions(); |
| 237 | if (!status.isOk()) { |
| 238 | *_aidl_return = false; |
| 239 | return status; |
| 240 | } |
| 241 | } |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 242 | |
Jan Sebechlebsky | 3b478c4 | 2023-11-23 13:15:56 +0100 | [diff] [blame] | 243 | auto status = validateConfiguration(configuration); |
| 244 | if (!status.isOk()) { |
| 245 | *_aidl_return = false; |
| 246 | return status; |
| 247 | } |
| 248 | |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 249 | std::lock_guard lock(mLock); |
| 250 | if (mTokenToCameraName.find(token) != mTokenToCameraName.end()) { |
| 251 | ALOGE( |
| 252 | "Attempt to register camera corresponding to already registered binder " |
| 253 | "token: " |
| 254 | "0x%" PRIxPTR, |
| 255 | reinterpret_cast<uintptr_t>(token.get())); |
| 256 | *_aidl_return = false; |
Jan Sebechlebsky | 3b478c4 | 2023-11-23 13:15:56 +0100 | [diff] [blame] | 257 | return ndk::ScopedAStatus::ok(); |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 258 | } |
| 259 | |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 260 | std::shared_ptr<VirtualCameraDevice> camera = |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 261 | mVirtualCameraProvider->createCamera(configuration, cameraId, deviceId); |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 262 | if (camera == nullptr) { |
| 263 | ALOGE("Failed to create camera for binder token 0x%" PRIxPTR, |
| 264 | reinterpret_cast<uintptr_t>(token.get())); |
| 265 | *_aidl_return = false; |
| 266 | return ndk::ScopedAStatus::fromServiceSpecificError( |
| 267 | Status::EX_SERVICE_SPECIFIC); |
| 268 | } |
| 269 | |
| 270 | mTokenToCameraName[token] = camera->getCameraName(); |
Jan Sebechlebsky | b36090e | 2024-04-11 09:19:33 +0200 | [diff] [blame^] | 271 | *_aidl_return = true; |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 272 | return ndk::ScopedAStatus::ok(); |
| 273 | } |
| 274 | |
| 275 | ndk::ScopedAStatus VirtualCameraService::unregisterCamera( |
| 276 | const ::ndk::SpAIBinder& token) { |
Jan Sebechlebsky | de6f16f | 2023-11-29 09:27:36 +0100 | [diff] [blame] | 277 | if (!mPermissionProxy.checkCallingPermission(kCreateVirtualDevicePermission)) { |
| 278 | ALOGE("%s: caller (pid %d, uid %d) doesn't hold %s permission", __func__, |
| 279 | getpid(), getuid(), kCreateVirtualDevicePermission); |
| 280 | return ndk::ScopedAStatus::fromExceptionCode(EX_SECURITY); |
| 281 | } |
| 282 | |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 283 | std::lock_guard lock(mLock); |
| 284 | |
| 285 | auto it = mTokenToCameraName.find(token); |
| 286 | if (it == mTokenToCameraName.end()) { |
| 287 | ALOGE( |
| 288 | "Attempt to unregister camera corresponding to unknown binder token: " |
| 289 | "0x%" PRIxPTR, |
| 290 | reinterpret_cast<uintptr_t>(token.get())); |
| 291 | return ndk::ScopedAStatus::ok(); |
| 292 | } |
| 293 | |
| 294 | mVirtualCameraProvider->removeCamera(it->second); |
| 295 | |
Tony Guo | 6cbe11b | 2024-03-17 02:34:23 +0000 | [diff] [blame] | 296 | mTokenToCameraName.erase(it); |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 297 | return ndk::ScopedAStatus::ok(); |
| 298 | } |
| 299 | |
Biswarup Pal | 68137fc | 2023-11-24 18:06:54 +0000 | [diff] [blame] | 300 | ndk::ScopedAStatus VirtualCameraService::getCameraId( |
Marvin Ramin | a819613 | 2024-03-15 15:55:22 +0000 | [diff] [blame] | 301 | const ::ndk::SpAIBinder& token, int32_t* _aidl_return) { |
Jan Sebechlebsky | de6f16f | 2023-11-29 09:27:36 +0100 | [diff] [blame] | 302 | if (!mPermissionProxy.checkCallingPermission(kCreateVirtualDevicePermission)) { |
| 303 | ALOGE("%s: caller (pid %d, uid %d) doesn't hold %s permission", __func__, |
| 304 | getpid(), getuid(), kCreateVirtualDevicePermission); |
| 305 | return ndk::ScopedAStatus::fromExceptionCode(EX_SECURITY); |
| 306 | } |
| 307 | |
Biswarup Pal | 68137fc | 2023-11-24 18:06:54 +0000 | [diff] [blame] | 308 | if (_aidl_return == nullptr) { |
| 309 | return ndk::ScopedAStatus::fromServiceSpecificError( |
Marvin Ramin | a819613 | 2024-03-15 15:55:22 +0000 | [diff] [blame] | 310 | Status::EX_ILLEGAL_ARGUMENT); |
Biswarup Pal | 68137fc | 2023-11-24 18:06:54 +0000 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | auto camera = getCamera(token); |
| 314 | if (camera == nullptr) { |
| 315 | ALOGE( |
| 316 | "Attempt to get camera id corresponding to unknown binder token: " |
| 317 | "0x%" PRIxPTR, |
| 318 | reinterpret_cast<uintptr_t>(token.get())); |
| 319 | return ndk::ScopedAStatus::ok(); |
| 320 | } |
| 321 | |
| 322 | *_aidl_return = camera->getCameraId(); |
| 323 | |
| 324 | return ndk::ScopedAStatus::ok(); |
| 325 | } |
| 326 | |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 327 | std::shared_ptr<VirtualCameraDevice> VirtualCameraService::getCamera( |
| 328 | const ::ndk::SpAIBinder& token) { |
| 329 | if (token == nullptr) { |
| 330 | return nullptr; |
| 331 | } |
| 332 | |
| 333 | std::lock_guard lock(mLock); |
| 334 | auto it = mTokenToCameraName.find(token); |
| 335 | if (it == mTokenToCameraName.end()) { |
| 336 | return nullptr; |
| 337 | } |
| 338 | |
| 339 | return mVirtualCameraProvider->getCamera(it->second); |
| 340 | } |
| 341 | |
Jan Sebechlebsky | 773c014 | 2024-03-25 12:17:05 +0100 | [diff] [blame] | 342 | binder_status_t VirtualCameraService::handleShellCommand(int, int out, int err, |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 343 | const char** args, |
| 344 | uint32_t numArgs) { |
| 345 | if (numArgs <= 0) { |
| 346 | dprintf(out, kShellCmdHelp); |
Jan Sebechlebsky | 76d7e21 | 2023-11-28 14:10:25 +0100 | [diff] [blame] | 347 | fsync(out); |
| 348 | return STATUS_OK; |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 349 | } |
| 350 | |
Jan Sebechlebsky | 773c014 | 2024-03-25 12:17:05 +0100 | [diff] [blame] | 351 | auto isNullptr = [](const char* ptr) { return ptr == nullptr; }; |
| 352 | if (args == nullptr || std::any_of(args, args + numArgs, isNullptr)) { |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 353 | return STATUS_BAD_VALUE; |
| 354 | } |
Marvin Ramin | a819613 | 2024-03-15 15:55:22 +0000 | [diff] [blame] | 355 | |
Jan Sebechlebsky | 773c014 | 2024-03-25 12:17:05 +0100 | [diff] [blame] | 356 | std::variant<CommandWithOptions, std::string> cmdOrErrorMessage = |
| 357 | parseCommand(args, numArgs); |
| 358 | if (std::holds_alternative<std::string>(cmdOrErrorMessage)) { |
| 359 | dprintf(err, "Error: %s\n", |
| 360 | std::get<std::string>(cmdOrErrorMessage).c_str()); |
| 361 | return STATUS_BAD_VALUE; |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 362 | } |
| 363 | |
Jan Sebechlebsky | 773c014 | 2024-03-25 12:17:05 +0100 | [diff] [blame] | 364 | const CommandWithOptions& cmd = |
| 365 | std::get<CommandWithOptions>(cmdOrErrorMessage); |
| 366 | binder_status_t status = STATUS_OK; |
| 367 | switch (cmd.command) { |
| 368 | case Command::HELP: |
| 369 | dprintf(out, kShellCmdHelp); |
| 370 | break; |
| 371 | case Command::ENABLE_TEST_CAMERA: |
| 372 | status = enableTestCameraCmd(out, err, cmd.optionToValueMap); |
| 373 | break; |
| 374 | case Command::DISABLE_TEST_CAMERA: |
| 375 | disableTestCameraCmd(out); |
| 376 | break; |
| 377 | } |
| 378 | |
| 379 | fsync(err); |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 380 | fsync(out); |
Jan Sebechlebsky | 773c014 | 2024-03-25 12:17:05 +0100 | [diff] [blame] | 381 | return status; |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 382 | } |
| 383 | |
Jan Sebechlebsky | 773c014 | 2024-03-25 12:17:05 +0100 | [diff] [blame] | 384 | binder_status_t VirtualCameraService::enableTestCameraCmd( |
| 385 | const int out, const int err, |
| 386 | const std::map<std::string, std::string>& options) { |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 387 | if (mTestCameraToken != nullptr) { |
Jan Sebechlebsky | 773c014 | 2024-03-25 12:17:05 +0100 | [diff] [blame] | 388 | dprintf(out, "Test camera is already enabled (%s).\n", |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 389 | getCamera(mTestCameraToken)->getCameraName().c_str()); |
Jan Sebechlebsky | 773c014 | 2024-03-25 12:17:05 +0100 | [diff] [blame] | 390 | return STATUS_OK; |
| 391 | } |
| 392 | |
| 393 | std::optional<int> cameraId; |
| 394 | auto it = options.find("camera_id"); |
| 395 | if (it != options.end()) { |
| 396 | cameraId = parseInt(it->second); |
| 397 | if (!cameraId.has_value()) { |
| 398 | dprintf(err, "Invalid camera_id: %s\n, must be number > 0", |
| 399 | it->second.c_str()); |
| 400 | return STATUS_BAD_VALUE; |
| 401 | } |
| 402 | } |
| 403 | |
| 404 | std::optional<LensFacing> lensFacing; |
| 405 | it = options.find("lens_facing"); |
| 406 | if (it != options.end()) { |
| 407 | lensFacing = parseLensFacing(it->second); |
| 408 | if (!lensFacing.has_value()) { |
| 409 | dprintf(err, "Invalid lens_facing: %s\n, must be front|back|external", |
| 410 | it->second.c_str()); |
| 411 | return STATUS_BAD_VALUE; |
| 412 | } |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | sp<BBinder> token = sp<BBinder>::make(); |
| 416 | mTestCameraToken.set(AIBinder_fromPlatformBinder(token)); |
| 417 | |
| 418 | bool ret; |
Jan Sebechlebsky | 3b478c4 | 2023-11-23 13:15:56 +0100 | [diff] [blame] | 419 | VirtualCameraConfiguration configuration; |
Biswarup Pal | 6152a30 | 2023-12-19 12:44:09 +0000 | [diff] [blame] | 420 | configuration.supportedStreamConfigs.push_back({.width = kVgaWidth, |
| 421 | .height = kVgaHeight, |
| 422 | Format::YUV_420_888, |
| 423 | .maxFps = kMaxFps}); |
Jan Sebechlebsky | 773c014 | 2024-03-25 12:17:05 +0100 | [diff] [blame] | 424 | configuration.lensFacing = lensFacing.value_or(LensFacing::EXTERNAL); |
| 425 | registerCamera(mTestCameraToken, configuration, cameraId.value_or(sNextId++), |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 426 | kDefaultDeviceId, &ret); |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 427 | if (ret) { |
Jan Sebechlebsky | 773c014 | 2024-03-25 12:17:05 +0100 | [diff] [blame] | 428 | dprintf(out, "Successfully registered test camera %s\n", |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 429 | getCamera(mTestCameraToken)->getCameraName().c_str()); |
| 430 | } else { |
Jan Sebechlebsky | 773c014 | 2024-03-25 12:17:05 +0100 | [diff] [blame] | 431 | dprintf(err, "Failed to create test camera\n"); |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 432 | } |
Jan Sebechlebsky | 773c014 | 2024-03-25 12:17:05 +0100 | [diff] [blame] | 433 | return STATUS_OK; |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | void VirtualCameraService::disableTestCameraCmd(const int out) { |
| 437 | if (mTestCameraToken == nullptr) { |
| 438 | dprintf(out, "Test camera is not registered."); |
| 439 | } |
| 440 | unregisterCamera(mTestCameraToken); |
| 441 | mTestCameraToken.set(nullptr); |
| 442 | } |
| 443 | |
| 444 | } // namespace virtualcamera |
| 445 | } // namespace companion |
| 446 | } // namespace android |