Audio and Video Streaming with Liquidsoap and Icecast

Jun 14, 2010
Eric Aguayo

Recent studies indicate that online audio and video streaming and other multimedia resources are becoming more popular than conventional TV and radio by making use of different tools such as Icecast, Shoutcast, Liquidsoap, etc. In the past, conventional TV and radio stations broadcasted their content over the internet as an alternative source for consumers and in some cases as a means to reach people living abroad. Nowadays, for TV and especially for radio stations it is a “must” to be able to stream their content over the internet because that is where many consumers are looking for it. This change in consumer behavior provides more opportunities for smaller companies that provide online audio and video streams to promote their content over the internet and reach a greater audience.

Streaming live convent over the internet requires much more extra care than just providing web page content since a downtime in the service can significantly affect the image of the company and cause it to lose content consumers and advertisers. There are several services on the internet that provide services to reliably stream audio and video content over the internet. However these paid services can be very expensive due to the importance of the availability of the service. Nevertheless, there is a cheaper alternative which is secure and reliable at the same time to set up an audio and/or video stream service on your own.

Any streaming service requires 3 components in order to work: a streaming server, a stream source and a client.

  • Streaming server:  This is the main component that passes (and sometimes re-encode) the audio and/or video content from each of the sources to the connected audience. There are several streaming servers both proprietary and open source that can be used. Among the open source alternatives, the most popular are Shoutcast and Icecast, both are easy to install and have proved to be very reliable and secure. To provide a single streaming point Shoutcast is the preferred option, however, to provide a more flexible service that allows multiple sources (different bitrates for example) Icecast might be more suitable. The advantage of Icecast over Shoutcast is that it introduces the concept of mountpoints which are independent streams mounted over the same server which distributes the resources efficiently among all the mountpoints. A Shoutcast example will be shown in this case.
  • Stream Source: A source provides the audio and/or video content to the streaming server. It usually provides capabilities to manage playlists and re-encode audio/video content. It does not need to be on the same physical server as the streaming server but it is recommended to be on a different server. A plugin for Winamp can be used as a source for a Shoutcast server which basically will send any content played on Winamp to a Shoutcast stream server. Liquidsoap is very powerful and programmable streaming source that allows generating a reliable streaming with fallback options. Liquidsoap can be used with either Icecast or Shoutcast. In addition, Liquidsoap provides a very strong a powerful API that allows generating different playlists, introducing jingles and defining fallback sources to produce a continuous streaming in case the main source becomes unavailable. Fallback is also available at server level in Icecast to provide an even more reliable streaming. The liquidsoap API also provides DSP (Digital Signal Processing) capabilities that allow programmatically mixing tracks, normalizing and controlling volume, and even introducing some effects. In this example, A simple Liquisoap script will be shown
  • Client: A client is referred to the software being used by the consumer to playback the stream.  This can vary greatly and depends on the preference of each user, but most of the current players (WMP, Quicktime, Winamp, iTunes, etc) will be able to playback audio streams in mp3 and other widely used formats. Some players provide plug-ins so streams can be played directly from the web browser. Other common formats in audio streams are aac+, asf, and ogg.

The following are the steps to set-up a streaming server in an Ubuntu server. First we need to install the Icecast streaming server, so we execute the following code as root

apt-get install icecast2

Then we need to edit the Icecast configuration file which can be done with your favorite editor, in this case to open with VIM execute

vim /etc/icecast2/icecast2.xml

In the XML file you will find several options that can be configured depending on the resources available on your system and network. More details about these settings can be found on the Icecast website.

Change the following lines to set up the credentials you want to use for authentication, the source password will generally be used for all the clients connecting to the server unless specified on the mountpoint; the default username for sources is “source”

<authentication>
  
<!-- Sources log in with username 'source' -->
       <source-password>hackmesource</source-password>
       <!-- Admin logs in with the username given below -->
       <admin-user>admin</admin-user>
       <admin-password>hackmeadmin</admin-password>
</authentication>

