Compare commits

...

3 Commits

Author SHA1 Message Date
GuanYuankai db25f3e3a0 增加ignore 2025-10-21 07:40:05 +00:00
GuanYuankai 337a02a9ec 增加HLS转发协议 2025-10-21 07:40:05 +00:00
GuanYuankai f42ee41573 完成GStreamer工作流最小核心,采集视频数据 2025-10-21 07:39:48 +00:00
6 changed files with 221 additions and 39 deletions

1
.gitignore vendored
View File

@ -48,6 +48,7 @@ build-gyk/
# Ignore Mosquitto runtime data and logs
mosquitto/data/
mosquitto/log/
hls_streams/
# 特例列表
!rknn_sdk/librknn_api/aarch64/*.so

View File

@ -4,7 +4,11 @@
"name": "Linux",
"includePath": [
"${workspaceFolder}/**",
"/usr/include"
"/usr/include",
"/usr/include/gstreamer-1.0",
"/usr/include/glib-2.0",
"/usr/lib/aarch64-linux-gnu/glib-2.0/include"
// (GStreamer GLib, GLib .include )
],
"defines": [
"CROW_USE_BOOST"

View File

@ -14,7 +14,6 @@ find_package(SQLite3 REQUIRED)
find_package(PkgConfig REQUIRED)
pkg_check_modules(GST REQUIRED gstreamer-1.0 gstreamer-app-1.0)
find_package(OpenCV REQUIRED)
# Crow CMake
# Crow 使 Boost Asio
add_subdirectory(src/vendor/crow)
@ -117,42 +116,23 @@ target_link_libraries(test PRIVATE
)
# 1. 定义新的可执行文件 (我们将把代码放在 src/streamer/ 目录下)
# add_executable(edge_streamer
# src/streamer/main.cpp
# src/streamer/StreamManager.cpp
# )
add_executable(edge_streamer
src/streamer/main.cpp
)
# # 2.
# target_link_libraries(edge_streamer PRIVATE
# # GStreamer (来自我们新增的 pkg_check_modules)
# ${GST_LIBRARIES}
#
target_link_libraries(edge_streamer PRIVATE
# --- GStreamer (来自 pkg_check_modules) ---
${GST_LIBRARIES}
# Crow
# nlohmann_json
# PahoMqttCpp::paho-mqttpp3
# Boost::system
# Boost::thread
# rknn_api
# rknnrt
# pthread
# ssl
# crypto
# )
pthread
)
# # 3.
# target_include_directories(edge_streamer PRIVATE
# #
# ${CMAKE_CURRENT_SOURCE_DIR}/src/streamer/include
# ${CMAKE_CURRENT_SOURCE_DIR}/src/vendor
# # GStreamer
# ${GST_INCLUDE_DIRS}
# ${Boost_INCLUDE_DIRS}
# # RKNN C-API 的头文件 (来自我们 Dockerfile /usr/local/include)
# /usr/local/include
# )
#
target_include_directories(edge_streamer PRIVATE
#
${CMAKE_CURRENT_SOURCE_DIR}/src/streamer/include
# --- GStreamer (来自 pkg_check_modules) ---
${GST_INCLUDE_DIRS}
)

View File

@ -1,7 +1,7 @@
{
"modbus_rtu_devices": [
{
"enabled": true,
"enabled": false,
"device_id": "rtu_temp_sensor_lab",
"port_path": "/dev/ttyS7",
"baud_rate": 9600,
@ -13,7 +13,7 @@
]
},
{
"enabled": true,
"enabled": false,
"device_id": "rotary encoder",
"port_path": "/dev/ttyS7",
"baud_rate": 9600,

155
src/streamer/main.cpp Normal file
View File

@ -0,0 +1,155 @@
/*
* : src/streamer/main.cpp
* : 1.0 - HLS
* : 1. 线 (rtspsrc -> ... -> appsink)
* 2. HLS 线 (appsrc -> ... -> hlssink)
* 3. C++ (1) (2)
*/
#include <gst/gst.h>
#include <gst/app/gstappsink.h>
#include <gst/app/gstappsrc.h>
#include <glib.h>
#include <stdlib.h>
typedef struct {
GMainLoop *loop;
GstElement *ingest_pipeline; // 采集管线
GstElement *hls_pipeline; // HLS 编码管线
GstElement *hls_appsrc; // HLS 管线的 "appsrc" 元件
} AppData;
/**
* @brief (Callback)
* 'appsink'
*/
static GstFlowReturn on_new_sample (GstAppSink * appsink, gpointer user_data)
{
AppData *data = (AppData *) user_data;
static guint frame_count = 0;
// 从 appsink 拉取样本 (包含解码后的原始帧)
GstSample *sample = gst_app_sink_pull_sample (appsink);
if (sample == NULL) {
return GST_FLOW_ERROR;
}
// 检查 HLS 编码管线的 appsrc 是否已准备好
if (data->hls_appsrc) {
// 将样本sample直接推送到 hls_appsrc
// GStreamer 会自动处理缓冲和线程
GstFlowReturn ret = gst_app_src_push_sample (GST_APP_SRC (data->hls_appsrc), sample);
if (ret != GST_FLOW_OK) {
g_warning ("Failed to push sample to HLS appsrc");
}
}
// 打印日志
frame_count++;
if (frame_count % 100 == 0) {
g_print ("Hardware decoder acquired frame %u\n", frame_count);
}
// 【重要】释放样本
// 无论推送成功与否,我们都必须释放 samplehls_appsrc 已持有它
gst_sample_unref (sample);
return GST_FLOW_OK;
}
int main (int argc, char *argv[])
{
AppData data = {0}; // 初始化数据结构
GstElement *appsink;
GError *error = NULL;
gst_init (&argc, &argv);
data.loop = g_main_loop_new (NULL, FALSE);
// --- 2. 【新增】HLS 路径和目录准备 ---
const char *hls_output_dir = "/app/hls_streams/live";
const char *playlist_location = "/app/hls_streams/live/playlist.m3u8";
g_print("Creating HLS directory: %s\n", hls_output_dir);
g_autofree gchar *mkdir_cmd = g_strdup_printf("mkdir -p %s", hls_output_dir);
system(mkdir_cmd);
const gchar *ingest_pipeline_str =
"rtspsrc location=rtsp://admin:hzx12345@192.168.1.10:554/Streaming/Channels/1301 latency=300 protocols=tcp ! "
"application/x-rtp, media=(string)video ! "
"rtpjitterbuffer latency=100 ! "
"rtph265depay ! h265parse ! queue ! mppvideodec ! "
"video/x-raw,format=NV12 ! "
"appsink name=sink emit-signals=true max-buffers=5 drop=true";
g_print ("Using Ingest pipeline:\n%s\n", ingest_pipeline_str);
data.ingest_pipeline = gst_parse_launch (ingest_pipeline_str, &error);
if (error) {
g_printerr ("Failed to parse ingest pipeline: %s\n", error->message);
g_error_free (error);
return -1;
}
appsink = gst_bin_get_by_name (GST_BIN (data.ingest_pipeline), "sink");
g_signal_connect (appsink, "new-sample", G_CALLBACK (on_new_sample), &data);
g_object_unref (appsink);
// --- 配置 HLS 编码管线 (HLS Pipeline) ---
g_autofree gchar *hls_pipeline_str = g_strdup_printf(
"appsrc name=source ! " // (您的) appsrcC++代码已为其设置了 1920x1080 NV12 Caps
"mpph264enc ! " // (您的) H.264 硬件编码器
"h264parse ! " // (您的) H.264 解析器
"mpegtsmux ! " // [关键修复] 将 H.264 裸流打包成 MPEG-TS 流
"hlssink "
" playlist_root=http://192.168.0.108:8080/streams/live "
" playlist_location=%s "
" location=%s/segment%%05d.ts "
" target-duration=1 "
" max-files=3",
playlist_location,
hls_output_dir
);
g_print ("Using HLS pipeline:\n%s\n", hls_pipeline_str);
data.hls_pipeline = gst_parse_launch (hls_pipeline_str, &error);
if (error) {
g_printerr ("Failed to parse HLS pipeline: %s\n", error->message);
g_error_free (error);
return -1;
}
data.hls_appsrc = gst_bin_get_by_name (GST_BIN (data.hls_pipeline), "source");
if (!data.hls_appsrc) {
g_printerr("Failed to get HLS appsrc element.\n");
return -1;
}
GstCaps *caps = gst_caps_new_simple ("video/x-raw",
"format", G_TYPE_STRING, "NV12",
"width", G_TYPE_INT, 1920,
"height", G_TYPE_INT, 1080,
"framerate", GST_TYPE_FRACTION, 25, 1,
NULL);
g_object_set (data.hls_appsrc, "caps", caps, NULL);
g_object_set (data.hls_appsrc, "format", GST_FORMAT_TIME, NULL);
gst_caps_unref (caps);
g_print ("Starting Ingest pipeline...\n");
gst_element_set_state (data.ingest_pipeline, GST_STATE_PLAYING);
g_print ("Starting HLS pipeline...\n");
gst_element_set_state (data.hls_pipeline, GST_STATE_PLAYING);
g_print ("All pipelines started. Waiting for frames...\n");
g_main_loop_run (data.loop);
g_print ("Exiting...\n");
gst_element_set_state (data.ingest_pipeline, GST_STATE_NULL);
gst_element_set_state (data.hls_pipeline, GST_STATE_NULL);
gst_object_unref (data.ingest_pipeline);
gst_object_unref (data.hls_pipeline);
if (data.hls_appsrc) g_object_unref (data.hls_appsrc);
g_main_loop_unref (data.loop);
return 0;
}

