Reputation: 114
First of all, I want to point that there is not much examples how to read rtsp stream from cameras.
So far I made working rtsp stream reading using vlcj which was quite hard, because of many problem.
To begin with creating app reading rtsp stream I needed to download VLC in version 2.1.2 which was important in my case. Next this was adding two dependencies
vlcj library in version 3.9.0:
<dependency>
<groupId>uk.co.caprica</groupId>
<artifactId>vlcj</artifactId>
<version>3.9.0</version>
</dependency>
And then I had to downgrade JNA version to version 3.5.2 so:
<dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna</artifactId>
<version>3.5.2</version>
</dependency>
With this configuration I managed to read rtsp stream with this code:
public static void main(final String[] args) {
/* import .dll libraries */
NativeLibrary.addSearchPath(RuntimeUtil.getLibVlcLibraryName(), "C:\\Program Files\\VideoLAN\\VLC");
Native.loadLibrary(RuntimeUtil.getLibVlcLibraryName(), LibVlc.class);
/* rtsp stream url */
String mrl = "rtsp://192.168.1.1:5555/h264";
String[] options = { ":network-caching=400" };
/* getting MediaPlayer */
MediaPlayerFactory mediaPlayerFactory = new MediaPlayerFactory(args);
HeadlessMediaPlayer mediaPlayer = mediaPlayerFactory.newHeadlessMediaPlayer();
mediaPlayer.playMedia(mrl, options);
/* infinite loop keeping stream reading alive */
while (true) {
}
}
So far I used simple HeadlessMediaPlayer
just for tests.
The problem I met is about huge delay between camera and my stream, because it's about 1s, which is not good. Using VLC I could set delay for 300-500ms and it was really nice, but as you see in here String[] options = {":network-caching=400"};
my caching is set to 400. But it doesn't change a lot. Stream has too big delay.
Anyone fixed that?
Another question is about making this rtsp stream into http stream so maybe I could use it in JavaFX, because there is a media player but it does not support rtsp stream, only HTTP.
Upvotes: 0
Views: 1861
Reputation: 4146
It is not always clear or consistent how those switches/options should be used.
You should always enable and check the native log to see if the option was actually applied or not.
You could try:
String[] options = {"--network-caching", "400"};
Sometimes those options will work on the playMedia call, but other times they must be set when you create your MediaPlayerFactory.
Also, generally speaking don't use a tight loop like while(true)
, use Thread.currentThread().join()
instead.
Upvotes: 1