Change the following lines to indicate the port you want to use to access the stream server, in this case I have changed the default which is 8000 to 80 so I do not need to specify the port when accessing through the web browser (i.e. ‘http://localhost’ can be used instead of ‘http://localhost:8000’).

<listen-socket>
   <port>80</port>
</listen-socket>

Finally define one or more mountpoints to be used to access the streamed content. You can set up a fallback mountpoint in case a source is not available. Here is an example of a mountpoint, username and password that will override the default source username and password defined.

<mount>
    <mount-name>/radio</mount-name>
    <username>radio</username>
    <password>hackmeradio</password>
    <burst-size>65536</burst-size>
    <fallback-mount>/backuprelay</fallback-mount>
    <fallback-override>1</fallback-override>
    <fallback-when-full>1</fallback-when-full>
    <bitrate>128</bitrate>
    <genre>Electronica</genre>
</mount>

Save the configuration file and restart the server with the following line as root

service icecast2 restart

Now we need to install the source, in this case we are going to use liquidsoap. You can easily install liquidsoap by executing the following line

apt-get install liquidsoap

However if you want to get full support for mp3 streaming, it is recommended to build liquidsoap from the source by downloading liquidsoap-full-xxx.tar.gz file from the sourceforge download site. Instructions on how to install from the source are very straightforward and can be found on the project website.

After installing liquidsoap, we need to tell it what to play, so we need to write a script that makes use of the Liquidsoap API. Liquidsoap will automatically read any file with extension .liq on the folder /etc/liquidsoap, so we are going to create the following example script named radio.liq within this folder

#!/usr/bin/liquidsoap
# Set the files and playlists,
default = single("/root/default.mp3")
jingles = playlist("/root/jingles")
tech =  playlist.safe("/root/featured/tech")
minimal = playlist("/root/featured/minimal")
prog_trance = playlist("/root/featured/prog trance")
tech_trance = playlist("/root/featured/tech trance")
electro_house = playlist("/root/featured/electro house")
prog_house = playlist("/root/featured/prog house")
psy = playlist("/root/featured/psy")
chillout = playlist("/root/featured/chillout")
sets = playlist("/root/featured/sets")
liveset = mksafe(input.http("http://66.7.216.22:8968"))
liveset = rewrite_metadata([("artist", "Santiago Saa"),("title", "120 minutest Live")], liveset)
# Play files and playlists according to the radio program,
# and the default file if anything goes wrong.
radio = fallback(
  [ switch( track_sensitive=false,
    [
      ({ (1w or 2w or 3w) and 5h-8h }, prog_house),
      ({ (4w or 5w) and 5h05-8h }, prog_house),
      ({ (1w or 2w or 3w or 4w or 5w) and 8h-11h }, prog_trance),
      ({ (1w or 2w or 3w or 4w or 5w) and 11h-13h }, electro_house),
      ({ (1w or 2w or 3w or 4w or 5w) and 13h-15h }, prog_house),
      ({ (1w or 2w or 3w or 4w or 5w) and 15h-17h }, tech),
      ({ (1w or 2w or 3w or 4w or 5w) and 17h-20h }, sets),
      ({ (1w or 2w or 3w or 4w or 5w) and 20h-22h }, tech),
      ({ (1w or 2w or 3w or 4w or 5w) and 22h-0h }, prog_house),
      ({ 2w and 0h-5h }, psy),
      ({ 3w and 0h-5h }, psy),
      ({ 4w and 0h-3h }, psy),
      ({ 4w and 3h-5h05 }, liveset),
      ({ 5w and 0h-3h }, psy),
      ({ 5w and 3h-5h05 }, liveset),
      ({ 6w and 0h-3h }, psy),
      ({ 6w and 3h-5h05 }, liveset),
      ({ 6w and 5h05-8h }, psy),
      ({ 6w and 8h-11h }, tech_trance),
      ({ 6w and 11h-18h }, prog_trance),
      ({ 6w and 18h-21h }, tech_trance),
      ({ 6w and 21h-0h }, psy),
      ({ 7w and 0h-2h }, tech_trance),
      ({ 7w and 2h-3h }, psy),
      ({ 7w and 3h-5h }, psy),
      ({ 7w and 5h-7h }, tech),
      ({ 7w and 7h-23h59 }, minimal),
      ({ 1w and 0h-5h }, minimal),
      ({ true }, tech),
    ]),
  tech])
# Add the normal jingles
radio = random(weights=[1,5],[ jingles, radio ])
# Output the full stream in OGG and MP3
output.icecast.mp3(
  host="localhost",port=80, user”radio”,password="hackmeradio",
  genre="Electronica",url="http://localhost",
  description="Radio",mount="radio",
  name="(((( Evolution Bass Radio ))))", bitrate=128,radio)

Finally we need to restart liquidsoap so the script can be parsed and loaded

service liquidsoap restart

The scripting language is very straightforward, single and playlist commands are used to create sources. The Playlist command in this case will read all the files on the indicated folder, however if a specific file is specified it will read the audio files listed on that file. The liveset variable takes external sources to pass it to the stream which can be used to broadcast live streams. Next we use the fallback and switch commands to program the radio to play the different playlists according to a specific calendar and play a default playlist in case anything goes wrong, in this case we are using tech as the default playlist. By using the playlist.safe command we ensure that playlist will be loaded before the service starts, making it a reliable source. Making stream sources safe can consume several resources during startup but will ensure the reliability of the stream. We include jingles played randomly by using the random command. Finally we output the whole radio source to the Icecast server in mp3 format by using the command output.Icecast.mp3. Note that we are indicating the username and password previously set up on the Icecast configuration file.

The script shows some of the capabilities of liquidsoap, however the script can be as complex as needed. It is recommendable to plan how the service is going to work and what will be the fallback points in case any of the sources become unavailable before they start writing the script. A full API reference and more sample scripts can be found on the liquidsoap project site.