chenshijun
2019-04-16 f946a62d3921e86b44ff8e2973138304b9cd53cd
QiaoJiaSystem/GB28181DecoderModel/FFmpegDecoderJPG.cpp
@@ -32,7 +32,7 @@
              dst.data, dst.linesize);
    sws_freeContext(convert_ctx);
    DBG("m.size is " << m.size());
//    DBG("m.size is " << m.size());
//    LOG_IF();
    return m;
@@ -45,7 +45,9 @@
}
BASICGB28181::FFmpegDecoderJPG::~FFmpegDecoderJPG() {
    //清空队列
    while (m_rtpQueue.count_queue()) {
        //#todo delete frameBuffInfo*
        m_rtpQueue.popNotWait();
    }
@@ -75,10 +77,9 @@
    }
#ifdef TestCode
        DBG(" m_rtpQueue.push before ");
        DBG(" m_rtpQueue.push befores ");
#endif
        m_rtpQueue.
        push(info);
        m_rtpQueue.push(info);
#ifdef TestCode
        DBG(" m_rtpQueue.push after ");
#endif
@@ -96,10 +97,11 @@
    do {
//        DBG(" m_rtpQueue.pop before ");
        //从缓存中获取buffinfo
        frameBuffInfo *buffinfo = fFmpegDecoderJPG->m_rtpQueue.pop();
//        DBG(" m_rtpQueue.pop after ");
        diff = len - buffinfo->buffLen;
//        printf("bufsize is :%ld,len is :%ld, datalen:%d \n", bufsize, len, buffinfo->buffLen);
        //帧长大于bufsize
        if (diff < 0) {
//            DBG("/帧长大于bufsize" << diff);
@@ -130,7 +132,7 @@
        delete[] buffinfo->buff;
        delete buffinfo;
    } while (diff > 0);
    //#todo 触发信号
//    DBG("emitSigal(\"read_dataOk\") begin");
//    gSignalLock.emitSigal("read_dataOk");
    fFmpegDecoderJPG->m_readData = true;
@@ -143,7 +145,8 @@
 * @param p_this
 */
void BASICGB28181::FFmpegDecoderJPG::BareFlowDecoderThd(FFmpegDecoderJPG *p_this) {
    DBG(p_this->m_camIdx << "  BareFlowDecoderThd ok ... gpuIdx is " << p_this->m_gpuIdx);
    DBG(p_this->m_camIdx <<  "  BareFlowDecoderThd ok ... gpuIdx is " << p_this->m_gpuIdx);
    DBG("p_thisADDR:" << p_this << "p_this->m_running" << p_this->m_running);
    while (!p_this->m_running) {
        p_this->m_running = true;
@@ -151,12 +154,14 @@
//    avformat_network_init();
        p_this->ic = avformat_alloc_context();
        //创建ffmpeg使用的内存空间
        p_this->iobuffer = (unsigned char *) av_malloc(p_this->m_buf_size);
        if (!p_this->iobuffer) {
            ERR("av_malloc: err======" << p_this->m_camIdx);
            p_this->m_running = false;
            continue;
        }
//     使用回调函数和内存块创建 AVIOContext
        p_this->avio = avio_alloc_context(p_this->iobuffer, p_this->m_buf_size, 0, p_this, p_this->read_data, NULL,
                                          NULL);
        if (!p_this->avio) {
@@ -193,6 +198,12 @@
                vi = i;
                break;
            }
        }
        if(vi == -1)//无视频包
        {
            ERR("no video packet!!");
            p_this->m_running = false;
            continue;
        }
        p_this->stream = p_this->ic->streams[vi];
        p_this->video_st = p_this->stream;
@@ -267,7 +278,11 @@
//        BASICGB28181::avframe_to_cvmat(frame).copyTo(p_this->m_image);
            p_this->m_image = std::move(BASICGB28181::avframe_to_cvmat(p_this->frame));
            if(p_this->m_image.empty()) {
                ERR("camID:" << p_this->m_camIdx << " frameW:" << p_this->frame->width);
                continue;
            }
//            将i帧保存为快照
            if (p_this->m_SnapshotNotSaveRet && (p_this->pkt.flags & AV_PKT_FLAG_KEY)) {
                try {
                    std::string strNewName = "./";
@@ -304,7 +319,7 @@
#ifdef TestCode
                DBG("emitSigal(\"DecoderImageOK\") begin");
#endif
            //触发信号
            //触发上层信号
            gSignalLock.emitSigal(p_this->m_camIdx + "DecoderImageOK");
//#ifdef TestCode
//        DBG("emitSigal(\"DecoderImageOK\") after");
@@ -349,6 +364,7 @@
        }
        m_camIdx = camIdx;
        DBG("BareFlowDecoderThd camIdx : " << camIdx);
//        启动解码线程
        std::thread t_BareFlowDecoder(BareFlowDecoderThd, this);
        t_BareFlowDecoder.detach();
    );
@@ -357,7 +373,7 @@
bool BASICGB28181::FFmpegDecoderJPG::stopThd() {
    TryCath(
        DBG(m_camIdx << "  FFmpegDecoderJPG stopThd ... " << m_camIdx);
        DBG("FFmpegDecoderJPG stopThd ... " << m_camIdx);
        m_running = false;
    );
    return true;
@@ -422,7 +438,7 @@
        if (!m_packetsVec.empty()) {
            auto iter = m_packetsVec.begin();
            while (iter->m_frameId < lastFrameId) {
                INFO("DropFrame: " << iter->m_frameId);
//                INFO("DropFrame: " << iter->m_frameId);
                delete iter->m_packet.data;
                iter = m_packetsVec.erase(iter);
            }
@@ -441,7 +457,7 @@
            delete iter->m_packet.data;
            iter = m_packetsVec.erase(iter);
            while (!(iter->m_packet.flags & AV_PKT_FLAG_KEY)) {
                INFO("DropFrame: " << iter->m_frameId);
//                INFO("DropFrame: " << iter->m_frameId);
                delete iter->m_packet.data;
                iter = m_packetsVec.erase(iter);
            }
@@ -452,7 +468,7 @@
bool BASICGB28181::FFmpegDecoderJPG::SaveVideo(std::string path, int64_t lastFrameId) {
    std::lock_guard<std::mutex> lock(g_mutex);
    INFO("SaveVideo: " << path << "m_packetsVec.size : " << m_packetsVec.size());
//    INFO("SaveVideo: " << path << "m_packetsVec.size : " << m_packetsVec.size());
    if (!m_packetsVec.empty()) {
        startWrite(path.c_str());
        int64_t firstKeyFramePts = m_packetsVec[0].m_packet.pts;
@@ -460,7 +476,7 @@
        unsigned long int frame_index = 0;
        for (const auto &item:m_packetsVec) {
            if (item.m_frameId < lastFrameId) {
                DBG("item.m_frameId < lastFrameId   " << item.m_frameId << "  " << lastFrameId);
//                DBG("item.m_frameId < lastFrameId   " << item.m_frameId << "  " << lastFrameId);
                conversion(const_cast<AVPacket *> (&item.m_packet), firstKeyFramePts, firstKeyFrameDts, video_st,
                           frame_index);
                frame_index++;
@@ -524,7 +540,7 @@
    m_frameIndex++;
    m_packetsVec.push_back({m_frameIndex, newPacket});
    if (newPacket.flags & AV_PKT_FLAG_KEY) {
        DBG("newPacket.flags & AV_PKT_FLAG_KEY   ");
//        DBG("newPacket.flags & AV_PKT_FLAG_KEY   ");
        m_last_I_FrameId = m_frameIndex;
    }
}
@@ -606,7 +622,7 @@
        AVPacket *pkg = static_cast<AVPacket *>(packet);
        DBG("frame_index==%d\n" << frame_index);
//        DBG("frame_index==\n" << frame_index);
        //Write PTS
        AVRational time_base1 = inStream->time_base;
        //Duration between 2 frames (us)