Broadcasting audio from ubuntu via HTTP (aka stream audio out to sonos on ubuntu)

This post is for all the folks who, like me, have tried (and failed out of frustration) 20 different times to stream local audio from an ubuntu machine to some other thing on the network via HTTP.  Today my goal was to get the Sonos in the office to stream from my machine — aka whatever was playing on my machine should come out of sonos.  I finally sat down and got it done with icecast and darkice today.  The basics for this article come from here.

OK, so without further ado, the basics:

sudo apt-get install icecast2 darkice
sudo nano /etc/default/icecast2

Change ENABLE=false to ENABLE=true.  The reason this is false by default is that icecast is actually a community radio streaming thing that is intended for multiple people to use.  Therefore it has some passwords in the default configuration file (/etc/icecast2/icecast.xml) that they want you to change before turning the thing on at first.  Since this is just us using icecast and we’re all on the local network, I chose to go with simplicity and defaults here.

Now, create a new config file someplace you like (perhaps ~/.darkice.cfg) which contains the following:

[general]
duration = 0 # duration of encoding, in seconds. 0 means forever
bufferSecs = 1 # size of internal slip buffer, in seconds
reconnect = yes # reconnect to the server(s) if disconnected
[input]
device = pulse
sampleRate = 44100 # sample rate in Hz. try 11025, 22050 or 44100
bitsPerSample = 16 # bits per sample. try 16
channel = 2 # channels. 1 = mono, 2 = stereo
[icecast2-0]
bitrateMode = vbr # variable bit rate (cbr for constant)
quality = 1.0 # 1.0 is best quality
format = mp3
bitrate = 256 # bitrate
server = localhost # or IP
port = 8000 # port for IceCast2 
accesspassword = hackme # source password to the IceCast2 
servermountPoint = mystream.mp3 # mount point on the IceCast2 server or any name
name = mystream

Now bootup icecast

sudo /etc/init.d/icecast2 start

Now start darkice (the thing which actually reads pulse and sends it to icecast. Icecast then provides an HTTP URI you can use to give out to others)

darkice -c ~/.darkice.cfg

Now head on over to your consuming device and give it the url http://MY_IP:8000/mystream.mp3 (replacing MY_IP with your machines ip, aka 192.168.1.105).  In sonos you go to the “Radio” tab, hit “Add” then type in that url and a fancy-shamancy name.  Click the radio you created and shazam, good to go!

Note: there is a 3-4 second delay using icecast.  I’m not sure how much of that is icecast vs. sonos, but for just audio streaming its great.  That is, until I decide I really want to watch a youtube video with the audio coming out of sonos and I spend a few hours putting jack in the system…

Note: this was all done on Ubuntu 11.10 using standard repo versions of icecast, darkice, pulseaudio etc.

7 comments

  1. On Windows with CYGWIN, I do something similar to stream what’s on Windows’ “Stereo Mix” through VLC to any machine running squeeze server, as with icecast there is a frustrating 3-5 second delay.

    #!/bin/bash
    /cygdrive/c/Program\ Files\ \(x86\)/VideoLAN/VLC/vlc.exe –no-video –no-sout-video dshow:// :dshow-vdev=none :dshow-adev=”Stereo Mix (Realtek High Defini” :dshow-caching=200 –sout “#transcode{vcodec=none,acodec=mp3,ab=128,channels=2,samplerate=44100}:http{dst=:8080/vlcstream.mp3}” :no-sout-rtp-sap :no-sout-standard-sap :ttl=1 :sout-keep

    In squeeze player, I listen to the URL http://whateverhost:8080/vlcstream.mp3

  2. I get

    ALSA lib pcm.c:2212:(snd_pcm_open_noupdate) Unknown PCM pulse
    DarkIce: DarkIce.cpp:1187: can’t open connector [0]

    when i try to start darkice

    • I got that too when I ran darkice as root. Running as the normal user though and that went away for me.

      • The reason i tried to run it as root is because I got this message, which perhaps might explain the 3-4 seconds lag?

        “Could not set POSIX real-time scheduling, this may cause recording skips.
        Try to run darkice as the super-user.”

        • Yeah, I haven’t dug into that error too much yet. I imagine if we did run as root we could get better performance, but I didn’t mind the lag and haven’t dug into the cause of the alsa error.

  3. I still can’t get it to work, I’ve followed the tutorial to the letter, and all i get is a blank mp3 stream

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>