Skip to content
This repository has been archived by the owner on Oct 7, 2021. It is now read-only.

Issue when using mobile-ffmpeg, with Unity3D on Android #258

Closed
emongev opened this issue Oct 24, 2019 · 36 comments
Closed

Issue when using mobile-ffmpeg, with Unity3D on Android #258

emongev opened this issue Oct 24, 2019 · 36 comments
Assignees
Labels
enhancement New feature or request fixed

Comments

@emongev
Copy link

emongev commented Oct 24, 2019

Description
I get an app crash whenever I call Resources.UnloadUnusedAssets after executing something on the library. Im currently testing it with the "-i path -hide_banner" arguments.

Expected behavior
I expect the app not to crash.

Current behavior
The app crashes immediately.

Screenshots
If applicable, add screenshots to help explain your problem.

Logs
https://ghostbin.co/paste/vt9t4

Environment

  • Platform: Android/IOS
  • Architecture: armv7
  • Version (if applicable) 4.2.2
  • Source branch (if applicable) [e.g. master, dev-v2.x]
  • Xcode version (if applicable) [e.g. 7.3.1, 9.0.1]
  • Cocoapods version (if applicable) [e.g. 1.2.1]
  • Android Studio version (if applicable) [e.g. 3.1]
  • Android NDK version (if applicable) [e.g. 16b, 17c]

Other
Add any other context about the problem here.

@cyliax
Copy link

cyliax commented Oct 24, 2019

I have the same problem. I used all the different versions of Unity (2018 LTS to 2019.2), built it for ARMv7 and ARM64 and reduced my app to the minimum. The encoding process is working with sync or async calls and I can repeat them without any problem. But it seems mobileffmpeg/ffmpeg is using resources or memory that belong to Unity.

It crashed when I call:

  • Resources.UnloadUnusedAssets (always)
  • start to record Audios with the Microphone class (second or third attempt)
  • play videos and play audios (after some attempt, even if I use new copied resources)
  • reloading the entire Unity scene (always)

As long as there was no encoding process started, everything runs fine, but with the start of mobileffmpeg Unity becomes unstable. I spend so much time right now to fix that in Unity, but without success. There must be something within the plugin, not unlinking or discard resources.

I'm happy that I'm not alone anymore with that huge problem. My logs look the same:

signal 11 (SIGSEGV).... Consumer closed input channel or an error occurred.

@emongev
Copy link
Author

emongev commented Oct 24, 2019

