185 lines
5.0 KiB
C++
185 lines
5.0 KiB
C++
#include "HxDevice.h"
|
|
|
|
HxDevice::HxDevice()
|
|
{
|
|
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");
|
|
|
|
HxTask::run(this, &HxDevice::frame_decode_process, 5, "_frame_decode_process");
|
|
|
|
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");
|
|
}
|
|
}
|
|
|
|
bool HxDevice::determine_alarm_detection_timestamp(int event_type)
|
|
{
|
|
auto timestamp = QDateTime::currentDateTime();
|
|
|
|
if (!alarm_detection_timestamps.contains(event_type))
|
|
{
|
|
alarm_detection_timestamps.insert(event_type, timestamp);
|
|
|
|
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)
|
|
{
|
|
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(true)
|
|
{
|
|
if(!capture.isOpened() || !capture.read(mat))
|
|
break;
|
|
|
|
mutex.lock();
|
|
frames.enqueue(mat);
|
|
mutex.unlock();
|
|
|
|
HxTrace::debug_write_line("videolivestream", QString("type=%1, read one frame").arg(type));
|
|
|
|
QThread::msleep(10);
|
|
|
|
stream_timestamp = QDateTime::currentDateTime();
|
|
}
|
|
|
|
/* 关闭,释放 */
|
|
capture.release();
|
|
|
|
HxTrace::debug_write_line("videolivestream", QString("type=%1, video close").arg(type));
|
|
}
|
|
|
|
void HxDevice::frame_decode_process(void)
|
|
{
|
|
if (frames.isEmpty())
|
|
return;
|
|
|
|
mutex.lock();
|
|
auto frame = frames.dequeue();
|
|
mutex.unlock();
|
|
|
|
if (frame.data != nullptr)
|
|
{
|
|
HxTrace::debug_write_line("device", QString("get frame"));
|
|
|
|
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++;
|
|
}
|
|
}
|
|
}
|
|
|
|
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;
|
|
}
|