2016-09-05 12:00:04 -07:00
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
/* 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. */
|
|
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
|
2016-11-05 22:11:55 -07:00
|
|
|
#ifndef CS_SOURCEIMPL_H_
|
|
|
|
|
#define CS_SOURCEIMPL_H_
|
2016-09-05 12:00:04 -07:00
|
|
|
|
2016-12-04 09:50:01 -08:00
|
|
|
#include <atomic>
|
2016-09-05 12:00:04 -07:00
|
|
|
#include <condition_variable>
|
|
|
|
|
#include <cstddef>
|
|
|
|
|
#include <memory>
|
|
|
|
|
#include <mutex>
|
|
|
|
|
#include <string>
|
|
|
|
|
#include <vector>
|
|
|
|
|
|
2016-09-10 21:30:39 -07:00
|
|
|
#include "llvm/ArrayRef.h"
|
2016-10-23 18:20:56 -07:00
|
|
|
#include "llvm/StringMap.h"
|
2016-09-05 12:00:04 -07:00
|
|
|
#include "llvm/StringRef.h"
|
2016-12-22 21:19:50 -08:00
|
|
|
|
2016-11-05 22:11:55 -07:00
|
|
|
#include "cscore_cpp.h"
|
2016-09-05 12:00:04 -07:00
|
|
|
#include "Frame.h"
|
2016-12-20 20:48:31 -08:00
|
|
|
#include "Image.h"
|
2016-12-22 21:19:50 -08:00
|
|
|
#include "PropertyImpl.h"
|
2016-09-05 12:00:04 -07:00
|
|
|
|
|
|
|
|
namespace cs {
|
|
|
|
|
|
|
|
|
|
class SourceImpl {
|
|
|
|
|
friend class Frame;
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
SourceImpl(llvm::StringRef name);
|
|
|
|
|
virtual ~SourceImpl();
|
2016-10-23 18:20:56 -07:00
|
|
|
SourceImpl(const SourceImpl& oth) = delete;
|
|
|
|
|
SourceImpl& operator=(const SourceImpl& oth) = delete;
|
2016-09-05 12:00:04 -07:00
|
|
|
|
|
|
|
|
llvm::StringRef GetName() const { return m_name; }
|
2016-10-23 18:20:56 -07:00
|
|
|
|
|
|
|
|
void SetDescription(llvm::StringRef description);
|
|
|
|
|
llvm::StringRef GetDescription(llvm::SmallVectorImpl<char>& buf) const;
|
|
|
|
|
|
2016-12-04 09:50:01 -08:00
|
|
|
void SetConnected(bool connected);
|
|
|
|
|
bool IsConnected() const { return m_connected; }
|
2016-09-05 12:00:04 -07:00
|
|
|
|
2016-09-08 23:52:23 -07:00
|
|
|
// Functions to keep track of the overall number of sinks connected to this
|
|
|
|
|
// source. Primarily used by sinks to determine if other sinks are using
|
|
|
|
|
// the same source.
|
|
|
|
|
int GetNumSinks() const { return m_numSinks; }
|
2016-10-13 00:16:24 -07:00
|
|
|
void AddSink() {
|
|
|
|
|
++m_numSinks;
|
|
|
|
|
NumSinksChanged();
|
|
|
|
|
}
|
|
|
|
|
void RemoveSink() {
|
|
|
|
|
--m_numSinks;
|
|
|
|
|
NumSinksChanged();
|
|
|
|
|
}
|
2016-09-08 23:52:23 -07:00
|
|
|
|
|
|
|
|
// Functions to keep track of the number of sinks connected to this source
|
|
|
|
|
// that are "enabled", in other words, listening for new images. Primarily
|
|
|
|
|
// used by sources to determine whether they should actually bother trying
|
|
|
|
|
// to get source frames.
|
2016-10-13 00:16:24 -07:00
|
|
|
int GetNumSinksEnabled() const { return m_numSinksEnabled; }
|
2016-09-08 23:52:23 -07:00
|
|
|
|
|
|
|
|
void EnableSink() {
|
|
|
|
|
++m_numSinksEnabled;
|
2016-10-13 00:16:24 -07:00
|
|
|
NumSinksEnabledChanged();
|
2016-09-08 23:52:23 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DisableSink() {
|
|
|
|
|
--m_numSinksEnabled;
|
2016-10-13 00:16:24 -07:00
|
|
|
NumSinksEnabledChanged();
|
2016-09-08 23:52:23 -07:00
|
|
|
}
|
|
|
|
|
|
2016-10-22 22:09:47 -07:00
|
|
|
// Gets the current frame time (without waiting for a new one).
|
|
|
|
|
uint64_t GetCurFrameTime();
|
|
|
|
|
|
2016-09-05 12:00:04 -07:00
|
|
|
// Gets the current frame (without waiting for a new one).
|
|
|
|
|
Frame GetCurFrame();
|
|
|
|
|
|
|
|
|
|
// Blocking function that waits for the next frame and returns it.
|
|
|
|
|
Frame GetNextFrame();
|
|
|
|
|
|
2017-02-17 01:17:12 -08:00
|
|
|
// Blocking function that waits for the next frame and returns it (with
|
|
|
|
|
// timeout in seconds). If timeout expires, returns empty frame.
|
|
|
|
|
Frame GetNextFrame(double timeout);
|
|
|
|
|
|
2016-09-05 12:00:04 -07:00
|
|
|
// Force a wakeup of all GetNextFrame() callers by sending an empty frame.
|
|
|
|
|
void Wakeup();
|
|
|
|
|
|
|
|
|
|
// Property functions
|
2016-10-23 18:20:56 -07:00
|
|
|
int GetPropertyIndex(llvm::StringRef name) const;
|
|
|
|
|
llvm::ArrayRef<int> EnumerateProperties(llvm::SmallVectorImpl<int>& vec,
|
|
|
|
|
CS_Status* status) const;
|
2016-11-15 23:54:50 -08:00
|
|
|
CS_PropertyKind GetPropertyKind(int property) const;
|
2016-10-23 18:20:56 -07:00
|
|
|
llvm::StringRef GetPropertyName(int property,
|
|
|
|
|
llvm::SmallVectorImpl<char>& buf,
|
|
|
|
|
CS_Status* status) const;
|
|
|
|
|
int GetProperty(int property, CS_Status* status) const;
|
2016-09-20 22:17:12 -07:00
|
|
|
virtual void SetProperty(int property, int value, CS_Status* status) = 0;
|
2016-10-23 18:20:56 -07:00
|
|
|
int GetPropertyMin(int property, CS_Status* status) const;
|
|
|
|
|
int GetPropertyMax(int property, CS_Status* status) const;
|
|
|
|
|
int GetPropertyStep(int property, CS_Status* status) const;
|
|
|
|
|
int GetPropertyDefault(int property, CS_Status* status) const;
|
|
|
|
|
llvm::StringRef GetStringProperty(int property,
|
|
|
|
|
llvm::SmallVectorImpl<char>& buf,
|
|
|
|
|
CS_Status* status) const;
|
2016-09-19 23:50:47 -07:00
|
|
|
virtual void SetStringProperty(int property, llvm::StringRef value,
|
|
|
|
|
CS_Status* status) = 0;
|
2016-10-23 18:20:56 -07:00
|
|
|
std::vector<std::string> GetEnumPropertyChoices(int property,
|
|
|
|
|
CS_Status* status) const;
|
2016-09-05 12:00:04 -07:00
|
|
|
|
2017-01-01 14:24:13 -08:00
|
|
|
// Standard common camera properties
|
|
|
|
|
virtual void SetBrightness(int brightness, CS_Status* status) = 0;
|
|
|
|
|
virtual int GetBrightness(CS_Status* status) const = 0;
|
|
|
|
|
virtual void SetWhiteBalanceAuto(CS_Status* status) = 0;
|
|
|
|
|
virtual void SetWhiteBalanceHoldCurrent(CS_Status* status) = 0;
|
|
|
|
|
virtual void SetWhiteBalanceManual(int value, CS_Status* status) = 0;
|
|
|
|
|
virtual void SetExposureAuto(CS_Status* status) = 0;
|
|
|
|
|
virtual void SetExposureHoldCurrent(CS_Status* status) = 0;
|
|
|
|
|
virtual void SetExposureManual(int value, CS_Status* status) = 0;
|
|
|
|
|
|
2016-09-29 00:04:16 -07:00
|
|
|
// Video mode functions
|
2016-10-23 18:20:56 -07:00
|
|
|
VideoMode GetVideoMode(CS_Status* status) const;
|
2016-09-29 00:04:16 -07:00
|
|
|
virtual bool SetVideoMode(const VideoMode& mode, CS_Status* status) = 0;
|
|
|
|
|
|
|
|
|
|
// These have default implementations but can be overridden for custom
|
|
|
|
|
// or optimized behavior.
|
|
|
|
|
virtual bool SetPixelFormat(VideoMode::PixelFormat pixelFormat,
|
|
|
|
|
CS_Status* status);
|
|
|
|
|
virtual bool SetResolution(int width, int height, CS_Status* status);
|
|
|
|
|
virtual bool SetFPS(int fps, CS_Status* status);
|
|
|
|
|
|
2016-10-23 18:20:56 -07:00
|
|
|
std::vector<VideoMode> EnumerateVideoModes(CS_Status* status) const;
|
2016-10-13 00:16:24 -07:00
|
|
|
|
2016-12-20 20:48:31 -08:00
|
|
|
std::unique_ptr<Image> AllocImage(VideoMode::PixelFormat pixelFormat,
|
|
|
|
|
int width, int height, std::size_t size);
|
2016-11-12 01:58:59 -08:00
|
|
|
|
2016-09-05 12:00:04 -07:00
|
|
|
protected:
|
2016-11-10 00:00:20 -08:00
|
|
|
void PutFrame(VideoMode::PixelFormat pixelFormat, int width, int height,
|
|
|
|
|
llvm::StringRef data, Frame::Time time);
|
2016-12-20 20:48:31 -08:00
|
|
|
void PutFrame(std::unique_ptr<Image> image, Frame::Time time);
|
|
|
|
|
void PutError(llvm::StringRef msg, Frame::Time time);
|
2016-10-13 00:16:24 -07:00
|
|
|
|
|
|
|
|
// Notification functions for corresponding atomics
|
|
|
|
|
virtual void NumSinksChanged() = 0;
|
|
|
|
|
virtual void NumSinksEnabledChanged() = 0;
|
2016-09-05 12:00:04 -07:00
|
|
|
|
2016-09-08 23:52:23 -07:00
|
|
|
std::atomic_int m_numSinks{0};
|
2016-10-13 00:16:24 -07:00
|
|
|
std::atomic_int m_numSinksEnabled{0};
|
2016-09-05 12:00:04 -07:00
|
|
|
|
2016-10-23 18:20:56 -07:00
|
|
|
protected:
|
|
|
|
|
// Get a property; must be called with m_mutex held.
|
2016-12-22 21:19:50 -08:00
|
|
|
PropertyImpl* GetProperty(int property) {
|
2016-10-23 18:20:56 -07:00
|
|
|
if (property <= 0 || static_cast<size_t>(property) > m_propertyData.size())
|
|
|
|
|
return nullptr;
|
|
|
|
|
return m_propertyData[property - 1].get();
|
|
|
|
|
}
|
2016-12-22 21:19:50 -08:00
|
|
|
const PropertyImpl* GetProperty(int property) const {
|
2016-10-23 18:20:56 -07:00
|
|
|
if (property <= 0 || static_cast<size_t>(property) > m_propertyData.size())
|
|
|
|
|
return nullptr;
|
|
|
|
|
return m_propertyData[property - 1].get();
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-22 20:51:04 -08:00
|
|
|
// Create an "empty" property. This is called by GetPropertyIndex to create
|
|
|
|
|
// properties that don't exist (as GetPropertyIndex can't fail).
|
|
|
|
|
// Note: called with m_mutex held.
|
2016-12-22 21:19:50 -08:00
|
|
|
virtual std::unique_ptr<PropertyImpl> CreateEmptyProperty(
|
2016-12-22 20:51:04 -08:00
|
|
|
llvm::StringRef name) const = 0;
|
|
|
|
|
|
2016-10-23 18:20:56 -07:00
|
|
|
// Cache properties. Implementations must return false and set status to
|
|
|
|
|
// CS_SOURCE_IS_DISCONNECTED if not possible to cache.
|
|
|
|
|
virtual bool CacheProperties(CS_Status* status) const = 0;
|
|
|
|
|
|
2016-12-22 21:19:50 -08:00
|
|
|
void NotifyPropertyCreated(int propIndex, PropertyImpl& prop);
|
|
|
|
|
|
|
|
|
|
// Update property value; must be called with m_mutex held.
|
|
|
|
|
void UpdatePropertyValue(int property, bool setString, int value,
|
|
|
|
|
llvm::StringRef valueStr);
|
|
|
|
|
|
2016-10-23 18:20:56 -07:00
|
|
|
// Cached properties and video modes (protected with m_mutex)
|
2016-12-22 21:19:50 -08:00
|
|
|
mutable std::vector<std::unique_ptr<PropertyImpl>> m_propertyData;
|
2016-10-23 18:20:56 -07:00
|
|
|
mutable llvm::StringMap<int> m_properties;
|
|
|
|
|
mutable std::vector<VideoMode> m_videoModes;
|
|
|
|
|
// Current video mode
|
|
|
|
|
mutable VideoMode m_mode;
|
|
|
|
|
// Whether CacheProperties() has been successful at least once (and thus
|
|
|
|
|
// should not be called again)
|
|
|
|
|
mutable std::atomic_bool m_properties_cached{false};
|
|
|
|
|
|
|
|
|
|
mutable std::mutex m_mutex;
|
|
|
|
|
|
2016-09-05 12:00:04 -07:00
|
|
|
private:
|
2016-12-20 20:48:31 -08:00
|
|
|
void ReleaseImage(std::unique_ptr<Image> image);
|
|
|
|
|
std::unique_ptr<Frame::Impl> AllocFrameImpl();
|
|
|
|
|
void ReleaseFrameImpl(std::unique_ptr<Frame::Impl> data);
|
2016-09-05 12:00:04 -07:00
|
|
|
|
|
|
|
|
std::string m_name;
|
2016-10-23 18:20:56 -07:00
|
|
|
std::string m_description;
|
2016-09-08 23:52:23 -07:00
|
|
|
|
2016-09-05 12:00:04 -07:00
|
|
|
std::mutex m_frameMutex;
|
2016-09-08 23:52:23 -07:00
|
|
|
std::condition_variable m_frameCv;
|
2016-09-05 12:00:04 -07:00
|
|
|
|
2016-10-13 00:16:24 -07:00
|
|
|
bool m_destroyFrames{false};
|
2016-09-05 12:00:04 -07:00
|
|
|
|
2016-12-20 20:48:31 -08:00
|
|
|
// Pool of frames/images to reduce malloc traffic.
|
2016-10-23 18:20:56 -07:00
|
|
|
std::mutex m_poolMutex;
|
2016-12-20 20:48:31 -08:00
|
|
|
std::vector<std::unique_ptr<Frame::Impl>> m_framesAvail;
|
|
|
|
|
std::vector<std::unique_ptr<Image>> m_imagesAvail;
|
2016-12-04 09:50:01 -08:00
|
|
|
|
|
|
|
|
std::atomic_bool m_connected{false};
|
2017-08-14 22:27:28 -07:00
|
|
|
|
|
|
|
|
// Most recent frame (returned to callers of GetNextFrame)
|
|
|
|
|
// Access protected by m_frameMutex.
|
|
|
|
|
// MUST be located below m_poolMutex as the Frame destructor calls back
|
|
|
|
|
// into SourceImpl::ReleaseImage, which locks m_poolMutex.
|
|
|
|
|
Frame m_frame;
|
2016-09-05 12:00:04 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // namespace cs
|
|
|
|
|
|
2016-11-05 22:11:55 -07:00
|
|
|
#endif // CS_SOURCEIMPL_H_
|