Changyeon Jo | 2400b69 | 2019-07-18 21:32:48 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 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_0::IEvsDisplay; |
| 35 | using ::android::hardware::automotive::evs::V1_0::EvsResult; |
Changyeon Jo | 2400b69 | 2019-07-18 21:32:48 -0700 | [diff] [blame] | 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 | * 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 | */ |
| 48 | class FrameHandler : public IEvsCameraStream { |
| 49 | public: |
| 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 Jo | c6fa0ab | 2019-10-12 05:25:44 -0700 | [diff] [blame^] | 58 | virtual ~FrameHandler() { |
| 59 | if (mCamera != nullptr) { |
| 60 | /* shutdown a camera explicitly */ |
| 61 | shutdown(); |
| 62 | } |
| 63 | } |
| 64 | |
Changyeon Jo | 2400b69 | 2019-07-18 21:32:48 -0700 | [diff] [blame] | 65 | 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 Jo | c6fa0ab | 2019-10-12 05:25:44 -0700 | [diff] [blame^] | 76 | bool waitForEvent(const EvsEventType aTargetEvent, |
| 77 | EvsEvent &eventDesc); |
Changyeon Jo | 2400b69 | 2019-07-18 21:32:48 -0700 | [diff] [blame] | 78 | void getFramesCounters(unsigned* received, unsigned* displayed); |
| 79 | void getFrameDimension(unsigned* width, unsigned* height); |
| 80 | |
| 81 | private: |
Changyeon Jo | c6fa0ab | 2019-10-12 05:25:44 -0700 | [diff] [blame^] | 82 | // Implementation for ::android::hardware::automotive::evs::V1_0::IEvsCameraStream |
Changyeon Jo | 468cc1d | 2019-10-12 05:18:16 -0700 | [diff] [blame] | 83 | Return<void> deliverFrame(const BufferDesc_1_0& buffer) override; |
Changyeon Jo | c6fa0ab | 2019-10-12 05:25:44 -0700 | [diff] [blame^] | 84 | |
| 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 Jo | 2400b69 | 2019-07-18 21:32:48 -0700 | [diff] [blame] | 88 | |
| 89 | // Local implementation details |
| 90 | bool copyBufferContents(const BufferDesc_1_0& tgtBuffer, const BufferDesc_1_1& srcBuffer); |
Changyeon Jo | c6fa0ab | 2019-10-12 05:25:44 -0700 | [diff] [blame^] | 91 | const char *eventToString(const EvsEventType aType); |
Changyeon Jo | 2400b69 | 2019-07-18 21:32:48 -0700 | [diff] [blame] | 92 | |
| 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 Jo | d2a8246 | 2019-07-30 11:57:17 -0700 | [diff] [blame] | 103 | std::condition_variable mEventSignal; |
| 104 | std::condition_variable mFrameSignal; |
Changyeon Jo | 2400b69 | 2019-07-18 21:32:48 -0700 | [diff] [blame] | 105 | |
| 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 Jo | c6fa0ab | 2019-10-12 05:25:44 -0700 | [diff] [blame^] | 112 | EvsEvent mLatestEventDesc; |
Changyeon Jo | 2400b69 | 2019-07-18 21:32:48 -0700 | [diff] [blame] | 113 | }; |
| 114 | |
| 115 | |
| 116 | #endif //EVS_VTS_FRAMEHANDLER_H |