WPILibC++  unspecified
cscore_oo.h
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) 2015-2018 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_OO_H_
9 #define CSCORE_CSCORE_OO_H_
10 
11 #include <initializer_list>
12 #include <string>
13 #include <utility>
14 #include <vector>
15 
16 #include "cscore_cpp.h"
17 
18 namespace cs {
19 
20 //
21 // Object-oriented interface
22 //
23 
24 // Forward declarations so friend declarations work correctly
25 class CvSource;
26 class VideoEvent;
27 class VideoSink;
28 class VideoSource;
29 
31  friend class CvSource;
32  friend class VideoEvent;
33  friend class VideoSink;
34  friend class VideoSource;
35 
36  public:
37  enum Kind {
38  kNone = CS_PROP_NONE,
39  kBoolean = CS_PROP_BOOLEAN,
40  kInteger = CS_PROP_INTEGER,
41  kString = CS_PROP_STRING,
42  kEnum = CS_PROP_ENUM
43  };
44 
45  VideoProperty() : m_handle(0), m_kind(kNone) {}
46 
47  std::string GetName() const;
48 
49  Kind GetKind() const { return m_kind; }
50 
51  explicit operator bool() const { return m_kind != kNone; }
52 
53  // Kind checkers
54  bool IsBoolean() const { return m_kind == kBoolean; }
55  bool IsInteger() const { return m_kind == kInteger; }
56  bool IsString() const { return m_kind == kString; }
57  bool IsEnum() const { return m_kind == kEnum; }
58 
59  int Get() const;
60  void Set(int value);
61  int GetMin() const;
62  int GetMax() const;
63  int GetStep() const;
64  int GetDefault() const;
65 
66  // String-specific functions
67  std::string GetString() const;
68  llvm::StringRef GetString(llvm::SmallVectorImpl<char>& buf) const;
69  void SetString(llvm::StringRef value);
70 
71  // Enum-specific functions
72  std::vector<std::string> GetChoices() const;
73 
74  CS_Status GetLastStatus() const { return m_status; }
75 
76  private:
77  explicit VideoProperty(CS_Property handle);
78  VideoProperty(CS_Property handle, Kind kind);
79 
80  mutable CS_Status m_status;
81  CS_Property m_handle;
82  Kind m_kind;
83 };
84 
86 class VideoSource {
87  friend class VideoEvent;
88  friend class VideoSink;
89 
90  public:
91  enum Kind {
92  kUnknown = CS_SOURCE_UNKNOWN,
93  kUsb = CS_SOURCE_USB,
94  kHttp = CS_SOURCE_HTTP,
95  kCv = CS_SOURCE_CV
96  };
97 
98  VideoSource() noexcept : m_handle(0) {}
99  VideoSource(const VideoSource& source);
100  VideoSource(VideoSource&& other) noexcept;
101  VideoSource& operator=(VideoSource other) noexcept;
102  ~VideoSource();
103 
104  explicit operator bool() const { return m_handle != 0; }
105 
106  int GetHandle() const { return m_handle; }
107 
108  bool operator==(const VideoSource& other) const {
109  return m_handle == other.m_handle;
110  }
111 
112  bool operator!=(const VideoSource& other) const { return !(*this == other); }
113 
115  Kind GetKind() const;
116 
119  std::string GetName() const;
120 
122  std::string GetDescription() const;
123 
127  uint64_t GetLastFrameTime() const;
128 
130  bool IsConnected() const;
131 
136  VideoProperty GetProperty(llvm::StringRef name);
137 
139  std::vector<VideoProperty> EnumerateProperties() const;
140 
142  VideoMode GetVideoMode() const;
143 
146  bool SetVideoMode(const VideoMode& mode);
147 
154  bool SetVideoMode(VideoMode::PixelFormat pixelFormat, int width, int height,
155  int fps);
156 
160  bool SetPixelFormat(VideoMode::PixelFormat pixelFormat);
161 
166  bool SetResolution(int width, int height);
167 
171  bool SetFPS(int fps);
172 
174  std::vector<VideoMode> EnumerateVideoModes() const;
175 
176  CS_Status GetLastStatus() const { return m_status; }
177 
180  std::vector<VideoSink> EnumerateSinks();
181 
184  static std::vector<VideoSource> EnumerateSources();
185 
186  friend void swap(VideoSource& first, VideoSource& second) noexcept {
187  using std::swap;
188  swap(first.m_status, second.m_status);
189  swap(first.m_handle, second.m_handle);
190  }
191 
192  protected:
193  explicit VideoSource(CS_Source handle) : m_handle(handle) {}
194 
195  mutable CS_Status m_status = 0;
196  CS_Source m_handle;
197 };
198 
200 class VideoCamera : public VideoSource {
201  public:
202  enum WhiteBalance {
203  kFixedIndoor = 3000,
204  kFixedOutdoor1 = 4000,
205  kFixedOutdoor2 = 5000,
206  kFixedFluorescent1 = 5100,
207  kFixedFlourescent2 = 5200
208  };
209 
210  VideoCamera() = default;
211 
213  void SetBrightness(int brightness);
214 
216  int GetBrightness();
217 
219  void SetWhiteBalanceAuto();
220 
222  void SetWhiteBalanceHoldCurrent();
223 
225  void SetWhiteBalanceManual(int value);
226 
228  void SetExposureAuto();
229 
231  void SetExposureHoldCurrent();
232 
234  void SetExposureManual(int value);
235 
236  protected:
237  explicit VideoCamera(CS_Source handle) : VideoSource(handle) {}
238 };
239 
241 class UsbCamera : public VideoCamera {
242  public:
243  UsbCamera() = default;
244 
248  UsbCamera(llvm::StringRef name, int dev);
249 
254 
257  static std::vector<UsbCameraInfo> EnumerateUsbCameras();
258 
260  std::string GetPath() const;
261 };
262 
264 class HttpCamera : public VideoCamera {
265  public:
266  enum HttpCameraKind {
267  kUnknown = CS_HTTP_UNKNOWN,
268  kMJPGStreamer = CS_HTTP_MJPGSTREAMER,
269  kCSCore = CS_HTTP_CSCORE,
270  kAxis = CS_HTTP_AXIS
271  };
272 
278  HttpCameraKind kind = kUnknown);
279 
284  HttpCamera(llvm::StringRef name, const char* url,
285  HttpCameraKind kind = kUnknown);
286 
291  HttpCamera(llvm::StringRef name, const std::string& url,
292  HttpCameraKind kind = kUnknown);
293 
299  HttpCameraKind kind = kUnknown);
300 
305  template <typename T>
306  HttpCamera(llvm::StringRef name, std::initializer_list<T> urls,
307  HttpCameraKind kind = kUnknown);
308 
312  HttpCameraKind GetHttpCameraKind() const;
313 
315  void SetUrls(llvm::ArrayRef<std::string> urls);
316 
318  template <typename T>
319  void SetUrls(std::initializer_list<T> urls);
320 
322  std::vector<std::string> GetUrls() const;
323 };
324 
326 class AxisCamera : public HttpCamera {
327  static std::string HostToUrl(llvm::StringRef host);
328  static std::vector<std::string> HostToUrl(llvm::ArrayRef<std::string> hosts);
329  template <typename T>
330  static std::vector<std::string> HostToUrl(std::initializer_list<T> hosts);
331 
332  public:
338 
343  AxisCamera(llvm::StringRef name, const char* host);
344 
349  AxisCamera(llvm::StringRef name, const std::string& host);
350 
356 
361  template <typename T>
362  AxisCamera(llvm::StringRef name, std::initializer_list<T> hosts);
363 };
364 
366 class CvSource : public VideoSource {
367  public:
368  CvSource() = default;
369 
373  CvSource(llvm::StringRef name, const VideoMode& mode);
374 
381  CvSource(llvm::StringRef name, VideoMode::PixelFormat pixelFormat, int width,
382  int height, int fps);
383 
389  void PutFrame(cv::Mat& image);
390 
393  void NotifyError(llvm::StringRef msg);
394 
397  void SetConnected(bool connected);
398 
401  void SetDescription(llvm::StringRef description);
402 
412  VideoProperty CreateProperty(llvm::StringRef name, VideoProperty::Kind kind,
413  int minimum, int maximum, int step,
414  int defaultValue, int value);
415 
424  VideoProperty CreateIntegerProperty(llvm::StringRef name, int minimum,
425  int maximum, int step, int defaultValue,
426  int value);
427 
433  VideoProperty CreateBooleanProperty(llvm::StringRef name, bool defaultValue,
434  bool value);
435 
441  VideoProperty CreateStringProperty(llvm::StringRef name,
442  llvm::StringRef value);
443 
447  void SetEnumPropertyChoices(const VideoProperty& property,
449 
453  template <typename T>
454  void SetEnumPropertyChoices(const VideoProperty& property,
455  std::initializer_list<T> choices);
456 };
457 
459 class VideoSink {
460  friend class VideoEvent;
461  friend class VideoSource;
462 
463  public:
464  enum Kind {
465  kUnknown = CS_SINK_UNKNOWN,
466  kMjpeg = CS_SINK_MJPEG,
467  kCv = CS_SINK_CV
468  };
469 
470  VideoSink() noexcept : m_handle(0) {}
471  VideoSink(const VideoSink& sink);
472  VideoSink(VideoSink&& sink) noexcept;
473  VideoSink& operator=(VideoSink other) noexcept;
474  ~VideoSink();
475 
476  explicit operator bool() const { return m_handle != 0; }
477 
478  int GetHandle() const { return m_handle; }
479 
480  bool operator==(const VideoSink& other) const {
481  return m_handle == other.m_handle;
482  }
483 
484  bool operator!=(const VideoSink& other) const { return !(*this == other); }
485 
487  Kind GetKind() const;
488 
491  std::string GetName() const;
492 
494  std::string GetDescription() const;
495 
500  void SetSource(VideoSource source);
501 
504  VideoSource GetSource() const;
505 
510  VideoProperty GetSourceProperty(llvm::StringRef name);
511 
512  CS_Status GetLastStatus() const { return m_status; }
513 
516  static std::vector<VideoSink> EnumerateSinks();
517 
518  friend void swap(VideoSink& first, VideoSink& second) noexcept {
519  using std::swap;
520  swap(first.m_status, second.m_status);
521  swap(first.m_handle, second.m_handle);
522  }
523 
524  protected:
525  explicit VideoSink(CS_Sink handle) : m_handle(handle) {}
526 
527  mutable CS_Status m_status = 0;
528  CS_Sink m_handle;
529 };
530 
532 class MjpegServer : public VideoSink {
533  public:
534  MjpegServer() = default;
535 
540  MjpegServer(llvm::StringRef name, llvm::StringRef listenAddress, int port);
541 
545  MjpegServer(llvm::StringRef name, int port) : MjpegServer(name, "", port) {}
546 
548  std::string GetListenAddress() const;
549 
551  int GetPort() const;
552 };
553 
555 class CvSink : public VideoSink {
556  public:
557  CvSink() = default;
558 
563  explicit CvSink(llvm::StringRef name);
564 
573  CvSink(llvm::StringRef name, std::function<void(uint64_t time)> processFrame);
574 
577  void SetDescription(llvm::StringRef description);
578 
585  uint64_t GrabFrame(cv::Mat& image, double timeout = 0.225) const;
586 
592  uint64_t GrabFrameNoTimeout(cv::Mat& image) const;
593 
596  std::string GetError() const;
597 
602  void SetEnabled(bool enabled);
603 };
604 
606 class VideoEvent : public RawEvent {
607  public:
609  VideoSource GetSource() const;
610 
612  VideoSink GetSink() const;
613 
615  VideoProperty GetProperty() const;
616 };
617 
621  public:
622  VideoListener() : m_handle(0) {}
623 
629  VideoListener(std::function<void(const VideoEvent& event)> callback,
630  int eventMask, bool immediateNotify);
631 
632  VideoListener(const VideoListener&) = delete;
633  VideoListener& operator=(const VideoListener&) = delete;
634  VideoListener(VideoListener&& other) noexcept;
635  VideoListener& operator=(VideoListener&& other) noexcept;
636  ~VideoListener();
637 
638  friend void swap(VideoListener& first, VideoListener& second) noexcept {
639  using std::swap;
640  swap(first.m_handle, second.m_handle);
641  }
642 
643  private:
644  CS_Listener m_handle;
645 };
646 
647 } // namespace cs
648 
649 #include "cscore_oo.inl"
650 
651 #endif // CSCORE_CSCORE_OO_H_
Definition: SinkImpl.h:19
A source for video that provides a sequence of frames.
Definition: cscore_oo.h:86
A sink for video that accepts a sequence of frames.
Definition: cscore_oo.h:459
An event generated by the library and provided to event listeners.
Definition: cscore_oo.h:606
Listener event.
Definition: cscore_cpp.h:71
A source that represents a USB camera.
Definition: cscore_oo.h:241
An event listener.
Definition: cscore_oo.h:620
A source that represents a MJPEG-over-HTTP (IP) camera.
Definition: cscore_oo.h:264
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: ArrayRef.h:32
bool IsConnected(NT_Inst inst)
Return whether or not the instance is connected to another node.
Definition: ntcore_cpp.cpp:904
Definition: cscore_oo.h:30
A source for user code to provide OpenCV images as video frames.
Definition: cscore_oo.h:366
MjpegServer(llvm::StringRef name, int port)
Create a MJPEG-over-HTTP server sink.
Definition: cscore_oo.h:545
A source that represents a video camera.
Definition: cscore_oo.h:200
A sink that acts as a MJPEG-over-HTTP network server.
Definition: cscore_oo.h:532
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:42
A source that represents an Axis IP camera.
Definition: cscore_oo.h:326
A sink for user code to accept video frames as OpenCV images.
Definition: cscore_oo.h:555
Video mode.
Definition: cscore_cpp.h:46