Manually discover virtual camera service
The virtual camera service is not declared in the VINTF manifest.
Currently libcameraservice only looks for declared AIDL ICameraProvider services
This CL checks if the virtual camera service is running and adds the
corresponding CameraProvider to the CameraProviderManager
Test: CameraProviderManagerTest.AidlVirtualCameraProviderDiscovered
Test: CameraProviderManagerTest.AidlVirtualCameraProviderDiscoveredOnInit
Bug: b/270352264
Change-Id: I15667ea18bc32c90f76c1e25faa5c42199495aab
diff --git a/services/camera/libcameraservice/common/CameraProviderManager.cpp b/services/camera/libcameraservice/common/CameraProviderManager.cpp
index 3801470..54dc2bc 100644
--- a/services/camera/libcameraservice/common/CameraProviderManager.cpp
+++ b/services/camera/libcameraservice/common/CameraProviderManager.cpp
@@ -63,6 +63,7 @@
namespace {
const bool kEnableLazyHal(property_get_bool("ro.camera.enableLazyHal", false));
const std::string kExternalProviderName = "external/0";
+const std::string kVirtualProviderName = "virtual/0";
} // anonymous namespace
const float CameraProviderManager::kDepthARTolerance = .1f;
@@ -71,6 +72,8 @@
CameraProviderManager::HidlServiceInteractionProxyImpl
CameraProviderManager::sHidlServiceInteractionProxy{};
+CameraProviderManager::AidlServiceInteractionProxyImpl
+CameraProviderManager::sAidlServiceInteractionProxy{};
CameraProviderManager::~CameraProviderManager() {
}
@@ -133,6 +136,29 @@
return OK;
}
+std::shared_ptr<aidl::android::hardware::camera::provider::ICameraProvider>
+CameraProviderManager::AidlServiceInteractionProxyImpl::getAidlService(
+ const std::string& serviceName) {
+ using aidl::android::hardware::camera::provider::ICameraProvider;
+
+ AIBinder* binder = nullptr;
+ if (flags::lazy_aidl_wait_for_service()) {
+ binder = AServiceManager_waitForService(serviceName.c_str());
+ } else {
+ binder = AServiceManager_getService(serviceName.c_str());
+ }
+
+ if (binder == nullptr) {
+ ALOGD("%s: AIDL Camera provider HAL '%s' is not actually available", __FUNCTION__,
+ serviceName.c_str());
+ return nullptr;
+ }
+ std::shared_ptr<ICameraProvider> interface =
+ ICameraProvider::fromBinder(ndk::SpAIBinder(binder));
+
+ return interface;
+};
+
static std::string getFullAidlProviderName(const std::string instance) {
std::string aidlHalServiceDescriptor =
std::string(aidl::android::hardware::camera::provider::ICameraProvider::descriptor);
@@ -145,6 +171,13 @@
auto sm = defaultServiceManager();
auto aidlProviders = sm->getDeclaredInstances(
String16(aidlHalServiceDescriptor));
+
+ if (isVirtualCameraHalEnabled()) {
+ // Virtual Camera provider is not declared in the VINTF manifest so we
+ // manually add it if the binary is present.
+ aidlProviders.push_back(String16(kVirtualProviderName.c_str()));
+ }
+
for (const auto &aidlInstance : aidlProviders) {
std::string aidlServiceName =
getFullAidlProviderName(toStdString(aidlInstance));
@@ -160,12 +193,19 @@
}
status_t CameraProviderManager::initialize(wp<CameraProviderManager::StatusListener> listener,
- HidlServiceInteractionProxy* hidlProxy) {
+ HidlServiceInteractionProxy* hidlProxy, AidlServiceInteractionProxy* aidlProxy) {
std::lock_guard<std::mutex> lock(mInterfaceMutex);
if (hidlProxy == nullptr) {
- ALOGE("%s: No valid service interaction proxy provided", __FUNCTION__);
+ ALOGE("%s: No valid service Hidl interaction proxy provided", __FUNCTION__);
return BAD_VALUE;
}
+
+ if (aidlProxy == nullptr) {
+ ALOGE("%s: No valid service Aidl interaction proxy provided", __FUNCTION__);
+ return BAD_VALUE;
+ }
+ mAidlServiceProxy = aidlProxy;
+
mListener = listener;
mDeviceState = 0;
auto res = tryToInitAndAddHidlProvidersLocked(hidlProxy);
@@ -1974,14 +2014,8 @@
const std::string& providerName, const sp<ProviderInfo>& providerInfo) {
using aidl::android::hardware::camera::provider::ICameraProvider;
- AIBinder *binder = nullptr;
- if (flags::lazy_aidl_wait_for_service()) {
- binder = AServiceManager_waitForService(providerName.c_str());
- } else {
- binder = AServiceManager_getService(providerName.c_str());
- }
std::shared_ptr<ICameraProvider> interface =
- ICameraProvider::fromBinder(ndk::SpAIBinder(binder));
+ mAidlServiceProxy->getAidlService(providerName.c_str());
if (interface == nullptr) {
ALOGW("%s: AIDL Camera provider HAL '%s' is not actually available", __FUNCTION__,
@@ -3127,4 +3161,8 @@
}
}
+bool CameraProviderManager::isVirtualCameraHalEnabled() {
+ return flags::virtual_camera_service_discovery();
+}
+
} // namespace android
diff --git a/services/camera/libcameraservice/common/CameraProviderManager.h b/services/camera/libcameraservice/common/CameraProviderManager.h
index 2a5e73b..fd04854 100644
--- a/services/camera/libcameraservice/common/CameraProviderManager.h
+++ b/services/camera/libcameraservice/common/CameraProviderManager.h
@@ -174,6 +174,24 @@
virtual hardware::hidl_vec<hardware::hidl_string> listServices() override;
};
+ // Proxy to inject fake services in test.
+ class AidlServiceInteractionProxy {
+ public:
+ // Returns the Aidl service with the given serviceName
+ virtual std::shared_ptr<aidl::android::hardware::camera::provider::ICameraProvider>
+ getAidlService(const std::string& serviceName) = 0;
+
+ virtual ~AidlServiceInteractionProxy() = default;
+ };
+
+ // Standard use case - call into the normal static methods which invoke
+ // the real service manager
+ class AidlServiceInteractionProxyImpl : public AidlServiceInteractionProxy {
+ public:
+ virtual std::shared_ptr<aidl::android::hardware::camera::provider::ICameraProvider>
+ getAidlService(const std::string& serviceName) override;
+ };
+
/**
* Listener interface for device/torch status changes
*/
@@ -209,7 +227,8 @@
* used for testing. The lifetime of the proxy must exceed the lifetime of the manager.
*/
status_t initialize(wp<StatusListener> listener,
- HidlServiceInteractionProxy *hidlProxy = &sHidlServiceInteractionProxy);
+ HidlServiceInteractionProxy* hidlProxy = &sHidlServiceInteractionProxy,
+ AidlServiceInteractionProxy* aidlProxy = &sAidlServiceInteractionProxy);
status_t getCameraIdIPCTransport(const std::string &id,
IPCTransport *providerTransport) const;
@@ -424,6 +443,7 @@
wp<StatusListener> mListener;
HidlServiceInteractionProxy* mHidlServiceProxy;
+ AidlServiceInteractionProxy* mAidlServiceProxy;
// Current overall Android device physical status
int64_t mDeviceState;
@@ -432,6 +452,7 @@
mutable std::mutex mProviderLifecycleLock;
static HidlServiceInteractionProxyImpl sHidlServiceInteractionProxy;
+ static AidlServiceInteractionProxyImpl sAidlServiceInteractionProxy;
struct HalCameraProvider {
// Empty parent struct for storing either aidl / hidl camera provider reference
@@ -868,6 +889,8 @@
status_t usbDeviceDetached(const std::string &usbDeviceId);
ndk::ScopedAStatus onAidlRegistration(const std::string& in_name,
const ::ndk::SpAIBinder& in_binder);
+
+ static bool isVirtualCameraHalEnabled();
};
} // namespace android
diff --git a/services/camera/libcameraservice/common/aidl/AidlProviderInfo.cpp b/services/camera/libcameraservice/common/aidl/AidlProviderInfo.cpp
index 9f28b5f..257103f 100644
--- a/services/camera/libcameraservice/common/aidl/AidlProviderInfo.cpp
+++ b/services/camera/libcameraservice/common/aidl/AidlProviderInfo.cpp
@@ -130,11 +130,15 @@
}
mDeathRecipient = ndk::ScopedAIBinder_DeathRecipient(AIBinder_DeathRecipient_new(binderDied));
- auto link = AIBinder_linkToDeath(interface->asBinder().get(), mDeathRecipient.get(), this);
- if (link != STATUS_OK) {
- ALOGW("%s: Unable to link to provider '%s' death notifications",
- __FUNCTION__, mProviderName.c_str());
- return DEAD_OBJECT;
+
+ if (!flags::virtual_camera_service_discovery() || interface->isRemote()) {
+ binder_status_t link =
+ AIBinder_linkToDeath(interface->asBinder().get(), mDeathRecipient.get(), this);
+ if (link != STATUS_OK) {
+ ALOGW("%s: Unable to link to provider '%s' death notifications (%d)", __FUNCTION__,
+ mProviderName.c_str(), link);
+ return DEAD_OBJECT;
+ }
}
if (!kEnableLazyHal) {
@@ -284,13 +288,12 @@
if (interface == nullptr) {
ALOGV("Camera provider actually needs restart, calling getService(%s)",
mProviderName.c_str());
- AIBinder * binder = nullptr;
- if (flags::lazy_aidl_wait_for_service()) {
- binder = AServiceManager_waitForService(mProviderName.c_str());
- } else {
- binder = AServiceManager_getService(mProviderName.c_str());
+ interface = mManager->mAidlServiceProxy->getAidlService(mProviderName.c_str());
+
+ if (interface == nullptr) {
+ ALOGD("%s: %s service not started", __FUNCTION__, mProviderName.c_str());
+ return nullptr;
}
- interface = ICameraProvider::fromBinder(ndk::SpAIBinder(binder));
// Set all devices as ENUMERATING, provider should update status
// to PRESENT after initializing.