实时 RTSP 流解码:在 Android 上使用 MediaPlayer 时视频延迟较大
我正在从 PC 上的 VLC 播放 Live RTSP 流到 Android MediaPlayer 类(两者都在同一本地网络上)。它播放流畅,没有错误 - 问题是屏幕上的解码视频比实时视频晚大约 5 到 7 秒。
从调试和回调中我可以看到实时数据到达设备<启动mMediaPlayer.prepareAsync()
后1秒。此时 MediaPlayer 类开始计算流的格式、尺寸等。然后,在视频显示在屏幕上之前(5 到 7 秒后),将调用 onPrepared()
,其中我调用mMediaPlayer.start()
。看起来这个 start()
播放了最初从准备阶段开始捕获的视频。
我在 start()
之前和之后都尝试过 seekTo(5000)
,但它对延迟完全没有影响。
对于实时视频通话应用程序来说,几秒钟的设置延迟是完全可以的,但视频呈现后的这种延迟对我来说是无法接受的。
public void surfaceCreated(SurfaceHolder holder)
{
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mMediaPlayer.setOnInfoListener(this);
mMediaPlayer.setOnErrorListener(this);
mMediaPlayer.setOnVideoSizeChangedListener(this);
mMediaPlayer.setOnBufferingUpdateListener(this);
mMediaPlayer.setDataSource("rtsp://192.168.1.4:5544/test");
mMediaPlayer.setDisplay(holder);
mMediaPlayer.setScreenOnWhilePlaying(true);
mMediaPlayer.setOnPreparedListener(this);
mMediaPlayer.setOnCompletionListener(this);
mMediaPlayer.prepareAsync();
...
public void onPrepared(MediaPlayer mediaplayer)
{
mMediaPlayer.start();
...
有什么想法可以减少这种延迟,或者寻求 MediaPlayer 缓冲的内容的结尾吗?我的设备是3.1,minSdkVersion是2.2。
编辑:
我在 AwesomePlayer.cpp 中发现了一些高水位线和低水位线(2s 和 8s)。作为一个快速测试,我破解了 libstagefright.so 来制作这些 0.1 和 1。但这对延迟没有影响。我的搜索仍在继续...
I'm playing a Live RTSP stream from VLC on a PC to Android MediaPlayer class (both on same local network). It plays smoothly with no errors - the problem is that the decoded video on screen is between around 5 and 7 seconds behind live.
From debug and callbacks I can see that the live data is arriving on the device < 1s after starting mMediaPlayer.prepareAsync()
. This is when the MediaPlayer class begins to work out what format the stream is with what dimensions etc. Then just before video is shown on screen (between 5 and 7 seconds later), onPrepared()
is called where I call mMediaPlayer.start()
. It looks like this start()
plays the video that was originally captured from the start of the prepare stage.
I've tried seekTo(5000)
both before and after start()
, but it has no effect on the lag at all.
For a live video calling app, the setup delay of a few seconds is perfectly fine, but this lag once video is presented is unaccaptable to me.
public void surfaceCreated(SurfaceHolder holder)
{
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mMediaPlayer.setOnInfoListener(this);
mMediaPlayer.setOnErrorListener(this);
mMediaPlayer.setOnVideoSizeChangedListener(this);
mMediaPlayer.setOnBufferingUpdateListener(this);
mMediaPlayer.setDataSource("rtsp://192.168.1.4:5544/test");
mMediaPlayer.setDisplay(holder);
mMediaPlayer.setScreenOnWhilePlaying(true);
mMediaPlayer.setOnPreparedListener(this);
mMediaPlayer.setOnCompletionListener(this);
mMediaPlayer.prepareAsync();
...
public void onPrepared(MediaPlayer mediaplayer)
{
mMediaPlayer.start();
...
Any ideas how I can reduce this lag, or seek to the end of what is buffered by MediaPlayer? My device is 3.1, minSdkVersion is 2.2.
EDIT:
I've found some high and low water-marks in AwesomePlayer.cpp (2s and 8s). As a quick test I have hacked the libstagefright.so to make these 0.1s and 1s. This however made no effect on the delay. My search continues...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不会给出最终答案,但让我分享一下我现在所拥有的。
rtspsrc
的latency=0
参数;v4l2src
的is-live=1
参数,当然还有x264enc tune=zerolatency
。无法控制
MediaPlayer
/VideoView
的编解码器/媒体源参数。据我所知,MediaCodec API 中都没有。所以我们需要使用 GStreamer 或 ffmpeg。
易于使用和可移植性还没发现。
I'm not giving a final answer, but let me share what I have by now.
latency=0
parameter ofrtspsrc
;v4l2src
'sis-live=1
parameter and, of course,x264enc tune=zerolatency
.There is no way to control
MediaPlayer
/VideoView
's codec/media source parameters. Neither in MediaCodec API, as far as I see.So we need to go for GStreamer or ffmpeg.
Ease of use and portability are to be found out yet.
我面临着同样的问题。解决这个问题的一种方法是重写整个播放类,以控制输入编解码器的内容。但这将是最后(也是痛苦的)手段。我还在四处张望……叹息……
I'm facing the same problem. One way to dsolve this is to rewrite the whole playback class in order to control what gets fed into the codec. But this would be the last (and painful) resort. I'm still looking around... sigh...
我有完全相同的问题。起初我以为它不起作用,但我忘记了我的应用程序打开了,过了一会儿视频就出现了。
有趣的是,如果我使用我在 VideoView 教程中找到的这个视频[1],延迟就会小得多。我正在考虑安装 Darwin Streaming 服务器来检查这是 VLC 的问题还是其他问题。
[1] rtsp://v5.cache1.c.youtube.com/CjYLENy73wIaLQnhycnrJQ8qmRMYESARFEIJbXYtZ29vZ2xlSARSBXdhdGNoYPj_hYjnq6uUTQw=/0/0/0/video.3gp
I'm having exactly the same problem. At first I thought that it was not working, but I forgot my app opened and after a while the video showed up.
The funny thing is that if I use this video[1] which I found in a tutorial on VideoView, the lag is much smaller. I am thinking on installing Darwin Streaming server to check if it is a matter of VLC or another issue.
[1] rtsp://v5.cache1.c.youtube.com/CjYLENy73wIaLQnhycnrJQ8qmRMYESARFEIJbXYtZ29vZ2xlSARSBXdhdGNoYPj_hYjnq6uUTQw=/0/0/0/video.3gp