@cyliax Haha yeah i saw your posts on being able to work with the library Asynchronously and thought you had everything resolved. (If we get this resolved I'll probably ask you about how you got it running asynchronously haha)

On your crashes, I think its always the same thing. I was recording images from a RenderTexture and it always crashed on the 13th image, but i noticed on the memory profiler that every 13th image, without me calling the ffmpeg library previously, the GC allocation got released. So either that means Unity was internally calling UnloadUnusedAssets, or C# was doing GC.Collect and UnloadUnusedAssets calls GC.Collect internally as well.

Reloading a scene in a non-additive fashion causes Unity to call UnloadUnusedAssets as well.

After much testing Im also sure the plugin is mishandling memory in some way that causes the app to panic when garbage collection is done.

Have you manage to find anything in your tests? Today Im gonna try out a couple of things and will post here if I find anything.

@cyliax
Copy link

cyliax commented Oct 25, 2019

Your right, I thought everything is resolved, but with that crashes all the time, it is not. So I wasn't that happy and I haven't shared my solution yet (but I will).

You might be right, that all our problems are coming from the same Resources.UnloadUnusedAsset and GC.Collect somehow. That leads me to disable garbage collection in my single scene with:

GarbageCollector.GCMode = GarbageCollector.Mode.Disabled;

for a test, and you know what: IT IS WORKING! (as long as I'm not reloading the scene)

But of course, that can't be the solution. I would be interesting if a non Unity app using the plugin is crashing as well, when calling Javas System.gc().

@emongev
Copy link
Author

emongev commented Oct 26, 2019

@cyliax Huh really? I did test disabling Garbage Collection but got the crash anyways, but even then disabling garbage collection makes the app a ticking bomb until it crashes haha

@tanersener
Copy link
Owner

Tried a lot of things to resolve this, unfortunately I wasn't successful.

@emongev
Copy link
Author

emongev commented Nov 25, 2019

So yeah, I retried @cyliax 's mode of disabling the Garbage Collection and it works. Ive also managed to get it running asynchronously but without disabling the garbage collector the app crashes without finishing processing the video.

Ive tried researching a lot on what could be causing this, and apparently it should be an error from the unmanaged part of code (the C part) but obviously the ffmpeg library is immense and would take a long time to figure out which part causes the issue. So ideally we should try and find where the issue is.

Ive got this trace:

`11-25 16:00:47.029 8062 8081 E CRASH : signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0013c288

11-25 16:00:47.029 8062 8081 E CRASH : *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***

11-25 16:00:47.029 8062 8081 E CRASH : Build type 'Release', Scripting Backend 'il2cpp', CPU 'armeabi-v7a'

11-25 16:00:47.029 8062 8081 E CRASH : Build fingerprint: 'samsung/beyond2qlteue/beyond2q:9/redacted/Gredacted:user/release-keys'

11-25 16:00:47.029 8062 8081 E CRASH : Revision: '17'

11-25 16:00:47.029 8062 8081 E CRASH : pid: 8062, tid: 8081, name: UnityMain >>> com.redacted.redacted <<<

11-25 16:00:47.029 8062 8081 E CRASH : r0 c8906bc0 r1 00000000 r2 00000000 r3 00000000

11-25 16:00:47.029 8062 8081 E CRASH : r4 00000000 r5 c8905aa0 r6 c962e680 r7 00000005

11-25 16:00:47.029 8062 8081 E CRASH : r8 00000000 r9 00000000 sl 00000001 fp caafe970

11-25 16:00:47.029 8062 8081 E CRASH : ip c9628e2c sp caafe950 lr c8906868 pc 0013c288 cpsr c06f8700

11-25 16:00:47.029 8062 8081 E CRASH :

11-25 16:00:47.029 8062 8081 E CRASH : backtrace:`

If we can find a way to translate the trace to something more readable that could help.

@Legacycz
Copy link

Hello,
I'm currently facing the same issue. Is there any progress on your side?
I might try a workaround by exporting Unity into an Android project and add the library in the native app, where Unity project will be a in a different library (https://forum.unity.com/threads/integration-unity-as-a-library-in-native-android-app-version-2.751712/). I'm not sure thought if that will fix the problem.

@cyliax
Copy link

cyliax commented Mar 30, 2020

Has anybody had success with this garbage collection/crash problem? I could cry because I'm loosing day after day and nothing helps. Extracted AAR to JAR, using instance calls except of static, switching back to sync calls, updating Unity to 2019.3.6f1, updating mobile-ffmpeg to 4.3.1, checking and unchecking options in unity... and so on.

At the moment I'm still switching off garbage collection at app start, then I have an overlay button to enable garbage collection and call Resources.UnloadUnusedAssets() or GC.Collect(), and switching it off again.

I can press that button as often as I like before mobile-ffmpeg was running. But when I started a successful ffmpeg conversion and I press that button once or twice... crash, without a helpful crash log via ADB.

@tanersener if I could send you a simple Unity example scene with everything you need, It would be awesome if you'd have a look. Would it help to support you with some money via your donation channel?

Any help is highly appreciated, thank you for your great work.

@tanersener
Copy link
Owner

@cyliax created my own sample project to demonstrate this issue. Unfortunately, I don't have any ideas about the solution. This integration is too complex for me. I think someone who has experience in loading native libraries inside Unity can help us solve this.

@tanersener
Copy link
Owner

I have a question about the crash scenario.

As far as I understand, application crashes when MobileFFmpeg is not used and one of theResources.UnloadUnusedAssets()/GC.Collect() methods is executed.

My question is; what happens when Resources.UnloadUnusedAssets()/GC.Collect() is called when FFmpeg.execute() is still executing the command. Does your application crash in that case too?

@cyliax
Copy link

cyliax commented Apr 2, 2020

I found a workaround and it this also answers your question a bit (I will build your scenario today):

My workaround for now:
As long as MobileFFmpeg plugin wasn't startet I let Unity decide when to call Resources.UnloadUnusedAssets() or call it by myself as normal.
After the plugin was started, it is possible to run Resources.UnloadUnusedAssets() ONCE but not TWICE. So at the moment when I'm starting the plugin I disable Unity's automatic GC and after that I'm using a little helper which is doing a dummy call to the plugin just asking MobileFFmpeg for its "-version" and call Resources.UnloadUnusedAssets() after that (everywhere in my app).
This sounds stupid but it is working, the app isn't crashing anymore when I do this dummy call before GC. GC is done, when the completeHandler of the async call was exectuted and the plugin as already set back to null in Unity.

Now back to your question: I'll try calling GC during encoding...

@GoncaloBastos
Copy link

Hey @tanersener! I've experienced something similar, where Android is crashing silently when trying to load an asset after using ffmpeg (doesn't have to be the asset that was just generated).

No error is given or exception thrown before the crash so it makes it very difficult to track, but after some research we were able to figure out that it was only ever happening when making certain calls AFTER using mobile-ffmpeg. The same call made before using ffmpeg does not cause the app to crash.

@wloczykij89
Copy link

wloczykij89 commented May 27, 2020

Hey guys. I use ffmpeg for operations on audio data, e.g. generating .ogg files. In my cases, @cyliax 's solution doesn't work. Has anyone else succeeded in making Unity not crash after encoding?

I build mobile-ffmpeg.arr with lots of comments on the native side written in c and tried different things. In the "fftools_ffmpeg_opt.c" file in "ffmpeg_parse_options(int argc, char **argv)" method, I removed call to "term_init ();". After that it seems to work with Unity without crash. I'm not a "c" guy, don't understand why it's not crashing after that and don't know consequences of removing this call.
Someone?

void term_init(void)
{
#if HAVE_TERMIOS_H
    if (!run_as_daemon && stdin_interaction) {
        struct termios tty;
        if (tcgetattr (0, &tty) == 0) {
            oldtty = tty;
            restore_tty = 1;

            tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
                             |INLCR|IGNCR|ICRNL|IXON);
            tty.c_oflag |= OPOST;
            tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
            tty.c_cflag &= ~(CSIZE|PARENB);
            tty.c_cflag |= CS8;
            tty.c_cc[VMIN] = 1;
            tty.c_cc[VTIME] = 0;

            tcsetattr (0, TCSANOW, &tty);
        }
        signal(SIGQUIT, sigterm_handler); /* Quit (POSIX).  */
    }
#endif

    signal(SIGINT , sigterm_handler); /* Interrupt (ANSI).    */
    signal(SIGTERM, sigterm_handler); /* Termination (ANSI).  */
#ifdef SIGXCPU
    signal(SIGXCPU, sigterm_handler);
#endif
#ifdef SIGPIPE
    signal(SIGPIPE, SIG_IGN); /* Broken pipe (POSIX). */
#endif
#if HAVE_SETCONSOLECTRLHANDLER
    SetConsoleCtrlHandler((PHANDLER_ROUTINE) CtrlHandler, TRUE);
#endif
}

P.S. @tanersener I love you man for this project :)

