IceCast on OpenWrt

采集环境声音, 用于远程监控. (Latency = 10s)

How It Works

                +-> (LAN) -> (icecast) -+
(in) -> (ices) -|                       |-> (out) -> (mpd) -> (file)
                +-> (WAN) -> (icecast) -+

Hardware Requirements

  • Raspberry Pi 2B (OpenWrt DD) - ¥194
  • ALFA AWUS036NH - ¥220
  • Logitech Webcam C170 - ¥79

Install Softwares

$ opkg update
$ opkg install icecast ices
$ opkg install kmod-usb-audio alsa-utils

$ icecast -c /etc/icecast.xml
$ ices /etc/ices.xml

Make sure conifg files are OK.

$ ls /dev/snd
controlC0  controlC1  pcmC0D0p   pcmC0D1p   pcmC1D0c   timer

$ arecord -l
**** List of CAPTURE Hardware Devices ****
card 1: C170 [Webcam C170], device 0: USB Audio [USB Audio]  
  Subdevices: 1/1
  Subdevice #0: subdevice #0

$ arecord -D plughw:1,0 -f dat test.wav
Recording WAVE 'test.wav' : Unsigned 8 bit, Rate 8000 Hz, Mono  
^CAborted by signal Interrupt...

$ aplay -l
**** List of PLAYBACK Hardware Devices ****
card 0: ALSA [bcm2835 ALSA], device 0: bcm2835 ALSA [bcm2835 ALSA]  
  Subdevices: 8/8
  Subdevice #0: subdevice #0
  Subdevice #1: subdevice #1
  Subdevice #2: subdevice #2
  Subdevice #3: subdevice #3
  Subdevice #4: subdevice #4
  Subdevice #5: subdevice #5
  Subdevice #6: subdevice #6
  Subdevice #7: subdevice #7
card 0: ALSA [bcm2835 ALSA], device 1: bcm2835 ALSA [bcm2835 IEC958/HDMI]  
  Subdevices: 1/1
  Subdevice #0: subdevice #0

$ aplay -D plughw:0,0 test.wav
Playing WAVE 'test.wav' : Unsigned 8 bit, Rate 8000 Hz, Mono  

Config Ices

You need to create two files:

  • /etc/ices.xml
  • /etc/init.d/ices
  • /root/ices.meta
<?xml version="1.0"?>  
<ices>  
    <background>0</background>
    <logpath>/var/log/ices</logpath>
    <logfile>ices.log</logfile>
    <logsize>2048</logsize>
    <loglevel>4</loglevel>
    <consolelog>1</consolelog>
    <pidfile>/var/run/ices.pid</pidfile>
    <stream>
        <metadata>
            <name>Example stream name</name>
            <genre>Example genre</genre>
            <description>A short description of your stream</description>
            <url>http://easypi.info</url>
        </metadata>
        <input>
            <module>alsa</module>
            <param name="rate">44100</param>
            <param name="channels">1</param>
            <param name="device">hw:1,0</param>
            <param name="metadata">1</param>
            <param name="metadatafilename">/root/ices.meta</param>
        </input>
        <instance>
            <hostname>127.0.0.1</hostname>
            <port>8000</port>
            <password>hackme</password>
            <mount>/live.ogg</mount>
            <encode>
                <quality>0</quality>
                <samplerate>44100</samplerate>
                <channels>1</channels>
            </encode>
        </instance>
        <instance>
            <hostname>1.2.3.4</hostname>
            <port>8000</port>
            <password>hackme</password>
            <mount>/live.ogg</mount>
            <encode>
                <quality>0</quality>
                <samplerate>44100</samplerate>
                <channels>1</channels>
            </encode>
        </instance>
    </stream>
</ices>  
artist=alsa  
title=My ALSA Device  
#!/bin/sh /etc/rc.common

# Startup script
# Copyright (C) 2015 OpenWrt.org

USE_PROCD=1  
START=95  
STOP=10

start_service() {

           [ -d /var/log/ices ] || {
               mkdir -m 0755 -p /var/log/ices
           }

           procd_open_instance
           procd_set_param command /usr/bin/ices /etc/ices.xml
           procd_set_param respawn
           procd_close_instance
}

Docker Deployment

icecast:  
  image: vimagick/icecast
  ports:
    - "8000:8000"
  restart: always

Audio Recorder (TODO)

# sed -i "s/title=.*/title=$(date +%FT%T)/" /root/ices.meta
# kill -s SIGUSR1 $(cat /var/run/ices.pid)
mpd:  
  image: vimagick/mpd
  ports:
    - "6600:6600"
    - "8800:8800"
  links:
    - icecast
  volumes:
    - ./mpd.conf:/etc/mpd.conf
    - ./music:/var/lib/mpd/music
    - ./playlists:/var/lib/mpd/playlists
    - ./recorder:/var/lib/mpd/recorder
  restart: always

icecast:  
  image: vimagick/icecast
  ports:
    - "8000:8000"
  restart: always
audio_output {  
        type            "recorder"
        name            "My Recorder"
        encoder         "vorbis"
        path            "/var/lib/mpd/recorder/live.ogg"
#       format_path     "/var/lib/mpd/recorder/%artist% - %title%.ogg"
        bitrate         "128"
        format          "44100:16:1"
}

format_path hasn't been supported yet.

$ docker-compose up -d
$ docker-compose exec mpd sh
>>> mpc add http://icecast:8000/live.ogg
>>> mpc play
>>> exit

Result

References