2024-08-12 03:34:13 +00:00
|
|
|
#include "CameraEngine.h"
#include "myutils.h"
using namespace ai_matrix;
namespace
{
const int LOW_THRESHOLD = 128;
const int MAX_THRESHOLD = 4096;
const uint16_t DELAY_TIME = 20000;
}
CameraEngine::CameraEngine() {}
CameraEngine::~CameraEngine() {}
APP_ERROR CameraEngine::Init()
{
bUseEngine_ = true;
bHwDecode_ = MyYaml::GetIns()->GetBoolValue("gc_hardware_decode");
dataSourceConfig_ = MyYaml::GetIns()->GetDataSourceConfigById(engineId_); //获取摄像机参数
if (MyYaml::GetIns()->GetStringValue("gc_data_source") != "camera" || !dataSourceConfig_.bUse)
{
bUseEngine_ = false;
LogWarn << "engineId_:" << engineId_ << " not use engine";
return APP_ERR_OK;
}
strPort0_ = engineName_ + "_" + std::to_string(engineId_) + "_0";
strPort1_ = engineName_ + "_" + std::to_string(engineId_) + "_1";
nDelayTime = MyYaml::GetIns()->GetIntValue("gc_load_delay");
LogInfo << "engineId_:" << engineId_ << " CameraEngine Init ok";
return APP_ERR_OK;
}
APP_ERROR CameraEngine::DeInit()
{
if (!bUseEngine_)
{
LogWarn << "engineId_:" << engineId_ << " not use engine";
return APP_ERR_OK;
}
ResetCamera();
LogInfo << "engineId_:" << engineId_ << " CameraEngine DeInit ok";
return APP_ERR_OK;
}
void CameraEngine::ResetCamera()
{
if (pFormatCtx_ != nullptr)
{
// clear th cache of the queue
avformat_close_input(&pFormatCtx_);
pFormatCtx_ = nullptr;
}
}
APP_ERROR CameraEngine::ConnectCamera()
{
pFormatCtx_ = CreateFormatContext(); // create context
if (pFormatCtx_ == nullptr)
{
LogError << "engineId_:" << engineId_ << " pFormatCtx_ null!";
return APP_ERR_COMM_FAILURE;
}
//0-代表输入
av_dump_format(pFormatCtx_, 0, dataSourceConfig_.strUrl.c_str(), 0);
// get stream infomation
int iRet = APP_ERR_OK;
iRet = GetStreamInfo();
if (iRet != APP_ERR_OK)
{
LogError << "engineId_:" << engineId_ << " Stream Info Check failed, iRet = " << iRet;
return APP_ERR_COMM_FAILURE;
}
return APP_ERR_OK;
}
APP_ERROR CameraEngine::GetStreamInfo()
{
if (pFormatCtx_ != nullptr)
{
iVideoStream_ = -1;
iAudioStream_ = -1;
//frameInfo_.iFrameId = 0; //帧号从0开始
for (unsigned int i = 0; i < pFormatCtx_->nb_streams; i++)
{
AVStream *inStream = pFormatCtx_->streams[i];
if (inStream->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
{
iVideoStream_ = i;
frameInfo_.iHeight = inStream->codecpar->height;
frameInfo_.iWidth = inStream->codecpar->width;
//获取帧率,帧率的打印都在流中的两个成员.且应取平均帧率为先,为{x,0}或者{0,1}则取实时帧率
if (inStream->avg_frame_rate.den == 0 || (inStream->avg_frame_rate.num == 0 && inStream->avg_frame_rate.den == 1))
{
frameInfo_.iRate = inStream->r_frame_rate.num / inStream->r_frame_rate.den;
}
else
{
frameInfo_.iRate = inStream->avg_frame_rate.num / inStream->avg_frame_rate.den;
}
LogDebug << "engineId_:" << engineId_ << " width:" << frameInfo_.iWidth << " height:" << frameInfo_.iHeight
<< " rate:" << frameInfo_.iRate << " iVideoStream_:" << iVideoStream_;
}
else if (inStream->codecpar->codec_type == AVMEDIA_TYPE_AUDIO)
{
iAudioStream_ = i;
LogDebug << "engineId_:" << engineId_ << " iAudioStream_:" << iAudioStream_;
}
}
if (iVideoStream_ == -1)
{
LogError << "engineId_:" << engineId_ << " Didn't find a video stream!";
return APP_ERR_COMM_FAILURE;
}
if (frameInfo_.iHeight < LOW_THRESHOLD || frameInfo_.iWidth < LOW_THRESHOLD ||
frameInfo_.iHeight > MAX_THRESHOLD || frameInfo_.iWidth > MAX_TH
|