Skip to content

Instantly share code, notes, and snippets.

@mekya
Created January 30, 2015 04:19
Show Gist options
  • Save mekya/01506107a3076c5fecb5 to your computer and use it in GitHub Desktop.
Save mekya/01506107a3076c5fecb5 to your computer and use it in GitHub Desktop.
GoPro Hero4 Silver - Live Preview on PC (java, ffplay, ffmpeg)
import java.io.IOException;
import java.io.InputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.SocketException;
import java.net.URL;
import java.util.Locale;
public class GoProStreamer {
private static final String CAMERA_IP = "10.5.5.9";
private static int PORT = 8554;
private static DatagramSocket mOutgoingUdpSocket;
private Process streamingProcess;
private KeepAliveThread mKeepAliveThread;
public GoProStreamer() {
try {
mOutgoingUdpSocket = new DatagramSocket();
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void startStreamService() {
HttpURLConnection localConnection = null;
try {
String str = "http://" + CAMERA_IP + "/gp/gpExec?p1=gpStreamA9&c1=restart";
localConnection = (HttpURLConnection) new URL(str).openConnection();
localConnection.addRequestProperty("Cache-Control", "no-cache");
localConnection.setConnectTimeout(5000);
localConnection.setReadTimeout(5000);
int i = localConnection.getResponseCode();
if (i >= 400) {
throw new IOException("sendGET HTTP error " + i);
}
}
catch (Exception e) {
e.printStackTrace();
}
if (localConnection != null) {
localConnection.disconnect();
}
}
private void sendUdpCommand(int paramInt)throws SocketException, IOException
{
Locale localLocale = Locale.US;
Object[] arrayOfObject = new Object[4];
arrayOfObject[0] = Integer.valueOf(0);
arrayOfObject[1] = Integer.valueOf(0);
arrayOfObject[2] = Integer.valueOf(paramInt);
arrayOfObject[3] = Double.valueOf(0.0D);
byte[] arrayOfByte = String.format(localLocale, "_GPHD_:%d:%d:%d:%1f\n", arrayOfObject).getBytes();
String str = CAMERA_IP;
int i = PORT;
DatagramPacket localDatagramPacket = new DatagramPacket(arrayOfByte, arrayOfByte.length, new InetSocketAddress(str, i));
this.mOutgoingUdpSocket.send(localDatagramPacket);
}
private void startStreaming() {
Thread thread = new Thread() {
@Override
public void run() {
try {
streamingProcess = Runtime.getRuntime().exec("ffplay udp://10.5.5.9:8554");
InputStream errorStream = streamingProcess.getErrorStream();
byte[] data = new byte[1024];
int length = 0;
while ((length = errorStream.read(data, 0, data.length)) > 0) {
System.out.println(new String(data, 0, length));
System.out.println(System.currentTimeMillis());
}
} catch (IOException e) {
e.printStackTrace();
}
}
};
thread.start();
}
private void keepAlive() {
mKeepAliveThread = new KeepAliveThread();
mKeepAliveThread.start();
}
class KeepAliveThread extends Thread {
public void run() {
try {
Thread.currentThread().setName("keepalive");
if (mOutgoingUdpSocket == null) {
mOutgoingUdpSocket = new DatagramSocket();
}
while ((!Thread.currentThread().isInterrupted()) && (mOutgoingUdpSocket != null)) {
sendUdpCommand(2);
Thread.sleep(2500L);
System.out.println("keep alive udp");
}
}
catch (SocketException e) {
e.printStackTrace();
}
catch (InterruptedException e) {
e.printStackTrace();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
private void stopStreaming() {
if (streamingProcess != null) {
streamingProcess.destroy();
streamingProcess = null;
}
stopKeepalive();
mOutgoingUdpSocket.disconnect();
mOutgoingUdpSocket.close();
}
private void stopKeepalive() {
if (mKeepAliveThread != null) {
mKeepAliveThread.interrupt();
try {
mKeepAliveThread.join(10L);
mKeepAliveThread = null;
}
catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}
public static void main(String[] args) {
GoProStreamer streamer = new GoProStreamer();
streamer.startStreamService();
streamer.keepAlive();
streamer.startStreaming();
}
}
@incipio-swati
Copy link

Hi
I have included this code for live streaming in Android for Hero4 but Runtime.getRuntime().exec("ffplay udp://10.5.5.9:8554"); gives the following error

W/System.err
java.io.IOException: Error running exec().
Command: [ffplay, udp://10.5.5.9:8554] Working Directory: null Environment: null

04-01 17:36:52.205: W/System.err(21238): at java.lang.ProcessManager.exec(ProcessManager.java:211)
04-01 17:36:52.205: W/System.err(21238):
at java.lang.Runtime.exec(Runtime.java:173)
04-01 17:36:52.205: W/System.err(21238):
at java.lang.Runtime.exec(Runtime.java:246)
04-01 17:36:52.205: W/System.err(21238):
at java.lang.Runtime.exec(Runtime.java:189)
04-01 17:36:52.205: W/System.err(21238):
at org.bluetooth.bledemo.GoProStreamer$1.run(GoProStreamer.java:78)
04-01 17:36:52.205: W/System.err(21238):
Caused by: java.io.IOException: Permission denied
04-01 17:36:52.205: W/System.err(21238):
at java.lang.ProcessManager.exec(Native Method)
04-01 17:36:52.205: W/System.err(21238):
at java.lang.ProcessManager.exec(ProcessManager.java:209)
04-01 17:36:52.205: W/System.err(21238): ... 4 more

Please let me know the fix for this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment