I am using jna
module to connect two processes that both perform FFMPEG commands. Send SDTOUT
of FFMPEG command on the server side to NampedPipe and receive STDIN
from that NampedPipe for other FFMPEG command on the Client side.
this is how I capture STDOUT
and send into the pipe in server Side:
InputStream out = inputProcess.getInputStream();
byte[] buffer = new byte[maxBufferSize];
while (inputProcess.isAlive()) {
int no = out.available();
if (no > 0 && no > maxBufferSize) {
int n = out.read(buffer, 0,maxBufferSize);
IntByReference lpNumberOfBytesWritten = new IntByReference(maxBufferSize);
Kernel32.INSTANCE.WriteFile(pipe, buffer, buffer.length, lpNumberOfBytesWritten, null);
}
}
And this is how I capture STDIN
and feed it to the Client Side:
OutputStream in = outputProcess.getOutputStream();
while (pipeOpenValue >= 1 && outputProcess.isAlive() && ServerIdState) {
// read from pipe
resp = Kernel32.INSTANCE.ReadFile(handle, readBuffer,readBuffer.length, lpNumberOfBytesRead, null);
// Write to StdIn inputProcess
if (outputProcess != null) {
in.write(readBuffer);
in.flush();
}
// check pipe status
Kernel32.INSTANCE.GetNamedPipeHandleState(handle, null,PipeOpenStatus, null, null, null, 2048);
pipeOpenValue = PipeOpenStatus.getValue();
WinDef.ULONGByReference ServerId = new WinDef.ULONGByReference();
ServerIdState = Kernel32.INSTANCE.GetNamedPipeServerProcessId(handle, ServerId);
}
But I faced two problems:
- High CPU usage due to iterating two loops in Server and Client. (find by profiling resources by VisualVM)
- Slower operation than just connecting two FFMPEG command with regular
|
in command prompt. Speed depends on buffer size but large buffer size blocks operation and small buffer size reduce speed further.
Questions:
- Is there any way not to send and receive in chunks of bytes? Just stream
STDOUT
to the Namedpipe and capture it in Client. (Eliminate two Loops) - If I cant use NampedPipe, is there any other way to Connect two FFMPEG process that runs in different java modules but in the same machine?
Thanks