[mythtv] A/V Sync Performance & Alsa buffer

Tim Pletcher pletchtd at gmail.com
Fri Jan 17 00:38:35 UTC 2020


In prior discussions on the topic of av sync performance on master, I have
mentioned that I experience significant jitter on a low end Intel machine
using VAAPI when viewing 1080i 29.97fps source material with
deinterlacing.  When looking at the playback, timestamp logs, this
variability seems to be driven by variability in the Audiotimestamp
results. As a consequence, I have worked with Mark K to tune the amount of
filtering of the AV sync measurement to achieve acceptable performance.

While I had always chalked this up to a consequence of using a low powered
machine, I have remained curious why I see the variability in the
audio timestamp.  In digging into the audio chain code, I saw that the
audiotimestamp is ultimately calculated based on the results of the alsa
function snd_pcm_delay().  My understanding is that the results of this
function call reports athe number of audio frames to audibility based on
what is in the buffer already and the known downstream processing latencies
reported by the underlying hardware driver.  There is an interaction
between the emptying of the buffer via DMA, buffer refilling, and buffer
underrun that can induce variability in the results.

Going back my log files, I had always seen this reported in my frontend log
over different versions of mythtv:
      audio/audiooutputalsa.cpp:236 (IncPreallocBufferSize) ALSA: Try to
manually increase audio buffer with: echo 128 | sudo tee
/proc/asound/card0/pcm3p/sub0/prealloc

I am using an alsa:hdmi device and never bothered to properly address this
with the required init script to ensure the value was set upon booting.  On
a hunch with better understanding of the potential buffer impact on the
audiotimestamp values, I set increased the prealloc value as requested.  Lo
and behold, the timestamp variability essentially disappeared.

Before adjusting prealloc
Audioadjust results with /proc/asound/card0/pcm3p/sub0/prealloc value of 64
<https://imgur.com/DXQ6cQF>

After adjusting prealloc
Audioadjust results with /proc/asound/card0/pcm3p/sub0/prealloc value of 128
<https://imgur.com/1tEgS3s>

With the updated alsa buffer value, the required amount of filtering of the
audioadjust value in the sync calculations is much less in my case and the
filtering could be reduced to speed up the AV sync loop. I still find it
acceptably fast to reach sync on my systems but I throw this out as an
option anyway.

In retrospect, I would hear an occasional audio dropout that was probably a
buffer underrun but this was rare and the audio playback was never bad per
se.  It is unfortunate that the need to tune this prealloc value is
highlighted through a single line in the log file. Additionally, updating
prealloc with a persistent value also takes some work to set up a script
and a systemd service.  Are there any better ways to automate this or work
around that would be more seamless for a less technical user?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mythtv.org/pipermail/mythtv-dev/attachments/20200116/0a9f03d9/attachment.htm>


More information about the mythtv-dev mailing list