Mpv: Unable to reach low latencies when reproducing live streams.

Created on 9 Mar 2017  Â·  16Comments  Â·  Source: mpv-player/mpv

mpv version and platform

mpv 0.24.0 (C) 2000-2017 mpv/MPlayer/mplayer2 projects
built on Sun Feb 12 12:54:08 CET 2017
ffmpeg library versions:
libavutil 55.34.101
libavcodec 57.64.101
libavformat 57.56.101
libswscale 4.2.100
libavfilter 6.65.100
libswresample 2.3.100
ffmpeg version: 3.2.4

On Archlinux X86_64

Reproduction steps

1# cat /tmp/file.sdp
SDP:
v=0
o=- 0 0 IN IP4 127.0.0.1
s=No Name
c=IN IP4 127.0.0.1
t=0 0
a=tool:libavformat 57.56.101
m=video 6000 RTP/AVP 96
a=rtpmap:96 H264/90000
a=fmtp:96 packetization-mode=1

1# fmpeg -f x11grab -s 1280x720 -framerate 30 -i :0.0  -c:v libx264 -preset veryfast -tune zerolatency -pix_fmt yuv444p  -x264opts crf=20:vbv-maxrate=3000:vbv-bufsize=100:intra-refresh=1:slice-max-size=1500:keyint=30:ref=1 -ab 24k -ar 22050 -bsf:v h264_mp4toannexb -f mpegts udp://127.0.0.1:12345

2# mpv udp://127.0.0.1:12345 --no-cache --untimed --no-correct-pts --fps=9999 --speed=100

3# mplayer -benchmark udp://127.0.0.1:12345

