1*ec779b8eSAndroid Build Coastguard Worker /* 2*ec779b8eSAndroid Build Coastguard Worker * Copyright (C) 2023 The Android Open Source Project 3*ec779b8eSAndroid Build Coastguard Worker * 4*ec779b8eSAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License"); 5*ec779b8eSAndroid Build Coastguard Worker * you may not use this file except in compliance with the License. 6*ec779b8eSAndroid Build Coastguard Worker * You may obtain a copy of the License at 7*ec779b8eSAndroid Build Coastguard Worker * 8*ec779b8eSAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0 9*ec779b8eSAndroid Build Coastguard Worker * 10*ec779b8eSAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software 11*ec779b8eSAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS, 12*ec779b8eSAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13*ec779b8eSAndroid Build Coastguard Worker * See the License for the specific language governing permissions and 14*ec779b8eSAndroid Build Coastguard Worker * limitations under the License. 15*ec779b8eSAndroid Build Coastguard Worker */ 16*ec779b8eSAndroid Build Coastguard Worker 17*ec779b8eSAndroid Build Coastguard Worker #ifndef ANDROID_COMPANION_VIRTUALCAMERA_VIRTUALCAMERASERVICE_H 18*ec779b8eSAndroid Build Coastguard Worker #define ANDROID_COMPANION_VIRTUALCAMERA_VIRTUALCAMERASERVICE_H 19*ec779b8eSAndroid Build Coastguard Worker 20*ec779b8eSAndroid Build Coastguard Worker #include <memory> 21*ec779b8eSAndroid Build Coastguard Worker #include <mutex> 22*ec779b8eSAndroid Build Coastguard Worker #include <unordered_map> 23*ec779b8eSAndroid Build Coastguard Worker 24*ec779b8eSAndroid Build Coastguard Worker #include "VirtualCameraDevice.h" 25*ec779b8eSAndroid Build Coastguard Worker #include "VirtualCameraProvider.h" 26*ec779b8eSAndroid Build Coastguard Worker #include "aidl/android/companion/virtualcamera/BnVirtualCameraService.h" 27*ec779b8eSAndroid Build Coastguard Worker #include "util/Permissions.h" 28*ec779b8eSAndroid Build Coastguard Worker 29*ec779b8eSAndroid Build Coastguard Worker namespace android { 30*ec779b8eSAndroid Build Coastguard Worker namespace companion { 31*ec779b8eSAndroid Build Coastguard Worker namespace virtualcamera { 32*ec779b8eSAndroid Build Coastguard Worker 33*ec779b8eSAndroid Build Coastguard Worker // Implementation of Virtual Camera Service for managing virtual camera devices. 34*ec779b8eSAndroid Build Coastguard Worker class VirtualCameraService 35*ec779b8eSAndroid Build Coastguard Worker : public aidl::android::companion::virtualcamera::BnVirtualCameraService { 36*ec779b8eSAndroid Build Coastguard Worker public: 37*ec779b8eSAndroid Build Coastguard Worker VirtualCameraService( 38*ec779b8eSAndroid Build Coastguard Worker std::shared_ptr<VirtualCameraProvider> virtualCameraProvider, 39*ec779b8eSAndroid Build Coastguard Worker const PermissionsProxy& permissionProxy = PermissionsProxy::get()); 40*ec779b8eSAndroid Build Coastguard Worker 41*ec779b8eSAndroid Build Coastguard Worker // Register camera corresponding to the binder token. 42*ec779b8eSAndroid Build Coastguard Worker ndk::ScopedAStatus registerCamera( 43*ec779b8eSAndroid Build Coastguard Worker const ::ndk::SpAIBinder& token, 44*ec779b8eSAndroid Build Coastguard Worker const ::aidl::android::companion::virtualcamera::VirtualCameraConfiguration& 45*ec779b8eSAndroid Build Coastguard Worker configuration, 46*ec779b8eSAndroid Build Coastguard Worker int32_t deviceId, bool* _aidl_return) override EXCLUDES(mLock); 47*ec779b8eSAndroid Build Coastguard Worker 48*ec779b8eSAndroid Build Coastguard Worker // Register camera corresponding to the binder token. 49*ec779b8eSAndroid Build Coastguard Worker ndk::ScopedAStatus registerCamera( 50*ec779b8eSAndroid Build Coastguard Worker const ::ndk::SpAIBinder& token, 51*ec779b8eSAndroid Build Coastguard Worker const ::aidl::android::companion::virtualcamera::VirtualCameraConfiguration& 52*ec779b8eSAndroid Build Coastguard Worker configuration, 53*ec779b8eSAndroid Build Coastguard Worker const std::string& cameraId, int32_t deviceId, bool* _aidl_return) 54*ec779b8eSAndroid Build Coastguard Worker EXCLUDES(mLock); 55*ec779b8eSAndroid Build Coastguard Worker 56*ec779b8eSAndroid Build Coastguard Worker // Unregisters camera corresponding to the binder token. 57*ec779b8eSAndroid Build Coastguard Worker ndk::ScopedAStatus unregisterCamera(const ::ndk::SpAIBinder& token) override 58*ec779b8eSAndroid Build Coastguard Worker EXCLUDES(mLock); 59*ec779b8eSAndroid Build Coastguard Worker 60*ec779b8eSAndroid Build Coastguard Worker // Returns the camera id corresponding to the binder token. 61*ec779b8eSAndroid Build Coastguard Worker ndk::ScopedAStatus getCameraId(const ::ndk::SpAIBinder& token, 62*ec779b8eSAndroid Build Coastguard Worker std::string* _aidl_return) override 63*ec779b8eSAndroid Build Coastguard Worker EXCLUDES(mLock); 64*ec779b8eSAndroid Build Coastguard Worker 65*ec779b8eSAndroid Build Coastguard Worker // Returns VirtualCameraDevice corresponding to binder token or nullptr if 66*ec779b8eSAndroid Build Coastguard Worker // there's no camera asociated with the token. 67*ec779b8eSAndroid Build Coastguard Worker std::shared_ptr<VirtualCameraDevice> getCamera(const ::ndk::SpAIBinder& token) 68*ec779b8eSAndroid Build Coastguard Worker EXCLUDES(mLock); 69*ec779b8eSAndroid Build Coastguard Worker 70*ec779b8eSAndroid Build Coastguard Worker // Handle cmd shell commands `adb shell cmd virtual_camera_service` [args]. 71*ec779b8eSAndroid Build Coastguard Worker binder_status_t handleShellCommand(int in, int out, int err, const char** args, 72*ec779b8eSAndroid Build Coastguard Worker uint32_t numArgs) override; 73*ec779b8eSAndroid Build Coastguard Worker 74*ec779b8eSAndroid Build Coastguard Worker // Do not verify presence on required EGL extensions when registering virtual 75*ec779b8eSAndroid Build Coastguard Worker // camera. Only to be used by unit tests. disableEglVerificationForTest()76*ec779b8eSAndroid Build Coastguard Worker void disableEglVerificationForTest() { 77*ec779b8eSAndroid Build Coastguard Worker mVerifyEglExtensions = false; 78*ec779b8eSAndroid Build Coastguard Worker } 79*ec779b8eSAndroid Build Coastguard Worker 80*ec779b8eSAndroid Build Coastguard Worker // Default virtual device id (the host device id) 81*ec779b8eSAndroid Build Coastguard Worker static constexpr int kDefaultDeviceId = 0; 82*ec779b8eSAndroid Build Coastguard Worker 83*ec779b8eSAndroid Build Coastguard Worker private: 84*ec779b8eSAndroid Build Coastguard Worker // Create and enable test camera instance if there's none. 85*ec779b8eSAndroid Build Coastguard Worker binder_status_t enableTestCameraCmd( 86*ec779b8eSAndroid Build Coastguard Worker int out, int err, const std::map<std::string, std::string>& options); 87*ec779b8eSAndroid Build Coastguard Worker // Disable and destroy test camera instance if there's one. 88*ec779b8eSAndroid Build Coastguard Worker binder_status_t disableTestCameraCmd(int out); 89*ec779b8eSAndroid Build Coastguard Worker 90*ec779b8eSAndroid Build Coastguard Worker // Register camera corresponding to the binder token without checking for 91*ec779b8eSAndroid Build Coastguard Worker // caller permission. 92*ec779b8eSAndroid Build Coastguard Worker ndk::ScopedAStatus registerCameraNoCheck( 93*ec779b8eSAndroid Build Coastguard Worker const ::ndk::SpAIBinder& token, 94*ec779b8eSAndroid Build Coastguard Worker const ::aidl::android::companion::virtualcamera::VirtualCameraConfiguration& 95*ec779b8eSAndroid Build Coastguard Worker configuration, 96*ec779b8eSAndroid Build Coastguard Worker const std::string& cameraId, int32_t deviceId, bool* _aidl_return) 97*ec779b8eSAndroid Build Coastguard Worker EXCLUDES(mLock); 98*ec779b8eSAndroid Build Coastguard Worker 99*ec779b8eSAndroid Build Coastguard Worker std::shared_ptr<VirtualCameraProvider> mVirtualCameraProvider; 100*ec779b8eSAndroid Build Coastguard Worker bool mVerifyEglExtensions = true; 101*ec779b8eSAndroid Build Coastguard Worker const PermissionsProxy& mPermissionProxy; 102*ec779b8eSAndroid Build Coastguard Worker 103*ec779b8eSAndroid Build Coastguard Worker std::mutex mLock; 104*ec779b8eSAndroid Build Coastguard Worker struct BinderTokenHash { operatorBinderTokenHash105*ec779b8eSAndroid Build Coastguard Worker std::size_t operator()(const ::ndk::SpAIBinder& key) const { 106*ec779b8eSAndroid Build Coastguard Worker return std::hash<void*>{}(reinterpret_cast<void*>(key.get())); 107*ec779b8eSAndroid Build Coastguard Worker } 108*ec779b8eSAndroid Build Coastguard Worker }; 109*ec779b8eSAndroid Build Coastguard Worker // Map Binder tokens to names of cameras managed by camera provider. 110*ec779b8eSAndroid Build Coastguard Worker std::unordered_map<::ndk::SpAIBinder, std::string, BinderTokenHash> 111*ec779b8eSAndroid Build Coastguard Worker mTokenToCameraName GUARDED_BY(mLock); 112*ec779b8eSAndroid Build Coastguard Worker 113*ec779b8eSAndroid Build Coastguard Worker // Local binder token for test camera instance, or nullptr if there's none. 114*ec779b8eSAndroid Build Coastguard Worker ::ndk::SpAIBinder mTestCameraToken; 115*ec779b8eSAndroid Build Coastguard Worker }; 116*ec779b8eSAndroid Build Coastguard Worker 117*ec779b8eSAndroid Build Coastguard Worker } // namespace virtualcamera 118*ec779b8eSAndroid Build Coastguard Worker } // namespace companion 119*ec779b8eSAndroid Build Coastguard Worker } // namespace android 120*ec779b8eSAndroid Build Coastguard Worker 121*ec779b8eSAndroid Build Coastguard Worker #endif // ANDROID_COMPANION_VIRTUALCAMERA_VIRTUALCAMERASERVICE_H 122