Prepare cscore for merge into allwpilib.

This commit is contained in:
Peter Johnson
2017-12-20 19:28:58 -08:00
parent 6d3d52f923
commit ea73c10cd8
166 changed files with 0 additions and 335 deletions

View File

@@ -0,0 +1,20 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2016-2018 FIRST. 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 CSCORE_CSCORE_H_
#define CSCORE_CSCORE_H_
/* C API */
#include "cscore_c.h"
#ifdef __cplusplus
/* C++ API */
#include "cscore_cpp.h"
#include "cscore_oo.h"
#endif /* __cplusplus */
#endif // CSCORE_CSCORE_H_

View File

@@ -0,0 +1,403 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2016-2018 FIRST. 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 CSCORE_CSCORE_C_H_
#define CSCORE_CSCORE_C_H_
#include <stdint.h>
#include <cstddef>
#ifdef __cplusplus
extern "C" {
#endif
struct CvMat;
//
// The C API is handle-based. Sources and sinks are reference counted
// internally to the library. Any time a source or sink handle is returned
// or provided to a callback, the reference count is incremented.
// Calling CS_ReleaseSource() or CS_ReleaseSink() decrements the reference
// count, and when the reference count reaches zero, the object is destroyed.
// Connecting a source to a sink increments the reference count of the source,
// and when the sink is destroyed (its reference count reaches zero), the
// source reference count is decremented.
//
//
// Typedefs
//
typedef int CS_Bool;
typedef int CS_Status;
typedef int CS_Handle;
typedef CS_Handle CS_Property;
typedef CS_Handle CS_Listener;
typedef CS_Handle CS_Sink;
typedef CS_Handle CS_Source;
//
// Status values
//
enum CS_StatusValue {
CS_PROPERTY_WRITE_FAILED = 2000,
CS_OK = 0,
CS_INVALID_HANDLE = -2000, // handle was invalid (does not exist)
CS_WRONG_HANDLE_SUBTYPE = -2001,
CS_INVALID_PROPERTY = -2002,
CS_WRONG_PROPERTY_TYPE = -2003,
CS_READ_FAILED = -2004,
CS_SOURCE_IS_DISCONNECTED = -2005,
CS_EMPTY_VALUE = -2006,
CS_BAD_URL = -2007,
CS_TELEMETRY_NOT_ENABLED = -2008
};
//
// Logging levels
//
enum CS_LogLevel {
CS_LOG_CRITICAL = 50,
CS_LOG_ERROR = 40,
CS_LOG_WARNING = 30,
CS_LOG_INFO = 20,
CS_LOG_DEBUG = 10,
CS_LOG_DEBUG1 = 9,
CS_LOG_DEBUG2 = 8,
CS_LOG_DEBUG3 = 7,
CS_LOG_DEBUG4 = 6
};
//
// Pixel formats
//
enum CS_PixelFormat {
CS_PIXFMT_UNKNOWN = 0,
CS_PIXFMT_MJPEG,
CS_PIXFMT_YUYV,
CS_PIXFMT_RGB565,
CS_PIXFMT_BGR,
CS_PIXFMT_GRAY
};
//
// Frame formats
//
typedef struct CS_VideoMode {
int pixelFormat;
int width;
int height;
int fps;
} CS_VideoMode;
//
// Property kinds
//
enum CS_PropertyKind {
CS_PROP_NONE = 0,
CS_PROP_BOOLEAN = 1,
CS_PROP_INTEGER = 2,
CS_PROP_STRING = 4,
CS_PROP_ENUM = 8
};
//
// Source kinds
//
enum CS_SourceKind {
CS_SOURCE_UNKNOWN = 0,
CS_SOURCE_USB = 1,
CS_SOURCE_HTTP = 2,
CS_SOURCE_CV = 4
};
//
// HTTP Camera kinds
//
enum CS_HttpCameraKind {
CS_HTTP_UNKNOWN = 0,
CS_HTTP_MJPGSTREAMER = 1,
CS_HTTP_CSCORE = 2,
CS_HTTP_AXIS = 3
};
//
// Sink kinds
//
enum CS_SinkKind { CS_SINK_UNKNOWN = 0, CS_SINK_MJPEG = 2, CS_SINK_CV = 4 };
//
// Listener event kinds
//
enum CS_EventKind {
CS_SOURCE_CREATED = 0x0001,
CS_SOURCE_DESTROYED = 0x0002,
CS_SOURCE_CONNECTED = 0x0004,
CS_SOURCE_DISCONNECTED = 0x0008,
CS_SOURCE_VIDEOMODES_UPDATED = 0x0010,
CS_SOURCE_VIDEOMODE_CHANGED = 0x0020,
CS_SOURCE_PROPERTY_CREATED = 0x0040,
CS_SOURCE_PROPERTY_VALUE_UPDATED = 0x0080,
CS_SOURCE_PROPERTY_CHOICES_UPDATED = 0x0100,
CS_SINK_SOURCE_CHANGED = 0x0200,
CS_SINK_CREATED = 0x0400,
CS_SINK_DESTROYED = 0x0800,
CS_SINK_ENABLED = 0x1000,
CS_SINK_DISABLED = 0x2000,
CS_NETWORK_INTERFACES_CHANGED = 0x4000,
CS_TELEMETRY_UPDATED = 0x8000
};
//
// Telemetry kinds
//
enum CS_TelemetryKind {
CS_SOURCE_BYTES_RECEIVED = 1,
CS_SOURCE_FRAMES_RECEIVED = 2
};
//
// Listener event
//
struct CS_Event {
CS_EventKind kind;
// Valid for CS_SOURCE_* and CS_SINK_* respectively
CS_Source source;
CS_Sink sink;
// Source/sink/property name
const char* name;
// Fields for CS_SOURCE_VIDEOMODE_CHANGED event
CS_VideoMode mode;
// Fields for CS_SOURCE_PROPERTY_* events
CS_Property property;
CS_PropertyKind propertyKind;
int value;
const char* valueStr;
};
//
// Property Functions
//
enum CS_PropertyKind CS_GetPropertyKind(CS_Property property,
CS_Status* status);
char* CS_GetPropertyName(CS_Property property, CS_Status* status);
int CS_GetProperty(CS_Property property, CS_Status* status);
void CS_SetProperty(CS_Property property, int value, CS_Status* status);
int CS_GetPropertyMin(CS_Property property, CS_Status* status);
int CS_GetPropertyMax(CS_Property property, CS_Status* status);
int CS_GetPropertyStep(CS_Property property, CS_Status* status);
int CS_GetPropertyDefault(CS_Property property, CS_Status* status);
char* CS_GetStringProperty(CS_Property property, CS_Status* status);
void CS_SetStringProperty(CS_Property property, const char* value,
CS_Status* status);
char** CS_GetEnumPropertyChoices(CS_Property property, int* count,
CS_Status* status);
//
// Source Creation Functions
//
CS_Source CS_CreateUsbCameraDev(const char* name, int dev, CS_Status* status);
CS_Source CS_CreateUsbCameraPath(const char* name, const char* path,
CS_Status* status);
CS_Source CS_CreateHttpCamera(const char* name, const char* url,
enum CS_HttpCameraKind kind, CS_Status* status);
CS_Source CS_CreateHttpCameraMulti(const char* name, const char** urls,
int count, enum CS_HttpCameraKind kind,
CS_Status* status);
CS_Source CS_CreateCvSource(const char* name, const CS_VideoMode* mode,
CS_Status* status);
//
// Source Functions
//
CS_SourceKind CS_GetSourceKind(CS_Source source, CS_Status* status);
char* CS_GetSourceName(CS_Source source, CS_Status* status);
char* CS_GetSourceDescription(CS_Source source, CS_Status* status);
uint64_t CS_GetSourceLastFrameTime(CS_Source source, CS_Status* status);
CS_Bool CS_IsSourceConnected(CS_Source source, CS_Status* status);
CS_Property CS_GetSourceProperty(CS_Source source, const char* name,
CS_Status* status);
CS_Property* CS_EnumerateSourceProperties(CS_Source source, int* count,
CS_Status* status);
void CS_GetSourceVideoMode(CS_Source source, CS_VideoMode* mode,
CS_Status* status);
CS_Bool CS_SetSourceVideoMode(CS_Source source, const CS_VideoMode* mode,
CS_Status* status);
CS_Bool CS_SetSourceVideoModeDiscrete(CS_Source source,
enum CS_PixelFormat pixelFormat,
int width, int height, int fps,
CS_Status* status);
CS_Bool CS_SetSourcePixelFormat(CS_Source source,
enum CS_PixelFormat pixelFormat,
CS_Status* status);
CS_Bool CS_SetSourceResolution(CS_Source source, int width, int height,
CS_Status* status);
CS_Bool CS_SetSourceFPS(CS_Source source, int fps, CS_Status* status);
CS_VideoMode* CS_EnumerateSourceVideoModes(CS_Source source, int* count,
CS_Status* status);
CS_Sink* CS_EnumerateSourceSinks(CS_Source source, int* count,
CS_Status* status);
CS_Source CS_CopySource(CS_Source source, CS_Status* status);
void CS_ReleaseSource(CS_Source source, CS_Status* status);
//
// Camera Source Common Property Fuctions
//
void CS_SetCameraBrightness(CS_Source source, int brightness,
CS_Status* status);
int CS_GetCameraBrightness(CS_Source source, CS_Status* status);
void CS_SetCameraWhiteBalanceAuto(CS_Source source, CS_Status* status);
void CS_SetCameraWhiteBalanceHoldCurrent(CS_Source source, CS_Status* status);
void CS_SetCameraWhiteBalanceManual(CS_Source source, int value,
CS_Status* status);
void CS_SetCameraExposureAuto(CS_Source source, CS_Status* status);
void CS_SetCameraExposureHoldCurrent(CS_Source source, CS_Status* status);
void CS_SetCameraExposureManual(CS_Source source, int value, CS_Status* status);
//
// UsbCamera Source Functions
//
char* CS_GetUsbCameraPath(CS_Source source, CS_Status* status);
//
// HttpCamera Source Functions
//
CS_HttpCameraKind CS_GetHttpCameraKind(CS_Source source, CS_Status* status);
void CS_SetHttpCameraUrls(CS_Source source, const char** urls, int count,
CS_Status* status);
char** CS_GetHttpCameraUrls(CS_Source source, int* count, CS_Status* status);
//
// OpenCV Source Functions
//
void CS_PutSourceFrame(CS_Source source, struct CvMat* image,
CS_Status* status);
void CS_NotifySourceError(CS_Source source, const char* msg, CS_Status* status);
void CS_SetSourceConnected(CS_Source source, CS_Bool connected,
CS_Status* status);
void CS_SetSourceDescription(CS_Source source, const char* description,
CS_Status* status);
CS_Property CS_CreateSourceProperty(CS_Source source, const char* name,
enum CS_PropertyKind kind, int minimum,
int maximum, int step, int defaultValue,
int value, CS_Status* status);
void CS_SetSourceEnumPropertyChoices(CS_Source source, CS_Property property,
const char** choices, int count,
CS_Status* status);
//
// Sink Creation Functions
//
CS_Sink CS_CreateMjpegServer(const char* name, const char* listenAddress,
int port, CS_Status* status);
CS_Sink CS_CreateCvSink(const char* name, CS_Status* status);
CS_Sink CS_CreateCvSinkCallback(const char* name, void* data,
void (*processFrame)(void* data, uint64_t time),
CS_Status* status);
//
// Sink Functions
//
CS_SinkKind CS_GetSinkKind(CS_Sink sink, CS_Status* status);
char* CS_GetSinkName(CS_Sink sink, CS_Status* status);
char* CS_GetSinkDescription(CS_Sink sink, CS_Status* status);
void CS_SetSinkSource(CS_Sink sink, CS_Source source, CS_Status* status);
CS_Property CS_GetSinkSourceProperty(CS_Sink sink, const char* name,
CS_Status* status);
CS_Source CS_GetSinkSource(CS_Sink sink, CS_Status* status);
CS_Sink CS_CopySink(CS_Sink sink, CS_Status* status);
void CS_ReleaseSink(CS_Sink sink, CS_Status* status);
//
// MjpegServer Sink Functions
//
char* CS_GetMjpegServerListenAddress(CS_Sink sink, CS_Status* status);
int CS_GetMjpegServerPort(CS_Sink sink, CS_Status* status);
//
// OpenCV Sink Functions
//
void CS_SetSinkDescription(CS_Sink sink, const char* description,
CS_Status* status);
uint64_t CS_GrabSinkFrame(CS_Sink sink, struct CvMat* image, CS_Status* status);
uint64_t CS_GrabSinkFrameTimeout(CS_Sink sink, struct CvMat* image,
double timeout, CS_Status* status);
char* CS_GetSinkError(CS_Sink sink, CS_Status* status);
void CS_SetSinkEnabled(CS_Sink sink, CS_Bool enabled, CS_Status* status);
//
// Listener Functions
//
void CS_SetListenerOnStart(void (*onStart)(void* data), void* data);
void CS_SetListenerOnExit(void (*onExit)(void* data), void* data);
CS_Listener CS_AddListener(void* data,
void (*callback)(void* data, const CS_Event* event),
int eventMask, int immediateNotify,
CS_Status* status);
void CS_RemoveListener(CS_Listener handle, CS_Status* status);
int CS_NotifierDestroyed(void);
//
// Telemetry Functions
//
void CS_SetTelemetryPeriod(double seconds);
double CS_GetTelemetryElapsedTime(void);
int64_t CS_GetTelemetryValue(CS_Handle handle, CS_TelemetryKind kind,
CS_Status* status);
double CS_GetTelemetryAverageValue(CS_Handle handle, CS_TelemetryKind kind,
CS_Status* status);
//
// Logging Functions
//
typedef void (*CS_LogFunc)(unsigned int level, const char* file,
unsigned int line, const char* msg);
void CS_SetLogger(CS_LogFunc func, unsigned int min_level);
void CS_SetDefaultLogger(unsigned int min_level);
//
// Utility Functions
//
typedef struct CS_UsbCameraInfo {
int dev;
char* path;
char* name;
} CS_UsbCameraInfo;
CS_UsbCameraInfo* CS_EnumerateUsbCameras(int* count, CS_Status* status);
void CS_FreeEnumeratedUsbCameras(CS_UsbCameraInfo* cameras, int count);
CS_Source* CS_EnumerateSources(int* count, CS_Status* status);
void CS_ReleaseEnumeratedSources(CS_Source* sources, int count);
CS_Sink* CS_EnumerateSinks(int* count, CS_Status* status);
void CS_ReleaseEnumeratedSinks(CS_Sink* sinks, int count);
void CS_FreeString(char* str);
void CS_FreeEnumPropertyChoices(char** choices, int count);
void CS_FreeHttpCameraUrls(char** urls, int count);
void CS_FreeEnumeratedProperties(CS_Property* properties, int count);
void CS_FreeEnumeratedVideoModes(CS_VideoMode* modes, int count);
char* CS_GetHostname();
char** CS_GetNetworkInterfaces(int* count);
void CS_FreeNetworkInterfaces(char** interfaces, int count);
#ifdef __cplusplus
} // extern "C"
#endif
#endif // CSCORE_CSCORE_C_H_

