noson-app icon indicating copy to clipboard operation
noson-app copied to clipboard

Desktop audio from pulseaudio to Playback has High Latency

Open maxsei opened this issue 4 years ago • 18 comments

When I run the application and I am playing a video on youtube for example, the audio is delayed by few seconds. I do not mind a small amount of latency but the time in between skipping from one part of an audio stream to another is far from negligible.

OS: Ubuntu 18.04.3 LTS x86_64 pulseaudio version: pulseaudio 11.1 noson version: noson 4.2.5.snap

maxsei avatar Jan 26 '20 17:01 maxsei

That is normal. The audio stream needs to be processed by the app and your sonos device. This feature is designed to cast music, not to watch a video program.

janbar avatar Jan 30 '20 19:01 janbar

Hello I start using the app last week and have the same problem. Since i have no computer speaker, the function for video is necessary. But i can understand it is not a priority since it works so great for music.

OS : fedora 32

elfiMD avatar May 15 '20 22:05 elfiMD

The latency offset option is missing in case of noson pulse device:

obrazek

Vitexus avatar Oct 07 '20 13:10 Vitexus

There is no solution to synchronize the video exposed by a video player on your computer and the audio played by a remote device like Sonos player. The two parts cannot be synchronized. A sync of video and audio is built in a single system or a particular system doing for that.

janbar avatar Oct 19 '20 14:10 janbar

There is most certainly a way to reduce latency dramatically, though. Right now there's around ~8 seconds of latency, thanks to a number of insanely huge buffers in the pulse->flac pipeline. For example, change #define SAMPLES 1024 here:

https://github.com/janbar/noson/blob/c63343f95cece5f462d812ac66141f16cab20610/noson/src/flacencoder.cpp#L24

into #define SAMPLES 128, and you'll already see a dramatic improvement. There are two additional buffers I saw that could probably be dramatically reduced as well.

Aside from the harder question of video synchronization, just getting latency around a second or less would be a significant improvement in terms of basic usability.

zx2c4 avatar Mar 07 '21 21:03 zx2c4

I will try to reduce the buffer size or just wait for 10% loaded.

janbar avatar Mar 10 '21 09:03 janbar

@zx2c4 , the value of SAMPLES isn't linked with a buffer size. It is the max size of chunk of data I could send to the Flac encoder. The value of 1024 samples is 1/40 second, so a very short period. Also I checked my code and there is no "blocking" buffer. The only thing that could get a lag should be the chunk size of the HTTP response sent to the Sonos device: for now 16KB, and so ~1/4 of second. Here the culprit seems to be the Sonos device which request many data chunk before starting to play the stream.

If you tried with 128 for SAMPLES, and seen a dramatic improvement, there is probably something relating with the Flac decoder in the Sonos device ... to be checked.

janbar avatar Mar 10 '21 20:03 janbar

I refactored the code around the FLAC encoder used to pulse. I found a way to reduce the lag and I succeeded to keep less than 1 seconde. The root cause of this issue is there was no buffer ! My code push-pull the data without buffering, and seems that causes a lag from the client (Sonos device) on startup. Probably it needs a minimal set of frame and it computes its lag on the initial timeout to retrieve first frames.

janbar avatar Mar 14 '21 13:03 janbar

An update of the app including changes for the pulse streamer will be available from my PPA, in couple of hours.

https://launchpad.net/~jlbarriere68/+archive/ubuntu/noson-app

The LAG is very short because I manage 1 seconde of blank on startup to limit output rate and let the device to wake up. Then I disable the mute and send full stream...

janbar avatar Mar 19 '21 21:03 janbar

Amazing! Thank you for working out all the nitty gritty details of this.

zx2c4 avatar Mar 19 '21 21:03 zx2c4

Hi I'm currently building this on raspbian to create a media center with some sonos (so I can't check the PPA). Does your fix been merged ? because I tested today and everything works perfectly fine, but I still got the latency described in this issue..

AlphaBril avatar May 18 '21 17:05 AlphaBril

@AlphaBril , the latenty is less than before but still few seconds we cannot handle: the Sonos device use an internal buffer.

janbar avatar May 23 '21 18:05 janbar

In my first test I had a latency of 10s (9s of internal buffer), luckily its down now to 2.5/3

The options that came to mind for further reducing it:

  • Not sure if this would work, but if the sound is streamed with higher bitrate/quality the buffer might fill up faster and it couldn't hold as much "time" as it does now.
  • I assume multiple Sonos speakers share the sound with very low latency, so we could pretend to be a speaker and use their communication interface. Obviously that would be a lot of work to reverse engineer and to implement.

WuerfelDev avatar Oct 20 '21 18:10 WuerfelDev

Do you know how e.g. apple w/ airplay manages to synchronize videos w/ the sound? Because if I watch a video on a mac, and stream the audio to airplay, it is in sync

AntonOellerer avatar Feb 02 '22 10:02 AntonOellerer

I'd be also interesting how the Sonos Port product handles that, and if it suffers from the same issue. Anyone has experience on that?

albertschulz avatar Apr 03 '22 10:04 albertschulz

In the past I have used VLC with the Audio Track Synchronisation settings. Once you have the values its pretty easy to setup again.

Screenshot from 2022-06-08 15-32-33

rickhewes avatar Jun 08 '22 14:06 rickhewes

I wonder if there's any way to query the audio lag of the pipeline somewhere? At least for Bluetooth audio, this seems to be a solved problem and vlc syncs its video to the BT audio lag of a wireless headphone, so there must be some infrastructure handling this alreay.

hzulla avatar Oct 06 '22 08:10 hzulla

(for the record, it's ca. 9s delay with vlc & noson on my system. I find it quite difficult to find the proper sync value for this by hand and would be grateful for any mechanism that automates this.)

hzulla avatar Oct 06 '22 08:10 hzulla