WPILibC++ 2027.0.0-alpha-4
Loading...
Searching...
No Matches
RawSink.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 <functional>
8#include <string_view>
9
10#include "wpi/cs/ImageSink.hpp"
11#include "wpi/cs/cscore_raw.hpp"
12#include "wpi/util/RawFrame.hpp"
13
14namespace wpi::cs {
15
16/**
17 * A sink for user code to accept video frames as raw bytes.
18 *
19 * This is a complex API, most cases should use CvSource.
20 */
21class RawSink : public ImageSink {
22 public:
23 RawSink() = default;
24
25 /**
26 * Create a sink for accepting raw images.
27 *
28 * <p>GrabFrame() must be called on the created sink to get each new
29 * image.
30 *
31 * @param name Source name (arbitrary unique identifier)
32 */
33 explicit RawSink(std::string_view name) {
35 }
36
37 /**
38 * Create a sink for accepting raws images in a separate thread.
39 *
40 * <p>A thread will be created that calls WaitForFrame() and calls the
41 * processFrame() callback each time a new frame arrives.
42 *
43 * @param name Source name (arbitrary unique identifier)
44 * @param processFrame Frame processing function; will be called with a
45 * time=0 if an error occurred. processFrame should call GetImage()
46 * or GetError() as needed, but should not call (except in very
47 * unusual circumstances) WaitForImage().
48 */
49 RawSink(std::string_view name,
50 std::function<void(uint64_t time)> processFrame) {
51 m_handle = CreateRawSinkCallback(name, false, processFrame, &m_status);
52 }
53
54 protected:
55 /**
56 * Wait for the next frame and get the image.
57 * Times out (returning 0) after timeout seconds.
58 * The provided image will have three 8-bit channels stored in BGR order.
59 *
60 * @return Frame time, or 0 on error (call GetError() to obtain the error
61 * message); the frame time is in the same time base as
62 * wpi::util::Now(), and is in 1 us increments.
63 */
64 [[nodiscard]]
65 uint64_t GrabFrame(wpi::util::RawFrame& image, double timeout = 0.225) const {
66 m_status = 0;
67 return GrabSinkFrameTimeout(m_handle, image, timeout, &m_status);
68 }
69
70 /**
71 * Wait for the next frame and get the image. May block forever.
72 * The provided image will have three 8-bit channels stored in BGR order.
73 *
74 * @return Frame time, or 0 on error (call GetError() to obtain the error
75 * message); the frame time is in the same time base as
76 * wpi::util::Now(), and is in 1 us increments.
77 */
78 [[nodiscard]]
79 uint64_t GrabFrameNoTimeout(wpi::util::RawFrame& image) const {
80 m_status = 0;
81 return GrabSinkFrame(m_handle, image, &m_status);
82 }
83
84 /**
85 * Wait for the next frame and get the image. May block forever.
86 * The provided image will have three 8-bit channels stored in BGR order.
87 *
88 * <p>If lastFrameTime is provided and non-zero, the sink will fill image with
89 * the first frame from the source that is not equal to lastFrameTime. If
90 * lastFrameTime is zero, the time of the current frame owned by the CvSource
91 * is used, and this function will block until the connected CvSource provides
92 * a new frame.
93 *
94 * @return Frame time, or 0 on error (call GetError() to obtain the error
95 * message); the frame time is in the same time base as
96 * wpi::util::Now(), and is in 1 us increments.
97 */
98 [[nodiscard]]
99 uint64_t GrabFrameLastTime(wpi::util::RawFrame& image, uint64_t lastFrameTime,
100 double timeout = 0.225) const {
101 m_status = 0;
102 return GrabSinkFrameTimeoutLastTime(m_handle, image, timeout, lastFrameTime,
103 &m_status);
104 }
105};
106
107} // namespace wpi::cs
@ name
Definition base.h:690
RawSink()=default
uint64_t GrabFrame(wpi::util::RawFrame &image, double timeout=0.225) const
Wait for the next frame and get the image.
Definition RawSink.hpp:65
uint64_t GrabFrameNoTimeout(wpi::util::RawFrame &image) const
Wait for the next frame and get the image.
Definition RawSink.hpp:79
RawSink(std::string_view name, std::function< void(uint64_t time)> processFrame)
Create a sink for accepting raws images in a separate thread.
Definition RawSink.hpp:49
RawSink(std::string_view name)
Create a sink for accepting raw images.
Definition RawSink.hpp:33
uint64_t GrabFrameLastTime(wpi::util::RawFrame &image, uint64_t lastFrameTime, double timeout=0.225) const
Wait for the next frame and get the image.
Definition RawSink.hpp:99
CS_Sink m_handle
Definition VideoSink.hpp:229
CS_Status m_status
Definition VideoSink.hpp:228
uint64_t GrabSinkFrameTimeout(CS_Sink sink, WPI_RawFrame &image, double timeout, CS_Status *status)
CS_Sink CreateRawSink(std::string_view name, bool isCv, CS_Status *status)
uint64_t GrabSinkFrame(CS_Sink sink, WPI_RawFrame &image, CS_Status *status)
CS_Sink CreateRawSinkCallback(std::string_view name, bool isCv, std::function< void(uint64_t time)> processFrame, CS_Status *status)
uint64_t GrabSinkFrameTimeoutLastTime(CS_Sink sink, WPI_RawFrame &image, double timeout, uint64_t lastFrameTime, CS_Status *status)
CameraServer (cscore) namespace.
Definition CvSource.hpp:15
Definition RawFrame.hpp:20