DvppDecoder.cpp 2.94 KB
#include "DvppDecoder.h"

void receiver_finish_cbk(const void* userPtr){
    if(userPtr != nullptr){
        DvppDecoder* self = (DvppDecoder*)userPtr;
        self->taskFinishing();
    }
}

DvppDecoder::DvppDecoder(){
    m_pktQueueptr = new CircularQueue<AVPacket *>();
}

DvppDecoder::~DvppDecoder(){
    delete m_pktQueueptr;
    m_pktQueueptr = nullptr;
}

bool DvppDecoder::init(FFDecConfig cfg){

    m_dec_name = cfg.dec_name;
    
    ReceiverConfig receiver_config;
    receiver_config.uri = cfg.uri.c_str();
    receiver_config.dec_name = cfg.dec_name;
    receiver_config.force_tcp = cfg.force_tcp;
    receiver_config.pktQueueptr = m_pktQueueptr;
    receiver_config.receiver_finished_cbk = receiver_finish_cbk;
    AVCodecContext* avctx = m_receiver.init_FFmpeg(receiver_config);
    if(avctx == nullptr){
        return false;
    }
    m_receiver.setFinishCbkArg(this);

    DvppDecConfig dec_cfg;
    if(avctx->codec_id == AV_CODEC_ID_H264){
        dec_cfg.codec_id = 0;
    }else if(avctx->codec_id == AV_CODEC_ID_HEVC){
        dec_cfg.codec_id = 1;
    }else {
        return false;
    }
    dec_cfg.dec_name = cfg.dec_name;
    dec_cfg.post_decoded_cbk = cfg.post_decoded_cbk;
    dec_cfg.dev_id = cfg.gpuid;
    dec_cfg.force_tcp = cfg.force_tcp;
    dec_cfg.skip_frame = cfg.skip_frame;
    dec_cfg.profile = avctx->profile;
    dec_cfg.pktQueueptr = m_pktQueueptr;
    dec_cfg.width = avctx->width;
    dec_cfg.height = avctx->height;
    bool bRet = m_decoder.init_vdpp(dec_cfg);
    if(!bRet){
        return false;
    }

    m_cfg = cfg;

    decode_finished_cbk = cfg.decode_finished_cbk;

    m_bFinished = false;

    return true;
}

bool DvppDecoder::isSurport(FFDecConfig& cfg){
    return true;
}

bool DvppDecoder::start(){
    m_receiver.start();
    m_decoder.start();
    return true;
}

void DvppDecoder::close(){
    m_receiver.close();
}

void DvppDecoder::setPostDecArg(const void* postDecArg){
    m_decoder.setPostDecArg(postDecArg);
}

void DvppDecoder::setFinishedDecArg(const void* finishedDecArg){
    m_finishedDecArg = finishedDecArg;
}

void DvppDecoder::pause(){
    m_receiver.pause();
}

void DvppDecoder::resume(){
    m_receiver.resume();
}

void DvppDecoder::setDecKeyframe(bool bKeyframe){
    m_receiver.setDecKeyframe(bKeyframe);
}

bool DvppDecoder::isRunning(){
    return m_receiver.isRunning();
}

bool DvppDecoder::isFinished(){
    return m_bFinished;
}

bool DvppDecoder::isPausing(){
    return m_receiver.isPausing();
}

bool DvppDecoder::getResolution(int &width, int &height){
    return m_receiver.getResolution(width, height);
}

float DvppDecoder::fps(){
    return m_receiver.fps();
}

FFImgInfo* DvppDecoder::snapshot(){
	// TODO
	return nullptr;
}

int DvppDecoder::getCachedQueueLength(){
    return 0;
}

void DvppDecoder::taskFinishing(){
    // receiver 中读取线程结束时执行
    m_decoder.close();
    decode_finished_cbk(m_finishedDecArg);

    m_bFinished = true;

    LOG_INFO("[{}]- task finished.", m_dec_name);
}