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