WPILibC++ 2024.3.2
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 };
226
227 /** Connection strategy. Used for SetConnectionStrategy(). */
229 /**
230 * Automatically connect or disconnect based on whether any sinks are
231 * connected to this source. This is the default behavior.
232 */
234
235 /**
236 * Try to keep the connection open regardless of whether any sinks are
237 * connected.
238 */
240
241 /**
242 * Never open the connection. If this is set when the connection is open,
243 * close the connection.
244 */
246 };
247
248 VideoSource() noexcept = default;
250 VideoSource(VideoSource&& other) noexcept;
251 VideoSource& operator=(VideoSource other) noexcept;
252 ~VideoSource();
253
254 explicit operator bool() const { return m_handle != 0; }
255
256 int GetHandle() const { return m_handle; }
257
258 bool operator==(const VideoSource& other) const {
259 return m_handle == other.m_handle;
260 }
261
262 /**
263 * Get the kind of the source.
264 */
265 Kind GetKind() const;
266
267 /**
268 * Get the name of the source. The name is an arbitrary identifier
269 * provided when the source is created, and should be unique.
270 */
271 std::string GetName() const;
272
273 /**
274 * Get the source description. This is source-kind specific.
275 */
276 std::string GetDescription() const;
277
278 /**
279 * Get the last time a frame was captured.
280 * This uses the same time base as wpi::Now().
281 *
282 * @return Time in 1 us increments.
283 */
284 uint64_t GetLastFrameTime() const;
285
286 /**
287 * Sets the connection strategy. By default, the source will automatically
288 * connect or disconnect based on whether any sinks are connected.
289 *
290 * <p>This function is non-blocking; look for either a connection open or
291 * close event or call IsConnected() to determine the connection state.
292 *
293 * @param strategy connection strategy (auto, keep open, or force close)
294 */
296
297 /**
298 * Is the source currently connected to whatever is providing the images?
299 */
300 bool IsConnected() const;
301
302 /**
303 * Gets source enable status. This is determined with a combination of
304 * connection strategy and the number of sinks connected.
305 *
306 * @return True if enabled, false otherwise.
307 */
308 bool IsEnabled() const;
309
310 /** Get a property.
311 *
312 * @param name Property name
313 * @return Property contents (of kind Property::kNone if no property with
314 * the given name exists)
315 */
317
318 /**
319 * Enumerate all properties of this source.
320 */
321 std::vector<VideoProperty> EnumerateProperties() const;
322
323 /**
324 * Get the current video mode.
325 */
326 VideoMode GetVideoMode() const;
327
328 /**
329 * Set the video mode.
330 *
331 * @param mode Video mode
332 */
333 bool SetVideoMode(const VideoMode& mode);
334
335 /**
336 * Set the video mode.
337 *
338 * @param pixelFormat desired pixel format
339 * @param width desired width
340 * @param height desired height
341 * @param fps desired FPS
342 * @return True if set successfully
343 */
344 bool SetVideoMode(VideoMode::PixelFormat pixelFormat, int width, int height,
345 int fps);
346
347 /**
348 * Set the pixel format.
349 *
350 * @param pixelFormat desired pixel format
351 * @return True if set successfully
352 */
353 bool SetPixelFormat(VideoMode::PixelFormat pixelFormat);
354
355 /**
356 * Set the resolution.
357 *
358 * @param width desired width
359 * @param height desired height
360 * @return True if set successfully
361 */
362 bool SetResolution(int width, int height);
363
364 /**
365 * Set the frames per second (FPS).
366 *
367 * @param fps desired FPS
368 * @return True if set successfully
369 */
370 bool SetFPS(int fps);
371
372 /**
373 * Set video mode and properties from a JSON configuration string.
374 *
375 * The format of the JSON input is:
376 *
377 * <pre>
378 * {
379 * "pixel format": "MJPEG", "YUYV", etc
380 * "width": video mode width
381 * "height": video mode height
382 * "fps": video mode fps
383 * "brightness": percentage brightness
384 * "white balance": "auto", "hold", or value
385 * "exposure": "auto", "hold", or value
386 * "properties": [
387 * {
388 * "name": property name
389 * "value": property value
390 * }
391 * ]
392 * }
393 * </pre>
394 *
395 * @param config configuration
396 * @return True if set successfully
397 */
398 bool SetConfigJson(std::string_view config);
399
400 /**
401 * Set video mode and properties from a JSON configuration object.
402 *
403 * @param config configuration
404 * @return True if set successfully
405 */
406 bool SetConfigJson(const wpi::json& config);
407
408 /**
409 * Get a JSON configuration string.
410 *
411 * @return JSON configuration string
412 */
413 std::string GetConfigJson() const;
414
415 /**
416 * Get a JSON configuration object.
417 *
418 * @return JSON configuration object
419 */
420 wpi::json GetConfigJsonObject() const;
421
422 /**
423 * Get the actual FPS.
424 *
425 * <p>SetTelemetryPeriod() must be called for this to be valid.
426 *
427 * @return Actual FPS averaged over the telemetry period.
428 */
429 double GetActualFPS() const;
430
431 /**
432 * Get the data rate (in bytes per second).
433 *
434 * <p>SetTelemetryPeriod() must be called for this to be valid.
435 *
436 * @return Data rate averaged over the telemetry period.
437 */
438 double GetActualDataRate() const;
439
440 /**
441 * Enumerate all known video modes for this source.
442 */
443 std::vector<VideoMode> EnumerateVideoModes() const;
444
445 CS_Status GetLastStatus() const { return m_status; }
446
447 /**
448 * Enumerate all sinks connected to this source.
449 *
450 * @return Vector of sinks.
451 */
452 std::vector<VideoSink> EnumerateSinks();
453
454 /**
455 * Enumerate all existing sources.
456 *
457 * @return Vector of sources.
458 */
459 static std::vector<VideoSource> EnumerateSources();
460
461 friend void swap(VideoSource& first, VideoSource& second) noexcept {
462 using std::swap;
463 swap(first.m_status, second.m_status);
464 swap(first.m_handle, second.m_handle);
465 }
466
467 protected:
468 explicit VideoSource(CS_Source handle) : m_handle(handle) {}
469
470 mutable CS_Status m_status = 0;
471
472 /// Video source handle.
474};
475
476/**
477 * A source that represents a video camera.
478 */
479class VideoCamera : public VideoSource {
480 public:
481 /**
482 * White balance.
483 */
485 /// Fixed indoor white balance.
487 /// Fixed outdoor white balance 1.
489 /// Fixed outdoor white balance 2.
491 /// Fixed fluorescent white balance 1.
493 /// Fixed fluorescent white balance 2.
494 kFixedFlourescent2 = 5200
495 };
496
497 VideoCamera() = default;
498
499 /**
500 * Set the brightness, as a percentage (0-100).
501 */
502 void SetBrightness(int brightness);
503
504 /**
505 * Get the brightness, as a percentage (0-100).
506 */
507 int GetBrightness();
508
509 /**
510 * Set the white balance to auto.
511 */
512 void SetWhiteBalanceAuto();
513
514 /**
515 * Set the white balance to hold current.
516 */
518
519 /**
520 * Set the white balance to manual, with specified color temperature.
521 */
522 void SetWhiteBalanceManual(int value);
523
524 /**
525 * Set the exposure to auto aperature.
526 */
527 void SetExposureAuto();
528
529 /**
530 * Set the exposure to hold current.
531 */
533
534 /**
535 * Set the exposure to manual, as a percentage (0-100).
536 */
537 void SetExposureManual(int value);
538
539 protected:
540 explicit VideoCamera(CS_Source handle) : VideoSource(handle) {}
541};
542
543/**
544 * A source that represents a USB camera.
545 */
546class UsbCamera : public VideoCamera {
547 public:
548 UsbCamera() = default;
549
550 /**
551 * Create a source for a USB camera based on device number.
552 *
553 * @param name Source name (arbitrary unique identifier)
554 * @param dev Device number (e.g. 0 for /dev/video0)
555 */
556 UsbCamera(std::string_view name, int dev);
557
558 /**
559 * Create a source for a USB camera based on device path.
560 *
561 * @param name Source name (arbitrary unique identifier)
562 * @param path Path to device (e.g. "/dev/video0" on Linux)
563 */
565
566 /**
567 * Enumerate USB cameras on the local system.
568 *
569 * @return Vector of USB camera information (one for each camera)
570 */
571 static std::vector<UsbCameraInfo> EnumerateUsbCameras();
572
573 /**
574 * Change the path to the device.
575 */
576 void SetPath(std::string_view path);
577
578 /**
579 * Get the path to the device.
580 */
581 std::string GetPath() const;
582
583 /**
584 * Get the full camera information for the device.
585 */
586 UsbCameraInfo GetInfo() const;
587
588 /**
589 * Set how verbose the camera connection messages are.
590 *
591 * @param level 0=don't display Connecting message, 1=do display message
592 */
593 void SetConnectVerbose(int level);
594};
595
596/**
597 * A source that represents a MJPEG-over-HTTP (IP) camera.
598 */
599class HttpCamera : public VideoCamera {
600 public:
601 /**
602 * HTTP camera kind.
603 */
605 /// Unknown camera kind.
607 /// MJPG Streamer camera.
609 /// CS Core camera.
611 /// Axis camera.
613 };
614
615 /**
616 * Create a source for a MJPEG-over-HTTP (IP) camera.
617 *
618 * @param name Source name (arbitrary unique identifier)
619 * @param url Camera URL (e.g. "http://10.x.y.11/video/stream.mjpg")
620 * @param kind Camera kind (e.g. kAxis)
621 */
623 HttpCameraKind kind = kUnknown);
624
625 /**
626 * Create a source for a MJPEG-over-HTTP (IP) camera.
627 *
628 * @param name Source name (arbitrary unique identifier)
629 * @param url Camera URL (e.g. "http://10.x.y.11/video/stream.mjpg")
630 * @param kind Camera kind (e.g. kAxis)
631 */
632 HttpCamera(std::string_view name, const char* url,
633 HttpCameraKind kind = kUnknown);
634
635 /**
636 * Create a source for a MJPEG-over-HTTP (IP) camera.
637 *
638 * @param name Source name (arbitrary unique identifier)
639 * @param url Camera URL (e.g. "http://10.x.y.11/video/stream.mjpg")
640 * @param kind Camera kind (e.g. kAxis)
641 */
642 HttpCamera(std::string_view name, const std::string& url,
643 HttpCameraKind kind = kUnknown);
644
645 /**
646 * Create a source for a MJPEG-over-HTTP (IP) camera.
647 *
648 * @param name Source name (arbitrary unique identifier)
649 * @param urls Array of Camera URLs
650 * @param kind Camera kind (e.g. kAxis)
651 */
652 HttpCamera(std::string_view name, std::span<const std::string> urls,
653 HttpCameraKind kind = kUnknown);
654
655 /**
656 * Create a source for a MJPEG-over-HTTP (IP) camera.
657 *
658 * @param name Source name (arbitrary unique identifier)
659 * @param urls Array of Camera URLs
660 * @param kind Camera kind (e.g. kAxis)
661 */
662 template <typename T>
663 HttpCamera(std::string_view name, std::initializer_list<T> urls,
664 HttpCameraKind kind = kUnknown);
665
666 /**
667 * Get the kind of HTTP camera.
668 *
669 * <p>Autodetection can result in returning a different value than the camera
670 * was created with.
671 */
673
674 /**
675 * Change the URLs used to connect to the camera.
676 */
677 void SetUrls(std::span<const std::string> urls);
678
679 /**
680 * Change the URLs used to connect to the camera.
681 */
682 template <typename T>
683 void SetUrls(std::initializer_list<T> urls);
684
685 /**
686 * Get the URLs used to connect to the camera.
687 */
688 std::vector<std::string> GetUrls() const;
689};
690
691/**
692 * A source that represents an Axis IP camera.
693 */
694class AxisCamera : public HttpCamera {
695 static std::string HostToUrl(std::string_view host);
696 static std::vector<std::string> HostToUrl(std::span<const std::string> hosts);
697 template <typename T>
698 static std::vector<std::string> HostToUrl(std::initializer_list<T> hosts);
699
700 public:
701 /**
702 * Create a source for an Axis IP camera.
703 *
704 * @param name Source name (arbitrary unique identifier)
705 * @param host Camera host IP or DNS name (e.g. "10.x.y.11")
706 */
708
709 /**
710 * Create a source for an Axis IP camera.
711 *
712 * @param name Source name (arbitrary unique identifier)
713 * @param host Camera host IP or DNS name (e.g. "10.x.y.11")
714 */
715 AxisCamera(std::string_view name, const char* host);
716
717 /**
718 * Create a source for an Axis IP camera.
719 *
720 * @param name Source name (arbitrary unique identifier)
721 * @param host Camera host IP or DNS name (e.g. "10.x.y.11")
722 */
723 AxisCamera(std::string_view name, const std::string& host);
724
725 /**
726 * Create a source for an Axis IP camera.
727 *
728 * @param name Source name (arbitrary unique identifier)
729 * @param hosts Array of Camera host IPs/DNS names
730 */
731 AxisCamera(std::string_view name, std::span<const std::string> hosts);
732
733 /**
734 * Create a source for an Axis IP camera.
735 *
736 * @param name Source name (arbitrary unique identifier)
737 * @param hosts Array of Camera host IPs/DNS names
738 */
739 template <typename T>
740 AxisCamera(std::string_view name, std::initializer_list<T> hosts);
741};
742
743/**
744 * A base class for single image providing sources.
745 */
746class ImageSource : public VideoSource {
747 protected:
748 ImageSource() = default;
749
750 public:
751 /**
752 * Signal sinks that an error has occurred. This should be called instead
753 * of NotifyFrame when an error occurs.
754 *
755 * @param msg Notification message.
756 */
758
759 /**
760 * Set source connection status. Defaults to true.
761 *
762 * @param connected True for connected, false for disconnected
763 */
764 void SetConnected(bool connected);
765
766 /**
767 * Set source description.
768 *
769 * @param description Description
770 */
771 void SetDescription(std::string_view description);
772
773 /**
774 * Create a property.
775 *
776 * @param name Property name
777 * @param kind Property kind
778 * @param minimum Minimum value
779 * @param maximum Maximum value
780 * @param step Step value
781 * @param defaultValue Default value
782 * @param value Current value
783 * @return Property
784 */
786 int minimum, int maximum, int step,
787 int defaultValue, int value);
788
789 /**
790 * Create an integer property.
791 *
792 * @param name Property name
793 * @param minimum Minimum value
794 * @param maximum Maximum value
795 * @param step Step value
796 * @param defaultValue Default value
797 * @param value Current value
798 * @return Property
799 */
801 int maximum, int step, int defaultValue,
802 int value);
803
804 /**
805 * Create a boolean property.
806 *
807 * @param name Property name
808 * @param defaultValue Default value
809 * @param value Current value
810 * @return Property
811 */
813 bool value);
814
815 /**
816 * Create a string property.
817 *
818 * @param name Property name
819 * @param value Current value
820 * @return Property
821 */
823 std::string_view value);
824
825 /**
826 * Configure enum property choices.
827 *
828 * @param property Property
829 * @param choices Choices
830 */
831 void SetEnumPropertyChoices(const VideoProperty& property,
832 std::span<const std::string> choices);
833
834 /**
835 * Configure enum property choices.
836 *
837 * @param property Property
838 * @param choices Choices
839 */
840 template <typename T>
841 void SetEnumPropertyChoices(const VideoProperty& property,
842 std::initializer_list<T> choices);
843};
844
845/**
846 * A sink for video that accepts a sequence of frames.
847 */
849 friend class VideoEvent;
850 friend class VideoSource;
851
852 public:
853 enum Kind {
854 /// Unknown sink type.
856 /// MJPEG video sink.
858 /// CV video sink.
860 };
861
862 VideoSink() noexcept = default;
863 VideoSink(const VideoSink& sink);
864 VideoSink(VideoSink&& sink) noexcept;
865 VideoSink& operator=(VideoSink other) noexcept;
866 ~VideoSink();
867
868 /**
869 * Returns true if the VideoSink is valid.
870 *
871 * @return True if the VideoSink is valid.
872 */
873 explicit operator bool() const { return m_handle != 0; }
874
875 /**
876 * Returns the VideoSink handle.
877 *
878 * @return The VideoSink handle.
879 */
880 int GetHandle() const { return m_handle; }
881
882 bool operator==(const VideoSink& other) const {
883 return m_handle == other.m_handle;
884 }
885
886 /**
887 * Get the kind of the sink.
888 */
889 Kind GetKind() const;
890
891 /**
892 * Get the name of the sink. The name is an arbitrary identifier
893 * provided when the sink is created, and should be unique.
894 */
895 std::string GetName() const;
896
897 /**
898 * Get the sink description. This is sink-kind specific.
899 */
900 std::string GetDescription() const;
901
902 /**
903 * Get a property of the sink.
904 *
905 * @param name Property name
906 * @return Property (kind Property::kNone if no property with
907 * the given name exists)
908 */
910
911 /**
912 * Enumerate all properties of this sink.
913 */
914 std::vector<VideoProperty> EnumerateProperties() const;
915
916 /**
917 * Set properties from a JSON configuration string.
918 *
919 * The format of the JSON input is:
920 *
921 * <pre>
922 * {
923 * "properties": [
924 * {
925 * "name": property name
926 * "value": property value
927 * }
928 * ]
929 * }
930 * </pre>
931 *
932 * @param config configuration
933 * @return True if set successfully
934 */
935 bool SetConfigJson(std::string_view config);
936
937 /**
938 * Set properties from a JSON configuration object.
939 *
940 * @param config configuration
941 * @return True if set successfully
942 */
943 bool SetConfigJson(const wpi::json& config);
944
945 /**
946 * Get a JSON configuration string.
947 *
948 * @return JSON configuration string
949 */
950 std::string GetConfigJson() const;
951
952 /**
953 * Get a JSON configuration object.
954 *
955 * @return JSON configuration object
956 */
957 wpi::json GetConfigJsonObject() const;
958
959 /**
960 * Configure which source should provide frames to this sink. Each sink
961 * can accept frames from only a single source, but a single source can
962 * provide frames to multiple clients.
963 *
964 * @param source Source
965 */
967
968 /**
969 * Get the connected source.
970 *
971 * @return Connected source (empty if none connected).
972 */
973 VideoSource GetSource() const;
974
975 /**
976 * Get a property of the associated source.
977 *
978 * @param name Property name
979 * @return Property (kind Property::kNone if no property with
980 * the given name exists or no source connected)
981 */
983
984 CS_Status GetLastStatus() const { return m_status; }
985
986 /**
987 * Enumerate all existing sinks.
988 *
989 * @return Vector of sinks.
990 */
991 static std::vector<VideoSink> EnumerateSinks();
992
993 friend void swap(VideoSink& first, VideoSink& second) noexcept {
994 using std::swap;
995 swap(first.m_status, second.m_status);
996 swap(first.m_handle, second.m_handle);
997 }
998
999 protected:
1000 explicit VideoSink(CS_Sink handle) : m_handle(handle) {}
1001
1002 mutable CS_Status m_status = 0;
1004};
1005
1006/**
1007 * A sink that acts as a MJPEG-over-HTTP network server.
1008 */
1009class MjpegServer : public VideoSink {
1010 public:
1011 MjpegServer() = default;
1012
1013 /**
1014 * Create a MJPEG-over-HTTP server sink.
1015 *
1016 * @param name Sink name (arbitrary unique identifier)
1017 * @param listenAddress TCP listen address (empty string for all addresses)
1018 * @param port TCP port number
1019 */
1020 MjpegServer(std::string_view name, std::string_view listenAddress, int port);
1021
1022 /**
1023 * Create a MJPEG-over-HTTP server sink.
1024 *
1025 * @param name Sink name (arbitrary unique identifier)
1026 * @param port TCP port number
1027 */
1028 MjpegServer(std::string_view name, int port) : MjpegServer(name, "", port) {}
1029
1030 /**
1031 * Get the listen address of the server.
1032 */
1033 std::string GetListenAddress() const;
1034
1035 /**
1036 * Get the port number of the server.
1037 */
1038 int GetPort() const;
1039
1040 /**
1041 * Set the stream resolution for clients that don't specify it.
1042 *
1043 * <p>It is not necessary to set this if it is the same as the source
1044 * resolution.
1045 *
1046 * <p>Setting this different than the source resolution will result in
1047 * increased CPU usage, particularly for MJPEG source cameras, as it will
1048 * decompress, resize, and recompress the image, instead of using the
1049 * camera's MJPEG image directly.
1050 *
1051 * @param width width, 0 for unspecified
1052 * @param height height, 0 for unspecified
1053 */
1054 void SetResolution(int width, int height);
1055
1056 /**
1057 * Set the stream frames per second (FPS) for clients that don't specify it.
1058 *
1059 * <p>It is not necessary to set this if it is the same as the source FPS.
1060 *
1061 * @param fps FPS, 0 for unspecified
1062 */
1063 void SetFPS(int fps);
1064
1065 /**
1066 * Set the compression for clients that don't specify it.
1067 *
1068 * <p>Setting this will result in increased CPU usage for MJPEG source cameras
1069 * as it will decompress and recompress the image instead of using the
1070 * camera's MJPEG image directly.
1071 *
1072 * @param quality JPEG compression quality (0-100), -1 for unspecified
1073 */
1074 void SetCompression(int quality);
1075
1076 /**
1077 * Set the default compression used for non-MJPEG sources. If not set,
1078 * 80 is used. This function has no effect on MJPEG source cameras; use
1079 * SetCompression() instead to force recompression of MJPEG source images.
1080 *
1081 * @param quality JPEG compression quality (0-100)
1082 */
1083 void SetDefaultCompression(int quality);
1084};
1085
1086/**
1087 * A base class for single image reading sinks.
1088 */
1089class ImageSink : public VideoSink {
1090 protected:
1091 ImageSink() = default;
1092
1093 public:
1094 /**
1095 * Set sink description.
1096 *
1097 * @param description Description
1098 */
1099 void SetDescription(std::string_view description);
1100
1101 /**
1102 * Get error string. Call this if WaitForFrame() returns 0 to determine
1103 * what the error is.
1104 */
1105 std::string GetError() const;
1106
1107 /**
1108 * Enable or disable getting new frames.
1109 *
1110 * <p>Disabling will cause processFrame (for callback-based CvSinks) to not
1111 * be called and WaitForFrame() to not return. This can be used to save
1112 * processor resources when frames are not needed.
1113 */
1114 void SetEnabled(bool enabled);
1115};
1116
1117/**
1118 * An event generated by the library and provided to event listeners.
1119 */
1120class VideoEvent : public RawEvent {
1121 public:
1122 /**
1123 * Returns the source associated with the event (if any).
1124 *
1125 * @return The source associated with the event (if any).
1126 */
1127 VideoSource GetSource() const;
1128
1129 /**
1130 * Returns the sink associated with the event (if any).
1131 *
1132 * @return The sink associated with the event (if any).
1133 */
1134 VideoSink GetSink() const;
1135
1136 /**
1137 * Returns the property associated with the event (if any).
1138 *
1139 * @return The property associated with the event (if any).
1140 */
1141 VideoProperty GetProperty() const;
1142};
1143
1144/**
1145 * An event listener. This calls back to a desigated callback function when
1146 * an event matching the specified mask is generated by the library.
1147 */
1149 public:
1150 VideoListener() = default;
1151
1152 /**
1153 * Create an event listener.
1154 *
1155 * @param callback Callback function
1156 * @param eventMask Bitmask of VideoEvent::Kind values
1157 * @param immediateNotify Whether callback should be immediately called with
1158 * a representative set of events for the current library state.
1159 */
1160 VideoListener(std::function<void(const VideoEvent& event)> callback,
1161 int eventMask, bool immediateNotify);
1162
1165 VideoListener(VideoListener&& other) noexcept;
1166 VideoListener& operator=(VideoListener&& other) noexcept;
1168
1169 friend void swap(VideoListener& first, VideoListener& second) noexcept {
1170 using std::swap;
1171 swap(first.m_handle, second.m_handle);
1172 }
1173
1174 private:
1175 CS_Listener m_handle{0};
1176};
1177
1178/** @} */
1179
1180} // namespace cs
1181
1182#include "cscore_oo.inc"
1183
1184#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:111
A source that represents an Axis IP camera.
Definition: cscore_oo.h:694
AxisCamera(std::string_view name, std::string_view host)
Create a source for an Axis IP camera.
Definition: cscore_oo.inc:375
A source that represents a MJPEG-over-HTTP (IP) camera.
Definition: cscore_oo.h:599
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:287
std::vector< std::string > GetUrls() const
Get the URLs used to connect to the camera.
Definition: cscore_oo.inc:349
void SetUrls(std::span< const std::string > urls)
Change the URLs used to connect to the camera.
Definition: cscore_oo.inc:333
HttpCameraKind
HTTP camera kind.
Definition: cscore_oo.h:604
@ kMJPGStreamer
MJPG Streamer camera.
Definition: cscore_oo.h:608
@ kCSCore
CS Core camera.
Definition: cscore_oo.h:610
@ kAxis
Axis camera.
Definition: cscore_oo.h:612
@ kUnknown
Unknown camera kind.
Definition: cscore_oo.h:606
HttpCameraKind GetHttpCameraKind() const
Get the kind of HTTP camera.
Definition: cscore_oo.inc:327
A base class for single image reading sinks.
Definition: cscore_oo.h:1089
void SetEnabled(bool enabled)
Enable or disable getting new frames.
Definition: cscore_oo.inc:596
std::string GetError() const
Get error string.
Definition: cscore_oo.inc:591
void SetDescription(std::string_view description)
Set sink description.
Definition: cscore_oo.inc:586
ImageSink()=default
A base class for single image providing sources.
Definition: cscore_oo.h:746
VideoProperty CreateBooleanProperty(std::string_view name, bool defaultValue, bool value)
Create a boolean property.
Definition: cscore_oo.inc:432
ImageSource()=default
void SetDescription(std::string_view description)
Set source description.
Definition: cscore_oo.inc:403
VideoProperty CreateIntegerProperty(std::string_view name, int minimum, int maximum, int step, int defaultValue, int value)
Create an integer property.
Definition: cscore_oo.inc:419
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:408
void SetConnected(bool connected)
Set source connection status.
Definition: cscore_oo.inc:398
VideoProperty CreateStringProperty(std::string_view name, std::string_view value)
Create a string property.
Definition: cscore_oo.inc:443
void NotifyError(std::string_view msg)
Signal sinks that an error has occurred.
Definition: cscore_oo.inc:393
void SetEnumPropertyChoices(const VideoProperty &property, std::span< const std::string > choices)
Configure enum property choices.
Definition: cscore_oo.inc:455
A sink that acts as a MJPEG-over-HTTP network server.
Definition: cscore_oo.h:1009
void SetFPS(int fps)
Set the stream frames per second (FPS) for clients that don't specify it.
Definition: cscore_oo.inc:569
void SetResolution(int width, int height)
Set the stream resolution for clients that don't specify it.
Definition: cscore_oo.inc:562
MjpegServer()=default
std::string GetListenAddress() const
Get the listen address of the server.
Definition: cscore_oo.inc:552
MjpegServer(std::string_view name, int port)
Create a MJPEG-over-HTTP server sink.
Definition: cscore_oo.h:1028
void SetDefaultCompression(int quality)
Set the default compression used for non-MJPEG sources.
Definition: cscore_oo.inc:580
void SetCompression(int quality)
Set the compression for clients that don't specify it.
Definition: cscore_oo.inc:574
int GetPort() const
Get the port number of the server.
Definition: cscore_oo.inc:557
A source that represents a USB camera.
Definition: cscore_oo.h:546
UsbCameraInfo GetInfo() const
Get the full camera information for the device.
Definition: cscore_oo.inc:276
static std::vector< UsbCameraInfo > EnumerateUsbCameras()
Enumerate USB cameras on the local system.
Definition: cscore_oo.inc:261
std::string GetPath() const
Get the path to the device.
Definition: cscore_oo.inc:271
void SetConnectVerbose(int level)
Set how verbose the camera connection messages are.
Definition: cscore_oo.inc:281
void SetPath(std::string_view path)
Change the path to the device.
Definition: cscore_oo.inc:266
UsbCamera()=default
A source that represents a video camera.
Definition: cscore_oo.h:479
VideoCamera(CS_Source handle)
Definition: cscore_oo.h:540
void SetExposureAuto()
Set the exposure to auto aperature.
Definition: cscore_oo.inc:238
void SetWhiteBalanceManual(int value)
Set the white balance to manual, with specified color temperature.
Definition: cscore_oo.inc:233
void SetWhiteBalanceAuto()
Set the white balance to auto.
Definition: cscore_oo.inc:223
int GetBrightness()
Get the brightness, as a percentage (0-100).
Definition: cscore_oo.inc:218
void SetBrightness(int brightness)
Set the brightness, as a percentage (0-100).
Definition: cscore_oo.inc:213
void SetWhiteBalanceHoldCurrent()
Set the white balance to hold current.
Definition: cscore_oo.inc:228
VideoCamera()=default
void SetExposureManual(int value)
Set the exposure to manual, as a percentage (0-100).
Definition: cscore_oo.inc:248
WhiteBalance
White balance.
Definition: cscore_oo.h:484
@ kFixedOutdoor1
Fixed outdoor white balance 1.
Definition: cscore_oo.h:488
@ kFixedFlourescent2
Fixed fluorescent white balance 2.
Definition: cscore_oo.h:494
@ kFixedIndoor
Fixed indoor white balance.
Definition: cscore_oo.h:486
@ kFixedFluorescent1
Fixed fluorescent white balance 1.
Definition: cscore_oo.h:492
@ kFixedOutdoor2
Fixed outdoor white balance 2.
Definition: cscore_oo.h:490
void SetExposureHoldCurrent()
Set the exposure to hold current.
Definition: cscore_oo.inc:243
An event generated by the library and provided to event listeners.
Definition: cscore_oo.h:1120
VideoSink GetSink() const
Returns the sink associated with the event (if any).
Definition: cscore_oo.inc:606
VideoProperty GetProperty() const
Returns the property associated with the event (if any).
Definition: cscore_oo.inc:611
VideoSource GetSource() const
Returns the source associated with the event (if any).
Definition: cscore_oo.inc:601
An event listener.
Definition: cscore_oo.h:1148
friend void swap(VideoListener &first, VideoListener &second) noexcept
Definition: cscore_oo.h:1169
~VideoListener()
Definition: cscore_oo.inc:637
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:33
int Get() const
Returns property value.
Definition: cscore_oo.inc:23
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:64
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:18
int GetDefault() const
Returns property default value.
Definition: cscore_oo.inc:48
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:28
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:69
int GetStep() const
Returns property step size.
Definition: cscore_oo.inc:43
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:53
int GetMax() const
Returns property maximum value.
Definition: cscore_oo.inc:38
A sink for video that accepts a sequence of frames.
Definition: cscore_oo.h:848
void SetSource(VideoSource source)
Configure which source should provide frames to this sink.
Definition: cscore_oo.inc:512
Kind
Definition: cscore_oo.h:853
@ kCv
CV video sink.
Definition: cscore_oo.h:859
@ kUnknown
Unknown sink type.
Definition: cscore_oo.h:855
@ kMjpeg
MJPEG video sink.
Definition: cscore_oo.h:857
VideoProperty GetProperty(std::string_view name)
Get a property of the sink.
Definition: cscore_oo.inc:507
friend void swap(VideoSink &first, VideoSink &second) noexcept
Definition: cscore_oo.h:993
std::string GetDescription() const
Get the sink description.
Definition: cscore_oo.inc:502
std::vector< VideoProperty > EnumerateProperties() const
Enumerate all properties of this sink.
Kind GetKind() const
Get the kind of the sink.
Definition: cscore_oo.inc:492
VideoProperty GetSourceProperty(std::string_view name)
Get a property of the associated source.
Definition: cscore_oo.inc:527
bool SetConfigJson(std::string_view config)
Set properties from a JSON configuration string.
Definition: cscore_oo.inc:532
int GetHandle() const
Returns the VideoSink handle.
Definition: cscore_oo.h:880
VideoSource GetSource() const
Get the connected source.
Definition: cscore_oo.inc:521
CS_Status m_status
Definition: cscore_oo.h:1002
VideoSink() noexcept=default
std::string GetConfigJson() const
Get a JSON configuration string.
Definition: cscore_oo.inc:542
bool operator==(const VideoSink &other) const
Definition: cscore_oo.h:882
CS_Sink m_handle
Definition: cscore_oo.h:1003
VideoSink(CS_Sink handle)
Definition: cscore_oo.h:1000
static std::vector< VideoSink > EnumerateSinks()
Enumerate all existing sinks.
std::string GetName() const
Get the name of the sink.
Definition: cscore_oo.inc:497
CS_Status GetLastStatus() const
Definition: cscore_oo.h:984
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:166
double GetActualDataRate() const
Get the data rate (in bytes per second).
Definition: cscore_oo.inc:202
CS_Source m_handle
Video source handle.
Definition: cscore_oo.h:473
VideoSource() noexcept=default
VideoProperty GetProperty(std::string_view name)
Get a property.
Definition: cscore_oo.inc:144
VideoMode GetVideoMode() const
Get the current video mode.
Definition: cscore_oo.inc:149
CS_Status m_status
Definition: cscore_oo.h:470
int GetHandle() const
Definition: cscore_oo.h:256
friend void swap(VideoSource &first, VideoSource &second) noexcept
Definition: cscore_oo.h:461
VideoSource(CS_Source handle)
Definition: cscore_oo.h:468
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:134
std::string GetDescription() const
Get the source description.
Definition: cscore_oo.inc:117
wpi::json GetConfigJsonObject() const
Get a JSON configuration object.
bool IsEnabled() const
Gets source enable status.
Definition: cscore_oo.inc:139
ConnectionStrategy
Connection strategy.
Definition: cscore_oo.h:228
@ kConnectionKeepOpen
Try to keep the connection open regardless of whether any sinks are connected.
Definition: cscore_oo.h:239
@ kConnectionAutoManage
Automatically connect or disconnect based on whether any sinks are connected to this source.
Definition: cscore_oo.h:233
@ kConnectionForceClose
Never open the connection.
Definition: cscore_oo.h:245
std::vector< VideoMode > EnumerateVideoModes() const
Enumerate all known video modes for this source.
Definition: cscore_oo.inc:208
uint64_t GetLastFrameTime() const
Get the last time a frame was captured.
Definition: cscore_oo.inc:122
double GetActualFPS() const
Get the actual FPS.
Definition: cscore_oo.inc:196
bool operator==(const VideoSource &other) const
Definition: cscore_oo.h:258
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
@ 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:181
bool SetFPS(int fps)
Set the frames per second (FPS).
Definition: cscore_oo.inc:176
static std::vector< VideoSource > EnumerateSources()
Enumerate all existing sources.
void SetConnectionStrategy(ConnectionStrategy strategy)
Sets the connection strategy.
Definition: cscore_oo.inc:127
CS_Status GetLastStatus() const
Definition: cscore_oo.h:445
Kind GetKind() const
Get the kind of the source.
Definition: cscore_oo.inc:107
std::string GetConfigJson() const
Get a JSON configuration string.
Definition: cscore_oo.inc:191
bool SetResolution(int width, int height)
Set the resolution.
Definition: cscore_oo.inc:171
bool SetVideoMode(const VideoMode &mode)
Set the video mode.
Definition: cscore_oo.inc:154
std::string GetName() const
Get the name of the source.
Definition: cscore_oo.inc:112
basic_string_view< char > string_view
Definition: core.h:501
@ CS_CONNECTION_AUTO_MANAGE
Automatically connect or disconnect based on whether any sinks are connected to this source.
Definition: cscore_c.h:181
@ CS_CONNECTION_KEEP_OPEN
Try to keep the connection open regardless of whether any sinks are connected.
Definition: cscore_c.h:187
@ CS_CONNECTION_FORCE_CLOSE
Never open the connection.
Definition: cscore_c.h:193
@ CS_HTTP_MJPGSTREAMER
Definition: cscore_c.h:126
@ CS_HTTP_CSCORE
Definition: cscore_c.h:127
@ CS_HTTP_UNKNOWN
Definition: cscore_c.h:125
@ CS_HTTP_AXIS
Definition: cscore_c.h:128
@ CS_PROP_ENUM
Definition: cscore_c.h:107
@ CS_PROP_NONE
Definition: cscore_c.h:103
@ CS_PROP_INTEGER
Definition: cscore_c.h:105
@ CS_PROP_BOOLEAN
Definition: cscore_c.h:104
@ CS_PROP_STRING
Definition: cscore_c.h:106
@ CS_SINK_MJPEG
Definition: cscore_c.h:136
@ CS_SINK_CV
Definition: cscore_c.h:137
@ CS_SINK_UNKNOWN
Definition: cscore_c.h:135
@ CS_SOURCE_USB
Definition: cscore_c.h:115
@ CS_SOURCE_HTTP
Definition: cscore_c.h:116
@ CS_SOURCE_UNKNOWN
Definition: cscore_c.h:114
@ CS_SOURCE_CV
Definition: cscore_c.h:117
CS_Handle CS_Source
Definition: cscore_c.h:53
int CS_Status
Definition: cscore_c.h:46
CS_Handle CS_Property
Definition: cscore_c.h:49
CS_Handle CS_Sink
Definition: cscore_c.h:52
CS_Handle CS_Listener
Definition: cscore_c.h:50
CameraServer (cscore) namespace.
Definition: cscore_oo.inc:16
const T & first(const T &value, const Tail &...)
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:101
USB camera information.
Definition: cscore_cpp.h:44
Video mode.
Definition: cscore_cpp.h:62
PixelFormat
Definition: cscore_cpp.h:63