Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 7eccd7c0 authored by Vadim Caen's avatar Vadim Caen Committed by Jan Sebechlebsky
Browse files

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
parent b701a546
Loading
Loading
Loading
Loading
+7 −0
Original line number Diff line number Diff line
@@ -34,3 +34,10 @@ flag {
     description: "Enable measuring how much usage there is for zoom settings overrde"
     bug: "307409002"
}

flag {
     namespace: "camera_platform"
     name: "virtual_camera_service_discovery"
     description: "Enable discovery of the Virtual Camera HAL without a VINTF entry"
     bug: "305170199"
}
+47 −9
Original line number Diff line number Diff line
@@ -63,6 +63,7 @@ namespace flags = com::android::internal::camera::flags;
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 @@ const bool CameraProviderManager::kFrameworkJpegRDisabled =

CameraProviderManager::HidlServiceInteractionProxyImpl
CameraProviderManager::sHidlServiceInteractionProxy{};
CameraProviderManager::AidlServiceInteractionProxyImpl
CameraProviderManager::sAidlServiceInteractionProxy{};

CameraProviderManager::~CameraProviderManager() {
}
@@ -133,6 +136,29 @@ status_t CameraProviderManager::tryToInitAndAddHidlProvidersLocked(
    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 @@ status_t CameraProviderManager::tryToAddAidlProvidersLocked() {
    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::tryToAddAidlProvidersLocked() {
}

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 @@ status_t CameraProviderManager::tryToInitializeAidlProviderLocked(
        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 @@ void CameraProviderManager::filterLogicalCameraIdsLocked(
    }
}

bool CameraProviderManager::isVirtualCameraHalEnabled() {
    return flags::virtual_camera_service_discovery();
}

} // namespace android
+24 −1
Original line number Diff line number Diff line
@@ -174,6 +174,24 @@ public:
        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 @@ public:
     * 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 @@ private:

    wp<StatusListener> mListener;
    HidlServiceInteractionProxy* mHidlServiceProxy;
    AidlServiceInteractionProxy* mAidlServiceProxy;

    // Current overall Android device physical status
    int64_t mDeviceState;
@@ -432,6 +452,7 @@ private:
    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 @@ private:
    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
+14 −11
Original line number Diff line number Diff line
@@ -130,12 +130,16 @@ status_t AidlProviderInfo::initializeAidlProvider(
    }

    mDeathRecipient = ndk::ScopedAIBinder_DeathRecipient(AIBinder_DeathRecipient_new(binderDied));
    auto link = AIBinder_linkToDeath(interface->asBinder().get(), mDeathRecipient.get(), this);

    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",
                __FUNCTION__, mProviderName.c_str());
            ALOGW("%s: Unable to link to provider '%s' death notifications (%d)", __FUNCTION__,
                  mProviderName.c_str(), link);
            return DEAD_OBJECT;
        }
    }

    if (!kEnableLazyHal) {
        // Save HAL reference indefinitely
@@ -284,13 +288,12 @@ const std::shared_ptr<ICameraProvider> AidlProviderInfo::startProviderInterface(
        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.
+2 −0
Original line number Diff line number Diff line
@@ -53,6 +53,7 @@ cc_test {
        "android.hardware.camera.device@3.4",
        "android.hardware.camera.device@3.7",
        "android.hidl.token@1.0-utils",
        "camera_platform_flags_c_lib",
    ],

    static_libs: [
@@ -63,6 +64,7 @@ cc_test {
        "android.hardware.camera.provider-V2-ndk",
        "libcameraservice",
        "libgmock",
        "libflagtest",
    ],

    srcs: [
Loading