From c402412da9e89c4927ac12b82ca2d8b7a4e0296b Mon Sep 17 00:00:00 2001
From: zhuanglj <545382438@qq.com>
Date: 星期二, 29 四月 2025 11:05:54 +0800
Subject: [PATCH] 添加推流分辨率参数

---
 PreProcessFn.cpp |  431 ++++++++++++++++++++++++++++++++++-------------------
 1 files changed, 272 insertions(+), 159 deletions(-)

diff --git a/PreProcessFn.cpp b/PreProcessFn.cpp
index 86c2dce..585fbd0 100644
--- a/PreProcessFn.cpp
+++ b/PreProcessFn.cpp
@@ -9,7 +9,7 @@
 #include <mysql/mysql.h> 
 #include <future>
 #include "iostream"
-
+#include <hiredis.h>
 #include <string>
 #include <locale>
 #include <codecvt>
@@ -255,7 +255,7 @@
 	//cout << current_time << endl;
 	// 濡傛灉褰曞埗鏃堕棿瓒呰繃 10 绉掞紝鍒欏仠姝㈠綍鍒�
 	if (seconds >= _recordtime) {
-		cout << "宸插綍鍒� 10 绉掕棰戯紝鍋滄褰曞埗" << endl;
+		std::cout << "宸插綍鍒� 10 绉掕棰戯紝鍋滄褰曞埗" << endl;
 		video_writer.release();
 	}
 	else
@@ -284,7 +284,7 @@
 		if (mysql_query(mysql, query.c_str()))
 		{
 			string err_string = mysql_error(mysql);
-			cout << err_string << endl;
+			std::cout << err_string << endl;
 		}		
 	}
 }
@@ -321,7 +321,7 @@
 		if (mysql_query(mysql, query.c_str()))
 		{
 			string err_string = mysql_error(mysql);
-			cout << err_string << endl;
+			std::cout << err_string << endl;
 		}
 	}
 }
@@ -330,6 +330,38 @@
 size_t WriteCallback(void* contents, size_t size, size_t nmemb, void* userp) {
     ((std::string*)userp)->append((char*)contents, size * nmemb);
     return size * nmemb;
+}
+
+string join(const vector<string>& sequence, const string& separator)
+{
+	std::string result;
+	for (size_t i = 0; i < sequence.size(); ++i)
+		result += sequence[i] + ((i != sequence.size() - 1) ? separator : "");
+	return result;
+}
+
+/// <summary>
+/// 鍐欏叆鏁版嵁鑷硆edis妯″潡
+/// </summary>
+void fnWriteToRedis(redisContext*& context, string ipccode)
+{
+	redisReply* reply;
+
+	//妫�娴媟edis鏄惁杩炴帴锛屾姤璀﹀啓鍏edis
+	if (!context == NULL && !context->err) {
+
+		// 璁板綍鎶ヨ鐨勬椂鍊欙紝鍚屾椂鍐欏叆redis鏁版嵁搴�
+		std::string key = "camera::run::" + ipccode;
+		std::string value = "1";
+
+		//鍐欏叆redis
+		reply = (redisReply*)redisCommand(context, "SET %s %s", key.c_str(), value.c_str());
+		if (reply == NULL) {
+			printf("淇℃伅鍐欏叆redis澶辫触\n");
+			redisFree(context);
+		}
+		freeReplyObject(reply);
+	}
 }
 
 /// @brief 瑙嗛娴佹媺鍙栧鐞�
@@ -455,6 +487,44 @@
 	rtspStream.release();	
 }
 
