chenshijun
2019-03-14 d3d1d88da20abffe10c62089e758768a3461b8ca
解决ffmpeg打开失败没处理导致段错误的问题
2个文件已修改
89 ■■■■■ 已修改文件
QiaoJiaSystem/GB28181DecoderModel/FFmpegDecoderJPG.cpp 84 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
QiaoJiaSystem/GB28181DecoderModel/VideoCaptureElementWithRtp.cpp 5 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
QiaoJiaSystem/GB28181DecoderModel/FFmpegDecoderJPG.cpp
@@ -143,27 +143,48 @@
 */
void BASICGB28181::FFmpegDecoderJPG::BareFlowDecoderThd(FFmpegDecoderJPG *p_this) {
    DBG(p_this->m_camIdx << "  BareFlowDecoderThd ok ... gpuIdx is " << p_this->m_gpuIdx);
//    while(!p_this->m_running) {
    p_this->m_running = true;
//    av_register_all();
//    avformat_network_init();
    p_this->ic = avformat_alloc_context();
    p_this->iobuffer = (unsigned char *) av_malloc(p_this->m_buf_size);
    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->iobuffer) {
            ERR("av_malloc: err======" << p_this->m_camIdx);
            p_this->m_running = false;
            return;
        }
        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) {
            ERR("avio_alloc_context: err======" << p_this->m_camIdx);
            p_this->m_running = false;
            return;
        }
    p_this->ic->pb = p_this->avio;
    int err = av_probe_input_buffer(p_this->ic->pb, &p_this->ic->iformat, nullptr, nullptr, 0, p_this->m_buf_size);
    if(err){
        ERR("av_probe_input_buffer" << err);
            ERR("av_probe_input_buffer: err======" << err  << p_this->m_camIdx);
            p_this->m_running = false;
            return;
    }
    err = avformat_open_input(&p_this->ic, "", NULL, NULL);
    if(err){
        ERR("avformat_open_input" << err);
            ERR("avformat_open_input: err======" << err  << p_this->m_camIdx);
            p_this->m_running = false;
            return;
    }
//    int err2 = avformat_find_stream_info(ic, nullptr);
    err = avformat_find_stream_info(p_this->ic, NULL);
    if(err){
        ERR("avformat_find_stream_info" << err);
            ERR("avformat_find_stream_info: err======" << err  << p_this->m_camIdx);
//        avformat_close_input(&p_this->ic);
//        DBG("avformat_close_input(&p_this->ic);");
            p_this->m_running = false;
            return;
    }
    int vi = -1;
    for (int i = 0; i < p_this->ic->nb_streams; ++i) {
@@ -175,7 +196,14 @@
    p_this->stream = p_this->ic->streams[vi];
    p_this->video_st = p_this->stream;
    p_this->ctx = avcodec_alloc_context3(nullptr);
    int err3 = avcodec_parameters_to_context(p_this->ctx, p_this->stream->codecpar);
        err = avcodec_parameters_to_context(p_this->ctx, p_this->stream->codecpar);
        if (err) {
            ERR("avcodec_parameters_to_context: err======" << err  << p_this->m_camIdx);
//        avformat_close_input(&p_this->ic);
//        DBG("avformat_close_input(&p_this->ic);");
            p_this->m_running = false;
            return;
        }
    p_this->codec = avcodec_find_decoder(p_this->ctx->codec_id);
    //是否启用GPU
@@ -190,12 +218,21 @@
                    p_this->codec = p_this->codec_cuvid;
                } else {
//                    return false;
                    ERR("codec_cuvid is NULL");
                        ERR("codec_cuvid is NULL"  << p_this->m_camIdx);
                }
            }
        }
    }
    int err4 = avcodec_open2(p_this->ctx, p_this->codec, nullptr);
        err = avcodec_open2(p_this->ctx, p_this->codec, nullptr);
        if (err) {
            ERR("avcodec_open2: err======" << err  << p_this->m_camIdx);
//        avformat_close_input(&p_this->ic);
//        DBG("avformat_close_input(&p_this->ic);");
//        avcodec_free_context(&p_this->ctx);
//        DBG("avcodec_free_context(&p_this->ctx);");
            p_this->m_running = false;
            return;
        }
    av_init_packet(&p_this->pkt);
