2025-10-14 17:13:08 +08:00
|
|
|
// rtsp_manager.cpp
|
|
|
|
|
#include "rtmp_manager.hpp"
|
|
|
|
|
#include "logger.hpp"
|
|
|
|
|
#include <chrono>
|
|
|
|
|
#include <thread>
|
2025-10-15 08:50:01 +08:00
|
|
|
#include <iostream>
|
2025-10-14 17:49:26 +08:00
|
|
|
#include <algorithm>
|
2025-10-14 17:13:08 +08:00
|
|
|
|
|
|
|
|
std::unordered_map<std::string, RTMPManager::StreamContext> RTMPManager::streams;
|
|
|
|
|
std::mutex RTMPManager::streams_mutex;
|
2025-10-15 08:50:01 +08:00
|
|
|
RTMPManager::StreamCallback RTMPManager::status_callback = nullptr;
|
2025-10-14 17:13:08 +08:00
|
|
|
|
|
|
|
|
static inline std::string stream_type_suffix(StreamType type)
|
|
|
|
|
{
|
|
|
|
|
return (type == StreamType::MAIN) ? "_main" : "_sub";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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 08:50:01 +08:00
|
|
|
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<std::mutex> lock(streams_mutex);
|
|
|
|
|
auto it = streams.find(key);
|
|
|
|
|
if (it != streams.end())
|
|
|
|
|
it->second.status = status;
|
|
|
|
|
if (status_callback)
|
|
|
|
|
status_callback(key, status);
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-14 17:13:08 +08:00
|
|
|
GstElement *RTMPManager::create_pipeline(const Camera &cam, StreamType type)
|
|
|
|
|
{
|
|
|
|
|
int width = cam.width;
|
|
|
|
|
int height = cam.height;
|
|
|
|
|
int fps = cam.fps;
|
|
|
|
|
int 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:13:08 +08:00
|
|
|
}
|
|
|
|
|
|
2025-10-14 17:49:26 +08:00
|
|
|
std::string stream_name = cam.name + stream_type_suffix(type);
|
2025-10-14 17:13:08 +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) +
|
2025-10-15 08:50:01 +08:00
|
|
|
",framerate=" + std::to_string(fps) +
|
|
|
|
|
"/1 ! queue max-size-buffers=1 leaky=downstream "
|
|
|
|
|
"! mpph264enc bps=" +
|
2025-10-14 17:13:08 +08:00
|
|
|
std::to_string(bitrate) +
|
|
|
|
|
" gop=" + std::to_string(fps) +
|
2025-10-15 08:50:01 +08:00
|
|
|
" ! h264parse ! flvmux streamable=true name=mux "
|
|
|
|
|
"! rtmpsink location=\"rtmp://127.0.0.1/live/" +
|
|
|
|
|
stream_name + " live=1\" sync=false";
|
2025-10-14 17:13:08 +08:00
|
|
|
|
2025-10-15 08:50:01 +08:00
|
|
|
LOG_INFO("[RTMP] Creating pipeline for '" + stream_name + "': " + pipeline_str);
|
2025-10-14 17:13:08 +08:00
|
|
|
|
|
|
|
|
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));
|
2025-10-14 17:13:08 +08:00
|
|
|
g_error_free(error);
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
return pipeline;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RTMPManager::stream_loop(Camera cam, StreamType type)
|
|
|
|
|
{
|
|
|
|
|
const 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);
|
|
|
|
|
|
|
|
|
|
const int MAX_RETRIES = 5;
|
|
|
|
|
int retry_count = 0;
|
2025-10-14 17:13:08 +08:00
|
|
|
|
|
|
|
|
while (true)
|
|
|
|
|
{
|
|
|
|
|
{
|
|
|
|
|
std::lock_guard<std::mutex> lock(streams_mutex);
|
2025-10-15 08:50:01 +08:00
|
|
|
if (!streams[key].running)
|
2025-10-14 17:13:08 +08:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
GstElement *pipeline = create_pipeline(cam, type);
|
|
|
|
|
if (!pipeline)
|
|
|
|
|
{
|
2025-10-15 08:50:01 +08:00
|
|
|
update_status(key, {false, StreamResult::PIPELINE_ERROR, "Failed to create pipeline"});
|
2025-10-14 17:13:08 +08:00
|
|
|
std::this_thread::sleep_for(std::chrono::seconds(3));
|
2025-10-15 08:50:01 +08:00
|
|
|
if (++retry_count > MAX_RETRIES)
|
|
|
|
|
break;
|
2025-10-14 17:13:08 +08:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
GstBus *bus = gst_element_get_bus(pipeline);
|
|
|
|
|
gst_element_set_state(pipeline, GST_STATE_PLAYING);
|
2025-10-15 08:50:01 +08:00
|
|
|
update_status(key, {true, StreamResult::OK, ""});
|
|
|
|
|
LOG_INFO("[RTMP] " + key + " is streaming.");
|
2025-10-14 17:13:08 +08:00
|
|
|
|
2025-10-15 08:50:01 +08:00
|
|
|
bool stop_flag = false;
|
|
|
|
|
GstMessage *msg = nullptr;
|
2025-10-14 17:13:08 +08:00
|
|
|
|
|
|
|
|
while (true)
|
|
|
|
|
{
|
2025-10-15 08:50:01 +08:00
|
|
|
msg = gst_bus_timed_pop_filtered(
|
2025-10-14 17:13:08 +08:00
|
|
|
bus, GST_CLOCK_TIME_NONE,
|
|
|
|
|
static_cast<GstMessageType>(GST_MESSAGE_ERROR | GST_MESSAGE_EOS));
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
std::lock_guard<std::mutex> lock(streams_mutex);
|
2025-10-15 08:50:01 +08:00
|
|
|
if (!streams[key].running)
|
|
|
|
|
{
|
|
|
|
|
stop_flag = true;
|
2025-10-14 17:13:08 +08:00
|
|
|
break;
|
2025-10-15 08:50:01 +08:00
|
|
|
}
|
2025-10-14 17:13:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!msg)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
if (GST_MESSAGE_TYPE(msg) == GST_MESSAGE_ERROR)
|
|
|
|
|
{
|
|
|
|
|
GError *err = nullptr;
|
|
|
|
|
gchar *debug = nullptr;
|
|
|
|
|
gst_message_parse_error(msg, &err, &debug);
|
2025-10-15 08:50:01 +08:00
|
|
|
std::string err_msg = err ? err->message : "Unknown GStreamer error";
|
|
|
|
|
LOG_ERROR("[RTMP] Error in " + key + ": " + err_msg);
|
|
|
|
|
update_status(key, {false, StreamResult::CONNECTION_FAIL, err_msg});
|
2025-10-14 17:13:08 +08:00
|
|
|
g_error_free(err);
|
|
|
|
|
g_free(debug);
|
2025-10-15 08:50:01 +08:00
|
|
|
stop_flag = true;
|
2025-10-14 17:13:08 +08:00
|
|
|
}
|
|
|
|
|
else if (GST_MESSAGE_TYPE(msg) == GST_MESSAGE_EOS)
|
|
|
|
|
{
|
2025-10-15 08:50:01 +08:00
|
|
|
LOG_WARN("[RTMP] EOS on " + key);
|
|
|
|
|
update_status(key, {false, StreamResult::EOS_RECEIVED, "EOS"});
|
|
|
|
|
stop_flag = true;
|
2025-10-14 17:13:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
gst_message_unref(msg);
|
2025-10-15 08:50:01 +08:00
|
|
|
if (stop_flag)
|
2025-10-14 17:13:08 +08:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
gst_element_set_state(pipeline, GST_STATE_NULL);
|
|
|
|
|
gst_object_unref(bus);
|
2025-10-15 08:39:35 +08:00
|
|
|
gst_object_unref(pipeline);
|
2025-10-14 17:13:08 +08:00
|
|
|
|
2025-10-15 08:50:01 +08:00
|
|
|
if (!stop_flag)
|
2025-10-14 17:13:08 +08:00
|
|
|
break;
|
|
|
|
|
|
2025-10-15 08:50:01 +08:00
|
|
|
if (++retry_count > MAX_RETRIES)
|
2025-10-14 17:13:08 +08:00
|
|
|
{
|
2025-10-15 08:50:01 +08:00
|
|
|
LOG_ERROR("[RTMP] " + key + " reached max retries. Giving up.");
|
2025-10-14 17:13:08 +08:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-15 08:50:01 +08:00
|
|
|
LOG_WARN("[RTMP] Reconnecting " + key + " in 3s...");
|
2025-10-14 17:13:08 +08:00
|
|
|
std::this_thread::sleep_for(std::chrono::seconds(3));
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-15 08:50:01 +08:00
|
|
|
update_status(key, {false, StreamResult::UNKNOWN, "Stream loop exited"});
|
|
|
|
|
LOG_INFO("[RTMP] Stream loop ended for " + key);
|
2025-10-14 17:13:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RTMPManager::start_camera(const Camera &cam, StreamType type)
|
|
|
|
|
{
|
|
|
|
|
std::string key = make_stream_key(cam.name, type);
|
2025-10-15 08:50:01 +08:00
|
|
|
std::lock_guard<std::mutex> lock(streams_mutex);
|
2025-10-15 08:39:35 +08:00
|
|
|
|
2025-10-15 08:50:01 +08:00
|
|
|
auto it = streams.find(key);
|
|
|
|
|
if (it != streams.end() && it->second.running)
|
2025-10-14 17:13:08 +08:00
|
|
|
{
|
2025-10-15 08:50:01 +08:00
|
|
|
LOG_WARN("[RTMP] " + key + " already streaming.");
|
2025-10-14 17:13:08 +08:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
StreamContext ctx;
|
2025-10-15 08:50:01 +08:00
|
|
|
ctx.running = true;
|
2025-10-14 17:13:08 +08:00
|
|
|
ctx.thread = std::thread([cam, type]()
|
2025-10-15 08:50:01 +08:00
|
|
|
{ stream_loop(cam, type); });
|
2025-10-15 08:54:08 +08:00
|
|
|
streams.emplace(key, std::move(ctx));
|
2025-10-14 17:13:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RTMPManager::stop_camera(const std::string &cam_name, StreamType type)
|
|
|
|
|
{
|
|
|
|
|
std::string key = make_stream_key(cam_name, type);
|
2025-10-15 08:50:01 +08:00
|
|
|
std::unique_lock<std::mutex> lock(streams_mutex);
|
2025-10-14 17:13:08 +08:00
|
|
|
auto it = streams.find(key);
|
|
|
|
|
if (it == streams.end())
|
|
|
|
|
{
|
2025-10-15 08:50:01 +08:00
|
|
|
LOG_WARN("[RTMP] " + key + " not found.");
|
2025-10-14 17:13:08 +08:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-15 08:50:01 +08:00
|
|
|
it->second.running = false;
|
2025-10-15 08:39:35 +08:00
|
|
|
auto th = std::move(it->second.thread);
|
2025-10-14 17:13:08 +08:00
|
|
|
streams.erase(it);
|
2025-10-15 08:39:35 +08:00
|
|
|
lock.unlock();
|
|
|
|
|
|
|
|
|
|
if (th.joinable())
|
|
|
|
|
th.join();
|
2025-10-15 08:50:01 +08:00
|
|
|
|
|
|
|
|
update_status(key, {false, StreamResult::OK, "Stopped manually"});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RTMPManager::stop_all()
|
|
|
|
|
{
|
|
|
|
|
std::vector<std::string> keys;
|
|
|
|
|
{
|
|
|
|
|
std::lock_guard<std::mutex> lock(streams_mutex);
|
|
|
|
|
for (auto &kv : streams)
|
|
|
|
|
keys.push_back(kv.first);
|
|
|
|
|
}
|
|
|
|
|
for (auto &key : keys)
|
|
|
|
|
{
|
|
|
|
|
if (key.find("_main") != std::string::npos)
|
|
|
|
|
stop_camera(key.substr(0, key.size() - 5), StreamType::MAIN);
|
|
|
|
|
else
|
|
|
|
|
stop_camera(key.substr(0, key.size() - 4), StreamType::SUB);
|
|
|
|
|
}
|
2025-10-14 17:13:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool RTMPManager::is_streaming(const std::string &cam_name, StreamType type)
|
|
|
|
|
{
|
|
|
|
|
std::lock_guard<std::mutex> lock(streams_mutex);
|
2025-10-15 08:50:01 +08:00
|
|
|
auto key = make_stream_key(cam_name, type);
|
2025-10-14 17:13:08 +08:00
|
|
|
auto it = streams.find(key);
|
2025-10-15 08:50:01 +08:00
|
|
|
return it != streams.end() && it->second.running;
|
2025-10-14 17:13:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool RTMPManager::is_any_streaming()
|
|
|
|
|
{
|
|
|
|
|
std::lock_guard<std::mutex> lock(streams_mutex);
|
|
|
|
|
for (auto &kv : streams)
|
2025-10-15 08:50:01 +08:00
|
|
|
if (kv.second.running)
|
2025-10-14 17:13:08 +08:00
|
|
|
return true;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string RTMPManager::get_stream_url(const std::string &cam_name, StreamType type)
|
|
|
|
|
{
|
2025-10-14 17:49:26 +08:00
|
|
|
return "rtmp://127.0.0.1/live/" + make_stream_key(cam_name, type);
|
2025-10-14 17:13:08 +08:00
|
|
|
}
|