Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2023 The Android Open Source Project |
| 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 | #ifndef ANDROID_COMPANION_VIRTUALCAMERA_VIRTUALCAMERASERVICE_H |
| 18 | #define ANDROID_COMPANION_VIRTUALCAMERA_VIRTUALCAMERASERVICE_H |
| 19 | |
| 20 | #include <memory> |
| 21 | #include <mutex> |
| 22 | #include <unordered_map> |
| 23 | |
| 24 | #include "VirtualCameraDevice.h" |
| 25 | #include "VirtualCameraProvider.h" |
| 26 | #include "aidl/android/companion/virtualcamera/BnVirtualCameraService.h" |
Jan Sebechlebsky | de6f16f | 2023-11-29 09:27:36 +0100 | [diff] [blame] | 27 | #include "util/Permissions.h" |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 28 | |
| 29 | namespace android { |
| 30 | namespace companion { |
| 31 | namespace virtualcamera { |
| 32 | |
| 33 | // Implementation of Virtual Camera Service for managing virtual camera devices. |
| 34 | class VirtualCameraService |
| 35 | : public aidl::android::companion::virtualcamera::BnVirtualCameraService { |
| 36 | public: |
| 37 | VirtualCameraService( |
Jan Sebechlebsky | de6f16f | 2023-11-29 09:27:36 +0100 | [diff] [blame] | 38 | std::shared_ptr<VirtualCameraProvider> virtualCameraProvider, |
| 39 | const PermissionsProxy& permissionProxy = PermissionsProxy::get()); |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 40 | |
| 41 | // Register camera corresponding to the binder token. |
| 42 | ndk::ScopedAStatus registerCamera( |
| 43 | const ::ndk::SpAIBinder& token, |
| 44 | const ::aidl::android::companion::virtualcamera::VirtualCameraConfiguration& |
| 45 | configuration, |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 46 | int32_t deviceId, bool* _aidl_return) override EXCLUDES(mLock); |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 47 | |
Marvin Ramin | a819613 | 2024-03-15 15:55:22 +0000 | [diff] [blame] | 48 | // Register camera corresponding to the binder token. |
| 49 | ndk::ScopedAStatus registerCamera( |
| 50 | const ::ndk::SpAIBinder& token, |
| 51 | const ::aidl::android::companion::virtualcamera::VirtualCameraConfiguration& |
| 52 | configuration, |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 53 | int cameraId, int32_t deviceId, bool* _aidl_return) EXCLUDES(mLock); |
Marvin Ramin | a819613 | 2024-03-15 15:55:22 +0000 | [diff] [blame] | 54 | |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 55 | // Unregisters camera corresponding to the binder token. |
| 56 | ndk::ScopedAStatus unregisterCamera(const ::ndk::SpAIBinder& token) override |
| 57 | EXCLUDES(mLock); |
| 58 | |
Biswarup Pal | 68137fc | 2023-11-24 18:06:54 +0000 | [diff] [blame] | 59 | // Returns the camera id corresponding to the binder token. |
| 60 | ndk::ScopedAStatus getCameraId( |
| 61 | const ::ndk::SpAIBinder& token, int32_t* _aidl_return) override EXCLUDES(mLock); |
| 62 | |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 63 | // Returns VirtualCameraDevice corresponding to binder token or nullptr if |
| 64 | // there's no camera asociated with the token. |
| 65 | std::shared_ptr<VirtualCameraDevice> getCamera(const ::ndk::SpAIBinder& token) |
| 66 | EXCLUDES(mLock); |
| 67 | |
| 68 | // Handle cmd shell commands `adb shell cmd virtual_camera_service` [args]. |
| 69 | binder_status_t handleShellCommand(int in, int out, int err, const char** args, |
| 70 | uint32_t numArgs) override; |
| 71 | |
Jan Sebechlebsky | b36090e | 2024-04-11 09:19:33 +0200 | [diff] [blame^] | 72 | // Do not verify presence on required EGL extensions when registering virtual |
| 73 | // camera. Only to be used by unit tests. |
| 74 | void disableEglVerificationForTest() { |
| 75 | mVerifyEglExtensions = false; |
| 76 | } |
| 77 | |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 78 | private: |
| 79 | // Create and enable test camera instance if there's none. |
Jan Sebechlebsky | 773c014 | 2024-03-25 12:17:05 +0100 | [diff] [blame] | 80 | binder_status_t enableTestCameraCmd( |
| 81 | int out, int err, const std::map<std::string, std::string>& options); |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 82 | // Disable and destroy test camera instance if there's one. |
| 83 | void disableTestCameraCmd(int out); |
| 84 | |
| 85 | std::shared_ptr<VirtualCameraProvider> mVirtualCameraProvider; |
Jan Sebechlebsky | b36090e | 2024-04-11 09:19:33 +0200 | [diff] [blame^] | 86 | bool mVerifyEglExtensions = true; |
Jan Sebechlebsky | de6f16f | 2023-11-29 09:27:36 +0100 | [diff] [blame] | 87 | const PermissionsProxy& mPermissionProxy; |
| 88 | |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 89 | std::mutex mLock; |
| 90 | struct BinderTokenHash { |
| 91 | std::size_t operator()(const ::ndk::SpAIBinder& key) const { |
| 92 | return std::hash<void*>{}(reinterpret_cast<void*>(key.get())); |
| 93 | } |
| 94 | }; |
| 95 | // Map Binder tokens to names of cameras managed by camera provider. |
| 96 | std::unordered_map<::ndk::SpAIBinder, std::string, BinderTokenHash> |
| 97 | mTokenToCameraName GUARDED_BY(mLock); |
| 98 | |
| 99 | // Local binder token for test camera instance, or nullptr if there's none. |
| 100 | ::ndk::SpAIBinder mTestCameraToken; |
Marvin Ramin | a819613 | 2024-03-15 15:55:22 +0000 | [diff] [blame] | 101 | |
| 102 | // Numerical id to assign to next created camera. |
| 103 | static std::atomic_int sNextId; |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 104 | }; |
| 105 | |
| 106 | } // namespace virtualcamera |
| 107 | } // namespace companion |
| 108 | } // namespace android |
| 109 | |
| 110 | #endif // ANDROID_COMPANION_VIRTUALCAMERA_VIRTUALCAMERASERVICE_H |