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  wpi::StringRef GetString(wpi::SmallVectorImpl<char>& buf) const;
69  void SetString(wpi::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(wpi::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 
176  double GetActualFPS() const;
177 
181  double GetActualDataRate() const;
182 
184  std::vector<VideoMode> EnumerateVideoModes() const;
185 
186  CS_Status GetLastStatus() const { return m_status; }
187 
190  std::vector<VideoSink> EnumerateSinks();
191 
194  static std::vector<VideoSource> EnumerateSources();
195 
196  friend void swap(VideoSource& first, VideoSource& second) noexcept {
197  using std::swap;
198  swap(first.m_status, second.m_status);
199  swap(first.m_handle, second.m_handle);
200  }
201 
202  protected:
203  explicit VideoSource(CS_Source handle) : m_handle(handle) {}
204 
205  mutable CS_Status m_status = 0;
206  CS_Source m_handle;
207 };
208 
210 class VideoCamera : public VideoSource {
211  public:
212  enum WhiteBalance {
213  kFixedIndoor = 3000,
214  kFixedOutdoor1 = 4000,
215  kFixedOutdoor2 = 5000,
216  kFixedFluorescent1 = 5100,
217  kFixedFlourescent2 = 5200
218  };
219 
220  VideoCamera() = default;
221 
223  void SetBrightness(int brightness);
224 
226  int GetBrightness();
227 
229  void SetWhiteBalanceAuto();
230 
232  void SetWhiteBalanceHoldCurrent();
233 
235  void SetWhiteBalanceManual(int value);
236 
238  void SetExposureAuto();
239 
241  void SetExposureHoldCurrent();
242 
244  void SetExposureManual(int value);
245 
246  protected:
247  explicit VideoCamera(CS_Source handle) : VideoSource(handle) {}
248 };
249 
251 class UsbCamera : public VideoCamera {
252  public:
253  UsbCamera() = default;
254 
258  UsbCamera(wpi::StringRef name, int dev);
259 
264 
267  static std::vector<UsbCameraInfo> EnumerateUsbCameras();
268 
270  std::string GetPath() const;
271 };
272 
274 class HttpCamera : public VideoCamera {
275  public:
276  enum HttpCameraKind {
277  kUnknown = CS_HTTP_UNKNOWN,
278  kMJPGStreamer = CS_HTTP_MJPGSTREAMER,
279  kCSCore = CS_HTTP_CSCORE,
280  kAxis = CS_HTTP_AXIS
281  };
282 
288  HttpCameraKind kind = kUnknown);
289 
294  HttpCamera(wpi::StringRef name, const char* url,
295  HttpCameraKind kind = kUnknown);
296 
301  HttpCamera(wpi::StringRef name, const std::string& url,
302  HttpCameraKind kind = kUnknown);
303 
309  HttpCameraKind kind = kUnknown);
310 
315  template <typename T>
316  HttpCamera(wpi::StringRef name, std::initializer_list<T> urls,
317  HttpCameraKind kind = kUnknown);
318 
322  HttpCameraKind GetHttpCameraKind() const;
323 
325  void SetUrls(wpi::ArrayRef<std::string> urls);
326 
328  template <typename T>
329  void SetUrls(std::initializer_list<T> urls);
330 
332  std::vector<std::string> GetUrls() const;
333 };
334 
336 class AxisCamera : public HttpCamera {
337  static std::string HostToUrl(wpi::StringRef host);
338  static std::vector<std::string> HostToUrl(wpi::ArrayRef<std::string> hosts);
339  template <typename T>
340  static std::vector<std::string> HostToUrl(std::initializer_list<T> hosts);
341 
342  public:
348 
353  AxisCamera(wpi::StringRef name, const char* host);
354 
359  AxisCamera(wpi::StringRef name, const std::string& host);
360 
366 
371  template <typename T>
372  AxisCamera(wpi::StringRef name, std::initializer_list<T> hosts);
373 };
374 
376 class CvSource : public VideoSource {
377  public:
378  CvSource() = default;
379 
383  CvSource(wpi::StringRef name, const VideoMode& mode);
384 
391  CvSource(wpi::StringRef name, VideoMode::PixelFormat pixelFormat, int width,
392  int height, int fps);
393 
399  void PutFrame(cv::Mat& image);
400 
403  void NotifyError(wpi::StringRef msg);
404 
407  void SetConnected(bool connected);
408 
411  void SetDescription(wpi::StringRef description);
412 
422  VideoProperty CreateProperty(wpi::StringRef name, VideoProperty::Kind kind,
423  int minimum, int maximum, int step,
424  int defaultValue, int value);
425 
434  VideoProperty CreateIntegerProperty(wpi::StringRef name, int minimum,
435  int maximum, int step, int defaultValue,
436  int value);
437 
443  VideoProperty CreateBooleanProperty(wpi::StringRef name, bool defaultValue,
444  bool value);
445 
451  VideoProperty CreateStringProperty(wpi::StringRef name, wpi::StringRef value);
452 
456  void SetEnumPropertyChoices(const VideoProperty& property,
458 
462  template <typename T>
463  void SetEnumPropertyChoices(const VideoProperty& property,
464  std::initializer_list<T> choices);
465 };
466 
468 class VideoSink {
469  friend class VideoEvent;
470  friend class VideoSource;
471 
472  public:
473  enum Kind {
474  kUnknown = CS_SINK_UNKNOWN,
475  kMjpeg = CS_SINK_MJPEG,
476  kCv = CS_SINK_CV
477  };
478 
479  VideoSink() noexcept : m_handle(0) {}
480  VideoSink(const VideoSink& sink);
481  VideoSink(VideoSink&& sink) noexcept;
482  VideoSink& operator=(VideoSink other) noexcept;
483  ~VideoSink();
484 
485  explicit operator bool() const { return m_handle != 0; }
486 
487  int GetHandle() const { return m_handle; }
488 
489  bool operator==(const VideoSink& other) const {
490  return m_handle == other.m_handle;
491  }
492 
493  bool operator!=(const VideoSink& other) const { return !(*this == other); }
494 
496  Kind GetKind() const;
497 
500  std::string GetName() const;
501 
503  std::string GetDescription() const;
504 
509  void SetSource(VideoSource source);
510 
513  VideoSource GetSource() const;
514 
519  VideoProperty GetSourceProperty(wpi::StringRef name);
520 
521  CS_Status GetLastStatus() const { return m_status; }
522 
525  static std::vector<VideoSink> EnumerateSinks();
526 
527  friend void swap(VideoSink& first, VideoSink& second) noexcept {
528  using std::swap;
529  swap(first.m_status, second.m_status);
530  swap(first.m_handle, second.m_handle);
531  }
532 
533  protected:
534  explicit VideoSink(CS_Sink handle) : m_handle(handle) {}
535 
536  mutable CS_Status m_status = 0;
537  CS_Sink m_handle;
538 };
539 
541 class MjpegServer : public VideoSink {
542  public:
543  MjpegServer() = default;
544 
549  MjpegServer(wpi::StringRef name, wpi::StringRef listenAddress, int port);
550 
554  MjpegServer(wpi::StringRef name, int port) : MjpegServer(name, "", port) {}
555 
557  std::string GetListenAddress() const;
558 
560  int GetPort() const;
561 };
562 
564 class CvSink : public VideoSink {
565  public:
566  CvSink() = default;
567 
572  explicit CvSink(wpi::StringRef name);
573 
582  CvSink(wpi::StringRef name, std::function<void(uint64_t time)> processFrame);
583 
586  void SetDescription(wpi::StringRef description);
587 
594  uint64_t GrabFrame(cv::Mat& image, double timeout = 0.225) const;
595 
601  uint64_t GrabFrameNoTimeout(cv::Mat& image) const;
602 
605  std::string GetError() const;
606 
611  void SetEnabled(bool enabled);
612 };
613 
615 class VideoEvent : public RawEvent {
616  public:
618  VideoSource GetSource() const;
619 
621  VideoSink GetSink() const;
622 
624  VideoProperty GetProperty() const;
625 };
626 
630  public:
631  VideoListener() : m_handle(0) {}
632 
638  VideoListener(std::function<void(const VideoEvent& event)> callback,
639  int eventMask, bool immediateNotify);
640 
641  VideoListener(const VideoListener&) = delete;
642  VideoListener& operator=(const VideoListener&) = delete;
643  VideoListener(VideoListener&& other) noexcept;
644  VideoListener& operator=(VideoListener&& other) noexcept;
645  ~VideoListener();
646 
647  friend void swap(VideoListener& first, VideoListener& second) noexcept {
648  using std::swap;
649  swap(first.m_handle, second.m_handle);
650  }
651 
652  private:
653  CS_Listener m_handle;
654 };
655 
656 } // namespace cs
657 
658 #include "cscore_oo.inl"
659 
660 #endif // CSCORE_CSCORE_OO_H_
Definition: CvSourceImpl.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:468
An event generated by the library and provided to event listeners.
Definition: cscore_oo.h:615
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: ArrayRef.h:41
Listener event.
Definition: cscore_cpp.h:71
A source that represents a USB camera.
Definition: cscore_oo.h:251
An event listener.
Definition: cscore_oo.h:629
A source that represents a MJPEG-over-HTTP (IP) camera.
Definition: cscore_oo.h:274
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
MjpegServer(wpi::StringRef name, int port)
Create a MJPEG-over-HTTP server sink.
Definition: cscore_oo.h:554
A source for user code to provide OpenCV images as video frames.
Definition: cscore_oo.h:376
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
A source that represents a video camera.
Definition: cscore_oo.h:210
A sink that acts as a MJPEG-over-HTTP network server.
Definition: cscore_oo.h:541
A source that represents an Axis IP camera.
Definition: cscore_oo.h:336
A sink for user code to accept video frames as OpenCV images.
Definition: cscore_oo.h:564
Video mode.
Definition: cscore_cpp.h:46