+/// <summary>
+/// 杩炴帴骞舵牎楠� Redis 鏈嶅姟鍣ㄥ瘑鐮�
+/// </summary>
+/// <param name="server"></param>
+/// <param name="port"></param>
+/// <param name="password"></param>
+/// <param name="context"></param>
+/// <returns></returns>
+bool connectToRedis(const char* server, int port, const char* password, redisContext*& context) {
+	// 杩炴帴 Redis 鏈嶅姟鍣�
+	context = redisConnect(server, port);
+	if (context == nullptr || context->err) {
+		std::cerr << "杩炴帴redis鏈嶅姟绔け璐ワ紒" << std::endl;
+		return false;
+	}
+
+	//// 鏍¢獙瀵嗙爜
+	//redisReply* reply = (redisReply*)redisCommand(context, "AUTH %s", password);
+	//if (reply == NULL) {
+	//	std::cerr << "杩炴帴鏍¢獙澶辫触锛�" << std::endl;
+	//	redisFree(context);
+	//	return false;
+	//}
+	//else {
+	//	// 妫�鏌ュ洖澶嶄互纭畾鏄惁閴存潈鎴愬姛
+	//	if (reply->type == REDIS_REPLY_ERROR && strcmp(reply->str, "OK") != 0) {
+	//		std::cerr << "瀵嗙爜閿欒: " << reply->str << std::endl;
+	//		freeReplyObject(reply);
+	//		redisFree(context);
+	//		return false;
+	//	}
+	//	freeReplyObject(reply);
+	//}
+
+	return true;
+}
+
+//json杞崲瀛楃涓�
 std::string jsontostr(Json::Value& json)
 {
 	string return_str;
@@ -502,6 +572,18 @@
 		mysql_set_character_set(mysql, "utf8mb4");
 		//mysql_query(mysql, "SET NAMES GB2312");//瑙e喅涓枃涔辩爜闂       
 	}	
+
+
+	//鍒濆鍖杛edis閾炬帴
+	redisContext* context;	
+	if (!connectToRedis(redispath.c_str(), stoi(redisport), redispass.c_str(), context))
+	{
+		cout << "redis杩炴帴鍒濆鍖栧け璐ワ紒" << endl;
+	}
+	else
+	{
+		cout << "redis杩炴帴鍒濆鍖栨垚鍔燂紒" << endl;
+	}
 
 	std::string limitDM = "1";//鍫嗙叅闄愬畾闃堝�硷紝瓒呰繃闃堝�艰繘琛屾姤璀︼紝閰嶇疆鏂囦欢璧嬪�硷紝姣忎釜鎽勫儚澶撮兘涓嶄竴鏍�
 
@@ -677,132 +759,125 @@
 		std::string modelAnalysis ="";
 		color_result = modelNode["color_result"].asString();//棰滆壊鍙嶆鍊�
 
-		// std::string area = modelNode["threshold"].asString();//鍖哄煙涓�
-		// std::string area2 = modelNode.get("area2", "").asString();//鍖哄煙浜�
 		Json::Value rects = modelNode["point_rects"];////妯″瀷鍒嗘瀽绫诲瀷缂栫爜  
 		for (auto k = 0; k < rects.size(); ++k) 
