Icecast, an oldschool and audio-specific complement to HA

For the past year or so, I’ve been using Icecast to add functionality to my HA instance, and the impetus of this was to make my home as not-internet-dependent as possible. Like the title says, Icecast is an elder statesman of open source software, but it is durable as hell and just works, always.

What Icecast does is take audio streams from a bunch of different sources (USB antennas, video streams, audio files, etc), and make them available as the sort of standard m3u streams you would expect to get from a local radio station’s website. But if you’re self-hosting Icecast, this is all on your local home network.

The original reason I got into this was because I wanted to use a $25 USB antenna dongle to listen to my local radio station (KEXP in Seattle, go listen if you haven’t!) – basically I wanted HDHomeRun+Channels functionality, but for radio, and it took me a while to find the right way to do this.

There are (in my head) a couple of killer apps for Icecast being used as a complement to HA. I wanted to just share the few that I have found valuable in the past year:

  1. Stream a local HD radio station on demand: Use an RTL-SDR dongle with a small antenna and pass the HD radio output to Icecast. In HA, I have a switch template that will play the radio on my Sonos or Google Homes (since it is a normal m3u stream, it’s easy to pass to media_player entities). This lets me listen to music even if the internet goes out. Yah, the internet never really goes out, but it’s a nice thing to know I can do.
  2. Stream NOAA weather radio when inclement weather happens: I have a few triggers for “oh shit something is happening” with respect to weather. I have a WeatherFlow station (using the excellent integration from @briis) that can detect lightning strikes, high winds, or fast rainfall. I also use the NWS alerts custom component (from @finity). If any of these things triggers, HA automatically starts sending NOAA weather radio (the same signal used by emergency storm radios) to speakers in the house (changed depending on time of day). This uses an antenna as well so it’s totally offline. Turnkey sample of this available: https://github.com/zacs/sample-icecast-noaa.
  3. Baby monitor from IP cameras: What I miss about the old analog baby monitors is having a constant audio stream from the nursery. Using a simple bash script I can send the audio from a Unifi camera RTSP stream to Icecast and have it available to do both ffmpeg_noise detections in HA, but also to use another switch template for “turn the nursery audio on in the bedroom speakers.” edit: I now use a Docker container to host the script: Docker Hub.
  4. Run my own radio station at home: This one is dumb, but fun. I just have all the audio files from the original BBC recording of the Hitchhikers Guide, and they shuffle on their own. I can turn on this radio station, and I get dropped randomly into whichever episode is playing. I kind of like the idea of emulating “linear” radio or TV, and I listen to this more often than I thought I would.

I run Icecast in its own Docker container independent of HA. The nice thing is that the two don’t need to communicate in a complex way, since Icecast just offers generic streams that are easily consumed by HA’s media_player entities.

Just thought I would share, as I don’t think I’ve ever seen Icecast or RTL-SDR dongles mentioned in these forums in the past, and I think they are a nice addition to HA. Happy to share any details about how I achieve any of the above as well, cheers!

6 Likes

Genius. (but I have to post 10 chars)

2 Likes

Since you’re already here you might as well post up a tutorial. Sounds interesting.

I have a rtl-sdr dongle I bought a while back for a use that I never even got started with and it’s just hanging around waiting for another purpose.

And thanks for the mention. :+1: :slightly_smiling_face:

Took me a while to get a sample up, but there I put one on Github: GitHub - zacs/sample-icecast-noaa: A sample docker setup for streaming NOAA radio from a USB RTL-S

The instructions in the readme should get you going. The TL;DR is that it will create two new Docker images: one to listen to the RTL-SDR and pipe the output to Icecast, and another to actually host the Icecast server. If you have any automations you want to tune to the radio, you just pass the m3u file from Icecast as the media_url.

2 Likes

Currently i use lms/squeezebox to listen to an mp3 stream from welle-cli / SDR.
I miss the radio logo and currently playing information. Will this be transmitted using your solution, @zacs?

I miss that as well :slight_smile: — I don’t believe the station logo is available to add to an Icecast stream, sadly.

Hi @zacs,

Can you share the “bash script” for get de audio from unifi camera rtsp stream and put into rtmp (icecast)?

I’m playting with this and i have lot of problems with codecs (not supported by chromecast google home, not supported by icecast2, …) and also i have problem with latency, it’s really high.

Thanks!

Sure, I actually have a public Docker image that does this. If you’re comfortable with that: Docker Hub

But all it really does is run this command:

ffmpeg -re ${FFMPEG_INPUT_OPTS} -i ${RTSP_URL} ${FFMPEG_OUTPUT_OPTS} -c:a libmp3lame -b:a 128k -content_type audio/mpeg -f mp3 icecast://source:${ICECAST_PASSWORD}@${ICECAST_URL}:8000/${ICECAST_MOUNTPOINT}