@wloczykij89
Copy link

I did further tests, which showed that after removing the following line, Unity no longer crash after encoding and calling Resources.UnloadUnusedAssets().

file fftools_ffmpeg.c -> void term_init(void)
signal(SIGXCPU, sigterm_handler);

Anybody knows what's going on? SIGXCPU signal means "CPU time limit exceeded".

This is signal handler:

static void
sigterm_handler(int sig)
{
    int ret;
    received_sigterm = sig;
    received_nb_signals++;
    term_exit_sigsafe();
    if(received_nb_signals > 3) {
        ret = write(2/*STDERR_FILENO*/, "Received > 3 system signals, hard exiting\n",
                    strlen("Received > 3 system signals, hard exiting\n"));
        if (ret < 0) { /* Do nothing */ };
        exit(123);
    }
}

@cyliax
Copy link

cyliax commented May 28, 2020

That are great news, Thanks for your test to find out where those crashes come from. Unfortunately I'm not a C guy as well, so I can't give you support. But I'll try your approach to get rid of my workaround routine. Using UnloadUnsedAssets as normal would be awesome.

@tanersener
Copy link
Owner

@wloczykij89 sigterm_handler is a common structure used in C applications. It is used to handle signals sent by Operating System.

I'm not sure whether your solution is fixing the problem or postponing it. Because not handling a signal looks scary a little bit. Is it possible to test which statement in sigterm_handler is causing the crash?

