Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ FFMPEG:多线程解码死锁?_C++_Multithreading_Ffmpeg_Deadlock_Decode - Fatal编程技术网

C++ FFMPEG:多线程解码死锁?

C++ FFMPEG:多线程解码死锁?,c++,multithreading,ffmpeg,deadlock,decode,C++,Multithreading,Ffmpeg,Deadlock,Decode,我正在使用FFMPEG解码来自IP摄像机的H264流。有很多摄像头,所以我使用多线程和FFMPEG 我通过下面的代码向FFMPEG注册了多线程 static int lockmgr(void **mtx, enum AVLockOp op) { switch (op) { case AV_LOCK_CREATE: *mtx = malloc(sizeof(pthread_mutex_t)); if (!*mtx) r

我正在使用FFMPEG解码来自IP摄像机的H264流。有很多摄像头,所以我使用多线程和FFMPEG

我通过下面的代码向FFMPEG注册了多线程

static int lockmgr(void **mtx, enum AVLockOp op)
{
    switch (op)
    {
    case AV_LOCK_CREATE:
        *mtx = malloc(sizeof(pthread_mutex_t));
        if (!*mtx)
            return 1;
        return !!pthread_mutex_init((pthread_mutex_t*)*mtx, NULL);
    case AV_LOCK_OBTAIN:
        return !!pthread_mutex_lock((pthread_mutex_t*)*mtx);
    case AV_LOCK_RELEASE:
        return !!pthread_mutex_unlock((pthread_mutex_t*)*mtx);
    case AV_LOCK_DESTROY:
        pthread_mutex_destroy((pthread_mutex_t*)*mtx);
        free(*mtx);
        return 0;
    }
    return 1;
}

av_lockmgr_register(lockmgr)
在连接到IP摄像机的每个线程中,解码来自该IP摄像机的H264流的代码如下

 int DecodeStream()
 {
     try
    {
        InitForH264Stream();

        int             bytesDecoded = 0;
        int             frameFinished = 0;
        while (commonGlobal->settings.iPCameraSettigs.isRunningThreadRequestVideo[rtpHeader->cameraInd])
        {
            while (_packet->size > 0)
            {
                // Decode the next chunk of data
                bytesDecoded = avcodec_decode_video2(rtpHeader->pCodecCtx, rtpHeader->pFrame,
                    &frameFinished, _packet);
                // Was there an error?
                if (bytesDecoded < 0)
                {
                    if (rtpHeader->packetPointer != NULL)
                    {
                        _packet->data = rtpHeader->packetPointer;
                        rtpHeader->packetPointer = NULL;
                        av_free_packet(_packet);
                    }
                    return RS_NOT_OK;
                }

                _packet->size -= bytesDecoded;
                _packet->data += bytesDecoded;
                if (rtpHeader->packetPointer != NULL && _packet->size == 0)
                {
                    _packet->data = rtpHeader->packetPointer;
                    rtpHeader->packetPointer = NULL;
                    av_free_packet(_packet);
                }
                if (frameFinished)
                {
                    return RS_OK;
                }
                // Did we finish the current frame? Then we can return
            }
            do
            {
                try
                {
                    av_init_packet(_packet);
                    rtpHeader->th->Reset();
                    int ret = AVERROR(EAGAIN);
                    while (AVERROR(EAGAIN) == ret)
                        ret = av_read_frame(pFormatCtx, _packet);
                    if (ret < 0)
                    {
                        if (ret == AVERROR(AVERROR_EOF) || (pFormatCtx->pb && pFormatCtx->pb->eof_reached))
                        {
                            sprintf(strErr, "Error end of file line %d", __LINE__);
                        }
                        if (pFormatCtx->pb && pFormatCtx->pb->error)
                        {
                            sprintf(strErr, "Error end of file line %d", __LINE__);
                        }
                        _packet->data = NULL;
                        return RS_NOT_OK;
                    }
                    if (_packet->stream_index != rtpHeader->videoStreamInd)
                        av_free_packet(_packet);
                    else
                        rtpHeader->packetPointer = _packet->data;
                }
                catch (...)
                {
                    _packet->data = NULL;
                    return RS_NOT_OK;
                }
            } while (_packet->stream_index != rtpHeader->videoStreamInd);
        }
    }
    catch (...)
    {
        _packet = NULL;
        commonGlobal->WriteRuntimeLogs("ReceiveRTPBlock() threw an Exception");
        UnInitForH264Stream();
        return RS_NOT_OK;
    }
 }


VOID UnInitForH264Stream()
{
    if (rtpHeader.pCodecCtx != NULL)
        avcodec_close(rtpHeader.pCodecCtx);

    if (pFormatCtx != NULL)
        av_close_input_file(pFormatCtx);

    if (rtpHeader.th != NULL)
    {
        delete rtpHeader.th;
        rtpHeader.th = NULL;
    }       

    if (rtpHeader.pFrame != NULL)
        avcodec_free_frame(&rtpHeader.pFrame);

    if (RGBFrame != NULL)
    {
        avcodec_free_frame(&RGBFrame);
        RGBFrame = NULL;            
    }

    if (ConversionContext != NULL)
    {
        sws_freeContext(ConversionContext);
        ConversionContext = NULL;
    }

    if (rgbBuffer != NULL)
    {
        av_free(rgbBuffer);
        rgbBuffer = NULL;
    }   
}
int DecodeStream()
{
尝试
{
InitForH264Stream();
int字节解码=0;
int frameFinished=0;
while(commonGlobal->settings.iPCameraSettigs.isRunningThreadRequestVideo[rtpHeader->cameraInd])
{
而(_数据包->大小>0)
{
//解码下一个数据块
bytesDecoded=avcodec\u decode\u VIDEO 2(rtpHeader->PCODECTX,rtpHeader->pFrame,
&框架完成,_包);
//有错误吗?
if(字节解码<0)
{
if(rtpHeader->packetPointer!=NULL)
{
_packet->data=rtpHeader->packetPointer;
rtpHeader->packetPointer=NULL;
av_免费_包(_包);
}
返回RS\u NOT\u OK;
}
_数据包->大小-=字节解码;
_数据包->数据+=字节解码;
如果(rtpHeader->packetPointer!=NULL&&&u packet->size==0)
{
_packet->data=rtpHeader->packetPointer;
rtpHeader->packetPointer=NULL;
av_免费_包(_包);
}
如果(框架完成)
{
返回RS_OK;
}
//我们完成当前帧了吗?然后我们可以返回
}
做
{
尝试
{
av_初始_数据包(_数据包);
rtpHeader->th->Reset();
int-ret=平均误差(EAGAIN);
while(averor(EAGAIN)=ret)
ret=av_读取_帧(pFormatCtx,_数据包);
如果(ret<0)
{
如果(ret==averor(averor_EOF)| |(pFormatCtx->pb&&pFormatCtx->pb->EOF到达))
{
sprintf(strErr,“文件行%d的错误结束”,第行);
}
if(pFormatCtx->pb&&pFormatCtx->pb->error)
{
sprintf(strErr,“文件行%d的错误结束”,第行);
}
_数据包->数据=NULL;
返回RS\u NOT\u OK;
}
如果(数据包->流索引!=rtpHeader->视频流索引)
av_免费_包(_包);
其他的
rtpHeader->packetPointer=\u packet->data;
}
捕获(…)
{
_数据包->数据=NULL;
返回RS\u NOT\u OK;
}
}while(_packet->stream_index!=rtpHeader->videoStreamInd);
}
}
捕获(…)
{
_数据包=空;
commonGlobal->WriteRuntimeLogs(“ReceiveRTPBlock()引发异常”);
uninitforh264流();
返回RS\u NOT\u OK;
}
}
VOID uninitforh264流()
{
如果(rtpHeader.pCodecCtx!=NULL)
avcodec_close(rtpHeader.pCodecCtx);
如果(pFormatCtx!=NULL)
av_关闭_输入_文件(pFormatCtx);
如果(rtpHeader.th!=NULL)
{
删除rtpHeader.th;
rtpHeader.th=NULL;
}       
if(rtpHeader.pFrame!=NULL)
avcodec_free_帧(&rtpHeader.pFrame);
if(RGBFrame!=NULL)
{
avcodec_free_帧(&RGBFrame);
RGBFrame=NULL;
}
if(ConversionContext!=NULL)
{
sws_freeContext(转换上下文);
ConversionContext=NULL;
}
if(rgbBuffer!=NULL)
{
av_免费(rgbBuffer);
rgbBuffer=NULL;
}   
}
  • 当函数
    avcodec\u decode\u video2()
    抛出异常时,我遇到了死锁,然后在
    avcodec\u close(rtpHeader.pCodecCtx)行调用
    uninitforh264 stream()
    时,程序被死锁我已经修复了,可能功能
    avcodec\u decode\u video2()
    工作正常(不抛出异常)

  • 但现在,有时我在解码时遇到了死锁,但我不知道是哪个函数导致了死锁。因为很难复制这个bug

有人会告诉我,我的代码中是否存在任何潜在的死锁

多谢各位

显示了锁的一个稍微不同的实现


此外,您似乎在解码函数中初始化ffmpeg。似乎指出了这样一个结论,即即使正确声明了lockmgr,打开和关闭流也不是线程安全的操作。你应该考虑把它们移到一个中心,同步的地方,你的流开始

谢谢!这段代码是概要代码,主要代码是按照您所说的“将它们移动到流开始的中央同步位置”实现的。你能解释一下我的代码和你展示给我的锁的上述实现之间的区别吗?我要删除*here:if(!*mtx)。你也不需要这个!!在pthread函数前面!!通常是“将指针投射到布尔”的C代码。我认为如果pthread函数返回0,它将返回1,这将指示失败。我不是百分之百的关心这个NVM!!应该可以,对于所有非0值,它将返回1