I am using libvlc to access an RTSP Stream (MJPEG format) from a network camera in a local network (rtsp://192.168.0.51:8554/MCDE3000).
The below code works fine for Internet RTSP Streams (H.264 Format).
My Kotlin code:
class VLCStreamActivity : AppCompatActivity() {
private lateinit var vlcVideoLayout: VLCVideoLayout
private var libVLC: LibVLC? = null
private var mediaPlayer: MediaPlayer? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_vlcstream)
// Initialize VLCVideoLayout
vlcVideoLayout = findViewById(R.id.vlc_video_layout)
// Initialize LibVLC
val options = arrayListOf(
"-vv", // Enable verbose logging
"--rtsp-frame-buffer-size=2000", // Adjust buffer size if necessary
"--network-caching=1500"
)
libVLC = LibVLC(this, options)
// Initialize MediaPlayer
mediaPlayer = MediaPlayer(libVLC).apply {
attachViews(
vlcVideoLayout,
null,
false,
false
)
}
// Play RTSP Stream
val media = Media(libVLC, Uri.parse("rtsp://192.168.0.51:8554/MCDE3000")).apply {
setHWDecoderEnabled(true, false) // Enable hardware decoding
addOption(":network-caching=3000") // Increase network caching to 3 seconds
addOption(":rtsp-tcp") // Force RTSP over TCP
addOption(":rtsp-timeout=60") // Extend the RTSP timeout to 60 seconds
addOption(":live-caching=3000") // Additional caching for live streams
addOption(":rtsp-frame-buffer-size=5000") // Increase buffer size
addOption(":demux=mjpeg") // Specify MJPEG as the demuxer
}
mediaPlayer?.media = media
media.release()
try {
mediaPlayer?.play()
} catch (e: Exception) {
Log.e("RTSPStreamActivity", "Error playing RTSP stream", e)
Toast.makeText(this, "Unable to play the stream.", Toast.LENGTH_SHORT).show()
}
}
}
Of course, INTERNET permission was enabled and the IP address was added in network_security_config.xml.
The log shows that the camera is successfully connected: input: rtsp://192.168.0.51:8554/MCDE3000' successfully opened
However, after 10 seconds, I received the error message: libvlc access: no data received in 10s, aborting
Full log:
2024-12-05 10:10:34.574 26275-26336 VLC com.example.androidwatchapp D [b40000748e7e4890/66e0] libvlc access: play start: 0.000000 stop:0.000000
2024-12-05 10:10:34.574 26275-26336 VLC com.example.androidwatchapp D [00000073cf553790/66e0] libvlc generic: using access module "live555"
2024-12-05 10:10:34.574 26275-26336 VLC com.example.androidwatchapp D [00000073cf5532e0/66e0] libvlc generic: looking for video decoder module matching "any": 15 candidates
2024-12-05 10:10:34.574 26275-26336 VLC com.example.androidwatchapp D [b4000074ee7f4280/66e0] libvlc decoder: using ffmpeg Lavc60.31.102
2024-12-05 10:10:34.574 26275-26336 VLC com.example.androidwatchapp D [b4000074ee7f4280/66e0] libvlc decoder: CPU flags: 0x00000160
2024-12-05 10:10:34.575 26275-26336 VLC com.example.androidwatchapp D [b4000074ee7f4280/66e0] libvlc decoder: allowing 5 thread(s) for decoding
2024-12-05 10:10:34.575 26275-26336 VLC com.example.androidwatchapp D [b4000074ee7f4280/66e0] libvlc decoder: codec (mjpeg) started
2024-12-05 10:10:34.575 26275-26336 VLC com.example.androidwatchapp D [00000073cf5532e0/66e0] libvlc generic: using video decoder module "avcodec"
2024-12-05 10:10:34.575 26275-26336 VLC com.example.androidwatchapp D [b40000751e7ebcf0/66e0] libvlc input: ES track selected: 'video/auto/0' (fourcc: 'MJPG')
2024-12-05 10:10:34.575 26275-26336 VLC com.example.androidwatchapp D [00000073cf5539b0/66e0] libvlc generic: looking for meta reader module matching "any": 2 candidates
2024-12-05 10:10:34.575 26275-26336 VLC com.example.androidwatchapp D [b40000741e7ff380/66e0] libvlc demux meta: Trying Lua scripts in /data/user/0/com.example.androidwatchapp/app_vlc/.share/lua/meta/reader
2024-12-05 10:10:34.575 26275-26336 VLC com.example.androidwatchapp D [00000073cf5539b0/66e0] libvlc generic: no meta reader modules matched with name any
2024-12-05 10:10:34.575 26275-26336 VLC com.example.androidwatchapp D [b40000751e7ebcf0/66e0] libvlc input: `rtsp://192.168.0.51:8554/MCDE3000' successfully opened
2024-12-05 10:10:45.113 26275-26336 VLC com.example.androidwatchapp E [b40000748e7e4890/66e0] libvlc access: no data received in 10s, aborting
2024-12-05 10:10:45.113 26275-26336 VLC com.example.androidwatchapp D [b40000751e7ebcf0/66e0] libvlc input: EOF reached
2024-12-05 10:10:45.113 26275-26336 VLC com.example.androidwatchapp D [b4000074ee7f4280/66e0] libvlc decoder: killing decoder fourcc `MJPG'
2024-12-05 10:10:45.113 26275-26336 VLC com.example.androidwatchapp D [b4000074ee7f4280/66e0] libvlc decoder: removing "video decoder" module "avcodec"
2024-12-05 10:10:45.114 26275-26336 VLC com.example.androidwatchapp D [b40000751e7ebcf0/66e0] libvlc input: ES track unselected: 'video/auto/0' (fourcc: 'MJPG')
2024-12-05 10:10:45.114 26275-26336 VLC com.example.androidwatchapp D [b40000748e7e4890/66e0] libvlc access: removing "access" module "live555"
2024-12-05 10:10:45.114 26275-26327 VLC-std com.example.androidwatchapp E Sending request: TEARDOWN rtsp://192.168.0.51:8554/MCDE3000/ RTSP/1.0
CSeq: 6
User-Agent: LibVLC/4.0.0-dev (LIVE555 Streaming Media v2022.07.14)
Session: -67682726
2024-12-05 10:10:45.114 26275-26336 VLC com.example.androidwatchapp D [b40000751e7ebcf0/66e0] libvlc input: ES track deleted: 'video/auto/0' (fourcc: 'MJPG')
2024-12-05 10:10:45.114 26275-26336 VLC com.example.androidwatchapp D [b40000751e7ebcf0/66e0] libvlc input: Program doesn't contain anymore ES
2024-12-05 10:10:45.114 26275-26333 VLC com.example.androidwatchapp D [b40000751e7ebcf0/66dd] libvlc input: destroying input for 'rtsp://192.168.0.51:8554/MCDE3000'
2