@wloczykij89
Copy link

@tanersener I totally agree with you and treat this as a clue to finding the real cause of the error. But damn, in my test project, it works after that change :/ Maybe I'll do more tests on a complex project and different devices.

The specific line of code that cause crash in sigterm_handler (int sig):
exit(123);

I have created test handler only for SIGXCPU:
signal (SIGXCPU, test_sigterm_handler);

"test_sigterm_handler" with logs and commented out "exit(123)" line:

static void test_sigterm_handler(int sig)
{
    int ret;
    received_sigterm = sig;
    received_nb_signals++;
    term_exit_sigsafe();

    int signal_id = received_nb_signals;
    av_log(NULL, AV_LOG_WARNING, "sigterm_handler 1 - signal_id: %d, sig: %d.\n", signal_id, sig);

    if(received_nb_signals > 3) {
        ret = write(2/*STDERR_FILENO*/, "Received > 3 system signals, hard exiting\n",
                    strlen("Received > 3 system signals, hard exiting\n"));

        av_log(NULL, AV_LOG_WARNING, "sigterm_handler 2 - signal_id: %d, sig: %d, ret: %d.\n", signal_id, sig, ret);

        if (ret < 0) { /* Do nothing */ };

        // cause Unity crash if call Resources.UnloadUnusedAssets() or GC.Collect()
        //exit(123);
    }

    av_log(NULL, AV_LOG_WARNING, "sigterm_handler 3 - signal_id: %d, sig: %d.\n", signal_id, sig);
}

During encoding, "test_sigterm_handler" may invoke several times or not at all.
After encoding and calling Resources.UnloadUnusedAssets() or GC.Collect(), "test_sigterm_handler" is invoked every time. Logcat output:

