/*----------------------------------------------------------------------------*/ /* Copyright (c) FIRST 2016. All Rights Reserved. */ /* Open Source Software - may be modified and shared by FRC teams. The code */ /* must be accompanied by the FIRST BSD license file in the root directory of */ /* the project. */ /*----------------------------------------------------------------------------*/ #ifndef CAMERASERVER_USBCAMERAIMPL_H_ #define CAMERASERVER_USBCAMERAIMPL_H_ #include #include #ifdef __linux__ #include #endif #include "llvm/raw_ostream.h" #include "llvm/DenseMap.h" #include "llvm/SmallVector.h" #include "llvm/StringMap.h" #include "support/raw_istream.h" #include "SourceImpl.h" namespace cs { class USBCameraImpl : public SourceImpl { public: USBCameraImpl(llvm::StringRef name, llvm::StringRef path); ~USBCameraImpl() override; llvm::StringRef GetDescription( llvm::SmallVectorImpl& buf) const override; // Property functions int GetProperty(llvm::StringRef name) const override; llvm::ArrayRef EnumerateProperties( llvm::SmallVectorImpl& vec) const override; CS_PropertyType GetPropertyType(int property) const override; llvm::StringRef GetPropertyName(int property, llvm::SmallVectorImpl& buf, CS_Status* status) const override; bool GetBooleanProperty(int property, CS_Status* status) const override; void SetBooleanProperty(int property, bool value, CS_Status* status) override; double GetDoubleProperty(int property, CS_Status* status) const override; void SetDoubleProperty(int property, double value, CS_Status* status) override; double GetPropertyMin(int property, CS_Status* status) const override; double GetPropertyMax(int property, CS_Status* status) const override; double GetPropertyStep(int property, CS_Status* status) const override; double GetPropertyDefault(int property, CS_Status* status) const override; llvm::StringRef GetStringProperty(int property, llvm::SmallVectorImpl& buf, CS_Status* status) const override; void SetStringProperty(int property, llvm::StringRef value, CS_Status* status) override; int GetEnumProperty(int property, CS_Status* status) const override; void SetEnumProperty(int property, int value, CS_Status* status) override; std::vector GetEnumPropertyChoices( int property, CS_Status* status) const override; void Stop(); struct PropertyData { PropertyData() = default; #ifdef __linux__ #ifdef VIDIOC_QUERY_EXT_CTRL PropertyData(const struct v4l2_query_ext_ctrl& ctrl); #endif PropertyData(const struct v4l2_queryctrl& ctrl); #endif std::string name; unsigned id; // implementation-level id int type; // implementation type, not CS_PropertyType! CS_PropertyType propType; double minimum; double maximum; double step; double defaultValue; }; private: mutable llvm::DenseMap m_property_data; mutable llvm::StringMap m_properties; mutable std::atomic_bool m_properties_cached{false}; void CacheProperty(PropertyData&& prop) const; void CacheProperties() const; bool GetPropertyTypeValueFd(int property, CS_PropertyType propType, unsigned* id, int* type, int* fd, CS_Status* status) const; void CameraThreadMain(); std::string m_path; std::string m_description; std::atomic_int m_fd; std::atomic_bool m_active; // set to false to terminate threads std::thread m_cameraThread; mutable std::mutex m_mutex; }; } // namespace cs #endif // CAMERASERVER_USBCAMERAIMPL_H_