How do I set up live audio streams to a DLNA compliant device?

87,205

Solution 1

Pulseaudio-DLNA

I created a little server which discovers all upnp renderers in your network and adds them as sinks to pulseaudio. So you can control every application via pavucontrol to play on your upnp devices.

That's the kind of comfort I always wanted when dealing with upnp devices under linux.

The application can be installed from source or DEB package downloadable from git, or after we had added the project's official ppa:qos/pulseaudio-dlna to our sources with:

sudo apt-get update && sudo apt-get install pulseaudio-dlna

We then run pulseaudio-dlna from the command line with following options:

pulseaudio-dlna [--host <host>] [--port <port>] [--encoder <encoder>] [--renderer-urls <urls>] [--debug]
pulseaudio-dlna [-h | --help | --version]

See also the pulseaudio-dlna "About" for more.

When there was a DLNA renderer present we can then select it from the sound menu as an output sink:

enter image description here

Solution 2

Pavucontrol is the missing item in this jigsaw puzzle! I had also set up everything correctly and the external device(LG TV) was showing that the sound was being played but I did not hear any sound. Today I installed pavucontrol and when I opened it I found the option to channel sound through the DLNA server. The DLNA option is only shown when there is sound output from a player to pulseaudio. enter image description here

Solution 3

One idea I had to stream "what I hear" to a DLNA renderer (like WDTV) was to server the stream with VLC as http stream with pulse://alsa_output.xxx.monitor as input and transcode it to MP3 or FLAC. Then I wanted to use some DLNA control point to let the renderer play taht stream. VLC does serve the transcoded stream correctly, but it does not allow to set the mime type, so the renderer refuses to play it.

The next idea was to write a http server in python that serves the stream instead. It gets the audio stream from pulse with parec, encodes it with flac (or lame or whatever you want) and sets the mime type correctly.

It works with the following (very simple) script:

#!/usr/bin/python

import BaseHTTPServer
import SocketServer
import subprocess

PORT = 8080
# run "pactl list short |grep monitor" to see what monitors are available
# you may add a null sink for streaming, so that what is streamed is not played back locally
# add null sink with "pactl load-module module-null-sink"
MONITOR = 'null.monitor'
MIMETYPE = 'audio/flac'
ENCODER = 'flac - -c --channels 2 --bps 16 --sample-rate 44100 --endian little --sign signed'
BUFFER = 65536

class Handler(BaseHTTPServer.BaseHTTPRequestHandler):
def do_HEAD(s):
    print s.client_address, s.path, s.command
    s.send_response(200)
    s.send_header('content-type', MIMETYPE)
    s.end_headers()
def do_GET(s):
    s.do_HEAD()
    pa = subprocess.Popen('parec -d {} | {}'.format(MONITOR, ENCODER), shell = True, bufsize = BUFFER, stdout = subprocess.PIPE)
    while True:
        data = pa.stdout.read(1024)
        if len(data) == 0: break
        s.wfile.write(data)
    print 'stream closed'


httpd = SocketServer.TCPServer(("", PORT), Handler)

print "listening on port", PORT

try:
httpd.serve_forever()
except KeyboardInterrupt:
pass

httpd.server_close()

Adjust the parameters, run it, and point the DLNA renderer to your machine. It worked for me with a WDTV as renderer and an Android phone with BubbleUPnP as control point (You can type in the stream URL when adding a new item to the playlist manually). But it should work with any DLNA compliant devices.

Solution 4

NOTE: This solution works, but a newer and probably better solution has been proposed by Massimo.