05/31 00:16:28.518 30049 30062 Warn Unity Call Resources.UnloadUnusedAssets();
05/31 00:16:28.518 30049 30062 Warn Unity  #0 0x5fe15df9 (libunity.so) ? 0x0
05/31 00:16:28.518 30049 30062 Warn Unity  #1 0x604ec57f (libunity.so) ? 0x0
05/31 00:16:28.518 30049 30062 Warn Unity  #2 0x5fffc185 (libunity.so) ? 0x0
05/31 00:16:28.518 30049 30062 Warn Unity  #3 0x5fffc0f3 (libunity.so) ? 0x0
05/31 00:16:28.518 30049 30062 Warn Unity  #4 0x61e7e174 (libil2cpp.so) ? 0x0
05/31 00:16:28.518 30049 30062 Warn Unity  #5 0x621cfe70 (libil2cpp.so) ? 0x0
05/31 00:16:28.518 30049 30062 Warn Unity  #6 0x61e71850 (libil2cpp.so) ? 0x0
05/31 00:16:28.518 30049 30062 Warn Unity  #7 0x61e79a80 (libil2cpp.so) ? 0x0
05/31 00:16:28.518 30049 30062 Warn Unity  #8 0x62162d20 (libil2cpp.so) ? 0x0
05/31 00:16:28.518 30049 30062 Warn Unity  #9 0x61ff36ec (libil2cpp.so) ? 0x0
05/31 00:16:28.518 30049 30062 Warn Unity  #10 0x61cb9fa4 (libil2cpp.so) ? 0x0
05/31 00:16:28.518 30049 30062 Warn Unity  #11 0x61cb9250 (libil2cpp.so) ? 0x0
05/31 00:16:28.518 30049 30062 Warn Unity  #12 0x61cb8f0c (libil2cpp.so) ? 0x0
05/31 00:16:28.518 30049 30062 Warn Unity  #13 0x61b4443c (libil2cpp.so) ? 0x0
05/31 00:16:28.518 30049 30062 Warn Unity  #14 0x61bc2838 (libil2cpp.so) ? 0x0
05/31 00:16:28.518 30049 30062 Warn Unity  #15 0x5ff8efbd (libunity.so) ? 0x0
05/31 00:16:28.518 30049 30062 Warn Unity  #16 0x5ff97df7 (libunity.so) ? 0x0
05/31 00:16:28.518 30049 30062 Warn Unity  #17 0x5ff9e12b (libunity.so) ? 0x0
05/31 00:16:28.518 30049 30062 Warn Unity  #18 0x5fe6f903 (libunity.so) ? 0x0
05/31 00:16:28.518 30049 30062 Warn Unity  #19 0x5ff0594d (libunity.so) ? 0x0
05/31 00:16:28.518 30049 30062 Warn Unity  #20 0x5ff05969 (libunity.so) ? 0x0
05/31 00:16:28.518 30049 30062 Warn Unity  #21 0x5ff05aff (libunity.so) ? 0x0
05/31 00:16:28.518 30049 30062 Warn Unity  #22 0x5ffde15d (libunity.so) ? 0x0
05/31 00:16:28.518 30049 30062 Warn Unity  #23 0x5ffeb9dd (libunity.so) ? 0x0
05/31 00:16:28.518 30049 30062 Warn Unity  #24 0x418d290c (libdvm.so) dvmPlatformInvoke 0x70
05/31 00:16:28.518 30049 30062 Warn Unity  #25 0x41903bbd (libdvm.so) dvmCallJNIMethod(unsigned int const*, JVal
05/31 00:16:28.538 30049 30127 Warn mobile-ffmpeg sigterm_handler 1 - signal_id: 1, sig: 24.
05/31 00:16:28.538 30049 30127 Warn mobile-ffmpeg sigterm_handler 1 - signal_id: 1, sig: 24.
05/31 00:16:28.538 30049 30127 Warn mobile-ffmpeg sigterm_handler 3 - signal_id: 1, sig: 24.
05/31 00:16:28.538 30049 30127 Warn mobile-ffmpeg sigterm_handler 3 - signal_id: 1, sig: 24.
05/31 00:16:36.762 30049 30049 Info View Touch down dispatch to android.view.SurfaceView{41ca62d0 VFE..... .F....I. 0,0-1280,800 #7f010000 app:id/unitySurfaceView}, event = MotionEvent { action=ACTION_DOWN, id[0]=0, x[0]=1035.1913, y[0]=779.02625, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=789991175, downTime=789991175, deviceId=2, source=0x1002 }
05/31 00:16:36.763 30049 30049 Info View Touch down dispatch to com.unity3d.player.UnityPlayer{41c9bfa8 V.E..... ......I. 0,0-1280,800}, event = MotionEvent { action=ACTION_DOWN, id[0]=0, x[0]=1035.1913, y[0]=779.02625, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=789991175, downTime=789991175, deviceId=2, source=0x1002 }
05/31 00:16:36.891 30049 30049 Info View Touch up dispatch to com.unity3d.player.UnityPlayer{41c9bfa8 V.E..... ......I. 0,0-1280,800}, event = MotionEvent { action=ACTION_UP, id[0]=0, x[0]=1035.1913, y[0]=779.02625, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=789991306, downTime=789991175, deviceId=2, source=0x1002 }
05/31 00:16:36.909 30049 30062 Warn Unity Call GC.Collect()
05/31 00:16:36.909 30049 30062 Warn Unity  #0 0x5fe15df9 (libunity.so) ? 0x0
05/31 00:16:36.909 30049 30062 Warn Unity  #1 0x604ec57f (libunity.so) ? 0x0
05/31 00:16:36.909 30049 30062 Warn Unity  #2 0x5fffc185 (libunity.so) ? 0x0
05/31 00:16:36.909 30049 30062 Warn Unity  #3 0x5fffc0f3 (libunity.so) ? 0x0
05/31 00:16:36.909 30049 30062 Warn Unity  #4 0x61e7e174 (libil2cpp.so) ? 0x0
05/31 00:16:36.909 30049 30062 Warn Unity  #5 0x621cfefc (libil2cpp.so) ? 0x0
05/31 00:16:36.909 30049 30062 Warn Unity  #6 0x61e71850 (libil2cpp.so) ? 0x0
05/31 00:16:36.909 30049 30062 Warn Unity  #7 0x61e79a80 (libil2cpp.so) ? 0x0
05/31 00:16:36.909 30049 30062 Warn Unity  #8 0x62162d20 (libil2cpp.so) ? 0x0
05/31 00:16:36.909 30049 30062 Warn Unity  #9 0x61ff36ec (libil2cpp.so) ? 0x0
05/31 00:16:36.909 30049 30062 Warn Unity  #10 0x61cb9fa4 (libil2cpp.so) ? 0x0
05/31 00:16:36.909 30049 30062 Warn Unity  #11 0x61cb9250 (libil2cpp.so) ? 0x0
05/31 00:16:36.909 30049 30062 Warn Unity  #12 0x61cb8f0c (libil2cpp.so) ? 0x0
05/31 00:16:36.909 30049 30062 Warn Unity  #13 0x61b4443c (libil2cpp.so) ? 0x0
05/31 00:16:36.909 30049 30062 Warn Unity  #14 0x61bc2838 (libil2cpp.so) ? 0x0
05/31 00:16:36.909 30049 30062 Warn Unity  #15 0x5ff8efbd (libunity.so) ? 0x0
05/31 00:16:36.909 30049 30062 Warn Unity  #16 0x5ff97df7 (libunity.so) ? 0x0
05/31 00:16:36.909 30049 30062 Warn Unity  #17 0x5ff9e12b (libunity.so) ? 0x0
05/31 00:16:36.909 30049 30062 Warn Unity  #18 0x5fe6f903 (libunity.so) ? 0x0
05/31 00:16:36.909 30049 30062 Warn Unity  #19 0x5ff0594d (libunity.so) ? 0x0
05/31 00:16:36.909 30049 30062 Warn Unity  #20 0x5ff05969 (libunity.so) ? 0x0
05/31 00:16:36.909 30049 30062 Warn Unity  #21 0x5ff05aff (libunity.so) ? 0x0
05/31 00:16:36.909 30049 30062 Warn Unity  #22 0x5ffde15d (libunity.so) ? 0x0
05/31 00:16:36.909 30049 30062 Warn Unity  #23 0x5ffeb9dd (libunity.so) ? 0x0
05/31 00:16:36.909 30049 30062 Warn Unity  #24 0x418d290c (libdvm.so) dvmPlatformInvoke 0x70
05/31 00:16:36.909 30049 30062 Warn Unity  #25 0x41903bbd (libdvm.so) dvmCallJNIMethod(unsigned int const*, JValue*, Method const*,
05/31 00:16:36.911 30049 30127 Warn mobile-ffmpeg sigterm_handler 1 - signal_id: 2, sig: 24.
05/31 00:16:36.911 30049 30127 Warn mobile-ffmpeg sigterm_handler 1 - signal_id: 3, sig: 24.
05/31 00:16:36.911 30049 30127 Warn mobile-ffmpeg sigterm_handler 3 - signal_id: 2, sig: 24.
05/31 00:16:36.911 30049 30127 Warn mobile-ffmpeg sigterm_handler 3 - signal_id: 3, sig: 24.
05/31 00:16:36.914 30049 30066 Debug dalvikvm threadid=17: interp stack at 0x6d521000
05/31 00:16:36.915 30049 30066 Debug dalvikvm threadid=17: bye!
05/31 00:16:36.915 30049 30066 Debug dalvikvm threadid=17: interp stack at 0x6d521000
05/31 00:16:36.915 30049 30066 Debug dalvikvm threadid=17: bye!
05/31 00:16:56.558 30049 30054 Debug dalvikvm GC_CONCURRENT freed 2956K (86629), 51% free 3164K/6420K, paused 2ms+1ms, total 30ms

@tanersener
Copy link
Owner

@wloczykij89 Thanks for doing these tests. I think you've already found the real cause of this issue.

If you ask google SIGXCPU Mono you can see that there are similar issues created by other Unity users. It looks like Unity uses Mono framework and Mono uses SIGXCPU internally for GC. That's why this signal is sent when Resources.UnloadUnusedAssets() or GC.Collect() is called. You can see this information in signal handling section of official Mono documentation.

The solution is to ignore SIGPWR and SIGXCPU signals. Unfortunately, these two signals still have to handled for non-Unity users. So I can't disable them totally. I'll try to find a solution for that.

@tanersener tanersener added enhancement New feature or request and removed needs-analysis labels Jun 13, 2020
@tanersener tanersener added this to To do in The Next FFmpeg Release via automation Jun 13, 2020
@tanersener
Copy link
Owner

Added a new API method, ignoreSignal, to ignore signals in the development branch. You can use this method to ignore SIGXCPU signal under Unity. Other frameworks that use Mono framework can benefit from that method too. As far as I know Xamarin also uses Mono framework.

ignoreSignal is added for both Android and iOS/tvOS platforms, it will be included in the next MobileFFmpeg release.

Android

Config.ignoreSignal(Signal.SIGXCPU);

iOS/tvOS

[MobileFFmpegConfig ignoreSignal:SIGXCPU];

@tanersener tanersener moved this from To do to Done in The Next FFmpeg Release Jun 17, 2020
@tanersener
Copy link
Owner

July 2020.

@cyliax
Copy link

cyliax commented Jun 25, 2020

Awesome, I'll try this as soon as I can. Hopefully I can remove my stupid garbage collection workarounds. Great work!

@shpitsmedia
Copy link

Hi cyliax and tanersener,

I'm seeing the same crash on my side too using Unity 2019, do you have a link to download an Android LTS binary so I could test it on my side too?

Thanks.

@tanersener
Copy link
Owner

@shpitsmedia You can use this dev build for testing, mobile-ffmpeg-min-4.3.3.LTS.aar.

@shpitsmedia
Copy link

@tanersener Amazing, it works! can you please also send me a link to the full LTS version?
Thank you so much for your help!

@tanersener
Copy link
Owner

I don't have a full LTS dev build. You can build it yourself though.

@shpitsmedia
Copy link

@tanersener This is a bit over my head :) do you know when you are going to release the next release?
Again, thanks a lot for the help!

