admin
2025-04-27 e9a9541f70351b2cf7a33b7862dd6481c0730df0
PreProcessFn.cpp
@@ -9,7 +9,7 @@
#include <mysql/mysql.h> 
#include <future>
#include "iostream"
#include <hiredis.h>
#include <string>
#include <locale>
#include <codecvt>
@@ -332,6 +332,38 @@
    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>
/// 写入数据至redis模块
/// </summary>
void fnWriteToRedis(redisContext*& context, string ipccode)
{
   redisReply* reply;
   //检测redis是否连接,报警写入redis
   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 视频流拉取处理
/// @param _rtspUrl 视频流源地址
/// @param queJC 未处理的视频流帧队列
@@ -455,6 +487,43 @@
   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;
}
std::string jsontostr(Json::Value& json)
{
   string return_str;
@@ -502,6 +571,18 @@
      mysql_set_character_set(mysql, "utf8mb4");
      //mysql_query(mysql, "SET NAMES GB2312");//解决中文乱码问题       
   }   
   //初始化redis链接
   redisContext* context;
   if (!connectToRedis(redispath.c_str(), stoi(redisport), redispass.c_str(), context))
   {
      cout << "redis连接初始化失败!" << endl;
   }
   else
   {
      cout << "redis连接初始化成功!" << endl;
   }
   std::string limitDM = "1";//堆煤限定阈值,超过阈值进行报警,配置文件赋值,每个摄像头都不一样
@@ -1710,7 +1791,9 @@
            currentFrame = 0;
         }
         currentFrame++;
         currentFrame++;
         fnWriteToRedis(context,ipccode);
      }
      catch (const std::exception& ex)
      {         
@@ -1867,6 +1950,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 +1999,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;
         }
         // 将帧写入到FFmpeg管道中
         fwrite(frame.data, 1, frame.total() * frame.elemSize(), pipe);
@@ -1931,6 +2034,37 @@
   //定义接受帧
   Mat frame;
   vector<string> arguments = {
      "ffmpeg "
      "-hwaccel","cuvid",
      "-hwaccel_output_format","cuda",
      "-y", "-an",
      "-f", "rawvideo",
      "-vcodec", "rawvideo",
      "-pix_fmt", "bgr24",
      "-s",  "1280x720",
      "-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 +2083,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;
         // 将帧写入到FFmpeg管道中
         fwrite(frame.data, 1, frame.total() * frame.elemSize(), pipe);