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 
65 private:
66     // Implementation for ::android::hardware::automotive::evs::V1_0::ICarCameraStream
67     Return<void> deliverFrame(const BufferDesc& buffer)  override;
68 
69     // Local implementation details
70     bool copyBufferContents(const BufferDesc& tgtBuffer, const BufferDesc& srcBuffer);
71 
72     // Values initialized as startup
73     android::sp <IEvsCamera>    mCamera;
74     CameraDesc                  mCameraInfo;
75     android::sp <IEvsDisplay>   mDisplay;
76     BufferControlFlag           mReturnMode;
77 
78     // Since we get frames delivered to us asnchronously via the ICarCameraStream interface,
79     // we need to protect all member variables that may be modified while we're streaming
80     // (ie: those below)
81     std::mutex                  mLock;
82     std::condition_variable     mSignal;
83 
84     std::queue<BufferDesc>      mHeldBuffers;
85     bool                        mRunning = false;
86     unsigned                    mFramesReceived = 0;    // Simple counter -- rolls over eventually!
87     unsigned                    mFramesDisplayed = 0;   // Simple counter -- rolls over eventually!
88 };
89 
90 
91 #endif //EVS_VTS_FRAMEHANDLER_H
92