From 6d606c618e47ac80a42107ac1b70462c18fdfb1a Mon Sep 17 00:00:00 2001 From: cxh Date: Thu, 16 Oct 2025 13:11:36 +0800 Subject: [PATCH] 1 --- src/rtmp_manager.cpp | 49 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 47 insertions(+), 2 deletions(-) diff --git a/src/rtmp_manager.cpp b/src/rtmp_manager.cpp index 3e636cf..eb064ec 100644 --- a/src/rtmp_manager.cpp +++ b/src/rtmp_manager.cpp @@ -1,13 +1,50 @@ // rtsp_manager.cpp #include "rtmp_manager.hpp" +#include +#include +#include +#include + #include -#include +#include #include +#include #include #include "logger.hpp" +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; @@ -314,7 +351,15 @@ bool RTMPManager::is_any_streaming() std::string RTMPManager::get_stream_url(const std::string &cam_name, StreamType type) { - return "rtmp://127.0.0.1/live/" + make_stream_key(cam_name, 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)