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