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