View File

@@ -0,0 +1,353 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2015-2018 FIRST. 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 CSCORE_CSCORE_CPP_H_
#define CSCORE_CSCORE_CPP_H_
#include <stdint.h>
#include <functional>
#include <string>
#include <vector>
#include <llvm/ArrayRef.h>
#include <llvm/SmallVector.h>
#include <llvm/StringRef.h>
#include "cscore_c.h"
namespace cv {
class Mat;
} // namespace cv
namespace cs {
//
// Handle-based interface for C++. Users are encouraged to use the
// object oriented interface instead; this interface is intended for use
// in applications such as JNI which require handle-based access.
//
/// USB camera information
struct UsbCameraInfo {
/// Device number (e.g. N in '/dev/videoN' on Linux)
int dev;
/// Path to device if available (e.g. '/dev/video0' on Linux)
std::string path;
/// Vendor/model name of the camera as provided by the USB driver
std::string name;
};
/// Video mode
struct VideoMode : public CS_VideoMode {
enum PixelFormat {
kUnknown = CS_PIXFMT_UNKNOWN,
kMJPEG = CS_PIXFMT_MJPEG,
kYUYV = CS_PIXFMT_YUYV,
kRGB565 = CS_PIXFMT_RGB565,
kBGR = CS_PIXFMT_BGR,
kGray = CS_PIXFMT_GRAY
};
VideoMode() {
pixelFormat = 0;
width = 0;
height = 0;
fps = 0;
}
VideoMode(PixelFormat pixelFormat_, int width_, int height_, int fps_) {
pixelFormat = pixelFormat_;
width = width_;
height = height_;
fps = fps_;
}
explicit operator bool() const { return pixelFormat == kUnknown; }
};
/// Listener event
struct RawEvent {
enum Kind {
kSourceCreated = CS_SOURCE_CREATED,
kSourceDestroyed = CS_SOURCE_DESTROYED,
kSourceConnected = CS_SOURCE_CONNECTED,
kSourceDisconnected = CS_SOURCE_DISCONNECTED,
kSourceVideoModesUpdated = CS_SOURCE_VIDEOMODES_UPDATED,
kSourceVideoModeChanged = CS_SOURCE_VIDEOMODE_CHANGED,
kSourcePropertyCreated = CS_SOURCE_PROPERTY_CREATED,
kSourcePropertyValueUpdated = CS_SOURCE_PROPERTY_VALUE_UPDATED,
kSourcePropertyChoicesUpdated = CS_SOURCE_PROPERTY_CHOICES_UPDATED,
kSinkSourceChanged = CS_SINK_SOURCE_CHANGED,
kSinkCreated = CS_SINK_CREATED,
kSinkDestroyed = CS_SINK_DESTROYED,
kSinkEnabled = CS_SINK_ENABLED,
kSinkDisabled = CS_SINK_DISABLED,
kNetworkInterfacesChanged = CS_NETWORK_INTERFACES_CHANGED,
kTelemetryUpdated = CS_TELEMETRY_UPDATED
};
RawEvent() = default;
explicit RawEvent(RawEvent::Kind kind_) : kind{kind_} {}
RawEvent(llvm::StringRef name_, CS_Handle handle_, RawEvent::Kind kind_)
: kind{kind_}, name{name_} {
if (kind_ == kSinkCreated || kind_ == kSinkDestroyed ||
kind_ == kSinkEnabled || kind_ == kSinkDisabled)
sinkHandle = handle_;
else
sourceHandle = handle_;
}
RawEvent(llvm::StringRef name_, CS_Source source_, const VideoMode& mode_)
: kind{kSourceVideoModeChanged},
sourceHandle{source_},
name{name_},
mode{mode_} {}
RawEvent(llvm::StringRef name_, CS_Source source_, RawEvent::Kind kind_,
CS_Property property_, CS_PropertyKind propertyKind_, int value_,
llvm::StringRef valueStr_)
: kind{kind_},
sourceHandle{source_},
name{name_},
propertyHandle{property_},
propertyKind{propertyKind_},
value{value_},
valueStr{valueStr_} {}
Kind kind;
// Valid for kSource* and kSink* respectively
CS_Source sourceHandle = CS_INVALID_HANDLE;
CS_Sink sinkHandle = CS_INVALID_HANDLE;
// Source/sink/property name
std::string name;
// Fields for kSourceVideoModeChanged event
VideoMode mode;
// Fields for kSourceProperty* events
CS_Property propertyHandle;
CS_PropertyKind propertyKind;
int value;
std::string valueStr;
};
//
// Property Functions
//
CS_PropertyKind GetPropertyKind(CS_Property property, CS_Status* status);
std::string GetPropertyName(CS_Property property, CS_Status* status);
llvm::StringRef GetPropertyName(CS_Property property,
llvm::SmallVectorImpl<char>& buf,
CS_Status* status);
int GetProperty(CS_Property property, CS_Status* status);
void SetProperty(CS_Property property, int value, CS_Status* status);
int GetPropertyMin(CS_Property property, CS_Status* status);
int GetPropertyMax(CS_Property property, CS_Status* status);
int GetPropertyStep(CS_Property property, CS_Status* status);
int GetPropertyDefault(CS_Property property, CS_Status* status);
std::string GetStringProperty(CS_Property property, CS_Status* status);
llvm::StringRef GetStringProperty(CS_Property property,
llvm::SmallVectorImpl<char>& buf,
CS_Status* status);
void SetStringProperty(CS_Property property, llvm::StringRef value,
CS_Status* status);
std::vector<std::string> GetEnumPropertyChoices(CS_Property property,
CS_Status* status);
//
// Source Creation Functions
//
CS_Source CreateUsbCameraDev(llvm::StringRef name, int dev, CS_Status* status);
CS_Source CreateUsbCameraPath(llvm::StringRef name, llvm::StringRef path,
CS_Status* status);
CS_Source CreateHttpCamera(llvm::StringRef name, llvm::StringRef url,
CS_HttpCameraKind kind, CS_Status* status);
CS_Source CreateHttpCamera(llvm::StringRef name,
llvm::ArrayRef<std::string> urls,
CS_HttpCameraKind kind, CS_Status* status);
CS_Source CreateCvSource(llvm::StringRef name, const VideoMode& mode,
CS_Status* status);
//
// Source Functions
//
CS_SourceKind GetSourceKind(CS_Source source, CS_Status* status);
std::string GetSourceName(CS_Source source, CS_Status* status);
llvm::StringRef GetSourceName(CS_Source source,
llvm::SmallVectorImpl<char>& buf,
CS_Status* status);
std::string GetSourceDescription(CS_Source source, CS_Status* status);
llvm::StringRef GetSourceDescription(CS_Source source,
llvm::SmallVectorImpl<char>& buf,
CS_Status* status);
uint64_t GetSourceLastFrameTime(CS_Source source, CS_Status* status);
bool IsSourceConnected(CS_Source source, CS_Status* status);
CS_Property GetSourceProperty(CS_Source source, llvm::StringRef name,
CS_Status* status);
llvm::ArrayRef<CS_Property> EnumerateSourceProperties(
CS_Source source, llvm::SmallVectorImpl<CS_Property>& vec,
CS_Status* status);
VideoMode GetSourceVideoMode(CS_Source source, CS_Status* status);
bool SetSourceVideoMode(CS_Source source, const VideoMode& mode,
CS_Status* status);
bool SetSourcePixelFormat(CS_Source source, VideoMode::PixelFormat pixelFormat,
CS_Status* status);
bool SetSourceResolution(CS_Source source, int width, int height,
CS_Status* status);
bool SetSourceFPS(CS_Source source, int fps, CS_Status* status);
std::vector<VideoMode> EnumerateSourceVideoModes(CS_Source source,
CS_Status* status);
llvm::ArrayRef<CS_Sink> EnumerateSourceSinks(
CS_Source source, llvm::SmallVectorImpl<CS_Sink>& vec, CS_Status* status);
CS_Source CopySource(CS_Source source, CS_Status* status);
void ReleaseSource(CS_Source source, CS_Status* status);
//
// Camera Source Common Property Fuctions
//
void SetCameraBrightness(CS_Source source, int brightness, CS_Status* status);
int GetCameraBrightness(CS_Source source, CS_Status* status);
void SetCameraWhiteBalanceAuto(CS_Source source, CS_Status* status);
void SetCameraWhiteBalanceHoldCurrent(CS_Source source, CS_Status* status);
void SetCameraWhiteBalanceManual(CS_Source source, int value,
CS_Status* status);
void SetCameraExposureAuto(CS_Source source, CS_Status* status);
void SetCameraExposureHoldCurrent(CS_Source source, CS_Status* status);
void SetCameraExposureManual(CS_Source source, int value, CS_Status* status);
//
// UsbCamera Source Functions
//
std::string GetUsbCameraPath(CS_Source source, CS_Status* status);
//
// HttpCamera Source Functions
//
CS_HttpCameraKind GetHttpCameraKind(CS_Source source, CS_Status* status);
void SetHttpCameraUrls(CS_Source source, llvm::ArrayRef<std::string> urls,
CS_Status* status);
std::vector<std::string> GetHttpCameraUrls(CS_Source source, CS_Status* status);
//
// OpenCV Source Functions
//
void PutSourceFrame(CS_Source source, cv::Mat& image, CS_Status* status);
void NotifySourceError(CS_Source source, llvm::StringRef msg,
CS_Status* status);
void SetSourceConnected(CS_Source source, bool connected, CS_Status* status);
void SetSourceDescription(CS_Source source, llvm::StringRef description,
CS_Status* status);
CS_Property CreateSourceProperty(CS_Source source, llvm::StringRef name,
CS_PropertyKind kind, int minimum, int maximum,
int step, int defaultValue, int value,
CS_Status* status);
void SetSourceEnumPropertyChoices(CS_Source source, CS_Property property,
llvm::ArrayRef<std::string> choices,
CS_Status* status);
//
// Sink Creation Functions
//
CS_Sink CreateMjpegServer(llvm::StringRef name, llvm::StringRef listenAddress,
int port, CS_Status* status);
CS_Sink CreateCvSink(llvm::StringRef name, CS_Status* status);
CS_Sink CreateCvSinkCallback(llvm::StringRef name,
std::function<void(uint64_t time)> processFrame,
CS_Status* status);
//
// Sink Functions
//
CS_SinkKind GetSinkKind(CS_Sink sink, CS_Status* status);
std::string GetSinkName(CS_Sink sink, CS_Status* status);
llvm::StringRef GetSinkName(CS_Sink sink, llvm::SmallVectorImpl<char>& buf,
CS_Status* status);
std::string GetSinkDescription(CS_Sink sink, CS_Status* status);
llvm::StringRef GetSinkDescription(CS_Sink sink,
llvm::SmallVectorImpl<char>& buf,
CS_Status* status);
void SetSinkSource(CS_Sink sink, CS_Source source, CS_Status* status);
CS_Property GetSinkSourceProperty(CS_Sink sink, llvm::StringRef name,
CS_Status* status);
CS_Source GetSinkSource(CS_Sink sink, CS_Status* status);
CS_Sink CopySink(CS_Sink sink, CS_Status* status);
void ReleaseSink(CS_Sink sink, CS_Status* status);
//
// MjpegServer Sink Functions
//
std::string GetMjpegServerListenAddress(CS_Sink sink, CS_Status* status);
int GetMjpegServerPort(CS_Sink sink, CS_Status* status);
//
// OpenCV Sink Functions
//
void SetSinkDescription(CS_Sink sink, llvm::StringRef description,
CS_Status* status);
uint64_t GrabSinkFrame(CS_Sink sink, cv::Mat& image, CS_Status* status);
uint64_t GrabSinkFrameTimeout(CS_Sink sink, cv::Mat& image, double timeout,
CS_Status* status);
std::string GetSinkError(CS_Sink sink, CS_Status* status);
llvm::StringRef GetSinkError(CS_Sink sink, llvm::SmallVectorImpl<char>& buf,
CS_Status* status);
void SetSinkEnabled(CS_Sink sink, bool enabled, CS_Status* status);
//
// Listener Functions
//
void SetListenerOnStart(std::function<void()> onStart);
void SetListenerOnExit(std::function<void()> onExit);
CS_Listener AddListener(std::function<void(const RawEvent& event)> callback,
int eventMask, bool immediateNotify, CS_Status* status);
void RemoveListener(CS_Listener handle, CS_Status* status);
bool NotifierDestroyed();
//
// Telemetry Functions
//
void SetTelemetryPeriod(double seconds);
double GetTelemetryElapsedTime();
int64_t GetTelemetryValue(CS_Handle handle, CS_TelemetryKind kind,
CS_Status* status);
double GetTelemetryAverageValue(CS_Handle handle, CS_TelemetryKind kind,
CS_Status* status);
//
// Logging Functions
//
typedef std::function<void(unsigned int level, const char* file,
unsigned int line, const char* msg)>
LogFunc;
void SetLogger(LogFunc func, unsigned int min_level);
void SetDefaultLogger(unsigned int min_level);
//
// Utility Functions
//
std::vector<UsbCameraInfo> EnumerateUsbCameras(CS_Status* status);
llvm::ArrayRef<CS_Source> EnumerateSourceHandles(
llvm::SmallVectorImpl<CS_Source>& vec, CS_Status* status);
llvm::ArrayRef<CS_Sink> EnumerateSinkHandles(
llvm::SmallVectorImpl<CS_Sink>& vec, CS_Status* status);
std::string GetHostname();
std::vector<std::string> GetNetworkInterfaces();
} // namespace cs
// C functions taking a cv::Mat* for specific interop implementations
extern "C" {
uint64_t CS_GrabSinkFrameCpp(CS_Sink sink, cv::Mat* image, CS_Status* status);
uint64_t CS_GrabSinkFrameTimeoutCpp(CS_Sink sink, cv::Mat* image,
double timeout, CS_Status* status);
void CS_PutSourceFrameCpp(CS_Source source, cv::Mat* image, CS_Status* status);
} // extern "C"
#endif // CSCORE_CSCORE_CPP_H_

