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 HARDWARE_INTERFACES_CAMERA_PROVIDER_DEFAULT_EXTERNALCAMERAPROVIDER_H_ 18 #define HARDWARE_INTERFACES_CAMERA_PROVIDER_DEFAULT_EXTERNALCAMERAPROVIDER_H_ 19 20 #include <ExternalCameraUtils.h> 21 #include <SimpleThread.h> 22 #include <aidl/android/hardware/camera/common/CameraDeviceStatus.h> 23 #include <aidl/android/hardware/camera/common/VendorTagSection.h> 24 #include <aidl/android/hardware/camera/device/ICameraDevice.h> 25 #include <aidl/android/hardware/camera/provider/BnCameraProvider.h> 26 #include <aidl/android/hardware/camera/provider/CameraIdAndStreamCombination.h> 27 #include <aidl/android/hardware/camera/provider/ConcurrentCameraIdCombination.h> 28 #include <aidl/android/hardware/camera/provider/ICameraProviderCallback.h> 29 #include <poll.h> 30 #include <utils/Mutex.h> 31 #include <utils/Thread.h> 32 #include <thread> 33 #include <unordered_map> 34 #include <unordered_set> 35 36 namespace android { 37 namespace hardware { 38 namespace camera { 39 namespace provider { 40 namespace implementation { 41 42 using ::aidl::android::hardware::camera::common::CameraDeviceStatus; 43 using ::aidl::android::hardware::camera::common::VendorTagSection; 44 using ::aidl::android::hardware::camera::device::ICameraDevice; 45 using ::aidl::android::hardware::camera::provider::BnCameraProvider; 46 using ::aidl::android::hardware::camera::provider::CameraIdAndStreamCombination; 47 using ::aidl::android::hardware::camera::provider::ConcurrentCameraIdCombination; 48 using ::aidl::android::hardware::camera::provider::ICameraProviderCallback; 49 using ::android::hardware::camera::common::helper::SimpleThread; 50 using ::android::hardware::camera::external::common::ExternalCameraConfig; 51 52 class ExternalCameraProvider : public BnCameraProvider { 53 public: 54 ExternalCameraProvider(); 55 ~ExternalCameraProvider() override; 56 ndk::ScopedAStatus setCallback( 57 const std::shared_ptr<ICameraProviderCallback>& in_callback) override; 58 ndk::ScopedAStatus getVendorTags(std::vector<VendorTagSection>* _aidl_return) override; 59 ndk::ScopedAStatus getCameraIdList(std::vector<std::string>* _aidl_return) override; 60 ndk::ScopedAStatus getCameraDeviceInterface( 61 const std::string& in_cameraDeviceName, 62 std::shared_ptr<ICameraDevice>* _aidl_return) override; 63 ndk::ScopedAStatus notifyDeviceStateChange(int64_t in_deviceState) override; 64 ndk::ScopedAStatus getConcurrentCameraIds( 65 std::vector<ConcurrentCameraIdCombination>* _aidl_return) override; 66 ndk::ScopedAStatus isConcurrentStreamCombinationSupported( 67 const std::vector<CameraIdAndStreamCombination>& in_configs, 68 bool* _aidl_return) override; 69 70 private: 71 void addExternalCamera(const char* devName); 72 void deviceAdded(const char* devName); 73 void deviceRemoved(const char* devName); 74 void updateAttachedCameras(); 75 76 // A separate thread to monitor '/dev' directory for '/dev/video*' entries 77 // This thread calls back into ExternalCameraProvider when an actionable change is detected. 78 class HotplugThread : public SimpleThread { 79 public: 80 explicit HotplugThread(ExternalCameraProvider* parent); 81 ~HotplugThread() override; 82 83 protected: 84 bool threadLoop() override; 85 86 private: 87 // Returns true if thread initialization succeeded, and false if thread initialization 88 // failed. 89 bool initialize(); 90 91 ExternalCameraProvider* mParent = nullptr; 92 const std::unordered_set<std::string> mInternalDevices; 93 94 bool mIsInitialized = false; 95 96 int mINotifyFD = -1; 97 int mWd = -1; 98 99 // struct to wrap mINotifyFD and poll it with timeout 100 struct pollfd mPollFd = {}; 101 char mEventBuf[512] = {0}; 102 }; 103 104 Mutex mLock; 105 std::shared_ptr<ICameraProviderCallback> mCallback = nullptr; 106 std::unordered_map<std::string, CameraDeviceStatus> mCameraStatusMap; // camera id -> status 107 const ExternalCameraConfig mCfg; 108 std::shared_ptr<HotplugThread> mHotPlugThread; 109 }; 110 111 } // namespace implementation 112 } // namespace provider 113 } // namespace camera 114 } // namespace hardware 115 } // namespace android 116 117 #endif // HARDWARE_INTERFACES_CAMERA_PROVIDER_DEFAULT_EXTERNALCAMERAPROVIDER_H_ 118