+		{
+			Json::Value pointNode = rects[k];
+			lable_title=pointNode["title"].asString();
+			std::string point_type=pointNode["type"].asString();
+			Json::Value point_Arr= pointNode["points"];
+			for (auto x = 0; x < point_Arr.size(); ++x) 
 			{
-				Json::Value pointNode = rects[k];
-				lable_title=pointNode["title"].asString();
-				std::string point_type=pointNode["type"].asString();
-				Json::Value point_Arr= pointNode["points"];
-				for (auto x = 0; x < point_Arr.size(); ++x) 
+				int croodx = stoi(point_Arr[x]["x"].asString());
+				int croody = stoi(point_Arr[x]["y"].asString());
+				if(point_type=="detectarea")
 				{
-					int croodx = stoi(point_Arr[x]["x"].asString());
-					int croody = stoi(point_Arr[x]["y"].asString());
-					if(point_type=="detectarea")
-					{
-						detectarea.push_back(cv::Point(croodx, croody));
-					}
-					if (point_type=="flowarea")
-					{
-						flowarea.push_back(cv::Point(croodx, croody));
-					}
-					if (point_type=="flowrelate")
-					{
-						flowrelate.push_back(cv::Point(croodx, croody));
-					}
-					if (point_type=="leftroller")
-					{
-						leftarea.push_back(cv::Point(croodx, croody));
-					}
-					if (point_type=="rightroller")
-					{
-						rightarea.push_back(cv::Point(croodx, croody));
-					}
-					if (point_type=="beltarea")
-					{
-						beltarea.push_back(cv::Point(croodx, croody));
-					}
-					if (point_type=="workarea")
-					{
-						workarea.push_back(cv::Point(croodx, croody));
-					}
-						
+					detectarea.push_back(cv::Point(croodx, croody));
+				}
+				if (point_type=="flowarea")
+				{
+					flowarea.push_back(cv::Point(croodx, croody));
+				}
+				if (point_type=="flowrelate")
+				{
+					flowrelate.push_back(cv::Point(croodx, croody));
+				}
+				if (point_type=="leftroller")
+				{
+					leftarea.push_back(cv::Point(croodx, croody));
+				}
+				if (point_type=="rightroller")
+				{
+					rightarea.push_back(cv::Point(croodx, croody));
+				}
+				if (point_type=="beltarea")
+				{
+					beltarea.push_back(cv::Point(croodx, croody));
 				}	
-			}
+			}	
+		}
 
 		int* arr = new int[detectarea.size() * 2];
 		//鍔犺浇妯″瀷澶勭悊------
 		if(modelCode=="1") //鐨甫鐘舵��
 		{
-				if (!rBelt.initConfig(modelPath.c_str(),arr))
-				{
-					printf("鐨甫杩愯妯″瀷鍒濆鍖栧け璐ャ�俓n");
-				}
-				else
-				{
-					printf("鐨甫杩愯妯″瀷鍒濆鍖栨垚鍔熴�俓n");
-					rBelt.isLoad = true;
-				}
-				areaBelt =detectarea;
-				videoPathBelt = coalCode + ipccode + modelType + modelAnalysis;
-				wfBelt.setCoalCode(coalCode);
-				wfBelt.setCameraCode(ipccode);
-				wfBelt.setCameraName(ipcname);
-				wfBelt.setRtspUrl(ipcrtsppath);
-				wfBelt.setAnalyse(modelType);
-				//wfBelt.setAnalyseResult(modelAnalysis);
+			if (!rBelt.initConfig(modelPath.c_str(),arr))
+			{
+				printf("鐨甫杩愯妯″瀷鍒濆鍖栧け璐ャ�俓n");
+			}
+			else
+			{
+				printf("鐨甫杩愯妯″瀷鍒濆鍖栨垚鍔熴�俓n");
+				rBelt.isLoad = true;
+			}
+			areaBelt =detectarea;
+			videoPathBelt = coalCode + ipccode + modelType + modelAnalysis;
+			wfBelt.setCoalCode(coalCode);
+			wfBelt.setCameraCode(ipccode);
+			wfBelt.setCameraName(ipcname);
+			wfBelt.setRtspUrl(ipcrtsppath);
+			wfBelt.setAnalyse(modelType);
+			wfBelt.setAnalyseResult(modelAnalysis);
 		}
 		if(modelCode=="2") //鎻愬崌鏈鸿繍琛屾娴嬫ā鍨�
 		{
 			if (!rTSJ.initConfig(modelPath.c_str(), arr))
-				{
-					printf("鎻愬崌鏈烘ā鍨嬪垵濮嬪寲澶辫触銆俓n");
-				}
-				else
-				{
-					printf("鎻愬崌鏈烘ā鍨嬪垵濮嬪寲鎴愬姛銆俓n");
-					rTSJ.isLoad = true;
-				}
-				areaTSJ = detectarea;
-				videoPathTSJ = coalCode + ipccode + modelType;
-				wfTSJ.setCoalCode(coalCode);
-				wfTSJ.setCameraCode(ipccode);
-				wfTSJ.setCameraName(ipcname);
-				wfTSJ.setRtspUrl(ipcrtsppath);
-				wfTSJ.setAnalyse(modelType);
-				wfTSJ.setAnalyseResult(modelAnalysis);
-				break;
+			{
+				printf("鎻愬崌鏈烘ā鍨嬪垵濮嬪寲澶辫触銆俓n");
+			}
+			else
+			{
+				printf("鎻愬崌鏈烘ā鍨嬪垵濮嬪寲鎴愬姛銆俓n");
+				rTSJ.isLoad = true;
+			}
+			areaTSJ = detectarea;
+			videoPathTSJ = coalCode + ipccode + modelType;
+			wfTSJ.setCoalCode(coalCode);
+			wfTSJ.setCameraCode(ipccode);
+			wfTSJ.setCameraName(ipcname);
+			wfTSJ.setRtspUrl(ipcrtsppath);
+			wfTSJ.setAnalyse(modelType);
+			wfTSJ.setAnalyseResult(modelAnalysis);
+			break;
 		}
 		if(modelCode=="3") //浜哄憳鍖哄煙闂叆妯″瀷
 		{
 			if (!hatJC.initConfig(modelPath.c_str(), 0.5, 0.5))
-				{
-					printf("鐩爣妫�娴嬫ā鍨嬪垵濮嬪寲澶辫触\n");
-				}
-				else
-				{
-					printf("鐩爣妫�娴嬫ā鍨嬪垵濮嬪寲鎴愬姛銆俓n");
-					hatJC.isLoad = true;
-				}
-				areaPerson = detectarea;//鍗遍櫓鍖哄煙
-				workareaPerson = workarea;//宸ヤ綔鍖哄煙
-				videoPathPerson = coalCode + ipccode + modelType + modelAnalysis;
-				wfPerson.setCoalCode(coalCode);
-				wfPerson.setCameraCode(ipccode);
-				wfPerson.setCameraName(ipcname);
-				wfPerson.setRtspUrl(ipcrtsppath);
-				wfPerson.setAnalyse(modelType);
-				wfPerson.setAnalyseResult(modelAnalysis);
+			{
+				printf("鐩爣妫�娴嬫ā鍨嬪垵濮嬪寲澶辫触\n");
+			}
+			else
+			{
+				printf("鐩爣妫�娴嬫ā鍨嬪垵濮嬪寲鎴愬姛銆俓n");
+				hatJC.isLoad = true;
+			}
+			areaPerson = detectarea;//鍗遍櫓鍖哄煙
+			workareaPerson = workarea;//宸ヤ綔鍖哄煙
+			videoPathPerson = coalCode + ipccode + modelType + modelAnalysis;
+			wfPerson.setCoalCode(coalCode);
+			wfPerson.setCameraCode(ipccode);
+			wfPerson.setCameraName(ipcname);
+			wfPerson.setRtspUrl(ipcrtsppath);
+			wfPerson.setAnalyse(modelType);
+			wfPerson.setAnalyseResult(modelAnalysis);
 		}
 		if(modelCode=="4") //鎽勫儚澶撮伄鎸$畻娉�
 		{
 			if (!cover.initConfig()) {
-					printf("鎽勫儚澶撮伄鎸$畻娉曞垵濮嬪寲澶辫触\n");
-				}
-				else
-				{
-					std::cout << "鎽勫儚澶撮伄鎸$畻娉曞垵濮嬪寲鎴愬姛" << std::endl;
-					cover.isLoad = true;
-				}
-				videoPathCameraCover = coalCode + ipccode + modelType + modelAnalysis;
-				wfCameraCover.setCoalCode(coalCode);
-				wfCameraCover.setCameraCode(ipccode);
-				wfCameraCover.setCameraName(ipcname);
-				wfCameraCover.setRtspUrl(ipcrtsppath);
-				wfCameraCover.setAnalyse(modelType);
-				wfCameraCover.setAnalyseResult(modelAnalysis);
+				printf("鎽勫儚澶撮伄鎸$畻娉曞垵濮嬪寲澶辫触\n");
+			}
+			else
+			{
+				std::cout << "鎽勫儚澶撮伄鎸$畻娉曞垵濮嬪寲鎴愬姛" << std::endl;
+				cover.isLoad = true;
+			}
+			videoPathCameraCover = coalCode + ipccode + modelType + modelAnalysis;
+			wfCameraCover.setCoalCode(coalCode);
+			wfCameraCover.setCameraCode(ipccode);
+			wfCameraCover.setCameraName(ipcname);
+			wfCameraCover.setRtspUrl(ipcrtsppath);
+			wfCameraCover.setAnalyse(modelType);
+			wfCameraCover.setAnalyseResult(modelAnalysis);
 		}
 		if(modelCode=="5") //鎽勫儚澶寸Щ鍔ㄧ畻娉�
 		{
@@ -825,24 +900,25 @@
 		}
 		if(modelCode=="6") //鍫嗙叅妫�娴嬫ā鍨�
 		{
-			if (!dm.initConfig(modelPath.c_str(), arr)) {
-					printf("鍫嗙叅妯″瀷鍒濆鍖栧け璐n");
-				}
-				else
-				{
-					std::cout << "鍫嗙叅妯″瀷鍒濆鍖栨垚鍔�" << std::endl;
-					dm.isLoad = true;
-				}
-				videoPathDM = coalCode + ipccode + modelType + modelAnalysis;
-				areaDM = detectarea;
-				limitDM = modelLimit;//鍫嗙叅闄愬畾鐓ら噺鍗犳瘮
+			if (!dm.initConfig(modelPath.c_str(), arr)) 
+			{
+				printf("鍫嗙叅妯″瀷鍒濆鍖栧け璐n");
+			}
+			else
+			{
+				std::cout << "鍫嗙叅妯″瀷鍒濆鍖栨垚鍔�" << std::endl;
+				dm.isLoad = true;
+			}
+			videoPathDM = coalCode + ipccode + modelType + modelAnalysis;
+			areaDM = detectarea;
+			limitDM = modelLimit;//鍫嗙叅闄愬畾鐓ら噺鍗犳瘮
 
-				wfDM.setCoalCode(coalCode);
-				wfDM.setCameraCode(ipccode);
-				wfDM.setCameraName(ipcname);
-				wfDM.setRtspUrl(ipcrtsppath);
-				wfDM.setAnalyse(modelType);
-				wfDM.setAnalyseResult(modelAnalysis);
+			wfDM.setCoalCode(coalCode);
+			wfDM.setCameraCode(ipccode);
+			wfDM.setCameraName(ipcname);
+			wfDM.setRtspUrl(ipcrtsppath);
+			wfDM.setAnalyse(modelType);
+			wfDM.setAnalyseResult(modelAnalysis);
 		}
 		if(modelCode=="7") //鐨甫璺戝亸鍜屽紓鐗╂娴�
 		{
@@ -958,11 +1034,7 @@
 			wfFire.setAnalyse(modelType);
 			wfFire.setAnalyseResult(modelAnalysis);
 		}