@@ -218,7 +255,7 @@
        int err6 = avcodec_send_packet(p_this->ctx, &p_this->pkt);
        int err7 = avcodec_receive_frame(p_this->ctx, p_this->frame);
        if ((err7 == AVERROR(EAGAIN)) || (err5 < 0) || (err6 < 0)) {
            ERR(" error << err7:" << err7 << "  err5: " << err5 << " err6: " << err6);
                ERR("  err======: err7" << err7 << "  err5: " << err5 << " err6: " << err6  << p_this->m_camIdx);
            usleep(40000);
            continue;
        }
@@ -279,12 +316,13 @@
//        usleep(12000);
        }
#else
        usleep(usleepTime);
//            usleep(usleepTime);
#endif
    }
    DBG(" after while ");
    av_frame_free(&p_this->frame);
//    }
}
bool BASICGB28181::FFmpegDecoderJPG::startThd(const std::string &camIdx, const int &fps, const int &gpuIdx) {
@@ -296,6 +334,7 @@
            setenv("CUDA_VISIBLE_DEVICES", std::to_string(gpuIdx).c_str(), 0);
        }
        m_camIdx = camIdx;
        DBG("BareFlowDecoderThd camIdx : " << camIdx);
        std::thread t_BareFlowDecoder(BareFlowDecoderThd, this);
        t_BareFlowDecoder.detach();
    );
@@ -550,12 +589,8 @@
    if ((packet != nullptr) && (inVideoStream != nullptr)) {
        AVStream *inStream = (AVStream *) inVideoStream;
        AVPacket *pkg = static_cast<AVPacket *>(packet);
//        static unsigned  long int frame_index = 0
//            pkg->dts = ++frame_index;
//            pkg->pts = frame_index;
            //todo
//        if(pkg->pts==AV_NOPTS_VALUE){
            DBG("frame_index==%d\n" << frame_index);
            //Write PTS
            AVRational time_base1=inStream->time_base;
@@ -565,25 +600,8 @@
            pkg->pts=(double)(frame_index*calc_duration)/(double)(av_q2d(time_base1)*AV_TIME_BASE);
            pkg->dts=pkg->pts;
            pkg->duration=(double)calc_duration/(double)(av_q2d(time_base1)*AV_TIME_BASE);
//            frame_index++;
//        }
//        else{
//            DBG("pts" << pkg->pts << "firstKeyPts" << firstKeyPts);
//            DBG("dts" << pkg->dts << "firstKeyDts" << firstKeyDts);
//            pkg->pts -= firstKeyPts;
//            pkg->dts -= firstKeyDts;
//            pkg->pts = av_rescale_q_rnd(pkg->pts, inStream->time_base,
//                                        m_pOutVideo_stream->time_base,
//                                        (AVRounding) (AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX));
//            pkg->dts = av_rescale_q_rnd(pkg->dts, inStream->time_base,
//                                        m_pOutVideo_stream->time_base,
//                                        (AVRounding) (AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX));
//            pkg->duration = av_rescale_q(pkg->duration, inStream->time_base,
//                                         m_pOutVideo_stream->time_base);
//            pkg->pos = -1;
//        }
        DBG("pts:" << pkg->pts);
        DBG("dts:" << pkg->dts);
//        DBG("pts:" << pkg->pts);
//        DBG("dts:" << pkg->dts);
    }
}
QiaoJiaSystem/GB28181DecoderModel/VideoCaptureElementWithRtp.cpp
@@ -101,7 +101,12 @@
                p_this->m_running = true;
                p_this->m_fFmpegDecoderJPG.startThd(p_this->m_chanPubID, p_this->m_fps, p_this->m_gpuIdx);
                while (p_this->m_running) {
                    if(p_this->m_fFmpegDecoderJPG.getRunning()) {
                    usleep(300000);
                    }else{
                        p_this->m_running = false;
                        break;
                    }
                }
                DBG("videoCaptureElementThd stop ...");
                C_RealVideoStop(lrealhandle);