Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Process Listener API #413

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/main/java/com/github/kokorin/jaffree/ffmpeg/FFmpeg.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.github.kokorin.jaffree.process.LoggingStdReader;
import com.github.kokorin.jaffree.process.ProcessHandler;
import com.github.kokorin.jaffree.process.ProcessHelper;
import com.github.kokorin.jaffree.process.ProcessListener;
import com.github.kokorin.jaffree.process.StdReader;
import com.github.kokorin.jaffree.process.Stopper;
import org.slf4j.Logger;
Expand Down Expand Up @@ -50,6 +51,7 @@ public class FFmpeg {
private boolean overwriteOutput;
private ProgressListener progressListener;
private OutputListener outputListener;
private ProcessListener processListener;
private String progress;
//-filter_threads nb_threads (global)
//-debug_ts (global)
Expand Down Expand Up @@ -344,6 +346,17 @@ public FFmpeg setOutputListener(final OutputListener outputListener) {
return this;
}

/**
* Supply a ProcessListener to receive access to the Active Process instance of FFmpeg.
* <p>
* Providing you self control on how the processes are kept alive or tracked.
* Since the commandline doesn't guarantee to listen to shutdown commands.
*/
public FFmpeg setProcessListener(final ProcessListener processListener) {
this.processListener = processListener;
return this;
}

/**
* Send program-friendly progress information to url.
* <p>
Expand Down Expand Up @@ -506,6 +519,7 @@ protected ProcessHandler<FFmpegResult> createProcessHandler() {
.setStdErrReader(createStdErrReader(outputListener))
.setStdOutReader(createStdOutReader())
.setHelpers(helpers)
.setProcessListener(processListener)
.setArguments(buildArguments());
if (executorTimeoutMillis != null) {
processHandler.setExecutorTimeoutMillis(executorTimeoutMillis);
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/com/github/kokorin/jaffree/ffprobe/FFprobe.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.github.kokorin.jaffree.ffprobe.data.FormatParser;
import com.github.kokorin.jaffree.ffprobe.data.JsonFormatParser;
import com.github.kokorin.jaffree.process.ProcessHandler;
import com.github.kokorin.jaffree.process.ProcessListener;
import com.github.kokorin.jaffree.process.ProcessHelper;
import com.github.kokorin.jaffree.process.StdReader;

Expand Down Expand Up @@ -68,6 +69,7 @@ public class FFprobe {
private String format;
private Input input;

private ProcessListener processListener;
private FormatParser formatParser = new JsonFormatParser();

private final Path executable;
Expand Down Expand Up @@ -472,6 +474,17 @@ public FFprobe setInput(final Input input) {
return this;
}

/**
* Supply a ProcessListener to receive access to the Active Process instance of FFprobe.
* <p>
* Providing you self control on how the processes are kept alive or tracked.
* Since the commandline doesn't guarantee to listen to shutdown commands.
*/
public FFprobe setProcessListener(final ProcessListener processListener) {
this.processListener = processListener;
return this;
}

/**
* Sets ffprobe output format parser (and corresponding output format).
* <p>
Expand Down Expand Up @@ -545,6 +558,7 @@ public FFprobeResult execute() {
.setStdOutReader(createStdOutReader(formatParser))
.setStdErrReader(createStdErrReader())
.setHelpers(helpers)
.setProcessListener(processListener)
.setArguments(buildArguments())
.execute();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public class ProcessHandler<T> {
private StdReader<T> stdOutReader = new GobblingStdReader<>();
private StdReader<T> stdErrReader = new GobblingStdReader<>();
private List<ProcessHelper> helpers = null;
private ProcessListener listener = null;
private Stopper stopper = null;
private List<String> arguments = Collections.emptyList();
private int executorTimeoutMillis = DEFAULT_EXECUTOR_TIMEOUT_MILLIS;
Expand Down Expand Up @@ -99,6 +100,17 @@ public synchronized ProcessHandler<T> setHelpers(final List<ProcessHelper> helpe
return this;
}

/**
* Sets a {@link ProcessListener Listener} that gets called when a process gets started or stopped.
*
* @param listener the listener
* @return this
*/
public synchronized ProcessHandler<T> setProcessListener(final ProcessListener listener) {
this.listener = listener;
return this;
}

/**
* Sets {@link Stopper} which can be used to interrupt program execution.
*
Expand Down Expand Up @@ -158,13 +170,19 @@ public synchronized T execute() {
if (stopper != null) {
stopper.setProcess(process);
}
if(listener != null) {
listener.onProcessStart(process);
}

return interactWithProcess(process);
} catch (IOException e) {
collectDebugInformation();
throw new JaffreeException("Failed to start process.", e);
} finally {
if (process != null) {
if(listener != null) {
listener.onProcessStop(process);
}
process.destroy();
// Process must be destroyed before closing streams, can't use
// try-with-resources, as resources are closing when leaving try block,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.github.kokorin.jaffree.process;

/**
* @Author Speiger
*/
public class ProcessListener {

/**
* Callback for when the Process is started.
* @param process the started process
*/
public void onProcessStart(Process process);
/**
* Callback for when the Process is stopped for whatever reason.
* @param process the stopped process
*/
public void onProcessStop(Process process);
}
Loading