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