• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.1/IEvsCameraStream.h>
25 #include <android/hardware/automotive/evs/1.1/IEvsCamera.h>
26 #include <android/hardware/automotive/evs/1.1/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_1::IEvsDisplay;
35 using EvsDisplayState = ::android::hardware::automotive::evs::V1_0::DisplayState;
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  * StreamHandler:
42  * This class can be used to receive camera imagery from an IEvsCamera implementation.  It will
43  * hold onto the most recent image buffer, returning older ones.
44  * Note that the video frames are delivered on a background thread, while the control interface
45  * is actuated from the applications foreground thread.
46  */
47 class StreamHandler : public IEvsCameraStream {
48 public:
~StreamHandler()49     virtual ~StreamHandler() { shutdown(); };
50 
51     StreamHandler(android::sp <IEvsCamera> pCamera,
52                   uint32_t numBuffers = 2,
53                   bool useOwnBuffers = false,
54                   android_pixel_format_t format = HAL_PIXEL_FORMAT_RGBA_8888,
55                   int32_t width = 640,
56                   int32_t height = 360);
57     void shutdown();
58 
59     bool startStream();
60     void asyncStopStream();
61     void blockingStopStream();
62 
63     bool isRunning();
64 
65     bool newFrameAvailable();
66     const BufferDesc_1_1& getNewFrame();
67     void doneWithFrame(const BufferDesc_1_1& buffer);
68 
69 private:
70     // Implementation for ::android::hardware::automotive::evs::V1_0::IEvsCameraStream
71     Return<void> deliverFrame(const BufferDesc_1_0& buffer)  override;
72 
73     // Implementation for ::android::hardware::automotive::evs::V1_1::IEvsCameraStream
74     Return<void> deliverFrame_1_1(const hidl_vec<BufferDesc_1_1>& buffer)  override;
75     Return<void> notify(const EvsEventDesc& event) override;
76 
77     // Values initialized as startup
78     android::sp <IEvsCamera>    mCamera;
79 
80     // Since we get frames delivered to us asnchronously via the ICarCameraStream interface,
81     // we need to protect all member variables that may be modified while we're streaming
82     // (ie: those below)
83     std::mutex                  mLock;
84     std::condition_variable     mSignal;
85 
86     bool                        mRunning = false;
87 
88     BufferDesc                  mBuffers[2];
89     int                         mHeldBuffer = -1;   // Index of the one currently held by the client
90     int                         mReadyBuffer = -1;  // Index of the newest available buffer
91     hidl_vec<BufferDesc_1_1>    mOwnBuffers;
92     bool                        mUseOwnBuffers;
93 };
94 
95 
96 #endif //EVS_VTS_STREAMHANDLER_H
97