|
YODAU 1.0
YEAR OF THE DEPEND ADULT UNDERGARMENT
|
Central coordinator for streams, geometry, frame processing and events. More...
#include <backend/include/stream_manager.hpp>
Public Types | |
| using | local_stream_detector_fn = std::function<std::vector<stream>()> |
| Custom detector for local streams. | |
| using | manual_push_fn = std::function<void(const std::string& stream_name, frame&& f)> |
| Hook for manual frame pushing. | |
| using | daemon_start_fn |
| Hook used to start a background daemon that produces frames. | |
| using | frame_processor_fn = std::function<std::vector<event>(const stream& s, const frame& f)> |
| Frame analysis function. | |
| using | event_sink_fn = std::function<void(const event& e)> |
| Sink for individual events. | |
| using | event_batch_sink_fn = std::function<void(const std::vector<event>& events)> |
| Sink for event batches. | |
Public Member Functions | |
| stream_manager () | |
| Construct manager and attempt to discover local streams. | |
| void | dump (std::ostream &out) const |
| Dump all streams and lines to an output stream. | |
| void | dump_lines (std::ostream &out) const |
| Dump all registered lines. | |
| void | dump_stream (std::ostream &out, bool connections=false) const |
| Dump all registered streams. | |
| void | set_local_stream_detector (local_stream_detector_fn detector) |
| Set a custom local stream detector. | |
| void | refresh_local_streams () |
| Refresh local streams. | |
| stream & | add_stream (const std::string &path, const std::string &name={}, const std::string &type={}, bool loop=true) |
| Add a new stream to the manager. | |
| line_ptr | add_line (const std::string &points, bool closed=false, const std::string &name={}) |
| Add a new line (polyline/polygon) to the manager. | |
| stream & | set_line (const std::string &stream_name, const std::string &line_name) |
| Connect an existing line to an existing stream. | |
| std::shared_ptr< const stream > | find_stream (const std::string &name) const |
| Find a stream by name. | |
| std::vector< std::string > | stream_names () const |
| List names of all registered streams. | |
| std::vector< std::string > | line_names () const |
| List names of all registered lines. | |
| std::vector< std::string > | stream_lines (const std::string &stream_name) const |
| List names of lines connected to a given stream. | |
| void | set_manual_push_hook (manual_push_fn hook) |
| Set manual push hook. | |
| void | set_daemon_start_hook (daemon_start_fn hook) |
| Set daemon start hook. | |
| void | push_frame (const std::string &stream_name, frame &&f) |
| Push a frame into the manager for a specific stream. | |
| void | start_daemon (const std::string &stream_name) |
| Start a daemon for a stream. | |
| void | set_frame_processor (frame_processor_fn fn) |
| Set the frame processor. | |
| std::vector< event > | process_frame (const std::string &stream_name, frame &&f) |
| Analyze a frame and return generated events. | |
| void | set_event_sink (event_sink_fn fn) |
| Set per-event sink. | |
| void | set_event_batch_sink (event_batch_sink_fn fn) |
| Set batch event sink. | |
| void | set_analysis_interval_ms (int ms) |
| Set minimum analysis interval per stream, in milliseconds. | |
| void | start_stream (const std::string &name) |
| Start a stream daemon by name. | |
| void | stop_stream (const std::string &name) |
| Stop a running stream daemon by name. | |
| bool | is_stream_running (const std::string &name) const |
| Check whether a daemon for a stream is running. | |
| void | enable_fake_events (int interval_ms=700) |
| Enable periodic fake events generation. | |
| void | disable_fake_events () |
| Disable fake events generation. | |
| void | set_line_dir (const std::string &line_name, tripwire_dir dir) |
| Change the direction constraint of a stored line. | |
Private Member Functions | |
| std::vector< std::shared_ptr< stream > > | snapshot_streams () const |
| Take a snapshot of current streams. | |
| void | snapshot_hooks (frame_processor_fn &fp, event_sink_fn &es, event_batch_sink_fn &bes) const |
| Snapshot currently installed hooks. | |
| int | current_fake_interval_ms () const |
| Get current fake-event interval. | |
| void | run_fake_events (std::stop_token st) |
| Background loop for fake-event generation. | |
Private Attributes | |
| std::unordered_map< std::string, std::shared_ptr< stream > > | streams |
| Registered streams keyed by name. | |
| std::unordered_map< std::string, line_ptr > | lines |
| Registered lines keyed by name. | |
| size_t | stream_idx { 0 } |
| Auto-name index for streams ("stream_N"). | |
| size_t | line_idx { 0 } |
| Auto-name index for lines ("line_N"). | |
| local_stream_detector_fn | stream_detector {} |
| Optional external local stream detector. | |
| manual_push_fn | manual_push |
| Optional manual frame push hook. | |
| daemon_start_fn | daemon_start |
| Optional daemon start hook. | |
| frame_processor_fn | frame_processor |
| Optional frame analysis hook. | |
| event_sink_fn | event_sink |
| Optional per-event sink. | |
| event_batch_sink_fn | event_batch_sink |
| Optional batch event sink. | |
| int | analysis_interval_ms { 200 } |
| Per-stream analysis throttle interval. | |
| std::unordered_map< std::string, std::chrono::steady_clock::time_point > | last_analysis_ts |
| Last analysis time per stream. | |
| std::unordered_map< std::string, std::jthread > | daemons |
| Running daemon threads keyed by stream name. | |
| std::jthread | fake_thread |
| Fake-event generator thread. | |
| int | fake_interval_ms { 700 } |
| Current fake-event interval. | |
| bool | fake_enabled { false } |
| Whether fake-event generation is enabled. | |
| std::mutex | mtx |
| Global mutex protecting manager state. | |
Central coordinator for streams, geometry, frame processing and events.
The stream_manager owns:
Typical responsibilities:
Thread-safety:
Definition at line 45 of file stream_manager.hpp.
Hook used to start a background daemon that produces frames.
The manager provides an on-frame callback that the daemon should call for each produced frame. The daemon must also respect st and exit promptly when stop is requested.
| s | Stream to run. |
| on_frame | Callback to deliver produced frames to the manager. |
| st | Stop token to observe for cancellation. |
Definition at line 78 of file stream_manager.hpp.
| using yodau::backend::stream_manager::event_batch_sink_fn = std::function<void(const std::vector<event>& events)> |
Sink for event batches.
If set, push_frame delivers events to this sink as a batch.
Definition at line 107 of file stream_manager.hpp.
| using yodau::backend::stream_manager::event_sink_fn = std::function<void(const event& e)> |
Sink for individual events.
If batch sink is not set, events are delivered one-by-one to this sink.
Definition at line 100 of file stream_manager.hpp.
| using yodau::backend::stream_manager::frame_processor_fn = std::function<std::vector<event>(const stream& s, const frame& f)> |
Frame analysis function.
Called by process_frame and (optionally) by the fake-event generator.
| s | Stream metadata/context. |
| f | Frame to analyze (const reference). |
Definition at line 93 of file stream_manager.hpp.
| using yodau::backend::stream_manager::local_stream_detector_fn = std::function<std::vector<stream>()> |
Custom detector for local streams.
The detector is expected to return a list of streams discovered at call time. Returned streams are moved into the manager if their names are not already present.
Definition at line 54 of file stream_manager.hpp.
| using yodau::backend::stream_manager::manual_push_fn = std::function<void(const std::string& stream_name, frame&& f)> |
Hook for manual frame pushing.
If set via set_manual_push_hook, push_frame will delegate to this hook instead of analyzing frames internally.
| stream_name | Name of the stream the frame belongs to. |
| f | Frame to process/consume (moved). |
Definition at line 65 of file stream_manager.hpp.
| yodau::backend::stream_manager::stream_manager | ( | ) |
Construct manager and attempt to discover local streams.
On Linux, the constructor probes /dev/video* devices and adds those that look like capture devices. After that, if a custom local detector is set, it may be used when refresh_local_streams is called.
Definition at line 44 of file stream_manager.cpp.
| yodau::backend::line_ptr yodau::backend::stream_manager::add_line | ( | const std::string & | points, |
| bool | closed = false, | ||
| const std::string & | name = {} ) |
Add a new line (polyline/polygon) to the manager.
If name is empty or already used, a unique name "line_N" is generated. The points string is parsed with parse_points.
| points | Textual representation of points. |
| closed | Whether the line is closed. |
| name | Optional explicit line name. |
| std::runtime_error | on invalid point string. |
Definition at line 143 of file stream_manager.cpp.
| yodau::backend::stream & yodau::backend::stream_manager::add_stream | ( | const std::string & | path, |
| const std::string & | name = {}, | ||
| const std::string & | type = {}, | ||
| bool | loop = true ) |
Add a new stream to the manager.
If name is empty or already used, a unique name "stream_N" is generated.
| path | Stream path/URL. |
| name | Optional explicit stream name. |
| type | Optional explicit type override passed to stream ctor. |
| loop | Whether file streams should loop on EOF. |
Definition at line 128 of file stream_manager.cpp.
|
private |
Get current fake-event interval.
Definition at line 491 of file stream_manager.cpp.
| void yodau::backend::stream_manager::disable_fake_events | ( | ) |
Disable fake events generation.
Requests stop on the fake-event thread (if running).
Definition at line 430 of file stream_manager.cpp.
| void yodau::backend::stream_manager::dump | ( | std::ostream & | out | ) | const |
Dump all streams and lines to an output stream.
Equivalent to calling dump_stream and dump_lines.
| out | Output stream. |
Definition at line 46 of file stream_manager.cpp.
| void yodau::backend::stream_manager::dump_lines | ( | std::ostream & | out | ) | const |
Dump all registered lines.
Each line is printed with line::dump.
| out | Output stream. |
Definition at line 53 of file stream_manager.cpp.
| void yodau::backend::stream_manager::dump_stream | ( | std::ostream & | out, |
| bool | connections = false ) const |
Dump all registered streams.
If connections is true, prints connected line names per stream.
| out | Output stream. |
| connections | Whether to include stream-line connections. |
Definition at line 62 of file stream_manager.cpp.
| void yodau::backend::stream_manager::enable_fake_events | ( | int | interval_ms = 700 | ) |
Enable periodic fake events generation.
Starts/keeps a background thread that calls the frame processor with a dummy frame on all streams every interval_ms milliseconds.
| interval_ms | Period in ms (ignored if <= 0). |
Definition at line 403 of file stream_manager.cpp.
| std::shared_ptr< const yodau::backend::stream > yodau::backend::stream_manager::find_stream | ( | const std::string & | name | ) | const |
Find a stream by name.
| name | Stream name. |
Definition at line 174 of file stream_manager.cpp.
| bool yodau::backend::stream_manager::is_stream_running | ( | const std::string & | name | ) | const |
Check whether a daemon for a stream is running.
| name | Stream name. |
Definition at line 396 of file stream_manager.cpp.
| std::vector< std::string > yodau::backend::stream_manager::line_names | ( | ) | const |
List names of all registered lines.
Definition at line 189 of file stream_manager.cpp.
| std::vector< yodau::backend::event > yodau::backend::stream_manager::process_frame | ( | const std::string & | stream_name, |
| frame && | f ) |
Analyze a frame and return generated events.
Analysis is throttled per stream by analysis_interval_ms. If the stream does not exist or processor is not set, returns empty list.
| stream_name | Stream name. |
| f | Frame to analyze (moved into function; read-only for processor). |
Definition at line 267 of file stream_manager.cpp.
| void yodau::backend::stream_manager::push_frame | ( | const std::string & | stream_name, |
| frame && | f ) |
Push a frame into the manager for a specific stream.
Workflow:
| stream_name | Stream name. |
| f | Frame to process (moved). |
Definition at line 218 of file stream_manager.cpp.
| void yodau::backend::stream_manager::refresh_local_streams | ( | ) |
Refresh local streams.
Behavior:
Definition at line 83 of file stream_manager.cpp.
|
private |
Background loop for fake-event generation.
Periodically calls frame processor with a dummy frame on all streams and delivers events to configured sinks until st requests stop.
| st | Stop token. |
Definition at line 496 of file stream_manager.cpp.
| void yodau::backend::stream_manager::set_analysis_interval_ms | ( | int | ms | ) |
Set minimum analysis interval per stream, in milliseconds.
Values <= 0 are ignored.
| ms | Interval in ms. |
Definition at line 321 of file stream_manager.cpp.
| void yodau::backend::stream_manager::set_daemon_start_hook | ( | daemon_start_fn | hook | ) |
Set daemon start hook.
This hook is required for start_stream to do anything.
| hook | Hook functor (may be empty to unset). |
Definition at line 211 of file stream_manager.cpp.
| void yodau::backend::stream_manager::set_event_batch_sink | ( | event_batch_sink_fn | fn | ) |
Set batch event sink.
When set, overrides per-event sink for delivery.
| fn | Sink functor (may be empty to unset). |
Definition at line 314 of file stream_manager.cpp.
| void yodau::backend::stream_manager::set_event_sink | ( | event_sink_fn | fn | ) |
Set per-event sink.
Ignored when batch sink is set.
| fn | Sink functor (may be empty to unset). |
Definition at line 309 of file stream_manager.cpp.
| void yodau::backend::stream_manager::set_frame_processor | ( | frame_processor_fn | fn | ) |
Set the frame processor.
| fn | Analysis functor (may be empty to unset). |
Definition at line 259 of file stream_manager.cpp.
| yodau::backend::stream & yodau::backend::stream_manager::set_line | ( | const std::string & | stream_name, |
| const std::string & | line_name ) |
Connect an existing line to an existing stream.
| stream_name | Name of the target stream. |
| line_name | Name of the line to connect. |
| std::runtime_error | if stream or line is not found. |
Definition at line 157 of file stream_manager.cpp.
| void yodau::backend::stream_manager::set_line_dir | ( | const std::string & | line_name, |
| tripwire_dir | dir ) |
Change the direction constraint of a stored line.
Since lines are stored as immutable shared pointers, this method clones the line, changes line::dir, and replaces the pointer in the registry.
| line_name | Name of the line to reconfigure. |
| dir | New direction constraint. |
| std::runtime_error | if the line does not exist. |
Definition at line 450 of file stream_manager.cpp.
| void yodau::backend::stream_manager::set_local_stream_detector | ( | local_stream_detector_fn | detector | ) |
Set a custom local stream detector.
The detector is stored and refresh_local_streams is called immediately after setting.
| detector | Detector functor. |
Definition at line 73 of file stream_manager.cpp.
| void yodau::backend::stream_manager::set_manual_push_hook | ( | manual_push_fn | hook | ) |
Set manual push hook.
When set, push_frame will call this hook and return immediately, skipping internal analysis and sinks.
| hook | Hook functor (may be empty to unset). |
Definition at line 206 of file stream_manager.cpp.
|
private |
Snapshot currently installed hooks.
Copies hooks under lock so that background threads can use stable callables without racing with setters.
| fp | Out: frame processor. |
| es | Out: per-event sink. |
| bes | Out: batch sink. |
Definition at line 482 of file stream_manager.cpp.
|
private |
Take a snapshot of current streams.
Used to iterate without holding the manager lock for long periods.
Definition at line 468 of file stream_manager.cpp.
| void yodau::backend::stream_manager::start_daemon | ( | const std::string & | stream_name | ) |
Start a daemon for a stream.
Alias for start_stream.
| stream_name | Stream name. |
Definition at line 253 of file stream_manager.cpp.
| void yodau::backend::stream_manager::start_stream | ( | const std::string & | name | ) |
Start a stream daemon by name.
Requirements:
On Linux, local capture devices may be validated again before starting.
| name | Stream name. |
Definition at line 329 of file stream_manager.cpp.
References yodau::backend::automatic.
| void yodau::backend::stream_manager::stop_stream | ( | const std::string & | name | ) |
Stop a running stream daemon by name.
Requests stop on the associated std::jthread and deactivates the stream (sets pipeline to stream_pipeline::none).
| name | Stream name. |
Definition at line 368 of file stream_manager.cpp.
| std::vector< std::string > yodau::backend::stream_manager::stream_lines | ( | const std::string & | stream_name | ) | const |
List names of lines connected to a given stream.
| stream_name | Stream name. |
Definition at line 195 of file stream_manager.cpp.
| std::vector< std::string > yodau::backend::stream_manager::stream_names | ( | ) | const |
List names of all registered streams.
Definition at line 183 of file stream_manager.cpp.
|
private |
Per-stream analysis throttle interval.
Definition at line 473 of file stream_manager.hpp.
|
private |
Optional daemon start hook.
Definition at line 461 of file stream_manager.hpp.
|
private |
Running daemon threads keyed by stream name.
Definition at line 484 of file stream_manager.hpp.
|
private |
Optional batch event sink.
Definition at line 470 of file stream_manager.hpp.
|
private |
Optional per-event sink.
Definition at line 467 of file stream_manager.hpp.
|
private |
Whether fake-event generation is enabled.
Definition at line 493 of file stream_manager.hpp.
|
private |
Current fake-event interval.
Definition at line 490 of file stream_manager.hpp.
|
private |
Fake-event generator thread.
Definition at line 487 of file stream_manager.hpp.
|
private |
Optional frame analysis hook.
Definition at line 464 of file stream_manager.hpp.
|
private |
Last analysis time per stream.
Used to enforce analysis_interval_ms.
Definition at line 481 of file stream_manager.hpp.
|
private |
|
private |
Registered lines keyed by name.
Definition at line 446 of file stream_manager.hpp.
|
private |
Optional manual frame push hook.
Definition at line 458 of file stream_manager.hpp.
|
mutableprivate |
Global mutex protecting manager state.
Definition at line 496 of file stream_manager.hpp.
|
private |
Optional external local stream detector.
Definition at line 455 of file stream_manager.hpp.
|
private |
Auto-name index for streams ("stream_N").
Definition at line 449 of file stream_manager.hpp.
|
private |
Registered streams keyed by name.
Definition at line 443 of file stream_manager.hpp.