@tanersener
Copy link
Owner

July 2020.

@shpitsmedia
Copy link

@tanersener Excellent, its just around the corner, thanks again!

@shpitsmedia
Copy link

@tanersener Hi again, we've got a little problem, we're trying to merge 2 mp4 videos using -filter_complex concat but it seems that without libx264 the output video is very low quality, do you have a clue how can it be done without using GPL if even possible?

@tanersener
Copy link
Owner

@shpitsmedia You don't have too many options there. You can try to use vp8 or vp9, but they're slow. Increasing the quality of mpeg4 can be a better option. Can you try using some of the options given here or manually set the profile with something like -profile:v 15.

@shpitsmedia
Copy link

@tanersener mpeg4 works, thank you so much for the help!

@Legacycz
Copy link

Legacycz commented Jul 25, 2020

Hi, I just tried the MobileFFmpeg 4.4 (not the LTS) in Unity 2019.4.5f1 and it works. Many thanks :). You do a great job.

Here is the the code I used in Unity in case someone struggles with it:

using (AndroidJavaClass configClass = new AndroidJavaClass("com.arthenica.mobileffmpeg.Config"))
{
    AndroidJavaObject paramVal = new AndroidJavaClass("com.arthenica.mobileffmpeg.Signal").GetStatic<AndroidJavaObject>("SIGXCPU");
    configClass.CallStatic("ignoreSignal", new object[] { paramVal });

    using (AndroidJavaClass ffmpeg = new AndroidJavaClass("com.arthenica.mobileffmpeg.FFmpeg"))
    {
        cmd = " -i " + <input_path> + <output_path>; // put your ffmpeg command here
        int rc = ffmpeg.CallStatic<int>("execute", new object[] { cmd });
        Debug.Log("MobileFFmpeg result: " + rc);
    }
}

