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

Commit b2bc5a46 authored by Emilian Peev's avatar Emilian Peev Committed by Yin-Chia Yeh
Browse files

Camera: Initial offline session client

Test: N/A, no implementation yet
Bug: 135142453
Change-Id: I08fecba80ab88a8b70fe71fdd4b660d49e40324c
parent 5fd603ea
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -189,5 +189,5 @@ interface ICameraDeviceUser
     * @return Offline session object.
     */
    ICameraOfflineSession switchToOffline(in ICameraDeviceCallbacks callbacks,
            in Surface[] offlineOutputs);
            in int[] offlineOutputIds);
}
+1 −0
Original line number Diff line number Diff line
@@ -41,6 +41,7 @@ cc_library_shared {
        "api1/client2/CaptureSequencer.cpp",
        "api1/client2/ZslProcessor.cpp",
        "api2/CameraDeviceClient.cpp",
        "api2/CameraOfflineSessionClient.cpp",
        "api2/CompositeStream.cpp",
        "api2/DepthCompositeStream.cpp",
        "api2/HeicEncoderInfoManager.cpp",
+109 −30
Original line number Diff line number Diff line
@@ -128,6 +128,7 @@ static const String16
static constexpr int32_t kVendorClientScore = 200;
// Matches with PROCESS_STATE_PERSISTENT_UI in ActivityManager.java
static constexpr int32_t kVendorClientState = 1;
const String8 CameraService::kOfflineDevice("offline-");

Mutex CameraService::sProxyMutex;
sp<hardware::ICameraServiceProxy> CameraService::sCameraServiceProxy;
@@ -394,7 +395,7 @@ void CameraService::onDeviceStatusChanged(const String8& id,
        // to this device until the status changes
        updateStatus(StatusInternal::NOT_PRESENT, id);

        sp<BasicClient> clientToDisconnect;
        sp<BasicClient> clientToDisconnectOnline, clientToDisconnectOffline;
        {
            // Don't do this in updateStatus to avoid deadlock over mServiceLock
            Mutex::Autolock lock(mServiceLock);
@@ -402,11 +403,27 @@ void CameraService::onDeviceStatusChanged(const String8& id,
            // Remove cached shim parameters
            state->setShimParams(CameraParameters());

            // Remove the client from the list of active clients, if there is one
            clientToDisconnect = removeClientLocked(id);
            // Remove online as well as offline client from the list of active clients,
            // if they are present
            clientToDisconnectOnline = removeClientLocked(id);
            clientToDisconnectOffline = removeClientLocked(kOfflineDevice + id);
        }

        // Disconnect client
        disconnectClient(id, clientToDisconnectOnline);
        disconnectClient(kOfflineDevice + id, clientToDisconnectOffline);

        removeStates(id);
    } else {
        if (oldStatus == StatusInternal::NOT_PRESENT) {
            logDeviceAdded(id, String8::format("Device status changed from %d to %d", oldStatus,
                    newStatus));
        }
        updateStatus(newStatus, id);
    }

}

void CameraService::disconnectClient(const String8& id, sp<BasicClient> clientToDisconnect) {
    if (clientToDisconnect.get() != nullptr) {
        ALOGI("%s: Client for camera ID %s evicted due to device status change from HAL",
                __FUNCTION__, id.string());
@@ -419,16 +436,6 @@ void CameraService::onDeviceStatusChanged(const String8& id,
                "onDeviceStatusChanged must be called from the camera service process!");
        clientToDisconnect->disconnect();
    }

        removeStates(id);
    } else {
        if (oldStatus == StatusInternal::NOT_PRESENT) {
            logDeviceAdded(id, String8::format("Device status changed from %d to %d", oldStatus,
                    newStatus));
        }
        updateStatus(newStatus, id);
    }

}

