Search code examples
javaandroidnetwork-programminghttp-headersmjpeg

Cannot create a MJPEG server in Java/Android


I've been trying to create a MJPEG Server, and for that, I got a simple base server from here. Most of the code is same, just the handle(socket) function is modified. Here is the handle(...) code -

private void handle(Socket socket) {
    try {
        ...
        Read HTTP request... Not needed for MJPEG.
        ...
        // Initial header for M-JPEG.
        String header = "HTTP/1.0 200 OK\r\n" +
                "Connection: close\r\n" +
                "Max-Age: 0\r\n" +
                "Expires: 0\r\n" +
                "Cache-Control: no-store, no-cache, must-revalidate, pre-check=0, post-check=0, max-age=0\r\n" +
                "Pragma: no-cache\r\n" +
                "Content-Type: multipart/x-mixed-replace;boundary=--boundary\r\n\r\n";
        output.write(header.getBytes());

        // Start the loop for sending M-JPEGs
        isStreaming = true;
        if (!socket.isClosed()) {
            new Thread(new Runnable() {
                int id = 1;
                @Override
                public void run() {
                    try {
                        while (isStreaming) {
                            if (id > 2) id = 1;
                            byte[] buffer = loadContent(id + ".jpg");
                            output.write(("--boundary\r\n" +
                                    "Content-Type: image/jpeg\r\n" +
                                    "Content-Length: " + buffer.length + "\r\n").getBytes());
                            output.write(buffer);
                            Thread.sleep(1000);
                            Log.i("Web Server", "Current id: " + id);
                            id++;
                        }
                    } catch (IOException | InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        }
        output.flush();
        output.close();
        reader.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

I got the headers from some websites, and this server works in C++/Linux (I can't directly port the code because in C++, I used QTcpServer of Qt which is a bit different from SocketServer).

There are 2 JPGs in the assets folder and the work of this server is to show them and change between both of them each second.

When I open this site on Google Chrome in laptop, I get just a white screen (happens when the server connects, but doesn't output data properly).

If any other information is needed, please ask for them in the comments and I will edit the question and add them.

Thanks!


Solution

  • Found a solution. Apparently, using a Thread inside a Thread doesn't work. Here is the new code -

    if (!socket.isClosed()) {
        int id = 1
        while (isStreaming) {
            try {
                if (id > 2) id = 1;
                byte[] buffer = loadContent(id + ".jpg");
                assert buffer != null;
                Log.i("Web Server", "Size of image: " + buffer.length + "bytes");
    
                output.write(("--boundary\r\n" +
                              "Content-Type: image/jpeg\r\n" +
                              "Content-Length: " + buffer.length + "\r\n\r\n").getBytes());
                output.write(buffer);
                Thread.sleep(100);
                Log.i("Web Server", "Current id: " + id);
                id++;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }