1 /* 2 * Copyright (C) 2021 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 #ifndef SERVICE_JNI_EVS_STREAMHANDLER_H_ 17 #define SERVICE_JNI_EVS_STREAMHANDLER_H_ 18 19 #include "EvsServiceCallback.h" 20 21 #include <aidl/android/hardware/automotive/evs/BnEvsCameraStream.h> 22 #include <aidl/android/hardware/automotive/evs/BufferDesc.h> 23 #include <aidl/android/hardware/automotive/evs/EvsEventDesc.h> 24 #include <aidl/android/hardware/automotive/evs/IEvsCamera.h> 25 #include <aidl/android/hardware/graphics/common/HardwareBuffer.h> 26 #include <android-base/thread_annotations.h> 27 #include <android/binder_auto_utils.h> 28 29 #include <list> 30 31 namespace android::automotive::evs { 32 33 /* 34 * StreamHandler: 35 * This class can be used to receive camera imagery from an IEvsCamera implementation. It will 36 * hold onto the most recent image buffer, returning older ones. 37 * Note that the video frames are delivered on a background thread, while the control interface 38 * is actuated from the applications foreground thread. 39 */ 40 class StreamHandler final : public ::aidl::android::hardware::automotive::evs::BnEvsCameraStream { 41 public: 42 StreamHandler( 43 const std::shared_ptr<::aidl::android::hardware::automotive::evs::IEvsCamera>& camObj, 44 EvsServiceCallback* callback, int maxNumFramesInFlight); 45 virtual ~StreamHandler(); 46 void shutdown(); 47 bool startStream() EXCLUDES(mLock); 48 void blockingStopStream() EXCLUDES(mLock); 49 bool isRunning() EXCLUDES(mLock); 50 void doneWithFrame(const ::aidl::android::hardware::automotive::evs::BufferDesc& buffer) 51 EXCLUDES(mLock); 52 void doneWithFrame(int bufferId) EXCLUDES(mLock); 53 54 private: 55 // Implementation for ::aidl::android::hardware::automotive::evs::IEvsCameraStream 56 ::ndk::ScopedAStatus deliverFrame( 57 const std::vector<::aidl::android::hardware::automotive::evs::BufferDesc>& buffer) 58 override EXCLUDES(mLock); 59 ::ndk::ScopedAStatus notify( 60 const ::aidl::android::hardware::automotive::evs::EvsEventDesc& event) override 61 EXCLUDES(mLock); 62 63 // Values initialized as startup 64 std::shared_ptr<::aidl::android::hardware::automotive::evs::IEvsCamera> mEvsCamera; 65 66 // Since we get frames delivered to us asnchronously via the ICarCameraStream interface, 67 // we need to protect all member variables that may be modified while we're streaming 68 // (ie: those below) 69 std::mutex mLock; 70 std::condition_variable mCondition; 71 bool mRunning GUARDED_BY(mLock) = false; 72 73 // Callbacks to forward EVS events and frames 74 EvsServiceCallback* mCallback; 75 76 std::list<::aidl::android::hardware::automotive::evs::BufferDesc> mReceivedBuffers 77 GUARDED_BY(mLock); 78 int mMaxNumFramesInFlightPerClient; 79 80 // Track number of active streaming clients. 81 int mNumClients GUARDED_BY(mLock); 82 }; 83 84 } // namespace android::automotive::evs 85 86 #endif // SERVICE_JNI_EVS_STREAMHANDLER_H_ 87