mercredi 23 novembre 2016

Redirecting Online IPCam stream to localhost port in Java

I am able to get video streams from IP Cams using VLCJ in Java. The problem here is that I am developing a Java solution for an embedded solution and I would prefer not to carry around too much extra baggage with the app.

So, I've tried to open a connection and redirect it to a port on the localhost. With this I am able to download/open any kind of file, using curl or opening the localhost url on the browser; no problem at all. However, when I use the URL for an IPCam stream, it doesn't open on VLC and it only shows characters on the browser. If I do curl and redirect it to a file, I only get 1 second of video, even if I download 60MB of data...

Here's the source code I'm doing in Java:

public class HttpVideoRedirect{

    static InputStream videoContent = null;

    /**
     * @param args
     * @throws IOException
     */
    public static void main(String[] args) throws IOException {

        Thread videoProvider =
                new Thread(new Runnable(){
                    public void run(){
                        int c=0;
                        try {           
                            int r;
                            byte[] buf = new byte[1024];
                            ServerSocket serverSocket = null;
                            Socket clientSocket = null;
                            serverSocket = new ServerSocket(4444);
                            clientSocket = serverSocket.accept();

                            if(clientSocket.isConnected())
                                System.out.println("Connected to client");
                            OutputStream out = clientSocket.getOutputStream();

                            while( (r=videoContent.read(buf) ) > -1 ){
                                System.out.print(".");
                                c+=r;
//                              if(clientSocket.isConnected())
//                                  System.out.println("Still connected");
//                              else
//                                  System.out.println("Socket disconnected");
                                out.write(buf, 0, r );
                            }
                            clientSocket.close();
                            serverSocket.close();

                        } catch(SocketException se){
                            System.err.println("Socket exception");
                            System.err.println(se.getMessage());
                            se.printStackTrace();

                        } catch (IOException e) {
                            System.err.println("Accept failed.");
                            System.err.println(e.getMessage());
                            e.printStackTrace();
                        }
                        finally{
                            System.out.println("Read "+c+"B");
                        }
                    }
                });

        //videoContent = new URL("http://ift.tt/2fqzRcJ").openStream();
        //videoContent = new URL("http://ift.tt/2gjHkcR").openStream();
        //URL vidURL = new URL("http://ift.tt/2fquPNs");
        //URL vidURL = new URL("http://ift.tt/2fqzRcJ");
        URL vidURL = new URL("http://ift.tt/2gjHCjT");

        //URL vidURL = new URL("http://ift.tt/2fquPNs");
        HttpURLConnection conn = (HttpURLConnection) vidURL.openConnection();
        videoContent = conn.getInputStream();

        System.out.println("Content type: "+conn.getContentType());
        System.out.println("Content length: "+conn.getContentLengthLong());


        videoProvider.start();

        try {
            videoProvider.join();
        } catch (InterruptedException e) {
            System.out.println(e.getMessage());     
        }
    }
}

EDIT: The content type for the connection is: multipart/x-mixed-replace;boundary=--myboundary

When I open in firefox, the first lines displayed as text in the browser window is: --myboundary Content-Type: image/jpeg

Aucun commentaire:

Enregistrer un commentaire