WPILibC++ 2025.0.0-alpha-1-10-g1ccd8d1
cscore_oo.h
Go to the documentation of this file.
1// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5#ifndef CSCORE_CSCORE_OO_H_
6#define CSCORE_CSCORE_OO_H_
7
8#include <functional>
9#include <initializer_list>
10#include <span>
11#include <string>
12#include <string_view>
13#include <utility>
14#include <vector>
15
16#include "cscore_cpp.h"
17
18namespace cs {
19
20/**
21 * @defgroup cscore_oo cscore C++ object-oriented API
22 *
23 * Recommended interface for C++, identical to Java API.
24 *
25 * <p>The classes are RAII and handle reference counting internally.
26 *
27 * @{
28 */
29
30// Forward declarations so friend declarations work correctly
31class ImageSource;
32class VideoEvent;
33class VideoSink;
34class VideoSource;
35
36/**
37 * A source or sink property.
38 */
40 friend class ImageSource;
41 friend class VideoEvent;
42 friend class VideoSink;
43 friend class VideoSource;
44
45 public:
46 enum Kind {
47 /// No specific property.
49 /// Boolean property.
51 /// Integer property.
53 /// String property.
55 /// Enum property.
57 };
58
59 VideoProperty() = default;
60
61 /**
62 * Returns property name.
63 *
64 * @return Property name.
65 */
66 std::string GetName() const;
67
68 /**
69 * Returns property kind.
70 *
71 * @return Property kind.
72 */
73 Kind GetKind() const { return m_kind; }
74
75 /**
76 * Returns true if property is valid.
77 *
78 * @return True if property is valid.
79 */
80 explicit operator bool() const { return m_kind != kNone; }
81
82 /**
83 * Returns true if property is a boolean.
84 *
85 * @return True if property is a boolean.
86 */
87 bool IsBoolean() const { return m_kind == kBoolean; }
88
89 /**
90 * Returns true if property is an integer.
91 *
92 * @return True if property is an integer.
93 */
94 bool IsInteger() const { return m_kind == kInteger; }
95
96 /**
97 * Returns true if property is a string.
98 *
99 * @return True if property is a string.
100 */
101 bool IsString() const { return m_kind == kString; }
102
103 /**
104 * Returns true if property is an enum.
105 *
106 * @return True if property is an enum.
107 */
108 bool IsEnum() const { return m_kind == kEnum; }
109
110 /**
111 * Returns property value.
112 *
113 * @return Property value.
114 */
115 int Get() const;
116
117 /**
118 * Sets property value.
119 *
120 * @param value Property value.
121 */
122 void Set(int value);
123
124 /**
125 * Returns property minimum value.
126 *
127 * @return Property minimum value.
128 */
129 int GetMin() const;
130
131 /**
132 * Returns property maximum value.
133 *
134 * @return Property maximum value.
135 */
136 int GetMax() const;
137
138 /**
139 * Returns property step size.
140 *
141 * @return Property step size.
142 */
143 int GetStep() const;
144
145 /**
146 * Returns property default value.
147 *
148 * @return Property default value.
149 */
150 int GetDefault() const;
151
152 /**
153 * Returns the string property value.
154 *
155 * <p>This function is string-specific.
156 *
157 * @return The string property value.
158 */
159 std::string GetString() const;
160
161 /**
162 * Returns the string property value as a reference to the given buffer.
163 *
164 * This function is string-specific.
165 *
166 * @param buf The backing storage to which to write the property value.
167 * @return The string property value as a reference to the given buffer.
168 */
170
171 /**
172 * Sets the string property value.
173 *
174 * This function is string-specific.
175 *
176 * @param value String property value.
177 */
178 void SetString(std::string_view value);
179
180 /**
181 * Returns the possible values for the enum property value.
182 *
183 * This function is enum-specific.
184 *
185 * @return The possible values for the enum property value.
186 */
187 std::vector<std::string> GetChoices() const;
188
189 /**
190 * Returns the last status.
191 *
192 * @return The last status.
193 */
194 CS_Status GetLastStatus() const { return m_status; }
195
196 private:
197 explicit VideoProperty(CS_Property handle);
198 VideoProperty(CS_Property handle, Kind kind);
199
200 mutable CS_Status m_status{0};
201 CS_Property m_handle{0};
202 Kind m_kind{kNone};
203};
204
205/**
206 * A source for video that provides a sequence of frames.
207 */
209 friend class VideoEvent;
210 friend class VideoSink;
211
212 public:
213 /**
214 * Video source kind.
215 */
216 enum Kind {
217 /// Unknown video source.
219 /// USB video source.
221 /// HTTP video source.
223 /// CV video source.
225 /// Raw video source.
227 };
228
229 /** Connection strategy. Used for SetConnectionStrategy(). */
231 /**
232 * Automatically connect or disconnect based on whether any sinks are
233 * connected to this source. This is the default behavior.
234 */
236
237 /**
238 * Try to keep the connection open regardless of whether any sinks are
239 * connected.
240 */
242
243 /**
244 * Never open the connection. If this is set when the connection is open,
245 * close the connection.
246 */
248 };
249
250 VideoSource() noexcept = default;
252 VideoSource(VideoSource&& other) noexcept;
253 VideoSource& operator=(VideoSource other) noexcept;
254 ~VideoSource();
255
256 explicit operator bool() const { return m_handle != 0; }
257
258 int GetHandle() const { return m_handle; }
259
260 bool operator==(const VideoSource& other) const {
261 return m_handle == other.m_handle;
262 }
263
264 /**
265 * Get the kind of the source.
266 */
267 Kind GetKind() const;
268
269 /**
270 * Get the name of the source. The name is an arbitrary identifier
271 * provided when the source is created, and should be unique.
272 */
273 std::string GetName() const;
274
275 /**
276 * Get the source description. This is source-kind specific.
277 */
278 std::string GetDescription() const;
279
280 /**
281 * Get the last time a frame was captured.
282 * This uses the same time base as wpi::Now().
283 *
284 * @return Time in 1 us increments.
285 */
286 uint64_t GetLastFrameTime() const;
287
288 /**
289 * Sets the connection strategy. By default, the source will automatically
290 * connect or disconnect based on whether any sinks are connected.
291 *
292 * <p>This function is non-blocking; look for either a connection open or
293 * close event or call IsConnected() to determine the connection state.
294 *
295 * @param strategy connection strategy (auto, keep open, or force close)
296 */
298
299 /**
300 * Is the source currently connected to whatever is providing the images?
301 */
302 bool IsConnected() const;
303
304 /**
305 * Gets source enable status. This is determined with a combination of
306 * connection strategy and the number of sinks connected.
307 *
308 * @return True if enabled, false otherwise.
309 */
310 bool IsEnabled() const;
311
312 /** Get a property.
313 *
314 * @param name Property name
315 * @return Property contents (of kind Property::kNone if no property with
316 * the given name exists)
317 */
319
320 /**
321 * Enumerate all properties of this source.
322 */
323 std::vector<VideoProperty> EnumerateProperties() const;
324
325 /**
326 * Get the current video mode.
327 */
328 VideoMode GetVideoMode() const;
329
330 /**
331 * Set the video mode.
332 *
333 * @param mode Video mode
334 */
335 bool SetVideoMode(const VideoMode& mode);
336
337 /**
338 * Set the video mode.
339 *
340 * @param pixelFormat desired pixel format
341 * @param width desired width
342 * @param height desired height
343 * @param fps desired FPS
344 * @return True if set successfully
345 */
346 bool SetVideoMode(VideoMode::PixelFormat pixelFormat, int width, int height,
347 int fps);
348
349 /**
350 * Set the pixel format.
351 *
352 * @param pixelFormat desired pixel format
353 * @return True if set successfully
354 */
355 bool SetPixelFormat(VideoMode::PixelFormat pixelFormat);
356
357 /**
358 * Set the resolution.
359 *
360 * @param width desired width
361 * @param height desired height
362 * @return True if set successfully
363 */
364 bool SetResolution(int width, int height);
365
366 /**
367 * Set the frames per second (FPS).
368 *
369 * @param fps desired FPS
370 * @return True if set successfully
371 */
372 bool SetFPS(int fps);
373
374 /**
375 * Set video mode and properties from a JSON configuration string.
376 *
377 * The format of the JSON input is:
378 *
379 * <pre>
380 * {
381 * "pixel format": "MJPEG", "YUYV", etc
382 * "width": video mode width
383 * "height": video mode height
384 * "fps": video mode fps
385 * "brightness": percentage brightness
386 * "white balance": "auto", "hold", or value
387 * "exposure": "auto", "hold", or value
388 * "properties": [
389 * {
390 * "name": property name
391 * "value": property value
392 * }
393 * ]
394 * }
395 * </pre>
396 *
397 * @param config configuration
398 * @return True if set successfully
399 */
400 bool SetConfigJson(std::string_view config);
401
402 /**
403 * Set video mode and properties from a JSON configuration object.
404 *
405 * @param config configuration
406 * @return True if set successfully
407 */
408 bool SetConfigJson(const wpi::json& config);
409
410 /**
411 * Get a JSON configuration string.
412 *
413 * @return JSON configuration string
414 */
415 std::string GetConfigJson() const;
416
417 /**
418 * Get a JSON configuration object.
419 *
420 * @return JSON configuration object
421 */
422 wpi::json GetConfigJsonObject() const;
423
424 /**
425 * Get the actual FPS.
426 *
427 * <p>SetTelemetryPeriod() must be called for this to be valid.
428 *
429 * @return Actual FPS averaged over the telemetry period.
430 */
431 double GetActualFPS() const;
432
433 /**
434 * Get the data rate (in bytes per second).
435 *
436 * <p>SetTelemetryPeriod() must be called for this to be valid.
437 *
438 * @return Data rate averaged over the telemetry period.
439 */
440 double GetActualDataRate() const;
441
442 /**
443 * Enumerate all known video modes for this source.
444 */
445 std::vector<VideoMode> EnumerateVideoModes() const;
446
447 CS_Status GetLastStatus() const { return m_status; }
448
449 /**
450 * Enumerate all sinks connected to this source.
451 *
452 * @return Vector of sinks.
453 */
454 std::vector<VideoSink> EnumerateSinks();
455
456 /**
457 * Enumerate all existing sources.
458 *
459 * @return Vector of sources.
460 */
461 static std::vector<VideoSource> EnumerateSources();
462
463 friend void swap(VideoSource& first, VideoSource& second) noexcept {
464 using std::swap;
465 swap(first.m_status, second.m_status);
466 swap(first.m_handle, second.m_handle);
467 }
468
469 protected:
470 explicit VideoSource(CS_Source handle) : m_handle(handle) {}
471
472 mutable CS_Status m_status = 0;
473
474 /// Video source handle.
476};
477
478/**
479 * A source that represents a video camera.
480 */
481class VideoCamera : public VideoSource {
482 public:
483 /**
484 * White balance.
485 */
487 /// Fixed indoor white balance.
489 /// Fixed outdoor white balance 1.
491 /// Fixed outdoor white balance 2.
493 /// Fixed fluorescent white balance 1.
495 /// Fixed fluorescent white balance 2.
496 kFixedFlourescent2 = 5200
497 };
498
499 VideoCamera() = default;
500
501 /**
502 * Set the brightness, as a percentage (0-100).
503 */
504 void SetBrightness(int brightness);
505
506 /**
507 * Get the brightness, as a percentage (0-100).
508 */
509 int GetBrightness();
510
511 /**
512 * Set the white balance to auto.
513 */
514 void SetWhiteBalanceAuto();
515
516 /**
517 * Set the white balance to hold current.
518 */
520
521 /**
522 * Set the white balance to manual, with specified color temperature.
523 */
524 void SetWhiteBalanceManual(int value);
525
526 /**
527 * Set the exposure to auto aperature.
528 */
529 void SetExposureAuto();
530
531 /**
532 * Set the exposure to hold current.
533 */
535
536 /**
537 * Set the exposure to manual, as a percentage (0-100).
538 */
539 void SetExposureManual(int value);
540
541 protected:
542 explicit VideoCamera(CS_Source handle) : VideoSource(handle) {}
543};
544
545/**
546 * A source that represents a USB camera.
547 */
548class UsbCamera : public VideoCamera {
549 public:
550 UsbCamera() = default;
551
552 /**
553 * Create a source for a USB camera based on device number.
554 *
555 * @param name Source name (arbitrary unique identifier)
556 * @param dev Device number (e.g. 0 for /dev/video0)
557 */
558 UsbCamera(std::string_view name, int dev);
559
560 /**
561 * Create a source for a USB camera based on device path.
562 *
563 * @param name Source name (arbitrary unique identifier)
564 * @param path Path to device (e.g. "/dev/video0" on Linux)
565 */
567
568 /**
569 * Enumerate USB cameras on the local system.
570 *
571 * @return Vector of USB camera information (one for each camera)
572 */
573 static std::vector<UsbCameraInfo> EnumerateUsbCameras();
574
575 /**
576 * Change the path to the device.
577 */
578 void SetPath(std::string_view path);
579
580 /**
581 * Get the path to the device.
582 */
583 std::string GetPath() const;
584
585 /**
586 * Get the full camera information for the device.
587 */
588 UsbCameraInfo GetInfo() const;
589
590 /**
591 * Set how verbose the camera connection messages are.
592 *
593 * @param level 0=don't display Connecting message, 1=do display message
594 */
595 void SetConnectVerbose(int level);
596};
597
598/**
599 * A source that represents a MJPEG-over-HTTP (IP) camera.
600 */
601class HttpCamera : public VideoCamera {
602 public:
603 /**
604 * HTTP camera kind.
605 */
607 /// Unknown camera kind.
609 /// MJPG Streamer camera.
611 /// CS Core camera.
613 /// Axis camera.
615 };
616
617 /**
618 * Create a source for a MJPEG-over-HTTP (IP) camera.
619 *
620 * @param name Source name (arbitrary unique identifier)
621 * @param url Camera URL (e.g. "http://10.x.y.11/video/stream.mjpg")
622 * @param kind Camera kind (e.g. kAxis)
623 */
625 HttpCameraKind kind = kUnknown);
626
627 /**
628 * Create a source for a MJPEG-over-HTTP (IP) camera.
629 *
630 * @param name Source name (arbitrary unique identifier)
631 * @param url Camera URL (e.g. "http://10.x.y.11/video/stream.mjpg")
632 * @param kind Camera kind (e.g. kAxis)
633 */
634 HttpCamera(std::string_view name, const char* url,
635 HttpCameraKind kind = kUnknown);
636
637 /**
638 * Create a source for a MJPEG-over-HTTP (IP) camera.
639 *
640 * @param name Source name (arbitrary unique identifier)
641 * @param url Camera URL (e.g. "http://10.x.y.11/video/stream.mjpg")
642 * @param kind Camera kind (e.g. kAxis)
643 */
644 HttpCamera(std::string_view name, const std::string& url,
645 HttpCameraKind kind = kUnknown);
646
647 /**
648 * Create a source for a MJPEG-over-HTTP (IP) camera.
649 *
650 * @param name Source name (arbitrary unique identifier)
651 * @param urls Array of Camera URLs
652 * @param kind Camera kind (e.g. kAxis)
653 */
654 HttpCamera(std::string_view name, std::span<const std::string> urls,
655 HttpCameraKind kind = kUnknown);
656
657 /**
658 * Create a source for a MJPEG-over-HTTP (IP) camera.
659 *
660 * @param name Source name (arbitrary unique identifier)
661 * @param urls Array of Camera URLs
662 * @param kind Camera kind (e.g. kAxis)
663 */
664 template <typename T>
665 HttpCamera(std::string_view name, std::initializer_list<T> urls,
666 HttpCameraKind kind = kUnknown);
667
668 /**
669 * Get the kind of HTTP camera.
670 *
671 * <p>Autodetection can result in returning a different value than the camera
672 * was created with.
673 */
675
676 /**
677 * Change the URLs used to connect to the camera.
678 */
679 void SetUrls(std::span<const std::string> urls);
680
681 /**
682 * Change the URLs used to connect to the camera.
683 */
684 template <typename T>
685 void SetUrls(std::initializer_list<T> urls);
686
687 /**
688 * Get the URLs used to connect to the camera.
689 */
690 std::vector<std::string> GetUrls() const;
691};
692
693/**
694 * A source that represents an Axis IP camera.
695 *
696 * @deprecated Use HttpCamera instead.
697 */
698class [[deprecated("Use HttpCamera instead.")]] AxisCamera : public HttpCamera {
699 static std::string HostToUrl(std::string_view host);
700 static std::vector<std::string> HostToUrl(std::span<const std::string> hosts);
701 template <typename T>
702 static std::vector<std::string> HostToUrl(std::initializer_list<T> hosts);
703
704 public:
705 /**
706 * Create a source for an Axis IP camera.
707 *
708 * @param name Source name (arbitrary unique identifier)
709 * @param host Camera host IP or DNS name (e.g. "10.x.y.11")
710 */
712
713 /**
714 * Create a source for an Axis IP camera.
715 *
716 * @param name Source name (arbitrary unique identifier)
717 * @param host Camera host IP or DNS name (e.g. "10.x.y.11")
718 */
719 AxisCamera(std::string_view name, const char* host);
720
721 /**
722 * Create a source for an Axis IP camera.
723 *
724 * @param name Source name (arbitrary unique identifier)
725 * @param host Camera host IP or DNS name (e.g. "10.x.y.11")
726 */
727 AxisCamera(std::string_view name, const std::string& host);
728
729 /**
730 * Create a source for an Axis IP camera.
731 *
732 * @param name Source name (arbitrary unique identifier)
733 * @param hosts Array of Camera host IPs/DNS names
734 */
735 AxisCamera(std::string_view name, std::span<const std::string> hosts);
736
737 /**
738 * Create a source for an Axis IP camera.
739 *
740 * @param name Source name (arbitrary unique identifier)
741 * @param hosts Array of Camera host IPs/DNS names
742 */
743 template <typename T>
744 AxisCamera(std::string_view name, std::initializer_list<T> hosts);
745};
746
747/**
748 * A base class for single image providing sources.
749 */
750class ImageSource : public VideoSource {
751 protected:
752 ImageSource() = default;
753
754 public:
755 /**
756 * Signal sinks that an error has occurred. This should be called instead
757 * of NotifyFrame when an error occurs.
758 *
759 * @param msg Notification message.
760 */
762
763 /**
764 * Set source connection status. Defaults to true.
765 *
766 * @param connected True for connected, false for disconnected
767 */
768 void SetConnected(bool connected);
769
770 /**
771 * Set source description.
772 *
773 * @param description Description
774 */
775 void SetDescription(std::string_view description);
776
777 /**
778 * Create a property.
779 *
780 * @param name Property name
781 * @param kind Property kind
782 * @param minimum Minimum value
783 * @param maximum Maximum value
784 * @param step Step value
785 * @param defaultValue Default value
786 * @param value Current value
787 * @return Property
788 */
790 int minimum, int maximum, int step,
791 int defaultValue, int value);
792
793 /**
794 * Create an integer property.
795 *
796 * @param name Property name
797 * @param minimum Minimum value
798 * @param maximum Maximum value
799 * @param step Step value
800 * @param defaultValue Default value
801 * @param value Current value
802 * @return Property
803 */
805 int maximum, int step, int defaultValue,
806 int value);
807
808 /**
809 * Create a boolean property.
810 *
811 * @param name Property name
812 * @param defaultValue Default value
813 * @param value Current value
814 * @return Property
815 */
817 bool value);
818
819 /**
820 * Create a string property.
821 *
822 * @param name Property name
823 * @param value Current value
824 * @return Property
825 */
827 std::string_view value);
828
829 /**
830 * Configure enum property choices.
831 *
832 * @param property Property
833 * @param choices Choices
834 */
835 void SetEnumPropertyChoices(const VideoProperty& property,
836 std::span<const std::string> choices);
837
838 /**
839 * Configure enum property choices.
840 *
841 * @param property Property
842 * @param choices Choices
843 */
844 template <typename T>
845 void SetEnumPropertyChoices(const VideoProperty& property,
846 std::initializer_list<T> choices);
847};
848
849/**
850 * A sink for video that accepts a sequence of frames.
851 */
853 friend class VideoEvent;
854 friend class VideoSource;
855
856 public:
857 enum Kind {
858 /// Unknown sink type.
860 /// MJPEG video sink.
862 /// CV video sink.
864 /// Raw video sink.
866 };
867
868 VideoSink() noexcept = default;
869 VideoSink(const VideoSink& sink);
870 VideoSink(VideoSink&& sink) noexcept;
871 VideoSink& operator=(VideoSink other) noexcept;
872 ~VideoSink();
873
874 /**
875 * Returns true if the VideoSink is valid.
876 *
877 * @return True if the VideoSink is valid.
878 */
879 explicit operator bool() const { return m_handle != 0; }
880
881 /**
882 * Returns the VideoSink handle.
883 *
884 * @return The VideoSink handle.
885 */
886 int GetHandle() const { return m_handle; }
887
888 bool operator==(const VideoSink& other) const {
889 return m_handle == other.m_handle;
890 }
891
892 /**
893 * Get the kind of the sink.
894 */
895 Kind GetKind() const;
896
897 /**
898 * Get the name of the sink. The name is an arbitrary identifier
899 * provided when the sink is created, and should be unique.
900 */
901 std::string GetName() const;
902
903 /**
904 * Get the sink description. This is sink-kind specific.
905 */
906 std::string GetDescription() const;
907
908 /**
909 * Get a property of the sink.
910 *
911 * @param name Property name
912 * @return Property (kind Property::kNone if no property with
913 * the given name exists)
914 */
916
917 /**
918 * Enumerate all properties of this sink.
919 */
920 std::vector<VideoProperty> EnumerateProperties() const;
921
922 /**
923 * Set properties from a JSON configuration string.
924 *
925 * The format of the JSON input is:
926 *
927 * <pre>
928 * {
929 * "properties": [
930 * {
931 * "name": property name
932 * "value": property value
933 * }
934 * ]
935 * }
936 * </pre>
937 *
938 * @param config configuration
939 * @return True if set successfully
940 */
941 bool SetConfigJson(std::string_view config);
942
943 /**
944 * Set properties from a JSON configuration object.
945 *
946 * @param config configuration
947 * @return True if set successfully
948 */
949 bool SetConfigJson(const wpi::json& config);
950
951 /**
952 * Get a JSON configuration string.
953 *
954 * @return JSON configuration string
955 */
956 std::string GetConfigJson() const;
957
958 /**
959 * Get a JSON configuration object.
960 *
961 * @return JSON configuration object
962 */
963 wpi::json GetConfigJsonObject() const;
964
965 /**
966 * Configure which source should provide frames to this sink. Each sink
967 * can accept frames from only a single source, but a single source can
968 * provide frames to multiple clients.
969 *
970 * @param source Source
971 */
973
974 /**
975 * Get the connected source.
976 *
977 * @return Connected source (empty if none connected).
978 */
979 VideoSource GetSource() const;
980
981 /**
982 * Get a property of the associated source.
983 *
984 * @param name Property name
985 * @return Property (kind Property::kNone if no property with
986 * the given name exists or no source connected)
987 */
989
990 CS_Status GetLastStatus() const { return m_status; }
991
992 /**
993 * Enumerate all existing sinks.
994 *
995 * @return Vector of sinks.
996 */
997 static std::vector<VideoSink> EnumerateSinks();
998
999 friend void swap(VideoSink& first, VideoSink& second) noexcept {
1000 using std::swap;
1001 swap(first.m_status, second.m_status);
1002 swap(first.m_handle, second.m_handle);
1003 }
1004
1005 protected:
1006 explicit VideoSink(CS_Sink handle) : m_handle(handle) {}
1007
1008 mutable CS_Status m_status = 0;
1010};
1011
1012/**
1013 * A sink that acts as a MJPEG-over-HTTP network server.
1014 */
1015class MjpegServer : public VideoSink {
1016 public:
1017 MjpegServer() = default;
1018
1019 /**
1020 * Create a MJPEG-over-HTTP server sink.
1021 *
1022 * @param name Sink name (arbitrary unique identifier)
1023 * @param listenAddress TCP listen address (empty string for all addresses)
1024 * @param port TCP port number
1025 */
1026 MjpegServer(std::string_view name, std::string_view listenAddress, int port);
1027
1028 /**
1029 * Create a MJPEG-over-HTTP server sink.
1030 *
1031 * @param name Sink name (arbitrary unique identifier)
1032 * @param port TCP port number
1033 */
1034 MjpegServer(std::string_view name, int port) : MjpegServer(name, "", port) {}
1035
1036 /**
1037 * Get the listen address of the server.
1038 */
1039 std::string GetListenAddress() const;
1040
1041 /**
1042 * Get the port number of the server.
1043 */
1044 int GetPort() const;
1045
1046 /**
1047 * Set the stream resolution for clients that don't specify it.
1048 *
1049 * <p>It is not necessary to set this if it is the same as the source
1050 * resolution.
1051 *
1052 * <p>Setting this different than the source resolution will result in
1053 * increased CPU usage, particularly for MJPEG source cameras, as it will
1054 * decompress, resize, and recompress the image, instead of using the
1055 * camera's MJPEG image directly.
1056 *
1057 * @param width width, 0 for unspecified
1058 * @param height height, 0 for unspecified
1059 */
1060 void SetResolution(int width, int height);
1061
1062 /**
1063 * Set the stream frames per second (FPS) for clients that don't specify it.
1064 *
1065 * <p>It is not necessary to set this if it is the same as the source FPS.
1066 *
1067 * @param fps FPS, 0 for unspecified
1068 */
1069 void SetFPS(int fps);
1070
1071 /**
1072 * Set the compression for clients that don't specify it.
1073 *
1074 * <p>Setting this will result in increased CPU usage for MJPEG source cameras
1075 * as it will decompress and recompress the image instead of using the
1076 * camera's MJPEG image directly.
1077 *
1078 * @param quality JPEG compression quality (0-100), -1 for unspecified
1079 */
1080 void SetCompression(int quality);
1081
1082 /**
1083 * Set the default compression used for non-MJPEG sources. If not set,
1084 * 80 is used. This function has no effect on MJPEG source cameras; use
1085 * SetCompression() instead to force recompression of MJPEG source images.
1086 *
1087 * @param quality JPEG compression quality (0-100)
1088 */
1089 void SetDefaultCompression(int quality);
1090};
1091
1092/**
1093 * A base class for single image reading sinks.
1094 */
1095class ImageSink : public VideoSink {
1096 protected:
1097 ImageSink() = default;
1098
1099 public:
1100 /**
1101 * Set sink description.
1102 *
1103 * @param description Description
1104 */
1105 void SetDescription(std::string_view description);
1106
1107 /**
1108 * Get error string. Call this if WaitForFrame() returns 0 to determine
1109 * what the error is.
1110 */
1111 std::string GetError() const;
1112
1113 /**
1114 * Enable or disable getting new frames.
1115 *
1116 * <p>Disabling will cause processFrame (for callback-based CvSinks) to not
1117 * be called and WaitForFrame() to not return. This can be used to save
1118 * processor resources when frames are not needed.
1119 */
1120 void SetEnabled(bool enabled);
1121};
1122
1123/**
1124 * An event generated by the library and provided to event listeners.
1125 */
1126class VideoEvent : public RawEvent {
1127 public:
1128 /**
1129 * Returns the source associated with the event (if any).
1130 *
1131 * @return The source associated with the event (if any).
1132 */
1133 VideoSource GetSource() const;
1134
1135 /**
1136 * Returns the sink associated with the event (if any).
1137 *
1138 * @return The sink associated with the event (if any).
1139 */
1140 VideoSink GetSink() const;
1141
1142 /**
1143 * Returns the property associated with the event (if any).
1144 *
1145 * @return The property associated with the event (if any).
1146 */
1147 VideoProperty GetProperty() const;
1148};
1149
1150/**
1151 * An event listener. This calls back to a desigated callback function when
1152 * an event matching the specified mask is generated by the library.
1153 */
1155 public:
1156 VideoListener() = default;
1157
1158 /**
1159 * Create an event listener.
1160 *
1161 * @param callback Callback function
1162 * @param eventMask Bitmask of VideoEvent::Kind values
1163 * @param immediateNotify Whether callback should be immediately called with
1164 * a representative set of events for the current library state.
1165 */
1166 VideoListener(std::function<void(const VideoEvent& event)> callback,
1167 int eventMask, bool immediateNotify);
1168
1171 VideoListener(VideoListener&& other) noexcept;
1172 VideoListener& operator=(VideoListener&& other) noexcept;
1174
1175 friend void swap(VideoListener& first, VideoListener& second) noexcept {
1176 using std::swap;
1177 swap(first.m_handle, second.m_handle);
1178 }
1179
1180 private:
1181 CS_Listener m_handle{0};
1182};
1183
1184/** @} */
1185
1186} // namespace cs
1187
1188#include "cscore_oo.inc"
1189
1190#endif // CSCORE_CSCORE_OO_H_
and restrictions which apply to each piece of software is included later in this file and or inside of the individual applicable source files The disclaimer of warranty in the WPILib license above applies to all code in and nothing in any of the other licenses gives permission to use the names of FIRST nor the names of the WPILib contributors to endorse or promote products derived from this software The following pieces of software have additional or alternate and or Google Inc All rights reserved Redistribution and use in source and binary with or without are permitted provided that the following conditions are this list of conditions and the following disclaimer *Redistributions in binary form must reproduce the above copyright this list of conditions and the following disclaimer in the documentation and or other materials provided with the distribution *Neither the name of Google Inc nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS AS IS AND ANY EXPRESS OR IMPLIED BUT NOT LIMITED THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY OR CONSEQUENTIAL WHETHER IN STRICT OR EVEN IF ADVISED OF THE POSSIBILITY OF SUCH January AND DISTRIBUTION Definitions License shall mean the terms and conditions for and distribution as defined by Sections through of this document Licensor shall mean the copyright owner or entity authorized by the copyright owner that is granting the License Legal Entity shall mean the union of the acting entity and all other entities that control are controlled by or are under common control with that entity For the purposes of this definition control direct or to cause the direction or management of such whether by contract or including but not limited to software source documentation source
Definition: ThirdPartyNotices.txt:112
A source that represents an Axis IP camera.
Definition: cscore_oo.h:698
A source that represents a MJPEG-over-HTTP (IP) camera.
Definition: cscore_oo.h:601
HttpCamera(std::string_view name, std::string_view url, HttpCameraKind kind=kUnknown)
Create a source for a MJPEG-over-HTTP (IP) camera.
Definition: cscore_oo.inc:289
std::vector< std::string > GetUrls() const
Get the URLs used to connect to the camera.
Definition: cscore_oo.inc:351
void SetUrls(std::span< const std::string > urls)
Change the URLs used to connect to the camera.
Definition: cscore_oo.inc:335
HttpCameraKind
HTTP camera kind.
Definition: cscore_oo.h:606
@ kMJPGStreamer
MJPG Streamer camera.
Definition: cscore_oo.h:610
@ kCSCore
CS Core camera.
Definition: cscore_oo.h:612
@ kAxis
Axis camera.
Definition: cscore_oo.h:614
@ kUnknown
Unknown camera kind.
Definition: cscore_oo.h:608
HttpCameraKind GetHttpCameraKind() const
Get the kind of HTTP camera.
Definition: cscore_oo.inc:329
A base class for single image reading sinks.
Definition: cscore_oo.h:1095
void SetEnabled(bool enabled)
Enable or disable getting new frames.
Definition: cscore_oo.inc:599
std::string GetError() const
Get error string.
Definition: cscore_oo.inc:594
void SetDescription(std::string_view description)
Set sink description.
Definition: cscore_oo.inc:589
ImageSink()=default
A base class for single image providing sources.
Definition: cscore_oo.h:750
VideoProperty CreateBooleanProperty(std::string_view name, bool defaultValue, bool value)
Create a boolean property.
Definition: cscore_oo.inc:435
ImageSource()=default
void SetDescription(std::string_view description)
Set source description.
Definition: cscore_oo.inc:406
VideoProperty CreateIntegerProperty(std::string_view name, int minimum, int maximum, int step, int defaultValue, int value)
Create an integer property.
Definition: cscore_oo.inc:422
VideoProperty CreateProperty(std::string_view name, VideoProperty::Kind kind, int minimum, int maximum, int step, int defaultValue, int value)
Create a property.
Definition: cscore_oo.inc:411
void NotifyError(std::string_view msg)
Signal sinks that an error has occurred.
Definition: cscore_oo.inc:396
void SetConnected(bool connected)
Set source connection status.
Definition: cscore_oo.inc:401
VideoProperty CreateStringProperty(std::string_view name, std::string_view value)
Create a string property.
Definition: cscore_oo.inc:446
void SetEnumPropertyChoices(const VideoProperty &property, std::span< const std::string > choices)
Configure enum property choices.
Definition: cscore_oo.inc:458
A sink that acts as a MJPEG-over-HTTP network server.
Definition: cscore_oo.h:1015
void SetFPS(int fps)
Set the stream frames per second (FPS) for clients that don't specify it.
Definition: cscore_oo.inc:572
void SetResolution(int width, int height)
Set the stream resolution for clients that don't specify it.
Definition: cscore_oo.inc:565
MjpegServer()=default
std::string GetListenAddress() const
Get the listen address of the server.
Definition: cscore_oo.inc:555
MjpegServer(std::string_view name, int port)
Create a MJPEG-over-HTTP server sink.
Definition: cscore_oo.h:1034
void SetDefaultCompression(int quality)
Set the default compression used for non-MJPEG sources.
Definition: cscore_oo.inc:583
void SetCompression(int quality)
Set the compression for clients that don't specify it.
Definition: cscore_oo.inc:577
int GetPort() const
Get the port number of the server.
Definition: cscore_oo.inc:560
A source that represents a USB camera.
Definition: cscore_oo.h:548
UsbCameraInfo GetInfo() const
Get the full camera information for the device.
Definition: cscore_oo.inc:278
static std::vector< UsbCameraInfo > EnumerateUsbCameras()
Enumerate USB cameras on the local system.
Definition: cscore_oo.inc:263
std::string GetPath() const
Get the path to the device.
Definition: cscore_oo.inc:273
void SetConnectVerbose(int level)
Set how verbose the camera connection messages are.
Definition: cscore_oo.inc:283
void SetPath(std::string_view path)
Change the path to the device.
Definition: cscore_oo.inc:268
UsbCamera()=default
A source that represents a video camera.
Definition: cscore_oo.h:481
VideoCamera(CS_Source handle)
Definition: cscore_oo.h:542
void SetExposureAuto()
Set the exposure to auto aperature.
Definition: cscore_oo.inc:240
void SetWhiteBalanceManual(int value)
Set the white balance to manual, with specified color temperature.
Definition: cscore_oo.inc:235
void SetWhiteBalanceAuto()
Set the white balance to auto.
Definition: cscore_oo.inc:225
int GetBrightness()
Get the brightness, as a percentage (0-100).
Definition: cscore_oo.inc:220
void SetBrightness(int brightness)
Set the brightness, as a percentage (0-100).
Definition: cscore_oo.inc:215
void SetWhiteBalanceHoldCurrent()
Set the white balance to hold current.
Definition: cscore_oo.inc:230
VideoCamera()=default
void SetExposureManual(int value)
Set the exposure to manual, as a percentage (0-100).
Definition: cscore_oo.inc:250
WhiteBalance
White balance.
Definition: cscore_oo.h:486
@ kFixedOutdoor1
Fixed outdoor white balance 1.
Definition: cscore_oo.h:490
@ kFixedFlourescent2
Fixed fluorescent white balance 2.
Definition: cscore_oo.h:496
@ kFixedIndoor
Fixed indoor white balance.
Definition: cscore_oo.h:488
@ kFixedFluorescent1
Fixed fluorescent white balance 1.
Definition: cscore_oo.h:494
@ kFixedOutdoor2
Fixed outdoor white balance 2.
Definition: cscore_oo.h:492
void SetExposureHoldCurrent()
Set the exposure to hold current.
Definition: cscore_oo.inc:245
An event generated by the library and provided to event listeners.
Definition: cscore_oo.h:1126
VideoSink GetSink() const
Returns the sink associated with the event (if any).
Definition: cscore_oo.inc:609
VideoProperty GetProperty() const
Returns the property associated with the event (if any).
Definition: cscore_oo.inc:614
VideoSource GetSource() const
Returns the source associated with the event (if any).
Definition: cscore_oo.inc:604
An event listener.
Definition: cscore_oo.h:1154
friend void swap(VideoListener &first, VideoListener &second) noexcept
Definition: cscore_oo.h:1175
~VideoListener()
Definition: cscore_oo.inc:640
VideoListener & operator=(const VideoListener &)=delete
VideoListener()=default
VideoListener(const VideoListener &)=delete
A source or sink property.
Definition: cscore_oo.h:39
int GetMin() const
Returns property minimum value.
Definition: cscore_oo.inc:35
int Get() const
Returns property value.
Definition: cscore_oo.inc:25
bool IsInteger() const
Returns true if property is an integer.
Definition: cscore_oo.h:94
VideoProperty()=default
void SetString(std::string_view value)
Sets the string property value.
Definition: cscore_oo.inc:66
bool IsEnum() const
Returns true if property is an enum.
Definition: cscore_oo.h:108
std::string GetName() const
Returns property name.
Definition: cscore_oo.inc:20
int GetDefault() const
Returns property default value.
Definition: cscore_oo.inc:50
Kind
Definition: cscore_oo.h:46
@ kString
String property.
Definition: cscore_oo.h:54
@ kInteger
Integer property.
Definition: cscore_oo.h:52
@ kEnum
Enum property.
Definition: cscore_oo.h:56
@ kBoolean
Boolean property.
Definition: cscore_oo.h:50
@ kNone
No specific property.
Definition: cscore_oo.h:48
CS_Status GetLastStatus() const
Returns the last status.
Definition: cscore_oo.h:194
void Set(int value)
Sets property value.
Definition: cscore_oo.inc:30
bool IsBoolean() const
Returns true if property is a boolean.
Definition: cscore_oo.h:87
std::vector< std::string > GetChoices() const
Returns the possible values for the enum property value.
Definition: cscore_oo.inc:71
int GetStep() const
Returns property step size.
Definition: cscore_oo.inc:45
Kind GetKind() const
Returns property kind.
Definition: cscore_oo.h:73
bool IsString() const
Returns true if property is a string.
Definition: cscore_oo.h:101
std::string GetString() const
Returns the string property value.
Definition: cscore_oo.inc:55
int GetMax() const
Returns property maximum value.
Definition: cscore_oo.inc:40
A sink for video that accepts a sequence of frames.
Definition: cscore_oo.h:852
void SetSource(VideoSource source)
Configure which source should provide frames to this sink.
Definition: cscore_oo.inc:515
Kind
Definition: cscore_oo.h:857
@ kCv
CV video sink.
Definition: cscore_oo.h:863
@ kUnknown
Unknown sink type.
Definition: cscore_oo.h:859
@ kRaw
Raw video sink.
Definition: cscore_oo.h:865
@ kMjpeg
MJPEG video sink.
Definition: cscore_oo.h:861
VideoProperty GetProperty(std::string_view name)
Get a property of the sink.
Definition: cscore_oo.inc:510
friend void swap(VideoSink &first, VideoSink &second) noexcept
Definition: cscore_oo.h:999
std::string GetDescription() const
Get the sink description.
Definition: cscore_oo.inc:505
std::vector< VideoProperty > EnumerateProperties() const
Enumerate all properties of this sink.
Kind GetKind() const
Get the kind of the sink.
Definition: cscore_oo.inc:495
VideoProperty GetSourceProperty(std::string_view name)
Get a property of the associated source.
Definition: cscore_oo.inc:530
bool SetConfigJson(std::string_view config)
Set properties from a JSON configuration string.
Definition: cscore_oo.inc:535
int GetHandle() const
Returns the VideoSink handle.
Definition: cscore_oo.h:886
VideoSource GetSource() const
Get the connected source.
Definition: cscore_oo.inc:524
CS_Status m_status
Definition: cscore_oo.h:1008
VideoSink() noexcept=default
std::string GetConfigJson() const
Get a JSON configuration string.
Definition: cscore_oo.inc:545
bool operator==(const VideoSink &other) const
Definition: cscore_oo.h:888
CS_Sink m_handle
Definition: cscore_oo.h:1009
VideoSink(CS_Sink handle)
Definition: cscore_oo.h:1006
static std::vector< VideoSink > EnumerateSinks()
Enumerate all existing sinks.
std::string GetName() const
Get the name of the sink.
Definition: cscore_oo.inc:500
CS_Status GetLastStatus() const
Definition: cscore_oo.h:990
wpi::json GetConfigJsonObject() const
Get a JSON configuration object.
A source for video that provides a sequence of frames.
Definition: cscore_oo.h:208
std::vector< VideoSink > EnumerateSinks()
Enumerate all sinks connected to this source.
bool SetPixelFormat(VideoMode::PixelFormat pixelFormat)
Set the pixel format.
Definition: cscore_oo.inc:168
double GetActualDataRate() const
Get the data rate (in bytes per second).
Definition: cscore_oo.inc:204
CS_Source m_handle
Video source handle.
Definition: cscore_oo.h:475
VideoSource() noexcept=default
VideoProperty GetProperty(std::string_view name)
Get a property.
Definition: cscore_oo.inc:146
VideoMode GetVideoMode() const
Get the current video mode.
Definition: cscore_oo.inc:151
CS_Status m_status
Definition: cscore_oo.h:472
int GetHandle() const
Definition: cscore_oo.h:258
friend void swap(VideoSource &first, VideoSource &second) noexcept
Definition: cscore_oo.h:463
VideoSource(CS_Source handle)
Definition: cscore_oo.h:470
std::vector< VideoProperty > EnumerateProperties() const
Enumerate all properties of this source.
bool IsConnected() const
Is the source currently connected to whatever is providing the images?
Definition: cscore_oo.inc:136
std::string GetDescription() const
Get the source description.
Definition: cscore_oo.inc:119
wpi::json GetConfigJsonObject() const
Get a JSON configuration object.
bool IsEnabled() const
Gets source enable status.
Definition: cscore_oo.inc:141
ConnectionStrategy
Connection strategy.
Definition: cscore_oo.h:230
@ kConnectionKeepOpen
Try to keep the connection open regardless of whether any sinks are connected.
Definition: cscore_oo.h:241
@ kConnectionAutoManage
Automatically connect or disconnect based on whether any sinks are connected to this source.
Definition: cscore_oo.h:235
@ kConnectionForceClose
Never open the connection.
Definition: cscore_oo.h:247
std::vector< VideoMode > EnumerateVideoModes() const
Enumerate all known video modes for this source.
Definition: cscore_oo.inc:210
uint64_t GetLastFrameTime() const
Get the last time a frame was captured.
Definition: cscore_oo.inc:124
double GetActualFPS() const
Get the actual FPS.
Definition: cscore_oo.inc:198
bool operator==(const VideoSource &other) const
Definition: cscore_oo.h:260
Kind
Video source kind.
Definition: cscore_oo.h:216
@ kCv
CV video source.
Definition: cscore_oo.h:224
@ kHttp
HTTP video source.
Definition: cscore_oo.h:222
@ kRaw
Raw video source.
Definition: cscore_oo.h:226
@ kUnknown
Unknown video source.
Definition: cscore_oo.h:218
@ kUsb
USB video source.
Definition: cscore_oo.h:220
bool SetConfigJson(std::string_view config)
Set video mode and properties from a JSON configuration string.
Definition: cscore_oo.inc:183
bool SetFPS(int fps)
Set the frames per second (FPS).
Definition: cscore_oo.inc:178
static std::vector< VideoSource > EnumerateSources()
Enumerate all existing sources.
void SetConnectionStrategy(ConnectionStrategy strategy)
Sets the connection strategy.
Definition: cscore_oo.inc:129
CS_Status GetLastStatus() const
Definition: cscore_oo.h:447
Kind GetKind() const
Get the kind of the source.
Definition: cscore_oo.inc:109
std::string GetConfigJson() const
Get a JSON configuration string.
Definition: cscore_oo.inc:193
bool SetResolution(int width, int height)
Set the resolution.
Definition: cscore_oo.inc:173
bool SetVideoMode(const VideoMode &mode)
Set the video mode.
Definition: cscore_oo.inc:156
std::string GetName() const
Get the name of the source.
Definition: cscore_oo.inc:114
basic_string_view< char > string_view
Definition: core.h:518
@ CS_CONNECTION_AUTO_MANAGE
Automatically connect or disconnect based on whether any sinks are connected to this source.
Definition: cscore_c.h:182
@ CS_CONNECTION_KEEP_OPEN
Try to keep the connection open regardless of whether any sinks are connected.
Definition: cscore_c.h:188
@ CS_CONNECTION_FORCE_CLOSE
Never open the connection.
Definition: cscore_c.h:194
@ CS_HTTP_MJPGSTREAMER
Definition: cscore_c.h:127
@ CS_HTTP_CSCORE
Definition: cscore_c.h:128
@ CS_HTTP_UNKNOWN
Definition: cscore_c.h:126
@ CS_HTTP_AXIS
Definition: cscore_c.h:129
@ CS_PROP_ENUM
Definition: cscore_c.h:108
@ CS_PROP_NONE
Definition: cscore_c.h:104
@ CS_PROP_INTEGER
Definition: cscore_c.h:106
@ CS_PROP_BOOLEAN
Definition: cscore_c.h:105
@ CS_PROP_STRING
Definition: cscore_c.h:107
@ CS_SINK_MJPEG
Definition: cscore_c.h:137
@ CS_SINK_RAW
Definition: cscore_c.h:139
@ CS_SINK_CV
Definition: cscore_c.h:138
@ CS_SINK_UNKNOWN
Definition: cscore_c.h:136
@ CS_SOURCE_USB
Definition: cscore_c.h:116
@ CS_SOURCE_HTTP
Definition: cscore_c.h:117
@ CS_SOURCE_UNKNOWN
Definition: cscore_c.h:115
@ CS_SOURCE_RAW
Definition: cscore_c.h:119
@ CS_SOURCE_CV
Definition: cscore_c.h:118
CS_Handle CS_Source
Definition: cscore_c.h:54
int CS_Status
Definition: cscore_c.h:47
CS_Handle CS_Property
Definition: cscore_c.h:50
CS_Handle CS_Sink
Definition: cscore_c.h:53
CS_Handle CS_Listener
Definition: cscore_c.h:51
CameraServer (cscore) namespace.
Definition: cscore_cpp.h:29
auto first(const T &value, const Tail &...) -> const T &
Definition: compile.h:60
WPI_BASIC_JSON_TPL_DECLARATION void swap(wpi::WPI_BASIC_JSON_TPL &j1, wpi::WPI_BASIC_JSON_TPL &j2) noexcept(//NOLINT(readability-inconsistent-declaration-parameter-name) is_nothrow_move_constructible< wpi::WPI_BASIC_JSON_TPL >::value &&//NOLINT(misc-redundant-expression) is_nothrow_move_assignable< wpi::WPI_BASIC_JSON_TPL >::value)
exchanges the values of two JSON objects
Definition: json.h:5219
fps
Definition: velocity.h:46
constexpr const char * name(const T &)
Listener event.
Definition: cscore_cpp.h:102
USB camera information.
Definition: cscore_cpp.h:44
Video mode.
Definition: cscore_cpp.h:62
PixelFormat
Definition: cscore_cpp.h:63