kokorin / Jaffree

______ Stop the War in Ukraine! _______ Java ffmpeg and ffprobe command-line wrapper
Apache License 2.0
471 stars 80 forks source link

Expose the `pid` to be able to kill the process manually. #383

Open HenriquePassarelli opened 10 months ago

HenriquePassarelli commented 10 months ago

Describe the issue When I lose connection to the internet, the process sometimes doesn't close by itself, so I would like to get the pid for each process I run FFMpeg to close/kill/verify manually.

To Reproduce Steps to reproduce the behavior: While running the RTSP stream, stop the connection with the internet, and you will see that the process will still run even though no one is consuming it.

Expected behavior Close the process after losing connection or add a way to close it.

Additional context Linux Ubuntu 20.04

kokorin commented 10 months ago

Have you checked force stop example?

HenriquePassarelli commented 10 months ago

I'm using it, but even calling it sometimes doesn't stop the process.

kokorin commented 10 months ago

Sometimes? Could you be more specific? Does force stop work in case you described in this issue?

HenriquePassarelli commented 10 months ago

The messages when the force is called, are being shown, however, when I check the processes the ffmpeg execution is still there. I'm running with executeAsync providing the executor.

kokorin commented 10 months ago

Could you post debug logs? For that you need to configure both ffmpeg log level and java log level (logback or whatever you use)

HenriquePassarelli commented 10 months ago

https://github.com/kokorin/Jaffree/assets/71354731/0437e38d-ee25-4ec6-bdcb-28f958375157

https://github.com/kokorin/Jaffree/assets/71354731/c45a64ce-e4d2-4b7f-a347-c8080bedfaa8

HenriquePassarelli commented 10 months ago

I implemented a workaround that reads all ffmpeg processes on my machine and checks the command to see if they match the arguments, if so, I keep the pid and then I can kill it. That's why I wanted the pid exposed to be used or an internal call to it besides canceling the future.

kokorin commented 10 months ago

Please, provide logs as requested.

HenriquePassarelli commented 10 months ago

Here I'm logging the stop call that comes from my code that calls the future forceStop

4380793 INFO [qtp1643944254-970] FFMpegHelper: Kill rtsp jobs - 1 
4380794 INFO [qtp1643944254-970] FFMPEGRunner: STOP FFMPEGRunner
4380794 INFO [qtp1643944254-970] FFMPEGRunner: Future status before - false
4380794 INFO [qtp1643944254-970] FFMPEGRunner: Future status - true

So as you can see the future was cancelled but the process is still running.

My stop method

 public void stop() {
        if (isStopping.getAndSet(true)) return;
        LOGGER.info("STOP FFMPEGRunner");
        isRunning.set(false);
        connections.set(0);
        reference.get().clear();
        if (future != null) {
            LOGGER.info("Future status before - {}", future.isCancelled());
            future.forceStop();
            LOGGER.info("Future status - {}\n\n\n\n\n", future.isCancelled());
        }
        onClose.run();
    }
kokorin commented 9 months ago

Could you post debug logs? For that you need to configure both ffmpeg log level and java log level (logback or whatever you use)

Please, provide logs as requested

HenriquePassarelli commented 9 months ago

The logs aren't providing much.

The basics. A call to stop the process.

stop-call

It had to be closed using the Ubuntu UI ending-process-via-linux-ui

Reloading without connection no action needs to be taken. error-reloading-stream

Common user flow, start and stop stream, working. image

The full log is a not working example. stop-no-connection

The full log is a working example. full-working

HenriquePassarelli commented 9 months ago

I used a workaround to deal with it that could be added to the code, which is the https://github.com/profesorfalken/jProcesses lib to handle to close the process. It is really handy cause it deals with UNIX and Windows systems.

kokorin commented 9 months ago

Please, notice that I asked for debug logs both ffmpeg and Jaffree.

Speiger commented 2 days ago

@kokorin If FFmpeg doesn't abide by the force request of the stopper then you are effectively leaving a process alive even if the JVM shuts down. So exposing the Process instance itself would be nice. Especially if you have a highly multithreaded application that doesn't require a lot of Processing power but a lot of traffic on your Storage drives. Having to go to taskmanager and having to cancel these tasks while the taskmanager isn't responding well because your PC is overloaded for some reason, kinda makes this more important too.

So i highly suggest that there is a way where you can configure a bit more control over it. (Started using the library when i was using the command line directly before because i wanted a nicer interface, and this is the first thing i realized)

@HenriquePassarelli You can solve this yourself temporary, by creating your own FFmpeg/Probe class that extends the original one and implement a custom Stopper interface that just wraps around the existing one. Because that gets the process info :) Meaning you can even do that without async then :)