WPILibC++  unspecified
cscore_oo.h
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) 2015-2017 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 
125  uint64_t GetLastFrameTime() const;
126 
128  bool IsConnected() const;
129 
134  VideoProperty GetProperty(llvm::StringRef name);
135 
137  std::vector<VideoProperty> EnumerateProperties() const;
138 
140  VideoMode GetVideoMode() const;
141 
144  bool SetVideoMode(const VideoMode& mode);
145 
152  bool SetVideoMode(VideoMode::PixelFormat pixelFormat, int width, int height,
153  int fps);
154 
158  bool SetPixelFormat(VideoMode::PixelFormat pixelFormat);
159 
164  bool SetResolution(int width, int height);
165 
169  bool SetFPS(int fps);
170 
172  std::vector<VideoMode> EnumerateVideoModes() const;
173 
174  CS_Status GetLastStatus() const { return m_status; }
175 
178  std::vector<VideoSink> EnumerateSinks();
179 
182  static std::vector<VideoSource> EnumerateSources();
183 
184  friend void swap(VideoSource& first, VideoSource& second) noexcept {
185  using std::swap;
186  swap(first.m_status, second.m_status);
187  swap(first.m_handle, second.m_handle);
188  }
189 
190  protected:
191  explicit VideoSource(CS_Source handle) : m_handle(handle) {}
192 
193  mutable CS_Status m_status = 0;
194  CS_Source m_handle;
195 };
196 
198 class VideoCamera : public VideoSource {
199  public:
200  enum WhiteBalance {
201  kFixedIndoor = 3000,
202  kFixedOutdoor1 = 4000,
203  kFixedOutdoor2 = 5000,
204  kFixedFluorescent1 = 5100,
205  kFixedFlourescent2 = 5200
206  };
207 
208  VideoCamera() = default;
209 
211  void SetBrightness(int brightness);
212 
214  int GetBrightness();
215 
217  void SetWhiteBalanceAuto();
218 
220  void SetWhiteBalanceHoldCurrent();
221 
223  void SetWhiteBalanceManual(int value);
224 
226  void SetExposureAuto();
227 
229  void SetExposureHoldCurrent();
230 
232  void SetExposureManual(int value);
233 
234  protected:
235  explicit VideoCamera(CS_Source handle) : VideoSource(handle) {}
236 };
237 
239 class UsbCamera : public VideoCamera {
240  public:
241  UsbCamera() = default;
242 
246  UsbCamera(llvm::StringRef name, int dev);
247 
252 
255  static std::vector<UsbCameraInfo> EnumerateUsbCameras();
256 
258  std::string GetPath() const;
259 };
260 
262 class HttpCamera : public VideoCamera {
263  public:
264  enum HttpCameraKind {
265  kUnknown = CS_HTTP_UNKNOWN,
266  kMJPGStreamer = CS_HTTP_MJPGSTREAMER,
267  kCSCore = CS_HTTP_CSCORE,
268  kAxis = CS_HTTP_AXIS
269  };
270 
276  HttpCameraKind kind = kUnknown);
277 
282  HttpCamera(llvm::StringRef name, const char* url,
283  HttpCameraKind kind = kUnknown);
284 
289  HttpCamera(llvm::StringRef name, const std::string& url,
290  HttpCameraKind kind = kUnknown);
291 
297  HttpCameraKind kind = kUnknown);
298 
303  template <typename T>
304  HttpCamera(llvm::StringRef name, std::initializer_list<T> urls,
305  HttpCameraKind kind = kUnknown);
306 
310  HttpCameraKind GetHttpCameraKind() const;
311 
313  void SetUrls(llvm::ArrayRef<std::string> urls);
314 
316  template <typename T>
317  void SetUrls(std::initializer_list<T> urls);
318 
320  std::vector<std::string> GetUrls() const;
321 };
322 
324 class AxisCamera : public HttpCamera {
325  static std::string HostToUrl(llvm::StringRef host);
326  static std::vector<std::string> HostToUrl(llvm::ArrayRef<std::string> hosts);
327  template <typename T>
328  static std::vector<std::string> HostToUrl(std::initializer_list<T> hosts);
329 
330  public:
336 
341  AxisCamera(llvm::StringRef name, const char* host);
342 
347  AxisCamera(llvm::StringRef name, const std::string& host);
348 
354 
359  template <typename T>
360  AxisCamera(llvm::StringRef name, std::initializer_list<T> hosts);
361 };
362 
364 class CvSource : public VideoSource {
365  public:
366  CvSource() = default;
367 
371  CvSource(llvm::StringRef name, const VideoMode& mode);
372 
379  CvSource(llvm::StringRef name, VideoMode::PixelFormat pixelFormat, int width,
380  int height, int fps);
381 
387  void PutFrame(cv::Mat& image);
388 
391  void NotifyError(llvm::StringRef msg);
392 
395  void SetConnected(bool connected);
396 
399  void SetDescription(llvm::StringRef description);
400 
410  VideoProperty CreateProperty(llvm::StringRef name, VideoProperty::Kind kind,
411  int minimum, int maximum, int step,
412  int defaultValue, int value);
413 
422  VideoProperty CreateIntegerProperty(llvm::StringRef name, int minimum,
423  int maximum, int step, int defaultValue,
424  int value);
425 
431  VideoProperty CreateBooleanProperty(llvm::StringRef name, bool defaultValue,
432  bool value);
433 
439  VideoProperty CreateStringProperty(llvm::StringRef name,
440  llvm::StringRef value);
441 
445  void SetEnumPropertyChoices(const VideoProperty& property,
447 
451  template <typename T>
452  void SetEnumPropertyChoices(const VideoProperty& property,
453  std::initializer_list<T> choices);
454 };
455 
457 class VideoSink {
458  friend class VideoEvent;
459  friend class VideoSource;
460 
461  public:
462  enum Kind {
463  kUnknown = CS_SINK_UNKNOWN,
464  kMjpeg = CS_SINK_MJPEG,
465  kCv = CS_SINK_CV
466  };
467 
468  VideoSink() noexcept : m_handle(0) {}
469  VideoSink(const VideoSink& sink);
470  VideoSink(VideoSink&& sink) noexcept;
471  VideoSink& operator=(VideoSink other) noexcept;
472  ~VideoSink();
473 
474  explicit operator bool() const { return m_handle != 0; }
475 
476  int GetHandle() const { return m_handle; }
477 
478  bool operator==(const VideoSink& other) const {
479  return m_handle == other.m_handle;
480  }
481 
482  bool operator!=(const VideoSink& other) const { return !(*this == other); }
483 
485  Kind GetKind() const;
486 
489  std::string GetName() const;
490 
492  std::string GetDescription() const;
493 
498  void SetSource(VideoSource source);
499 
502  VideoSource GetSource() const;
503 
508  VideoProperty GetSourceProperty(llvm::StringRef name);
509 
510  CS_Status GetLastStatus() const { return m_status; }
511 
514  static std::vector<VideoSink> EnumerateSinks();
515 
516  friend void swap(VideoSink& first, VideoSink& second) noexcept {
517  using std::swap;
518  swap(first.m_status, second.m_status);
519  swap(first.m_handle, second.m_handle);
520  }
521 
522  protected:
523  explicit VideoSink(CS_Sink handle) : m_handle(handle) {}
524 
525  mutable CS_Status m_status = 0;
526  CS_Sink m_handle;
527 };
528 
530 class MjpegServer : public VideoSink {
531  public:
532  MjpegServer() = default;
533 
538  MjpegServer(llvm::StringRef name, llvm::StringRef listenAddress, int port);
539 
543  MjpegServer(llvm::StringRef name, int port) : MjpegServer(name, "", port) {}
544 
546  std::string GetListenAddress() const;
547 
549  int GetPort() const;
550 };
551 
553 class CvSink : public VideoSink {
554  public:
555  CvSink() = default;
556 
561  explicit CvSink(llvm::StringRef name);
562 
571  CvSink(llvm::StringRef name, std::function<void(uint64_t time)> processFrame);
572 
575  void SetDescription(llvm::StringRef description);
576 
582  uint64_t GrabFrame(cv::Mat& image, double timeout = 0.225) const;
583 
588  uint64_t GrabFrameNoTimeout(cv::Mat& image) const;
589 
592  std::string GetError() const;
593 
598  void SetEnabled(bool enabled);
599 };
600 
602 class VideoEvent : public RawEvent {
603  public:
605  VideoSource GetSource() const;
606 
608  VideoSink GetSink() const;
609 
611  VideoProperty GetProperty() const;
612 };
613 
617  public:
618  VideoListener() : m_handle(0) {}
619 
625  VideoListener(std::function<void(const VideoEvent& event)> callback,
626  int eventMask, bool immediateNotify);
627 
628  VideoListener(const VideoListener&) = delete;
629  VideoListener& operator=(const VideoListener&) = delete;
630  VideoListener(VideoListener&& other) noexcept;
631  VideoListener& operator=(VideoListener&& other) noexcept;
632  ~VideoListener();
633 
634  friend void swap(VideoListener& first, VideoListener& second) noexcept {
635  using std::swap;
636  swap(first.m_handle, second.m_handle);
637  }
638 
639  private:
640  CS_Listener m_handle;
641 };
642 
643 } // namespace cs
644 
645 #include "cscore_oo.inl"
646 
647 #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:457
An event generated by the library and provided to event listeners.
Definition: cscore_oo.h:602
Listener event.
Definition: cscore_cpp.h:71
A source that represents a USB camera.
Definition: cscore_oo.h:239
An event listener.
Definition: cscore_oo.h:616
A source that represents a MJPEG-over-HTTP (IP) camera.
Definition: cscore_oo.h:262
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:902
Definition: cscore_oo.h:30
A source for user code to provide OpenCV images as video frames.
Definition: cscore_oo.h:364
MjpegServer(llvm::StringRef name, int port)
Create a MJPEG-over-HTTP server sink.
Definition: cscore_oo.h:543
A source that represents a video camera.
Definition: cscore_oo.h:198
A sink that acts as a MJPEG-over-HTTP network server.
Definition: cscore_oo.h:530
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:324
A sink for user code to accept video frames as OpenCV images.
Definition: cscore_oo.h:553
Video mode.
Definition: cscore_cpp.h:46