HxNvr/HxIVA/HxDevice.cpp

185 lines
5.0 KiB
C++
Raw Normal View History

2024-01-30 18:59:29 +08:00
#include "HxDevice.h"
2024-02-03 18:39:36 +08:00
HxDevice::HxDevice()
2024-01-30 18:59:29 +08:00
{
type = -1;
2024-02-01 18:28:27 +08:00
frame_id = 0;
address = "";
2024-01-30 18:59:29 +08:00
bgr_frame_buffer.u32Width = 1280;
bgr_frame_buffer.u32Height = 720;
bgr_frame_buffer.pu8VirAddr = (unsigned char*)calloc(bgr_frame_buffer.u32Width * bgr_frame_buffer.u32Height * 3, sizeof(unsigned char));
detect_frame_buffer.u32Width = 1280;
detect_frame_buffer.u32Height = 720;
detect_frame_buffer.pu8VirAddr = (unsigned char*)calloc(detect_frame_buffer.u32Width * detect_frame_buffer.u32Height * 3, sizeof(unsigned char));
}
2024-02-01 18:28:27 +08:00
void HxDevice::startup(int type, QString url, QString file)
2024-01-30 18:59:29 +08:00
{
this->type = type;
2024-02-01 18:28:27 +08:00
if(url.isEmpty())
{
this->address = file;
this->connection_type = File;
}
else
{
this->address = url;
this->connection_type = Rtsp;
}
2024-01-30 18:59:29 +08:00
/* 通道被启用的情况下开始线程 */
2024-02-01 18:28:27 +08:00
if (!address.isEmpty())
2024-01-30 18:59:29 +08:00
{
HxTask::run(this, &HxDevice::frame_read_process, 5000, "_frame_read_process");
2024-02-03 18:39:36 +08:00
HxTask::run(this, &HxDevice::frame_decode_process, 5, "_frame_decode_process");
2024-02-04 18:27:06 +08:00
HxTask::run([=]{
if(stream_timestamp.secsTo(QDateTime::currentDateTime()) >= 5)
{
capture.release();
QThread::sleep(5);
stream_timestamp = QDateTime::currentDateTime();
}
HxTrace::debug_write_line("videolivestream", QString("type=%1, frames count=%2").arg(type).arg(frames.size()));
}, 1000, "_frame_test_process");
2024-01-30 18:59:29 +08:00
}
}
2024-02-04 18:27:06 +08:00
bool HxDevice::determine_alarm_detection_timestamp(int event_type)
2024-01-30 18:59:29 +08:00
{
2024-02-04 18:27:06 +08:00
auto timestamp = QDateTime::currentDateTime();
if (!alarm_detection_timestamps.contains(event_type))
{
alarm_detection_timestamps.insert(event_type, timestamp);
2024-01-30 18:59:29 +08:00
2024-02-04 18:27:06 +08:00
return true;
}
if(alarm_detection_timestamps[event_type].secsTo(timestamp) > (warm_param_config.nAbnormalWarnIntervalFrameCount[event_type] / 25))
{
/* 更新时间 */
alarm_detection_timestamps[event_type] = timestamp;
return true;
}
return false;
}
void HxDevice::frame_read_process(void)
{
2024-02-01 18:28:27 +08:00
if (!capture.isOpened())
2024-01-30 18:59:29 +08:00
{
2024-02-01 18:28:27 +08:00
if(connection_type == Rtsp)
2024-01-30 18:59:29 +08:00
{
2024-02-01 18:28:27 +08:00
QString rtspurl = QString("rtspsrc location=%1 latency=0 ! rtph264depay ! h264parse ! mppvideodec ! videoconvert ! video/x-raw,format=(string)BGR ! appsink sync=false").arg(address);
capture = cv::VideoCapture(rtspurl.toUtf8().data(), cv::CAP_GSTREAMER);
2024-01-30 18:59:29 +08:00
}
2024-02-01 18:28:27 +08:00
else
2024-01-30 18:59:29 +08:00
{
2024-02-01 18:28:27 +08:00
capture = cv::VideoCapture(address.toUtf8().data());
2024-01-30 18:59:29 +08:00
}
2024-02-01 18:28:27 +08:00
if(capture.isOpened())
2024-01-30 18:59:29 +08:00
{
2024-02-01 18:28:27 +08:00
HxTrace::debug_write_line("videolivestream", QString("type=%1, video open success, fps=%2").arg(type).arg(capture.get(CV_CAP_PROP_FPS)));
2024-01-30 18:59:29 +08:00
}
}
2024-02-04 18:27:06 +08:00
2024-02-01 18:28:27 +08:00
cv::Mat mat;
2024-02-04 18:27:06 +08:00
while(true)
2024-01-30 18:59:29 +08:00
{
2024-02-04 18:27:06 +08:00
if(!capture.isOpened() || !capture.read(mat))
break;
2024-02-01 18:28:27 +08:00
mutex.lock();
frames.enqueue(mat);
mutex.unlock();
2024-02-04 18:27:06 +08:00
HxTrace::debug_write_line("videolivestream", QString("type=%1, read one frame").arg(type));
2024-02-03 18:39:36 +08:00
QThread::msleep(10);
2024-02-04 18:27:06 +08:00
stream_timestamp = QDateTime::currentDateTime();
2024-01-30 18:59:29 +08:00
}
2024-02-01 18:28:27 +08:00
/* 关闭,释放 */
capture.release();
HxTrace::debug_write_line("videolivestream", QString("type=%1, video close").arg(type));
2024-01-30 18:59:29 +08:00
}
2024-02-03 18:39:36 +08:00
void HxDevice::frame_decode_process(void)
{
if (frames.isEmpty())
return;
mutex.lock();
auto frame = frames.dequeue();
mutex.unlock();
if (frame.data != nullptr)
{
2024-02-04 18:27:06 +08:00
HxTrace::debug_write_line("device", QString("get frame"));
2024-02-03 18:39:36 +08:00
if (car_info.fVelocity >= 10)
{
bgr_frame_buffer.nFrameId = frame_id; // 帧号
bgr_frame_buffer.u64PTS = QDateTime::currentMSecsSinceEpoch(); // 时间戳(毫秒)
memcpy(bgr_frame_buffer.pu8VirAddr, frame.data, 1280 * 720 * 3);
mv_convert_images(&bgr_frame_buffer, &detect_frame_buffer); // bgr->nv16
#if USE_ALGORITHM
MvObjectEventDetect(type, &detect_frame_buffer, &car_info);
HxTrace::debug_write_line("device", QString("send frame_id=%1").arg(frame_id + 1));
#endif
frame_id++;
}
}
}
2024-01-30 18:59:29 +08:00
int HxDevice::mv_convert_images(VideoFrameDataInfo *src_buffer, VideoFrameDataInfo *dst_buffer)
{
Q_UNUSED(src_buffer);
Q_UNUSED(dst_buffer);
#if USE_ALGORITHM
rga_buffer_t src;
rga_buffer_t dst;
src = wrapbuffer_virtualaddr(src_buffer->pu8VirAddr, src_buffer->u32Width, src_buffer->u32Height, RK_FORMAT_BGR_888);
dst = wrapbuffer_virtualaddr(dst_buffer->pu8VirAddr, dst_buffer->u32Width, dst_buffer->u32Height, RK_FORMAT_YCbCr_420_SP);
if (src.width == 0 || dst.width == 0)
{
return -1;
}
src.format = RK_FORMAT_BGR_888;
dst.format = RK_FORMAT_YCbCr_422_SP;
IM_STATUS STATUS;
STATUS = imcvtcolor(src, dst, src.format, dst.format);
if (STATUS != IM_STATUS_SUCCESS)
{
qDebug("imcvtcolor error\n");
return -1;
}
dst_buffer->u64PTS = src_buffer->u64PTS;
dst_buffer->nFrameId = src_buffer->nFrameId;
#endif
return 0;
}