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 EVS_VTS_FRAMEHANDLER_H
18 #define EVS_VTS_FRAMEHANDLER_H
19 
20 #include <queue>
21 
22 #include <FrameHandler.h>
23 
24 #include <android/hardware/automotive/evs/1.1/IEvsCameraStream.h>
25 #include <android/hardware/automotive/evs/1.1/IEvsCamera.h>
26 #include <android/hardware/automotive/evs/1.0/IEvsDisplay.h>
27 
28 using namespace ::android::hardware::automotive::evs::V1_1;
29 using ::android::hardware::Return;
30 using ::android::hardware::Void;
31 using ::android::hardware::hidl_vec;
32 using ::android::hardware::hidl_handle;
33 using ::android::sp;
34 using ::android::hardware::automotive::evs::V1_0::IEvsDisplay;
35 using ::android::hardware::automotive::evs::V1_0::EvsResult;
36 using BufferDesc_1_0 = ::android::hardware::automotive::evs::V1_0::BufferDesc;
37 using BufferDesc_1_1 = ::android::hardware::automotive::evs::V1_1::BufferDesc;
38 
39 
40 /*
41  * FrameHandler:
42  * This class can be used to receive camera imagery from an IEvsCamera implementation.  Given an
43  * IEvsDisplay instance at startup, it will forward the received imagery to the display,
44  * providing a trivial implementation of a rear vew camera type application.
45  * Note that the video frames are delivered on a background thread, while the control interface
46  * is actuated from the applications foreground thread.
47  */
48 class FrameHandler : public IEvsCameraStream {
49 public:
50     enum BufferControlFlag {
51         eAutoReturn,
52         eNoAutoReturn,
53     };
54 
55     FrameHandler(android::sp <IEvsCamera> pCamera, CameraDesc cameraInfo,
56                  android::sp <IEvsDisplay> pDisplay = nullptr,
57                  BufferControlFlag mode = eAutoReturn);
~FrameHandler()58     virtual ~FrameHandler() {
59         if (mCamera != nullptr) {
60             /* shutdown a camera explicitly */
61             shutdown();
62         }
63     }
64 
65     void shutdown();
66 
67     bool startStream();
68     void asyncStopStream();
69     void blockingStopStream();
70 
71     bool returnHeldBuffer();
72 
73     bool isRunning();
74 
75     void waitForFrameCount(unsigned frameCount);
76     bool waitForEvent(const EvsEventDesc& aTargetEvent,
77                             EvsEventDesc& aReceivedEvent,
78                             bool ignorePayload = false);
79     void getFramesCounters(unsigned* received, unsigned* displayed);
80     void getFrameDimension(unsigned* width, unsigned* height);
81 
82 private:
83     // Implementation for ::android::hardware::automotive::evs::V1_0::IEvsCameraStream
84     Return<void> deliverFrame(const BufferDesc_1_0& buffer) override;
85 
86     // Implementation for ::android::hardware::automotive::evs::V1_1::IEvsCameraStream
87     Return<void> deliverFrame_1_1(const hidl_vec<BufferDesc_1_1>& buffer) override;
88     Return<void> notify(const EvsEventDesc& event) override;
89 
90     // Local implementation details
91     bool copyBufferContents(const BufferDesc_1_0& tgtBuffer, const BufferDesc_1_1& srcBuffer);
92     const char *eventToString(const EvsEventType aType);
93 
94     // Values initialized as startup
95     android::sp <IEvsCamera>    mCamera;
96     CameraDesc                  mCameraInfo;
97     android::sp <IEvsDisplay>   mDisplay;
98     BufferControlFlag           mReturnMode;
99 
100     // Since we get frames delivered to us asynchronously via the IEvsCameraStream interface,
101     // we need to protect all member variables that may be modified while we're streaming
102     // (ie: those below)
103     std::mutex                            mLock;
104     std::mutex                            mEventLock;
105     std::condition_variable               mEventSignal;
106     std::condition_variable               mFrameSignal;
107     std::queue<hidl_vec<BufferDesc_1_1>>  mHeldBuffers;
108 
109     bool                        mRunning = false;
110     unsigned                    mFramesReceived = 0;    // Simple counter -- rolls over eventually!
111     unsigned                    mFramesDisplayed = 0;   // Simple counter -- rolls over eventually!
112     unsigned                    mFrameWidth = 0;
113     unsigned                    mFrameHeight = 0;
114     EvsEventDesc                mLatestEventDesc;
115 };
116 
117 
118 #endif //EVS_VTS_FRAMEHANDLER_H
119