1 /*
2  * Copyright (C) 2017 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 <android/hardware/automotive/evs/1.0/IEvsCameraStream.h>
23 #include <android/hardware/automotive/evs/1.0/IEvsCamera.h>
24 #include <android/hardware/automotive/evs/1.0/IEvsDisplay.h>
25 
26 using namespace ::android::hardware::automotive::evs::V1_0;
27 using ::android::hardware::Return;
28 using ::android::hardware::Void;
29 using ::android::hardware::hidl_vec;
30 using ::android::hardware::hidl_handle;
31 using ::android::sp;
32 
33 
34 /*
35  * FrameHandler:
36  * This class can be used to receive camera imagery from an IEvsCamera implementation.  Given an
37  * IEvsDisplay instance at startup, it will forward the received imagery to the display,
38  * providing a trivial implementation of a rear vew camera type application.
39  * Note that the video frames are delivered on a background thread, while the control interface
40  * is actuated from the applications foreground thread.
41  */
42 class FrameHandler : public IEvsCameraStream {
43 public:
44     enum BufferControlFlag {
45         eAutoReturn,
46         eNoAutoReturn,
47     };
48 
49     FrameHandler(android::sp <IEvsCamera> pCamera, CameraDesc cameraInfo,
50                  android::sp <IEvsDisplay> pDisplay = nullptr,
51                  BufferControlFlag mode = eAutoReturn);
52     void shutdown();
53 
54     bool startStream();
55     void asyncStopStream();
56     void blockingStopStream();
57 
58     bool returnHeldBuffer();
59 
60     bool isRunning();
61 
62     void waitForFrameCount(unsigned frameCount);
63     void getFramesCounters(unsigned* received, unsigned* displayed);
64     void getFrameDimension(unsigned* width, unsigned* height);
65 
66 private:
67     // Implementation for ::android::hardware::automotive::evs::V1_0::ICarCameraStream
68     Return<void> deliverFrame(const BufferDesc& buffer)  override;
69 
70     // Local implementation details
71     bool copyBufferContents(const BufferDesc& tgtBuffer, const BufferDesc& srcBuffer);
72 
73     // Values initialized as startup
74     android::sp <IEvsCamera>    mCamera;
75     CameraDesc                  mCameraInfo;
76     android::sp <IEvsDisplay>   mDisplay;
77     BufferControlFlag           mReturnMode;
78 
79     // Since we get frames delivered to us asnchronously via the ICarCameraStream interface,
80     // we need to protect all member variables that may be modified while we're streaming
81     // (ie: those below)
82     std::mutex                  mLock;
83     std::condition_variable     mSignal;
84 
85     std::queue<BufferDesc>      mHeldBuffers;
86     bool                        mRunning = false;
87     unsigned                    mFramesReceived = 0;    // Simple counter -- rolls over eventually!
88     unsigned                    mFramesDisplayed = 0;   // Simple counter -- rolls over eventually!
89     unsigned                    mFrameWidth = 0;
90     unsigned                    mFrameHeight = 0;
91 };
92 
93 
94 #endif //EVS_VTS_FRAMEHANDLER_H
95