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 #include "sync/unique_fd.h"
22 #include "sync/unique_fence.h"
23 #include "sync/unique_timeline.h"
24 
25 #include <deque>
26 #include <list>
27 #include <thread>
28 #include <unordered_map>
29 
30 #include <android/hardware/automotive/evs/1.1/types.h>
31 #include <android/hardware/automotive/evs/1.1/IEvsCamera.h>
32 #include <android/hardware/automotive/evs/1.1/IEvsCameraStream.h>
33 #include <utils/Mutex.h>
34 #include <utils/SystemClock.h>
35 
36 using namespace ::android::hardware::automotive::evs::V1_1;
37 using ::android::hardware::camera::device::V3_2::Stream;
38 using ::android::hardware::Return;
39 using ::android::hardware::hidl_handle;
40 using ::android::hardware::automotive::evs::V1_0::EvsResult;
41 using IEvsCamera_1_0 = ::android::hardware::automotive::evs::V1_0::IEvsCamera;
42 using IEvsCamera_1_1 = ::android::hardware::automotive::evs::V1_1::IEvsCamera;
43 using BufferDesc_1_0 = ::android::hardware::automotive::evs::V1_0::BufferDesc;
44 using BufferDesc_1_1 = ::android::hardware::automotive::evs::V1_1::BufferDesc;
45 using IEvsCameraStream_1_0 = ::android::hardware::automotive::evs::V1_0::IEvsCameraStream;
46 using IEvsCameraStream_1_1 = ::android::hardware::automotive::evs::V1_1::IEvsCameraStream;
47 
48 namespace android {
49 namespace automotive {
50 namespace evs {
51 namespace V1_1 {
52 namespace implementation {
53 
54 
55 class VirtualCamera;    // From VirtualCamera.h
56 
57 
58 // This class wraps the actual hardware IEvsCamera objects.  There is a one to many
59 // relationship between instances of this class and instances of the VirtualCamera class.
60 // This class implements the IEvsCameraStream interface so that it can receive the video
61 // stream from the hardware camera and distribute it to the associated VirtualCamera objects.
62 class HalCamera : public IEvsCameraStream_1_1 {
63 public:
64     HalCamera(sp<IEvsCamera_1_1> hwCamera,
65               std::string deviceId = "",
66               int32_t recordId = 0,
67               Stream cfg = {})
mHwCamera(hwCamera)68         : mHwCamera(hwCamera),
69           mId(deviceId),
70           mStreamConfig(cfg),
71           mSyncSupported(UniqueTimeline::Supported()),
72           mTimeCreatedMs(android::uptimeMillis()),
73           mUsageStats(new CameraUsageStats(recordId)) {
74         mCurrentRequests = &mFrameRequests[0];
75         mNextRequests    = &mFrameRequests[1];
76     }
77 
78     virtual ~HalCamera();
79 
80     // Factory methods for client VirtualCameras
81     sp<VirtualCamera>     makeVirtualCamera();
82     bool                  ownVirtualCamera(sp<VirtualCamera> virtualCamera);
83     void                  disownVirtualCamera(sp<VirtualCamera> virtualCamera);
84 
85     // Implementation details
getHwCamera()86     sp<IEvsCamera_1_0>  getHwCamera()       { return mHwCamera; };
getClientCount()87     unsigned            getClientCount()    { return mClients.size(); };
getId()88     std::string         getId()             { return mId; }
getStreamConfig()89     Stream&             getStreamConfig()   { return mStreamConfig; }
90     bool                changeFramesInFlight(int delta);
91     bool                changeFramesInFlight(const hardware::hidl_vec<BufferDesc_1_1>& buffers,
92                                              int* delta);
93     UniqueFence         requestNewFrame(sp<VirtualCamera> virtualCamera,
94                                         const int64_t timestamp);
95 
96     Return<EvsResult>   clientStreamStarting();
97     void                clientStreamEnding(const VirtualCamera* client);
98     Return<void>        doneWithFrame(const BufferDesc_1_0& buffer);
99     Return<void>        doneWithFrame(const BufferDesc_1_1& buffer);
100     Return<EvsResult>   setMaster(sp<VirtualCamera> virtualCamera);
101     Return<EvsResult>   forceMaster(sp<VirtualCamera> virtualCamera);
102     Return<EvsResult>   unsetMaster(sp<VirtualCamera> virtualCamera);
103     Return<EvsResult>   setParameter(sp<VirtualCamera> virtualCamera,
104                                      CameraParam id, int32_t& value);
105     Return<EvsResult>   getParameter(CameraParam id, int32_t& value);
isSyncSupported()106     bool                isSyncSupported() const { return mSyncSupported; }
107 
108     // Returns a snapshot of collected usage statistics
109     CameraUsageStatsRecord getStats() const;
110 
111     // Returns active stream configuration
112     Stream getStreamConfiguration() const;
113 
114     // Returns a string showing the current status
115     std::string toString(const char* indent = "") const;
116 
117     // Returns a string showing current stream configuration
118     static std::string toString(Stream configuration, const char* indent = "");
119 
120     // Methods from ::android::hardware::automotive::evs::V1_0::IEvsCameraStream follow.
121     Return<void> deliverFrame(const BufferDesc_1_0& buffer) override;
122 
123     // Methods from ::android::hardware::automotive::evs::V1_1::IEvsCameraStream follow.
124     Return<void> deliverFrame_1_1(const hardware::hidl_vec<BufferDesc_1_1>& buffer) override;
125     Return<void> notify(const EvsEventDesc& event) override;
126 
127 private:
128     sp<IEvsCamera_1_1>              mHwCamera;
129     std::list<wp<VirtualCamera>>    mClients;   // Weak pointers -> objects destruct if client dies
130 
131     enum {
132         STOPPED,
133         RUNNING,
134         STOPPING,
135     }                               mStreamState = STOPPED;
136 
137     struct FrameRecord {
138         uint32_t    frameId;
139         uint32_t    refCount;
FrameRecordFrameRecord140         FrameRecord(uint32_t id) : frameId(id), refCount(0) {};
141     };
142     std::vector<FrameRecord>        mFrames;
143     wp<VirtualCamera>               mMaster = nullptr;
144     std::string                     mId;
145     Stream                          mStreamConfig;
146 
147     struct FrameRequest {
148         wp<VirtualCamera> client = nullptr;
149         int64_t           timestamp = -1;
150     };
151 
152     // synchronization
153     mutable std::mutex        mFrameMutex;
154     std::deque<FrameRequest>  mFrameRequests[2] GUARDED_BY(mFrameMutex);
155     std::deque<FrameRequest>* mCurrentRequests  PT_GUARDED_BY(mFrameMutex);
156     std::deque<FrameRequest>* mNextRequests     PT_GUARDED_BY(mFrameMutex);
157     std::unordered_map<uint64_t,
158                        std::unique_ptr<UniqueTimeline>> mTimelines GUARDED_BY(mFrameMutex);
159     bool                      mSyncSupported;
160 
161     // Time this object was created
162     int64_t mTimeCreatedMs;
163 
164     // usage statistics to collect
165     android::sp<CameraUsageStats> mUsageStats;
166 };
167 
168 } // namespace implementation
169 } // namespace V1_1
170 } // namespace evs
171 } // namespace automotive
172 } // namespace android
173 
174 #endif  // ANDROID_AUTOMOTIVE_EVS_V1_1_HALCAMERA_H
175