// video_service_manager.cc #include "video_service_manager.h" #include "spdlog/spdlog.h" #include 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."); m_stream_configs_json = nlohmann::json::array(); } 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; } } 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; } std::unique_ptr module = nullptr; try { if (module_type == "intrusion_detection") { module = std::make_unique(); } 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( std::move(module), input_url, output_rtsp, module_config); 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()); } } spdlog::info( "VideoServiceManager finished setup. {} streams are now running.", services_.size()); } void VideoServiceManager::stop_all() { spdlog::info("Stopping all video services ({})...", services_.size()); for (auto &service : services_) { if (service) { service->stop(); } } services_.clear(); spdlog::info("All video services stopped and cleared."); }