bonus-edge-proxy/src/videoService/video_service_manager.cc

121 lines
3.7 KiB
C++
Raw Normal View History

2025-11-06 18:11:11 +08:00
// video_service_manager.cc
2025-10-27 11:06:06 +08:00
#include "video_service_manager.h"
#include "spdlog/spdlog.h"
2025-11-06 18:11:11 +08:00
#include <fstream>
2025-10-27 11:06:06 +08:00
2025-11-04 18:38:05 +08:00
VideoServiceManager::~VideoServiceManager() { stop_all(); }
bool VideoServiceManager::load_config(const std::string &config_path) {
std::ifstream ifs(config_path);
if (!ifs.is_open()) {
spdlog::error("Failed to open video config file: {}", config_path);
return false;
}
try {
nlohmann::json video_json;
ifs >> video_json;
m_enabled = video_json.value("/video_service/enabled"_json_pointer, false);
if (video_json.contains("video_streams") &&
video_json["video_streams"].is_array()) {
m_stream_configs_json = video_json["video_streams"];
} else {
spdlog::warn("Video config contains no 'video_streams' array.");
2025-11-06 18:11:11 +08:00
m_stream_configs_json = nlohmann::json::array();
2025-11-04 18:38:05 +08:00
}
spdlog::info("Successfully loaded video config. Service enabled: {}. "
"Streams found: {}",
m_enabled, m_stream_configs_json.size());
return true;
} catch (const nlohmann::json::exception &e) {
spdlog::error("Failed to parse video config file '{}'. Error: {}",
config_path, e.what());
return false;
}
2025-10-27 11:06:06 +08:00
}
2025-11-04 18:38:05 +08:00
void VideoServiceManager::load_and_start() {
if (!m_enabled) {
spdlog::warn("VideoService is disabled in video configuration. No streams "
"will be started.");
return;
}
spdlog::info("Found {} video stream configurations.",
m_stream_configs_json.size());
for (const auto &sc_json : m_stream_configs_json) {
std::string id = sc_json.value("id", "unknown");
bool enabled = sc_json.value("enabled", false);
std::string input_url = sc_json.value("input_url", "");
std::string output_rtsp = sc_json.value("output_rtsp", "");
std::string module_type = sc_json.value("module_type", "");
nlohmann::json module_config =
sc_json.value("module_config", nlohmann::json::object());
if (!enabled) {
spdlog::info(
"Video stream '{}' (input: {}) is disabled in config, skipping.", id,
input_url);
continue;
2025-10-27 11:06:06 +08:00
}
2025-11-04 18:38:05 +08:00
std::unique_ptr<IAnalysisModule> module = nullptr;
2025-10-27 11:06:06 +08:00
2025-11-04 18:38:05 +08:00
try {
if (module_type == "intrusion_detection") {
2025-10-27 11:06:06 +08:00
2025-11-06 18:11:11 +08:00
module = std::make_unique<IntrusionModule>();
2025-11-04 18:38:05 +08:00
} else if (module_type == "face_recognition") {
spdlog::warn("Module type 'face_recognition' for stream '{}' is not "
"yet implemented.",
id);
continue;
} else {
spdlog::error("Unknown module_type '{}' for stream '{}'. Skipping.",
module_type, id);
continue;
}
auto service = std::make_unique<VideoService>(
2025-11-06 18:11:11 +08:00
std::move(module), input_url, output_rtsp, module_config);
2025-11-04 18:38:05 +08:00
if (service->start()) {
spdlog::info("Successfully started video service for stream '{}' "
"[Module: {}]. Output is [{}].",
id, module_type, output_rtsp);
services_.push_back(std::move(service));
} else {
spdlog::error(
"Failed to start video service for stream '{}' [Input: {}].", id,
input_url);
}
} catch (const std::exception &e) {
spdlog::error(
"Exception while creating VideoService for stream '{}' [{}]: {}", id,
input_url, e.what());
2025-10-27 11:06:06 +08:00
}
2025-11-04 18:38:05 +08:00
}
spdlog::info(
"VideoServiceManager finished setup. {} streams are now running.",
services_.size());
2025-10-27 11:06:06 +08:00
}
2025-11-04 18:38:05 +08:00
void VideoServiceManager::stop_all() {
spdlog::info("Stopping all video services ({})...", services_.size());
for (auto &service : services_) {
if (service) {
service->stop();
2025-10-27 11:06:06 +08:00
}
2025-11-04 18:38:05 +08:00
}
services_.clear();
spdlog::info("All video services stopped and cleared.");
2025-10-27 11:06:06 +08:00
}