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 | |
| 21 | #include <cinttypes> |
| 22 | #include <cstdint> |
| 23 | #include <cstdio> |
| 24 | #include <memory> |
| 25 | #include <mutex> |
| 26 | |
| 27 | #include "VirtualCameraDevice.h" |
| 28 | #include "VirtualCameraProvider.h" |
Jan Sebechlebsky | 3b478c4 | 2023-11-23 13:15:56 +0100 | [diff] [blame] | 29 | #include "aidl/android/companion/virtualcamera/Format.h" |
| 30 | #include "aidl/android/companion/virtualcamera/VirtualCameraConfiguration.h" |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 31 | #include "android/binder_auto_utils.h" |
| 32 | #include "android/binder_libbinder.h" |
| 33 | #include "binder/Status.h" |
Jan Sebechlebsky | de6f16f | 2023-11-29 09:27:36 +0100 | [diff] [blame] | 34 | #include "util/Permissions.h" |
Jan Sebechlebsky | 3b478c4 | 2023-11-23 13:15:56 +0100 | [diff] [blame] | 35 | #include "util/Util.h" |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 36 | |
| 37 | using ::android::binder::Status; |
| 38 | |
| 39 | namespace android { |
| 40 | namespace companion { |
| 41 | namespace virtualcamera { |
| 42 | |
Jan Sebechlebsky | 3b478c4 | 2023-11-23 13:15:56 +0100 | [diff] [blame] | 43 | using ::aidl::android::companion::virtualcamera::Format; |
Biswarup Pal | 112458f | 2023-12-28 19:50:17 +0000 | [diff] [blame^] | 44 | using ::aidl::android::companion::virtualcamera::LensFacing; |
Biswarup Pal | 6152a30 | 2023-12-19 12:44:09 +0000 | [diff] [blame] | 45 | using ::aidl::android::companion::virtualcamera::SensorOrientation; |
Jan Sebechlebsky | 3b478c4 | 2023-11-23 13:15:56 +0100 | [diff] [blame] | 46 | using ::aidl::android::companion::virtualcamera::SupportedStreamConfiguration; |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 47 | using ::aidl::android::companion::virtualcamera::VirtualCameraConfiguration; |
| 48 | |
| 49 | namespace { |
| 50 | |
Jan Sebechlebsky | 3b478c4 | 2023-11-23 13:15:56 +0100 | [diff] [blame] | 51 | constexpr int kVgaWidth = 640; |
| 52 | constexpr int kVgaHeight = 480; |
Biswarup Pal | 6152a30 | 2023-12-19 12:44:09 +0000 | [diff] [blame] | 53 | constexpr int kMaxFps = 60; |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 54 | constexpr char kEnableTestCameraCmd[] = "enable_test_camera"; |
| 55 | constexpr char kDisableTestCameraCmd[] = "disable_test_camera"; |
| 56 | constexpr char kShellCmdHelp[] = R"( |
| 57 | Available commands: |
| 58 | * enable_test_camera |
| 59 | * disable_test_camera |
| 60 | )"; |
Jan Sebechlebsky | de6f16f | 2023-11-29 09:27:36 +0100 | [diff] [blame] | 61 | constexpr char kCreateVirtualDevicePermission[] = |
| 62 | "android.permission.CREATE_VIRTUAL_DEVICE"; |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 63 | |
Jan Sebechlebsky | 3b478c4 | 2023-11-23 13:15:56 +0100 | [diff] [blame] | 64 | ndk::ScopedAStatus validateConfiguration( |
| 65 | const VirtualCameraConfiguration& configuration) { |
| 66 | if (configuration.supportedStreamConfigs.empty()) { |
| 67 | ALOGE("%s: No supported input configuration specified", __func__); |
| 68 | return ndk::ScopedAStatus::fromServiceSpecificError( |
| 69 | Status::EX_ILLEGAL_ARGUMENT); |
| 70 | } |
| 71 | |
| 72 | for (const SupportedStreamConfiguration& config : |
| 73 | configuration.supportedStreamConfigs) { |
| 74 | if (!isFormatSupportedForInput(config.width, config.height, |
Biswarup Pal | 6152a30 | 2023-12-19 12:44:09 +0000 | [diff] [blame] | 75 | config.pixelFormat, config.maxFps)) { |
Jan Sebechlebsky | 3b478c4 | 2023-11-23 13:15:56 +0100 | [diff] [blame] | 76 | ALOGE("%s: Requested unsupported input format: %d x %d (%d)", __func__, |
| 77 | config.width, config.height, static_cast<int>(config.pixelFormat)); |
| 78 | return ndk::ScopedAStatus::fromServiceSpecificError( |
| 79 | Status::EX_ILLEGAL_ARGUMENT); |
| 80 | } |
| 81 | } |
Biswarup Pal | 6152a30 | 2023-12-19 12:44:09 +0000 | [diff] [blame] | 82 | |
| 83 | if (configuration.sensorOrientation != SensorOrientation::ORIENTATION_0 && |
| 84 | configuration.sensorOrientation != SensorOrientation::ORIENTATION_90 && |
| 85 | configuration.sensorOrientation != SensorOrientation::ORIENTATION_180 && |
| 86 | configuration.sensorOrientation != SensorOrientation::ORIENTATION_270) { |
| 87 | return ndk::ScopedAStatus::fromServiceSpecificError( |
| 88 | Status::EX_ILLEGAL_ARGUMENT); |
| 89 | } |
| 90 | |
Biswarup Pal | 112458f | 2023-12-28 19:50:17 +0000 | [diff] [blame^] | 91 | if (configuration.lensFacing != LensFacing::FRONT && |
| 92 | configuration.lensFacing != LensFacing::BACK && |
| 93 | configuration.lensFacing != LensFacing::EXTERNAL) { |
| 94 | return ndk::ScopedAStatus::fromServiceSpecificError( |
| 95 | Status::EX_ILLEGAL_ARGUMENT); |
| 96 | } |
| 97 | |
Jan Sebechlebsky | 3b478c4 | 2023-11-23 13:15:56 +0100 | [diff] [blame] | 98 | return ndk::ScopedAStatus::ok(); |
| 99 | } |
| 100 | |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 101 | } // namespace |
| 102 | |
| 103 | VirtualCameraService::VirtualCameraService( |
Jan Sebechlebsky | de6f16f | 2023-11-29 09:27:36 +0100 | [diff] [blame] | 104 | std::shared_ptr<VirtualCameraProvider> virtualCameraProvider, |
| 105 | const PermissionsProxy& permissionProxy) |
| 106 | : mVirtualCameraProvider(virtualCameraProvider), |
| 107 | mPermissionProxy(permissionProxy) { |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | ndk::ScopedAStatus VirtualCameraService::registerCamera( |
| 111 | const ::ndk::SpAIBinder& token, |
| 112 | const VirtualCameraConfiguration& configuration, bool* _aidl_return) { |
Jan Sebechlebsky | de6f16f | 2023-11-29 09:27:36 +0100 | [diff] [blame] | 113 | if (!mPermissionProxy.checkCallingPermission(kCreateVirtualDevicePermission)) { |
| 114 | ALOGE("%s: caller (pid %d, uid %d) doesn't hold %s permission", __func__, |
| 115 | getpid(), getuid(), kCreateVirtualDevicePermission); |
| 116 | return ndk::ScopedAStatus::fromExceptionCode(EX_SECURITY); |
| 117 | } |
| 118 | |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 119 | if (_aidl_return == nullptr) { |
| 120 | return ndk::ScopedAStatus::fromServiceSpecificError( |
| 121 | Status::EX_ILLEGAL_ARGUMENT); |
| 122 | } |
Jan Sebechlebsky | de6f16f | 2023-11-29 09:27:36 +0100 | [diff] [blame] | 123 | |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 124 | *_aidl_return = true; |
| 125 | |
Jan Sebechlebsky | 3b478c4 | 2023-11-23 13:15:56 +0100 | [diff] [blame] | 126 | auto status = validateConfiguration(configuration); |
| 127 | if (!status.isOk()) { |
| 128 | *_aidl_return = false; |
| 129 | return status; |
| 130 | } |
| 131 | |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 132 | std::lock_guard lock(mLock); |
| 133 | if (mTokenToCameraName.find(token) != mTokenToCameraName.end()) { |
| 134 | ALOGE( |
| 135 | "Attempt to register camera corresponding to already registered binder " |
| 136 | "token: " |
| 137 | "0x%" PRIxPTR, |
| 138 | reinterpret_cast<uintptr_t>(token.get())); |
| 139 | *_aidl_return = false; |
Jan Sebechlebsky | 3b478c4 | 2023-11-23 13:15:56 +0100 | [diff] [blame] | 140 | return ndk::ScopedAStatus::ok(); |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 141 | } |
| 142 | |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 143 | std::shared_ptr<VirtualCameraDevice> camera = |
Biswarup Pal | 6152a30 | 2023-12-19 12:44:09 +0000 | [diff] [blame] | 144 | mVirtualCameraProvider->createCamera(configuration); |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 145 | if (camera == nullptr) { |
| 146 | ALOGE("Failed to create camera for binder token 0x%" PRIxPTR, |
| 147 | reinterpret_cast<uintptr_t>(token.get())); |
| 148 | *_aidl_return = false; |
| 149 | return ndk::ScopedAStatus::fromServiceSpecificError( |
| 150 | Status::EX_SERVICE_SPECIFIC); |
| 151 | } |
| 152 | |
| 153 | mTokenToCameraName[token] = camera->getCameraName(); |
| 154 | return ndk::ScopedAStatus::ok(); |
| 155 | } |
| 156 | |
| 157 | ndk::ScopedAStatus VirtualCameraService::unregisterCamera( |
| 158 | const ::ndk::SpAIBinder& token) { |
Jan Sebechlebsky | de6f16f | 2023-11-29 09:27:36 +0100 | [diff] [blame] | 159 | if (!mPermissionProxy.checkCallingPermission(kCreateVirtualDevicePermission)) { |
| 160 | ALOGE("%s: caller (pid %d, uid %d) doesn't hold %s permission", __func__, |
| 161 | getpid(), getuid(), kCreateVirtualDevicePermission); |
| 162 | return ndk::ScopedAStatus::fromExceptionCode(EX_SECURITY); |
| 163 | } |
| 164 | |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 165 | std::lock_guard lock(mLock); |
| 166 | |
| 167 | auto it = mTokenToCameraName.find(token); |
| 168 | if (it == mTokenToCameraName.end()) { |
| 169 | ALOGE( |
| 170 | "Attempt to unregister camera corresponding to unknown binder token: " |
| 171 | "0x%" PRIxPTR, |
| 172 | reinterpret_cast<uintptr_t>(token.get())); |
| 173 | return ndk::ScopedAStatus::ok(); |
| 174 | } |
| 175 | |
| 176 | mVirtualCameraProvider->removeCamera(it->second); |
| 177 | |
| 178 | return ndk::ScopedAStatus::ok(); |
| 179 | } |
| 180 | |
Biswarup Pal | 68137fc | 2023-11-24 18:06:54 +0000 | [diff] [blame] | 181 | ndk::ScopedAStatus VirtualCameraService::getCameraId( |
| 182 | const ::ndk::SpAIBinder& token, int32_t* _aidl_return) { |
Jan Sebechlebsky | de6f16f | 2023-11-29 09:27:36 +0100 | [diff] [blame] | 183 | if (!mPermissionProxy.checkCallingPermission(kCreateVirtualDevicePermission)) { |
| 184 | ALOGE("%s: caller (pid %d, uid %d) doesn't hold %s permission", __func__, |
| 185 | getpid(), getuid(), kCreateVirtualDevicePermission); |
| 186 | return ndk::ScopedAStatus::fromExceptionCode(EX_SECURITY); |
| 187 | } |
| 188 | |
Biswarup Pal | 68137fc | 2023-11-24 18:06:54 +0000 | [diff] [blame] | 189 | if (_aidl_return == nullptr) { |
| 190 | return ndk::ScopedAStatus::fromServiceSpecificError( |
| 191 | Status::EX_ILLEGAL_ARGUMENT); |
| 192 | } |
| 193 | |
| 194 | auto camera = getCamera(token); |
| 195 | if (camera == nullptr) { |
| 196 | ALOGE( |
| 197 | "Attempt to get camera id corresponding to unknown binder token: " |
| 198 | "0x%" PRIxPTR, |
| 199 | reinterpret_cast<uintptr_t>(token.get())); |
| 200 | return ndk::ScopedAStatus::ok(); |
| 201 | } |
| 202 | |
| 203 | *_aidl_return = camera->getCameraId(); |
| 204 | |
| 205 | return ndk::ScopedAStatus::ok(); |
| 206 | } |
| 207 | |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 208 | std::shared_ptr<VirtualCameraDevice> VirtualCameraService::getCamera( |
| 209 | const ::ndk::SpAIBinder& token) { |
| 210 | if (token == nullptr) { |
| 211 | return nullptr; |
| 212 | } |
| 213 | |
| 214 | std::lock_guard lock(mLock); |
| 215 | auto it = mTokenToCameraName.find(token); |
| 216 | if (it == mTokenToCameraName.end()) { |
| 217 | return nullptr; |
| 218 | } |
| 219 | |
| 220 | return mVirtualCameraProvider->getCamera(it->second); |
| 221 | } |
| 222 | |
| 223 | binder_status_t VirtualCameraService::handleShellCommand(int in, int out, |
| 224 | int err, |
| 225 | const char** args, |
| 226 | uint32_t numArgs) { |
| 227 | if (numArgs <= 0) { |
| 228 | dprintf(out, kShellCmdHelp); |
Jan Sebechlebsky | 76d7e21 | 2023-11-28 14:10:25 +0100 | [diff] [blame] | 229 | fsync(out); |
| 230 | return STATUS_OK; |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | if (args == nullptr || args[0] == nullptr) { |
| 234 | return STATUS_BAD_VALUE; |
| 235 | } |
| 236 | const char* const cmd = args[0]; |
| 237 | if (strcmp(kEnableTestCameraCmd, cmd) == 0) { |
| 238 | enableTestCameraCmd(in, err); |
| 239 | } else if (strcmp(kDisableTestCameraCmd, cmd) == 0) { |
| 240 | disableTestCameraCmd(in); |
| 241 | } else { |
| 242 | dprintf(out, kShellCmdHelp); |
| 243 | } |
| 244 | |
| 245 | fsync(out); |
| 246 | return STATUS_OK; |
| 247 | } |
| 248 | |
| 249 | void VirtualCameraService::enableTestCameraCmd(const int out, const int err) { |
| 250 | if (mTestCameraToken != nullptr) { |
| 251 | dprintf(out, "Test camera is already enabled (%s).", |
| 252 | getCamera(mTestCameraToken)->getCameraName().c_str()); |
| 253 | return; |
| 254 | } |
| 255 | |
| 256 | sp<BBinder> token = sp<BBinder>::make(); |
| 257 | mTestCameraToken.set(AIBinder_fromPlatformBinder(token)); |
| 258 | |
| 259 | bool ret; |
Jan Sebechlebsky | 3b478c4 | 2023-11-23 13:15:56 +0100 | [diff] [blame] | 260 | VirtualCameraConfiguration configuration; |
Biswarup Pal | 6152a30 | 2023-12-19 12:44:09 +0000 | [diff] [blame] | 261 | configuration.supportedStreamConfigs.push_back({.width = kVgaWidth, |
| 262 | .height = kVgaHeight, |
| 263 | Format::YUV_420_888, |
| 264 | .maxFps = kMaxFps}); |
Biswarup Pal | 112458f | 2023-12-28 19:50:17 +0000 | [diff] [blame^] | 265 | configuration.lensFacing = LensFacing::EXTERNAL; |
Jan Sebechlebsky | 3b478c4 | 2023-11-23 13:15:56 +0100 | [diff] [blame] | 266 | registerCamera(mTestCameraToken, configuration, &ret); |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 267 | if (ret) { |
| 268 | dprintf(out, "Successfully registered test camera %s", |
| 269 | getCamera(mTestCameraToken)->getCameraName().c_str()); |
| 270 | } else { |
| 271 | dprintf(err, "Failed to create test camera"); |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | void VirtualCameraService::disableTestCameraCmd(const int out) { |
| 276 | if (mTestCameraToken == nullptr) { |
| 277 | dprintf(out, "Test camera is not registered."); |
| 278 | } |
| 279 | unregisterCamera(mTestCameraToken); |
| 280 | mTestCameraToken.set(nullptr); |
| 281 | } |
| 282 | |
| 283 | } // namespace virtualcamera |
| 284 | } // namespace companion |
| 285 | } // namespace android |