147 lines
3.9 KiB
C++
147 lines
3.9 KiB
C++
/*
|
|
* @Date: 2024-09-04 14:52:16
|
|
* @LastEditors: Jacky
|
|
* @LastEditTime: 2024-09-13 17:21:29
|
|
* @FilePath: /GeneralTracker/Universal/CModuleVenc.cpp
|
|
*/
|
|
|
|
#include "CModuleVenc.h"
|
|
#include "IStreamConfig.h"
|
|
#include "IStreamTs.h"
|
|
#include "IStreamRtsp.h"
|
|
|
|
int CModuleVenc::create(const char* name){
|
|
m_pcName = std::string(name);
|
|
m_pSelfQueue.queueInit(m_pcName, QUEUE_SIZE);
|
|
memset(&_encode_param, 0, sizeof(encodeAttr_t));
|
|
return 0;
|
|
}
|
|
|
|
int CModuleVenc::destroy(){
|
|
stop();
|
|
return 0;
|
|
}
|
|
|
|
int CModuleVenc::set_data(const char* data, void* value){
|
|
return 0;
|
|
}
|
|
|
|
char* CModuleVenc::get_data(){
|
|
return nullptr;
|
|
}
|
|
|
|
int CModuleVenc::start(const char* data){
|
|
if(_vi_thread){
|
|
return -1;
|
|
}
|
|
|
|
IStreamConfig config("./config/stream_out.json");
|
|
config.parse(m_pcName, [this](IStreamConfig::stream_config_t stream) -> int {
|
|
if(stream.enable){
|
|
CStreamOut *out = nullptr;
|
|
switch (stream.type)
|
|
{
|
|
case IStreamConfig::STREAM_RTSP:
|
|
out = new IStreamRtsp(stream);
|
|
break;
|
|
case IStreamConfig::STREAM_TS:
|
|
out = new IStreamTs(stream);
|
|
break;
|
|
case IStreamConfig::STREAM_WEBSOCKET:
|
|
// out = new IStreamWebsocket(stream);
|
|
break;
|
|
case IStreamConfig::STREAM_UDP:
|
|
// out = new IStreamUdp(stream);
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
if(out){
|
|
out->start();
|
|
this->_stream_out_list.push_back(out);
|
|
}
|
|
}
|
|
|
|
//init venc param.
|
|
_encode_param.srcWidth = stream.width;
|
|
_encode_param.srcHeight = stream.height;
|
|
_encode_param.gop = stream.gop;
|
|
_encode_param.fps = stream.fps;
|
|
_encode_param.targetBitrate = stream.bitrate;
|
|
|
|
printf("start venc -- %d:%d, %f\n", _encode_param.srcWidth, _encode_param.srcHeight, _encode_param.fps);
|
|
return 0;
|
|
});
|
|
|
|
_thread_flag = true;
|
|
_vi_thread = new std::thread(std::bind(&CModuleVenc::moduleThread, std::ref(*this)));
|
|
return 0;
|
|
}
|
|
int CModuleVenc::stop(){
|
|
if(_vi_thread){
|
|
_thread_flag = false;
|
|
_vi_thread->join();
|
|
delete _vi_thread;
|
|
_vi_thread = nullptr;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
void CModuleVenc::push_data(DataProcessBase *src, pool::IMemoryBlock* block){
|
|
m_pSelfQueue.sendFrame(block);
|
|
}
|
|
|
|
int CModuleVenc::frameCallback(const int chn, char *ptr, int size, encodeData_t *param, void *userdata){
|
|
CModuleVenc *p = (CModuleVenc *)userdata;
|
|
|
|
if(access("./dump_venc", F_OK) == 0){
|
|
if(p->fd.is_open()){
|
|
p->fd.write(ptr, size);
|
|
}else{
|
|
p->fd.open("./tmp.h264", std::ios::out | std::ios::binary);
|
|
}
|
|
}else{
|
|
if(p->fd.is_open()){
|
|
p->fd.close();
|
|
}
|
|
}
|
|
|
|
for(auto iter = p->_stream_out_list.begin(); iter != p->_stream_out_list.end(); iter ++){
|
|
CStreamOut *out = (CStreamOut *)(*iter);
|
|
out->sendData((unsigned char *)ptr, size);
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
int CModuleVenc::moduleThread(){
|
|
int timeout = 500;
|
|
encodeAttr_t param;
|
|
memset(¶m, 0x0, sizeof(encodeAttr_t));
|
|
param.srcWidth = _encode_param.srcWidth;
|
|
param.srcHeight = _encode_param.srcHeight;
|
|
param.fps = _encode_param.fps;
|
|
param.inputFormat = GD_PIXEL_FORMAT_NV12;
|
|
param.gop = 50;
|
|
param.outputFormat = H264;
|
|
encodeOpen(_chn, ¶m, frameCallback, this);
|
|
encodeStart(_chn);
|
|
|
|
_fps = new Fps(m_pcName);
|
|
|
|
while(_thread_flag){
|
|
pool::IMemoryBlock* mb = m_pSelfQueue.getFrame(timeout);
|
|
if(mb){
|
|
AdditionalImp* imp = mb->data();
|
|
GD_VIDEO_FRAME_S *frame = mb->yuv();
|
|
//发送编码
|
|
encodeSendFrame(_chn, frame);
|
|
push_input_pipe(mb);
|
|
m_pSelfQueue.releaseFrame(mb);
|
|
|
|
_fps->fps();
|
|
}
|
|
}
|
|
encodeClose(_chn);
|
|
return 0;
|
|
}
|