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" 27 #include "util/Permissions.h" 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( 38 std::shared_ptr<VirtualCameraProvider> virtualCameraProvider, 39 const PermissionsProxy& permissionProxy = PermissionsProxy::get()); 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, 46 int32_t deviceId, bool* _aidl_return) override EXCLUDES(mLock); 47 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, 53 const std::string& cameraId, int32_t deviceId, bool* _aidl_return) 54 EXCLUDES(mLock); 55 56 // Unregisters camera corresponding to the binder token. 57 ndk::ScopedAStatus unregisterCamera(const ::ndk::SpAIBinder& token) override 58 EXCLUDES(mLock); 59 60 // Returns the camera id corresponding to the binder token. 61 ndk::ScopedAStatus getCameraId(const ::ndk::SpAIBinder& token, 62 std::string* _aidl_return) override 63 EXCLUDES(mLock); 64 65 // Returns VirtualCameraDevice corresponding to binder token or nullptr if 66 // there's no camera asociated with the token. 67 std::shared_ptr<VirtualCameraDevice> getCamera(const ::ndk::SpAIBinder& token) 68 EXCLUDES(mLock); 69 70 // Handle cmd shell commands `adb shell cmd virtual_camera_service` [args]. 71 binder_status_t handleShellCommand(int in, int out, int err, const char** args, 72 uint32_t numArgs) override; 73 74 // Do not verify presence on required EGL extensions when registering virtual 75 // camera. Only to be used by unit tests. disableEglVerificationForTest()76 void disableEglVerificationForTest() { 77 mVerifyEglExtensions = false; 78 } 79 80 // Default virtual device id (the host device id) 81 static constexpr int kDefaultDeviceId = 0; 82 83 private: 84 // Create and enable test camera instance if there's none. 85 binder_status_t enableTestCameraCmd( 86 int out, int err, const std::map<std::string, std::string>& options); 87 // Disable and destroy test camera instance if there's one. 88 void disableTestCameraCmd(int out); 89 90 std::shared_ptr<VirtualCameraProvider> mVirtualCameraProvider; 91 bool mVerifyEglExtensions = true; 92 const PermissionsProxy& mPermissionProxy; 93 94 std::mutex mLock; 95 struct BinderTokenHash { operatorBinderTokenHash96 std::size_t operator()(const ::ndk::SpAIBinder& key) const { 97 return std::hash<void*>{}(reinterpret_cast<void*>(key.get())); 98 } 99 }; 100 // Map Binder tokens to names of cameras managed by camera provider. 101 std::unordered_map<::ndk::SpAIBinder, std::string, BinderTokenHash> 102 mTokenToCameraName GUARDED_BY(mLock); 103 104 // Local binder token for test camera instance, or nullptr if there's none. 105 ::ndk::SpAIBinder mTestCameraToken; 106 }; 107 108 } // namespace virtualcamera 109 } // namespace companion 110 } // namespace android 111 112 #endif // ANDROID_COMPANION_VIRTUALCAMERA_VIRTUALCAMERASERVICE_H 113