The latency can’t be helped, really. Unifi Protect’s RTSP streams are 10-20 seconds delayed. The same issue is present in the official Protect integration.

Thanks,

Finally this was very similar that I managed to make it work (libmp3lame and very important add the header “audio/mpeg” for use it with chromecast).

My problem is the delay between the play and the start of playback on the google home. I’m using “player.play_media”, I think that google home needs to fill a buffer or something like this because I don’t find a reason for the large delay.

I have tested with different values for “burst-size” in icecast but the problem continue.

Thanks!

If you’re using Unifi cameras, then the delay is on the Unifi side, not Icecast or Google. The video stream gets transcoded into RTSP on the NVR itself, and I assume the process gets a kind of low priority. Mine tend to be ~10-20s delayed which is annoying, although it makes for a fun game when kids do something funny in the basement then sprint upstairs to watch themselves do it on the Nest Hub :man_facepalming:.

Hi,

I’m using a foscam camera, with a dedicated RTSP stream for audio channel. Playing this stream with VLC (configured without buffering) the audio is ~1-2 seconds delayed, from icecast with VLC I have the same results.

The problem is playing from google home, also I have tested limit the buffer client size from the icecast side and then google home has a very big delay (~2-3 minutes) so I’m sure that google home has a big buffer.

Ah gotcha. Yah, my cameras in Homekit are a very short delay, and on Google Home they are more delayed. It is a little better if I just call camera.play_stream using the local IP of the feed (instead of using the Google Home and asking for the camera by voice), but still not great.

I created an Icecast addon for Home Assistant, although my use case is internet radio streams

docs

It also optionally “hosts” ffmpeg long-running commands that push streams to the Icecast server. Those are specified via the addon’s configuration.

2 Likes

This part is especially nice! I have a tiny Docker container for every stream I want to transcode (for example I have a small one taking the RTSP stream from my nursery camera and publishing it to Icecast), so you solution is much more straightforward.

Hi @zacs
I’d like to bring HD radio into HA. I have experience with RTL-sdr, but not in decoding HD radio. Looking at icecast’s website, I don’t see anything that talks about that specific use case. Do you mind explaining how you went about that? Are you piping a stream from nrsc5 directly into icecast?

That’s exactly right. I’m using nrsc5 to interface with the RTL-SDR, and piping the output into Icecast using ezstream. I have a docker container running Icecast (handling a bunch of streams), and then one docker container for each RTL-SDR consumer (one for nrsc5, one to listen to NOA using rtlfm, etc).

Are you familiar with docker? I’m happy to share my compose files and the dockerfiles/scripts I use to make this happen (it’s probably under 200 lines of code total, not very complicated).

My biggest use case is a single Homekit button to “play KEXP in the kitchen and dining room” on my Sonos, so there’s a lot going on behind the scenes but the experience is really straightforward.

1 Like

I would love to see your compose files for this. I tried getting it up and running a month or so ago and wasn’t having much luck.

Sure, it’s actually the Dockerfile moreso than the compose. I made a sample repo (similar to the NOAA wideband FM one I posted earlier) that should be pretty turnkey for you. It’s pretty straightforward, the only thing that complicates things is that the Docker image needs to clone and build nrsc5, which might take a minute or two.

If the container is up, you can tell if it’s working when you run docker logs --tail 50 nrsc5 you should see the verbose LAME output like this:

303600/338935 (90%)|   11:39/   13:00| 3:02:04/ 3:23:16|   15.624x|   21:11 
 32 [     0]                                                                   
 40 [     0]                                                                   
 48 [     0]                                                                   
 56 [     0]                                                                   
 64 [    19] *                                                                 
 80 [  1326] *                                                                 
 96 [136425] ******************************************************************
112 [120489] ***********************************************************       
128 [ 33060] ****************                                                  
160 [  9813] *****                                                             
192 [  2145] **                                                                
224 [   323] *                                                                 
256 [     0]                                                                   
320 [     0]                                                                   
-------------------------------------------------------------------21:12-------
   kbps       mono %     long switch short %                                   
1 Like

Thank you very much for sharing your invaluable experience.

My understanding is still very limited. But I would like to switch to using a smart home without the Internet, using only my local network. (Maybe this is off topic, sorry)
Please tell me if there is such an opportunity to transfer an audio stream to Google speakers in the complete absence of the Internet (only in the local network)?

My problem is that the speakers refuse to interact with Home Assistant (with any other device in the same way, for example, the chromecast stops working with android phones) after some time when the Internet is disconnected from the local network. The google speaker groups I created fall apart and don’t work. Only volume control is still available from Home Assistant.

Can you please tell me if there is any way to fix this?

My goal was similar, although I just wanted to be resilient to internet outages. My guess is that you will never have luck with a permanently-disconnected Google device. However, I bet that Sonos devices would do just fine without internet.

1 Like