1 /* 2 * Copyright (C) 2022 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 AUTOMOTIVE_EVS_VTS_FRAMEHANDLER_H 18 #define AUTOMOTIVE_EVS_VTS_FRAMEHANDLER_H 19 20 #include <aidl/android/hardware/automotive/evs/BnEvsCameraStream.h> 21 #include <aidl/android/hardware/automotive/evs/EvsEventDesc.h> 22 #include <aidl/android/hardware/automotive/evs/IEvsCamera.h> 23 #include <aidl/android/hardware/automotive/evs/IEvsDisplay.h> 24 25 #include <mutex> 26 #include <queue> 27 28 /* 29 * FrameHandler: 30 * This class can be used to receive camera imagery from an IEvsCamera implementation. Given an 31 * IEvsDisplay instance at startup, it will forward the received imagery to the display, 32 * providing a trivial implementation of a rear vew camera type application. 33 * Note that the video frames are delivered on a background thread, while the control interface 34 * is actuated from the applications foreground thread. 35 */ 36 class FrameHandler : public ::aidl::android::hardware::automotive::evs::BnEvsCameraStream { 37 public: 38 enum BufferControlFlag { 39 eAutoReturn, 40 eNoAutoReturn, 41 }; 42 43 FrameHandler( 44 const std::shared_ptr<::aidl::android::hardware::automotive::evs::IEvsCamera>& pCamera, 45 const ::aidl::android::hardware::automotive::evs::CameraDesc& cameraInfo, 46 const std::shared_ptr<::aidl::android::hardware::automotive::evs::IEvsDisplay>& 47 pDisplay, 48 BufferControlFlag mode = eAutoReturn); ~FrameHandler()49 virtual ~FrameHandler() { 50 if (mCamera != nullptr) { 51 /* shutdown a camera explicitly */ 52 shutdown(); 53 } 54 } 55 56 void shutdown(); 57 bool startStream(); 58 void asyncStopStream(); 59 void blockingStopStream(); 60 bool returnHeldBuffer(); 61 bool isRunning(); 62 void waitForFrameCount(unsigned frameCount); 63 bool waitForEvent(const ::aidl::android::hardware::automotive::evs::EvsEventDesc& aTargetEvent, 64 ::aidl::android::hardware::automotive::evs::EvsEventDesc& aReceivedEvent, 65 bool ignorePayload = false); 66 void getFramesCounters(unsigned* received, unsigned* displayed); 67 void getFrameDimension(unsigned* width, unsigned* height); 68 69 private: 70 // Methods from ::aidl::android::hardware::automotive::evs::IEvsCameraStream follow. 71 ::ndk::ScopedAStatus deliverFrame( 72 const std::vector<::aidl::android::hardware::automotive::evs::BufferDesc>& buffer) 73 override; 74 ::ndk::ScopedAStatus notify( 75 const ::aidl::android::hardware::automotive::evs::EvsEventDesc& event) override; 76 77 // Local implementation details 78 bool copyBufferContents( 79 const ::aidl::android::hardware::automotive::evs::BufferDesc& tgtBuffer, 80 const ::aidl::android::hardware::automotive::evs::BufferDesc& srcBuffer); 81 const char* eventToString(const ::aidl::android::hardware::automotive::evs::EvsEventType aType); 82 83 std::shared_ptr<::aidl::android::hardware::automotive::evs::IEvsCamera> mCamera; 84 ::aidl::android::hardware::automotive::evs::CameraDesc mCameraInfo; 85 std::shared_ptr<::aidl::android::hardware::automotive::evs::IEvsDisplay> mDisplay; 86 BufferControlFlag mReturnMode; 87 88 // Since we get frames delivered to us asynchronously via the IEvsCameraStream interface, 89 // we need to protect all member variables that may be modified while we're streaming 90 // (ie: those below) 91 std::mutex mLock; 92 std::mutex mEventLock; 93 std::condition_variable mEventSignal; 94 std::condition_variable mFrameSignal; 95 std::queue<std::vector<::aidl::android::hardware::automotive::evs::BufferDesc>> mHeldBuffers; 96 97 bool mRunning = false; 98 unsigned mFramesReceived = 0; // Simple counter -- rolls over eventually! 99 unsigned mFramesDisplayed = 0; // Simple counter -- rolls over eventually! 100 unsigned mFrameWidth = 0; 101 unsigned mFrameHeight = 0; 102 ::aidl::android::hardware::automotive::evs::EvsEventDesc mLatestEventDesc; 103 }; 104 105 #endif // AUTOMOTIVE_EVS_VTS_FRAMEHANDLER_H 106