blob: d5c3f6bfde4d3a74ddf68b73d76108999e76c178 [file] [log] [blame]
Scott Randolph6c085582017-03-30 14:24:14 -07001/*
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
26using namespace ::android::hardware::automotive::evs::V1_0;
27using ::android::hardware::Return;
28using ::android::hardware::Void;
29using ::android::hardware::hidl_vec;
30using ::android::hardware::hidl_handle;
31using ::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 */
42class FrameHandler : public IEvsCameraStream {
43public:
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
65private:
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 void registerBufferHelper(const BufferDesc& buffer);
72 void unregisterBufferHelper(const BufferDesc& buffer);
73
74 // Values initialized as startup
75 android::sp <IEvsCamera> mCamera;
76 CameraDesc mCameraInfo;
77 android::sp <IEvsDisplay> mDisplay;
78 BufferControlFlag mReturnMode;
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 std::queue<BufferDesc> mHeldBuffers;
87 bool mRunning = false;
88 unsigned mFramesReceived = 0; // Simple counter -- rolls over eventually!
89 unsigned mFramesDisplayed = 0; // Simple counter -- rolls over eventually!
90};
91
92
93#endif //EVS_VTS_FRAMEHANDLER_H