SpireCV/video_io/driver/sv_camera_G1.cpp

107 lines
2.5 KiB
C++

/*
* @Description:
* @Author: L LC @amov
* @Date: 2023-12-19 18:30:17
* @LastEditors: L LC @amov
* @LastEditTime: 2023-12-21 17:56:47
* @FilePath: /SpireCV/video_io/driver/sv_camera_G1.cpp
*/
#include "sv_camera_privately.h"
#include <fstream>
class sv_camera_G1 : public sv_p::CameraBase
{
private:
std::string _ip;
public:
bool setStream(const std::string &ip, uint16_t port);
sv::CameraType getType(void) { return sv::CameraType::G1; }
std::string getName(void) { return _ip; }
bool open(void);
sv_camera_G1(int timeOut);
~sv_camera_G1();
static CameraBase *creat(int timeOut)
{
return new sv_camera_G1(timeOut);
}
};
sv_camera_G1::sv_camera_G1(int timeOut) : sv_p::CameraBase(timeOut)
{
}
bool sv_camera_G1::setStream(const std::string &ip, uint16_t port)
{
bool ret = false;
if (!this->cap.isOpened())
{
this->_ip = ip;
ret = true;
}
return ret;
}
const static uint32_t imageHList[3] = {1520, 1080, 720};
const static uint32_t imageWList[3] = {2704, 1920, 1280};
// 无论如何都用gst打开 因此安装的时候必须安装gst
bool sv_camera_G1::open(void)
{
bool ret = false;
std::ostringstream pipeline;
pipeline << "rtspsrc location = rtsp://" << this->_ip << ":554/H264?W=";
// 判断尺寸是否合法 不合法则找最高画质进行缩放
uint8_t i;
for (i = 0; i < 3; i++)
{
if (imageHList[i] <= this->getH())
{
if (imageWList[i] <= this->getW())
{
break;
}
}
}
// 在范围内
if (i < 3)
{
pipeline << imageWList[i] << "&H=" << imageHList[i];
}
else
{
pipeline << imageWList[2] << "&H=" << imageHList[2];
}
pipeline << "&FPS=" << this->getExpectFps() << "&BR=4000000 latency=100 "
<< "! application/x-rtp,media=video ! rtph264depay ! parsebin ! ";
#ifdef PLATFORM_JETSON
pipeline << "nvv4l2decoder enable-max-performancegst=1 \
! nvvidconv ! video/x-raw,format=(string)BGRx"
<< ",width=(int)" << this->getW()
<< ",height=(int)" << this->getH();
#else
pipeline << "avdec_h264 ! videoscale ! video/x-raw"
<< ",width=(int)" << this->getW()
<< ",height=(int)" << this->getH();
#endif
pipeline << " ! videoconvert ! video/x-raw,format=(string)BGR ! appsink sync=false";
if (this->cap.open(pipeline.str(), cv::CAP_GSTREAMER))
{
// 开启读取线程
std::thread readLoop(&CameraBase::readThread, this);
this->readThreadHandle = readLoop.native_handle();
readLoop.detach();
ret = true;
}
return ret;
}