| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "stream.hpp" | ||
| 2 | |||
| 3 | #include <ranges> | ||
| 4 | |||
| 5 | ✗ | yodau::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) | |
| 12 | ✗ | , active(stream_pipeline::none) { | |
| 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") { | |
| 18 | ✗ | this->type = stream_type::local; | |
| 19 | ✗ | } else if (type_str == "file") { | |
| 20 | ✗ | this->type = stream_type::file; | |
| 21 | ✗ | } else if (type_str == "rtsp") { | |
| 22 | ✗ | this->type = stream_type::rtsp; | |
| 23 | ✗ | } else if (type_str == "http") { | |
| 24 | ✗ | this->type = stream_type::http; | |
| 25 | } else { | ||
| 26 | ✗ | this->type = detected; | |
| 27 | } | ||
| 28 | ✗ | } | |
| 29 | |||
| 30 | ✗ | yodau::backend::stream::stream(stream&& other) noexcept | |
| 31 | ✗ | : name(std::move(other.name)) | |
| 32 | ✗ | , path(std::move(other.path)) | |
| 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 | |||
| 41 | yodau::backend::stream& | ||
| 42 | ✗ | yodau::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 | |||
| 59 | yodau::backend::stream_type | ||
| 60 | ✗ | yodau::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 | |||
| 73 | ✗ | std::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 | |||
| 84 | std::string | ||
| 85 | ✗ | yodau::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 | |||
| 96 | ✗ | std::string yodau::backend::stream::get_name() const { return name; } | |
| 97 | |||
| 98 | ✗ | std::string yodau::backend::stream::get_path() const { return path; } | |
| 99 | |||
| 100 | ✗ | yodau::backend::stream_type yodau::backend::stream::get_type() const { | |
| 101 | ✗ | return type; | |
| 102 | } | ||
| 103 | |||
| 104 | ✗ | bool yodau::backend::stream::is_looping() const { return loop; } | |
| 105 | |||
| 106 | ✗ | void 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 | |||
| 129 | ✗ | void yodau::backend::stream::activate(const stream_pipeline pipeline) { | |
| 130 | ✗ | active = pipeline; | |
| 131 | ✗ | } | |
| 132 | |||
| 133 | ✗ | yodau::backend::stream_pipeline yodau::backend::stream::pipeline() const { | |
| 134 | ✗ | return active; | |
| 135 | } | ||
| 136 | |||
| 137 | ✗ | void yodau::backend::stream::deactivate() { active = stream_pipeline::none; } | |
| 138 | |||
| 139 | ✗ | void 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 | |||
| 147 | ✗ | std::vector<std::string> yodau::backend::stream::line_names() const { | |
| 148 | ✗ | std::scoped_lock lock(lines_mtx); | |
| 149 | ✗ | return lines | std::views::keys | |
| 150 | ✗ | | std::ranges::to<std::vector<std::string>>(); | |
| 151 | ✗ | } | |
| 152 | |||
| 153 | std::vector<yodau::backend::line_ptr> | ||
| 154 | ✗ | yodau::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 | ✗ | } | |
| 163 |