View File

@ -1,6 +1,8 @@
// 文件名: src/web/web_server.cc
#include "web_server.h"
#include "spdlog/spdlog.h"
#include <fstream>
#include <sstream>
// 构造函数现在需要调用基类的构造函数
WebServer::WebServer(SystemMonitor::SystemMonitor& monitor, DeviceManager& deviceManager, LiveDataCache& liveDataCache,uint16_t port)
@ -109,4 +111,44 @@ void WebServer::setup_routes() {
return response;
});
CROW_ROUTE((*this), "/streams/<string>/<string>")
.methods("GET"_method)
([this](const crow::request& req, crow::response& res, std::string stream_id, std::string filename) {
// **安全检查**: 防止路径遍历攻击
if (filename.find("..") != std::string::npos || stream_id.find("..") != std::string::npos) {
res.code = 400;
res.end("Bad Request");
return;
}
// HLS 文件在磁盘上的真实路径
std::string file_path = "/app/hls_streams/" + stream_id + "/" + filename;
std::ifstream file(file_path, std::ios::binary);
if (!file.is_open()) {
// 文件未找到
res.code = 404;
res.end("Not Found");
return;
}
// 将文件内容读入字符串流
std::ostringstream contents;
contents << file.rdbuf();
file.close();
res.body = contents.str();
if (filename.find(".m3u8") != std::string::npos) {
res.set_header("Content-Type", "application/vnd.apple.mpegurl");
res.set_header("Cache-Control", "no-cache, no-store, must-revalidate");
res.set_header("Pragma", "no-cache");
res.set_header("Expires", "0");
} else if (filename.find(".ts") != std::string::npos) {
res.set_header("Content-Type", "video/MP2T");
} else {
res.set_header("Content-Type", "application/octet-stream");
}
res.end();
});
}