YODAU 1.0
YEAR OF THE DEPEND ADULT UNDERGARMENT
Loading...
Searching...
No Matches
stream.cpp
Go to the documentation of this file.
1#include "stream.hpp"
2
3#include <ranges>
4
5yodau::backend::stream::stream(
6 std::string path, std::string name, const std::string& type_str,
7 const bool loop
8)
9 : name(std::move(name))
10 , path(std::move(path))
11 , loop(loop)
13 const auto detected = identify(this->path);
14
15 if (type_str.empty() || type_str == type_name(detected)) {
16 this->type = detected;
17 } else if (type_str == "local") {
19 } else if (type_str == "file") {
21 } else if (type_str == "rtsp") {
23 } else if (type_str == "http") {
25 } else {
26 this->type = detected;
27 }
28}
29
30yodau::backend::stream::stream(stream&& other) noexcept
33 , type(other.type)
34 , loop(other.loop)
35 , active(other.active) {
36
37 std::scoped_lock lock(other.lines_mtx);
38 lines = std::move(other.lines);
39}
40
41yodau::backend::stream&
42yodau::backend::stream::operator=(stream&& other) noexcept {
43 if (this == &other) {
44 return *this;
45 }
46
47 std::scoped_lock lock(lines_mtx, other.lines_mtx);
48
49 name = std::move(other.name);
50 path = std::move(other.path);
51 type = other.type;
52 loop = other.loop;
53 active = other.active;
54 lines = std::move(other.lines);
55
56 return *this;
57}
58
59yodau::backend::stream_type
60yodau::backend::stream::identify(const std::string& path) {
61 if (path.rfind("/dev/video", 0) == 0) {
62 return stream_type::local;
63 }
64 if (path.rfind("rtsp://", 0) == 0) {
65 return stream_type::rtsp;
66 }
67 if (path.rfind("http://", 0) == 0 || path.rfind("https://", 0) == 0) {
68 return stream_type::http;
69 }
70 return stream_type::file;
71}
72
73std::string yodau::backend::stream::type_name(const stream_type type) {
74 static constexpr std::array<std::string_view, 4> type_names {
75 "local", "file", "rtsp", "http"
76 };
77 const auto idx = static_cast<size_t>(type);
78 if (idx >= type_names.size()) {
79 return "unknown";
80 }
81 return std::string(type_names[idx]);
82}
83
84std::string
85yodau::backend::stream::pipeline_name(const stream_pipeline pipeline) {
86 static constexpr std::array<std::string_view, 3> pipeline_names {
87 "manual", "automatic", "none"
88 };
89 const auto idx = static_cast<size_t>(pipeline);
90 if (idx >= pipeline_names.size()) {
91 return "unknown";
92 }
93 return std::string(pipeline_names[idx]);
94}
95
96std::string yodau::backend::stream::get_name() const { return name; }
97
98std::string yodau::backend::stream::get_path() const { return path; }
99
100yodau::backend::stream_type yodau::backend::stream::get_type() const {
101 return type;
102}
103
104bool yodau::backend::stream::is_looping() const { return loop; }
105
106void yodau::backend::stream::dump(
107 std::ostream& out, const bool connections
108) const {
109 out << "Stream(name=" << name << ", path=" << path
110 << ", type=" << type_name(type)
111 << ", loop=" << (loop ? "true" : "false")
112 << ", active_pipeline=" << pipeline_name(active) << ")";
113
114 if (!connections) {
115 return;
116 }
117
118 const auto names = line_names();
119 if (names.empty()) {
120 return;
121 }
122
123 out << "\n\tConnected lines:";
124 for (const auto& ln : names) {
125 out << ' ' << ln;
126 }
127}
128
129void yodau::backend::stream::activate(const stream_pipeline pipeline) {
130 active = pipeline;
131}
132
134 return active;
135}
136
138
139void yodau::backend::stream::connect_line(line_ptr line) {
140 if (!line) {
141 return;
142 }
143 std::scoped_lock lock(lines_mtx);
144 lines.emplace(line->name, line);
145}
146
148 std::scoped_lock lock(lines_mtx);
149 return lines | std::views::keys
150 | std::ranges::to<std::vector<std::string>>();
151}
152
154yodau::backend::stream::lines_snapshot() const {
155 std::scoped_lock lock(lines_mtx);
156 std::vector<line_ptr> out;
157 out.reserve(lines.size());
158 for (const auto& lp : lines | std::views::values) {
159 out.push_back(lp);
160 }
161 return out;
162}
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::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(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
void deactivate()
Deactivate the stream.
Definition stream.cpp:137
static std::string type_name(const stream_type type)
Convert a stream type to a canonical textual name.
Definition stream.cpp:73
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
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
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
stream_type
Source/transport type of a video stream.
Definition stream.hpp:23