WPILibC++  2020.3.2-60-g3011ebe
cscore_cpp.h
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) 2015-2019 FIRST. All Rights Reserved. */
3 /* Open Source Software - may be modified and shared by FRC teams. The code */
4 /* must be accompanied by the FIRST BSD license file in the root directory of */
5 /* the project. */
6 /*----------------------------------------------------------------------------*/
7 
8 #ifndef CSCORE_CSCORE_CPP_H_
9 #define CSCORE_CSCORE_CPP_H_
10 
11 #include <stdint.h>
12 
13 #include <functional>
14 #include <string>
15 #include <vector>
16 
17 #include <wpi/ArrayRef.h>
18 #include <wpi/SmallVector.h>
19 #include <wpi/StringRef.h>
20 #include <wpi/Twine.h>
21 
22 #include "cscore_c.h"
23 
24 #ifdef _WIN32
25 // Disable uninitialized variable warnings
26 #pragma warning(push)
27 #pragma warning(disable : 26495)
28 #endif
29 
30 namespace wpi {
31 class json;
32 } // namespace wpi
33 
35 namespace cs {
36 
50 struct UsbCameraInfo {
52  int dev = -1;
54  std::string path;
56  std::string name;
58  std::vector<std::string> otherPaths;
60  int vendorId = -1;
62  int productId = -1;
63 };
64 
68 struct VideoMode : public CS_VideoMode {
69  enum PixelFormat {
70  kUnknown = CS_PIXFMT_UNKNOWN,
71  kMJPEG = CS_PIXFMT_MJPEG,
72  kYUYV = CS_PIXFMT_YUYV,
73  kRGB565 = CS_PIXFMT_RGB565,
74  kBGR = CS_PIXFMT_BGR,
75  kGray = CS_PIXFMT_GRAY
76  };
77  VideoMode() {
78  pixelFormat = 0;
79  width = 0;
80  height = 0;
81  fps = 0;
82  }
83  VideoMode(PixelFormat pixelFormat_, int width_, int height_, int fps_) {
84  pixelFormat = pixelFormat_;
85  width = width_;
86  height = height_;
87  fps = fps_;
88  }
89  explicit operator bool() const { return pixelFormat == kUnknown; }
90 
91  bool operator==(const VideoMode& other) const {
92  return pixelFormat == other.pixelFormat && width == other.width &&
93  height == other.height && fps == other.fps;
94  }
95 
96  bool operator!=(const VideoMode& other) const { return !(*this == other); }
97 };
98 
102 struct RawEvent {
103  enum Kind {
104  kSourceCreated = CS_SOURCE_CREATED,
105  kSourceDestroyed = CS_SOURCE_DESTROYED,
106  kSourceConnected = CS_SOURCE_CONNECTED,
107  kSourceDisconnected = CS_SOURCE_DISCONNECTED,
108  kSourceVideoModesUpdated = CS_SOURCE_VIDEOMODES_UPDATED,
109  kSourceVideoModeChanged = CS_SOURCE_VIDEOMODE_CHANGED,
110  kSourcePropertyCreated = CS_SOURCE_PROPERTY_CREATED,
111  kSourcePropertyValueUpdated = CS_SOURCE_PROPERTY_VALUE_UPDATED,
112  kSourcePropertyChoicesUpdated = CS_SOURCE_PROPERTY_CHOICES_UPDATED,
113  kSinkSourceChanged = CS_SINK_SOURCE_CHANGED,
114  kSinkCreated = CS_SINK_CREATED,
115  kSinkDestroyed = CS_SINK_DESTROYED,
116  kSinkEnabled = CS_SINK_ENABLED,
117  kSinkDisabled = CS_SINK_DISABLED,
118  kNetworkInterfacesChanged = CS_NETWORK_INTERFACES_CHANGED,
119  kTelemetryUpdated = CS_TELEMETRY_UPDATED,
120  kSinkPropertyCreated = CS_SINK_PROPERTY_CREATED,
121  kSinkPropertyValueUpdated = CS_SINK_PROPERTY_VALUE_UPDATED,
122  kSinkPropertyChoicesUpdated = CS_SINK_PROPERTY_CHOICES_UPDATED
123  };
124 
125  RawEvent() = default;
126  explicit RawEvent(RawEvent::Kind kind_) : kind{kind_} {}
127  RawEvent(const wpi::Twine& name_, CS_Handle handle_, RawEvent::Kind kind_)
128  : kind{kind_}, name{name_.str()} {
129  if (kind_ == kSinkCreated || kind_ == kSinkDestroyed ||
130  kind_ == kSinkEnabled || kind_ == kSinkDisabled)
131  sinkHandle = handle_;
132  else
133  sourceHandle = handle_;
134  }
135  RawEvent(const wpi::Twine& name_, CS_Source source_, const VideoMode& mode_)
136  : kind{kSourceVideoModeChanged},
137  sourceHandle{source_},
138  name{name_.str()},
139  mode{mode_} {}
140  RawEvent(const wpi::Twine& name_, CS_Source source_, RawEvent::Kind kind_,
141  CS_Property property_, CS_PropertyKind propertyKind_, int value_,
142  const wpi::Twine& valueStr_)
143  : kind{kind_},
144  sourceHandle{source_},
145  name{name_.str()},
146  propertyHandle{property_},
147  propertyKind{propertyKind_},
148  value{value_},
149  valueStr{valueStr_.str()} {}
150 
151  Kind kind;
152 
153  // Valid for kSource* and kSink* respectively
154  CS_Source sourceHandle = CS_INVALID_HANDLE;
155  CS_Sink sinkHandle = CS_INVALID_HANDLE;
156 
157  // Source/sink/property name
158  std::string name;
159 
160  // Fields for kSourceVideoModeChanged event
161  VideoMode mode;
162 
163  // Fields for kSourceProperty* events
164  CS_Property propertyHandle;
165  CS_PropertyKind propertyKind;
166  int value;
167  std::string valueStr;
168 };
169 
174 CS_PropertyKind GetPropertyKind(CS_Property property, CS_Status* status);
175 std::string GetPropertyName(CS_Property property, CS_Status* status);
176 wpi::StringRef GetPropertyName(CS_Property property,
178  CS_Status* status);
179 int GetProperty(CS_Property property, CS_Status* status);
180 void SetProperty(CS_Property property, int value, CS_Status* status);
181 int GetPropertyMin(CS_Property property, CS_Status* status);
182 int GetPropertyMax(CS_Property property, CS_Status* status);
183 int GetPropertyStep(CS_Property property, CS_Status* status);
184 int GetPropertyDefault(CS_Property property, CS_Status* status);
185 std::string GetStringProperty(CS_Property property, CS_Status* status);
186 wpi::StringRef GetStringProperty(CS_Property property,
188  CS_Status* status);
189 void SetStringProperty(CS_Property property, const wpi::Twine& value,
190  CS_Status* status);
191 std::vector<std::string> GetEnumPropertyChoices(CS_Property property,
192  CS_Status* status);
199 CS_Source CreateUsbCameraDev(const wpi::Twine& name, int dev,
200  CS_Status* status);
201 CS_Source CreateUsbCameraPath(const wpi::Twine& name, const wpi::Twine& path,
202  CS_Status* status);
203 CS_Source CreateHttpCamera(const wpi::Twine& name, const wpi::Twine& url,
204  CS_HttpCameraKind kind, CS_Status* status);
205 CS_Source CreateHttpCamera(const wpi::Twine& name,
207  CS_HttpCameraKind kind, CS_Status* status);
208 CS_Source CreateCvSource(const wpi::Twine& name, const VideoMode& mode,
209  CS_Status* status);
216 CS_SourceKind GetSourceKind(CS_Source source, CS_Status* status);
217 std::string GetSourceName(CS_Source source, CS_Status* status);
218 wpi::StringRef GetSourceName(CS_Source source, wpi::SmallVectorImpl<char>& buf,
219  CS_Status* status);
220 std::string GetSourceDescription(CS_Source source, CS_Status* status);
221 wpi::StringRef GetSourceDescription(CS_Source source,
223  CS_Status* status);
224 uint64_t GetSourceLastFrameTime(CS_Source source, CS_Status* status);
225 void SetSourceConnectionStrategy(CS_Source source,
226  CS_ConnectionStrategy strategy,
227  CS_Status* status);
228 bool IsSourceConnected(CS_Source source, CS_Status* status);
229 bool IsSourceEnabled(CS_Source source, CS_Status* status);
230 CS_Property GetSourceProperty(CS_Source source, const wpi::Twine& name,
231  CS_Status* status);
232 wpi::ArrayRef<CS_Property> EnumerateSourceProperties(
233  CS_Source source, wpi::SmallVectorImpl<CS_Property>& vec,
234  CS_Status* status);
235 VideoMode GetSourceVideoMode(CS_Source source, CS_Status* status);
236 bool SetSourceVideoMode(CS_Source source, const VideoMode& mode,
237  CS_Status* status);
238 bool SetSourcePixelFormat(CS_Source source, VideoMode::PixelFormat pixelFormat,
239  CS_Status* status);
240 bool SetSourceResolution(CS_Source source, int width, int height,
241  CS_Status* status);
242 bool SetSourceFPS(CS_Source source, int fps, CS_Status* status);
243 bool SetSourceConfigJson(CS_Source source, wpi::StringRef config,
244  CS_Status* status);
245 bool SetSourceConfigJson(CS_Source source, const wpi::json& config,
246  CS_Status* status);
247 std::string GetSourceConfigJson(CS_Source source, CS_Status* status);
248 wpi::json GetSourceConfigJsonObject(CS_Source source, CS_Status* status);
249 std::vector<VideoMode> EnumerateSourceVideoModes(CS_Source source,
250  CS_Status* status);
251 wpi::ArrayRef<CS_Sink> EnumerateSourceSinks(CS_Source source,
253  CS_Status* status);
254 CS_Source CopySource(CS_Source source, CS_Status* status);
255 void ReleaseSource(CS_Source source, CS_Status* status);
262 void SetCameraBrightness(CS_Source source, int brightness, CS_Status* status);
263 int GetCameraBrightness(CS_Source source, CS_Status* status);
264 void SetCameraWhiteBalanceAuto(CS_Source source, CS_Status* status);
265 void SetCameraWhiteBalanceHoldCurrent(CS_Source source, CS_Status* status);
266 void SetCameraWhiteBalanceManual(CS_Source source, int value,
267  CS_Status* status);
268 void SetCameraExposureAuto(CS_Source source, CS_Status* status);
269 void SetCameraExposureHoldCurrent(CS_Source source, CS_Status* status);
270 void SetCameraExposureManual(CS_Source source, int value, CS_Status* status);
277 std::string GetUsbCameraPath(CS_Source source, CS_Status* status);
278 UsbCameraInfo GetUsbCameraInfo(CS_Source source, CS_Status* status);
285 CS_HttpCameraKind GetHttpCameraKind(CS_Source source, CS_Status* status);
286 void SetHttpCameraUrls(CS_Source source, wpi::ArrayRef<std::string> urls,
287  CS_Status* status);
288 std::vector<std::string> GetHttpCameraUrls(CS_Source source, CS_Status* status);
295 void NotifySourceError(CS_Source source, const wpi::Twine& msg,
296  CS_Status* status);
297 void SetSourceConnected(CS_Source source, bool connected, CS_Status* status);
298 void SetSourceDescription(CS_Source source, const wpi::Twine& description,
299  CS_Status* status);
300 CS_Property CreateSourceProperty(CS_Source source, const wpi::Twine& name,
301  CS_PropertyKind kind, int minimum, int maximum,
302  int step, int defaultValue, int value,
303  CS_Status* status);
304 void SetSourceEnumPropertyChoices(CS_Source source, CS_Property property,
306  CS_Status* status);
313 CS_Sink CreateMjpegServer(const wpi::Twine& name,
314  const wpi::Twine& listenAddress, int port,
315  CS_Status* status);
316 CS_Sink CreateCvSink(const wpi::Twine& name, CS_Status* status);
317 CS_Sink CreateCvSinkCallback(const wpi::Twine& name,
318  std::function<void(uint64_t time)> processFrame,
319  CS_Status* status);
320 
327 CS_SinkKind GetSinkKind(CS_Sink sink, CS_Status* status);
328 std::string GetSinkName(CS_Sink sink, CS_Status* status);
329 wpi::StringRef GetSinkName(CS_Sink sink, wpi::SmallVectorImpl<char>& buf,
330  CS_Status* status);
331 std::string GetSinkDescription(CS_Sink sink, CS_Status* status);
332 wpi::StringRef GetSinkDescription(CS_Sink sink, wpi::SmallVectorImpl<char>& buf,
333  CS_Status* status);
334 CS_Property GetSinkProperty(CS_Sink sink, const wpi::Twine& name,
335  CS_Status* status);
336 wpi::ArrayRef<CS_Property> EnumerateSinkProperties(
337  CS_Sink sink, wpi::SmallVectorImpl<CS_Property>& vec, CS_Status* status);
338 void SetSinkSource(CS_Sink sink, CS_Source source, CS_Status* status);
339 CS_Property GetSinkSourceProperty(CS_Sink sink, const wpi::Twine& name,
340  CS_Status* status);
341 bool SetSinkConfigJson(CS_Sink sink, wpi::StringRef config, CS_Status* status);
342 bool SetSinkConfigJson(CS_Sink sink, const wpi::json& config,
343  CS_Status* status);
344 std::string GetSinkConfigJson(CS_Sink sink, CS_Status* status);
345 wpi::json GetSinkConfigJsonObject(CS_Sink sink, CS_Status* status);
346 CS_Source GetSinkSource(CS_Sink sink, CS_Status* status);
347 CS_Sink CopySink(CS_Sink sink, CS_Status* status);
348 void ReleaseSink(CS_Sink sink, CS_Status* status);
355 std::string GetMjpegServerListenAddress(CS_Sink sink, CS_Status* status);
356 int GetMjpegServerPort(CS_Sink sink, CS_Status* status);
363 void SetSinkDescription(CS_Sink sink, const wpi::Twine& description,
364  CS_Status* status);
365 std::string GetSinkError(CS_Sink sink, CS_Status* status);
366 wpi::StringRef GetSinkError(CS_Sink sink, wpi::SmallVectorImpl<char>& buf,
367  CS_Status* status);
368 void SetSinkEnabled(CS_Sink sink, bool enabled, CS_Status* status);
375 void SetListenerOnStart(std::function<void()> onStart);
376 void SetListenerOnExit(std::function<void()> onExit);
377 
378 CS_Listener AddListener(std::function<void(const RawEvent& event)> callback,
379  int eventMask, bool immediateNotify, CS_Status* status);
380 
381 void RemoveListener(CS_Listener handle, CS_Status* status);
384 bool NotifierDestroyed();
385 
390 void SetTelemetryPeriod(double seconds);
391 double GetTelemetryElapsedTime();
392 int64_t GetTelemetryValue(CS_Handle handle, CS_TelemetryKind kind,
393  CS_Status* status);
394 double GetTelemetryAverageValue(CS_Handle handle, CS_TelemetryKind kind,
395  CS_Status* status);
402 using LogFunc = std::function<void(unsigned int level, const char* file,
403  unsigned int line, const char* msg)>;
404 void SetLogger(LogFunc func, unsigned int min_level);
405 void SetDefaultLogger(unsigned int min_level);
412 void Shutdown();
419 std::vector<UsbCameraInfo> EnumerateUsbCameras(CS_Status* status);
420 
421 wpi::ArrayRef<CS_Source> EnumerateSourceHandles(
422  wpi::SmallVectorImpl<CS_Source>& vec, CS_Status* status);
423 wpi::ArrayRef<CS_Sink> EnumerateSinkHandles(wpi::SmallVectorImpl<CS_Sink>& vec,
424  CS_Status* status);
425 
426 std::string GetHostname();
427 
428 std::vector<std::string> GetNetworkInterfaces();
433 } // namespace cs
434 
435 #ifdef _WIN32
436 // Disable uninitialized variable warnings
437 #pragma warning(pop)
438 #endif
439 
440 #endif // CSCORE_CSCORE_CPP_H_
wpi::json
a class to store JSON values
Definition: json.h:2655
CS_TelemetryKind
CS_TelemetryKind
Telemetry kinds.
Definition: cscore_c.h:181
cs::VideoMode
Video mode.
Definition: cscore_cpp.h:68
cs::UsbCameraInfo::dev
int dev
Device number (e.g.
Definition: cscore_cpp.h:52
cs::RawEvent
Listener event.
Definition: cscore_cpp.h:102
wpi::ArrayRef
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:42
cs::UsbCameraInfo::path
std::string path
Path to device if available (e.g.
Definition: cscore_cpp.h:54
wpi::SmallVectorImpl< char >
CS_VideoMode
Video mode.
Definition: cscore_c.h:104
CS_HttpCameraKind
CS_HttpCameraKind
HTTP Camera kinds.
Definition: cscore_c.h:136
CS_SinkKind
CS_SinkKind
Sink kinds.
Definition: cscore_c.h:146
cs::UsbCameraInfo::productId
int productId
USB Product Id.
Definition: cscore_cpp.h:62
wpi
WPILib C++ utilities (wpiutil) namespace.
Definition: Endian.h:31
CS_ConnectionStrategy
CS_ConnectionStrategy
Connection strategy.
Definition: cscore_c.h:187
CS_SourceKind
CS_SourceKind
Source kinds.
Definition: cscore_c.h:125
cs
CameraServer (cscore) namespace.
Definition: cscore_raw_cv.h:17
cs::UsbCameraInfo
USB camera information.
Definition: cscore_cpp.h:50
cs::UsbCameraInfo::otherPaths
std::vector< std::string > otherPaths
Other path aliases to device (e.g.
Definition: cscore_cpp.h:58
wpi::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
cs::UsbCameraInfo::vendorId
int vendorId
USB Vendor Id.
Definition: cscore_cpp.h:60
wpi::Twine::str
std::string str() const
Return the twine contents as a std::string.
cs::UsbCameraInfo::name
std::string name
Vendor/model name of the camera as provided by the USB driver.
Definition: cscore_cpp.h:56
CS_PropertyKind
CS_PropertyKind
Property kinds.
Definition: cscore_c.h:114
wpi::Twine
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:85