HxNvr/HxIVA/HxDevice.cpp
2024-02-01 18:28:27 +08:00

147 lines
3.8 KiB
C++

#include "HxDevice.h"
HxDevice::HxDevice(int millisecond)
: HxThread{ millisecond }
{
type = -1;
frame_id = 0;
address = "";
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));
}
void HxDevice::startup(int type, QString url, QString file)
{
this->type = type;
if(url.isEmpty())
{
this->address = file;
this->connection_type = File;
}
else
{
this->address = url;
this->connection_type = Rtsp;
}
/* 通道被启用的情况下开始线程 */
if (!address.isEmpty())
{
HxTask::run(this, &HxDevice::frame_read_process, 5000, "_frame_read_process");
this->start();
}
}
void HxDevice::frame_read_process(void)
{
cv::VideoCapture capture;
if (!capture.isOpened())
{
if(connection_type == Rtsp)
{
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);
}
else
{
capture = cv::VideoCapture(address.toUtf8().data());
}
if(capture.isOpened())
{
HxTrace::debug_write_line("videolivestream", QString("type=%1, video open success, fps=%2").arg(type).arg(capture.get(CV_CAP_PROP_FPS)));
}
}
cv::Mat mat;
while(capture.read(mat))
{
mutex.lock();
frames.enqueue(mat);
mutex.unlock();
msleep(10);
}
/* 关闭,释放 */
capture.release();
HxTrace::debug_write_line("videolivestream", QString("type=%1, video close").arg(type));
}
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;
}
void HxDevice::action()
{
if (frames.isEmpty())
return;
mutex.lock();
auto frame = frames.dequeue();
mutex.unlock();
if (frame.data != nullptr)
{
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++;
}
}
}
void HxDevice::continue_with()
{
}