YODAU 1.0
YEAR OF THE DEPEND ADULT UNDERGARMENT
Loading...
Searching...
No Matches
stream.hpp
Go to the documentation of this file.
1#ifndef YODAU_BACKEND_STREAM_HPP
2#define YODAU_BACKEND_STREAM_HPP
3
4#include "geometry.hpp"
5
6#include <mutex>
7#include <ostream>
8#include <string>
9#include <unordered_map>
10#include <vector>
11
12namespace yodau::backend {
13
14/**
15 * @brief Source/transport type of a video stream.
16 *
17 * This is a lightweight classification inferred from the stream path/URL
18 * or specified explicitly by the user.
19 *
20 * @see stream::identify
21 * @see stream::type_name
22 */
24 /** Local capture device (e.g., /dev/video*). */
26 /** File-based stream (path to a video file). */
28 /** RTSP network stream. */
30 /** HTTP/HTTPS network stream. */
32};
33
34/**
35 * @brief Processing pipeline mode for a stream.
36 *
37 * A stream can be inactive (@ref none), or active in either a user-managed
38 * (@ref manual) or auto-managed (@ref automatic) pipeline.
39 */
40enum class stream_pipeline {
41 /** Stream is active with manual/user-controlled processing. */
43 /** Stream is active with automatic backend-controlled processing. */
45 /** Stream is not active in any pipeline. */
47};
48
49/**
50 * @brief Represents a single video stream and its analytic connections.
51 *
52 * A stream owns metadata about where it comes from (path, type),
53 * whether it should loop when exhausted, and which pipeline (if any)
54 * it is currently active in.
55 *
56 * The stream also maintains a set of connected geometric lines
57 * (tripwires / ROIs), identified by their logical names.
58 *
59 * Thread-safety:
60 * - All access to connected lines is synchronized via @ref lines_mtx.
61 * - Non-line metadata (name/path/type/loop/active) is not internally
62 * synchronized.
63 */
64class stream {
65public:
66 /**
67 * @brief Construct a stream description.
68 *
69 * The actual @ref stream_type is determined as:
70 * 1. Detect type from @p path using @ref identify().
71 * 2. If @p type_str is empty or matches detected type name,
72 * use the detected type.
73 * 3. Otherwise try to parse @p type_str as an explicit override.
74 * Unknown strings fall back to detection.
75 *
76 * @param path Stream path or URL.
77 * @param name Logical stream name/identifier.
78 * @param type_str Optional textual override ("local", "file", "rtsp",
79 * "http").
80 * @param loop Whether file-based streams should loop on end-of-file.
81 */
82 stream(
83 std::string path, std::string name, const std::string& type_str = {},
84 bool loop = true
85 );
86
87 /// Non-copyable (streams manage shared connections / mutex).
88 stream(const stream&) = delete;
89 /// Non-copyable (streams manage shared connections / mutex).
90 stream& operator=(const stream&) = delete;
91
92 /**
93 * @brief Move-construct a stream.
94 *
95 * Moves metadata and connected lines. The moved-from object remains valid
96 * but in an unspecified state.
97 */
98 stream(stream&& other) noexcept;
99
100 /**
101 * @brief Move-assign a stream.
102 *
103 * Safely swaps line connections under locks of both objects.
104 *
105 * @return *this.
106 */
107 stream& operator=(stream&& other) noexcept;
108
109 /**
110 * @brief Identify stream type from a path/URL.
111 *
112 * Detection rules (as implemented):
113 * - "/dev/video*" -> @ref local
114 * - "rtsp://" -> @ref rtsp
115 * - "http(s)://" -> @ref http
116 * - otherwise -> @ref file
117 *
118 * @param path Stream path or URL.
119 * @return Detected @ref stream_type.
120 */
121 static stream_type identify(const std::string& path);
122
123 /**
124 * @brief Convert a stream type to a canonical textual name.
125 *
126 * @param type Stream type.
127 * @return One of: "local", "file", "rtsp", "http", or "unknown".
128 */
129 static std::string type_name(const stream_type type);
130
131 /**
132 * @brief Convert a pipeline mode to its textual name.
133 *
134 * @param pipeline Pipeline kind.
135 * @return One of: "manual", "automatic", "none", or "unknown".
136 */
137 static std::string pipeline_name(const stream_pipeline pipeline);
138
139 /**
140 * @brief Get logical stream name.
141 */
142 std::string get_name() const;
143
144 /**
145 * @brief Get stream path or URL.
146 */
147 std::string get_path() const;
148
149 /**
150 * @brief Get the stream transport/source type.
151 */
152 stream_type get_type() const;
153
154 /**
155 * @brief Whether the stream is configured to loop on exhaustion.
156 *
157 * Typically relevant for file streams.
158 */
159 bool is_looping() const;
160
161 /**
162 * @brief Dump stream metadata to an output stream.
163 *
164 * If @p connections is true, also prints names of any connected lines.
165 *
166 * @param out Output stream.
167 * @param connections Whether to include connected line names.
168 */
169 void dump(std::ostream& out, bool connections = false) const;
170
171 /**
172 * @brief Activate the stream in a pipeline.
173 *
174 * This sets the current active pipeline mode.
175 *
176 * @param pipeline Pipeline to activate in (default: automatic).
177 */
179
180 /**
181 * @brief Get current pipeline activity of the stream.
182 *
183 * @return Current @ref stream_pipeline.
184 */
186
187 /**
188 * @brief Deactivate the stream.
189 *
190 * Sets pipeline mode to @ref stream_pipeline::none.
191 */
192 void deactivate();
193
194 /**
195 * @brief Connect a geometric line to this stream.
196 *
197 * The line is stored by its @ref line::name. If @p line is null,
198 * the call is ignored.
199 *
200 * @param line Shared pointer to an immutable line.
201 */
202 void connect_line(line_ptr line);
203
204 /**
205 * @brief Get a list of names of all connected lines.
206 *
207 * @return Vector of line names.
208 */
209 std::vector<std::string> line_names() const;
210
211 /**
212 * @brief Get a snapshot of all connected lines.
213 *
214 * Returns a stable copy of shared pointers at the time of call.
215 *
216 * @return Vector of connected @ref line_ptr.
217 */
219
220private:
221 /** @brief Logical stream name. */
222 std::string name;
223
224 /** @brief Path or URL to the stream source. */
225 std::string path;
226
227 /** @brief Detected or user-specified stream type. */
229
230 /** @brief Looping behavior for file streams. */
231 bool loop { true };
232
233 /** @brief Currently active pipeline mode. */
235
236 /**
237 * @brief Connected lines keyed by their logical names.
238 *
239 * Protected by @ref lines_mtx.
240 */
242
243 /** @brief Mutex guarding @ref lines. */
244 mutable std::mutex lines_mtx;
245};
246
247} // namespace yodau::backend
248
249#endif // YODAU_BACKEND_STREAM_HPP
Simple interactive CLI (REPL) for controlling a stream_manager.
void cmd_start_stream(const std::vector< std::string > &args) const
Handler for start-stream.
void dispatch_command(const std::string &cmd, const std::vector< std::string > &args) const
Dispatch a command to its handler.
void cmd_set_line(const std::vector< std::string > &args) const
Handler for set-line.
int run() const
Run the interactive command loop.
void cmd_add_line(const std::vector< std::string > &args) const
Handler for add-line.
static cxxopts::ParseResult parse_with_cxxopts(const std::string &cmd, const std::vector< std::string > &args, cxxopts::Options &options)
Parse command arguments using cxxopts.
backend::stream_manager & stream_mgr
Stream manager controlled by this CLI.
void cmd_stop_stream(const std::vector< std::string > &args) const
Handler for stop-stream.
void cmd_list_streams(const std::vector< std::string > &args) const
Handler for list-streams.
void cmd_add_stream(const std::vector< std::string > &args) const
Handler for add-stream.
void cmd_list_lines(const std::vector< std::string > &args) const
Handler for list-lines.
cli_client(backend::stream_manager &mgr)
Construct a CLI client operating on an existing manager.
Definition cli_client.cpp:5
static std::vector< std::string > tokenize(const std::string &line)
Split a line into whitespace-separated tokens.
Central coordinator for streams, geometry, frame processing and events.
std::function< void(const std::string &stream_name, frame &&f)> manual_push_fn
Hook for manual frame pushing.
Represents a single video stream and its analytic connections.
Definition stream.hpp:64
std::string get_name() const
Get logical stream name.
Definition stream.cpp:96
stream & operator=(stream &&other) noexcept
Move-assign a stream.
Definition stream.cpp:42
std::string name
Logical stream name.
Definition stream.hpp:222
std::vector< line_ptr > lines_snapshot() const
Get a snapshot of all connected lines.
Definition stream.cpp:154
void activate(stream_pipeline pipeline=stream_pipeline::automatic)
Activate the stream in a pipeline.
Definition stream.cpp:129
stream(std::string path, std::string name, const std::string &type_str={}, bool loop=true)
Construct a stream description.
Definition stream.cpp:5
stream & operator=(const stream &)=delete
Non-copyable (streams manage shared connections / mutex).
stream(stream &&other) noexcept
Move-construct a stream.
Definition stream.cpp:30
bool loop
Looping behavior for file streams.
Definition stream.hpp:231
void dump(std::ostream &out, bool connections=false) const
Dump stream metadata to an output stream.
Definition stream.cpp:106
std::vector< std::string > line_names() const
Get a list of names of all connected lines.
Definition stream.cpp:147
bool is_looping() const
Whether the stream is configured to loop on exhaustion.
Definition stream.cpp:104
void connect_line(line_ptr line)
Connect a geometric line to this stream.
Definition stream.cpp:139
stream_pipeline pipeline() const
Get current pipeline activity of the stream.
Definition stream.cpp:133
stream(const stream &)=delete
Non-copyable (streams manage shared connections / mutex).
std::unordered_map< std::string, line_ptr > lines
Connected lines keyed by their logical names.
Definition stream.hpp:241
void deactivate()
Deactivate the stream.
Definition stream.cpp:137
std::string path
Path or URL to the stream source.
Definition stream.hpp:225
static std::string type_name(const stream_type type)
Convert a stream type to a canonical textual name.
Definition stream.cpp:73
static stream_type identify(const std::string &path)
Identify stream type from a path/URL.
Definition stream.cpp:60
std::string get_path() const
Get stream path or URL.
Definition stream.cpp:98
stream_type get_type() const
Get the stream transport/source type.
Definition stream.cpp:100
stream_type type
Detected or user-specified stream type.
Definition stream.hpp:228
std::mutex lines_mtx
Mutex guarding lines.
Definition stream.hpp:244
static std::string pipeline_name(const stream_pipeline pipeline)
Convert a pipeline mode to its textual name.
Definition stream.cpp:85
stream_pipeline active
Currently active pipeline mode.
Definition stream.hpp:234
static yodau::backend::tripwire_dir parse_tripwire_dir(const std::string &s)
line_ptr make_line(std::vector< point > points, std::string name, bool closed=false)
Create and normalize a line.
Definition geometry.cpp:82
std::string normalize_str(std::string_view str)
Remove whitespace and parentheses from a string.
Definition geometry.cpp:133
std::vector< point > parse_points(const std::string &points_str)
Parse points from a textual representation.
Definition geometry.cpp:94
float parse_float(std::string_view num_str)
Parse a float from a string view.
Definition geometry.cpp:145
stream_pipeline
Processing pipeline mode for a stream.
Definition stream.hpp:40
std::shared_ptr< line const > line_ptr
Shared, immutable line pointer.
Definition geometry.hpp:146
event_kind
High-level classification of backend events.
Definition event.hpp:19
tripwire_dir
Allowed crossing direction for a tripwire.
Definition geometry.hpp:62
pixel_format
Pixel format of a frame buffer.
Definition frame.hpp:16
stream_type
Source/transport type of a video stream.
Definition stream.hpp:23
Generic event produced by the backend.
Definition event.hpp:44
std::chrono::steady_clock::time_point ts
Monotonic timestamp when the event was generated.
Definition event.hpp:70
event_kind kind
Type of the event.
Definition event.hpp:50
std::string stream_name
Name/identifier of the stream that produced the event.
Definition event.hpp:57
std::optional< point > pos_pct
Optional position associated with the event in percentage coordinates.
Definition event.hpp:79
std::string line_name
Name of the line / ROI / rule responsible for this event.
Definition event.hpp:86
std::string message
Human-readable event description or payload.
Definition event.hpp:65
Video frame container.
Definition frame.hpp:44
int width
Frame width in pixels.
Definition frame.hpp:48
int stride
Number of bytes per row.
Definition frame.hpp:60
int height
Frame height in pixels.
Definition frame.hpp:53
std::vector< std::uint8_t > data
Raw pixel bytes.
Definition frame.hpp:74
std::chrono::steady_clock::time_point ts
Monotonic timestamp when the frame was captured/produced.
Definition frame.hpp:79
pixel_format format
Pixel format of the buffer.
Definition frame.hpp:67
Polyline / polygon described in percentage coordinates.
Definition geometry.hpp:81
std::string name
Logical name of the line (e.g., "entrance_tripwire").
Definition geometry.hpp:85
tripwire_dir dir
Optional tripwire direction constraint.
Definition geometry.hpp:104
bool closed
Whether the chain is closed.
Definition geometry.hpp:97
void dump(std::ostream &out) const
Print a human-readable representation of the line.
Definition geometry.cpp:17
std::vector< point > points
Vertex list in percentage coordinates.
Definition geometry.hpp:90
void normalize()
Canonicalize point order.
Definition geometry.cpp:29
bool operator==(const line &other) const
Equality check using canonical point comparison.
Definition geometry.cpp:70
Point in percentage-based image coordinates.
Definition geometry.hpp:19
float distance_to(const point &other) const
Compute Euclidean distance to another point.
Definition geometry.cpp:7
float x
Horizontal coordinate (percentage of width).
Definition geometry.hpp:23
float y
Vertical coordinate (percentage of height).
Definition geometry.hpp:28
static constexpr float epsilon
Tolerance used for fuzzy point comparisons.
Definition geometry.hpp:33
bool compare(const point &other) const
Compare two points with tolerance epsilon.
Definition geometry.cpp:13