2011年7月5日 星期二

[多媒體] Setup Audio Streaming Service on Ubuntu (Icecast2+Ices2)

Due to a requirement of project, I tried to setup an audio streaming service before. I choose Icecast as my streaming server, and Ices2 to generates Ogg Vorbis sources for Icecast2 to stream. The details are as follows.

environment:
* server: ubuntu 10.04 (192.168.104.131)
* client: ubuntu 10.04 (192.168.104.1)

Icecast is free server software for streaming multimedia. Ices comes in two flavors, the 0.x flavor which supports sending an MP3 stream to an icecast server, and the 2.x flavor which supports sending an Ogg Vorbis stream to an icecast server. In summary, Icecast is a streaming server, and streamer, likes ices or darkice, providers streaming content.
Ices comes in two flavors, the 0.x flavor which supports sending an MP3 stream to an icecast server, and the 2.x flavor which supports sending an Ogg Vorbis stream to an icecast server.

1. Setup Icecast(2)
1.1 install required libraries
server$ sudo apt-get install icecast2
server$ sudo /etc/init.d/icecast2 start
icecast2 daemon disabled - read /etc/default/icecast2.

1.2 enable icecast2
server$ sudo vim /etc/default/icecast2
# 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

1.3 start icecast2
server$ sudo /etc/init.d/icecast2 start
Starting icecast2: Starting icecast2
Detaching from the console
icecast2.


2. Icecast2 + Ices2
2.1 install required libraries & setup environment
server$ sudo apt-get install ices2
server$ sudo mkdir /var/log/ices
server$ sudo mkdir -p /etc/ices2/music

2.2 modified default value (or not)
server$ sudo cp /usr/share/doc/ices2/examples/ices-playlist.xml /etc/ices2
server$ sudo vim /etc/ices2/ices-playlist.xml
<password>hackme</password>                         # modified me (or not)
<mount>/example1.ogg</mount>                        # modified me (or not)
<param name="file">/etc/ices2/playlist.txt</param>  # modified me

2.3 add playlist
server$ sudo cp /path/to/my_song_1.ogg /path/to/my_song_2.ogg /etc/ices2/music
server$ sudo ices2 /etc/ices2/ices-playlist.xml
server$ sudo vim /etc/ices2/playlist.txt
/etc/ices2/music/my_song_1.ogg
/etc/ices2/music/my_song_2.ogg

2.4 modified script of icecast2 to fit with ices
server$ sudo vim /etc/init.d/icecast2
#! /bin/sh
### BEGIN INIT INFO
# Provides:          icecast2
# Required-Start:    $remote_fs $network
# Required-Stop:     $remote_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Starts the icecast audio streaming server daemon
### END INIT INFO
#
# icecast2
#
#       Written by Miquel van Smoorenburg .
#       Modified for Debian 
#       by Ian Murdock .
#
#       Further modified by Keegan Quinn 
#       for use with Icecast 2
#

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/bin/icecast2
NAME=icecast2
DESC=icecast2
ICES=/usr/bin/ices2                           # add this line
ICES_CONFIGFILE=/etc/ices2/ices-playlist.xml  # add this line

test -x $DAEMON || exit 0

# Defaults
CONFIGFILE="/etc/icecast2/icecast.xml"
CONFIGDEFAULTFILE="/etc/default/icecast2"
USERID=icecast2
GROUPID=icecast
ENABLE="false"

# Reads config file (will override defaults above)
[ -r "$CONFIGDEFAULTFILE" ] && . $CONFIGDEFAULTFILE

if [ "$ENABLE" != "true" ]; then
    echo "$NAME daemon disabled - read $CONFIGDEFAULTFILE."
    exit 0
fi

set -e

case "$1" in
    start)
        echo -n "Starting $DESC: "
        start-stop-daemon --start --quiet --chuid $USERID:$GROUPID \
            --exec $DAEMON -- -b -c $CONFIGFILE
        echo "$NAME."
        ;;
    stop)
        echo -n "Stopping $DESC: "
        # Send TERM after 5 seconds, wait at most 30 seconds.
        start-stop-daemon --stop --oknodo --retry TERM/5/0/30 --quiet --exec $DAEMON
        echo "$NAME."
        ;;
    reload|force-reload)
        echo "Reloading $DESC configuration files."
        start-stop-daemon --stop --signal 1 --quiet --exec $DAEMON
        ;;
    restart)
        echo -n "Restarting $DESC: "
        # Send TERM after 5 seconds, wait at most 30 seconds.
        start-stop-daemon --stop --oknodo --retry TERM/5/0/30 --quiet --exec $DAEMON
        start-stop-daemon --start --quiet --chuid $USERID:$GROUPID \
            --exec $DAEMON -- -b -c $CONFIGFILE
        echo "$NAME."
        ;;
    *)
        echo "Usage: $0 {start|stop|restart|reload|force-reload}" >&2
        exit 1
        ;;
esac

exit 0

2.5 start ices2
server$ sudo ices2 /etc/ices2/ices-playlist.xml

2.6 receive audio streaming via browser
client$ firefox -url http://192.168.104.131:8000/example1.ogg


reference:
* The Linux MP3 & Ogg HOWTO : Streaming Servers and Web Interfaces
* The Linux MP3-HOWTO : Streaming MP3's.
* Ogg Vorbis Streaming: ices2 HOWTO
* Run Your Own Webradio Station With Icecast2 And Ices2 (part1)
* Run Your Own Webradio Station With Icecast2 And Ices2 (part2)

If you want to choose Apache + Musicindex as your streaming server, there are some references helps
* Stream your music with musicindex
* README of musicindex
* HowTo stream mp3 ogg mp4 files with Apache2 libapache2 mod musicindex