5

I am using ffmpeg compiled for android and works pretty acceptable for now, however sometimes errors appear (based on some android phone configurations) and the app simply force closes with this message:

Fatal signal 11 (SIGSEGV) at 0x00000001 (code=1), thread 20745 (AsyncTask #2)

The ffmpeg call is inside a try/catch; however, it does not seem to care.

So, how can I prevent this force close and show the user a message?

chiastic-security
  • 20,430
  • 4
  • 39
  • 67
Alin
  • 14,809
  • 40
  • 129
  • 218

2 Answers2

1

I'm afraid I can't do that. See also this answer which hints at why.

When ffmpeg dies, it takes with it your entire program. This is just the way things are. When programming in Java, you don't have to think about programs crashing in that manner, but when ffmpeg, which is written i C, dies, it can take down your entire Java program.

try/catch does not help, because ffmpeg does not know or care about Java exceptions. Your only solution while staying within a Java program, is to either find the bug which makes ffmpeg die, or find what triggers the bug and call ffmpeg in such a way that it does not crash. As pointed out by Alex Cohn, another solution is to run ffmpeg in another process, so that it can not take down anything else but its own process.

Community
  • 1
  • 1
Prof. Falken
  • 24,226
  • 19
  • 100
  • 173
  • 1
    as a side note I've added ffmpeg call in a remote service so if it crashes, the app lives on, only service dies. – Alin Oct 06 '14 at 16:19
  • and how you can call it in another process: https://github.com/IljaKosynkin/FFmpeg-Development-Kit/blob/master/JNI/app/jni/videokit.c#L41 (here's JNI, it's better to use it with shared libraries, because you aren't allowed to use static binaries in commercial) – user25 Jun 03 '18 at 08:53
0

You can run ffmpeg not as a library, but as a separate executable process. This may be significantly less efficient, but in such setup your process may survive ffmpeg crash.

You can also setup your app such that it has Activity and Service that run in separate processes, see e.g. How to create an Android Activity and Service that use separate processes.

This allows for some watchdog mechanism, and more. I cannot tell without careful testing if this way can deliver better performance than running ffmpeg executable, or worse.

Community
  • 1
  • 1
Alex Cohn
  • 56,089
  • 9
  • 113
  • 307
  • In fact, AFAIK, since you can't `fork()` on Android, you *have* to create an Android Service, but I'm not sure what happens when ffmpeg crashes the service. Can you make it restart automatically? – Prof. Falken Oct 03 '14 at 08:22
  • Sure, you can always restart a service, whether it stopped naturally or due to some crash. I have never tried `fork()` on Android, but `Runtime. getRuntime().exec("/data/local/tmp/ffmpeg")` works beautifully from Java. – Alex Cohn Oct 03 '14 at 14:32
  • You mean you would bundle the ffmpeg binary in your Android app like that? Interesting. If it would be a sensible solution depends on how ffmpeg was used. Was it used for streaming from an API? Does exec() block? And so on... – Prof. Falken Oct 03 '14 at 14:37
  • _[Here](http://www.javaworld.com/article/2071275/core-java/when-runtime-exec---won-t.html)_ is a detailed instruction on using Runtime.exec(). No, it does not block unless you add call to `Process.waitFor()`. – Alex Cohn Oct 05 '14 at 13:54
  • _[Here](http://stackoverflow.com/a/17384650/192373)_ is an explanation how to easily bundle "command-line" executables with APK. – Alex Cohn Oct 05 '14 at 13:56
  • By the way, even though I never had a chance to `fork()` on Android, this API is supported by NDK. – Alex Cohn Oct 05 '14 at 14:13
  • and how you can call it in another process: https://github.com/IljaKosynkin/FFmpeg-Development-Kit/blob/master/JNI/app/jni/videokit.c#L41 (here's JNI, it's better to use it with shared libraries, because you aren't allowed to use static binaries in commercial) – user25 Jun 03 '18 at 08:53
  • @user25 well, the trivial way is to use Runtime.exec() from Java. – Alex Cohn Jun 03 '18 at 11:11
  • @AlexCohn what do you mean? how can I use it for calling method? – user25 Jun 03 '18 at 11:45
  • @AlexCohn it's for external programs – user25 Jun 03 '18 at 11:47
  • @user25 all this discussion was about **ffmpeg** which can be naturally built as a separate executable. I never proposed this as a general replacement for JNI. – Alex Cohn Jun 03 '18 at 19:55