WPILibC++ 2027.0.0-alpha-4
Loading...
Searching...
No Matches
VideoSource.hpp
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#pragma once
6
7#include <stdint.h>
8
9#include <string>
10#include <utility>
11#include <vector>
12
14#include "wpi/cs/cscore_cpp.hpp"
16
17namespace wpi::cs {
18
19class VideoEvent;
20class VideoSink;
21
22/**
23 * A source for video that provides a sequence of frames.
24 */
26 friend class VideoEvent;
27 friend class VideoSink;
28
29 public:
30 /**
31 * Video source kind.
32 */
33 enum Kind {
34 /// Unknown video source.
36 /// USB video source.
38 /// HTTP video source.
40 /// CV video source.
42 /// Raw video source.
44 };
45
46 /** Connection strategy. Used for SetConnectionStrategy(). */
48 /**
49 * Automatically connect or disconnect based on whether any sinks are
50 * connected to this source. This is the default behavior.
51 */
53
54 /**
55 * Try to keep the connection open regardless of whether any sinks are
56 * connected.
57 */
59
60 /**
61 * Never open the connection. If this is set when the connection is open,
62 * close the connection.
63 */
65 };
66
67 VideoSource() noexcept = default;
68
73
74 VideoSource(VideoSource&& other) noexcept : VideoSource() {
75 swap(*this, other);
76 }
77
79 swap(*this, other);
80 return *this;
81 }
82
84 m_status = 0;
85 if (m_handle != 0) {
87 }
88 }
89
90 explicit operator bool() const { return m_handle != 0; }
91
92 int GetHandle() const { return m_handle; }
93
94 bool operator==(const VideoSource& other) const {
95 return m_handle == other.m_handle;
96 }
97
98 /**
99 * Get the kind of the source.
100 */
101 Kind GetKind() const {
102 m_status = 0;
103 return static_cast<VideoSource::Kind>(GetSourceKind(m_handle, &m_status));
104 }
105
106 /**
107 * Get the name of the source. The name is an arbitrary identifier
108 * provided when the source is created, and should be unique.
109 */
110 std::string GetName() const {
111 m_status = 0;
113 }
114
115 /**
116 * Get the source description. This is source-kind specific.
117 */
118 std::string GetDescription() const {
119 m_status = 0;
121 }
122
123 /**
124 * Get the last time a frame was captured.
125 * This uses the same time base as wpi::util::Now().
126 *
127 * @return Time in 1 us increments.
128 */
129 uint64_t GetLastFrameTime() const {
130 m_status = 0;
132 }
133
134 /**
135 * Sets the connection strategy. By default, the source will automatically
136 * connect or disconnect based on whether any sinks are connected.
137 *
138 * <p>This function is non-blocking; look for either a connection open or
139 * close event or call IsConnected() to determine the connection state.
140 *
141 * @param strategy connection strategy (auto, keep open, or force close)
142 */
144 m_status = 0;
146 m_handle,
147 static_cast<CS_ConnectionStrategy>(static_cast<int>(strategy)),
148 &m_status);
149 }
150
151 /**
152 * Is the source currently connected to whatever is providing the images?
153 */
154 bool IsConnected() const {
155 m_status = 0;
157 }
158
159 /**
160 * Gets source enable status. This is determined with a combination of
161 * connection strategy and the number of sinks connected.
162 *
163 * @return True if enabled, false otherwise.
164 */
165 bool IsEnabled() const {
166 m_status = 0;
168 }
169
170 /** Get a property.
171 *
172 * @param name Property name
173 * @return Property contents (of kind Property::kNone if no property with
174 * the given name exists)
175 */
176 VideoProperty GetProperty(std::string_view name) {
177 m_status = 0;
179 }
180
181 /**
182 * Enumerate all properties of this source.
183 */
184 std::vector<VideoProperty> EnumerateProperties() const;
185
186 /**
187 * Get the current video mode.
188 */
190 m_status = 0;
192 }
193
194 /**
195 * Set the video mode.
196 *
197 * @param mode Video mode
198 */
199 bool SetVideoMode(const VideoMode& mode) {
200 m_status = 0;
201 return SetSourceVideoMode(m_handle, mode, &m_status);
202 }
203
204 /**
205 * Set the video mode.
206 *
207 * @param pixelFormat desired pixel format
208 * @param width desired width
209 * @param height desired height
210 * @param fps desired FPS
211 * @return True if set successfully
212 */
213 bool SetVideoMode(wpi::util::PixelFormat pixelFormat, int width, int height,
214 int fps) {
215 m_status = 0;
216 return SetSourceVideoMode(
217 m_handle, VideoMode{pixelFormat, width, height, fps}, &m_status);
218 }
219
220 /**
221 * Set the pixel format.
222 *
223 * @param pixelFormat desired pixel format
224 * @return True if set successfully
225 */
227 m_status = 0;
228 return SetSourcePixelFormat(m_handle, pixelFormat, &m_status);
229 }
230
231 /**
232 * Set the resolution.
233 *
234 * @param width desired width
235 * @param height desired height
236 * @return True if set successfully
237 */
238 bool SetResolution(int width, int height) {
239 m_status = 0;
240 return SetSourceResolution(m_handle, width, height, &m_status);
241 }
242
243 /**
244 * Set the frames per second (FPS).
245 *
246 * @param fps desired FPS
247 * @return True if set successfully
248 */
249 bool SetFPS(int fps) {
250 m_status = 0;
251 return SetSourceFPS(m_handle, fps, &m_status);
252 }
253
254 /**
255 * Set video mode and properties from a JSON configuration string.
256 *
257 * The format of the JSON input is:
258 *
259 * <pre>
260 * {
261 * "pixel format": "MJPEG", "YUYV", etc
262 * "width": video mode width
263 * "height": video mode height
264 * "fps": video mode fps
265 * "brightness": percentage brightness
266 * "white balance": "auto", "hold", or value
267 * "exposure": "auto", "hold", or value
268 * "properties": [
269 * {
270 * "name": property name
271 * "value": property value
272 * }
273 * ]
274 * }
275 * </pre>
276 *
277 * @param config configuration
278 * @return True if set successfully
279 */
280 bool SetConfigJson(std::string_view config) {
281 m_status = 0;
282 return SetSourceConfigJson(m_handle, config, &m_status);
283 }
284
285 /**
286 * Set video mode and properties from a JSON configuration object.
287 *
288 * @param config configuration
289 * @return True if set successfully
290 */
291 bool SetConfigJson(const wpi::util::json& config) {
292 m_status = 0;
293 return SetSourceConfigJson(m_handle, config, &m_status);
294 }
295
296 /**
297 * Get a JSON configuration string.
298 *
299 * @return JSON configuration string
300 */
301 std::string GetConfigJson() const {
302 m_status = 0;
304 }
305
306 /**
307 * Get a JSON configuration object.
308 *
309 * @return JSON configuration object
310 */
311 wpi::util::json GetConfigJsonObject() const;
312
313 /**
314 * Get the actual FPS.
315 *
316 * <p>SetTelemetryPeriod() must be called for this to be valid.
317 *
318 * @return Actual FPS averaged over the telemetry period.
319 */
325
326 /**
327 * Get the data rate (in bytes per second).
328 *
329 * <p>SetTelemetryPeriod() must be called for this to be valid.
330 *
331 * @return Data rate averaged over the telemetry period.
332 */
338
339 /**
340 * Enumerate all known video modes for this source.
341 */
342 std::vector<VideoMode> EnumerateVideoModes() const {
343 CS_Status status = 0;
344 return EnumerateSourceVideoModes(m_handle, &status);
345 }
346
347 CS_Status GetLastStatus() const { return m_status; }
348
349 /**
350 * Enumerate all sinks connected to this source.
351 *
352 * @return Vector of sinks.
353 */
354 std::vector<VideoSink> EnumerateSinks();
355
356 /**
357 * Enumerate all existing sources.
358 *
359 * @return Vector of sources.
360 */
361 static std::vector<VideoSource> EnumerateSources();
362
363 friend void swap(VideoSource& first, VideoSource& second) noexcept {
364 using std::swap;
365 swap(first.m_status, second.m_status);
366 swap(first.m_handle, second.m_handle);
367 }
368
369 protected:
370 explicit VideoSource(CS_Source handle) : m_handle(handle) {}
371
372 mutable CS_Status m_status = 0;
373
374 /// Video source handle.
376};
377
378} // namespace wpi::cs
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 glfw and nanopb were modified for use in 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:123
@ name
Definition base.h:690
An event generated by the library and provided to event listeners.
Definition VideoEvent.hpp:17
A source or sink property.
Definition VideoProperty.hpp:34
A sink for video that accepts a sequence of frames.
Definition VideoSink.hpp:24
bool IsConnected() const
Is the source currently connected to whatever is providing the images?
Definition VideoSource.hpp:154
bool SetPixelFormat(wpi::util::PixelFormat pixelFormat)
Set the pixel format.
Definition VideoSource.hpp:226
wpi::util::json GetConfigJsonObject() const
Get a JSON configuration object.
Kind GetKind() const
Get the kind of the source.
Definition VideoSource.hpp:101
uint64_t GetLastFrameTime() const
Get the last time a frame was captured.
Definition VideoSource.hpp:129
VideoSource(CS_Source handle)
Definition VideoSource.hpp:370
int GetHandle() const
Definition VideoSource.hpp:92
double GetActualDataRate() const
Get the data rate (in bytes per second).
Definition VideoSource.hpp:333
void SetConnectionStrategy(ConnectionStrategy strategy)
Sets the connection strategy.
Definition VideoSource.hpp:143
~VideoSource()
Definition VideoSource.hpp:83
CS_Status m_status
Definition VideoSource.hpp:372
std::vector< VideoProperty > EnumerateProperties() const
Enumerate all properties of this source.
bool SetConfigJson(std::string_view config)
Set video mode and properties from a JSON configuration string.
Definition VideoSource.hpp:280
CS_Status GetLastStatus() const
Definition VideoSource.hpp:347
friend class VideoSink
Definition VideoSource.hpp:27
friend void swap(VideoSource &first, VideoSource &second) noexcept
Definition VideoSource.hpp:363
VideoSource & operator=(VideoSource other) noexcept
Definition VideoSource.hpp:78
bool SetVideoMode(wpi::util::PixelFormat pixelFormat, int width, int height, int fps)
Set the video mode.
Definition VideoSource.hpp:213
bool IsEnabled() const
Gets source enable status.
Definition VideoSource.hpp:165
VideoMode GetVideoMode() const
Get the current video mode.
Definition VideoSource.hpp:189
VideoSource() noexcept=default
double GetActualFPS() const
Get the actual FPS.
Definition VideoSource.hpp:320
Kind
Video source kind.
Definition VideoSource.hpp:33
@ kUsb
USB video source.
Definition VideoSource.hpp:37
@ kUnknown
Unknown video source.
Definition VideoSource.hpp:35
@ kHttp
HTTP video source.
Definition VideoSource.hpp:39
@ kRaw
Raw video source.
Definition VideoSource.hpp:43
@ kCv
CV video source.
Definition VideoSource.hpp:41
static std::vector< VideoSource > EnumerateSources()
Enumerate all existing sources.
bool SetFPS(int fps)
Set the frames per second (FPS).
Definition VideoSource.hpp:249
std::string GetConfigJson() const
Get a JSON configuration string.
Definition VideoSource.hpp:301
std::string GetName() const
Get the name of the source.
Definition VideoSource.hpp:110
ConnectionStrategy
Connection strategy.
Definition VideoSource.hpp:47
@ kConnectionForceClose
Never open the connection.
Definition VideoSource.hpp:64
@ kConnectionKeepOpen
Try to keep the connection open regardless of whether any sinks are connected.
Definition VideoSource.hpp:58
@ kConnectionAutoManage
Automatically connect or disconnect based on whether any sinks are connected to this source.
Definition VideoSource.hpp:52
bool SetVideoMode(const VideoMode &mode)
Set the video mode.
Definition VideoSource.hpp:199
VideoSource(VideoSource &&other) noexcept
Definition VideoSource.hpp:74
bool SetResolution(int width, int height)
Set the resolution.
Definition VideoSource.hpp:238
friend class VideoEvent
Definition VideoSource.hpp:26
bool SetConfigJson(const wpi::util::json &config)
Set video mode and properties from a JSON configuration object.
Definition VideoSource.hpp:291
CS_Source m_handle
Video source handle.
Definition VideoSource.hpp:375
std::vector< VideoSink > EnumerateSinks()
Enumerate all sinks connected to this source.
std::vector< VideoMode > EnumerateVideoModes() const
Enumerate all known video modes for this source.
Definition VideoSource.hpp:342
VideoProperty GetProperty(std::string_view name)
Get a property.
Definition VideoSource.hpp:176
std::string GetDescription() const
Get the source description.
Definition VideoSource.hpp:118
bool operator==(const VideoSource &other) const
Definition VideoSource.hpp:94
CS_ConnectionStrategy
Connection strategy.
Definition cscore_c.h:170
@ CS_CONNECTION_AUTO_MANAGE
Automatically connect or disconnect based on whether any sinks are connected to this source.
Definition cscore_c.h:175
@ CS_CONNECTION_KEEP_OPEN
Try to keep the connection open regardless of whether any sinks are connected.
Definition cscore_c.h:181
@ CS_CONNECTION_FORCE_CLOSE
Never open the connection.
Definition cscore_c.h:187
@ CS_SOURCE_FRAMES_RECEIVED
Definition cscore_c.h:166
@ CS_SOURCE_BYTES_RECEIVED
Definition cscore_c.h:165
@ CS_SOURCE_USB
Definition cscore_c.h:110
@ CS_SOURCE_HTTP
Definition cscore_c.h:111
@ CS_SOURCE_UNKNOWN
Definition cscore_c.h:109
@ CS_SOURCE_RAW
Definition cscore_c.h:113
@ CS_SOURCE_CV
Definition cscore_c.h:112
VideoMode GetSourceVideoMode(CS_Source source, CS_Status *status)
bool SetSourceResolution(CS_Source source, int width, int height, CS_Status *status)
std::string GetSourceConfigJson(CS_Source source, CS_Status *status)
void SetSourceConnectionStrategy(CS_Source source, CS_ConnectionStrategy strategy, CS_Status *status)
bool SetSourceFPS(CS_Source source, int fps, CS_Status *status)
void ReleaseSource(CS_Source source, CS_Status *status)
std::string GetSourceDescription(CS_Source source, CS_Status *status)
std::vector< VideoMode > EnumerateSourceVideoModes(CS_Source source, CS_Status *status)
uint64_t GetSourceLastFrameTime(CS_Source source, CS_Status *status)
bool IsSourceEnabled(CS_Source source, CS_Status *status)
CS_Source CopySource(CS_Source source, CS_Status *status)
bool SetSourceConfigJson(CS_Source source, std::string_view config, CS_Status *status)
CS_Property GetSourceProperty(CS_Source source, std::string_view name, CS_Status *status)
CS_SourceKind GetSourceKind(CS_Source source, CS_Status *status)
std::string GetSourceName(CS_Source source, CS_Status *status)
bool SetSourcePixelFormat(CS_Source source, wpi::util::PixelFormat pixelFormat, CS_Status *status)
bool SetSourceVideoMode(CS_Source source, const VideoMode &mode, CS_Status *status)
bool IsSourceConnected(CS_Source source, CS_Status *status)
double GetTelemetryAverageValue(CS_Handle handle, CS_TelemetryKind kind, CS_Status *status)
CS_Handle CS_Source
Definition cscore_c.h:48
int CS_Status
Definition cscore_c.h:41
void swap(wpi::util::StringMap< T > &lhs, wpi::util::StringMap< T > &rhs)
Definition StringMap.hpp:775
CameraServer (cscore) namespace.
Definition CvSource.hpp:15
PixelFormat
Pixel formats.
Definition PixelFormat.hpp:14
Video mode.
Definition VideoMode.hpp:15