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 | |
| 60 | namespace { |
| 61 | |
Biswarup Pal | 15dcd13 | 2024-06-02 14:02:03 +0000 | [diff] [blame^] | 62 | constexpr char kCameraIdPrefix[] = "v"; |
Jan Sebechlebsky | 3b478c4 | 2023-11-23 13:15:56 +0100 | [diff] [blame] | 63 | constexpr int kVgaWidth = 640; |
| 64 | constexpr int kVgaHeight = 480; |
Biswarup Pal | 6152a30 | 2023-12-19 12:44:09 +0000 | [diff] [blame] | 65 | constexpr int kMaxFps = 60; |
Jan Sebechlebsky | 34469e4 | 2024-05-29 10:56:12 +0200 | [diff] [blame] | 66 | constexpr int kTestCameraDefaultInputFps = 30; |
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 | 34469e4 | 2024-05-29 10:56:12 +0200 | [diff] [blame] | 78 | --input_fps=(fps) - specify input fps for test camera, valid values are from 1 to 1000 |
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 | |
Biswarup Pal | 15dcd13 | 2024-06-02 14:02:03 +0000 | [diff] [blame^] | 90 | // Numerical portion for id to assign to next created camera. |
| 91 | static std::atomic_int sNextIdNumericalPortion{1000}; |
| 92 | |
Jan Sebechlebsky | 3b478c4 | 2023-11-23 13:15:56 +0100 | [diff] [blame] | 93 | ndk::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 Sebechlebsky | 288900f | 2024-05-24 14:47:54 +0200 | [diff] [blame] | 101 | 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 Sebechlebsky | 3b478c4 | 2023-11-23 13:15:56 +0100 | [diff] [blame] | 108 | for (const SupportedStreamConfiguration& config : |
| 109 | configuration.supportedStreamConfigs) { |
| 110 | if (!isFormatSupportedForInput(config.width, config.height, |
Biswarup Pal | 6152a30 | 2023-12-19 12:44:09 +0000 | [diff] [blame] | 111 | config.pixelFormat, config.maxFps)) { |
Jan Sebechlebsky | 3b478c4 | 2023-11-23 13:15:56 +0100 | [diff] [blame] | 112 | 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 Pal | 6152a30 | 2023-12-19 12:44:09 +0000 | [diff] [blame] | 118 | |
| 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 Pal | 112458f | 2023-12-28 19:50:17 +0000 | [diff] [blame] | 127 | 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 Sebechlebsky | 3b478c4 | 2023-11-23 13:15:56 +0100 | [diff] [blame] | 134 | return ndk::ScopedAStatus::ok(); |
| 135 | } |
| 136 | |
Jan Sebechlebsky | 773c014 | 2024-03-25 12:17:05 +0100 | [diff] [blame] | 137 | enum class Command { |
| 138 | ENABLE_TEST_CAMERA, |
| 139 | DISABLE_TEST_CAMERA, |
| 140 | HELP, |
| 141 | }; |
| 142 | |
| 143 | struct CommandWithOptions { |
| 144 | Command command; |
| 145 | std::map<std::string, std::string> optionToValueMap; |
| 146 | }; |
| 147 | |
| 148 | std::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 | |
| 156 | std::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 | |
| 165 | std::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; |
Biswarup Pal | 15dcd13 | 2024-06-02 14:02:03 +0000 | [diff] [blame^] | 195 | } |
Jan Sebechlebsky | 773c014 | 2024-03-25 12:17:05 +0100 | [diff] [blame] | 196 | |
Jan Sebechlebsky | b36090e | 2024-04-11 09:19:33 +0200 | [diff] [blame] | 197 | ndk::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 | |
Biswarup Pal | 15dcd13 | 2024-06-02 14:02:03 +0000 | [diff] [blame^] | 214 | std::string createCameraId(const int32_t deviceId) { |
| 215 | return kCameraIdPrefix + std::to_string(deviceId) + "_" + |
| 216 | std::to_string(sNextIdNumericalPortion++); |
| 217 | } |
| 218 | |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 219 | } // namespace |
| 220 | |
| 221 | VirtualCameraService::VirtualCameraService( |
Jan Sebechlebsky | de6f16f | 2023-11-29 09:27:36 +0100 | [diff] [blame] | 222 | std::shared_ptr<VirtualCameraProvider> virtualCameraProvider, |
| 223 | const PermissionsProxy& permissionProxy) |
| 224 | : mVirtualCameraProvider(virtualCameraProvider), |
| 225 | mPermissionProxy(permissionProxy) { |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | ndk::ScopedAStatus VirtualCameraService::registerCamera( |
| 229 | const ::ndk::SpAIBinder& token, |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 230 | const VirtualCameraConfiguration& configuration, const int32_t deviceId, |
| 231 | bool* _aidl_return) { |
Biswarup Pal | 15dcd13 | 2024-06-02 14:02:03 +0000 | [diff] [blame^] | 232 | return registerCamera(token, configuration, createCameraId(deviceId), |
| 233 | deviceId, _aidl_return); |
Marvin Ramin | a819613 | 2024-03-15 15:55:22 +0000 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | ndk::ScopedAStatus VirtualCameraService::registerCamera( |
| 237 | const ::ndk::SpAIBinder& token, |
Biswarup Pal | 15dcd13 | 2024-06-02 14:02:03 +0000 | [diff] [blame^] | 238 | const VirtualCameraConfiguration& configuration, |
| 239 | const std::string& cameraId, const int32_t deviceId, bool* _aidl_return) { |
Jan Sebechlebsky | de6f16f | 2023-11-29 09:27:36 +0100 | [diff] [blame] | 240 | if (!mPermissionProxy.checkCallingPermission(kCreateVirtualDevicePermission)) { |
| 241 | ALOGE("%s: caller (pid %d, uid %d) doesn't hold %s permission", __func__, |
| 242 | getpid(), getuid(), kCreateVirtualDevicePermission); |
| 243 | return ndk::ScopedAStatus::fromExceptionCode(EX_SECURITY); |
| 244 | } |
| 245 | |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 246 | if (_aidl_return == nullptr) { |
| 247 | return ndk::ScopedAStatus::fromServiceSpecificError( |
| 248 | Status::EX_ILLEGAL_ARGUMENT); |
| 249 | } |
Jan Sebechlebsky | de6f16f | 2023-11-29 09:27:36 +0100 | [diff] [blame] | 250 | |
Jan Sebechlebsky | b36090e | 2024-04-11 09:19:33 +0200 | [diff] [blame] | 251 | if (mVerifyEglExtensions) { |
| 252 | auto status = verifyRequiredEglExtensions(); |
| 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 | |
Jan Sebechlebsky | 3b478c4 | 2023-11-23 13:15:56 +0100 | [diff] [blame] | 259 | auto status = validateConfiguration(configuration); |
| 260 | if (!status.isOk()) { |
| 261 | *_aidl_return = false; |
| 262 | return status; |
| 263 | } |
| 264 | |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 265 | std::lock_guard lock(mLock); |
| 266 | if (mTokenToCameraName.find(token) != mTokenToCameraName.end()) { |
| 267 | ALOGE( |
| 268 | "Attempt to register camera corresponding to already registered binder " |
| 269 | "token: " |
| 270 | "0x%" PRIxPTR, |
| 271 | reinterpret_cast<uintptr_t>(token.get())); |
| 272 | *_aidl_return = false; |
Jan Sebechlebsky | 3b478c4 | 2023-11-23 13:15:56 +0100 | [diff] [blame] | 273 | return ndk::ScopedAStatus::ok(); |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 274 | } |
| 275 | |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 276 | std::shared_ptr<VirtualCameraDevice> camera = |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 277 | mVirtualCameraProvider->createCamera(configuration, cameraId, deviceId); |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 278 | if (camera == nullptr) { |
| 279 | ALOGE("Failed to create camera for binder token 0x%" PRIxPTR, |
| 280 | reinterpret_cast<uintptr_t>(token.get())); |
| 281 | *_aidl_return = false; |
| 282 | return ndk::ScopedAStatus::fromServiceSpecificError( |
| 283 | Status::EX_SERVICE_SPECIFIC); |
| 284 | } |
| 285 | |
| 286 | mTokenToCameraName[token] = camera->getCameraName(); |
Jan Sebechlebsky | b36090e | 2024-04-11 09:19:33 +0200 | [diff] [blame] | 287 | *_aidl_return = true; |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 288 | return ndk::ScopedAStatus::ok(); |
| 289 | } |
| 290 | |
| 291 | ndk::ScopedAStatus VirtualCameraService::unregisterCamera( |
| 292 | const ::ndk::SpAIBinder& token) { |
Jan Sebechlebsky | de6f16f | 2023-11-29 09:27:36 +0100 | [diff] [blame] | 293 | if (!mPermissionProxy.checkCallingPermission(kCreateVirtualDevicePermission)) { |
| 294 | ALOGE("%s: caller (pid %d, uid %d) doesn't hold %s permission", __func__, |
| 295 | getpid(), getuid(), kCreateVirtualDevicePermission); |
| 296 | return ndk::ScopedAStatus::fromExceptionCode(EX_SECURITY); |
| 297 | } |
| 298 | |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 299 | std::lock_guard lock(mLock); |
| 300 | |
| 301 | auto it = mTokenToCameraName.find(token); |
| 302 | if (it == mTokenToCameraName.end()) { |
| 303 | ALOGE( |
| 304 | "Attempt to unregister camera corresponding to unknown binder token: " |
| 305 | "0x%" PRIxPTR, |
| 306 | reinterpret_cast<uintptr_t>(token.get())); |
| 307 | return ndk::ScopedAStatus::ok(); |
| 308 | } |
| 309 | |
| 310 | mVirtualCameraProvider->removeCamera(it->second); |
| 311 | |
Tony Guo | 6cbe11b | 2024-03-17 02:34:23 +0000 | [diff] [blame] | 312 | mTokenToCameraName.erase(it); |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 313 | return ndk::ScopedAStatus::ok(); |
| 314 | } |
| 315 | |
Biswarup Pal | 68137fc | 2023-11-24 18:06:54 +0000 | [diff] [blame] | 316 | ndk::ScopedAStatus VirtualCameraService::getCameraId( |
Biswarup Pal | 15dcd13 | 2024-06-02 14:02:03 +0000 | [diff] [blame^] | 317 | const ::ndk::SpAIBinder& token, std::string* _aidl_return) { |
Jan Sebechlebsky | de6f16f | 2023-11-29 09:27:36 +0100 | [diff] [blame] | 318 | if (!mPermissionProxy.checkCallingPermission(kCreateVirtualDevicePermission)) { |
| 319 | ALOGE("%s: caller (pid %d, uid %d) doesn't hold %s permission", __func__, |
| 320 | getpid(), getuid(), kCreateVirtualDevicePermission); |
| 321 | return ndk::ScopedAStatus::fromExceptionCode(EX_SECURITY); |
| 322 | } |
| 323 | |
Biswarup Pal | 68137fc | 2023-11-24 18:06:54 +0000 | [diff] [blame] | 324 | if (_aidl_return == nullptr) { |
| 325 | return ndk::ScopedAStatus::fromServiceSpecificError( |
Marvin Ramin | a819613 | 2024-03-15 15:55:22 +0000 | [diff] [blame] | 326 | Status::EX_ILLEGAL_ARGUMENT); |
Biswarup Pal | 68137fc | 2023-11-24 18:06:54 +0000 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | auto camera = getCamera(token); |
| 330 | if (camera == nullptr) { |
| 331 | ALOGE( |
| 332 | "Attempt to get camera id corresponding to unknown binder token: " |
| 333 | "0x%" PRIxPTR, |
| 334 | reinterpret_cast<uintptr_t>(token.get())); |
| 335 | return ndk::ScopedAStatus::ok(); |
| 336 | } |
| 337 | |
| 338 | *_aidl_return = camera->getCameraId(); |
| 339 | |
| 340 | return ndk::ScopedAStatus::ok(); |
| 341 | } |
| 342 | |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 343 | std::shared_ptr<VirtualCameraDevice> VirtualCameraService::getCamera( |
| 344 | const ::ndk::SpAIBinder& token) { |
| 345 | if (token == nullptr) { |
| 346 | return nullptr; |
| 347 | } |
| 348 | |
| 349 | std::lock_guard lock(mLock); |
| 350 | auto it = mTokenToCameraName.find(token); |
| 351 | if (it == mTokenToCameraName.end()) { |
| 352 | return nullptr; |
| 353 | } |
| 354 | |
| 355 | return mVirtualCameraProvider->getCamera(it->second); |
| 356 | } |
| 357 | |
Jan Sebechlebsky | 773c014 | 2024-03-25 12:17:05 +0100 | [diff] [blame] | 358 | binder_status_t VirtualCameraService::handleShellCommand(int, int out, int err, |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 359 | const char** args, |
| 360 | uint32_t numArgs) { |
| 361 | if (numArgs <= 0) { |
| 362 | dprintf(out, kShellCmdHelp); |
Jan Sebechlebsky | 76d7e21 | 2023-11-28 14:10:25 +0100 | [diff] [blame] | 363 | fsync(out); |
| 364 | return STATUS_OK; |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 365 | } |
| 366 | |
Jan Sebechlebsky | 773c014 | 2024-03-25 12:17:05 +0100 | [diff] [blame] | 367 | auto isNullptr = [](const char* ptr) { return ptr == nullptr; }; |
| 368 | if (args == nullptr || std::any_of(args, args + numArgs, isNullptr)) { |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 369 | return STATUS_BAD_VALUE; |
| 370 | } |
Marvin Ramin | a819613 | 2024-03-15 15:55:22 +0000 | [diff] [blame] | 371 | |
Jan Sebechlebsky | 773c014 | 2024-03-25 12:17:05 +0100 | [diff] [blame] | 372 | std::variant<CommandWithOptions, std::string> cmdOrErrorMessage = |
| 373 | parseCommand(args, numArgs); |
| 374 | if (std::holds_alternative<std::string>(cmdOrErrorMessage)) { |
| 375 | dprintf(err, "Error: %s\n", |
| 376 | std::get<std::string>(cmdOrErrorMessage).c_str()); |
| 377 | return STATUS_BAD_VALUE; |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 378 | } |
| 379 | |
Jan Sebechlebsky | 773c014 | 2024-03-25 12:17:05 +0100 | [diff] [blame] | 380 | const CommandWithOptions& cmd = |
| 381 | std::get<CommandWithOptions>(cmdOrErrorMessage); |
| 382 | binder_status_t status = STATUS_OK; |
| 383 | switch (cmd.command) { |
| 384 | case Command::HELP: |
| 385 | dprintf(out, kShellCmdHelp); |
| 386 | break; |
| 387 | case Command::ENABLE_TEST_CAMERA: |
| 388 | status = enableTestCameraCmd(out, err, cmd.optionToValueMap); |
| 389 | break; |
| 390 | case Command::DISABLE_TEST_CAMERA: |
| 391 | disableTestCameraCmd(out); |
| 392 | break; |
| 393 | } |
| 394 | |
| 395 | fsync(err); |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 396 | fsync(out); |
Jan Sebechlebsky | 773c014 | 2024-03-25 12:17:05 +0100 | [diff] [blame] | 397 | return status; |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 398 | } |
| 399 | |
Jan Sebechlebsky | 773c014 | 2024-03-25 12:17:05 +0100 | [diff] [blame] | 400 | binder_status_t VirtualCameraService::enableTestCameraCmd( |
| 401 | const int out, const int err, |
| 402 | const std::map<std::string, std::string>& options) { |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 403 | if (mTestCameraToken != nullptr) { |
Jan Sebechlebsky | 773c014 | 2024-03-25 12:17:05 +0100 | [diff] [blame] | 404 | dprintf(out, "Test camera is already enabled (%s).\n", |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 405 | getCamera(mTestCameraToken)->getCameraName().c_str()); |
Jan Sebechlebsky | 773c014 | 2024-03-25 12:17:05 +0100 | [diff] [blame] | 406 | return STATUS_OK; |
| 407 | } |
| 408 | |
Biswarup Pal | 15dcd13 | 2024-06-02 14:02:03 +0000 | [diff] [blame^] | 409 | std::optional<std::string> cameraId; |
Jan Sebechlebsky | 773c014 | 2024-03-25 12:17:05 +0100 | [diff] [blame] | 410 | auto it = options.find("camera_id"); |
| 411 | if (it != options.end()) { |
Biswarup Pal | 15dcd13 | 2024-06-02 14:02:03 +0000 | [diff] [blame^] | 412 | cameraId = it->second; |
Jan Sebechlebsky | 773c014 | 2024-03-25 12:17:05 +0100 | [diff] [blame] | 413 | if (!cameraId.has_value()) { |
Biswarup Pal | 15dcd13 | 2024-06-02 14:02:03 +0000 | [diff] [blame^] | 414 | dprintf(err, "Invalid camera_id: %s", it->second.c_str()); |
Jan Sebechlebsky | 773c014 | 2024-03-25 12:17:05 +0100 | [diff] [blame] | 415 | return STATUS_BAD_VALUE; |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | std::optional<LensFacing> lensFacing; |
| 420 | it = options.find("lens_facing"); |
| 421 | if (it != options.end()) { |
| 422 | lensFacing = parseLensFacing(it->second); |
| 423 | if (!lensFacing.has_value()) { |
| 424 | dprintf(err, "Invalid lens_facing: %s\n, must be front|back|external", |
| 425 | it->second.c_str()); |
| 426 | return STATUS_BAD_VALUE; |
| 427 | } |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 428 | } |
| 429 | |
Jan Sebechlebsky | 34469e4 | 2024-05-29 10:56:12 +0200 | [diff] [blame] | 430 | std::optional<int> inputFps; |
| 431 | it = options.find("input_fps"); |
| 432 | if (it != options.end()) { |
| 433 | inputFps = parseInt(it->second); |
| 434 | if (!inputFps.has_value() || inputFps.value() < 1 || |
| 435 | inputFps.value() > 1000) { |
| 436 | dprintf(err, "Invalid input fps: %s\n, must be integer in <1,1000> range.", |
| 437 | it->second.c_str()); |
| 438 | return STATUS_BAD_VALUE; |
| 439 | } |
| 440 | } |
| 441 | |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 442 | sp<BBinder> token = sp<BBinder>::make(); |
| 443 | mTestCameraToken.set(AIBinder_fromPlatformBinder(token)); |
| 444 | |
| 445 | bool ret; |
Jan Sebechlebsky | 3b478c4 | 2023-11-23 13:15:56 +0100 | [diff] [blame] | 446 | VirtualCameraConfiguration configuration; |
Biswarup Pal | 6152a30 | 2023-12-19 12:44:09 +0000 | [diff] [blame] | 447 | configuration.supportedStreamConfigs.push_back({.width = kVgaWidth, |
| 448 | .height = kVgaHeight, |
Jan Sebechlebsky | 288900f | 2024-05-24 14:47:54 +0200 | [diff] [blame] | 449 | Format::RGBA_8888, |
Biswarup Pal | 6152a30 | 2023-12-19 12:44:09 +0000 | [diff] [blame] | 450 | .maxFps = kMaxFps}); |
Jan Sebechlebsky | 773c014 | 2024-03-25 12:17:05 +0100 | [diff] [blame] | 451 | configuration.lensFacing = lensFacing.value_or(LensFacing::EXTERNAL); |
Jan Sebechlebsky | 288900f | 2024-05-24 14:47:54 +0200 | [diff] [blame] | 452 | configuration.virtualCameraCallback = |
Jan Sebechlebsky | 34469e4 | 2024-05-29 10:56:12 +0200 | [diff] [blame] | 453 | ndk::SharedRefBase::make<VirtualCameraTestInstance>( |
| 454 | inputFps.value_or(kTestCameraDefaultInputFps)); |
Biswarup Pal | 15dcd13 | 2024-06-02 14:02:03 +0000 | [diff] [blame^] | 455 | registerCamera(mTestCameraToken, configuration, |
| 456 | cameraId.value_or(std::to_string(sNextIdNumericalPortion++)), |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 457 | kDefaultDeviceId, &ret); |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 458 | if (ret) { |
Jan Sebechlebsky | 773c014 | 2024-03-25 12:17:05 +0100 | [diff] [blame] | 459 | dprintf(out, "Successfully registered test camera %s\n", |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 460 | getCamera(mTestCameraToken)->getCameraName().c_str()); |
| 461 | } else { |
Jan Sebechlebsky | 773c014 | 2024-03-25 12:17:05 +0100 | [diff] [blame] | 462 | dprintf(err, "Failed to create test camera\n"); |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 463 | } |
Jan Sebechlebsky | 773c014 | 2024-03-25 12:17:05 +0100 | [diff] [blame] | 464 | return STATUS_OK; |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 465 | } |
| 466 | |
| 467 | void VirtualCameraService::disableTestCameraCmd(const int out) { |
| 468 | if (mTestCameraToken == nullptr) { |
| 469 | dprintf(out, "Test camera is not registered."); |
| 470 | } |
| 471 | unregisterCamera(mTestCameraToken); |
| 472 | mTestCameraToken.set(nullptr); |
| 473 | } |
| 474 | |
| 475 | } // namespace virtualcamera |
| 476 | } // namespace companion |
| 477 | } // namespace android |