Ffmpeg断言-抛出异常而不是中止



我在我的c++应用程序中使用ffmpeg。

当尝试播放某些文件时,ffmpeg内部的断言失败,这导致它调用abort(),从而终止我的应用程序。我不希望出现这种行为,而是希望有机会恢复,最好是通过异常恢复。

任何人都有任何想法,我如何才能解决问题与ffmpeg/断言可能终止我的应用程序?

编辑:

我现在能想到的唯一方法是改变ffmpeg断言宏,这样它就会导致访问冲突,我可以通过SEH异常捕获。丑陋和潜在的坏解决方案?

如果需要将"exception"编译为C语言,则可以使用setjmp/跳远对。在错误处理代码中使用setjmp,在FFMPG代码中使用longjmp代替abort。

如果你真的想捕获一个真正的异常,除以0可能比随机访问冲突更安全。

这段代码来自ffmpeg氧文档

/*
 * copyright (c) 2010 Michael Niedermayer <michaelni@gmx.at>
 *
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * FFmpeg is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with FFmpeg; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */
 #ifndef AVUTIL_AVASSERT_H
 #define AVUTIL_AVASSERT_H
 #include <stdlib.h>
 #include "avutil.h"
 #include "log.h"
 #define av_assert0(cond) do {                                           
     if (!(cond)) {                                                      
         av_log(NULL, AV_LOG_FATAL, "Assertion %s failed at %s:%dn",    
                AV_STRINGIFY(cond), __FILE__, __LINE__);                 
         abort();                                                        
     }                                                                   
 } while (0)

 #if defined(ASSERT_LEVEL) && ASSERT_LEVEL > 0
 #define av_assert1(cond) av_assert0(cond)
 #else
 #define av_assert1(cond) ((void)0)
 #endif

 #if defined(ASSERT_LEVEL) && ASSERT_LEVEL > 1
 #define av_assert2(cond) av_assert0(cond)
 #else
 #define av_assert2(cond) ((void)0)
 #endif
 #endif /* AVUTIL_AVASSERT_H */

您可以简单地重新定义要抛出的av_assert宏而不是abort()

如果您不能/不想重新工作ffmpeg代码,那么我会说fork另一个进程来执行ffmpeg操作,然后退出。您可以在主进程中等待该进程以某种方式退出,并确定它是如何进行的,而不会有主进程被终止的风险。

这可能不是世界上最好的解决方案,但它为您提供了所需的隔离,有希望知道发生了什么,而不必对ffpmpeg代码做太多的暴力。

最新更新