sweeper_video/src/rtmp_manager.cpp

428 lines
13 KiB
C++
Raw Normal View History

2025-10-15 15:36:48 +08:00
// rtmp_manager.cpp
#include "rtmp_manager.hpp"
2025-10-15 14:14:00 +08:00
#include <chrono>
2025-10-15 08:50:01 +08:00
#include <iostream>
2025-10-15 14:14:00 +08:00
#include <thread>
2025-10-15 10:18:02 +08:00
2025-10-15 14:14:00 +08:00
#include "logger.hpp"
static inline std::string stream_type_suffix(StreamType type) { return (type == StreamType::MAIN) ? "_main" : "_sub"; }
2025-10-15 08:57:40 +08:00
std::unordered_map<std::string, std::unique_ptr<RTMPManager::StreamContext>> RTMPManager::streams;
std::mutex RTMPManager::streams_mutex;
2025-10-15 08:50:01 +08:00
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.");
}
2025-10-15 14:14:00 +08:00
void RTMPManager::set_status_callback(StreamCallback cb) { status_callback = std::move(cb); }
2025-10-15 08:50:01 +08:00
void RTMPManager::update_status(const std::string &key, const StreamStatus &status)
{
2025-10-15 08:57:40 +08:00
{
std::lock_guard<std::mutex> lock(streams_mutex);
auto it = streams.find(key);
2025-10-15 14:14:00 +08:00
if (it != streams.end()) it->second->status = status;
2025-10-15 08:57:40 +08:00
}
2025-10-15 14:14:00 +08:00
if (status_callback) status_callback(key, status);
2025-10-15 08:50:01 +08:00
}
GstElement *RTMPManager::create_pipeline(const Camera &cam, StreamType type)
{
2025-10-15 10:18:02 +08:00
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);
2025-10-14 17:49:26 +08:00
bitrate = std::max(300000, bitrate / 2);
}
2025-10-14 17:49:26 +08:00
std::string stream_name = cam.name + stream_type_suffix(type);
2025-10-15 14:14:00 +08:00
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";
2025-10-15 08:50:01 +08:00
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)
{
2025-10-15 08:50:01 +08:00
LOG_ERROR("[RTMP] Pipeline parse error: " + std::string(error->message));
g_error_free(error);
return nullptr;
}
return pipeline;
}
2025-10-15 15:01:55 +08:00
int get_camera_index(const std::string &name)
2025-10-15 11:27:53 +08:00
{
2025-10-15 13:09:10 +08:00
for (size_t i = 0; i < g_app_config.cameras.size(); ++i)
2025-10-15 15:01:55 +08:00
if (g_app_config.cameras[i].name == name) return static_cast<int>(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);
std::string key = make_stream_key(cam.name, type);
2025-10-15 15:33:00 +08:00
2025-10-15 16:21:25 +08:00
try
2025-10-15 13:09:10 +08:00
{
2025-10-15 16:21:25 +08:00
auto ctx = std::make_unique<StreamContext>();
StreamContext *ctx_ptr = ctx.get();
2025-10-15 13:09:10 +08:00
{
2025-10-15 16:21:25 +08:00
std::lock_guard<std::mutex> 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;
}
2025-10-15 15:01:55 +08:00
2025-10-15 16:21:25 +08:00
ctx->running.store(true);
streams[key] = std::move(ctx);
}
2025-10-15 15:33:00 +08:00
2025-10-15 16:21:25 +08:00
std::future<StreamResultInfo> fut = ctx_ptr->start_result.get_future();
2025-10-15 15:33:00 +08:00
2025-10-15 16:21:25 +08:00
ctx_ptr->thread = std::thread([cam, type, ctx_ptr]() { RTMPManager::stream_loop(cam, type, ctx_ptr); });
2025-10-15 15:33:00 +08:00
2025-10-15 16:21:25 +08:00
if (fut.wait_for(std::chrono::seconds(10)) == std::future_status::ready)
{
res = fut.get();
}
else
{
res.result = 1;
res.reason = "Start timeout";
// 超时时停止线程
stop_camera(cam.name, type);
}
2025-10-15 15:33:00 +08:00
}
2025-10-15 16:21:25 +08:00
catch (const std::exception &e)
2025-10-15 15:33:00 +08:00
{
2025-10-15 16:21:25 +08:00
LOG_ERROR("[RTMP] Exception in start_camera for " + key + ": " + e.what());
2025-10-15 15:33:00 +08:00
res.result = 1;
2025-10-15 16:21:25 +08:00
res.reason = "Start exception: " + std::string(e.what());
2025-10-15 13:09:10 +08:00
}
2025-10-15 15:01:55 +08:00
return res;
}
2025-10-15 11:27:53 +08:00
2025-10-15 15:01:55 +08:00
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);
2025-10-15 11:27:53 +08:00
2025-10-15 15:01:55 +08:00
std::unique_ptr<StreamContext> ctx;
std::string key = make_stream_key(cam_name, type);
2025-10-15 16:07:36 +08:00
2025-10-15 15:01:55 +08:00
{
std::lock_guard<std::mutex> lock(streams_mutex);
auto it = streams.find(key);
if (it == streams.end())
{
res.result = 0;
res.reason = "Not streaming";
return res;
}
2025-10-15 16:13:52 +08:00
// 标记停止
2025-10-15 15:01:55 +08:00
it->second->running.store(false);
2025-10-15 16:07:36 +08:00
2025-10-15 16:13:52 +08:00
// 移交所有权给局部变量,避免在锁内 join
2025-10-15 15:01:55 +08:00
ctx = std::move(it->second);
streams.erase(it);
}
2025-10-15 16:13:52 +08:00
// 安全 join
if (ctx->thread.joinable()) ctx->thread.join();
2025-10-15 15:01:55 +08:00
res.result = 0;
res.reason = "Stopped manually";
return res;
2025-10-15 11:27:53 +08:00
}
2025-10-15 15:36:48 +08:00
void RTMPManager::stream_loop(Camera cam, StreamType type, StreamContext *ctx)
2025-10-15 15:01:55 +08:00
{
2025-10-15 15:36:48 +08:00
constexpr int FIRST_FRAME_TIMEOUT_SEC = 5;
2025-10-15 15:01:55 +08:00
2025-10-15 15:33:00 +08:00
StreamResultInfo res;
res.loc = get_camera_index(cam.name);
res.url = get_stream_url(cam.name, type);
2025-10-15 10:18:02 +08:00
std::string key = make_stream_key(cam.name, type);
2025-10-15 08:50:01 +08:00
LOG_INFO("[RTMP] Stream loop started for " + key);
2025-10-15 16:21:25 +08:00
GstElement *pipeline = nullptr;
GstBus *bus = nullptr;
2025-10-15 16:13:52 +08:00
bool start_result_set = false;
2025-10-15 16:21:25 +08:00
try
2025-10-15 16:13:52 +08:00
{
2025-10-15 16:21:25 +08:00
// 确保在异常情况下也能设置结果
auto set_start_result = [&](const StreamResultInfo &r)
{
if (!start_result_set)
{
try
{
ctx->start_result.set_value(r);
start_result_set = true;
}
catch (const std::future_error &e)
{
LOG_ERROR("[RTMP] Failed to set start result: " + std::string(e.what()));
}
}
};
// 创建管道
pipeline = create_pipeline(cam, type);
if (!pipeline)
2025-10-15 16:13:52 +08:00
{
2025-10-15 16:21:25 +08:00
res.result = 1;
res.reason = "Failed to create pipeline";
set_start_result(res);
return;
2025-10-15 16:13:52 +08:00
}
2025-10-15 16:21:25 +08:00
bus = gst_element_get_bus(pipeline);
if (!bus)
{
res.result = 1;
res.reason = "Failed to get pipeline bus";
set_start_result(res);
return;
}
2025-10-15 14:14:00 +08:00
2025-10-15 16:21:25 +08:00
// 设置管道状态
GstStateChangeReturn state_ret = gst_element_set_state(pipeline, GST_STATE_PLAYING);
if (state_ret == GST_STATE_CHANGE_FAILURE)
{
2025-10-15 16:21:25 +08:00
res.result = 1;
res.reason = "Failed to set pipeline to playing state";
set_start_result(res);
return;
}
2025-10-15 16:21:25 +08:00
bool first_frame_received = false;
auto start_time = std::chrono::steady_clock::now();
2025-10-15 15:33:00 +08:00
2025-10-15 16:21:25 +08:00
while (true)
2025-10-15 10:42:49 +08:00
{
2025-10-15 16:21:25 +08:00
// 检查是否应该停止
2025-10-15 15:33:00 +08:00
{
2025-10-15 16:21:25 +08:00
std::lock_guard<std::mutex> lock(streams_mutex);
auto it = streams.find(key);
if (it == streams.end() || !it->second->running.load())
{
LOG_INFO("[RTMP] Stream loop stopping for " + key);
break;
}
2025-10-15 15:33:00 +08:00
}
2025-10-15 10:42:49 +08:00
2025-10-15 16:21:25 +08:00
// 检查第一帧超时
if (!first_frame_received)
2025-10-15 10:42:49 +08:00
{
2025-10-15 16:21:25 +08:00
auto elapsed = std::chrono::steady_clock::now() - start_time;
if (std::chrono::duration_cast<std::chrono::seconds>(elapsed).count() > FIRST_FRAME_TIMEOUT_SEC)
{
res.result = 1;
res.reason = "No frames received within timeout";
set_start_result(res);
LOG_WARN("[RTMP] First frame timeout for " + key);
break;
}
2025-10-15 10:42:49 +08:00
}
2025-10-15 16:21:25 +08:00
// 等待GStreamer消息
GstMessage *msg = gst_bus_timed_pop_filtered(
bus, 100 * GST_MSECOND,
static_cast<GstMessageType>(GST_MESSAGE_ERROR | GST_MESSAGE_EOS | GST_MESSAGE_STATE_CHANGED |
GST_MESSAGE_ELEMENT));
if (!msg) continue;
switch (GST_MESSAGE_TYPE(msg))
{
case GST_MESSAGE_ERROR:
2025-10-15 11:06:23 +08:00
{
2025-10-15 16:21:25 +08:00
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;
2025-10-15 16:13:52 +08:00
set_start_result(res);
2025-10-15 16:21:25 +08:00
LOG_ERROR("[RTMP] Pipeline error for " + key + ": " + err_msg);
if (err) g_error_free(err);
if (debug) g_free(debug);
gst_message_unref(msg);
goto cleanup;
2025-10-15 11:06:23 +08:00
}
2025-10-15 16:21:25 +08:00
case GST_MESSAGE_EOS:
res.result = 1;
res.reason = "EOS received";
set_start_result(res);
LOG_INFO("[RTMP] EOS received for " + key);
gst_message_unref(msg);
goto cleanup;
case GST_MESSAGE_STATE_CHANGED:
// 可以添加状态变化的日志用于调试
break;
case GST_MESSAGE_ELEMENT:
if (!first_frame_received)
{
first_frame_received = true;
res.result = 0;
res.reason = "First frame received, started OK";
set_start_result(res);
LOG_INFO("[RTMP] First frame received for " + key);
}
break;
default:
break;
}
gst_message_unref(msg);
}
}
catch (const std::exception &e)
{
LOG_ERROR("[RTMP] Exception in stream_loop for " + key + ": " + e.what());
res.result = 1;
res.reason = "Exception: " + std::string(e.what());
if (!start_result_set)
{
try
{
ctx->start_result.set_value(res);
start_result_set = true;
}
catch (const std::future_error &)
{
// 忽略future错误
}
}
2025-10-15 16:21:25 +08:00
}
catch (...)
{
LOG_ERROR("[RTMP] Unknown exception in stream_loop for " + key);
res.result = 1;
res.reason = "Unknown exception";
if (!start_result_set)
{
try
{
ctx->start_result.set_value(res);
start_result_set = true;
}
catch (const std::future_error &)
{
// 忽略future错误
}
}
}
2025-10-15 10:42:49 +08:00
2025-10-15 16:21:25 +08:00
cleanup:
// 清理GStreamer资源
if (pipeline)
{
gst_element_set_state(pipeline, GST_STATE_NULL);
gst_object_unref(pipeline);
}
if (bus)
{
gst_object_unref(bus);
}
2025-10-15 16:21:25 +08:00
// 确保start_result被设置
if (!start_result_set)
{
try
{
res.result = 1;
res.reason = "Stream loop exited unexpectedly";
ctx->start_result.set_value(res);
}
catch (const std::future_error &)
{
// 忽略future错误
}
}
2025-10-15 15:33:00 +08:00
2025-10-15 16:21:25 +08:00
// 从streams中移除
2025-10-15 14:14:00 +08:00
{
std::lock_guard<std::mutex> lock(streams_mutex);
2025-10-15 15:33:00 +08:00
streams.erase(key);
}
LOG_INFO("[RTMP] Stream loop ended for " + key);
}
2025-10-15 15:46:27 +08:00
bool RTMPManager::is_streaming(const std::string &cam_name, StreamType type)
{
std::lock_guard<std::mutex> 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<std::mutex> lock(streams_mutex);
for (auto &kv : streams)
if (kv.second->running.load()) return true;
return false;
}
void RTMPManager::stop_all()
{
std::vector<std::unique_ptr<StreamContext>> ctxs;
2025-10-15 16:07:36 +08:00
2025-10-15 15:46:27 +08:00
{
std::lock_guard<std::mutex> lock(streams_mutex);
2025-10-15 16:13:52 +08:00
for (auto &kv : streams) kv.second->running.store(false); // 标记停止
2025-10-15 16:07:36 +08:00
2025-10-15 16:13:52 +08:00
// 移交所有权给局部变量,避免在锁内 join
for (auto &kv : streams) ctxs.push_back(std::move(kv.second));
2025-10-15 15:46:27 +08:00
streams.clear();
}
2025-10-15 16:13:52 +08:00
// 安全 join
2025-10-15 15:46:27 +08:00
for (auto &ctx : ctxs)
2025-10-15 16:07:36 +08:00
{
2025-10-15 16:13:52 +08:00
if (ctx->thread.joinable()) ctx->thread.join();
2025-10-15 16:07:36 +08:00
}
2025-10-15 15:46:27 +08:00
LOG_INFO("[RTMP] stop_all completed.");
}
2025-10-15 15:47:25 +08:00
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);
}