I'm invoking linphonec.exe, an interactive console application for controlling Linphone, an open source Skype-like application via Java (1.8u11) on Windows 7.
Everything worked fine in Linphone 3.8.2, however after updating to 3.9.1, I can no longer reliably invoke commands from Java into the application, some commands are processed, some are silently ignored.
Small example to recreate, issuing the help command..
public class CommandTest {
private static class Consumer implements Runnable {
private InputStream stream;
public Consumer(InputStream stream) {
this.stream = stream;
}
@Override
public void run() {
int c = 0;
try {
while ((c = stream.read()) != -1) {
System.out.print((char) c);
}
System.out.println("Stream finish");
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) throws Exception {
ProcessBuilder builder = new ProcessBuilder(
Arrays.asList("C:\\Program Files (x86)\\Linphone\\bin\\linphonec.exe"));
builder.redirectInput(Redirect.PIPE);
Process process = builder.start();
new Thread(new Consumer(process.getInputStream())).start();
new Thread(new Consumer(process.getErrorStream())).start();
BufferedWriter stdin = new BufferedWriter(new OutputStreamWriter(process.getOutputStream(), StandardCharsets.US_ASCII));
stdin.write("help\n");
stdin.flush();
process.waitFor();
}
}
Output - with 3.8.2
Ready
Warning: video is disabled in linphonec, use -V or -C or -D to enable.
linphonec> Commands are:
---------------------------
help Print commands help.
call Call a SIP uri or number
... etc...
Output - with 3.9.1
Ready
linphonec> linphonec>
Analysis
Problem narrowed down to the output from linphonec.exe, it seems that it is responding to commands, but those are not getting flushed to its standard out promptly
For example, the following does work, but you only get a result after a number of help commands have run, I can only assume this means the output buffer of the child process has reached a certain level in which case a flush occurs automatically
Code
stdin.write("webcam list\n");
stdin.flush();
Thread.sleep(100);
stdin.write("webcam list\n");
stdin.flush();
Thread.sleep(100);
stdin.write("webcam list\n");
stdin.flush();
Thread.sleep(100);
stdin.write("webcam list\n");
stdin.flush();
Thread.sleep(100);
stdin.write("help\n");
stdin.flush();
Thread.sleep(100);
stdin.write("help\n");
stdin.flush();
Thread.sleep(100);
stdin.write("help\n");
stdin.flush();
Thread.sleep(100);
stdin.write("help\n");
stdin.flush();
Output
linphonec> linphonec> linphonec> linphonec> linphonec> linphonec> linphonec> linphonec> Warning: video is disabled in linphonec, use -V or -C or -D to enable.
0: StaticImage: Static picture
0: StaticImage: Static picture
0: StaticImage: Static picture
0: StaticImage: Static picture
Commands are:
---------------------------
help Print commands help.
answer Answer a call
autoanswer Show/set auto-answer mode
call Call a SIP uri or number
- This conclusion is supported by fact the line "Warning: video is disabled in linphonec, use -V or -C or -D to enable." is only printed later, even though it is written in the main init() method before the main loop is even started. This line appears instantantly in 3.8.2, but only after the stdout buffer is flushed in 3.9.1
Analysis of linphone source
Most outputs, like help etc all use linphonec_out in linphonec.c, this has an explicit fflush() but does not appear to work...
char *res;
va_list args;
va_start (args, fmt);
res=ortp_strdup_vprintf(fmt,args);
va_end (args);
printf("%s",res);
fflush(stdout);
Questions
- Why is flush occuring immediately in 3.8.2 but not in 3.9.1
- Am I doing something obviously wrong in my Java
- Anyone have any success integrating with Java using this approach and recent versions of Linphone Desktop...