Here's an answer for Ubuntu 14.04 (also tested and working on 15.04), for completeness:

  1. install any needed packages: sudo apt-get install rygel rygel-preferences rygel-gst-launch

  2. create the file ~/.config/rygel.conf that contains the following:

    [GstLaunch]
    enabled=true
    title=@REALNAME@'s stream
    launch-items=myaudioflac;myaudiompeg
    
    myaudioflac-title=FLAC audio on @HOSTNAME@
    myaudioflac-mime=audio/flac
    myaudioflac-launch=pulsesrc device=upnp.monitor ! flacenc quality=8
    
    myaudiompeg-title=MPEG audio on @HOSTNAME@
    myaudiompeg-mime=audio/mpeg
    myaudiompeg-launch=pulsesrc device=upnp.monitor ! lamemp3enc target=quality quality=6
    
    [Playbin]
    enabled=true
    title=Audio/Video playback on @REALNAME@'s computer
    
    [general]
    interface=
    upnp-enabled=true
    
    [MediaExport]
    uris=
    
  3. Execute the following commands from the commandline (these could be put into a script if desired):

    pactl load-module module-http-protocol-tcp
    pactl load-module module-rygel-media-server 
    rygel &
    
  4. Run the paprefs command, and ensure that both DLNA options are enabled (checked) on the "Network Server" tab.

  5. Play some audio on your computer. Run the pavucontrol program, and on the "Playback" tab, change the output device to "DLNA/UPnP Streaming".

At this point, you should be able to play the MPEG and FLAC streams from a DLNA client (renderer / control point).

NOTE: you may need to restart your computer (or restart pulse) in order for things to begin to work.

Solution 5

I'm sorry I can't help you with Rygel at all, but there may be an alternative which may work for you.

The principle is get a program to record the stream to an audiofile, then launch miniDLNA with a custom config which points to the directory that stream is in.

Example: Say we're working in ~/stream/. Create ~/stream/minidlna.conf

network_interface=wlan0
media_dir=/home/<user>/stream/
friendly_name=Live Audio Stream
db_dir=/home/<user>/stream/
album_art_names=Cover.jpg/cover.jpg/AlbumArtSmall.jpg/albumartsmall.jpg/AlbumArt.jpg/albumart.jpg/Album.jpg/album.jpg/Folder.jpg/folder.jpg/Thumb.jpg/thumb.jpg
inotify=no
enable_tivo=no
strict_dlna=no
notify_interval=900
serial=12345678
model_number=1

Then save the stream to an audiofile in that directory. Googling for "FFmpeg record sound card audio" yielded this command

ffmpeg -f alsa -i default -acodec flac ~/stream/OutputStream.flac

but I didn't have much luck with it. Another option is vlc is you have a GUI available and this doesn't work.

Then boot up miniDLNA in another terminal window:

minidlna -d -f ~/stream/minidlna.conf -P ~/stream/minidlna.pid

It should locate OutputStream.flac and then be accessible from your network device.

Hopefully if you haven't already got it solved that's given you a few ideas.

Share:
87,205

Related videos on Youtube

Takkat
Author by

Takkat

Updated on September 18, 2022

