1 /* 2 * Copyright (C) 2019 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef ANDROID_AUTOMOTIVE_EVS_V1_1_HALCAMERA_H 18 #define ANDROID_AUTOMOTIVE_EVS_V1_1_HALCAMERA_H 19 20 #include "stats/CameraUsageStats.h" 21 22 #include <deque> 23 #include <list> 24 #include <thread> 25 #include <unordered_map> 26 27 #include <android/hardware/automotive/evs/1.1/types.h> 28 #include <android/hardware/automotive/evs/1.1/IEvsCamera.h> 29 #include <android/hardware/automotive/evs/1.1/IEvsCameraStream.h> 30 #include <utils/Mutex.h> 31 #include <utils/SystemClock.h> 32 33 using namespace ::android::hardware::automotive::evs::V1_1; 34 using ::android::hardware::camera::device::V3_2::Stream; 35 using ::android::hardware::Return; 36 using ::android::hardware::hidl_handle; 37 using ::android::hardware::automotive::evs::V1_0::EvsResult; 38 using IEvsCamera_1_0 = ::android::hardware::automotive::evs::V1_0::IEvsCamera; 39 using IEvsCamera_1_1 = ::android::hardware::automotive::evs::V1_1::IEvsCamera; 40 using BufferDesc_1_0 = ::android::hardware::automotive::evs::V1_0::BufferDesc; 41 using BufferDesc_1_1 = ::android::hardware::automotive::evs::V1_1::BufferDesc; 42 using IEvsCameraStream_1_0 = ::android::hardware::automotive::evs::V1_0::IEvsCameraStream; 43 using IEvsCameraStream_1_1 = ::android::hardware::automotive::evs::V1_1::IEvsCameraStream; 44 45 namespace android { 46 namespace automotive { 47 namespace evs { 48 namespace V1_1 { 49 namespace implementation { 50 51 52 class VirtualCamera; // From VirtualCamera.h 53 54 55 // This class wraps the actual hardware IEvsCamera objects. There is a one to many 56 // relationship between instances of this class and instances of the VirtualCamera class. 57 // This class implements the IEvsCameraStream interface so that it can receive the video 58 // stream from the hardware camera and distribute it to the associated VirtualCamera objects. 59 class HalCamera : public IEvsCameraStream_1_1 { 60 public: 61 HalCamera(sp<IEvsCamera_1_1> hwCamera, 62 std::string deviceId = "", 63 int32_t recordId = 0, 64 Stream cfg = {}) mHwCamera(hwCamera)65 : mHwCamera(hwCamera), 66 mId(deviceId), 67 mStreamConfig(cfg), 68 mTimeCreatedMs(android::uptimeMillis()), 69 mUsageStats(new CameraUsageStats(recordId)) { 70 mCurrentRequests = &mFrameRequests[0]; 71 mNextRequests = &mFrameRequests[1]; 72 } 73 74 virtual ~HalCamera(); 75 76 // Factory methods for client VirtualCameras 77 sp<VirtualCamera> makeVirtualCamera(); 78 bool ownVirtualCamera(sp<VirtualCamera> virtualCamera); 79 void disownVirtualCamera(sp<VirtualCamera> virtualCamera); 80 81 // Implementation details getHwCamera()82 sp<IEvsCamera_1_0> getHwCamera() { return mHwCamera; }; getClientCount()83 unsigned getClientCount() { return mClients.size(); }; getId()84 std::string getId() { return mId; } getStreamConfig()85 Stream& getStreamConfig() { return mStreamConfig; } 86 bool changeFramesInFlight(int delta); 87 bool changeFramesInFlight(const hardware::hidl_vec<BufferDesc_1_1>& buffers, 88 int* delta); 89 void requestNewFrame(sp<VirtualCamera> virtualCamera, 90 const int64_t timestamp); 91 92 Return<EvsResult> clientStreamStarting(); 93 void clientStreamEnding(const VirtualCamera* client); 94 Return<void> doneWithFrame(const BufferDesc_1_0& buffer); 95 Return<void> doneWithFrame(const BufferDesc_1_1& buffer); 96 Return<EvsResult> setMaster(sp<VirtualCamera> virtualCamera); 97 Return<EvsResult> forceMaster(sp<VirtualCamera> virtualCamera); 98 Return<EvsResult> unsetMaster(const VirtualCamera* virtualCamera); 99 Return<EvsResult> setParameter(sp<VirtualCamera> virtualCamera, 100 CameraParam id, int32_t& value); 101 Return<EvsResult> getParameter(CameraParam id, int32_t& value); 102 103 // Returns a snapshot of collected usage statistics 104 CameraUsageStatsRecord getStats() const; 105 106 // Returns active stream configuration 107 Stream getStreamConfiguration() const; 108 109 // Returns a string showing the current status 110 std::string toString(const char* indent = "") const; 111 112 // Returns a string showing current stream configuration 113 static std::string toString(Stream configuration, const char* indent = ""); 114 115 // Methods from ::android::hardware::automotive::evs::V1_0::IEvsCameraStream follow. 116 Return<void> deliverFrame(const BufferDesc_1_0& buffer) override; 117 118 // Methods from ::android::hardware::automotive::evs::V1_1::IEvsCameraStream follow. 119 Return<void> deliverFrame_1_1(const hardware::hidl_vec<BufferDesc_1_1>& buffer) override; 120 Return<void> notify(const EvsEventDesc& event) override; 121 122 private: 123 sp<IEvsCamera_1_1> mHwCamera; 124 std::list<wp<VirtualCamera>> mClients; // Weak pointers -> objects destruct if client dies 125 126 enum { 127 STOPPED, 128 RUNNING, 129 STOPPING, 130 } mStreamState = STOPPED; 131 132 struct FrameRecord { 133 uint32_t frameId; 134 uint32_t refCount; FrameRecordFrameRecord135 FrameRecord(uint32_t id) : frameId(id), refCount(0) {}; 136 }; 137 std::vector<FrameRecord> mFrames; 138 wp<VirtualCamera> mPrimaryClient = nullptr; 139 std::string mId; 140 Stream mStreamConfig; 141 142 struct FrameRequest { 143 wp<VirtualCamera> client = nullptr; 144 int64_t timestamp = -1; 145 }; 146 147 void cancelCaptureRequestFromClientLocked(std::deque<FrameRequest>* requests, 148 const VirtualCamera* client) REQUIRES(mFrameMutex); 149 150 // synchronization 151 mutable std::mutex mFrameMutex; 152 std::deque<FrameRequest> mFrameRequests[2] GUARDED_BY(mFrameMutex); 153 std::deque<FrameRequest>* mCurrentRequests PT_GUARDED_BY(mFrameMutex); 154 std::deque<FrameRequest>* mNextRequests PT_GUARDED_BY(mFrameMutex); 155 156 // Time this object was created 157 int64_t mTimeCreatedMs; 158 159 // usage statistics to collect 160 android::sp<CameraUsageStats> mUsageStats; 161 }; 162 163 } // namespace implementation 164 } // namespace V1_1 165 } // namespace evs 166 } // namespace automotive 167 } // namespace android 168 169 #endif // ANDROID_AUTOMOTIVE_EVS_V1_1_HALCAMERA_H 170