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 | 468cc1d | 2019-10-12 05:18:16 -0700 | [diff] [blame^] | 36 | using ::android::hardware::automotive::evs::V1_0::CameraDesc; |
Changyeon Jo | 2400b69 | 2019-07-18 21:32:48 -0700 | [diff] [blame] | 37 | using BufferDesc_1_0 = ::android::hardware::automotive::evs::V1_0::BufferDesc; |
| 38 | using 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 | */ |
| 49 | class FrameHandler : public IEvsCameraStream { |
| 50 | public: |
| 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); |
Changyeon Jo | 468cc1d | 2019-10-12 05:18:16 -0700 | [diff] [blame^] | 70 | bool waitForEvent(const InfoEventType aTargetEvent, |
| 71 | InfoEventDesc &eventDesc); |
Changyeon Jo | 2400b69 | 2019-07-18 21:32:48 -0700 | [diff] [blame] | 72 | void getFramesCounters(unsigned* received, unsigned* displayed); |
| 73 | void getFrameDimension(unsigned* width, unsigned* height); |
| 74 | |
| 75 | private: |
Changyeon Jo | 47b45af | 2019-10-11 10:17:33 -0700 | [diff] [blame] | 76 | // Implementation for ::android::hardware::automotive::evs::V1_1::IEvsCameraStream |
Changyeon Jo | 468cc1d | 2019-10-12 05:18:16 -0700 | [diff] [blame^] | 77 | Return<void> deliverFrame(const BufferDesc_1_0& buffer) override; |
| 78 | Return<void> notifyEvent(const EvsEvent& event) override; |
Changyeon Jo | 2400b69 | 2019-07-18 21:32:48 -0700 | [diff] [blame] | 79 | |
| 80 | // Local implementation details |
| 81 | bool copyBufferContents(const BufferDesc_1_0& tgtBuffer, const BufferDesc_1_1& srcBuffer); |
Changyeon Jo | 468cc1d | 2019-10-12 05:18:16 -0700 | [diff] [blame^] | 82 | const char *eventToString(const InfoEventType aType); |
Changyeon Jo | 2400b69 | 2019-07-18 21:32:48 -0700 | [diff] [blame] | 83 | |
| 84 | // Values initialized as startup |
| 85 | android::sp <IEvsCamera> mCamera; |
| 86 | CameraDesc mCameraInfo; |
| 87 | android::sp <IEvsDisplay> mDisplay; |
| 88 | BufferControlFlag mReturnMode; |
| 89 | |
| 90 | // Since we get frames delivered to us asynchronously via the IEvsCameraStream interface, |
| 91 | // we need to protect all member variables that may be modified while we're streaming |
| 92 | // (ie: those below) |
| 93 | std::mutex mLock; |
Changyeon Jo | d2a8246 | 2019-07-30 11:57:17 -0700 | [diff] [blame] | 94 | std::condition_variable mEventSignal; |
| 95 | std::condition_variable mFrameSignal; |
Changyeon Jo | 2400b69 | 2019-07-18 21:32:48 -0700 | [diff] [blame] | 96 | |
| 97 | std::queue<BufferDesc_1_1> mHeldBuffers; |
| 98 | bool mRunning = false; |
| 99 | unsigned mFramesReceived = 0; // Simple counter -- rolls over eventually! |
| 100 | unsigned mFramesDisplayed = 0; // Simple counter -- rolls over eventually! |
| 101 | unsigned mFrameWidth = 0; |
| 102 | unsigned mFrameHeight = 0; |
Changyeon Jo | 468cc1d | 2019-10-12 05:18:16 -0700 | [diff] [blame^] | 103 | InfoEventDesc mLatestEventDesc; |
Changyeon Jo | 2400b69 | 2019-07-18 21:32:48 -0700 | [diff] [blame] | 104 | }; |
| 105 | |
| 106 | |
| 107 | #endif //EVS_VTS_FRAMEHANDLER_H |