WPILibC++  2019.1.1-beta-1-15-gd03b020
 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 
116 
122 
128  };
129 
130  VideoSource() noexcept : m_handle(0) {}
131  VideoSource(const VideoSource& source);
132  VideoSource(VideoSource&& other) noexcept;
133  VideoSource& operator=(VideoSource other) noexcept;
134  ~VideoSource();
135 
136  explicit operator bool() const { return m_handle != 0; }
137 
138  int GetHandle() const { return m_handle; }
139 
140  bool operator==(const VideoSource& other) const {
141  return m_handle == other.m_handle;
142  }
143 
144  bool operator!=(const VideoSource& other) const { return !(*this == other); }
145 
149  Kind GetKind() const;
150 
155  std::string GetName() const;
156 
160  std::string GetDescription() const;
161 
168  uint64_t GetLastFrameTime() const;
169 
180 
184  bool IsConnected() const;
185 
192  bool IsEnabled() const;
193 
200  VideoProperty GetProperty(const wpi::Twine& name);
201 
205  std::vector<VideoProperty> EnumerateProperties() const;
206 
210  VideoMode GetVideoMode() const;
211 
217  bool SetVideoMode(const VideoMode& mode);
218 
228  bool SetVideoMode(VideoMode::PixelFormat pixelFormat, int width, int height,
229  int fps);
230 
237  bool SetPixelFormat(VideoMode::PixelFormat pixelFormat);
238 
246  bool SetResolution(int width, int height);
247 
254  bool SetFPS(int fps);
255 
263  double GetActualFPS() const;
264 
272  double GetActualDataRate() const;
273 
277  std::vector<VideoMode> EnumerateVideoModes() const;
278 
279  CS_Status GetLastStatus() const { return m_status; }
280 
286  std::vector<VideoSink> EnumerateSinks();
287 
293  static std::vector<VideoSource> EnumerateSources();
294 
295  friend void swap(VideoSource& first, VideoSource& second) noexcept {
296  using std::swap;
297  swap(first.m_status, second.m_status);
298  swap(first.m_handle, second.m_handle);
299  }
300 
301  protected:
302  explicit VideoSource(CS_Source handle) : m_handle(handle) {}
303 
304  mutable CS_Status m_status = 0;
305  CS_Source m_handle;
306 };
307 
311 class VideoCamera : public VideoSource {
312  public:
313  enum WhiteBalance {
314  kFixedIndoor = 3000,
315  kFixedOutdoor1 = 4000,
316  kFixedOutdoor2 = 5000,
317  kFixedFluorescent1 = 5100,
318  kFixedFlourescent2 = 5200
319  };
320 
321  VideoCamera() = default;
322 
326  void SetBrightness(int brightness);
327 
331  int GetBrightness();
332 
336  void SetWhiteBalanceAuto();
337 
342 
346  void SetWhiteBalanceManual(int value);
347 
351  void SetExposureAuto();
352 
356  void SetExposureHoldCurrent();
357 
361  void SetExposureManual(int value);
362 
363  protected:
364  explicit VideoCamera(CS_Source handle) : VideoSource(handle) {}
365 };
366 
370 class UsbCamera : public VideoCamera {
371  public:
372  UsbCamera() = default;
373 
380  UsbCamera(const wpi::Twine& name, int dev);
381 
388  UsbCamera(const wpi::Twine& name, const wpi::Twine& path);
389 
395  static std::vector<UsbCameraInfo> EnumerateUsbCameras();
396 
400  std::string GetPath() const;
401 
407  void SetConnectVerbose(int level);
408 };
409 
413 class HttpCamera : public VideoCamera {
414  public:
415  enum HttpCameraKind {
416  kUnknown = CS_HTTP_UNKNOWN,
417  kMJPGStreamer = CS_HTTP_MJPGSTREAMER,
418  kCSCore = CS_HTTP_CSCORE,
419  kAxis = CS_HTTP_AXIS
420  };
421 
429  HttpCamera(const wpi::Twine& name, const wpi::Twine& url,
430  HttpCameraKind kind = kUnknown);
431 
439  HttpCamera(const wpi::Twine& name, const char* url,
440  HttpCameraKind kind = kUnknown);
441 
449  HttpCamera(const wpi::Twine& name, const std::string& url,
450  HttpCameraKind kind = kUnknown);
451 
460  HttpCameraKind kind = kUnknown);
461 
469  template <typename T>
470  HttpCamera(const wpi::Twine& name, std::initializer_list<T> urls,
471  HttpCameraKind kind = kUnknown);
472 
479  HttpCameraKind GetHttpCameraKind() const;
480 
485 
489  template <typename T>
490  void SetUrls(std::initializer_list<T> urls);
491 
495  std::vector<std::string> GetUrls() const;
496 };
497 
501 class AxisCamera : public HttpCamera {
502  static std::string HostToUrl(const wpi::Twine& host);
503  static std::vector<std::string> HostToUrl(wpi::ArrayRef<std::string> hosts);
504  template <typename T>
505  static std::vector<std::string> HostToUrl(std::initializer_list<T> hosts);
506 
507  public:
515  AxisCamera(const wpi::Twine& name, const wpi::Twine& host);
516 
524  AxisCamera(const wpi::Twine& name, const char* host);
525 
533  AxisCamera(const wpi::Twine& name, const std::string& host);
534 
543 
551  template <typename T>
552  AxisCamera(const wpi::Twine& name, std::initializer_list<T> hosts);
553 };
554 
558 class CvSource : public VideoSource {
559  public:
560  CvSource() = default;
561 
568  CvSource(const wpi::Twine& name, const VideoMode& mode);
569 
579  CvSource(const wpi::Twine& name, VideoMode::PixelFormat pixelFormat,
580  int width, int height, int fps);
581 
591  void PutFrame(cv::Mat& image);
592 
597  void NotifyError(const wpi::Twine& msg);
598 
604  void SetConnected(bool connected);
605 
611  void SetDescription(const wpi::Twine& description);
612 
625  VideoProperty CreateProperty(const wpi::Twine& name, VideoProperty::Kind kind,
626  int minimum, int maximum, int step,
627  int defaultValue, int value);
628 
640  VideoProperty CreateIntegerProperty(const wpi::Twine& name, int minimum,
641  int maximum, int step, int defaultValue,
642  int value);
643 
652  VideoProperty CreateBooleanProperty(const wpi::Twine& name, bool defaultValue,
653  bool value);
654 
664  const wpi::Twine& value);
665 
672  void SetEnumPropertyChoices(const VideoProperty& property,
674 
681  template <typename T>
682  void SetEnumPropertyChoices(const VideoProperty& property,
683  std::initializer_list<T> choices);
684 };
685 
689 class VideoSink {
690  friend class VideoEvent;
691  friend class VideoSource;
692 
693  public:
694  enum Kind {
695  kUnknown = CS_SINK_UNKNOWN,
696  kMjpeg = CS_SINK_MJPEG,
697  kCv = CS_SINK_CV
698  };
699 
700  VideoSink() noexcept : m_handle(0) {}
701  VideoSink(const VideoSink& sink);
702  VideoSink(VideoSink&& sink) noexcept;
703  VideoSink& operator=(VideoSink other) noexcept;
704  ~VideoSink();
705 
706  explicit operator bool() const { return m_handle != 0; }
707 
708  int GetHandle() const { return m_handle; }
709 
710  bool operator==(const VideoSink& other) const {
711  return m_handle == other.m_handle;
712  }
713 
714  bool operator!=(const VideoSink& other) const { return !(*this == other); }
715 
719  Kind GetKind() const;
720 
725  std::string GetName() const;
726 
730  std::string GetDescription() const;
731 
739  VideoProperty GetProperty(const wpi::Twine& name);
740 
744  std::vector<VideoProperty> EnumerateProperties() const;
745 
753  void SetSource(VideoSource source);
754 
760  VideoSource GetSource() const;
761 
770 
771  CS_Status GetLastStatus() const { return m_status; }
772 
778  static std::vector<VideoSink> EnumerateSinks();
779 
780  friend void swap(VideoSink& first, VideoSink& second) noexcept {
781  using std::swap;
782  swap(first.m_status, second.m_status);
783  swap(first.m_handle, second.m_handle);
784  }
785 
786  protected:
787  explicit VideoSink(CS_Sink handle) : m_handle(handle) {}
788 
789  mutable CS_Status m_status = 0;
790  CS_Sink m_handle;
791 };
792 
796 class MjpegServer : public VideoSink {
797  public:
798  MjpegServer() = default;
799 
807  MjpegServer(const wpi::Twine& name, const wpi::Twine& listenAddress,
808  int port);
809 
816  MjpegServer(const wpi::Twine& name, int port) : MjpegServer(name, "", port) {}
817 
821  std::string GetListenAddress() const;
822 
826  int GetPort() const;
827 
842  void SetResolution(int width, int height);
843 
851  void SetFPS(int fps);
852 
862  void SetCompression(int quality);
863 
871  void SetDefaultCompression(int quality);
872 };
873 
877 class CvSink : public VideoSink {
878  public:
879  CvSink() = default;
880 
889  explicit CvSink(const wpi::Twine& name);
890 
903  CvSink(const wpi::Twine& name,
904  std::function<void(uint64_t time)> processFrame);
905 
911  void SetDescription(const wpi::Twine& description);
912 
922  uint64_t GrabFrame(cv::Mat& image, double timeout = 0.225) const;
923 
932  uint64_t GrabFrameNoTimeout(cv::Mat& image) const;
933 
938  std::string GetError() const;
939 
947  void SetEnabled(bool enabled);
948 };
949 
953 class VideoEvent : public RawEvent {
954  public:
958  VideoSource GetSource() const;
959 
963  VideoSink GetSink() const;
964 
968  VideoProperty GetProperty() const;
969 };
970 
976  public:
977  VideoListener() : m_handle(0) {}
978 
987  VideoListener(std::function<void(const VideoEvent& event)> callback,
988  int eventMask, bool immediateNotify);
989 
990  VideoListener(const VideoListener&) = delete;
991  VideoListener& operator=(const VideoListener&) = delete;
992  VideoListener(VideoListener&& other) noexcept;
993  VideoListener& operator=(VideoListener&& other) noexcept;
994  ~VideoListener();
995 
996  friend void swap(VideoListener& first, VideoListener& second) noexcept {
997  using std::swap;
998  swap(first.m_handle, second.m_handle);
999  }
1000 
1001  private:
1002  CS_Listener m_handle;
1003 };
1004 
1007 } // namespace cs
1008 
1009 #include "cscore_oo.inl"
1010 
1011 #endif // CSCORE_CSCORE_OO_H_
int GetPort() const
Get the port number of the server.
Definition: cscore_oo.inl:511
void SetWhiteBalanceManual(int value)
Set the white balance to manual, with specified color temperature.
Definition: cscore_oo.inl:210
void SetFPS(int fps)
Set the stream frames per second (FPS) for clients that don't specify it.
Definition: cscore_oo.inl:523
CameraServer (cscore) namespace.
Definition: cscore_oo.inl:11
VideoSource GetSource() const
Get the connected source.
Definition: cscore_oo.inl:490
void SetCompression(int quality)
Set the compression for clients that don't specify it.
Definition: cscore_oo.inl:528
void SetDescription(const wpi::Twine &description)
Set source description.
Definition: cscore_oo.inl:383
void SetSource(VideoSource source)
Configure which source should provide frames to this sink.
Definition: cscore_oo.inl:482
A source for video that provides a sequence of frames.
Definition: cscore_oo.h:97
Try to keep the connection open regardless of whether any sinks are connected.
Definition: cscore_c.h:193
VideoSink GetSink() const
Get the sink associated with the event (if any).
Definition: cscore_oo.inl:579
A sink for video that accepts a sequence of frames.
Definition: cscore_oo.h:689
void SetEnumPropertyChoices(const VideoProperty &property, wpi::ArrayRef< std::string > choices)
Configure enum property choices.
Definition: cscore_oo.inl:429
ConnectionStrategy
Connection strategy.
Definition: cscore_oo.h:110
VideoProperty GetSourceProperty(const wpi::Twine &name)
Get a property of the associated source.
Definition: cscore_oo.inl:496
void SetExposureHoldCurrent()
Set the exposure to hold current.
Definition: cscore_oo.inl:220
std::vector< VideoMode > EnumerateVideoModes() const
Enumerate all known video modes for this source.
Definition: cscore_oo.inl:185
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:953
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:254
VideoProperty GetProperty(const wpi::Twine &name)
Get a property of the sink.
Definition: cscore_oo.inl:477
bool IsConnected() const
Is the source currently connected to whatever is providing the images?
Definition: cscore_oo.inl:126
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:312
A source that represents a USB camera.
Definition: cscore_oo.h:370
AxisCamera(const wpi::Twine &name, const wpi::Twine &host)
Create a source for an Axis IP camera.
Definition: cscore_oo.inl:340
An event listener.
Definition: cscore_oo.h:975
uint64_t GrabFrame(cv::Mat &image, double timeout=0.225) const
Wait for the next frame and get the image.
Definition: cscore_oo.inl:554
VideoProperty CreateIntegerProperty(const wpi::Twine &name, int minimum, int maximum, int step, int defaultValue, int value)
Create an integer property.
Definition: cscore_oo.inl:399
A source that represents a MJPEG-over-HTTP (IP) camera.
Definition: cscore_oo.h:413
std::vector< VideoProperty > EnumerateProperties() const
Enumerate all properties of this source.
HttpCameraKind GetHttpCameraKind() const
Get the kind of HTTP camera.
Definition: cscore_oo.inl:292
void SetExposureManual(int value)
Set the exposure to manual, as a percentage (0-100).
Definition: cscore_oo.inl:225
double GetActualFPS() const
Get the actual FPS.
Definition: cscore_oo.inl:173
void SetDescription(const wpi::Twine &description)
Set sink description.
Definition: cscore_oo.inl:549
bool IsEnabled() const
Gets source enable status.
Definition: cscore_oo.inl:131
std::string GetName() const
Get the name of the sink.
Definition: cscore_oo.inl:467
Never open the connection.
Definition: cscore_c.h:199
Automatically connect or disconnect based on whether any sinks are connected to this source...
Definition: cscore_oo.h:115
uint64_t GrabFrameNoTimeout(cv::Mat &image) const
Wait for the next frame and get the image.
Definition: cscore_oo.inl:559
A source or sink property.
Definition: cscore_oo.h:39
Kind GetKind() const
Get the kind of the source.
Definition: cscore_oo.inl:99
Automatically connect or disconnect based on whether any sinks are connected to this source...
Definition: cscore_c.h:187
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:168
std::string GetError() const
Get error string.
Definition: cscore_oo.inl:564
void SetConnectionStrategy(ConnectionStrategy strategy)
Sets the connection strategy.
Definition: cscore_oo.inl:119
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:516
void SetWhiteBalanceHoldCurrent()
Set the white balance to hold current.
Definition: cscore_oo.inl:205
static std::vector< VideoSource > EnumerateSources()
Enumerate all existing sources.
bool SetResolution(int width, int height)
Set the resolution.
Definition: cscore_oo.inl:163
static std::vector< UsbCameraInfo > EnumerateUsbCameras()
Enumerate USB cameras on the local system.
Definition: cscore_oo.inl:238
A source for user code to provide OpenCV images as video frames.
Definition: cscore_oo.h:558
VideoProperty CreateStringProperty(const wpi::Twine &name, const wpi::Twine &value)
Create a string property.
Definition: cscore_oo.inl:418
void SetUrls(wpi::ArrayRef< std::string > urls)
Change the URLs used to connect to the camera.
Definition: cscore_oo.inl:298
void NotifyError(const wpi::Twine &msg)
Signal sinks that an error has occurred.
Definition: cscore_oo.inl:373
void SetDefaultCompression(int quality)
Set the default compression used for non-MJPEG sources.
Definition: cscore_oo.inl:534
std::string GetDescription() const
Get the sink description.
Definition: cscore_oo.inl:472
void SetExposureAuto()
Set the exposure to auto aperature.
Definition: cscore_oo.inl:215
VideoMode GetVideoMode() const
Get the current video mode.
Definition: cscore_oo.inl:141
Try to keep the connection open regardless of whether any sinks are connected.
Definition: cscore_oo.h:121
VideoProperty CreateBooleanProperty(const wpi::Twine &name, bool defaultValue, bool value)
Create a boolean property.
Definition: cscore_oo.inl:409
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:816
std::string GetPath() const
Get the path to the device.
Definition: cscore_oo.inl:243
A source that represents a video camera.
Definition: cscore_oo.h:311
Kind GetKind() const
Get the kind of the sink.
Definition: cscore_oo.inl:462
void SetEnabled(bool enabled)
Enable or disable getting new frames.
Definition: cscore_oo.inl:569
bool SetPixelFormat(VideoMode::PixelFormat pixelFormat)
Set the pixel format.
Definition: cscore_oo.inl:158
VideoProperty GetProperty(const wpi::Twine &name)
Get a property.
Definition: cscore_oo.inl:136
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:388
VideoSource GetSource() const
Get the source associated with the event (if any).
Definition: cscore_oo.inl:574
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:368
A sink that acts as a MJPEG-over-HTTP network server.
Definition: cscore_oo.h:796
VideoProperty GetProperty() const
Get the property associated with the event (if any).
Definition: cscore_oo.inl:584
void SetConnectVerbose(int level)
Set how verbose the camera connection messages are.
Definition: cscore_oo.inl:248
Never open the connection.
Definition: cscore_oo.h:127
A source that represents an Axis IP camera.
Definition: cscore_oo.h:501
void SetBrightness(int brightness)
Set the brightness, as a percentage (0-100).
Definition: cscore_oo.inl:190
A sink for user code to accept video frames as OpenCV images.
Definition: cscore_oo.h:877
double GetActualDataRate() const
Get the data rate (in bytes per second).
Definition: cscore_oo.inl:179
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:506
Video mode.
Definition: cscore_cpp.h:56
void SetWhiteBalanceAuto()
Set the white balance to auto.
Definition: cscore_oo.inl:200
void SetConnected(bool connected)
Set source connection status.
Definition: cscore_oo.inl:378
bool SetVideoMode(const VideoMode &mode)
Set the video mode.
Definition: cscore_oo.inl:146
int GetBrightness()
Get the brightness, as a percentage (0-100).
Definition: cscore_oo.inl:195