使用VLCJ的视频流

时间:2017-02-18 21:50:58

标签: java sockets video-streaming

您好我一直在使用vlcj来传输视频。我的服务器能够使用http流式传输视频。如何创建客户端程序来捕获流视频和dsiplay?

Server_main.java

public class Server_main {

public static InetAddress Server_Address;
public static int port;
public static int maxqueue=10;
public static int sleep;

public static void main(String args[]) throws Exception
{
    Connection c=new Connection();
    Server_Address=c.getAddress();
    port=c.getPort();
    ServerSocket server =new ServerSocket(port,maxqueue,Server_Address);
    System.out.println("server started");
    Socket client;
    while(true)
    {
        client=server.accept();
        NewThread newThread = new NewThread(client);

    }
}

static class NewThread implements Runnable
{
    Socket client;
    Thread t;


    NewThread(Socket client)
    {
        this.client=client;
        t=new Thread(this);
        t.start();
    }
    @Override
    public void run() {
        try
        {
            String path=Recieve_data(client);
            //send_data(client,new File(path));
            playmedia(path);
        }
        catch(Exception e)
        {

        }
    }

   private void send_data(Socket client, File file) throws IOException {
            OutputStream os=client.getOutputStream();
            OutputStreamWriter osw = new OutputStreamWriter(os);
            BufferedWriter bw = new BufferedWriter(osw);
            System.out.println(file.getPath());
            bw.write(file.getPath());

    }
    private String Recieve_data(Socket client) throws IOException {
        InputStream is=client.getInputStream();
        InputStreamReader isr = new InputStreamReader(is);
        BufferedReader br = new BufferedReader(isr);
        String path = br.readLine();
        System.out.println(path);
        return path;
    }
}

public static void playmedia(String path)    
{
    String media = path;
    String options = formatHttpStream("127.0.0.1", 5555);

    System.out.println("Streaming '" + media + "' to '" + options + "'");

    MediaPlayerFactory mediaPlayerFactory = new MediaPlayerFactory(path);
    HeadlessMediaPlayer mediaPlayer = mediaPlayerFactory.newHeadlessMediaPlayer();
    mediaPlayer.playMedia(media, options);

}
private static String formatHttpStream(String serverAddress, int serverPort) {
    StringBuilder sb = new StringBuilder(60);
    sb.append(":sout=#duplicate{dst=std{access=http,mux=ts,");
    sb.append("dst=");
    sb.append(serverAddress);
    sb.append(':');
    sb.append(serverPort);
    sb.append("}}");
    return sb.toString();
}

}

任何人都可以解释如何做客户端代码吗?

0 个答案:

没有答案