Best way to live streaming video from a Java server to an Android client?

I have been looking for the best way to cast a PC screen from a Java server to an Android client from a few days, but I cannot find another way for a TCP socket. I want to transfer from a PC to Android .

Now I just send images (frames) through TCP sockets. It works fine, but here are the cons:

  • high bandwidth consumption (unicast across multiple clients)
  • second server change between server and client
  • low frame rate

I tried with a UDP socket, but how to deal with a maximum packet size of 64Ko? The best solution would be UDP with multicast , but I read some problems about this on Android, which is device dependent.

Thanks in advance for your answers!

+4
source share
1 answer

I finally managed to transfer my PC screen through RTP on a Java server

code:

public static void main(String[] args) throws Exception {
    boolean found = new NativeDiscovery().discover();
    System.out.println(found);
    System.out.println(LibVlc.INSTANCE.libvlc_get_version()); 

    String media = "screen://";
    String options = formatRtpStream("230.0.0.1", 5555);

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

    MediaPlayerFactory mediaPlayerFactory = new MediaPlayerFactory(args);
    EmbeddedMediaPlayer mediaPlayer = mediaPlayerFactory.newEmbeddedMediaPlayer();

    Canvas canvas = new Canvas();
    canvas.setBackground(Color.black);
    CanvasVideoSurface videoSurface = mediaPlayerFactory.newVideoSurface(canvas);
    mediaPlayer.setVideoSurface(videoSurface);

    // Creating a JFrame to display stream (duplicate)
    JFrame f = new JFrame("vlcj duplicate output test");
    f.add(canvas);
    f.setSize(800, 600);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true);

    mediaPlayer.playMedia(media,
        options,
        ":no-sout-rtp-sap",
        ":no-sout-standard-sap",
        ":sout-all",
        ":sout-keep"
    );

    // Don't exit
    Thread.currentThread().join();
}

private static String formatRtpStream(String serverAddress, int serverPort) {
    StringBuilder sb = new StringBuilder(60);
    sb.append(":sout=");
    // Transcode with codec (mp4v here), 30 FPS, not resized (scale 1), audio disabled (only video).
    sb.append("#transcode{vcodec=mp4v,fps=30,scale=1,noaudio}");
    // Creating RTP address
    sb.append(":rtp{dst=");
    sb.append(serverAddress);
    sb.append(",port=");
    sb.append(serverPort);
    // Encapsulation method used for the resulting stream, this option has to be set.
    sb.append(",mux=ts}");
    return sb.toString();
} 

Display the current stream in the window:

You can replace this:

sb.append(":rtp{dst=");
sb.append(",mux=ts}");

Thus:

sb.append(":duplicate{dst=display,dst=rtp{dst=");
sb.append(",mux=ts}}");

This way you can see the resulting stream.


Some interesting links:

+3
source

Source: https://habr.com/ru/post/1614241/


All Articles