Changyeon Jo | c7deb56 | 2019-07-23 10:40:04 -0700 | [diff] [blame] | 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 | |
Changyeon Jo | ce08ba8 | 2020-05-06 16:25:21 -0700 | [diff] [blame] | 20 | #include "stats/CameraUsageStats.h" |
Changyeon Jo | c7deb56 | 2019-07-23 10:40:04 -0700 | [diff] [blame] | 21 | |
Changyeon Jo | 03c97d0 | 2019-12-01 14:37:12 -0800 | [diff] [blame] | 22 | #include <deque> |
Changyeon Jo | 7845781 | 2020-04-22 14:18:38 -0700 | [diff] [blame] | 23 | #include <list> |
| 24 | #include <thread> |
Changyeon Jo | 03c97d0 | 2019-12-01 14:37:12 -0800 | [diff] [blame] | 25 | #include <unordered_map> |
Changyeon Jo | c7deb56 | 2019-07-23 10:40:04 -0700 | [diff] [blame] | 26 | |
Changyeon Jo | ce08ba8 | 2020-05-06 16:25:21 -0700 | [diff] [blame] | 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> |
Changyeon Jo | c7deb56 | 2019-07-23 10:40:04 -0700 | [diff] [blame] | 32 | |
| 33 | using namespace ::android::hardware::automotive::evs::V1_1; |
Changyeon Jo | 03c97d0 | 2019-12-01 14:37:12 -0800 | [diff] [blame] | 34 | using ::android::hardware::camera::device::V3_2::Stream; |
Changyeon Jo | c7deb56 | 2019-07-23 10:40:04 -0700 | [diff] [blame] | 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: |
Changyeon Jo | 6bbd9a1 | 2020-05-08 17:26:43 -0700 | [diff] [blame] | 61 | HalCamera(sp<IEvsCamera_1_1> hwCamera, |
| 62 | std::string deviceId = "", |
| 63 | int32_t recordId = 0, |
| 64 | Stream cfg = {}) |
Changyeon Jo | b927b88 | 2019-11-21 10:16:33 -0800 | [diff] [blame] | 65 | : mHwCamera(hwCamera), |
Changyeon Jo | 03c97d0 | 2019-12-01 14:37:12 -0800 | [diff] [blame] | 66 | mId(deviceId), |
Changyeon Jo | b4dcddc | 2020-03-10 20:19:13 -0700 | [diff] [blame] | 67 | mStreamConfig(cfg), |
Changyeon Jo | ce08ba8 | 2020-05-06 16:25:21 -0700 | [diff] [blame] | 68 | mTimeCreatedMs(android::uptimeMillis()), |
Changyeon Jo | 6bbd9a1 | 2020-05-08 17:26:43 -0700 | [diff] [blame] | 69 | mUsageStats(new CameraUsageStats(recordId)) { |
Changyeon Jo | 03c97d0 | 2019-12-01 14:37:12 -0800 | [diff] [blame] | 70 | mCurrentRequests = &mFrameRequests[0]; |
| 71 | mNextRequests = &mFrameRequests[1]; |
| 72 | } |
Changyeon Jo | c7deb56 | 2019-07-23 10:40:04 -0700 | [diff] [blame] | 73 | |
Changyeon Jo | 6bbd9a1 | 2020-05-08 17:26:43 -0700 | [diff] [blame] | 74 | virtual ~HalCamera(); |
| 75 | |
Changyeon Jo | c7deb56 | 2019-07-23 10:40:04 -0700 | [diff] [blame] | 76 | // Factory methods for client VirtualCameras |
| 77 | sp<VirtualCamera> makeVirtualCamera(); |
Changyeon Jo | 03c97d0 | 2019-12-01 14:37:12 -0800 | [diff] [blame] | 78 | bool ownVirtualCamera(sp<VirtualCamera> virtualCamera); |
Changyeon Jo | c7deb56 | 2019-07-23 10:40:04 -0700 | [diff] [blame] | 79 | void disownVirtualCamera(sp<VirtualCamera> virtualCamera); |
| 80 | |
| 81 | // Implementation details |
| 82 | sp<IEvsCamera_1_0> getHwCamera() { return mHwCamera; }; |
| 83 | unsigned getClientCount() { return mClients.size(); }; |
Changyeon Jo | 03c97d0 | 2019-12-01 14:37:12 -0800 | [diff] [blame] | 84 | std::string getId() { return mId; } |
| 85 | Stream& getStreamConfig() { return mStreamConfig; } |
Changyeon Jo | c7deb56 | 2019-07-23 10:40:04 -0700 | [diff] [blame] | 86 | bool changeFramesInFlight(int delta); |
Changyeon Jo | 6e90a3b | 2020-03-29 07:50:53 -0700 | [diff] [blame] | 87 | bool changeFramesInFlight(const hardware::hidl_vec<BufferDesc_1_1>& buffers, |
| 88 | int* delta); |
Changyeon Jo | b1844d4 | 2020-12-08 18:36:00 -0800 | [diff] [blame^] | 89 | void requestNewFrame(sp<VirtualCamera> virtualCamera, |
Changyeon Jo | 03c97d0 | 2019-12-01 14:37:12 -0800 | [diff] [blame] | 90 | const int64_t timestamp); |
Changyeon Jo | c7deb56 | 2019-07-23 10:40:04 -0700 | [diff] [blame] | 91 | |
| 92 | Return<EvsResult> clientStreamStarting(); |
Changyeon Jo | d24fcb2 | 2020-06-09 13:40:31 -0700 | [diff] [blame] | 93 | void clientStreamEnding(const VirtualCamera* client); |
Changyeon Jo | c7deb56 | 2019-07-23 10:40:04 -0700 | [diff] [blame] | 94 | Return<void> doneWithFrame(const BufferDesc_1_0& buffer); |
| 95 | Return<void> doneWithFrame(const BufferDesc_1_1& buffer); |
Changyeon Jo | 330d1c2 | 2019-07-30 11:58:41 -0700 | [diff] [blame] | 96 | Return<EvsResult> setMaster(sp<VirtualCamera> virtualCamera); |
Changyeon Jo | 2c81913 | 2019-08-17 21:35:54 -0700 | [diff] [blame] | 97 | Return<EvsResult> forceMaster(sp<VirtualCamera> virtualCamera); |
Changyeon Jo | 330d1c2 | 2019-07-30 11:58:41 -0700 | [diff] [blame] | 98 | Return<EvsResult> unsetMaster(sp<VirtualCamera> virtualCamera); |
| 99 | Return<EvsResult> setParameter(sp<VirtualCamera> virtualCamera, |
| 100 | CameraParam id, int32_t& value); |
| 101 | Return<EvsResult> getParameter(CameraParam id, int32_t& value); |
Changyeon Jo | c7deb56 | 2019-07-23 10:40:04 -0700 | [diff] [blame] | 102 | |
Changyeon Jo | ce08ba8 | 2020-05-06 16:25:21 -0700 | [diff] [blame] | 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 = ""); |
Changyeon Jo | 298faca | 2020-04-02 12:44:44 -0700 | [diff] [blame] | 114 | |
Changyeon Jo | aab96aa | 2019-10-12 05:24:15 -0700 | [diff] [blame] | 115 | // Methods from ::android::hardware::automotive::evs::V1_0::IEvsCameraStream follow. |
Changyeon Jo | fbf7279 | 2019-10-12 05:21:00 -0700 | [diff] [blame] | 116 | Return<void> deliverFrame(const BufferDesc_1_0& buffer) override; |
Changyeon Jo | aab96aa | 2019-10-12 05:24:15 -0700 | [diff] [blame] | 117 | |
| 118 | // Methods from ::android::hardware::automotive::evs::V1_1::IEvsCameraStream follow. |
Changyeon Jo | 4ede4d6 | 2019-10-22 10:10:15 -0700 | [diff] [blame] | 119 | Return<void> deliverFrame_1_1(const hardware::hidl_vec<BufferDesc_1_1>& buffer) override; |
| 120 | Return<void> notify(const EvsEventDesc& event) override; |
Changyeon Jo | c7deb56 | 2019-07-23 10:40:04 -0700 | [diff] [blame] | 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; |
| 135 | FrameRecord(uint32_t id) : frameId(id), refCount(0) {}; |
| 136 | }; |
| 137 | std::vector<FrameRecord> mFrames; |
Changyeon Jo | 330d1c2 | 2019-07-30 11:58:41 -0700 | [diff] [blame] | 138 | wp<VirtualCamera> mMaster = nullptr; |
Changyeon Jo | 03c97d0 | 2019-12-01 14:37:12 -0800 | [diff] [blame] | 139 | std::string mId; |
| 140 | Stream mStreamConfig; |
| 141 | |
| 142 | struct FrameRequest { |
| 143 | wp<VirtualCamera> client = nullptr; |
| 144 | int64_t timestamp = -1; |
| 145 | }; |
Changyeon Jo | b927b88 | 2019-11-21 10:16:33 -0800 | [diff] [blame] | 146 | |
| 147 | // synchronization |
Changyeon Jo | 298faca | 2020-04-02 12:44:44 -0700 | [diff] [blame] | 148 | mutable std::mutex mFrameMutex; |
Changyeon Jo | 03c97d0 | 2019-12-01 14:37:12 -0800 | [diff] [blame] | 149 | std::deque<FrameRequest> mFrameRequests[2] GUARDED_BY(mFrameMutex); |
| 150 | std::deque<FrameRequest>* mCurrentRequests PT_GUARDED_BY(mFrameMutex); |
| 151 | std::deque<FrameRequest>* mNextRequests PT_GUARDED_BY(mFrameMutex); |
Changyeon Jo | 298faca | 2020-04-02 12:44:44 -0700 | [diff] [blame] | 152 | |
Changyeon Jo | ce08ba8 | 2020-05-06 16:25:21 -0700 | [diff] [blame] | 153 | // Time this object was created |
| 154 | int64_t mTimeCreatedMs; |
| 155 | |
| 156 | // usage statistics to collect |
| 157 | android::sp<CameraUsageStats> mUsageStats; |
Changyeon Jo | c7deb56 | 2019-07-23 10:40:04 -0700 | [diff] [blame] | 158 | }; |
| 159 | |
| 160 | } // namespace implementation |
| 161 | } // namespace V1_1 |
| 162 | } // namespace evs |
| 163 | } // namespace automotive |
| 164 | } // namespace android |
| 165 | |
| 166 | #endif // ANDROID_AUTOMOTIVE_EVS_V1_1_HALCAMERA_H |