void CameraService::onTorchStatusChanged(const String8& cameraId,
@@ -1696,6 +1703,77 @@ Status CameraService::connectHelper(const sp<CALLBACK>& cameraCb, const String8&
    return ret;
}

status_t CameraService::addOfflineClient(String8 cameraId, sp<BasicClient> offlineClient) {
    if (offlineClient.get() == nullptr) {
        return BAD_VALUE;
    }

    {
        // Acquire mServiceLock and prevent other clients from connecting
        std::unique_ptr<AutoConditionLock> lock =
                AutoConditionLock::waitAndAcquire(mServiceLockWrapper, DEFAULT_CONNECT_TIMEOUT_NS);

        if (lock == nullptr) {
            ALOGE("%s: (PID %d) rejected (too many other clients connecting)."
                    , __FUNCTION__, offlineClient->getClientPid());
            return TIMED_OUT;
        }

        auto onlineClientDesc = mActiveClientManager.get(cameraId);
        if (onlineClientDesc.get() == nullptr) {
            ALOGE("%s: No active online client using camera id: %s", __FUNCTION__,
                    cameraId.c_str());
            return BAD_VALUE;
        }

        // Offline clients do not evict or conflict with other online devices. Resource sharing
        // conflicts are handled by the camera provider which will either succeed or fail before
        // reaching this method.
        const auto& onlinePriority = onlineClientDesc->getPriority();
        auto offlineClientDesc = CameraClientManager::makeClientDescriptor(
                kOfflineDevice + onlineClientDesc->getKey(), offlineClient, /*cost*/ 0,
                /*conflictingKeys*/ std::set<String8>(), onlinePriority.getScore(),
                onlineClientDesc->getOwnerId(), onlinePriority.getState());

        // Allow only one offline device per camera
        auto incompatibleClients = mActiveClientManager.getIncompatibleClients(offlineClientDesc);
        if (!incompatibleClients.empty()) {
            ALOGE("%s: Incompatible offline clients present!", __FUNCTION__);
            return BAD_VALUE;
        }

        auto err = offlineClient->initialize(mCameraProviderManager, mMonitorTags);
        if (err != OK) {
            ALOGE("%s: Could not initialize offline client.", __FUNCTION__);
            return err;
        }

        auto evicted = mActiveClientManager.addAndEvict(offlineClientDesc);
        if (evicted.size() > 0) {
            for (auto& i : evicted) {
                ALOGE("%s: Invalid state: Offline client for camera %s was not removed ",
                        __FUNCTION__, i->getKey().string());
            }

            LOG_ALWAYS_FATAL("%s: Invalid state for CameraService, offline clients not evicted "
                    "properly", __FUNCTION__);

            return BAD_VALUE;
        }

        logConnectedOffline(offlineClientDesc->getKey(),
                static_cast<int>(offlineClientDesc->getOwnerId()),
                String8(offlineClient->getPackageName()));

        sp<IBinder> remoteCallback = offlineClient->getRemote();
        if (remoteCallback != nullptr) {
            remoteCallback->linkToDeath(this);
        }
    } // lock is destroyed, allow further connect calls

    return OK;
}

Status CameraService::setTorchMode(const String16& cameraId, bool enabled,
        const sp<IBinder>& clientBinder) {
    Mutex::Autolock lock(mServiceLock);
@@ -2300,6 +2378,13 @@ void CameraService::logDisconnected(const char* cameraId, int clientPid,
            clientPackage, clientPid));
}

void CameraService::logDisconnectedOffline(const char* cameraId, int clientPid,
        const char* clientPackage) {
    // Log the clients evicted
    logEvent(String8::format("DISCONNECT offline device %s client for package %s (PID %d)",
                cameraId, clientPackage, clientPid));
}

void CameraService::logConnected(const char* cameraId, int clientPid,
        const char* clientPackage) {
    // Log the clients evicted
@@ -2307,6 +2392,13 @@ void CameraService::logConnected(const char* cameraId, int clientPid,
            clientPackage, clientPid));
}

void CameraService::logConnectedOffline(const char* cameraId, int clientPid,
        const char* clientPackage) {
    // Log the clients evicted
    logEvent(String8::format("CONNECT offline device %s client for package %s (PID %d)", cameraId,
            clientPackage, clientPid));
}