View File

@@ -0,0 +1,661 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2015-2018 FIRST. 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 CSCORE_CSCORE_OO_H_
#define CSCORE_CSCORE_OO_H_
#include <initializer_list>
#include <string>
#include <utility>
#include <vector>
#include "cscore_cpp.h"
namespace cs {
//
// Object-oriented interface
//
// Forward declarations so friend declarations work correctly
class CvSource;
class VideoEvent;
class VideoSink;
class VideoSource;
class VideoProperty {
friend class CvSource;
friend class VideoEvent;
friend class VideoSink;
friend class VideoSource;
public:
enum Kind {
kNone = CS_PROP_NONE,
kBoolean = CS_PROP_BOOLEAN,
kInteger = CS_PROP_INTEGER,
kString = CS_PROP_STRING,
kEnum = CS_PROP_ENUM
};
VideoProperty() : m_handle(0), m_kind(kNone) {}
std::string GetName() const;
Kind GetKind() const { return m_kind; }
explicit operator bool() const { return m_kind != kNone; }
// Kind checkers
bool IsBoolean() const { return m_kind == kBoolean; }
bool IsInteger() const { return m_kind == kInteger; }
bool IsString() const { return m_kind == kString; }
bool IsEnum() const { return m_kind == kEnum; }
int Get() const;
void Set(int value);
int GetMin() const;
int GetMax() const;
int GetStep() const;
int GetDefault() const;
// String-specific functions
std::string GetString() const;
llvm::StringRef GetString(llvm::SmallVectorImpl<char>& buf) const;
void SetString(llvm::StringRef value);
// Enum-specific functions
std::vector<std::string> GetChoices() const;
CS_Status GetLastStatus() const { return m_status; }
private:
explicit VideoProperty(CS_Property handle);
VideoProperty(CS_Property handle, Kind kind);
mutable CS_Status m_status;
CS_Property m_handle;
Kind m_kind;
};
/// A source for video that provides a sequence of frames.
class VideoSource {
friend class VideoEvent;
friend class VideoSink;
public:
enum Kind {
kUnknown = CS_SOURCE_UNKNOWN,
kUsb = CS_SOURCE_USB,
kHttp = CS_SOURCE_HTTP,
kCv = CS_SOURCE_CV
};
VideoSource() noexcept : m_handle(0) {}
VideoSource(const VideoSource& source);
VideoSource(VideoSource&& other) noexcept;
VideoSource& operator=(VideoSource other) noexcept;
~VideoSource();
explicit operator bool() const { return m_handle != 0; }
int GetHandle() const { return m_handle; }
bool operator==(const VideoSource& other) const {
return m_handle == other.m_handle;
}
bool operator!=(const VideoSource& other) const { return !(*this == other); }
/// Get the kind of the source.
Kind GetKind() const;
/// Get the name of the source. The name is an arbitrary identifier
/// provided when the source is created, and should be unique.
std::string GetName() const;
/// Get the source description. This is source-kind specific.
std::string GetDescription() const;
/// Get the last time a frame was captured.
/// This uses the same time base as wpi::Now().
/// @return Time in 1 us increments.
uint64_t GetLastFrameTime() const;
/// Is the source currently connected to whatever is providing the images?
bool IsConnected() const;
/// Get a property.
/// @param name Property name
/// @return Property contents (of kind Property::kNone if no property with
/// the given name exists)
VideoProperty GetProperty(llvm::StringRef name);
/// Enumerate all properties of this source.
std::vector<VideoProperty> EnumerateProperties() const;
/// Get the current video mode.
VideoMode GetVideoMode() const;
/// Set the video mode.
/// @param mode Video mode
bool SetVideoMode(const VideoMode& mode);
/// Set the video mode.
/// @param pixelFormat desired pixel format
/// @param width desired width
/// @param height desired height
/// @param fps desired FPS
/// @return True if set successfully
bool SetVideoMode(VideoMode::PixelFormat pixelFormat, int width, int height,
int fps);
/// Set the pixel format.
/// @param pixelFormat desired pixel format
/// @return True if set successfully
bool SetPixelFormat(VideoMode::PixelFormat pixelFormat);
/// Set the resolution.
/// @param width desired width
/// @param height desired height
/// @return True if set successfully
bool SetResolution(int width, int height);
/// Set the frames per second (FPS).
/// @param fps desired FPS
/// @return True if set successfully
bool SetFPS(int fps);
/// Get the actual FPS.
/// SetTelemetryPeriod() must be called for this to be valid.
/// @return Actual FPS averaged over the telemetry period.
double GetActualFPS() const;
/// Get the data rate (in bytes per second).
/// SetTelemetryPeriod() must be called for this to be valid.
/// @return Data rate averaged over the telemetry period.
double GetActualDataRate() const;
/// Enumerate all known video modes for this source.
std::vector<VideoMode> EnumerateVideoModes() const;
CS_Status GetLastStatus() const { return m_status; }
/// Enumerate all sinks connected to this source.
/// @return Vector of sinks.
std::vector<VideoSink> EnumerateSinks();
/// Enumerate all existing sources.
/// @return Vector of sources.
static std::vector<VideoSource> EnumerateSources();
friend void swap(VideoSource& first, VideoSource& second) noexcept {
using std::swap;
swap(first.m_status, second.m_status);
swap(first.m_handle, second.m_handle);
}
protected:
explicit VideoSource(CS_Source handle) : m_handle(handle) {}
mutable CS_Status m_status = 0;
CS_Source m_handle;
};
/// A source that represents a video camera.
class VideoCamera : public VideoSource {
public:
enum WhiteBalance {
kFixedIndoor = 3000,
kFixedOutdoor1 = 4000,
kFixedOutdoor2 = 5000,
kFixedFluorescent1 = 5100,
kFixedFlourescent2 = 5200
};
VideoCamera() = default;
/// Set the brightness, as a percentage (0-100).
void SetBrightness(int brightness);
/// Get the brightness, as a percentage (0-100).
int GetBrightness();
/// Set the white balance to auto.
void SetWhiteBalanceAuto();
/// Set the white balance to hold current.
void SetWhiteBalanceHoldCurrent();
/// Set the white balance to manual, with specified color temperature.
void SetWhiteBalanceManual(int value);
/// Set the exposure to auto aperature.
void SetExposureAuto();
/// Set the exposure to hold current.
void SetExposureHoldCurrent();
/// Set the exposure to manual, as a percentage (0-100).
void SetExposureManual(int value);
protected:
explicit VideoCamera(CS_Source handle) : VideoSource(handle) {}
};
/// A source that represents a USB camera.
class UsbCamera : public VideoCamera {
public:
UsbCamera() = default;
/// Create a source for a USB camera based on device number.
/// @param name Source name (arbitrary unique identifier)
/// @param dev Device number (e.g. 0 for /dev/video0)
UsbCamera(llvm::StringRef name, int dev);
/// Create a source for a USB camera based on device path.
/// @param name Source name (arbitrary unique identifier)
/// @param path Path to device (e.g. "/dev/video0" on Linux)
UsbCamera(llvm::StringRef name, llvm::StringRef path);
/// Enumerate USB cameras on the local system.
/// @return Vector of USB camera information (one for each camera)
static std::vector<UsbCameraInfo> EnumerateUsbCameras();
/// Get the path to the device.
std::string GetPath() const;
};
/// A source that represents a MJPEG-over-HTTP (IP) camera.
class HttpCamera : public VideoCamera {
public:
enum HttpCameraKind {
kUnknown = CS_HTTP_UNKNOWN,
kMJPGStreamer = CS_HTTP_MJPGSTREAMER,
kCSCore = CS_HTTP_CSCORE,
kAxis = CS_HTTP_AXIS
};
/// Create a source for a MJPEG-over-HTTP (IP) camera.
/// @param name Source name (arbitrary unique identifier)
/// @param url Camera URL (e.g. "http://10.x.y.11/video/stream.mjpg")
/// @param kind Camera kind (e.g. kAxis)
HttpCamera(llvm::StringRef name, llvm::StringRef url,
HttpCameraKind kind = kUnknown);
/// Create a source for a MJPEG-over-HTTP (IP) camera.
/// @param name Source name (arbitrary unique identifier)
/// @param url Camera URL (e.g. "http://10.x.y.11/video/stream.mjpg")
/// @param kind Camera kind (e.g. kAxis)
HttpCamera(llvm::StringRef name, const char* url,
HttpCameraKind kind = kUnknown);
/// Create a source for a MJPEG-over-HTTP (IP) camera.
/// @param name Source name (arbitrary unique identifier)
/// @param url Camera URL (e.g. "http://10.x.y.11/video/stream.mjpg")
/// @param kind Camera kind (e.g. kAxis)
HttpCamera(llvm::StringRef name, const std::string& url,
HttpCameraKind kind = kUnknown);
/// Create a source for a MJPEG-over-HTTP (IP) camera.
/// @param name Source name (arbitrary unique identifier)
/// @param urls Array of Camera URLs
/// @param kind Camera kind (e.g. kAxis)
HttpCamera(llvm::StringRef name, llvm::ArrayRef<std::string> urls,
HttpCameraKind kind = kUnknown);
/// Create a source for a MJPEG-over-HTTP (IP) camera.
/// @param name Source name (arbitrary unique identifier)
/// @param urls Array of Camera URLs
/// @param kind Camera kind (e.g. kAxis)
template <typename T>
HttpCamera(llvm::StringRef name, std::initializer_list<T> urls,
HttpCameraKind kind = kUnknown);
/// Get the kind of HTTP camera.
/// Autodetection can result in returning a different value than the camera
/// was created with.
HttpCameraKind GetHttpCameraKind() const;
/// Change the URLs used to connect to the camera.
void SetUrls(llvm::ArrayRef<std::string> urls);
/// Change the URLs used to connect to the camera.
template <typename T>
void SetUrls(std::initializer_list<T> urls);
/// Get the URLs used to connect to the camera.
std::vector<std::string> GetUrls() const;
};
/// A source that represents an Axis IP camera.
class AxisCamera : public HttpCamera {
static std::string HostToUrl(llvm::StringRef host);
static std::vector<std::string> HostToUrl(llvm::ArrayRef<std::string> hosts);
template <typename T>
static std::vector<std::string> HostToUrl(std::initializer_list<T> hosts);
public:
/// Create a source for an Axis IP camera.
/// @param name Source name (arbitrary unique identifier)
/// @param host Camera host IP or DNS name (e.g. "10.x.y.11")
/// @param kind Camera kind (e.g. kAxis)
AxisCamera(llvm::StringRef name, llvm::StringRef host);
/// Create a source for an Axis IP camera.
/// @param name Source name (arbitrary unique identifier)
/// @param host Camera host IP or DNS name (e.g. "10.x.y.11")
/// @param kind Camera kind (e.g. kAxis)
AxisCamera(llvm::StringRef name, const char* host);
/// Create a source for an Axis IP camera.
/// @param name Source name (arbitrary unique identifier)
/// @param host Camera host IP or DNS name (e.g. "10.x.y.11")
/// @param kind Camera kind (e.g. kAxis)
AxisCamera(llvm::StringRef name, const std::string& host);
/// Create a source for an Axis IP camera.
/// @param name Source name (arbitrary unique identifier)
/// @param hosts Array of Camera host IPs/DNS names
/// @param kind Camera kind (e.g. kAxis)
AxisCamera(llvm::StringRef name, llvm::ArrayRef<std::string> hosts);
/// Create a source for an Axis IP camera.
/// @param name Source name (arbitrary unique identifier)
/// @param hosts Array of Camera host IPs/DNS names
/// @param kind Camera kind (e.g. kAxis)
template <typename T>
AxisCamera(llvm::StringRef name, std::initializer_list<T> hosts);
};
/// A source for user code to provide OpenCV images as video frames.
class CvSource : public VideoSource {
public:
CvSource() = default;
/// Create an OpenCV source.
/// @param name Source name (arbitrary unique identifier)
/// @param mode Video mode being generated
CvSource(llvm::StringRef name, const VideoMode& mode);
/// Create an OpenCV source.
/// @param name Source name (arbitrary unique identifier)
/// @param pixelFormat Pixel format
/// @param width width
/// @param height height
/// @param fps fps
CvSource(llvm::StringRef name, VideoMode::PixelFormat pixelFormat, int width,
int height, int fps);
/// Put an OpenCV image and notify sinks.
/// Only 8-bit single-channel or 3-channel (with BGR channel order) images
/// are supported. If the format, depth or channel order is different, use
/// cv::Mat::convertTo() and/or cv::cvtColor() to convert it first.
/// @param image OpenCV image
void PutFrame(cv::Mat& image);
/// Signal sinks that an error has occurred. This should be called instead
/// of NotifyFrame when an error occurs.
void NotifyError(llvm::StringRef msg);
/// Set source connection status. Defaults to true.
/// @param connected True for connected, false for disconnected
void SetConnected(bool connected);
/// Set source description.
/// @param description Description
void SetDescription(llvm::StringRef description);
/// Create a property.
/// @param name Property name
/// @param kind Property kind
/// @param minimum Minimum value
/// @param maximum Maximum value
/// @param step Step value
/// @param defaultValue Default value
/// @param value Current value
/// @return Property
VideoProperty CreateProperty(llvm::StringRef name, VideoProperty::Kind kind,
int minimum, int maximum, int step,
int defaultValue, int value);
/// Create an integer property.
/// @param name Property name
/// @param minimum Minimum value
/// @param maximum Maximum value
/// @param step Step value
/// @param defaultValue Default value
/// @param value Current value
/// @return Property
VideoProperty CreateIntegerProperty(llvm::StringRef name, int minimum,
int maximum, int step, int defaultValue,
int value);
/// Create a boolean property.
/// @param name Property name
/// @param defaultValue Default value
/// @param value Current value
/// @return Property
VideoProperty CreateBooleanProperty(llvm::StringRef name, bool defaultValue,
bool value);
/// Create a string property.
/// @param name Property name
/// @param defaultValue Default value
/// @param value Current value
/// @return Property
VideoProperty CreateStringProperty(llvm::StringRef name,
llvm::StringRef value);
/// Configure enum property choices.
/// @param property Property
/// @param choices Choices
void SetEnumPropertyChoices(const VideoProperty& property,
llvm::ArrayRef<std::string> choices);
/// Configure enum property choices.
/// @param property Property
/// @param choices Choices
template <typename T>
void SetEnumPropertyChoices(const VideoProperty& property,
std::initializer_list<T> choices);
};
/// A sink for video that accepts a sequence of frames.
class VideoSink {
friend class VideoEvent;
friend class VideoSource;
public:
enum Kind {
kUnknown = CS_SINK_UNKNOWN,
kMjpeg = CS_SINK_MJPEG,
kCv = CS_SINK_CV
};
VideoSink() noexcept : m_handle(0) {}
VideoSink(const VideoSink& sink);
VideoSink(VideoSink&& sink) noexcept;
VideoSink& operator=(VideoSink other) noexcept;
~VideoSink();
explicit operator bool() const { return m_handle != 0; }
int GetHandle() const { return m_handle; }
bool operator==(const VideoSink& other) const {
return m_handle == other.m_handle;
}
bool operator!=(const VideoSink& other) const { return !(*this == other); }
/// Get the kind of the sink.
Kind GetKind() const;
/// Get the name of the sink. The name is an arbitrary identifier
/// provided when the sink is created, and should be unique.
std::string GetName() const;
/// Get the sink description. This is sink-kind specific.
std::string GetDescription() const;
/// Configure which source should provide frames to this sink. Each sink
/// can accept frames from only a single source, but a single source can
/// provide frames to multiple clients.
/// @param source Source
void SetSource(VideoSource source);
/// Get the connected source.
/// @return Connected source (empty if none connected).
VideoSource GetSource() const;
/// Get a property of the associated source.
/// @param name Property name
/// @return Property (kind Property::kNone if no property with
/// the given name exists or no source connected)
VideoProperty GetSourceProperty(llvm::StringRef name);
CS_Status GetLastStatus() const { return m_status; }
/// Enumerate all existing sinks.
/// @return Vector of sinks.
static std::vector<VideoSink> EnumerateSinks();
friend void swap(VideoSink& first, VideoSink& second) noexcept {
using std::swap;
swap(first.m_status, second.m_status);
swap(first.m_handle, second.m_handle);
}
protected:
explicit VideoSink(CS_Sink handle) : m_handle(handle) {}
mutable CS_Status m_status = 0;
CS_Sink m_handle;
};
/// A sink that acts as a MJPEG-over-HTTP network server.
class MjpegServer : public VideoSink {
public:
MjpegServer() = default;
/// Create a MJPEG-over-HTTP server sink.
/// @param name Sink name (arbitrary unique identifier)
/// @param listenAddress TCP listen address (empty string for all addresses)
/// @param port TCP port number
MjpegServer(llvm::StringRef name, llvm::StringRef listenAddress, int port);
/// Create a MJPEG-over-HTTP server sink.
/// @param name Sink name (arbitrary unique identifier)
/// @param port TCP port number
MjpegServer(llvm::StringRef name, int port) : MjpegServer(name, "", port) {}
/// Get the listen address of the server.
std::string GetListenAddress() const;
/// Get the port number of the server.
int GetPort() const;
};
/// A sink for user code to accept video frames as OpenCV images.
class CvSink : public VideoSink {
public:
CvSink() = default;
/// Create a sink for accepting OpenCV images.
/// WaitForFrame() must be called on the created sink to get each new
/// image.
/// @param name Source name (arbitrary unique identifier)
explicit CvSink(llvm::StringRef name);
/// Create a sink for accepting OpenCV images in a separate thread.
/// A thread will be created that calls WaitForFrame() and calls the
/// processFrame() callback each time a new frame arrives.
/// @param name Source name (arbitrary unique identifier)
/// @param processFrame Frame processing function; will be called with a
/// time=0 if an error occurred. processFrame should call GetImage()
/// or GetError() as needed, but should not call (except in very
/// unusual circumstances) WaitForImage().
CvSink(llvm::StringRef name, std::function<void(uint64_t time)> processFrame);
/// Set sink description.
/// @param description Description
void SetDescription(llvm::StringRef description);
/// Wait for the next frame and get the image.
/// Times out (returning 0) after timeout seconds.
/// The provided image will have three 8-bit channels stored in BGR order.
/// @return Frame time, or 0 on error (call GetError() to obtain the error
/// message); the frame time is in the same time base as wpi::Now(),
/// and is in 1 us increments.
uint64_t GrabFrame(cv::Mat& image, double timeout = 0.225) const;
/// Wait for the next frame and get the image. May block forever.
/// The provided image will have three 8-bit channels stored in BGR order.
/// @return Frame time, or 0 on error (call GetError() to obtain the error
/// message); the frame time is in the same time base as wpi::Now(),
/// and is in 1 us increments.
uint64_t GrabFrameNoTimeout(cv::Mat& image) const;
/// Get error string. Call this if WaitForFrame() returns 0 to determine
/// what the error is.
std::string GetError() const;
/// Enable or disable getting new frames.
/// Disabling will cause processFrame (for callback-based CvSinks) to not
/// be called and WaitForFrame() to not return. This can be used to save
/// processor resources when frames are not needed.
void SetEnabled(bool enabled);
};
/// An event generated by the library and provided to event listeners.
class VideoEvent : public RawEvent {
public:
/// Get the source associated with the event (if any).
VideoSource GetSource() const;
/// Get the sink associated with the event (if any).
VideoSink GetSink() const;
/// Get the property associated with the event (if any).
VideoProperty GetProperty() const;
};
/// An event listener. This calls back to a desigated callback function when
/// an event matching the specified mask is generated by the library.
class VideoListener {
public:
VideoListener() : m_handle(0) {}
/// Create an event listener.
/// @param callback Callback function
/// @param eventMask Bitmask of VideoEvent::Kind values
/// @param immediateNotify Whether callback should be immediately called with
/// a representative set of events for the current library state.
VideoListener(std::function<void(const VideoEvent& event)> callback,
int eventMask, bool immediateNotify);
VideoListener(const VideoListener&) = delete;
VideoListener& operator=(const VideoListener&) = delete;
VideoListener(VideoListener&& other) noexcept;
VideoListener& operator=(VideoListener&& other) noexcept;
~VideoListener();
friend void swap(VideoListener& first, VideoListener& second) noexcept {
using std::swap;
swap(first.m_handle, second.m_handle);
}
private:
CS_Listener m_handle;
};
} // namespace cs
#include "cscore_oo.inl"
#endif // CSCORE_CSCORE_OO_H_

