我为mjpeg创建了一个测试服务器,但它只显示第一帧

时间:2015-03-25 04:58:05

标签: streaming video-streaming jpeg mjpeg

我在java中创建了一个服务器,用于通过IP传送MJPEG内容。 下面是代码

package mjpeg;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.Path;


public class MjpegStreamServer {

    private static BufferedReader in ;
    public static void main(String[] args) throws InterruptedException {
        try {
            ServerSocket sSock = new ServerSocket(8080);
            Socket s = sSock.accept();


            in = new BufferedReader(new InputStreamReader(s.getInputStream()));
            PrintWriter out = new PrintWriter(s.getOutputStream());

            // Reader thread
            Thread t = new Thread(new Runnable() {

                @Override
                public void run() {
                    String userInput;
                    try {
                        while ((userInput = in.readLine()) != null) {
                            System.out.println(userInput);
                        }
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                }
            });

            t.start();
            //

            //Writer main thread
            Path path = Paths.get("D:\\Image\\i.jpg");
            byte[] data = Files.readAllBytes(path);
            //data1  => "D:\\Image\\j.jpg



            String response;
            response = "HTTP/1.1 200 OK\r\nContent-Type: multipart/x-mixed-replace; boundary=--myboundary\r\n";
            out.print(response);
            int i = 0;
            int length = data.length;
            // length1 => data1.length

            while (i < 1000) {
                if (i % 2 == 0) {
                    String header = "--myboundary\r\n"
                            + "Content-Type:image/jpeg\r\n" + "Content-Length:"
                            + length + "\r\n\r\n";
                    out.print(header);
                    s.getOutputStream().write(data);
                } else {
                    String header = "--myboundary\r\n"
                            + "Content-Type:image/jpeg\r\n" + "Content-Length:"
                            + length1 + "\r\n\r\n";
                    out.print(header);
                    s.getOutputStream().write(data1);
                }
                out.print("\r\n");
                out.flush();
                i++;
                Thread.sleep(100);

            }
            sSock.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

这将交替发送两个图像, 我用浏览器测试了这个,浏览器只显示第一帧。

记录的输出是

HTTP/1.1 200 OK
Content-Type: multipart/x-mixed-replace; boundary=--myboundary

--myboundary
Content-Type:image/jpeg
Content-Length:71327

<JPEG Data>
--myboundary
Content-Type:image/jpeg
Content-Length:60808

<JPEG Data>

知道这里有什么问题吗?

0 个答案:

没有答案