2011-11-21 479 views
1

我在我的C++应用程序中使用ffmpeg。ffmpeg声明 - 抛出异常而不是中止

当试图播放某些文件时,ffmpeg中的声明失败,导致它调用abort()终止我的应用程序。我不想要这种行为,而是希望有机会恢复,最好是通过例外。

任何人都有任何想法,我可以解决ffmpeg/assert的问题,可能会终止我的应用程序?

编辑:

我能想到的,现在唯一的办法是改变ffmpeg的断言宏,它可以使访问冲突,我可以通过SEH异常赶上。丑陋的和可能不好的解决方案

+0

可能的重复http://stackoverflow.com/questions/37473/how-can-i-assert-without-using-abort – pnezis

+0

不是重复的,请参阅评论jroks答案。 – ronag

回答

1

如果“异常”需要被编译为C,你可以使用了setjmp/longjump对。将setjmp放入您的错误处理代码中,并使用longjmp代替FFMPG代码中的中止。

如果您确实需要一个真正的异常来捕获,除零除可能比随机访问冲突更安全。

0

此代码是从ffmpeg doxygen documentation

/* 
* copyright (c) 2010 Michael Niedermayer <[email protected]> 
* 
* 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:%d\n", \ 
       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()

+0

这并不那么简单,ffmpeg需要编译为C代码,而C没有例外。 – ronag

+0

我明白了,我并不知道ffmpeg只是C语言。 – jrok

0

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

它可能不是世界上最好的解决方案,但它可以让你获得所需的隔离,并且有一定的希望知道发生了什么,而不必对ffpmpeg代码做太多的暴力行为。

相关问题