View File

@@ -0,0 +1,573 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2015. 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 CSCORE_OO_INL_
#define CSCORE_OO_INL_
namespace cs {
inline std::string VideoProperty::GetName() const {
m_status = 0;
return GetPropertyName(m_handle, &m_status);
}
inline int VideoProperty::Get() const {
m_status = 0;
return GetProperty(m_handle, &m_status);
}
inline void VideoProperty::Set(int value) {
m_status = 0;
SetProperty(m_handle, value, &m_status);
}
inline int VideoProperty::GetMin() const {
m_status = 0;
return GetPropertyMin(m_handle, &m_status);
}
inline int VideoProperty::GetMax() const {
m_status = 0;
return GetPropertyMax(m_handle, &m_status);
}
inline int VideoProperty::GetStep() const {
m_status = 0;
return GetPropertyStep(m_handle, &m_status);
}
inline int VideoProperty::GetDefault() const {
m_status = 0;
return GetPropertyDefault(m_handle, &m_status);
}
inline std::string VideoProperty::GetString() const {
m_status = 0;
return GetStringProperty(m_handle, &m_status);
}
inline llvm::StringRef VideoProperty::GetString(
llvm::SmallVectorImpl<char>& buf) const {
m_status = 0;
return GetStringProperty(m_handle, buf, &m_status);
}
inline void VideoProperty::SetString(llvm::StringRef value) {
m_status = 0;
SetStringProperty(m_handle, value, &m_status);
}
inline std::vector<std::string> VideoProperty::GetChoices() const {
m_status = 0;
return GetEnumPropertyChoices(m_handle, &m_status);
}
inline VideoProperty::VideoProperty(CS_Property handle) : m_handle(handle) {
m_status = 0;
if (handle == 0)
m_kind = kNone;
else
m_kind =
static_cast<Kind>(static_cast<int>(GetPropertyKind(handle, &m_status)));
}
inline VideoProperty::VideoProperty(CS_Property handle, Kind kind)
: m_handle(handle), m_kind(kind) {}
inline VideoSource::VideoSource(const VideoSource& source)
: m_handle(source.m_handle == 0 ? 0
: CopySource(source.m_handle, &m_status)) {}
inline VideoSource::VideoSource(VideoSource&& other) noexcept : VideoSource() {
swap(*this, other);
}
inline VideoSource& VideoSource::operator=(VideoSource other) noexcept {
swap(*this, other);
return *this;
}
inline VideoSource::~VideoSource() {
m_status = 0;
if (m_handle != 0) ReleaseSource(m_handle, &m_status);
}
inline VideoSource::Kind VideoSource::GetKind() const {
m_status = 0;
return static_cast<VideoSource::Kind>(GetSourceKind(m_handle, &m_status));
}
inline std::string VideoSource::GetName() const {
m_status = 0;
return GetSourceName(m_handle, &m_status);
}
inline std::string VideoSource::GetDescription() const {
m_status = 0;
return GetSourceDescription(m_handle, &m_status);
}
inline uint64_t VideoSource::GetLastFrameTime() const {
m_status = 0;
return GetSourceLastFrameTime(m_handle, &m_status);
}
inline bool VideoSource::IsConnected() const {
m_status = 0;
return IsSourceConnected(m_handle, &m_status);
}
inline VideoProperty VideoSource::GetProperty(llvm::StringRef name) {
m_status = 0;
return VideoProperty{GetSourceProperty(m_handle, name, &m_status)};
}
inline VideoMode VideoSource::GetVideoMode() const {
m_status = 0;
return GetSourceVideoMode(m_handle, &m_status);
}
inline bool VideoSource::SetVideoMode(const VideoMode& mode) {
m_status = 0;
return SetSourceVideoMode(m_handle, mode, &m_status);
}
inline bool VideoSource::SetVideoMode(VideoMode::PixelFormat pixelFormat,
int width, int height, int fps) {
m_status = 0;
return SetSourceVideoMode(
m_handle, VideoMode{pixelFormat, width, height, fps}, &m_status);
}
inline bool VideoSource::SetPixelFormat(VideoMode::PixelFormat pixelFormat) {
m_status = 0;
return SetSourcePixelFormat(m_handle, pixelFormat, &m_status);
}
inline bool VideoSource::SetResolution(int width, int height) {
m_status = 0;
return SetSourceResolution(m_handle, width, height, &m_status);
}
inline bool VideoSource::SetFPS(int fps) {
m_status = 0;
return SetSourceFPS(m_handle, fps, &m_status);
}
inline double VideoSource::GetActualFPS() const {
m_status = 0;
return cs::GetTelemetryAverageValue(m_handle, CS_SOURCE_FRAMES_RECEIVED,
&m_status);
}
inline double VideoSource::GetActualDataRate() const {
m_status = 0;
return cs::GetTelemetryAverageValue(m_handle, CS_SOURCE_BYTES_RECEIVED,
&m_status);
}
inline std::vector<VideoMode> VideoSource::EnumerateVideoModes() const {
CS_Status status = 0;
return EnumerateSourceVideoModes(m_handle, &status);
}
inline void VideoCamera::SetBrightness(int brightness) {
m_status = 0;
SetCameraBrightness(m_handle, brightness, &m_status);
}
inline int VideoCamera::GetBrightness() {
m_status = 0;
return GetCameraBrightness(m_handle, &m_status);
}
inline void VideoCamera::SetWhiteBalanceAuto() {
m_status = 0;
SetCameraWhiteBalanceAuto(m_handle, &m_status);
}
inline void VideoCamera::SetWhiteBalanceHoldCurrent() {
m_status = 0;
SetCameraWhiteBalanceHoldCurrent(m_handle, &m_status);
}
inline void VideoCamera::SetWhiteBalanceManual(int value) {
m_status = 0;
SetCameraWhiteBalanceManual(m_handle, value, &m_status);
}
inline void VideoCamera::SetExposureAuto() {
m_status = 0;
SetCameraExposureAuto(m_handle, &m_status);
}
inline void VideoCamera::SetExposureHoldCurrent() {
m_status = 0;
SetCameraExposureHoldCurrent(m_handle, &m_status);
}
inline void VideoCamera::SetExposureManual(int value) {
m_status = 0;
SetCameraExposureManual(m_handle, value, &m_status);
}
inline UsbCamera::UsbCamera(llvm::StringRef name, int dev) {
m_handle = CreateUsbCameraDev(name, dev, &m_status);
}
inline UsbCamera::UsbCamera(llvm::StringRef name, llvm::StringRef path) {
m_handle = CreateUsbCameraPath(name, path, &m_status);
}
inline std::vector<UsbCameraInfo> UsbCamera::EnumerateUsbCameras() {
CS_Status status = 0;
return ::cs::EnumerateUsbCameras(&status);
}
inline std::string UsbCamera::GetPath() const {
m_status = 0;
return ::cs::GetUsbCameraPath(m_handle, &m_status);
}
inline HttpCamera::HttpCamera(llvm::StringRef name, llvm::StringRef url,
HttpCameraKind kind) {
m_handle = CreateHttpCamera(
name, url, static_cast<CS_HttpCameraKind>(static_cast<int>(kind)),
&m_status);
}
inline HttpCamera::HttpCamera(llvm::StringRef name, const char* url,
HttpCameraKind kind) {
m_handle = CreateHttpCamera(
name, url, static_cast<CS_HttpCameraKind>(static_cast<int>(kind)),
&m_status);
}
inline HttpCamera::HttpCamera(llvm::StringRef name, const std::string& url,
HttpCameraKind kind)
: HttpCamera(name, llvm::StringRef{url}, kind) {}
inline HttpCamera::HttpCamera(llvm::StringRef name,
llvm::ArrayRef<std::string> urls,
HttpCameraKind kind) {
m_handle = CreateHttpCamera(
name, urls, static_cast<CS_HttpCameraKind>(static_cast<int>(kind)),
&m_status);
}
template <typename T>
inline HttpCamera::HttpCamera(llvm::StringRef name,
std::initializer_list<T> urls,
HttpCameraKind kind) {
std::vector<std::string> vec;
vec.reserve(urls.size());
for (const auto& url : urls) vec.emplace_back(url);
m_handle = CreateHttpCamera(
name, vec, static_cast<CS_HttpCameraKind>(static_cast<int>(kind)),
&m_status);
}
inline HttpCamera::HttpCameraKind HttpCamera::GetHttpCameraKind() const {
m_status = 0;
return static_cast<HttpCameraKind>(
static_cast<int>(::cs::GetHttpCameraKind(m_handle, &m_status)));
}
inline void HttpCamera::SetUrls(llvm::ArrayRef<std::string> urls) {
m_status = 0;
::cs::SetHttpCameraUrls(m_handle, urls, &m_status);
}
template <typename T>
inline void HttpCamera::SetUrls(std::initializer_list<T> urls) {
std::vector<std::string> vec;
vec.reserve(urls.size());
for (const auto& url : urls) vec.emplace_back(url);
m_status = 0;
::cs::SetHttpCameraUrls(m_handle, vec, &m_status);
}
inline std::vector<std::string> HttpCamera::GetUrls() const {
m_status = 0;
return ::cs::GetHttpCameraUrls(m_handle, &m_status);
}
inline std::string AxisCamera::HostToUrl(llvm::StringRef host) {
std::string rv{"http://"};
rv += host;
rv += "/mjpg/video.mjpg";
return rv;
}
inline std::vector<std::string> AxisCamera::HostToUrl(
llvm::ArrayRef<std::string> hosts) {
std::vector<std::string> rv;
rv.reserve(hosts.size());
for (const auto& host : hosts)
rv.emplace_back(HostToUrl(llvm::StringRef{host}));
return rv;
}
template <typename T>
inline std::vector<std::string> AxisCamera::HostToUrl(
std::initializer_list<T> hosts) {
std::vector<std::string> rv;
rv.reserve(hosts.size());
for (const auto& host : hosts)
rv.emplace_back(HostToUrl(llvm::StringRef{host}));
return rv;
}
inline AxisCamera::AxisCamera(llvm::StringRef name, llvm::StringRef host)
: HttpCamera(name, HostToUrl(host), kAxis) {}
inline AxisCamera::AxisCamera(llvm::StringRef name, const char* host)
: HttpCamera(name, HostToUrl(host), kAxis) {}
inline AxisCamera::AxisCamera(llvm::StringRef name, const std::string& host)
: HttpCamera(name, HostToUrl(llvm::StringRef{host}), kAxis) {}
inline AxisCamera::AxisCamera(llvm::StringRef name,
llvm::ArrayRef<std::string> hosts)
: HttpCamera(name, HostToUrl(hosts), kAxis) {}
template <typename T>
inline AxisCamera::AxisCamera(llvm::StringRef name,
std::initializer_list<T> hosts)
: HttpCamera(name, HostToUrl(hosts), kAxis) {}
inline CvSource::CvSource(llvm::StringRef name, const VideoMode& mode) {
m_handle = CreateCvSource(name, mode, &m_status);
}
inline CvSource::CvSource(llvm::StringRef name, VideoMode::PixelFormat format,
int width, int height, int fps) {
m_handle =
CreateCvSource(name, VideoMode{format, width, height, fps}, &m_status);
}
inline void CvSource::PutFrame(cv::Mat& image) {
m_status = 0;
PutSourceFrame(m_handle, image, &m_status);
}
inline void CvSource::NotifyError(llvm::StringRef msg) {
m_status = 0;
NotifySourceError(m_handle, msg, &m_status);
}
inline void CvSource::SetConnected(bool connected) {
m_status = 0;
SetSourceConnected(m_handle, connected, &m_status);
}
inline void CvSource::SetDescription(llvm::StringRef description) {
m_status = 0;
SetSourceDescription(m_handle, description, &m_status);
}
inline VideoProperty CvSource::CreateProperty(llvm::StringRef name,
VideoProperty::Kind kind,
int minimum, int maximum,
int step, int defaultValue,
int value) {
m_status = 0;
return VideoProperty{CreateSourceProperty(
m_handle, name, static_cast<CS_PropertyKind>(static_cast<int>(kind)),
minimum, maximum, step, defaultValue, value, &m_status)};
}
inline VideoProperty CvSource::CreateIntegerProperty(llvm::StringRef name,
int minimum, int maximum,
int step, int defaultValue,
int value) {
m_status = 0;
return VideoProperty{CreateSourceProperty(
m_handle, name, static_cast<CS_PropertyKind>(static_cast<int>(VideoProperty::Kind::kInteger)),
minimum, maximum, step, defaultValue, value, &m_status)};
}
inline VideoProperty CvSource::CreateBooleanProperty(llvm::StringRef name,
bool defaultValue,
bool value) {
m_status = 0;
return VideoProperty{CreateSourceProperty(
m_handle, name, static_cast<CS_PropertyKind>(static_cast<int>(VideoProperty::Kind::kBoolean)),
0, 1, 1, defaultValue ? 1 : 0, value ? 1 : 0, &m_status)};
}
inline VideoProperty CvSource::CreateStringProperty(llvm::StringRef name,
llvm::StringRef value) {
m_status = 0;
auto prop = VideoProperty{CreateSourceProperty(
m_handle, name, static_cast<CS_PropertyKind>(static_cast<int>(VideoProperty::Kind::kString)),
0, 0, 0, 0, 0, &m_status)};
prop.SetString(value);
return prop;
}
inline void CvSource::SetEnumPropertyChoices(
const VideoProperty& property, llvm::ArrayRef<std::string> choices) {
m_status = 0;
SetSourceEnumPropertyChoices(m_handle, property.m_handle, choices, &m_status);
}
template <typename T>
inline void CvSource::SetEnumPropertyChoices(const VideoProperty& property,
std::initializer_list<T> choices) {
std::vector<std::string> vec;
vec.reserve(choices.size());
for (const auto& choice : choices) vec.emplace_back(choice);
m_status = 0;
SetSourceEnumPropertyChoices(m_handle, property.m_handle, vec, &m_status);
}
inline VideoSink::VideoSink(const VideoSink& sink)
: m_handle(sink.m_handle == 0 ? 0 : CopySink(sink.m_handle, &m_status)) {}
inline VideoSink::VideoSink(VideoSink&& other) noexcept : VideoSink() {
swap(*this, other);
}
inline VideoSink& VideoSink::operator=(VideoSink other) noexcept {
swap(*this, other);
return *this;
}
inline VideoSink::~VideoSink() {
m_status = 0;
if (m_handle != 0) ReleaseSink(m_handle, &m_status);
}
inline VideoSink::Kind VideoSink::GetKind() const {
m_status = 0;
return static_cast<VideoSink::Kind>(GetSinkKind(m_handle, &m_status));
}
inline std::string VideoSink::GetName() const {
m_status = 0;
return GetSinkName(m_handle, &m_status);
}
inline std::string VideoSink::GetDescription() const {
m_status = 0;
return GetSinkDescription(m_handle, &m_status);
}
inline void VideoSink::SetSource(VideoSource source) {
m_status = 0;
if (!source)
SetSinkSource(m_handle, 0, &m_status);
else
SetSinkSource(m_handle, source.m_handle, &m_status);
}
inline VideoSource VideoSink::GetSource() const {
m_status = 0;
auto handle = GetSinkSource(m_handle, &m_status);
return VideoSource{handle == 0 ? 0 : CopySource(handle, &m_status)};
}
inline VideoProperty VideoSink::GetSourceProperty(llvm::StringRef name) {
m_status = 0;
return VideoProperty{GetSinkSourceProperty(m_handle, name, &m_status)};
}
inline MjpegServer::MjpegServer(llvm::StringRef name,
llvm::StringRef listenAddress, int port) {
m_handle = CreateMjpegServer(name, listenAddress, port, &m_status);
}
inline std::string MjpegServer::GetListenAddress() const {
m_status = 0;
return cs::GetMjpegServerListenAddress(m_handle, &m_status);
}
inline int MjpegServer::GetPort() const {
m_status = 0;
return cs::GetMjpegServerPort(m_handle, &m_status);
}
inline CvSink::CvSink(llvm::StringRef name) {
m_handle = CreateCvSink(name, &m_status);
}
inline CvSink::CvSink(llvm::StringRef name,
std::function<void(uint64_t time)> processFrame) {
m_handle = CreateCvSinkCallback(name, processFrame, &m_status);
}
inline void CvSink::SetDescription(llvm::StringRef description) {
m_status = 0;
SetSinkDescription(m_handle, description, &m_status);
}
inline uint64_t CvSink::GrabFrame(cv::Mat& image, double timeout) const {
m_status = 0;
return GrabSinkFrameTimeout(m_handle, image, timeout, &m_status);
}
inline uint64_t CvSink::GrabFrameNoTimeout(cv::Mat& image) const {
m_status = 0;
return GrabSinkFrame(m_handle, image, &m_status);
}
inline std::string CvSink::GetError() const {
m_status = 0;
return GetSinkError(m_handle, &m_status);
}
inline void CvSink::SetEnabled(bool enabled) {
m_status = 0;
SetSinkEnabled(m_handle, enabled, &m_status);
}
inline VideoSource VideoEvent::GetSource() const {
CS_Status status = 0;
return VideoSource{sourceHandle == 0 ? 0 : CopySource(sourceHandle, &status)};
}
inline VideoSink VideoEvent::GetSink() const {
CS_Status status = 0;
return VideoSink{sinkHandle == 0 ? 0 : CopySink(sinkHandle, &status)};
}
inline VideoProperty VideoEvent::GetProperty() const {
return VideoProperty{propertyHandle,
static_cast<VideoProperty::Kind>(propertyKind)};
}
inline VideoListener::VideoListener(
std::function<void(const VideoEvent& event)> callback, int eventMask,
bool immediateNotify) {
CS_Status status = 0;
m_handle = AddListener(
[=](const RawEvent& event) {
callback(static_cast<const VideoEvent&>(event));
},
eventMask, immediateNotify, &status);
}
inline VideoListener::VideoListener(VideoListener&& other) noexcept
: VideoListener() {
swap(*this, other);
}
inline VideoListener& VideoListener::operator=(VideoListener&& other) noexcept {
swap(*this, other);
return *this;
}
inline VideoListener::~VideoListener() {
CS_Status status = 0;
if (m_handle != 0) RemoveListener(m_handle, &status);
}
} // namespace cs
#endif /* CSCORE_OO_INL_ */