For a long time I used VLC as video player, until I got bored by the fact that it froze after a while and had to be terminated using the operating system monitor. That was the reason why I tried out MPV, which is nearer to the (in)famous ffmpeg video library (used by almost every web-browser). But it was the same, even worse, MPV froze after only two videos.
Now that made me think whether these are really video player bugs. Mostly I use my own Java file-browser to select and start videos. Maybe the problem are not the players and the lots of clips I pass to them, but some Java restriction on started processes that freeze them? So I began to search on the web and found hints:
- https://stackoverflow.com/questions/37924632/java-process-running-from-windows-powershell-sometimes-randomly-stops-until-pres
- https://stackoverflow.com/questions/16983372/why-does-process-hang-if-the-parent-does-not-consume-stdout-stderr-in-java
Looks like you have to ALWAYS read away any process output! This would also explain why MPV blocked faster than VLC, because the first writes lots of logging to the console.
Here is the Java code of my bug-fix. Mind that this dismisses any error or normal output of the process. First, how to start a process from Java:
final String[] command = .... final String[] environment = .... final File workingDirectory = .... final Process p = Runtime.getRuntime().exec(command, environment, workingDirectory); readAwayProcessOutput(p.getInputStream()); readAwayProcessOutput(p.getErrorStream());
Mind that getInputStream() is the meaning how seen from the process starter perspective,
i.e. the stream delivers what the started process sends to the starter.
The getOutputStream() method is for writing to the started process.
Here is how to read away input-streams, whatever they deliver:
private void readAwayProcessOutput(InputStream in) { new Thread(new Runnable() { @Override public void run() { try { while (in.read() != -1) // -1 is end of stream ; in.close(); } catch (IOException e) { throw new RuntimeException(e); } } }).start(); }
This blocking bevaviour is something that is not documented so explicitly that a programmer would see it. Read it here:
It says:
....may cause the subprocess to block, or even deadlock.
Can we call it a communication problem:-?

Keine Kommentare:
Kommentar veröffentlichen