void CameraService::logRejected(const char* cameraId, int clientPid,
        const char* clientPackage, const char* reason) {
    // Log the client rejected
@@ -2744,6 +2836,7 @@ void CameraService::BasicClient::opChanged(int32_t op, const String16&) {
    if (mAppOpsManager == nullptr) {
        return;
    }
    // TODO : add offline camera session case
    if (op != AppOpsManager::OP_CAMERA) {
        ALOGW("Unexpected app ops notification received: %d", op);
        return;
@@ -2778,20 +2871,6 @@ void CameraService::BasicClient::block() {

// ----------------------------------------------------------------------------

sp<CameraService> CameraService::OfflineClient::sCameraService;

status_t CameraService::OfflineClient::startCameraOps() {
    // TODO
    return OK;
}

status_t CameraService::OfflineClient::finishCameraOps() {
    // TODO
    return OK;
}

// ----------------------------------------------------------------------------

void CameraService::Client::notifyError(int32_t errorCode,
        const CaptureResultExtras& resultExtras) {
    (void) resultExtras;
+23 −84
Original line number Diff line number Diff line
@@ -68,6 +68,7 @@ class CameraService :
{
    friend class BinderService<CameraService>;
    friend class CameraClient;
    friend class CameraOfflineSessionClient;
public:
    class Client;
    class BasicClient;
@@ -185,6 +186,9 @@ public:
    // Monitored UIDs availability notification
    void                notifyMonitoredUids();

    // Register an offline client for a given active camera id
    status_t addOfflineClient(String8 cameraId, sp<BasicClient> offlineClient);

    /////////////////////////////////////////////////////////////////////
    // Client functionality

@@ -310,10 +314,9 @@ public:
        sp<IBinder>                     mRemoteBinder;   // immutable after constructor

        // permissions management
        status_t                        startCameraOps();
        status_t                        finishCameraOps();
        virtual status_t                startCameraOps();
        virtual status_t                finishCameraOps();

    private:
        std::unique_ptr<AppOpsManager>  mAppOpsManager = nullptr;

        class OpsCallback : public BnAppOpsCallback {
@@ -402,87 +405,6 @@ public:
        int mCameraId;  // All API1 clients use integer camera IDs
    }; // class Client


    // Client for offline session. Note that offline session client does not affect camera service's
    // client arbitration logic. It is camera HAL's decision to decide whether a normal camera
    // client is conflicting with existing offline client(s).
    // The other distinctive difference between offline clients and normal clients is that normal
    // clients are created through ICameraService binder calls, while the offline session client
    // is created through ICameraDeviceUser::switchToOffline call.
    class OfflineClient : public virtual RefBase {

        virtual status_t dump(int fd, const Vector<String16>& args) = 0;

        // Block the client form using the camera
        virtual void block() = 0;

        // Return the package name for this client
        virtual String16 getPackageName() const = 0;

        // Notify client about a fatal error
        // TODO: maybe let impl notify within block?
        virtual void notifyError(int32_t errorCode,
                const CaptureResultExtras& resultExtras) = 0;

        // Get the UID of the application client using this
        virtual uid_t getClientUid() const = 0;

        // Get the PID of the application client using this
        virtual int getClientPid() const = 0;

        protected:
            OfflineClient(const sp<CameraService>& cameraService,
                    const String16& clientPackageName,
                    const String8& cameraIdStr,
                    int clientPid,
                    uid_t clientUid,
                    int servicePid): mCameraIdStr(cameraIdStr),
                            mClientPackageName(clientPackageName), mClientPid(clientPid),
                            mClientUid(clientUid), mServicePid(servicePid) {
                if (sCameraService == nullptr) {
                    sCameraService = cameraService;
                }
            }

            virtual ~OfflineClient() { /*TODO*/ }

            // these are initialized in the constructor.
            static sp<CameraService>        sCameraService;
            const String8                   mCameraIdStr;
            String16                        mClientPackageName;
            pid_t                           mClientPid;
            const uid_t                     mClientUid;
            const pid_t                     mServicePid;
            bool                            mDisconnected;

            // - The app-side Binder interface to receive callbacks from us
            sp<IBinder>                     mRemoteBinder;   // immutable after constructor

            // permissions management
            status_t                        startCameraOps();
            status_t                        finishCameraOps();

        private:
            std::unique_ptr<AppOpsManager>  mAppOpsManager = nullptr;

            class OpsCallback : public BnAppOpsCallback {
            public:
                explicit OpsCallback(wp<OfflineClient> client) : mClient(client) {}
                virtual void opChanged(int32_t /*op*/, const String16& /*packageName*/) {
                    //TODO
                }

            private:
                wp<OfflineClient> mClient;

            }; // class OpsCallback

            sp<OpsCallback> mOpsCallback;

            // IAppOpsCallback interface, indirected through opListener
            // virtual void opChanged(int32_t op, const String16& packageName);
    }; // class OfflineClient

    /**
     * A listener class that implements the LISTENER interface for use with a ClientManager, and
     * implements the following methods:
@@ -871,6 +793,17 @@ private:
     */
    void logDisconnected(const char* cameraId, int clientPid, const char* clientPackage);

    /**
     * Add an event log message that a client has been disconnected from offline device.
     */
    void logDisconnectedOffline(const char* cameraId, int clientPid, const char* clientPackage);

    /**
     * Add an event log message that an offline client has been connected.
     */
    void logConnectedOffline(const char* cameraId, int clientPid,
            const char* clientPackage);

    /**
     * Add an event log message that a client has been connected.
     */
@@ -1095,6 +1028,12 @@ private:
    void broadcastTorchModeStatus(const String8& cameraId,
            hardware::camera::common::V1_0::TorchModeStatus status);

    void disconnectClient(const String8& id, sp<BasicClient> clientToDisconnect);

    // Regular online and offline devices must not be in conflict at camera service layer.
    // Use separate keys for offline devices.
    static const String8 kOfflineDevice;

    // TODO: right now each BasicClient holds one AppOpsManager instance.
    // We can refactor the code so all of clients share this instance
    AppOpsManager mAppOps;
+28 −16
Original line number Diff line number Diff line
@@ -1903,7 +1903,7 @@ binder::Status CameraDeviceClient::getGlobalAudioRestriction(/*out*/ int32_t* ou

binder::Status CameraDeviceClient::switchToOffline(
        const sp<hardware::camera2::ICameraDeviceCallbacks>& cameraCb,
        const std::vector<view::Surface>& offlineOutputs,
        const std::vector<int>& offlineOutputIds,
        /*out*/
        sp<hardware::camera2::ICameraOfflineSession>* session) {
    ATRACE_CALL();
@@ -1917,7 +1917,7 @@ binder::Status CameraDeviceClient::switchToOffline(
        return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
    }

    if (offlineOutputs.empty()) {
    if (offlineOutputIds.empty()) {
        String8 msg = String8::format("Offline outputs must not be empty");
        ALOGE("%s: %s", __FUNCTION__, msg.string());
        return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
@@ -1929,10 +1929,9 @@ binder::Status CameraDeviceClient::switchToOffline(
        return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
    }

    std::vector<int32_t> offlineStreamIds(offlineOutputs.size());
    for (auto& surface : offlineOutputs) {
        sp<IBinder> binder = IInterface::asBinder(surface.graphicBufferProducer);
        ssize_t index = mStreamMap.indexOfKey(binder);
    std::vector<int32_t> offlineStreamIds(offlineOutputIds.size());
    for (const auto& streamId : offlineOutputIds) {
        ssize_t index = mConfiguredOutputs.indexOfKey(streamId);
        if (index == NAME_NOT_FOUND) {
            String8 msg = String8::format("Offline output is invalid");
            ALOGE("%s: %s", __FUNCTION__, msg.string());
@@ -1940,13 +1939,17 @@ binder::Status CameraDeviceClient::switchToOffline(
        }
        // TODO: Also check whether the offline output is supported by Hal for offline mode.

        sp<Surface> s = new Surface(surface.graphicBufferProducer);
        bool isCompositeStream = camera3::DepthCompositeStream::isDepthCompositeStream(s);
        isCompositeStream |= camera3::HeicCompositeStream::isHeicCompositeStream(s);
        bool isCompositeStream = false;
        for (const auto& gbp : mConfiguredOutputs[streamId].getGraphicBufferProducers()) {
            sp<Surface> s = new Surface(gbp, false /*controlledByApp*/);
            isCompositeStream = camera3::DepthCompositeStream::isDepthCompositeStream(s) |
                camera3::HeicCompositeStream::isHeicCompositeStream(s);
        }

        if (isCompositeStream) {
            // TODO: Add composite specific handling
        } else {
            offlineStreamIds.push_back(mStreamMap.valueAt(index).streamId());
            offlineStreamIds.push_back(streamId);
        }
    }

@@ -1959,15 +1962,24 @@ binder::Status CameraDeviceClient::switchToOffline(
    }

    sp<CameraOfflineSessionClient> offlineClient = new CameraOfflineSessionClient(sCameraService,
            offlineSession, cameraCb, mClientPackageName, mCameraIdStr, mClientPid, mClientUid,
            mServicePid);
    ret = offlineClient->initialize();
            offlineSession, cameraCb, mClientPackageName, mClientFeatureId, mCameraIdStr,
            mCameraFacing, mClientPid, mClientUid, mServicePid);
    ret = sCameraService->addOfflineClient(mCameraIdStr, offlineClient);
    if (ret == OK) {
        // TODO: We need to update mStreamMap, mConfiguredOutputs
    } else {
        switch(ret) {
            case BAD_VALUE:
                return STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
                "Camera %s: Failed to initilize offline session: %s (%d)",
                mCameraIdStr.string(), strerror(ret), ret);
                        "Illegal argument to HAL module for camera \"%s\"", mCameraIdStr.c_str());
            case TIMED_OUT:
                return STATUS_ERROR_FMT(CameraService::ERROR_CAMERA_IN_USE,
                        "Camera \"%s\" is already open", mCameraIdStr.c_str());
            default:
                return STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
                        "Failed to initialize camera \"%s\": %s (%d)", mCameraIdStr.c_str(),
                        strerror(-ret), ret);
        }
    }

    *session = offlineClient;
Loading