WPILibC++  2018.4.1-20180729184725-1146-g6db5f80
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
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 
30 // Forward declarations so friend declarations work correctly
31 class CvSource;
32 class VideoEvent;
33 class VideoSink;
34 class VideoSource;
35 
40  friend class CvSource;
41  friend class VideoEvent;
42  friend class VideoSink;
43  friend class VideoSource;
44 
45  public:
46  enum Kind {
47  kNone = CS_PROP_NONE,
48  kBoolean = CS_PROP_BOOLEAN,
49  kInteger = CS_PROP_INTEGER,
50  kString = CS_PROP_STRING,
51  kEnum = CS_PROP_ENUM
52  };
53 
54  VideoProperty() : m_handle(0), m_kind(kNone) {}
55 
56  std::string GetName() const;
57 
58  Kind GetKind() const { return m_kind; }
59 
60  explicit operator bool() const { return m_kind != kNone; }
61 
62  // Kind checkers
63  bool IsBoolean() const { return m_kind == kBoolean; }
64  bool IsInteger() const { return m_kind == kInteger; }
65  bool IsString() const { return m_kind == kString; }
66  bool IsEnum() const { return m_kind == kEnum; }
67 
68  int Get() const;
69  void Set(int value);
70  int GetMin() const;
71  int GetMax() const;
72  int GetStep() const;
73  int GetDefault() const;
74 
75  // String-specific functions
76  std::string GetString() const;
77  wpi::StringRef GetString(wpi::SmallVectorImpl<char>& buf) const;
78  void SetString(const wpi::Twine& value);
79 
80  // Enum-specific functions
81  std::vector<std::string> GetChoices() const;
82 
83  CS_Status GetLastStatus() const { return m_status; }
84 
85  private:
86  explicit VideoProperty(CS_Property handle);
87  VideoProperty(CS_Property handle, Kind kind);
88 
89  mutable CS_Status m_status;
90  CS_Property m_handle;
91  Kind m_kind;
92 };
93 
97 class VideoSource {
98  friend class VideoEvent;
99  friend class VideoSink;
100 
101  public:
102  enum Kind {
103  kUnknown = CS_SOURCE_UNKNOWN,
104  kUsb = CS_SOURCE_USB,
105  kHttp = CS_SOURCE_HTTP,
106  kCv = CS_SOURCE_CV
107  };
108 
109  VideoSource() noexcept : m_handle(0) {}
110  VideoSource(const VideoSource& source);
111  VideoSource(VideoSource&& other) noexcept;
112  VideoSource& operator=(VideoSource other) noexcept;
113  ~VideoSource();
114 
115  explicit operator bool() const { return m_handle != 0; }
116 
117  int GetHandle() const { return m_handle; }
118 
119  bool operator==(const VideoSource& other) const {
120  return m_handle == other.m_handle;
121  }
122 
123  bool operator!=(const VideoSource& other) const { return !(*this == other); }
124 
128  Kind GetKind() const;
129 
134  std::string GetName() const;
135 
139  std::string GetDescription() const;
140 
147  uint64_t GetLastFrameTime() const;
148 
152  bool IsConnected() const;
153 
160  VideoProperty GetProperty(const wpi::Twine& name);
161 
165  std::vector<VideoProperty> EnumerateProperties() const;
166 
170  VideoMode GetVideoMode() const;
171 
177  bool SetVideoMode(const VideoMode& mode);
178 
188  bool SetVideoMode(VideoMode::PixelFormat pixelFormat, int width, int height,
189  int fps);
190 
197  bool SetPixelFormat(VideoMode::PixelFormat pixelFormat);
198 
206  bool SetResolution(int width, int height);
207 
214  bool SetFPS(int fps);
215 
223  double GetActualFPS() const;
224 
232  double GetActualDataRate() const;
233 
237  std::vector<VideoMode> EnumerateVideoModes() const;
238 
239  CS_Status GetLastStatus() const { return m_status; }
240 
246  std::vector<VideoSink> EnumerateSinks();
247 
253  static std::vector<VideoSource> EnumerateSources();
254 
255  friend void swap(VideoSource& first, VideoSource& second) noexcept {
256  using std::swap;
257  swap(first.m_status, second.m_status);
258  swap(first.m_handle, second.m_handle);
259  }
260 
261  protected:
262  explicit VideoSource(CS_Source handle) : m_handle(handle) {}
263 
264  mutable CS_Status m_status = 0;
265  CS_Source m_handle;
266 };
267 
271 class VideoCamera : public VideoSource {
272  public:
273  enum WhiteBalance {
274  kFixedIndoor = 3000,
275  kFixedOutdoor1 = 4000,
276  kFixedOutdoor2 = 5000,
277  kFixedFluorescent1 = 5100,
278  kFixedFlourescent2 = 5200
279  };
280 
281  VideoCamera() = default;
282 
286  void SetBrightness(int brightness);
287 
291  int GetBrightness();
292 
296  void SetWhiteBalanceAuto();
297 
302 
306  void SetWhiteBalanceManual(int value);
307 
311  void SetExposureAuto();
312 
316  void SetExposureHoldCurrent();
317 
321  void SetExposureManual(int value);
322 
323  protected:
324  explicit VideoCamera(CS_Source handle) : VideoSource(handle) {}
325 };
326 
330 class UsbCamera : public VideoCamera {
331  public:
332  UsbCamera() = default;
333 
340  UsbCamera(const wpi::Twine& name, int dev);
341 
348  UsbCamera(const wpi::Twine& name, const wpi::Twine& path);
349 
355  static std::vector<UsbCameraInfo> EnumerateUsbCameras();
356 
360  std::string GetPath() const;
361 
367  void SetConnectVerbose(int level);
368 };
369 
373 class HttpCamera : public VideoCamera {
374  public:
375  enum HttpCameraKind {
376  kUnknown = CS_HTTP_UNKNOWN,
377  kMJPGStreamer = CS_HTTP_MJPGSTREAMER,
378  kCSCore = CS_HTTP_CSCORE,
379  kAxis = CS_HTTP_AXIS
380  };
381 
389  HttpCamera(const wpi::Twine& name, const wpi::Twine& url,
390  HttpCameraKind kind = kUnknown);
391 
399  HttpCamera(const wpi::Twine& name, const char* url,
400  HttpCameraKind kind = kUnknown);
401 
409  HttpCamera(const wpi::Twine& name, const std::string& url,
410  HttpCameraKind kind = kUnknown);
411 
420  HttpCameraKind kind = kUnknown);
421 
429  template <typename T>
430  HttpCamera(const wpi::Twine& name, std::initializer_list<T> urls,
431  HttpCameraKind kind = kUnknown);
432 
439  HttpCameraKind GetHttpCameraKind() const;
440 
445 
449  template <typename T>
450  void SetUrls(std::initializer_list<T> urls);
451 
455  std::vector<std::string> GetUrls() const;
456 };
457 
461 class AxisCamera : public HttpCamera {
462  static std::string HostToUrl(const wpi::Twine& host);
463  static std::vector<std::string> HostToUrl(wpi::ArrayRef<std::string> hosts);
464  template <typename T>
465  static std::vector<std::string> HostToUrl(std::initializer_list<T> hosts);
466 
467  public:
475  AxisCamera(const wpi::Twine& name, const wpi::Twine& host);
476 
484  AxisCamera(const wpi::Twine& name, const char* host);
485 
493  AxisCamera(const wpi::Twine& name, const std::string& host);
494 
503 
511  template <typename T>
512  AxisCamera(const wpi::Twine& name, std::initializer_list<T> hosts);
513 };
514 
518 class CvSource : public VideoSource {
519  public:
520  CvSource() = default;
521 
528  CvSource(const wpi::Twine& name, const VideoMode& mode);
529 
539  CvSource(const wpi::Twine& name, VideoMode::PixelFormat pixelFormat,
540  int width, int height, int fps);
541 
551  void PutFrame(cv::Mat& image);
552 
557  void NotifyError(const wpi::Twine& msg);
558 
564  void SetConnected(bool connected);
565 
571  void SetDescription(const wpi::Twine& description);
572 
585  VideoProperty CreateProperty(const wpi::Twine& name, VideoProperty::Kind kind,
586  int minimum, int maximum, int step,
587  int defaultValue, int value);
588 
600  VideoProperty CreateIntegerProperty(const wpi::Twine& name, int minimum,
601  int maximum, int step, int defaultValue,
602  int value);
603 
612  VideoProperty CreateBooleanProperty(const wpi::Twine& name, bool defaultValue,
613  bool value);
614 
624  const wpi::Twine& value);
625 
632  void SetEnumPropertyChoices(const VideoProperty& property,
634 
641  template <typename T>
642  void SetEnumPropertyChoices(const VideoProperty& property,
643  std::initializer_list<T> choices);
644 };
645 
649 class VideoSink {
650  friend class VideoEvent;
651  friend class VideoSource;
652 
653  public:
654  enum Kind {
655  kUnknown = CS_SINK_UNKNOWN,
656  kMjpeg = CS_SINK_MJPEG,
657  kCv = CS_SINK_CV
658  };
659 
660  VideoSink() noexcept : m_handle(0) {}
661  VideoSink(const VideoSink& sink);
662  VideoSink(VideoSink&& sink) noexcept;
663  VideoSink& operator=(VideoSink other) noexcept;
664  ~VideoSink();
665 
666  explicit operator bool() const { return m_handle != 0; }
667 
668  int GetHandle() const { return m_handle; }
669 
670  bool operator==(const VideoSink& other) const {
671  return m_handle == other.m_handle;
672  }
673 
674  bool operator!=(const VideoSink& other) const { return !(*this == other); }
675 
679  Kind GetKind() const;
680 
685  std::string GetName() const;
686 
690  std::string GetDescription() const;
691 
699  VideoProperty GetProperty(const wpi::Twine& name);
700 
704  std::vector<VideoProperty> EnumerateProperties() const;
705 
713  void SetSource(VideoSource source);
714 
720  VideoSource GetSource() const;
721 
730 
731  CS_Status GetLastStatus() const { return m_status; }
732 
738  static std::vector<VideoSink> EnumerateSinks();
739 
740  friend void swap(VideoSink& first, VideoSink& second) noexcept {
741  using std::swap;
742  swap(first.m_status, second.m_status);
743  swap(first.m_handle, second.m_handle);
744  }
745 
746  protected:
747  explicit VideoSink(CS_Sink handle) : m_handle(handle) {}
748 
749  mutable CS_Status m_status = 0;
750  CS_Sink m_handle;
751 };
752 
756 class MjpegServer : public VideoSink {
757  public:
758  MjpegServer() = default;
759 
767  MjpegServer(const wpi::Twine& name, const wpi::Twine& listenAddress,
768  int port);
769 
776  MjpegServer(const wpi::Twine& name, int port) : MjpegServer(name, "", port) {}
777 
781  std::string GetListenAddress() const;
782 
786  int GetPort() const;
787 
802  void SetResolution(int width, int height);
803 
811  void SetFPS(int fps);
812 
822  void SetCompression(int quality);
823 
831  void SetDefaultCompression(int quality);
832 };
833 
837 class CvSink : public VideoSink {
838  public:
839  CvSink() = default;
840 
849  explicit CvSink(const wpi::Twine& name);
850 
863  CvSink(const wpi::Twine& name,
864  std::function<void(uint64_t time)> processFrame);
865 
871  void SetDescription(const wpi::Twine& description);
872 
882  uint64_t GrabFrame(cv::Mat& image, double timeout = 0.225) const;
883 
892  uint64_t GrabFrameNoTimeout(cv::Mat& image) const;
893 
898  std::string GetError() const;
899 
907  void SetEnabled(bool enabled);
908 };
909 
913 class VideoEvent : public RawEvent {
914  public:
918  VideoSource GetSource() const;
919 
923  VideoSink GetSink() const;
924 
928  VideoProperty GetProperty() const;
929 };
930 
936  public:
937  VideoListener() : m_handle(0) {}
938 
947  VideoListener(std::function<void(const VideoEvent& event)> callback,
948  int eventMask, bool immediateNotify);
949 
950  VideoListener(const VideoListener&) = delete;
951  VideoListener& operator=(const VideoListener&) = delete;
952  VideoListener(VideoListener&& other) noexcept;
953  VideoListener& operator=(VideoListener&& other) noexcept;
954  ~VideoListener();
955 
956  friend void swap(VideoListener& first, VideoListener& second) noexcept {
957  using std::swap;
958  swap(first.m_handle, second.m_handle);
959  }
960 
961  private:
962  CS_Listener m_handle;
963 };
964 
967 } // namespace cs
968 
969 #include "cscore_oo.inl"
970 
971 #endif // CSCORE_CSCORE_OO_H_
int GetPort() const
Get the port number of the server.
Definition: cscore_oo.inl:499
void SetWhiteBalanceManual(int value)
Set the white balance to manual, with specified color temperature.
Definition: cscore_oo.inl:198
void SetFPS(int fps)
Set the stream frames per second (FPS) for clients that don't specify it.
Definition: cscore_oo.inl:511
CameraServer (cscore) namespace.
Definition: cscore_oo.inl:11
VideoSource GetSource() const
Get the connected source.
Definition: cscore_oo.inl:478
void SetCompression(int quality)
Set the compression for clients that don't specify it.
Definition: cscore_oo.inl:516
void SetDescription(const wpi::Twine &description)
Set source description.
Definition: cscore_oo.inl:371
void SetSource(VideoSource source)
Configure which source should provide frames to this sink.
Definition: cscore_oo.inl:470
A source for video that provides a sequence of frames.
Definition: cscore_oo.h:97
VideoSink GetSink() const
Get the sink associated with the event (if any).
Definition: cscore_oo.inl:567
A sink for video that accepts a sequence of frames.
Definition: cscore_oo.h:649
void SetEnumPropertyChoices(const VideoProperty &property, wpi::ArrayRef< std::string > choices)
Configure enum property choices.
Definition: cscore_oo.inl:417
VideoProperty GetSourceProperty(const wpi::Twine &name)
Get a property of the associated source.
Definition: cscore_oo.inl:484
void SetExposureHoldCurrent()
Set the exposure to hold current.
Definition: cscore_oo.inl:208
std::vector< VideoMode > EnumerateVideoModes() const
Enumerate all known video modes for this source.
Definition: cscore_oo.inl:173
std::vector< VideoProperty > EnumerateProperties() const
Enumerate all properties of this sink.
An event generated by the library and provided to event listeners.
Definition: cscore_oo.h:913
HttpCamera(const wpi::Twine &name, const wpi::Twine &url, HttpCameraKind kind=kUnknown)
Create a source for a MJPEG-over-HTTP (IP) camera.
Definition: cscore_oo.inl:242
VideoProperty GetProperty(const wpi::Twine &name)
Get a property of the sink.
Definition: cscore_oo.inl:465
bool IsConnected() const
Is the source currently connected to whatever is providing the images?
Definition: cscore_oo.inl:119
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:83
std::vector< std::string > GetUrls() const
Get the URLs used to connect to the camera.
Definition: cscore_oo.inl:300
A source that represents a USB camera.
Definition: cscore_oo.h:330
AxisCamera(const wpi::Twine &name, const wpi::Twine &host)
Create a source for an Axis IP camera.
Definition: cscore_oo.inl:328
An event listener.
Definition: cscore_oo.h:935
uint64_t GrabFrame(cv::Mat &image, double timeout=0.225) const
Wait for the next frame and get the image.
Definition: cscore_oo.inl:542
VideoProperty CreateIntegerProperty(const wpi::Twine &name, int minimum, int maximum, int step, int defaultValue, int value)
Create an integer property.
Definition: cscore_oo.inl:387
A source that represents a MJPEG-over-HTTP (IP) camera.
Definition: cscore_oo.h:373
std::vector< VideoProperty > EnumerateProperties() const
Enumerate all properties of this source.
HttpCameraKind GetHttpCameraKind() const
Get the kind of HTTP camera.
Definition: cscore_oo.inl:280
void SetExposureManual(int value)
Set the exposure to manual, as a percentage (0-100).
Definition: cscore_oo.inl:213
double GetActualFPS() const
Get the actual FPS.
Definition: cscore_oo.inl:161
void SetDescription(const wpi::Twine &description)
Set sink description.
Definition: cscore_oo.inl:537
std::string GetName() const
Get the name of the sink.
Definition: cscore_oo.inl:455
uint64_t GrabFrameNoTimeout(cv::Mat &image) const
Wait for the next frame and get the image.
Definition: cscore_oo.inl:547
A source or sink property.
Definition: cscore_oo.h:39
Kind GetKind() const
Get the kind of the source.
Definition: cscore_oo.inl:99
std::string GetDescription() const
Get the source description.
Definition: cscore_oo.inl:109
std::string GetName() const
Get the name of the source.
Definition: cscore_oo.inl:104
bool SetFPS(int fps)
Set the frames per second (FPS).
Definition: cscore_oo.inl:156
std::string GetError() const
Get error string.
Definition: cscore_oo.inl:552
static std::vector< VideoSink > EnumerateSinks()
Enumerate all existing sinks.
std::vector< VideoSink > EnumerateSinks()
Enumerate all sinks connected to this source.
void SetResolution(int width, int height)
Set the stream resolution for clients that don't specify it.
Definition: cscore_oo.inl:504
void SetWhiteBalanceHoldCurrent()
Set the white balance to hold current.
Definition: cscore_oo.inl:193
static std::vector< VideoSource > EnumerateSources()
Enumerate all existing sources.
bool SetResolution(int width, int height)
Set the resolution.
Definition: cscore_oo.inl:151
static std::vector< UsbCameraInfo > EnumerateUsbCameras()
Enumerate USB cameras on the local system.
Definition: cscore_oo.inl:226
A source for user code to provide OpenCV images as video frames.
Definition: cscore_oo.h:518
VideoProperty CreateStringProperty(const wpi::Twine &name, const wpi::Twine &value)
Create a string property.
Definition: cscore_oo.inl:406
void SetUrls(wpi::ArrayRef< std::string > urls)
Change the URLs used to connect to the camera.
Definition: cscore_oo.inl:286
void NotifyError(const wpi::Twine &msg)
Signal sinks that an error has occurred.
Definition: cscore_oo.inl:361
void SetDefaultCompression(int quality)
Set the default compression used for non-MJPEG sources.
Definition: cscore_oo.inl:522
std::string GetDescription() const
Get the sink description.
Definition: cscore_oo.inl:460
void SetExposureAuto()
Set the exposure to auto aperature.
Definition: cscore_oo.inl:203
VideoMode GetVideoMode() const
Get the current video mode.
Definition: cscore_oo.inl:129
VideoProperty CreateBooleanProperty(const wpi::Twine &name, bool defaultValue, bool value)
Create a boolean property.
Definition: cscore_oo.inl:397
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
MjpegServer(const wpi::Twine &name, int port)
Create a MJPEG-over-HTTP server sink.
Definition: cscore_oo.h:776
std::string GetPath() const
Get the path to the device.
Definition: cscore_oo.inl:231
A source that represents a video camera.
Definition: cscore_oo.h:271
Kind GetKind() const
Get the kind of the sink.
Definition: cscore_oo.inl:450
void SetEnabled(bool enabled)
Enable or disable getting new frames.
Definition: cscore_oo.inl:557
bool SetPixelFormat(VideoMode::PixelFormat pixelFormat)
Set the pixel format.
Definition: cscore_oo.inl:146
VideoProperty GetProperty(const wpi::Twine &name)
Get a property.
Definition: cscore_oo.inl:124
VideoProperty CreateProperty(const wpi::Twine &name, VideoProperty::Kind kind, int minimum, int maximum, int step, int defaultValue, int value)
Create a property.
Definition: cscore_oo.inl:376
VideoSource GetSource() const
Get the source associated with the event (if any).
Definition: cscore_oo.inl:562
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:79
void PutFrame(cv::Mat &image)
Put an OpenCV image and notify sinks.
Definition: cscore_oo.inl:356
A sink that acts as a MJPEG-over-HTTP network server.
Definition: cscore_oo.h:756
VideoProperty GetProperty() const
Get the property associated with the event (if any).
Definition: cscore_oo.inl:572
void SetConnectVerbose(int level)
Set how verbose the camera connection messages are.
Definition: cscore_oo.inl:236
A source that represents an Axis IP camera.
Definition: cscore_oo.h:461
void SetBrightness(int brightness)
Set the brightness, as a percentage (0-100).
Definition: cscore_oo.inl:178
A sink for user code to accept video frames as OpenCV images.
Definition: cscore_oo.h:837
double GetActualDataRate() const
Get the data rate (in bytes per second).
Definition: cscore_oo.inl:167
uint64_t GetLastFrameTime() const
Get the last time a frame was captured.
Definition: cscore_oo.inl:114
std::string GetListenAddress() const
Get the listen address of the server.
Definition: cscore_oo.inl:494
Video mode.
Definition: cscore_cpp.h:56
void SetWhiteBalanceAuto()
Set the white balance to auto.
Definition: cscore_oo.inl:188
void SetConnected(bool connected)
Set source connection status.
Definition: cscore_oo.inl:366
bool SetVideoMode(const VideoMode &mode)
Set the video mode.
Definition: cscore_oo.inl:134
int GetBrightness()
Get the brightness, as a percentage (0-100).
Definition: cscore_oo.inl:183