Java – Reduces latency when playing rtp streams with libvlc on Android

Reduces latency when playing rtp streams with libvlc on Android… here is a solution to the problem.

Reduces latency when playing rtp streams with libvlc on Android

I’m using LibVLC version 3.0.0 to play incoming mpeg2ts streams over RTP on Android. The code is as follows:

SurfaceView playerView; Initialized somewhere before    

LibVLC libVlc = new LibVLC(context, arrayListOf("--file-caching=150", "--network-caching=150",
                    "--clock-jitter=0", "--live-caching=150", "--clock-synchro=0",
                    "-vvv", "--drop-late-frames", "--skip-frames"));
MediaPlayer player = new MediaPlayer(libVlc);
IVLCVout vout = player.getVLCVout();
vout.setVideoView(playerView);
vout.attachViews();
Media media = new Media(libVlc, Uri.parse("rtp://@:" + UDP_PORT + "/"));
player.setMedia(media);
player.play();

This plays the stream, but there is a delay of about 2 seconds. I’m sure the latency can be reduced to ~300ms because other players can play it with this delay. What options should I use to reduce this latency? I know I have to trade it for quality at the cost of quality, but what do I do first?

Solution

There is a way to reduce the latency from about 2 seconds to about 200 milliseconds

Workaround:

 ArrayList<String> options = new ArrayList<>();
 options.add("--file-caching=2000");
 options.add("-vvv");

LibVLC mLibVLC = new LibVLC(getApplicationContext(), options);
 MediaPlayer mMediaPlayer =  new MediaPlayer(mLibVLC);

Media media = new Media(mLibVLC, Uri.parse("rtsp://192.168.0.1:1935/myApp/myStream"));
        media.setHWDecoderEnabled(true, false);
        media.addOption(":network-caching=150");
        media.addOption(":clock-jitter=0");
        media.addOption(":clock-synchro=0");

mMediaPlayer.setMedia(media);
 mMediaPlayer.play();

Hope this helps! =)

Related Problems and Solutions