This commit is contained in:
cxh 2025-10-16 13:11:36 +08:00
parent 93c71e1193
commit 6d606c618e

View File

@ -1,13 +1,50 @@
// rtsp_manager.cpp
#include "rtmp_manager.hpp"
#include <arpa/inet.h>
#include <ifaddrs.h>
#include <net/if.h>
#include <netinet/in.h>
#include <chrono>
#include <future>
#include <cstring>
#include <iostream>
#include <string>
#include <thread>
#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<std::string, std::unique_ptr<RTMPManager::StreamContext>> 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::StreamResultInfo> RTMPManager::process_push_request(const VideoPushRequest &req)