masmu / pulseaudio-dlna

A lightweight streaming server which brings DLNA / UPNP and Chromecast support to PulseAudio and Linux
GNU General Public License v3.0
1.25k stars 162 forks source link

Kodi renderer not working with FLAC encoder #157

Closed arigit closed 8 years ago

arigit commented 8 years ago

I think this is a long standing issue with pulseaudio-dlna Using the current pulseaudio-dlna GIT master as of 20160130; Kodi 15.2 (via OSMC on raspberry)

Opening a ticket for tracking - hoping this is acceptable.

In my usecase, my linux desktop hosts high def audio flac files (96/24 and the like) and I use a diskless/headless raspberry with a hifiberry DAC as DLNA renderer. When using "WAV" as encoder, I get audio output but the raspberry chokes (even for regular CD quality FLACs) and the end result is awful. When streaming using FLAC, the setup works great.

Since pulseaudio-dlna can stream FLAC to some proprietary boxes, hoping that open-source Kodi can be supported too :)

masmu commented 8 years ago

Well Kodi is tested to work with mp3, ogg, wav and l16. Did you try some of the other codecs?

It is not realistic that one guy implements compatibility with all devices of all manufacturers, even when I try to do so. If this is about quality: Your audio data is encoded to a specific codec anyway, even when you try to stream flac files with the flac encoder. There will be quality loss, because the audio is recorded by pulseaudio and it is using raw audio internally.

arigit commented 8 years ago

Yes - the other codecs work fine. The problem is that with the only lossless option that works (wav), the result is awful for high-bit-rate flacs, due to wifi/network limitations - and that issue is solved by FLAC (I know b/c I streamed flacs for hours on the same setup with zero issues). My audio is stored lossless, using pulseaudio set to passthru, so no quality loss in my setup.

Not expecting a quick fix and the issue might be in Kodi for all I know. I would hope though that people would log their findings here, if anyone figures this out. Kodi being open source and so popular on linux, and pulseaudio-dlna being awesome - eventually more audiophiles will run into this

masmu commented 8 years ago

l16 works? Since l16 is pretty similiar to wav it is also lossless.

How did you set pulseaudio to passthru?

sevachka commented 8 years ago

hey, I just wanted to enter this conversation: I think this is an issue with Kodi. Because it used to work before I have upgraded to Kodi 15 (and now 16) it run without issues using --encoder flac. @masmu : or did you change something about "flac" within the last 3-4 releases? :) Anyways: I love this software. Thank you so much for this easy-to-use piece of software.

masmu commented 8 years ago

I am not quite sure which version of Kodi was tested by myself. It was not 16, I guess 15 or 14 and flac did not work for me. If this is not a missing header issue there is in the end not much what you can do. I take what flac produces and send this to Kodi. Since a missing header issue is unlikely (all the other codecs work) I am currently guessing that this is a problem of Kodi's flac decoding capabilities.

@sevachka : I did not changed anything to the codecs stuff in quite a while, but you never know for sure if there are any side effects of the other stuff.

arigit commented 8 years ago

@masmu: I should have used another term rather than passthru, sorry - you can configure pulseaudio to prevent downsampling of 96/24 and/or 192/24 audio (multiple guides for this out there), but it's true that for guaranteed bitperfect audio you need to get rid of pulseaudio (this is what I did my my OSMC/Kodi/24-bit DAC-based renderer). Clearly as pulseaudio-dlna users we want pulseaudio in our linux desktop, and I guess that's why we are here discussing how to avoid an unnecessary additional stage of lossy encoding+decoding :)

RE LPCM encoding: it works with Kodi, but I have the same issue as with WAV: too much bandwidth required for a wifi-based setup to handle reliably. FLAC streaming works fine on Wireless N wifi even for 96/24 streams but WAV/L16 for 44/16 is more than what Wireless N can handle - continuous audio cuts.

I've been playing with Rygel but at the moment (Fedora 23 version and also its git master) is completely broken for Kodi.

If any log or packet trace would help here I'd be happy to provide!

masmu commented 8 years ago

@arigit : But pulseaudio uses raw audio internally. So even if you have your files in the appropriate format, as soon as you play them, pulseaudio handles them internally as raw audio. Until that point there is no pulseaudio-dlna involved. This is how pulseaudio works. Thats why you receive raw audio from pulseaudio if you record from it. And you always have to encode raw audio into a common format before sending it to the devices. There is no way around that. So, even if you have your files in flac and want them to be sent as flac they will be decoded and encoded. From that point of view, if you are using a cable connection and bandwidth is not a problem, there is nothing wrong with wav. It is lossless and the encoding process is fast.

The other question is: Why is Kodi not able to handle the flac stream?

arigit commented 8 years ago

Thanks Massimo. I undestand that an additional stage of encoding - decoding is mandatory; the point is to try and use FLAC - lossless codec - to prevent further audio signal degradation.

From the discussions in the Rygel list, many people confirmed that FLAC and MP3 DLNA streaming was working fine in the past with Kodi; recent changes in Kodi 15 and 16 broke Rygel streaming completely for all encoders. Those changes did not impact pulseaudio-dlna though, so different issue here. This at least proves the point that Kodi supports FLAC for DLNA streams in principle.

I will capture logs and traces for FLAC (non-working) and MP3 (working) later and attach them here. Maybe the logs/traces will point to where the problem could be

sevachka commented 8 years ago

I want to add another fact to this discussion: when I send audio as "flac", no playback will start with KODI. When I stop the stream (by CTRL-X), all of sudden the playback with Kodi is starting.... for a short period of time (like 30-60 seconds).