-		if (modelCode == "12")//浜哄憳璺屽��
-		{
-
-		}
-	}	
+	}
 
 	std::queue<int> quTSJ;//璁板綍鎻愬崌鏈鸿繍鍔ㄧ殑鍒ゆ柇缁撴灉
 	std::queue<int> quBelt;//璁板綍鐨甫杩愬姩鍒ゆ柇缁撴灉
@@ -1710,7 +1782,9 @@
 				currentFrame = 0;
 			}
 
-			currentFrame++;			
+			currentFrame++;		
+			
+			fnWriteToRedis(context,ipccode);
 		}
 		catch (const std::exception& ex)
 		{			
@@ -1867,6 +1941,37 @@
 	//瀹氫箟鎺ュ彈甯�
 	Mat frame;
 
+	vector<string> arguments = {
+		"ffmpeg "
+		"-hwaccel","cuvid",
+		"-hwaccel_output_format","cuda",
+		"-y", "-an",			
+		"-f", "rawvideo",
+		"-vcodec", "rawvideo",
+		"-pix_fmt", "bgr24",
+		"-s",  "640x480",
+		"-r", "15",			
+		"-i", "-",	
+		"-pix_fmt", "yuv420p",
+		"-f", "flv",
+		"-max_delay", "1000",
+		"-flvflags", "no_duration_filesize",
+		"-c:v","h264_nvenc",
+		"-b:v", "3M",
+		"-g:v", "15",
+		"-bf", "0",
+		"-bufsize", "50000000",
+		"-rtbufsize", "50000000",	
+		"rtmp://192.168.1.8:1935/live/camera1" };
+
+	string ffmpeg_command = join(arguments, " ");
+
+	// 鎵撳紑FFmpeg杩涚▼
+	FILE* pipe = popen(ffmpeg_command.c_str(), "w");
+	if (!pipe) {
+		std::cerr << "鏃犳硶鍚姩FFmpeg" << std::endl;
+	}
+		
 	while (!asyncStop) {
 		//std::cout << ipccode + "褰撳墠绾跨▼Rabbitmq鏁版嵁鏁伴噺2222:" << queJC2.size() << std::endl;		
 		try
@@ -1885,20 +1990,9 @@
 			queJC2.pop();
 
 			if (frame.empty())//甯т负绌猴紝鍒欒垗寮�
-				continue;
+				continue;	
 
-			// FFmpeg鎺ㄦ祦鍛戒护
-			std::string ffmpeg_command = 
-				"ffmpeg -y -f rawvideo -pixel_format bgr24 -video_size 640x480 "
-        		"-framerate 30 -i - -c:v libx264 -pix_fmt yuv420p -f rtsp "
-       			"rtsp://192.168.1.188:8554/live/stream";
-
-			// 鎵撳紑FFmpeg杩涚▼
-			FILE* pipe = popen(ffmpeg_command.c_str(), "w");
-			if (!pipe) {
-				std::cerr << "鏃犳硶鍚姩FFmpeg" << std::endl;
-			}
-
+			
 			// 灏嗗抚鍐欏叆鍒癋Fmpeg绠¢亾涓�
 			fwrite(frame.data, 1, frame.total() * frame.elemSize(), pipe);
 
@@ -1926,11 +2020,42 @@
 /// @brief 鎺ㄦ祦鍒版祦濯掍綋鏈嶅姟鍣�
 /// @param queJC2 澶勭悊瀹岀殑瑙嗛娴佸抚闃熷垪
 /// @param ipccode 鎽勫儚鏈虹紪鍙�
-void PreProcessFn::fnPushVideoToUrl(queue<Mat>& queJC2,string toRtsp, string fps, string ipccode)
+void PreProcessFn::fnPushVideoToUrl(queue<Mat>& queJC2,string toRtsp, string fps, string video_size,string ipccode)
 {	
 	//瀹氫箟鎺ュ彈甯�
 	Mat frame;
 
+	vector<string> arguments = {
+		"ffmpeg "
+		"-hwaccel","cuvid",
+		"-hwaccel_output_format","cuda",
+		"-y", "-an",			
+		"-f", "rawvideo",
+		"-vcodec", "rawvideo",
+		"-pix_fmt", "bgr24",
+		"-s",  video_size,
+		"-r", fps,			
+		"-i", "-",	
+		"-pix_fmt", "yuv420p",
+		"-f", "flv",
+		"-max_delay", "1000",
+		"-flvflags", "no_duration_filesize",
+		"-c:v","h264_nvenc",
+		"-b:v", "3M",
+		"-g:v", "15",
+		"-bf", "0",
+		"-bufsize", "50000000",
+		"-rtbufsize", "50000000",	
+		toRtsp };
+
+	string ffmpeg_command = join(arguments, " ");
+
+	// 鎵撳紑FFmpeg杩涚▼
+	FILE* pipe = popen(ffmpeg_command.c_str(), "w");
+	if (!pipe) {
+		std::cerr << "鏃犳硶鍚姩FFmpeg" << std::endl;
+	}
+		
 	while (!asyncStop) {
 		//std::cout << ipccode + "褰撳墠绾跨▼Rabbitmq鏁版嵁鏁伴噺2222:" << queJC2.size() << std::endl;		
 		try
@@ -1949,20 +2074,8 @@
 			queJC2.pop();
 
 			if (frame.empty())//甯т负绌猴紝鍒欒垗寮�
-				continue;
-
-			// FFmpeg鎺ㄦ祦鍛戒护
-			std::string ffmpeg_command = 
-				"ffmpeg -y -f rawvideo -pixel_format bgr24 -video_size 640x480 "
-        		"-framerate 30 -i - -c:v libx264 -pix_fmt yuv420p -f rtsp "
-       			"rtsp://192.168.1.8:8554/live/stream";
-
-			// 鎵撳紑FFmpeg杩涚▼
-			FILE* pipe = popen(ffmpeg_command.c_str(), "w");
-			if (!pipe) {
-				std::cerr << "鏃犳硶鍚姩FFmpeg" << std::endl;
-			}
-
+				continue;	
+			
 			// 灏嗗抚鍐欏叆鍒癋Fmpeg绠¢亾涓�
 			fwrite(frame.data, 1, frame.total() * frame.elemSize(), pipe);
 

--
Gitblit v1.8.0