...and notice that mplayer reproduces the stream with almost no lag (i'd say 10-20ms), while mpv introduces about 500ms delay.

Most helpful comment

This should be enough to reduce the latency in the playback chain: mpv udp://127.0.0.1:12345 --no-cache --untimed --no-demuxer-thread --video-sync=audio --vd-lavc-threads=1

There's a forced latency by 1 frame, which can be removed with this: http://sprunge.us/fJVJ

Everything else is added by the decoder or what's before that.

mplayer seems to behave worse with either ffmpeg://udp://... or -demuxer lavf, so that's where you have to look.

All 16 comments

Not sure. Can latency be reduced by changing the cache settings when --no-cache is enabled? Is --demuxer-readahead-secs relevant?

Thanks, but unfortunately it does not.

This should be enough to reduce the latency in the playback chain: mpv udp://127.0.0.1:12345 --no-cache --untimed --no-demuxer-thread --video-sync=audio --vd-lavc-threads=1

There's a forced latency by 1 frame, which can be removed with this: http://sprunge.us/fJVJ

Everything else is added by the decoder or what's before that.

mplayer seems to behave worse with either ffmpeg://udp://... or -demuxer lavf, so that's where you have to look.

With that options, it seems slightly better, but still far from "real time".
Thanks, i'll try other codecs too.

@wm4

There's a forced latency by 1 frame, which can be removed with this: http://sprunge.us/fJVJ

When I follow this link I get error "This application is temporarily over its serving quota. Please try again later.". After few days the link still does not work.

Does forced latency by 1 frame still exists? If it does could you please share again what was necessary to remove it?

I don't remember now, but i did everything suggested, and was never been able to cut the latency as much as i wanted.
The best tool i've found is still ffplay.
I use the following for the audio (i guess you've to remove -nodisp for video), with AUDIO_DELAY_COMPENSATION between 0 and 9000.
Latency on localhost (reading from stdin) is almost zero, but it still depends on the codec.
In that regard, it seems that opus performs better than others.

ffplay $PLAYURL -nostats -flags low_delay -nodisp -probesize 32 -fflags nobuffer+fastseek+flush_packets -analyzeduration 0 -sync ext -af aresample=async=1:min_comp=0.1:first_pts=$AUDIO_DELAY_COMPENSATION

Thank you for quick answer. For me the following command:

ffplay -codec:v mjpeg_cuvid -rtsp_transport udp rtsp://172.16.1.104/substream -nostats -flags low_delay -probesize 32 -fflags nobuffer+fastseek+flush_packets -analyzeduration 0 -sync ext -af aresample=async=1:min_comp=0.1:first_pts=0

produced 344ms latency. I do not have audio in video stream but removing audio related options did not improve the latency. I could not find a way to use vdpau with ffplay but at least I managed to get hardware decoding working with proper -codec:v argument otherwise latency was worse.

I have experimented with mpv a bit more and with the following command:

mpv --video-aspect 4:3 --profile=low-latency --no-cache --untimed --rtsp-transport=tcp rtsp://172.16.1.104/substream

I've got 148ms latency (30 FPS video in both cases).

To better understand what is happening I decided to try MJPEG@1FPS. And I discovered 0 frames delay. But if I use H.265 there is 1 frame delay. However, I cannot use MJPEG to reduce latency because even at low quality it uses so much bandwidth that H.265 with 1 frame delay (33ms at 30FPS) is similar in terms of latency and uses less bandwidth.

EDIT: Previously I have used --no-correct-pts --fps=1000 options to play frames as fast as possible so mpv would not wait up to 33ms (at 30 FPS) after receiving another frame, but if --untimed option is used, this is not necessary. I also have tried --opengl-glfinish=yes --opengl-swapinterval=0 options but they did not have any measurable effect on latency in my case. --video-aspect option is only useful for anamorphic video, on modern hardware it should not have any effect on latency.

It seems that my camera has built-in 100ms delay, mpv cannot do anything about it. But this is just my guess, I'm not sure about this since I did not test with localhost RTSP stream.

Anyway, the point is that it seems mpv does not have forced 1 frame latency anymore (I assume it would remain with any codec if it was still there). Why H.265 has it, I'm not sure yet, it adds more than ¼ of total latency I have left. However it is already good enough for my application so I'm posting this mostly in the hope that it will be useful to others (for me this github issue was on top in Google search about mpv latency).

Super useful thread.

I just tried the latest mpv command. mpv --video-aspect 4:3 --profile=low-latency --no-cache --untimed --no-demuxer-thread --vd-lavc-threads=1 udp://192.168.1.145:1234 which does not seem to recognize --profile=low-latency though.

Got the following error: Unknown profile 'low-latency'.
Any clue as of which (new) flag to use??

Yeah, ~there is no~ older versions might not have the built-in low-latency profile, as far as I know. You'd have to ask the commenter what's part of this specific profile.

The is a low-latency profile: https://mpv.io/manual/stable/#low-latency-playback
Are you using an old version? You can check the available profiles with --show-profile.

Well my mpv --version is 0.27.2

and doing a $ mvp --show-prolile=low-latency or even a $ mvp -profile=low-latency

Returns the same error Unknown profile 'low-latency'.

On Fri, Jan 3, 2020 at 12:00 AM bitraid notifications@github.com wrote:

The is a low-latency profile:
https://mpv.io/manual/stable/#low-latency-playback
Are you using an old version? You can check the available profiles with
--show-profile.

—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
https://github.com/mpv-player/mpv/issues/4213?email_source=notifications&email_token=AOAH7DC7LB22CMZ4QVLBKCTQ33WBZA5CNFSM4DC7IV4KYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEIARDNQ#issuecomment-570495414,
or unsubscribe
https://github.com/notifications/unsubscribe-auth/AOAH7DCP4FJY3LTFH7DSYLDQ33WBZANCNFSM4DC7IV4A
.

Your version is very old. Current version is 0.30.0.

% mpv --show-profile=low-latency
Profile low-latency: 
 audio-buffer=0
 vd-lavc-threads=1
 cache-pause=no
 demuxer-lavf-o-add=fflags=+nobuffer
 demuxer-lavf-probe-info=nostreams
 demuxer-lavf-analyzeduration=0.1
 video-sync=audio
 interpolation=no
 video-latency-hacks=yes

You can try to use all of above options instead of using low-latency profile (just add "--" in front of each option) or try upgrading mpv.

current version is 0.31.0

For the record:
$ sudo apt-get install mpv was telling me I was up to date (even though I
was at 0.27).

I had to do this
http://tipsonubuntu.com/2018/07/27/install-mpv-0-29-ubuntu-18-04-lts/ to
get the newest version. Namely
$ sudo add-apt-repository ppa:mc3man/mpv-tests
$ sudo apt-get install mpv

On Fri, Jan 3, 2020 at 10:41 AM der richter notifications@github.com
wrote:

current version is 0.31.0

—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
https://github.com/mpv-player/mpv/issues/4213?email_source=notifications&email_token=AOAH7DC7MRLAROXYFZOV4WTQ36BEBA5CNFSM4DC7IV4KYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEIBZMBA#issuecomment-570660356,
or unsubscribe
https://github.com/notifications/unsubscribe-auth/AOAH7DAZJ4UTTUAL755I233Q36BEBANCNFSM4DC7IV4A
.

$ sudo apt-get install mpv was telling me I was up to date (even though I was at 0.27)

Fairly typical.

untimed really makes a difference, thanks, now video latency is almost zero.
however i noticed that it uses a lot of cpu and produces a lot of heat that way.
I didn't bothered in the first place, thinking it was normal, but today i tried an ancient 0.27 version on an old atom and the cpu use is normal there.
Is this expected?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

olivergondza picture olivergondza  Â·  3Comments

paper9oll picture paper9oll  Â·  3Comments

laichiaheng picture laichiaheng  Â·  3Comments

ghost picture ghost  Â·  3Comments

lightonflux picture lightonflux  Â·  4Comments