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_STREAMHANDLER_H
18 #define EVS_VTS_STREAMHANDLER_H
19 
20 #include <queue>
21 
22 #include "ui/GraphicBuffer.h"
23 
24 #include <android/hardware/automotive/evs/1.0/IEvsCameraStream.h>
25 #include <android/hardware/automotive/evs/1.0/IEvsCamera.h>
26 #include <android/hardware/automotive/evs/1.0/IEvsDisplay.h>
27 
28 using namespace ::android::hardware::automotive::evs::V1_0;
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 
35 
36 /*
37  * StreamHandler:
38  * This class can be used to receive camera imagery from an IEvsCamera implementation.  It will
39  * hold onto the most recent image buffer, returning older ones.
40  * Note that the video frames are delivered on a background thread, while the control interface
41  * is actuated from the applications foreground thread.
42  */
43 class StreamHandler : public IEvsCameraStream {
44 public:
~StreamHandler()45     virtual ~StreamHandler() { shutdown(); };
46 
47     StreamHandler(android::sp <IEvsCamera> pCamera);
48     void shutdown();
49 
50     bool startStream();
51     void asyncStopStream();
52     void blockingStopStream();
53 
54     bool isRunning();
55 
56     bool newFrameAvailable();
57     const BufferDesc& getNewFrame();
58     void doneWithFrame(const BufferDesc& buffer);
59 
60 private:
61     // Implementation for ::android::hardware::automotive::evs::V1_0::ICarCameraStream
62     Return<void> deliverFrame(const BufferDesc& buffer)  override;
63 
64     // Values initialized as startup
65     android::sp <IEvsCamera>    mCamera;
66 
67     // Since we get frames delivered to us asnchronously via the ICarCameraStream interface,
68     // we need to protect all member variables that may be modified while we're streaming
69     // (ie: those below)
70     std::mutex                  mLock;
71     std::condition_variable     mSignal;
72 
73     bool                        mRunning = false;
74 
75     BufferDesc                  mBuffers[2];
76     int                         mHeldBuffer = -1;   // Index of the one currently held by the client
77     int                         mReadyBuffer = -1;  // Index of the newest available buffer
78 };
79 
80 
81 #endif //EVS_VTS_STREAMHANDLER_H
82