blob: e5f1b8f1123577ce35d8c7ddb64198c30da30c21 [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;
Changyeon Jo2400b692019-07-18 21:32:48 -070036using BufferDesc_1_0 = ::android::hardware::automotive::evs::V1_0::BufferDesc;
37using BufferDesc_1_1 = ::android::hardware::automotive::evs::V1_1::BufferDesc;
38
39
40/*
41 * FrameHandler:
42 * This class can be used to receive camera imagery from an IEvsCamera implementation. Given an
43 * IEvsDisplay instance at startup, it will forward the received imagery to the display,
44 * providing a trivial implementation of a rear vew camera type application.
45 * Note that the video frames are delivered on a background thread, while the control interface
46 * is actuated from the applications foreground thread.
47 */
48class FrameHandler : public IEvsCameraStream {
49public:
50 enum BufferControlFlag {
51 eAutoReturn,
52 eNoAutoReturn,
53 };
54
55 FrameHandler(android::sp <IEvsCamera> pCamera, CameraDesc cameraInfo,
56 android::sp <IEvsDisplay> pDisplay = nullptr,
57 BufferControlFlag mode = eAutoReturn);
Changyeon Joc6fa0ab2019-10-12 05:25:44 -070058 virtual ~FrameHandler() {
59 if (mCamera != nullptr) {
60 /* shutdown a camera explicitly */
61 shutdown();
62 }
63 }
64
Changyeon Jo2400b692019-07-18 21:32:48 -070065 void shutdown();
66
67 bool startStream();
68 void asyncStopStream();
69 void blockingStopStream();
70
71 bool returnHeldBuffer();
72
73 bool isRunning();
74
75 void waitForFrameCount(unsigned frameCount);
Changyeon Joc6fa0ab2019-10-12 05:25:44 -070076 bool waitForEvent(const EvsEventType aTargetEvent,
77 EvsEvent &eventDesc);
Changyeon Jo2400b692019-07-18 21:32:48 -070078 void getFramesCounters(unsigned* received, unsigned* displayed);
79 void getFrameDimension(unsigned* width, unsigned* height);
80
81private:
Changyeon Joc6fa0ab2019-10-12 05:25:44 -070082 // Implementation for ::android::hardware::automotive::evs::V1_0::IEvsCameraStream
Changyeon Jo468cc1d2019-10-12 05:18:16 -070083 Return<void> deliverFrame(const BufferDesc_1_0& buffer) override;
Changyeon Joc6fa0ab2019-10-12 05:25:44 -070084
85 // Implementation for ::android::hardware::automotive::evs::V1_1::IEvsCameraStream
86 Return<void> deliverFrame_1_1(const BufferDesc_1_1& buffer) override;
87 Return<void> notify(const EvsEvent& event) override;
Changyeon Jo2400b692019-07-18 21:32:48 -070088
89 // Local implementation details
90 bool copyBufferContents(const BufferDesc_1_0& tgtBuffer, const BufferDesc_1_1& srcBuffer);
Changyeon Joc6fa0ab2019-10-12 05:25:44 -070091 const char *eventToString(const EvsEventType aType);
Changyeon Jo2400b692019-07-18 21:32:48 -070092
93 // Values initialized as startup
94 android::sp <IEvsCamera> mCamera;
95 CameraDesc mCameraInfo;
96 android::sp <IEvsDisplay> mDisplay;
97 BufferControlFlag mReturnMode;
98
99 // Since we get frames delivered to us asynchronously via the IEvsCameraStream interface,
100 // we need to protect all member variables that may be modified while we're streaming
101 // (ie: those below)
102 std::mutex mLock;
Changyeon Jod2a82462019-07-30 11:57:17 -0700103 std::condition_variable mEventSignal;
104 std::condition_variable mFrameSignal;
Changyeon Jo2400b692019-07-18 21:32:48 -0700105
106 std::queue<BufferDesc_1_1> mHeldBuffers;
107 bool mRunning = false;
108 unsigned mFramesReceived = 0; // Simple counter -- rolls over eventually!
109 unsigned mFramesDisplayed = 0; // Simple counter -- rolls over eventually!
110 unsigned mFrameWidth = 0;
111 unsigned mFrameHeight = 0;
Changyeon Joc6fa0ab2019-10-12 05:25:44 -0700112 EvsEvent mLatestEventDesc;
Changyeon Jo2400b692019-07-18 21:32:48 -0700113};
114
115
116#endif //EVS_VTS_FRAMEHANDLER_H