2025-12-31 16:30:10 +08:00
|
|
|
#include "video_pipeline.hpp"
|
|
|
|
|
|
|
|
|
|
#include <chrono>
|
|
|
|
|
|
|
|
|
|
VideoPipeline::VideoPipeline() : running_(false) {}
|
|
|
|
|
|
|
|
|
|
VideoPipeline::~VideoPipeline() {
|
|
|
|
|
Stop();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void VideoPipeline::Start(const std::string& inputUrl, const std::string& outputUrl) {
|
|
|
|
|
if (running_)
|
|
|
|
|
return;
|
|
|
|
|
running_ = true;
|
|
|
|
|
spdlog::info("Starting VideoPipeline with Input: {}", inputUrl);
|
|
|
|
|
|
2026-01-04 14:52:14 +08:00
|
|
|
processingThread_ = std::thread(&VideoPipeline::processLoop, this, inputUrl, outputUrl, false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void VideoPipeline::StartTest(const std::string& filePath, const std::string& outputUrl) {
|
|
|
|
|
if (running_)
|
|
|
|
|
return;
|
|
|
|
|
running_ = true;
|
|
|
|
|
spdlog::info("Starting VideoPipeline (File Test Mode) Input: {}", filePath);
|
|
|
|
|
|
|
|
|
|
// true 表示是文件源
|
|
|
|
|
processingThread_ = std::thread(&VideoPipeline::processLoop, this, filePath, outputUrl, true);
|
2025-12-31 16:30:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void VideoPipeline::Stop() {
|
|
|
|
|
if (!running_)
|
|
|
|
|
return;
|
|
|
|
|
running_ = false;
|
|
|
|
|
if (processingThread_.joinable()) {
|
|
|
|
|
processingThread_.join();
|
|
|
|
|
}
|
|
|
|
|
spdlog::info("VideoPipeline Stopped.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::vector<DetectionResult> VideoPipeline::mockInference(const cv::Mat& frame) {
|
|
|
|
|
std::vector<DetectionResult> results;
|
|
|
|
|
static int dummyX = 100;
|
|
|
|
|
static int direction = 5;
|
|
|
|
|
|
|
|
|
|
// 简单的移动逻辑,模拟每帧的变化
|
|
|
|
|
dummyX += direction;
|
|
|
|
|
if (dummyX > frame.cols - 200 || dummyX < 0)
|
|
|
|
|
direction *= -1;
|
|
|
|
|
|
|
|
|
|
DetectionResult res;
|
|
|
|
|
res.x = dummyX;
|
|
|
|
|
res.y = 200;
|
|
|
|
|
res.width = 150;
|
|
|
|
|
res.height = 300;
|
2026-01-04 14:52:14 +08:00
|
|
|
res.label = "TEST_CLIP";
|
2025-12-31 16:30:10 +08:00
|
|
|
res.confidence = 0.95f;
|
|
|
|
|
|
|
|
|
|
results.push_back(res);
|
|
|
|
|
return results;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void VideoPipeline::drawOverlay(cv::Mat& frame, const std::vector<DetectionResult>& results) {
|
|
|
|
|
for (const auto& res : results) {
|
|
|
|
|
cv::rectangle(frame, cv::Rect(res.x, res.y, res.width, res.height), cv::Scalar(0, 255, 0),
|
|
|
|
|
2);
|
|
|
|
|
std::string text = res.label + " " + std::to_string(res.confidence).substr(0, 4);
|
|
|
|
|
cv::putText(frame, text, cv::Point(res.x, res.y - 5), cv::FONT_HERSHEY_SIMPLEX, 0.6,
|
|
|
|
|
cv::Scalar(0, 255, 0), 2);
|
|
|
|
|
}
|
|
|
|
|
cv::putText(frame, "RK3588 H.264 @ 20FPS", cv::Point(20, 50), cv::FONT_HERSHEY_SIMPLEX, 1.0,
|
|
|
|
|
cv::Scalar(0, 0, 255), 2);
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-04 14:52:14 +08:00
|
|
|
void VideoPipeline::processLoop(std::string inputUrl, std::string outputUrl, bool isFileSource) {
|
2025-12-31 16:30:10 +08:00
|
|
|
cv::VideoCapture cap;
|
2026-01-04 14:52:14 +08:00
|
|
|
cap.open(inputUrl); // 文件路径也是通过 open 打开
|
2025-12-31 16:30:10 +08:00
|
|
|
|
|
|
|
|
if (!cap.isOpened()) {
|
2026-01-04 14:52:14 +08:00
|
|
|
spdlog::error("Failed to open input: {}", inputUrl);
|
2025-12-31 16:30:10 +08:00
|
|
|
running_ = false;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const double TARGET_FPS = 20.0;
|
2026-01-04 14:52:14 +08:00
|
|
|
// 计算每帧的目标耗时 (毫秒) -> 1000 / 20 = 50ms
|
|
|
|
|
const double FRAME_DURATION_MS = 1000.0 / TARGET_FPS;
|
2025-12-31 16:30:10 +08:00
|
|
|
|
|
|
|
|
int width = cap.get(cv::CAP_PROP_FRAME_WIDTH);
|
|
|
|
|
int height = cap.get(cv::CAP_PROP_FRAME_HEIGHT);
|
|
|
|
|
|
2026-01-04 14:52:14 +08:00
|
|
|
spdlog::info("Source: {}x{} | Mode: {}", width, height,
|
|
|
|
|
isFileSource ? "FILE LOOP" : "LIVE STREAM");
|
2025-12-31 16:30:10 +08:00
|
|
|
|
|
|
|
|
std::stringstream pipeline;
|
|
|
|
|
pipeline << "appsrc ! "
|
|
|
|
|
<< "videoconvert ! "
|
|
|
|
|
<< "video/x-raw,format=NV12,width=" << width << ",height=" << height
|
|
|
|
|
<< ",framerate=20/1 ! "
|
|
|
|
|
<< "mpph264enc ! "
|
|
|
|
|
<< "h264parse ! "
|
2026-01-04 14:52:14 +08:00
|
|
|
<< "rtspclientsink location=" << outputUrl << " protocols=tcp";
|
2025-12-31 16:30:10 +08:00
|
|
|
|
|
|
|
|
cv::VideoWriter writer;
|
|
|
|
|
writer.open(pipeline.str(), cv::CAP_GSTREAMER, 0, TARGET_FPS, cv::Size(width, height), true);
|
|
|
|
|
|
|
|
|
|
if (!writer.isOpened()) {
|
2026-01-04 14:52:14 +08:00
|
|
|
spdlog::error("Failed to initialize VideoWriter.");
|
2025-12-31 16:30:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cv::Mat frame;
|
|
|
|
|
|
|
|
|
|
while (running_) {
|
2026-01-04 14:52:14 +08:00
|
|
|
// 记录循环开始时间
|
|
|
|
|
auto loop_start = std::chrono::steady_clock::now();
|
2025-12-31 16:30:10 +08:00
|
|
|
|
|
|
|
|
if (!cap.read(frame)) {
|
2026-01-04 14:52:14 +08:00
|
|
|
if (isFileSource) {
|
|
|
|
|
spdlog::info("End of file reached, looping...");
|
|
|
|
|
cap.set(cv::CAP_PROP_POS_FRAMES, 0);
|
|
|
|
|
continue;
|
|
|
|
|
} else {
|
|
|
|
|
spdlog::warn("Frame read failed. Reconnecting...");
|
|
|
|
|
std::this_thread::sleep_for(std::chrono::seconds(1));
|
|
|
|
|
cap.release();
|
|
|
|
|
cap.open(inputUrl);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2025-12-31 16:30:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (frame.empty())
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
// 1. 算法处理
|
|
|
|
|
auto results = mockInference(frame);
|
|
|
|
|
|
|
|
|
|
// 2. 绘制叠加
|
|
|
|
|
drawOverlay(frame, results);
|
|
|
|
|
|
2026-01-04 14:52:14 +08:00
|
|
|
// 3. 推流
|
2025-12-31 16:30:10 +08:00
|
|
|
if (writer.isOpened()) {
|
|
|
|
|
writer.write(frame);
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-04 14:52:14 +08:00
|
|
|
if (isFileSource) {
|
|
|
|
|
auto loop_end = std::chrono::steady_clock::now();
|
|
|
|
|
std::chrono::duration<double, std::milli> elapsed = loop_end - loop_start;
|
|
|
|
|
|
|
|
|
|
double elapsed_ms = elapsed.count();
|
|
|
|
|
double wait_ms = FRAME_DURATION_MS - elapsed_ms;
|
2025-12-31 16:30:10 +08:00
|
|
|
|
2026-01-04 14:52:14 +08:00
|
|
|
if (wait_ms > 0) {
|
|
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds((int)wait_ms));
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-12-31 16:30:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cap.release();
|
|
|
|
|
writer.release();
|
|
|
|
|
}
|