// rtsp_manager.cpp #include "rtmp_manager.hpp" #include #include #include #include #include #include #include #include #include #include #include #include #include "logger.hpp" static bool device_exists(const std::string &path) { struct stat st; return (stat(path.c_str(), &st) == 0); } static std::string get_ip_address(const std::string &ifname) { struct ifaddrs *ifaddr, *ifa; char ip[INET_ADDRSTRLEN] = {0}; if (getifaddrs(&ifaddr) == -1) { perror("getifaddrs"); return ""; } for (ifa = ifaddr; ifa != nullptr; ifa = ifa->ifa_next) { if (ifa->ifa_addr == nullptr) continue; // 只看 IPv4 if (ifa->ifa_addr->sa_family == AF_INET && ifname == ifa->ifa_name) { void *addr = &((struct sockaddr_in *)ifa->ifa_addr)->sin_addr; if (inet_ntop(AF_INET, addr, ip, sizeof(ip))) { freeifaddrs(ifaddr); return ip; } } } freeifaddrs(ifaddr); return ""; // 没找到 } 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); // 说明: // 1) 维持你当前的视频链路(NV12 -> mpph264enc -> h264parse -> flvmux)。 // 2) 加一个静音音轨:audiotestsrc wave=silence -> voaacenc -> aacparse -> mux. // 3) 给 rtmpsink 命名 name=rtmp_sink,供后续在 bus loop 中识别并判断“真正连上”时机。 std::string pipeline_str = "v4l2src device=" + cam.device + " ! video/x-raw,width=" + std::to_string(width) + ",height=" + std::to_string(height) + ",framerate=" + std::to_string(fps) + "/1 " "! videoconvert ! video/x-raw,format=NV12 " "! mpph264enc bps=" + std::to_string(bitrate) + " gop=" + std::to_string(fps) + " " "! h264parse " "! flvmux name=mux streamable=true " "audiotestsrc wave=silence ! audioconvert ! audioresample ! voaacenc ! aacparse ! mux. " "mux. ! rtmpsink name=rtmp_sink 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; } 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); if (res.loc < 0 || res.loc >= static_cast(g_app_config.cameras.size())) { res.result = 1; res.reason = "Invalid channel index"; return res; } // 新增:设备节点存在性检查 if (!device_exists(cam.device)) { res.result = 1; res.reason = "Device not found: " + cam.device; return res; } const std::string key = make_stream_key(cam.name, type); std::unique_ptr ctx; std::future status_future; { std::lock_guard lock(streams_mutex); auto it = streams.find(key); // 若旧线程仍在运行,先安全停止 if (it != streams.end()) { if (it->second->running.load()) { LOG_WARN("[RTMP] Restarting existing stream: " + key); it->second->running.store(false); if (it->second->thread.joinable()) it->second->thread.join(); } streams.erase(it); } ctx = std::make_unique(); ctx->running.store(true); status_future = ctx->start_promise.get_future(); StreamContext *ctx_ptr = ctx.get(); ctx->thread = std::thread( [cam, type, ctx_ptr]() { try { RTMPManager::stream_loop(cam, type, ctx_ptr); } catch (const std::exception &e) { LOG_ERROR("[RTMP] Exception in stream thread: " + std::string(e.what())); } catch (...) { LOG_ERROR("[RTMP] Unknown exception in stream thread"); } }); streams.emplace(key, std::move(ctx)); } // 等待首帧或错误,最多 7 秒 if (status_future.wait_for(std::chrono::seconds(7)) == std::future_status::ready) { const StreamStatus s = status_future.get(); res.result = s.running ? 0 : 1; res.reason = s.running ? "Started OK" : (s.last_error.empty() ? "Failed to start" : s.last_error); } else { res.result = 1; res.reason = "Timeout waiting for stream"; } return res; } 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); if (res.loc < 0 || res.loc >= static_cast(g_app_config.cameras.size())) { res.result = 1; res.reason = "Invalid channel index"; return res; } 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 = "Already stopped (no active stream)"; return res; } ctx = std::move(it->second); streams.erase(it); } bool was_running = ctx->running.load(); // ✅ 改为使用 running ctx->running.store(false); if (ctx->thread.joinable()) ctx->thread.join(); if (was_running) { res.result = 0; res.reason = "Stopped manually"; } else { res.result = 1; res.reason = "Not running (stream never started)"; } return res; } void RTMPManager::stream_loop(Camera cam, StreamType type, StreamContext *ctx) { const std::string key = make_stream_key(cam.name, type); auto try_set_start = [&](const StreamStatus &st) { bool expected = false; if (ctx && ctx->start_promise_set.compare_exchange_strong(expected, true)) { try { ctx->start_promise.set_value(st); } catch (...) { } } }; StreamStatus status; status.running = false; status.last_result = StreamResult::UNKNOWN; status.last_error.clear(); GstElement *pipeline = create_pipeline(cam, type); if (!pipeline) { status.last_result = StreamResult::PIPELINE_ERROR; status.last_error = "Failed to create pipeline"; try_set_start(status); return; } GstBus *bus = gst_element_get_bus(pipeline); gst_element_set_state(pipeline, GST_STATE_PLAYING); bool started_reported = false; // 是否已经上报“启动成功” auto t0 = std::chrono::steady_clock::now(); const int first_frame_timeout_s = 5; // 首帧/连通性保护上限(与原逻辑一致) const int connect_guard_ms = 2000; // 连接保护窗口:给 rtmpsink 一点时间去连接 while (true) { if (!ctx->running.load()) { status.running = false; status.last_result = StreamResult::UNKNOWN; status.last_error = "Stream stopped manually"; try_set_start(status); break; } // 超时保护:既没报错也没确认连接成功,超过上限则判失败 if (!started_reported) { auto elapsed_s = std::chrono::duration_cast(std::chrono::steady_clock::now() - t0).count(); if (elapsed_s > first_frame_timeout_s) { status.running = false; status.last_result = StreamResult::TIMEOUT; status.last_error = "No frames/connection established within timeout"; try_set_start(status); break; } } GstMessage *msg = gst_bus_timed_pop_filtered(bus, 200 * GST_MSECOND, (GstMessageType)(GST_MESSAGE_ERROR | GST_MESSAGE_EOS | GST_MESSAGE_STATE_CHANGED | GST_MESSAGE_STREAM_STATUS)); if (!msg) { // 保底:过了 connect_guard_ms 仍然没有 ERROR, // 并且 rtmpsink 进入 PLAYING,就认为真的起来了。 if (!started_reported) { auto elapsed_ms = std::chrono::duration_cast(std::chrono::steady_clock::now() - t0) .count(); if (elapsed_ms >= connect_guard_ms) { GstElement *sink = gst_bin_get_by_name(GST_BIN(pipeline), "rtmp_sink"); if (sink) { GstState cur, pending; if (gst_element_get_state(sink, &cur, &pending, 0) == GST_STATE_CHANGE_SUCCESS && cur == GST_STATE_PLAYING) { status.running = true; ctx->status.running = true; status.last_result = StreamResult::OK; status.last_error.clear(); try_set_start(status); started_reported = true; LOG_INFO("[RTMP] Guard-start OK for '" + cam.name + "'"); } gst_object_unref(sink); } } } continue; } switch (GST_MESSAGE_TYPE(msg)) { case GST_MESSAGE_STREAM_STATUS: { // 优先判断 rtmpsink 的 STREAM_STATUS(CREATE/START)来认定“真正连上” GstStreamStatusType type_ss; GstElement *owner = nullptr; gst_message_parse_stream_status(msg, &type_ss, &owner); if (!started_reported && owner) { const gchar *oname = GST_OBJECT_NAME(owner); if (oname && std::strcmp(oname, "rtmp_sink") == 0) { if (type_ss == GST_STREAM_STATUS_TYPE_CREATE || type_ss == GST_STREAM_STATUS_TYPE_ENTER || type_ss == GST_STREAM_STATUS_TYPE_START) { status.running = true; ctx->status.running = true; status.last_result = StreamResult::OK; status.last_error.clear(); try_set_start(status); started_reported = true; LOG_INFO(std::string("[RTMP] Connected (STREAM_STATUS) for '") + cam.name + "'"); } } } break; } case GST_MESSAGE_STATE_CHANGED: { // 不再用 pipeline 进入 PLAYING 判成功(容易误判) // 这里仅用于日志或后续扩展 break; } case GST_MESSAGE_ERROR: { GError *err = nullptr; gchar *dbg = nullptr; gst_message_parse_error(msg, &err, &dbg); status.running = false; status.last_result = StreamResult::CONNECTION_FAIL; status.last_error = err ? err->message : "GStreamer error"; LOG_ERROR("[RTMP] Stream error from '" + cam.name + "': " + status.last_error); // 立刻标记为停止,避免线程继续循环误判 ctx->status.running = false; ctx->running.store(false); gst_element_set_state(pipeline, GST_STATE_NULL); if (err) g_error_free(err); if (dbg) g_free(dbg); try_set_start(status); break; } case GST_MESSAGE_EOS: { status.running = false; status.last_result = StreamResult::EOS_RECEIVED; status.last_error = "EOS received"; try_set_start(status); break; } default: break; } gst_message_unref(msg); // 发生错误或 EOS 就退出循环 if (status.last_result == StreamResult::CONNECTION_FAIL || status.last_result == StreamResult::EOS_RECEIVED) { break; } } gst_element_set_state(pipeline, GST_STATE_NULL); if (bus) gst_object_unref(bus); gst_object_unref(pipeline); ctx->status.running = 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."); } 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; } std::string RTMPManager::get_stream_url(const std::string &cam_name, StreamType type) { // 获取当前设备 enp1s0 网卡 IP(或者改成你希望的接口) std::string ip = get_ip_address("enP2p33s0"); if (ip.empty()) ip = "127.0.0.1"; // 构建流名,例如 AHD2_main std::string stream_name = make_stream_key(cam_name, type); // 最终返回 WebRTC 拉流地址(不加 .flv) return "http://" + ip + ":1985/rtc/v1/whep/?app=live&stream=" + stream_name; } std::vector RTMPManager::process_push_request(const VideoPushRequest &req) { std::vector results; std::vector> futures; for (const auto &item : req.data) { StreamType type = (item.streamType == 0) ? StreamType::MAIN : StreamType::SUB; for (int ch : item.channels) { if (ch < 0 || ch >= static_cast(g_app_config.cameras.size())) continue; const auto &cam = g_app_config.cameras[ch]; if (item.switchVal == 0) { // 异步启动推流 futures.emplace_back( std::async(std::launch::async, [&, cam, type]() { return start_camera(cam, type); })); } else { // 异步停止推流 futures.emplace_back( std::async(std::launch::async, [&, cam, type]() { return stop_camera(cam.name, type); })); } } } // 收集所有结果 for (auto &f : futures) { try { results.push_back(f.get()); } catch (const std::exception &e) { StreamResultInfo err; err.result = 1; err.reason = std::string("Exception: ") + e.what(); results.push_back(err); } } return results; }