// rtmp_manager.cpp #include "rtmp_manager.hpp" #include #include #include #include "logger.hpp" static inline std::string stream_type_suffix(StreamType type) { return (type == StreamType::MAIN) ? "_main" : "_sub"; } std::unordered_map> RTMPManager::streams; std::mutex RTMPManager::streams_mutex; RTMPManager::StreamCallback RTMPManager::status_callback = nullptr; std::string RTMPManager::make_stream_key(const std::string &cam_name, StreamType type) { return cam_name + stream_type_suffix(type); } void RTMPManager::init() { gst_init(nullptr, nullptr); LOG_INFO("[RTMP] GStreamer initialized."); } void RTMPManager::set_status_callback(StreamCallback cb) { status_callback = std::move(cb); } void RTMPManager::update_status(const std::string &key, const StreamStatus &status) { { std::lock_guard lock(streams_mutex); auto it = streams.find(key); if (it != streams.end()) it->second->status = status; } if (status_callback) status_callback(key, status); } GstElement *RTMPManager::create_pipeline(const Camera &cam, StreamType type) { int width = cam.width, height = cam.height, fps = cam.fps, bitrate = cam.bitrate; if (type == StreamType::SUB) { width = std::max(160, width / 2); height = std::max(120, height / 2); fps = std::max(10, fps / 2); bitrate = std::max(300000, bitrate / 2); } std::string stream_name = cam.name + stream_type_suffix(type); std::string pipeline_str = "v4l2src device=" + cam.device + " ! video/x-raw,format=NV12,width=" + std::to_string(width) + ",height=" + std::to_string(height) + ",framerate=" + std::to_string(fps) + "/1 ! queue max-size-buffers=1 leaky=downstream " "! mpph264enc bps=" + std::to_string(bitrate) + " gop=" + std::to_string(fps) + " ! h264parse ! flvmux streamable=true name=mux " "! rtmpsink location=\"rtmp://127.0.0.1/live/" + stream_name + " live=1\" sync=false"; LOG_INFO("[RTMP] Creating pipeline for '" + stream_name + "': " + pipeline_str); GError *error = nullptr; GstElement *pipeline = gst_parse_launch(pipeline_str.c_str(), &error); if (error) { LOG_ERROR("[RTMP] Pipeline parse error: " + std::string(error->message)); g_error_free(error); return nullptr; } return pipeline; } int get_camera_index(const std::string &name) { for (size_t i = 0; i < g_app_config.cameras.size(); ++i) if (g_app_config.cameras[i].name == name) return static_cast(i); return -1; } // Start camera, return result info (不再 publish) RTMPManager::StreamResultInfo RTMPManager::start_camera(const Camera &cam, StreamType type) { StreamResultInfo res; res.loc = get_camera_index(cam.name); res.url = get_stream_url(cam.name, type); std::string key = make_stream_key(cam.name, type); auto ctx = std::make_unique(); StreamContext *ctx_ptr = ctx.get(); { std::lock_guard lock(streams_mutex); auto it = streams.find(key); if (it != streams.end() && it->second->running.load()) { res.result = 0; res.reason = "Already streaming"; return res; } ctx->running.store(true); streams[key] = std::move(ctx); } std::future fut = ctx_ptr->start_result.get_future(); ctx_ptr->thread = std::thread([cam, type, ctx_ptr]() { RTMPManager::stream_loop(cam, type, ctx_ptr); }); if (fut.wait_for(std::chrono::seconds(5)) == std::future_status::ready) { res = fut.get(); } else { res.result = 1; res.reason = "Start timeout"; } return res; } // Stop camera, return result info (不再 publish) RTMPManager::StreamResultInfo RTMPManager::stop_camera(const std::string &cam_name, StreamType type) { StreamResultInfo res; res.loc = get_camera_index(cam_name); res.url = get_stream_url(cam_name, type); std::unique_ptr ctx; std::string key = make_stream_key(cam_name, type); { std::lock_guard lock(streams_mutex); auto it = streams.find(key); if (it == streams.end()) { res.result = 0; res.reason = "Not streaming"; return res; } it->second->running.store(false); ctx = std::move(it->second); streams.erase(it); } if (ctx->thread.joinable()) ctx->thread.join(); res.result = 0; res.reason = "Stopped manually"; return res; } void RTMPManager::stream_loop(Camera cam, StreamType type, StreamContext *ctx) { constexpr int FIRST_FRAME_TIMEOUT_SEC = 5; StreamResultInfo res; res.loc = get_camera_index(cam.name); res.url = get_stream_url(cam.name, type); std::string key = make_stream_key(cam.name, type); LOG_INFO("[RTMP] Stream loop started for " + key); GstElement *pipeline = create_pipeline(cam, type); if (!pipeline) { res.result = 1; res.reason = "Failed to create pipeline"; ctx->start_result.set_value(res); return; } GstBus *bus = gst_element_get_bus(pipeline); gst_element_set_state(pipeline, GST_STATE_PLAYING); bool first_frame_received = false; auto start_time = std::chrono::steady_clock::now(); while (true) { { std::lock_guard lock(streams_mutex); auto it = streams.find(key); if (it == streams.end() || !it->second->running.load()) break; } GstMessage *msg = gst_bus_timed_pop_filtered(bus, 100 * GST_MSECOND, static_cast(GST_MESSAGE_ERROR | GST_MESSAGE_EOS | GST_MESSAGE_STATE_CHANGED | GST_MESSAGE_ELEMENT)); // 超时未收到第一帧 if (!first_frame_received) { auto elapsed = std::chrono::steady_clock::now() - start_time; if (std::chrono::duration_cast(elapsed).count() > FIRST_FRAME_TIMEOUT_SEC) { res.result = 1; res.reason = "No frames received within timeout"; ctx->start_result.set_value(res); break; } } if (!msg) continue; switch (GST_MESSAGE_TYPE(msg)) { case GST_MESSAGE_ERROR: { GError *err = nullptr; gchar *debug = nullptr; gst_message_parse_error(msg, &err, &debug); std::string err_msg = err ? err->message : "Unknown GStreamer error"; res.result = 1; res.reason = "Pipeline error: " + err_msg; ctx->start_result.set_value(res); if (err) g_error_free(err); if (debug) g_free(debug); break; } case GST_MESSAGE_EOS: res.result = 1; res.reason = "EOS received"; ctx->start_result.set_value(res); break; case GST_MESSAGE_STATE_CHANGED: // pipeline 播放状态变化,不算成功 break; case GST_MESSAGE_ELEMENT: if (!first_frame_received) { first_frame_received = true; res.result = 0; res.reason = "First frame received, started OK"; ctx->start_result.set_value(res); } break; default: break; } gst_message_unref(msg); } gst_element_set_state(pipeline, GST_STATE_NULL); if (bus) gst_object_unref(bus); gst_object_unref(pipeline); // 清理 streams { std::lock_guard lock(streams_mutex); streams.erase(key); } LOG_INFO("[RTMP] Stream loop ended for " + key); } bool RTMPManager::is_streaming(const std::string &cam_name, StreamType type) { std::lock_guard lock(streams_mutex); auto it = streams.find(make_stream_key(cam_name, type)); return it != streams.end() && it->second->running.load(); } bool RTMPManager::is_any_streaming() { std::lock_guard lock(streams_mutex); for (auto &kv : streams) if (kv.second->running.load()) return true; return false; } void RTMPManager::stop_all() { std::vector> ctxs; { std::lock_guard lock(streams_mutex); for (auto &kv : streams) kv.second->running.store(false); for (auto &kv : streams) ctxs.push_back(std::move(kv.second)); streams.clear(); } for (auto &ctx : ctxs) if (ctx->thread.joinable()) ctx->thread.join(); LOG_INFO("[RTMP] stop_all completed."); }