/* * Copyright (C) 2019 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef ANDROID_AUTOMOTIVE_EVS_V1_1_HALCAMERA_H #define ANDROID_AUTOMOTIVE_EVS_V1_1_HALCAMERA_H #include "stats/CameraUsageStats.h" #include #include #include #include #include #include #include #include #include using namespace ::android::hardware::automotive::evs::V1_1; using ::android::hardware::camera::device::V3_2::Stream; using ::android::hardware::Return; using ::android::hardware::hidl_handle; using ::android::hardware::automotive::evs::V1_0::EvsResult; using IEvsCamera_1_0 = ::android::hardware::automotive::evs::V1_0::IEvsCamera; using IEvsCamera_1_1 = ::android::hardware::automotive::evs::V1_1::IEvsCamera; using BufferDesc_1_0 = ::android::hardware::automotive::evs::V1_0::BufferDesc; using BufferDesc_1_1 = ::android::hardware::automotive::evs::V1_1::BufferDesc; using IEvsCameraStream_1_0 = ::android::hardware::automotive::evs::V1_0::IEvsCameraStream; using IEvsCameraStream_1_1 = ::android::hardware::automotive::evs::V1_1::IEvsCameraStream; namespace android { namespace automotive { namespace evs { namespace V1_1 { namespace implementation { class VirtualCamera; // From VirtualCamera.h // This class wraps the actual hardware IEvsCamera objects. There is a one to many // relationship between instances of this class and instances of the VirtualCamera class. // This class implements the IEvsCameraStream interface so that it can receive the video // stream from the hardware camera and distribute it to the associated VirtualCamera objects. class HalCamera : public IEvsCameraStream_1_1 { public: HalCamera(sp hwCamera, std::string deviceId = "", int32_t recordId = 0, Stream cfg = {}) : mHwCamera(hwCamera), mId(deviceId), mStreamConfig(cfg), mTimeCreatedMs(android::uptimeMillis()), mUsageStats(new CameraUsageStats(recordId)) { mCurrentRequests = &mFrameRequests[0]; mNextRequests = &mFrameRequests[1]; } virtual ~HalCamera(); // Factory methods for client VirtualCameras sp makeVirtualCamera(); bool ownVirtualCamera(sp virtualCamera); void disownVirtualCamera(sp virtualCamera); // Implementation details sp getHwCamera() { return mHwCamera; }; unsigned getClientCount() { return mClients.size(); }; std::string getId() { return mId; } Stream& getStreamConfig() { return mStreamConfig; } bool changeFramesInFlight(int delta); bool changeFramesInFlight(const hardware::hidl_vec& buffers, int* delta); void requestNewFrame(sp virtualCamera, const int64_t timestamp); Return clientStreamStarting(); void clientStreamEnding(const VirtualCamera* client); Return doneWithFrame(const BufferDesc_1_0& buffer); Return doneWithFrame(const BufferDesc_1_1& buffer); Return setMaster(sp virtualCamera); Return forceMaster(sp virtualCamera); Return unsetMaster(const VirtualCamera* virtualCamera); Return setParameter(sp virtualCamera, CameraParam id, int32_t& value); Return getParameter(CameraParam id, int32_t& value); // Returns a snapshot of collected usage statistics CameraUsageStatsRecord getStats() const; // Returns active stream configuration Stream getStreamConfiguration() const; // Returns a string showing the current status std::string toString(const char* indent = "") const; // Returns a string showing current stream configuration static std::string toString(Stream configuration, const char* indent = ""); // Methods from ::android::hardware::automotive::evs::V1_0::IEvsCameraStream follow. Return deliverFrame(const BufferDesc_1_0& buffer) override; // Methods from ::android::hardware::automotive::evs::V1_1::IEvsCameraStream follow. Return deliverFrame_1_1(const hardware::hidl_vec& buffer) override; Return notify(const EvsEventDesc& event) override; private: sp mHwCamera; std::list> mClients; // Weak pointers -> objects destruct if client dies enum { STOPPED, RUNNING, STOPPING, } mStreamState = STOPPED; struct FrameRecord { uint32_t frameId; uint32_t refCount; FrameRecord(uint32_t id) : frameId(id), refCount(0) {}; }; std::vector mFrames; wp mPrimaryClient = nullptr; std::string mId; Stream mStreamConfig; struct FrameRequest { wp client = nullptr; int64_t timestamp = -1; }; void cancelCaptureRequestFromClientLocked(std::deque* requests, const VirtualCamera* client) REQUIRES(mFrameMutex); // synchronization mutable std::mutex mFrameMutex; std::deque mFrameRequests[2] GUARDED_BY(mFrameMutex); std::deque* mCurrentRequests PT_GUARDED_BY(mFrameMutex); std::deque* mNextRequests PT_GUARDED_BY(mFrameMutex); // Time this object was created int64_t mTimeCreatedMs; // usage statistics to collect android::sp mUsageStats; }; } // namespace implementation } // namespace V1_1 } // namespace evs } // namespace automotive } // namespace android #endif // ANDROID_AUTOMOTIVE_EVS_V1_1_HALCAMERA_H