I'm also attaching .unitypackage with dependency solver (Unity project just needs to be set up for it to work) and very rough iOS wrapper. Let's hope it helps.
mobile-ffmpeg-unity.zip

Thanks again and keep up the good work :).

@tanersener
Copy link
Owner

tanersener commented Jul 29, 2020

Fixed and released in v4.4.

@amine2alami
Copy link

amine2alami commented Jan 23, 2021

Hello Everybody,
using the method Execute in Unity freezes the main thread of unity, here is how I solved it using the ExecuteAsync method:

public static void ExecuteAsync(string command) {
using (AndroidJavaClass configClass = new AndroidJavaClass("com.arthenica.mobileffmpeg.Config")){
AndroidJavaObject paramVal = new AndroidJavaClass("com.arthenica.mobileffmpeg.Signal").GetStatic("SIGXCPU");
configClass.CallStatic("ignoreSignal", new object[] { paramVal });
using (AndroidJavaClass ffmpeg = new AndroidJavaClass("com.arthenica.mobileffmpeg.FFmpeg"))
{
ExecuteCallback callback = new ExecuteCallback();
ffmpeg.CallStatic("executeAsync", new object[] { command, callback });
}
}
}

//implement the interface ExecuteCallback as separate class in the same script or own script file
class ExecuteCallback : AndroidJavaProxy{
public ExecuteCallback() : base("com.arthenica.mobileffmpeg.ExecuteCallback") { }
void apply(long executionId, int returnCode) {
Debug.Log(executionId+" "+returnCode);
CreatePuzzle.Instance.handleExecuteResult(returnCode);
}
}

@Spirit30

This comment has been minimized.

@SazCoR
Copy link

SazCoR commented Feb 9, 2021

Hi, Unity gives me an error when I execute this code:

void Start()
{
Debug.Log("Android function is to be called");

    using (AndroidJavaClass configClass = new AndroidJavaClass("com.arthenica.mobileffmpeg.Config"))
    {
        AndroidJavaObject paramVal = new AndroidJavaClass("com.arthenica.mobileffmpeg.Signal").GetStatic<AndroidJavaObject>("SIGXCPU");
        configClass.CallStatic("ignoreSignal", new object[] { paramVal });

    }
    Debug.Log("Android function is called");
}

I have been struggling with this for weeks, trying to download the audio from a youtube video link and use it as audioclip in Unity. For this I used YoutubeExplode at first, and it worked, but it exported the audio in .webm

I need to use mobile-ffmpeg but I have no idea how to use ffmpeg in unity nor converting .webm to mp3 or wav, please I really need help
image

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request fixed
Projects
No open projects
Development

No branches or pull requests

10 participants