Display RTSP Stream from IP Camera using LibVLC on Android

Display RTSP Stream from IP Camera using LibVLC on Android

LibVLC for Android is a library that allows to embed VLC engine on Android application. This tutorial provides example how to display RTSP stream from IP camera using LibVLC on Android application.

First, we need to add LibVLC dependency in the module's build.gradle file.

app/build.gradle

dependencies {
    // Other dependencies
    // ...
    implementation 'org.videolan.android:libvlc-all:3.4.4'
}

Request the INTERNET permission in the manifest file because application should have Internet access.

app/src/main/AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.app">
 
    <uses-permission android:name="android.permission.INTERNET" />
 
    <application>
        ...
    </application>
 
</manifest>

Open the layout XML file and add a VLCVideoLayout that will be used to display RTSP stream from IP camera.

app/src/main/res/layout/activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <org.videolan.libvlc.util.VLCVideoLayout
        android:id="@+id/videoLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</RelativeLayout>

When the activity starts, the RTSP stream from the IP camera is captured and displayed using media player. Make sure you have changed RTSP URL of your IP camera. Manufacturers might use different RTSP URLs. We used Reolink E1 Pro camera for testing. The network-caching option can be minimized to reduce the delay of RTSP stream coming from an IP camera. If you're set network-caching to low, then stream capture can freeze. So try to use various values and adjust this option to your mobile device.

When the activity enters a stopped state, the media player is stopped too and a video layout is detached from the player. Resources are released when the activity is destroyed.

app/src/main/java/com/example/app/MainActivity.java

package com.example.app;

import android.net.Uri;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import org.videolan.libvlc.LibVLC;
import org.videolan.libvlc.Media;
import org.videolan.libvlc.MediaPlayer;
import org.videolan.libvlc.util.VLCVideoLayout;

public class MainActivity extends AppCompatActivity
{
    private static final String url = "rtsp://user:pass@192.168.0.9:554/h264Preview_01_main";

    private LibVLC libVlc;
    private MediaPlayer mediaPlayer;
    private VLCVideoLayout videoLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        libVlc = new LibVLC(this);
        mediaPlayer = new MediaPlayer(libVlc);
        videoLayout = findViewById(R.id.videoLayout);
    }

    @Override
    protected void onStart()
    {
        super.onStart();

        mediaPlayer.attachViews(videoLayout, null, false, false);

        Media media = new Media(libVlc, Uri.parse(url));
        media.setHWDecoderEnabled(true, false);
        media.addOption(":network-caching=600");

        mediaPlayer.setMedia(media);
        media.release();
        mediaPlayer.play();
    }

    @Override
    protected void onStop()
    {
        super.onStop();

        mediaPlayer.stop();
        mediaPlayer.detachViews();
    }

    @Override
    protected void onDestroy()
    {
        super.onDestroy();

        mediaPlayer.release();
        libVlc.release();
    }
}

app/src/main/java/com/example/app/MainActivity.kt

package com.example.app

import android.net.Uri
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import org.videolan.libvlc.LibVLC
import org.videolan.libvlc.Media
import org.videolan.libvlc.MediaPlayer
import org.videolan.libvlc.util.VLCVideoLayout

class MainActivity : AppCompatActivity()
{
    private var url: String = "rtsp://user:pass@192.168.0.9:554/h264Preview_01_main"

    private lateinit var libVlc: LibVLC
    private lateinit var mediaPlayer: MediaPlayer
    private lateinit var videoLayout: VLCVideoLayout

    override fun onCreate(savedInstanceState: Bundle?)
    {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        libVlc = LibVLC(this)
        mediaPlayer = MediaPlayer(libVlc)
        videoLayout = findViewById(R.id.videoLayout)
    }

    override fun onStart()
    {
        super.onStart()

        mediaPlayer.attachViews(videoLayout, null, false, false)

        val media = Media(libVlc, Uri.parse(url))
        media.setHWDecoderEnabled(true, false)
        media.addOption(":network-caching=600")

        mediaPlayer.media = media
        media.release()
        mediaPlayer.play()
    }

    override fun onStop()
    {
        super.onStop()

        mediaPlayer.stop()
        mediaPlayer.detachViews()
    }

    override fun onDestroy()
    {
        super.onDestroy()

        mediaPlayer.release()
        libVlc.release()
    }
}

The 16 Comments Found

  1. Avatar
    Matniss Reply

    Hi, this tutorial is great. I'm Android Studio beginner, could you please teach me how to write this tutorial in Java? Thank you.

  2. Avatar
    ‫محمد أبو عاصي‬‎ Reply

    Best library and best explanation I've ever seen Thank you from the bottom of my heart

  3. Avatar
    Matniss Reply

    Hi, me again lol, I have a question, is it possible to add a progress dialog with showing message "Video is buffering... Please wait" when the video streaming is buffering? If possible could u please teach me how to do it?

    • Avatar
      lindevs Reply

      Hi, Matniss
      I created separate post how display a progress dialog when the RTSP stream is buffering in VLC player.

  4. Avatar
    Davide Rivera Reply

    Hi, nice tutorial. Could you explain me how can I use the library for playing/recording video? Thank you for you attention

  5. Avatar
    Juan Garcia Reply

    Hi, nice tutorial! I have a question, When i generated signed apk the size is 160mb aprox. Is too much, what can i do for to lose weight?

    • Avatar
      lindevs Reply

      Hi, Juan
      LibVLC for Android library uses code that is written in C++. As a result, APK contains the native libraries such as libvlc.so (~36-46 MB) for each Application Binary Interface (ABI) such as armeabi-v7a, arm64-v8a, x86, and x86_64. To minimize APK size, you can specify ABIs you want to support in the app/build.gradle file:

      android {
          defaultConfig {
              // ...
              ndk {
                  abiFilters "armeabi-v7a", "arm64-v8a"
              }
          }
      }
  6. Avatar
    boeun nam Reply

    Hi,
    Your program sources and explanations were very helpful.
    There is a delay of about 1 to 2 seconds with Android Studio emulation.
    Which one will reduce the screen latency?

    • Avatar
      lindevs Reply

      Hi,
      Delay depends on various factors such as IP camera, network speed, mobile device, etc. You can try to minimize the network-caching option. Also, I recommend testing with real device.

  7. Avatar
    Anil Reply

    HI Can you please tell me why its taking to much time when we call below
    mediaPlayer.stop()
    mediaPlayer.detachViews()

    its taking to much time to stop media player .

    • Avatar
      lindevs Reply

      Hi,
      I tested on mobile device. To stop media player takes about 40 milliseconds. Can you test on multiple devices? Maybe you are using Android emulator?

Leave a Comment

Cancel reply

Your email address will not be published.