2025-10-16 10:25:51 +08:00
|
|
|
// rtsp_manager.cpp
|
2025-10-14 17:13:08 +08:00
|
|
|
#include "rtmp_manager.hpp"
|
2025-10-15 14:14:00 +08:00
|
|
|
|
2025-10-16 10:25:51 +08:00
|
|
|
#include <chrono>
|
2025-10-16 10:52:51 +08:00
|
|
|
#include <future>
|
2025-10-16 10:25:51 +08:00
|
|
|
#include <iostream>
|
|
|
|
|
#include <thread>
|
2025-10-15 10:18:02 +08:00
|
|
|
|
2025-10-15 14:14:00 +08:00
|
|
|
#include "logger.hpp"
|
|
|
|
|
|
2025-10-16 10:25:51 +08:00
|
|
|
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;
|
2025-10-14 17:13:08 +08:00
|
|
|
std::mutex RTMPManager::streams_mutex;
|
2025-10-16 10:25:51 +08:00
|
|
|
RTMPManager::StreamCallback RTMPManager::status_callback = nullptr;
|
2025-10-14 17:13:08 +08:00
|
|
|
|
2025-10-16 10:25:51 +08:00
|
|
|
std::string RTMPManager::make_stream_key(const std::string &cam_name, StreamType type)
|
|
|
|
|
{
|
|
|
|
|
return cam_name + stream_type_suffix(type);
|
|
|
|
|
}
|
2025-10-14 17:13:08 +08:00
|
|
|
|
|
|
|
|
void RTMPManager::init()
|
|
|
|
|
{
|
|
|
|
|
gst_init(nullptr, nullptr);
|
2025-10-16 10:25:51 +08:00
|
|
|
LOG_INFO("[RTMP] GStreamer initialized.");
|
2025-10-14 17:13:08 +08:00
|
|
|
}
|
|
|
|
|
|
2025-10-16 10:25:51 +08:00
|
|
|
void RTMPManager::set_status_callback(StreamCallback cb) { status_callback = std::move(cb); }
|
2025-10-15 08:50:01 +08:00
|
|
|
|
2025-10-16 10:25:51 +08:00
|
|
|
void RTMPManager::update_status(const std::string &key, const StreamStatus &status)
|
|
|
|
|
{
|
2025-10-14 17:13:08 +08:00
|
|
|
{
|
2025-10-15 17:01:43 +08:00
|
|
|
std::lock_guard<std::mutex> lock(streams_mutex);
|
2025-10-16 10:25:51 +08:00
|
|
|
auto it = streams.find(key);
|
|
|
|
|
if (it != streams.end()) it->second->status = status;
|
2025-10-14 17:13:08 +08:00
|
|
|
}
|
2025-10-16 10:25:51 +08:00
|
|
|
if (status_callback) status_callback(key, status);
|
2025-10-14 17:13:08 +08:00
|
|
|
}
|
|
|
|
|
|
2025-10-16 10:25:51 +08:00
|
|
|
GstElement *RTMPManager::create_pipeline(const Camera &cam, StreamType type)
|
2025-10-15 11:27:53 +08:00
|
|
|
{
|
2025-10-16 10:25:51 +08:00
|
|
|
int width = cam.width, height = cam.height, fps = cam.fps, bitrate = cam.bitrate;
|
|
|
|
|
if (type == StreamType::SUB)
|
2025-10-15 17:01:43 +08:00
|
|
|
{
|
2025-10-16 10:25:51 +08:00
|
|
|
width = std::max(160, width / 2);
|
|
|
|
|
height = std::max(120, height / 2);
|
|
|
|
|
fps = std::max(10, fps / 2);
|
|
|
|
|
bitrate = std::max(300000, bitrate / 2);
|
2025-10-15 17:01:43 +08:00
|
|
|
}
|
2025-10-15 15:01:55 +08:00
|
|
|
|
2025-10-16 10:25:51 +08:00
|
|
|
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";
|
2025-10-15 16:21:25 +08:00
|
|
|
|
2025-10-16 10:25:51 +08:00
|
|
|
LOG_INFO("[RTMP] Creating pipeline for '" + stream_name + "': " + pipeline_str);
|
2025-10-15 15:33:00 +08:00
|
|
|
|
2025-10-16 10:25:51 +08:00
|
|
|
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;
|
2025-10-15 17:01:43 +08:00
|
|
|
}
|
2025-10-16 10:25:51 +08:00
|
|
|
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<int>(i);
|
|
|
|
|
return -1;
|
2025-10-15 17:01:43 +08:00
|
|
|
}
|
2025-10-15 15:33:00 +08:00
|
|
|
|
2025-10-16 10:25:51 +08:00
|
|
|
RTMPManager::StreamResultInfo RTMPManager::start_camera(const Camera &cam, StreamType type)
|
2025-10-15 17:01:43 +08:00
|
|
|
{
|
|
|
|
|
StreamResultInfo res;
|
2025-10-16 10:25:51 +08:00
|
|
|
res.loc = get_camera_index(cam.name);
|
|
|
|
|
res.url = get_stream_url(cam.name, type);
|
2025-10-15 15:33:00 +08:00
|
|
|
|
2025-10-16 10:25:51 +08:00
|
|
|
std::string key = make_stream_key(cam.name, type);
|
2025-10-16 10:52:51 +08:00
|
|
|
std::promise<StreamStatus> status_promise;
|
|
|
|
|
auto status_future = status_promise.get_future();
|
|
|
|
|
|
2025-10-15 17:01:43 +08:00
|
|
|
{
|
|
|
|
|
std::lock_guard<std::mutex> lock(streams_mutex);
|
2025-10-16 10:25:51 +08:00
|
|
|
auto it = streams.find(key);
|
|
|
|
|
if (it != streams.end() && it->second->running.load())
|
2025-10-15 16:21:25 +08:00
|
|
|
{
|
2025-10-15 17:01:43 +08:00
|
|
|
res.result = 0;
|
|
|
|
|
res.reason = "Already streaming";
|
|
|
|
|
return res;
|
2025-10-15 16:21:25 +08:00
|
|
|
}
|
2025-10-15 17:01:43 +08:00
|
|
|
|
|
|
|
|
auto ctx = std::make_unique<StreamContext>();
|
2025-10-16 10:25:51 +08:00
|
|
|
ctx->running.store(true);
|
2025-10-16 10:52:51 +08:00
|
|
|
// 把 promise 移进线程上下文
|
|
|
|
|
ctx->thread = std::thread([cam, type, &status_promise]() { stream_loop(cam, type, &status_promise); });
|
2025-10-16 10:25:51 +08:00
|
|
|
streams.emplace(key, std::move(ctx));
|
2025-10-15 13:09:10 +08:00
|
|
|
}
|
|
|
|
|
|
2025-10-16 10:52:51 +08:00
|
|
|
// 阻塞等待线程内部更新状态,最多等 5 秒
|
|
|
|
|
if (status_future.wait_for(std::chrono::seconds(7)) == std::future_status::ready)
|
|
|
|
|
{
|
|
|
|
|
StreamStatus status = status_future.get();
|
|
|
|
|
res.result = status.running ? 0 : 1;
|
|
|
|
|
res.reason = status.last_error.empty() ? "Started OK" : status.last_error;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
res.result = 1;
|
|
|
|
|
res.reason = "Timeout waiting for stream";
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-15 15:01:55 +08:00
|
|
|
return res;
|
|
|
|
|
}
|
2025-10-15 11:27:53 +08:00
|
|
|
|
2025-10-16 10:25:51 +08:00
|
|
|
RTMPManager::StreamResultInfo RTMPManager::stop_camera(const std::string &cam_name, StreamType type)
|
2025-10-15 15:01:55 +08:00
|
|
|
{
|
|
|
|
|
StreamResultInfo res;
|
2025-10-16 10:25:51 +08:00
|
|
|
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;
|
2025-10-16 10:25:51 +08:00
|
|
|
std::string key = make_stream_key(cam_name, type);
|
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-16 10:25:51 +08:00
|
|
|
it->second->running.store(false);
|
2025-10-15 15:01:55 +08:00
|
|
|
ctx = std::move(it->second);
|
|
|
|
|
streams.erase(it);
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-15 16:13:52 +08:00
|
|
|
if (ctx->thread.joinable()) ctx->thread.join();
|
2025-10-16 10:25:51 +08:00
|
|
|
|
2025-10-15 15:01:55 +08:00
|
|
|
res.result = 0;
|
2025-10-16 10:25:51 +08:00
|
|
|
res.reason = "Stopped manually";
|
2025-10-15 15:01:55 +08:00
|
|
|
return res;
|
2025-10-16 10:25:51 +08:00
|
|
|
}
|
|
|
|
|
|
2025-10-16 10:52:51 +08:00
|
|
|
void RTMPManager::stream_loop(Camera cam, StreamType type, std::promise<StreamStatus> *status_promise)
|
|
|
|
|
{
|
|
|
|
|
std::string key = make_stream_key(cam.name, type);
|
|
|
|
|
StreamStatus status;
|
|
|
|
|
status.running = false;
|
|
|
|
|
|
|
|
|
|
GstElement *pipeline = create_pipeline(cam, type);
|
|
|
|
|
if (!pipeline)
|
|
|
|
|
{
|
|
|
|
|
status.last_result = StreamResult::PIPELINE_ERROR;
|
|
|
|
|
status.last_error = "Failed to create pipeline";
|
|
|
|
|
if (status_promise) status_promise->set_value(status);
|
|
|
|
|
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)
|
|
|
|
|
{
|
2025-10-16 11:04:22 +08:00
|
|
|
// 检查 stop_flag 或超时
|
|
|
|
|
{
|
|
|
|
|
std::lock_guard<std::mutex> lock(streams_mutex);
|
|
|
|
|
auto it = streams.find(key);
|
|
|
|
|
if (it == streams.end() || !it->second->running.load())
|
|
|
|
|
{
|
|
|
|
|
break; // 停止逻辑
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-16 10:52:51 +08:00
|
|
|
if (!first_frame_received &&
|
|
|
|
|
std::chrono::duration_cast<std::chrono::seconds>(std::chrono::steady_clock::now() - start_time).count() > 5)
|
|
|
|
|
{
|
|
|
|
|
status.last_result = StreamResult::TIMEOUT;
|
|
|
|
|
status.last_error = "No frames received within timeout";
|
|
|
|
|
if (status_promise) status_promise->set_value(status);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
GstMessage *msg = gst_bus_timed_pop_filtered(
|
|
|
|
|
bus, 100 * GST_MSECOND,
|
|
|
|
|
static_cast<GstMessageType>(GST_MESSAGE_ERROR | GST_MESSAGE_EOS | GST_MESSAGE_STATE_CHANGED));
|
|
|
|
|
|
|
|
|
|
if (!msg) continue;
|
|
|
|
|
|
|
|
|
|
switch (GST_MESSAGE_TYPE(msg))
|
|
|
|
|
{
|
|
|
|
|
case GST_MESSAGE_STATE_CHANGED:
|
|
|
|
|
{
|
|
|
|
|
GstState old_state, new_state;
|
|
|
|
|
gst_message_parse_state_changed(msg, &old_state, &new_state, nullptr);
|
|
|
|
|
if (GST_MESSAGE_SRC(msg) == GST_OBJECT(pipeline) && new_state == GST_STATE_PLAYING &&
|
|
|
|
|
!first_frame_received)
|
|
|
|
|
{
|
|
|
|
|
first_frame_received = true;
|
|
|
|
|
status.running = true;
|
|
|
|
|
status.last_result = StreamResult::OK;
|
|
|
|
|
status.last_error = "";
|
2025-10-16 11:04:22 +08:00
|
|
|
if (status_promise) status_promise->set_value(status);
|
2025-10-16 10:52:51 +08:00
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case GST_MESSAGE_ERROR:
|
|
|
|
|
case GST_MESSAGE_EOS:
|
|
|
|
|
status.running = false;
|
|
|
|
|
status.last_result = (GST_MESSAGE_TYPE(msg) == GST_MESSAGE_ERROR) ? StreamResult::CONNECTION_FAIL
|
|
|
|
|
: StreamResult::EOS_RECEIVED;
|
|
|
|
|
status.last_error = "GStreamer error/EOS";
|
|
|
|
|
if (status_promise) status_promise->set_value(status);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
gst_message_unref(msg);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
gst_element_set_state(pipeline, GST_STATE_NULL);
|
2025-10-16 11:04:22 +08:00
|
|
|
if (bus) gst_object_unref(bus);
|
2025-10-16 10:52:51 +08:00
|
|
|
gst_object_unref(pipeline);
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-16 10:25:51 +08:00
|
|
|
void RTMPManager::stop_all()
|
|
|
|
|
{
|
|
|
|
|
std::vector<std::unique_ptr<StreamContext>> ctxs;
|
|
|
|
|
{
|
|
|
|
|
std::lock_guard<std::mutex> 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<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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::vector<RTMPManager::StreamResultInfo> RTMPManager::process_push_request(const VideoPushRequest &req)
|
|
|
|
|
{
|
|
|
|
|
std::vector<StreamResultInfo> results;
|
|
|
|
|
|
|
|
|
|
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<int>(g_app_config.cameras.size())) continue;
|
|
|
|
|
|
|
|
|
|
const auto &cam = g_app_config.cameras[ch];
|
|
|
|
|
StreamResultInfo info;
|
|
|
|
|
|
|
|
|
|
if (item.switchVal == 0)
|
|
|
|
|
{
|
|
|
|
|
// 开启推流
|
|
|
|
|
info = start_camera(cam, type);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// 停止推流
|
|
|
|
|
info = stop_camera(cam.name, type);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
results.push_back(info);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return results;
|
|
|
|
|
}
|