Advertisements

Broadcasting a multimedia stream using icecast2 and gst-launch in Ubuntu Linux

The following is (I hope) an adequately-documented procedure to stream desktop audio capture via icecast2.

What you will need:

  • Basic audio hardware
  • An Internet connection
  • Ability to accept inbound connections on a port (default 8000)
  • Some basic idea of what you’re doing :)

 

Setting up the icecast2 server

Icecast2 is a free server for streaming multimedia similar to SHOUTcast. I won’t go in to all of the differences here — you can easily Google them or visit the respective websites and do some light reading. What I will actually explain here is how to get the server actually running.

Install icecast2 from either Synaptic or command line.

sudo apt-get install icecast2

By default, the icecast2 server’s logging configuration is set up in a way that might deeply frustrate the amateur user. This is because it is set up to log to /var/log/icecast2, which is set up with the following permissions:

drwxr-xr-x 2 icecast2          icecast    4096 2011-06-27 23:45 icecast2

That’s 0755 for user icecast2 and group icecast. Fortunately that user and group account are already created for us, but the configuration file isn’t set to change the running process id (it’s commented out) and the entered defaults are for user nobody. No worries. Read on.

Now edit the  /etc/icecast2/icecast.xml config file using your favorite text editor. If you don’t mind my recommendation, and you’re doing this on a machine running Gnome or Unity, gedit is excellent. Make sure to edit the file as root.

sudo gedit /etc/icecast2/icecast.xml

Edit the file to your preference.

The next settings change depends on whether you’re running Ubuntu Desktop or Server. Find the section below that applies to you.

Ubuntu Desktop: Make sure to head down to the bottom and make the important change to set the correct user/group combination. Find the  section and uncomment (remove the <!-- and --> marks) from around the section. Change to icecast2 and to icecast, like so:

icecast2
icecast

Now start the icecast2 server

sudo icecast2 -c /etc/icecast2/icecast.xml

You should see messages similar to the following:

Changed groupid to 125.
Changed userid to 115.

… and then the program will not produce any more output, but remains running (does not drop back to a prompt). Congratulations, your icecast2 server is running. If it produced an error message, or dropped back to a prompt, investigate the configuration file for any problems, and make sure the port you have icecast2 set to isn’t being used by another process.

To stop the server, hold ctrl and press c (CTRL-c). It does take a few seconds after the keypress to completely halt.

Ubuntu Server: Edit the /etc/default/icecast2 file. The uid and gid should already be set correctly. Verify they are set as follows:

# Name or ID of the user and group the daemon should run under
USERID=icecast2
GROUPID=icecast

Next, follow the instructions on the last 3 lines:

# Edit /etc/icecast2/icecast.xml and change at least the passwords.
# Change this to true when done to enable the init.d script
ENABLE=true

Now start the service

/etc/init.d/icecast2 start

You should get the following output:

Starting icecast2
Detaching from the console
icecast2.

Finding your audio device for streaming

I tried using various programs to set up my audio stream, such as the SHOUTcast Transcoder (sc_trans), darkice, and a few others that I don’t even recall — but none worked correctly for my case. The reason? I either couldn’t figure out what audio device to use, or it wouldn’t work with the audio device I gave it.

As it turns out, Linux machines primarily use PulseAudio as a sound system and many of these utilities expect or are designed to work with an ALSA sound system. I tried /dev/dsp and /dev/audio as input devices but neither existed on my system. I tried padsp with darkice configured to use /dev/dsp but that didn’t work either. I found Alexandru Csete’s GStreamer article and a few other posts and figured gst-launch-0.10 would be the answer that I needed. As it turns out, that was completely correct.

With all credit to Alexandru Csete, here are the steps to find the correct PulseAudio device name for your scenario.

Run the following command:

pactl list | grep -A2 'Source #' | grep 'Name: ' | cut -d" " -f2

You should get output similiar to the following:

alsa_output.pci-0000_01_00.1.hdmi-stereo.monitor
alsa_output.pci-0000_00_1b.0.analog-stereo.monitor
alsa_input.pci-0000_00_1b.0.analog-stereo

These output ‘monitor’ sources feed your devices entire playback stream — effectively ‘what you hear’. The input sources are a microphone or other line-in device. Select the device appropriate for your scenario. If you are trying to capture a webcam microphone or other removable device, generate this list with your device plugged and then unplugged and then compare the lists to find your device.

Once you have your device name, it’s time to…

 

Set up your audio stream

Example command:

gst-launch-0.10 pulsesrc device=DEVICE ! audioconvert ! vorbisenc bitrate=BITRATE ! oggmux ! shout2send ip=IP port=PORT password=PASSWORD mount=MOUNT

An explanation of the options:

  • DEVICE: Your audio device (from above)
  • IP and PORT: The IP address and PORT number of your Icecast2 server
  • PASSWORD: The password to your icecast2 server
  • MOUNT: The mount point of your stream.
  • BITRATE: The bitrate you want your audio to be (see examples below)

In my case the audio device I wanted to use was:

alsa_output.pci-0000_00_1b.0.analog-stereo.monitor

So my line was:

gst-launch-0.10 pulsesrc device=alsa_output.pci-0000_00_1b.0.analog-stereo.monitor ! audioconvert ! vorbisenc bitrate=32000 ! oggmux ! shout2send ip=localhost port=8000 password=XXXXXX mount=stream

The above produces an OGG stream. This works pretty well, but some Windows users report issues with OGG streaming. If you want MP3 streaming instead, try this:

gst-launch-0.10 pulsesrc device=alsa_output.pci-0000_00_1b.0.analog-stereo.monitor ! audioconvert ! lame bitrate=32 ! shout2send ip=localhost port=8000 password=XXXXXX mount=stream

Note that if you are streaming to a SHOUTcast server (as opposed to an Icecast2 server), change the command as follows:

Remove mount= and replace it with protocol=2. Make sure you are connecting on the port specified in sc_serv.conf and not the +1 port. Example:

gst-launch-0.10 pulsesrc device=alsa_output.pci-0000_00_1b.0.analog-stereo.monitor ! audioconvert ! lame bitrate=32 ! shout2send ip=localhost port=8000 password=XXXXXX protocol=2

Running this produces output similar to the following:

Setting pipeline to PAUSED ...
Pipeline is live and does not need PREROLL ...
Setting pipeline to PLAYING ...
New clock: GstAudioSrcClock

If you get some sort of error investigate your shout2cast settings.

Again, CTRL-c stops the streaming.

At this point, you should have a working icecast2 server and a working stream (via gst-launch-0.10). Congratulations!

I’m sure this guide is far from perfect, so please feel free to share any feedback you may have in the comment sections below. Thank you!

Advertisements

, , , , , ,