Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Issue when calling ffmpeg.c twice that makes the app crashes?

I'm trying to call ffmpeg.c to trim a video based on this code 'video-trimmer'. So when I try to run the activity (that loads and uses the native lib) the first time I click trin it works and I could trim the video but when I try to run it again it crashes (and it only work with the application restarts).

So I spend three days looking for a solution for this issue, most of the answers says the issue with the static variables in ffmpeg.c and creating a lib that loads and unload the class fixes the issue (answer1, answer2). So I tried to apply the solution that is based on the answers and this github repo on the video-trimmer project but all my attempts failed.

Is there any one knows about a fork of the 'video-trimmer' project that fixes the issue?. or can anybody provide step by step answer of how to implement the solution in the 'video-trimmer' project (because I tried to follow all the solution on the web and apply them in that project but with no luck).

like image 741
Jimmy Avatar asked Dec 27 '12 05:12

Jimmy


1 Answers

the problem seems to be with the initialized values (some variables are declared as global static vars, presumably for ease of access but breaks OOP principles and causes us problems like you're facing), however, there are a few ways round this that I can think of:

  • write a quick function to manually set the static vars back to their correct init values (quick and dirty but works). A list of methods which should not be allowed to fire off as and when they please follows:
    • avcodec_register_all(), avdevice_register_all(), av_register_all()
    • avcodec_find_encoder(), avcodec_find_decoder(), av_find_stream_info()
    • avcodec_open(), avcodec_close()
      • these could be wrapped in a boolean controlled method for example so that if they have run previously they cannot run again.
  • another way to control things is to manually force the variable values (by use of a class or struct to control the ffmpeg global vars) that are being re-initialised on subsequent runs, for example on running the method which currently causes the code to fail, the first step could be to manually set the variables back to their default settings so that they run correctly as at the moment I suspect you have data remaining resident between iterations, and thats what is causing problems.
  • you could utilse mutexes to ensure that the aforementioned methods behave more responsibly when used with threads.

Addendum:

  • also (at the C level) use libffmpeginvoke in preference to libffmpeg if you are going to invoke main() multiple times
  • forcibly invoke Garbage Collection (yep this is another 'ugly' fix) on the call to load the ffmpeg lib, which would then clean things up allowing you to call another instance

Let me know if you need something more in-depth: I can try making a test framework to replicate your problems and see where I get, although that needs access to my home PC as when I am at work I have no Android SDK.

like image 51
GMasucci Avatar answered Oct 05 '22 03:10

GMasucci