Comments

  • Takkat
    Takkat over 1 year

    Is there a way to stream the live output of the soundcard from our 12.04.1 LTS amd64 desktop to a DLNA-compliant external device in our network? Selecting media content in shared directories using Rygel, miniDLNA, and uShare is always fine - but so far we completely failed to get a live audio stream to a client via DLNA.

    Pulseaudio claims to have a DLNA/UPnP media server that together with Rygel is supposed to do just this. But we were unable to get it running.

    We followed the steps outlined in live.gnome.org, this answer here, and also in another similar guide.


    In 12.04 LTS we can select the local audio device, or our GST-Launch stream in the DLNA client but Rygel displays the following message and the client states it reached the end of the playlist:

    (rygel:7380): Rygel-WARNING **: rygel-http-request.vala:97: Invalid seek request
    

    There was no way to listen to live audio streams on the client.


    Only after a distribution upgrade to 14.04 LTS we were able to select a live stream on our DLNA renderers from settings nicely outlined in below answer. Still, we needed to select an established stream after we started rygel and were unable to push a new stream to our UPnP devices. Audio metadata were not transmitted.


    Are there any other alternatives for sending the audio of our soundcard as live stream to a DLNA client?

  • Marcus
    Marcus over 11 years
    So, do you mean once you've broken off the ffmpeg process to stop recording, only then is the file recognized by miniDLNA? Also, is the audio quality any better? I'll try it out on my machine and see if I can get it going again. (I had something similar working last year for transcoding movies on the fly)
  • Takkat
    Takkat over 11 years
    Yeah in the first place. Second time I can acess it from the client but it starts from the beginning (i.e. whenever I started recording) and stops in the middle (ie. after exactly the time between start recording and start receiving on the client). Audio is fine now, had to disable duplex.
  • Takkat
    Takkat over 11 years
    Thank you for sharing this - its exactly what so far always failed with me. From this site's design we'd like to encourage you to outlined the steps you took (in addition to just posting the link to your blog) because links may disappear over time thus leaving your answer useless. I'll get back to you as soon as I was able to test this.
  • Takkat
    Takkat over 11 years
    Here's my observations so far: Following your guide crashes Rygel with SEGFAULT when selecting the audio stream from the client in 12.04. In 12.10 we are unable to create a stream with GST-Launch. Anything missing?
  • jdthood
    jdthood over 11 years
    The question was about 12.04 and this answer is for Ubuntu 12.10, otherwise I'd vote it up. :)
  • Takkat
    Takkat over 11 years
    Thank you for sharing this. How did you set up the DLNA server? For me atm Rygel quits with rather unhelpful segfaults.
  • Canastro
    Canastro over 11 years
    I have just normal settings. I followed the same link as you have mentioned in your post.[GstLaunch] enabled=true launch-items=audiotestsrc; audiotestsrc-title=Desktop Live Streaming! audiotestsrc-mime=audio/mpeg audiotestsrc-launch=pulsesrc device=upnp.monitor ! lamemp3enc target=quality quality=6
  • Takkat
    Takkat over 11 years
    Weird. With Rhythmbox the segfaults have disappeared but I still only get Invalid seek request from Rygel. Media directories are there but my GST-stream is always EOF.
  • pastim
    pastim almost 10 years
    I have found a few minor issues with this script.
  • pastim
    pastim almost 10 years
    @Adam - After several trials I found a few minor issues with this program. However, the biggest problem is that streaming stops with error 32 (Broken pipe) after a time that is in direct proportion to the amount of data sent. For a 24/96000 quality stream this is just over an hour. At 24/192000 it is a little over 30 minutes. For CD quality somewhat over 3 hours. By selecting the stream again on the renderer the stream starts again. I believe the solution may be 'chunked encoding'. I wondered if anyone has produced a chunked version.
  • Takkat
    Takkat over 9 years
    Great application - thank you! Worked fine on my devices including a Samsung Smart TV (UE40ES6100). Just a note: we also needed python-requests as a dependency and we could select the renderer from default audio controls - no need to install pavucontrol.
  • Massimo
    Massimo over 9 years
    Glad you like it. I just updated the README. Thanks for the hint!
  • JPT
    JPT almost 9 years
    Great solution. Thanks. But I have one question: There is a playback delay of several seconds (10 secs after pressing pause in VLC). Is this a DLNA "feature" or is there any way to reduce it? So DLNA does not make sense for video playback or gaming? :(
  • datashaman
    datashaman almost 9 years
    Person from the future here: that link has rotted. :P
  • Massimo
    Massimo over 8 years
    The delay comes from filling the HTTP buffer. It keeps the stream playing in case your connection has issues (weak wifi, etc.). If you want to reduce the delay, use a codec which needs much bandwidth (wav) to fill that buffer faster. Cable connection always helps. Otherwise this is very specific to your manufacturers firmware implementation. E.g. i have a delay with Cocy about 1 second. Sonos Play 1 with wav: 1 second, with mp3: 5 seconds. All connected via cable. But you won't get rid of it completely. Main purpose is music and audio books. Everything what does not need to be in sync.
  • dpc.pw
    dpc.pw over 8 years
  • springloaded
    springloaded about 6 years
    Great addition, it lets you set a sink for each app that can play audio! I can listen to music on my big stereo, and keep videos or game sounds on my computer. Thanks!
  • balu
    balu over 5 years
    @JPT and everyone else looking for a way to fix the delay (10s for me): Using shairport-sync I'm running an AirPlay sink on my RaspberryPi in parallel to the DLNA sink and must say that the delays are much shorter (~2s to start up; stopping is immediate). That being said, thanks a lot to Massimo because, forgetting about the delay, pulseaudio-dlna works like a charm and really is trivial to install! (AirPlay was quite a bit harder to set up for me.)
  • easwee
    easwee almost 5 years
    This solved my issue when all was connectedn and playing but just sound was missing!