arigit commented 8 years ago

Logs for reference:

Successful mp3 encoding: https://paste.gnome.org/pyi5d3bai

Failed flac encoding: https://paste.gnome.org/pfxbysc3m

So i tried to pull the stream manually using wget:

wget 192.168.1.2:8080/dWRuPSJ1dWlkOjczMjc3NWI0LTFkNDktMWExMC1hZDE5LTQ1MzMwODgzMzgwYSIsdHlwZT0iYnJpZGdlIg%3D%3D/stream.flac

With the FLAC stream, I got an invalid stream!! small 400KB file after 10 seconds of capturing, all media players (Audacious, Totem, VLC) complain that the format is invalid.

With the MP3 stream I did the same thing:

wget 192.168.1.2:8080/dWRuPSJ1dWlkOjczMjc3NWI0LTFkNDktMWExMC1hZDE5LTQ1MzMwODgzMzgwYSIsdHlwZT0iYnJpZGdlIg%3D%3D/stream.mp3

It also captured a file, about the same size after 10 seconds of capturing it... and it played perfectly on all media players!

I notice: the flac stream capture should be much bigger in size that the MP3, so indeed it seems something is wrong with it. Looks like pulseaudio-dlna is telling "FLAC" but is streaming something else - or something wrong.

I uploaded the FLAC file I captured here so you can see there is something wrong with it

http://filebin.ca/2WAwXt5bIODh/stream.flac

masmu commented 8 years ago

@arigit I looked into your stream.flac. It is a flac file, but it is missing its header. I am pretty sure this comes from a feature in the streamserver.py which tries to launch just one encoder per request url. Imaging you have 3 devices and all of them want to play the same stream, why should you launch 3 encoders basically all of them doing the same work. There should be one encoder which data is sent to all 3 devices. That feature aimed for performance reasons in case pulseaudio-dlna supports groups of devices some day. So far so good.

The problem with that feature is, that most encoders write a header at the beginning of the encoding process. But just one of those 3 devices would receive that header, if you share the encoders data between the streams. The 2 others will receive a headerless stream. I guess you started pulseaudio-dlna and after start up you took the stream url from the logs and downloaded it via wget. Since, the first stream was the stream initiated by your device the stream downloaded by wget was a headerless one.

The lame encoder is special in one regard. It never write a header when piping data to it, since by design it does this at the end of a file, which is problematic with live streams. :smile: So, mp3 streams are always headerless at the moment. I guess, because of the popularity of mp3 all the decoders out there do a pretty good job of being able to play those files without a proper header. Well, this might be different with flac decoders.

So, all of this is good to know, but Kodi should be able to play the flac stream, since there are no other devices out there which could consume the first stream with the header in it, right? Maybe not. I remember when playing with Kodi that it launched multiple requests for the same stream when instructing it to play. When I remember correctly this was because of bad design. They needed the same information in different Kodi modules and since there was no possibility to share those they simply started multiple requests, where in the end just one was launched for receiving the actual stream, all the others were closed pretty much instantly after receiving the http header. (I think there were 3 GET requests). So, in the end the actual stream request receives a headerless stream and may not able to play that without the header.

I created a branch where there is always a new encoder launched for those encoders which write a header. Could somebody test it?

masmu commented 8 years ago

Just tested it with Kodi 15.1. flac works for me with this.

sevachka commented 8 years ago

now it is working. It got a delay of 2-5 seconds before it starts/ changes ... but it is working. :) Most likely I do not hear a difference in sound quality. Although I do have a really good old-school HiFi with Canton Stereos.

Thank you! :) Do you prefer beer or wine? ;)

arigit commented 8 years ago

I can confirm that with updated streamserver.py and encoders.py, FLAC streaming works like a champ with Kodi. Tested with Raspberry/OSMC (Kodi on ARM) and also Kodi 15.2 on Fedora 23 (official package). Once this is merged to master... one more check to add in the client compatibility table!! Thank you Massimo!!!

arigit commented 8 years ago

I can also confirm that now music flows-through losslessly from my linux desktop to raspberry > kodi > amped speakers at 1/3 the average bitrate of wav-encoder and works perfectly over Wireless-N with zero drops. Fantastic. As good as it gets. Thanks again :)

masmu commented 8 years ago

Glad that it works, you are welcome. @sevachka : beer :wink:

sevachka commented 8 years ago

@masmu Bier soll es sein! :) Thank you again for this software. It´s VERY useful and, what I like the most about it, compatible with a lot of endpoints.

arigit commented 8 years ago

Wondering if there is a blocker to merge "ffmpeg-backend". Updated my audio system to kodi 16, and used the master branch of pulseaudio-dlna on a new install of fedora rawhide - and ran into this problem. Thanks!

masmu commented 8 years ago

Well it is merged in current master... But there is no new release yet.

masmu commented 8 years ago
jbonigomes commented 7 years ago

@arigit I am trying to get PulseAudio to work with my Chromecast audio device, same set up as you (PI3 + OSMC) but don't know how to. I am really new to the PI world, since you seem to have had good progress, perhaps you'd be able to point me in the right direction?

arigit commented 7 years ago

I use Kodi's own DLNA capabilities in the RPi2/OSMC, I just enabled UPNP in it. http://kodi.wiki/view/UPnP/Client Nothing else other than enabling this in Kodi.

This makes the RPi kodi discoverable by UPNP control points (e.g. a linux desktop setup appropriately with pulseaudio-dlna), and you can send a stream to it

jbonigomes commented 7 years ago

Alright, I see, thanks very much for that. I am on a Mac, would you recommend I use something other than PulseAudio-dlna?