在我的 libVLC 上查看 rtsp 流时,出现 1 秒的延迟。 我尝试使用 libVLC 中设置的所有可能选项来减少延迟。
我的主机上有一个 rtsp 服务器,它使用以下代码在 RTSP 中进行流传输
/v4l2rtspserver/v4l2rtspserver -W 320 -H 240 -F 30 -P 8554 $mycam
输出:
[NOTICE] /v4l2rtspserver/main.cpp:269
Version: 0.3.6-3-g233b631 live555 version:2022.10.01
[NOTICE] /v4l2rtspserver/src/V4l2RTSPServer.cpp:37
Create V4L2 Source.../dev/video0
[NOTICE] /v4l2rtspserver/libv4l2cpp/src/V4l2Device.cpp:133
driver:uvcvideo capabilities:84200001 mandatory:4000001
[NOTICE] /v4l2rtspserver/libv4l2cpp/src/V4l2Device.cpp:136
/dev/video0 support capture
[NOTICE] /v4l2rtspserver/libv4l2cpp/src/V4l2Device.cpp:139
/dev/video0 support streaming
[ERROR] /v4l2rtspserver/libv4l2cpp/src/V4l2Device.cpp:212
/dev/video0: Cannot set pixelformat to:H264 format is:YUYV
[NOTICE] /v4l2rtspserver/libv4l2cpp/src/V4l2Device.cpp:225
/dev/video0:MJPG size:320x240 bufferSize:153600
[NOTICE] /v4l2rtspserver/libv4l2cpp/src/V4l2Device.cpp:246
fps:1/30
[NOTICE] /v4l2rtspserver/libv4l2cpp/src/V4l2Device.cpp:247
nbBuffer:0
[NOTICE] /v4l2rtspserver/libv4l2cpp/src/V4l2MmapDevice.cpp:49
Device /dev/video0
[NOTICE] /v4l2rtspserver/libv4l2cpp/src/V4l2MmapDevice.cpp:73
Device /dev/video0 nb buffer:10
[NOTICE] /v4l2rtspserver/src/V4l2RTSPServer.cpp:62
Create Source .../dev/video0
[NOTICE] /v4l2rtspserver/inc/BaseServerMediaSubsession.h:49
format:video/JPEG
[NOTICE] /v4l2rtspserver/inc/V4l2RTSPServer.h:234
Play this stream using the URL "rtsp://XXXXXXX:8554/unicast"
[NOTICE] /v4l2rtspserver/src/V4L2DeviceSource.cpp:96
handleCmd_SETUP:SETUP rtsp://192.168.5.1:8554/unicast/track1 RTSP/1.0
CSeq: 4
User-Agent: LibVLC/4.0.0-dev (LIVE555 Streaming Media v2022.07.14)
Transport: RTP/AVP;unicast;client_port=57666-57667
String rtspStreamUrl;
Resources res = getResources();
rtspStreamUrl = preferences.getString("rtspStreamUrl", getResources().getString(R.string.Default_RtspStreamUrl)) ;
///
ArrayList<String> options = new ArrayList<>();
options.add("-vvv");
options.add("--low-delay");
options.add("--network-caching=100");
//options.add("--file-caching=100");
//options.add("--sub-track=0");
//options.add("--rtsp-tcp");
LibVLC libVLC = new LibVLC(this, options);
// VLC's MediaPlayer
org.videolan.libvlc.MediaPlayer vlcMediaPlayer = new org.videolan.libvlc.MediaPlayer(libVLC);
SurfaceView surfaceView = findViewById(R.id.surface_view);
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
surfaceView.setLayoutParams(params);
vlcMediaPlayer.getVLCVout().setVideoView(surfaceView);
vlcMediaPlayer.getVLCVout().attachViews();
Media media = new Media(libVLC, Uri.parse(rtspStreamUrl));
///
media.setHWDecoderEnabled(true, true);
media.addOption(":network-caching=250");//golden was 250
media.addOption(":clock-jitter=0");//golden was 0
media.addOption(":clock-synchro=0");
media.addOption(":no-dr");
media.addOption(":drop-late-frames");
media.addOption(":skip-frames");
media.addOption(":live-caching=50"); // for example, to set it to 50ms
///
vlcMediaPlayer.setMedia(media);
media.release();
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int screenHeight = metrics.heightPixels;
int screenWidth = metrics.widthPixels;
vlcMediaPlayer.getVLCVout().setWindowSize(screenWidth, screenHeight); // This sets the dimensions VLC will use
vlcMediaPlayer.setAspectRatio("16:9"); // or whatever aspect ratio you want, e.g., "4:3"
vlcMediaPlayer.play();
我有这个版本的库:
implementation 'org.videolan.android:libvlc-all:4.0.0-eap11'
关于如何调试这个问题,或者改变什么来减少延迟,有什么好主意吗?
我尝试在同一设备(Qgroundcontrol)上的单独客户端上可视化流,并且没有明显的延迟。
编辑: 事实证明,从服务器中删除 -F 帧速率选项会减少延迟,但延迟仍然很大,大约 600 毫秒
我注意到 VLC 的第三个版本比第四个版本好得多。您可以尝试安装不同版本并进行比较。 😉