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