简体   繁体   English

android 上的低延迟视频播放器

[英]Low latency video player on android

I'd like to be able to stream the video from my webcam to an Android app with a latency below 500ms, on my local network.我希望能够 stream 将视频从我的网络摄像头传输到 Android 应用程序,延迟低于 500 毫秒,在我的本地网络上。

To capture and send the video over the network, I use ffmpeg.为了通过网络捕获和发送视频,我使用 ffmpeg。

ffmpeg -f v4l2 -i /dev/video0 -preset ultrafast -tune zerolatency -vcodec libx264 -an -vf format=yuv420p -f mpegts  udp://192.168.1.155:5000

This command takes the webcam as an input, convert it and send it to a device using the mpegts protocol.此命令将网络摄像头作为输入,将其转换并使用 mpegts 协议将其发送到设备。
This is not a requirement, if another technique could work, I could change the way I send the video.这不是必需的,如果其他技术可行,我可以更改发送视频的方式。

I am able to read the video on another PC from the local network with a latency below 500 ms, using commands like我可以使用以下命令从本地网络以低于 500 毫秒的延迟读取另一台 PC 上的视频

gst-launch-1.0 -v udpsrc port=5000 ! video/mpegts ! tsdemux ! h264parse ! avdec_h264 ! fpsdisplaysink sync=false

or或者

mpv udp://0.0.0.0:5000 --no-cache --untimed --no-demuxer-thread --video-sync=audio --vd-lavc-threads=1 

So it is possible to have this range of latency.所以有可能有这个延迟范围。
I'd like to have the same thing on Android.我想在 Android 上也有同样的东西。

Here are my tries to do that.这是我的尝试。

Exoplayer外层播放器

After looking at the different players available on Android studio, it seems like Exoplayer is the go-to choice.在查看了 Android 工作室提供的不同播放器之后,似乎 Exoplayer 是首选。
I tried different options indicated in the live-streaming documentation, but I always end up with a stream taking seconds to start and with a latency of seconds.我尝试了直播文档中指出的不同选项,但我总是以 stream 开始,需要几秒钟的时间和几秒钟的延迟。
I tried to add a Button to seek to the default position of the windows, but it results in a loading of several seconds.我试图添加一个按钮来寻找 windows 的默认 position,但它会导致加载几秒钟。

DefaultExtractorsFactory extractorsFactory =
                new DefaultExtractorsFactory()
                        .setTsExtractorFlags(DefaultTsPayloadReaderFactory.FLAG_IGNORE_AAC_STREAM);

        player = new SimpleExoPlayer.Builder(this)
                .setMediaSourceFactory(
                        new DefaultMediaSourceFactory(this, extractorsFactory))
                .setLoadControl(new DefaultLoadControl.Builder()
                        .setBufferDurationsMs(DefaultLoadControl.DEFAULT_MIN_BUFFER_MS, DefaultLoadControl.DEFAULT_MAX_BUFFER_MS, 200, 200)
                        .build())
                .build();
        MyPlayerView playerView = findViewById(R.id.player_view);
        // Bind the player to the view.
        playerView.setPlayer(player);
        // Build the media item.
        MediaItem mediaItem = new MediaItem.Builder()
                .setUri(Uri.parse("udp://0.0.0.0:5000"))
                .setLiveMaxOffsetMs(500)
                .setLiveTargetOffsetMs(0)
                .setLiveMinOffsetMs(0)
                .build();
        // Set the media item to be played.
        player.setMediaItem(mediaItem);
        // Prepare the player.
        player.setPlayWhenReady(true);
        player.prepare();
        //player.seekToDefaultPosition();

This issue is about the same issue and the conclusion was that Exoplayer was not fit for this use case.这个问题是关于同一个问题,结论是 Exoplayer 不适合这个用例。

I'll be honest, ultra low-latency like this isn't ExoPlayer's main use-case老实说,像这样的超低延迟并不是 ExoPlayer 的主要用例

Vlc液晶显示器

Another try was to use the Vlc library.另一种尝试是使用 Vlc 库。
But I was unable to have the same low latency stream as with the two previous players with Vlc.但我无法拥有与前两个 Vlc 播放器相同的低延迟 stream。
I tried changing the preferences of Vlc to stream as fast as possible as described here我尝试尽可能快地将Vlc的首选项更改为 stream,如此处所述

Input/Codecs -> x264 preset: ultrafast - zerolatency
Input/Codecs -> Access Module: UDP input
Input/Codecs -> Clock Jitter: 500
Audio: disable audio

I also tried reducing the different buffers.我还尝试减少不同的缓冲区。
However, I still have a latency of more than 1 seconds with that.但是,我仍然有超过 1 秒的延迟。

Gstreamer流媒体

Another try was to create a react-native project to use the different players available here.另一种尝试是创建一个 react-native 项目以使用此处提供的不同播放器。
One player that seemed promising was react-native-gstreamer because it uses gstreamer which is able to stream with low latency (gst-launch command).一个看起来很有前途的播放器是react-native-gstreamer ,因为它使用 gstreamer,它能够以低延迟(gst-launch 命令)进行 stream。
But the library is now outdated.但是图书馆现在已经过时了。

Question问题

There were other tries, but none were successful.还有其他尝试,但没有一个成功。
Is there a problem with one of my approaches?我的一种方法有问题吗?
And if not, Is there a player on Android (that I missed) which is able to achieve low latency stream like gstream or mpv on linux?如果没有,Android 上是否有播放器(我错过了)能够在 linux 上实现低延迟 stream (如 gstream 或 mpv)?

I do not know a native low latency player in Android.我不知道 Android 中的原生低延迟播放器。
However you can use a WebView in Android Studio and use a player in the web.但是,您可以在 Android Studio 中使用WebView并在 web 中使用播放器。
With this solution I streamed the webcam of my pc to my phone (in the local network) with livecam .使用此解决方案,我使用livecam将我的电脑的网络摄像头流式传输到我的手机(在本地网络中)。
They use websockets to transmit the video frame by frame, which is not ideal.他们使用 websocket 逐帧传输视频,这并不理想。 With this method I had 370 ms of latency.使用这种方法,我有 370 毫秒的延迟。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM