Streaming Audio Over SSH

At home, I have a server/desktop running nearly 24/7 (Arch Linux if anyone is wondering). I use this server for all kinds of experiments. My backups are there (well, it’s one of my backup locations). My home dlna server is there. It’s also hooked up to my sound system for mpd so I can have it play my music, controllable by any device on my home wifi. Recently however, I wanted to be able to stream my laptop’s audio over my sound system, without having to plug it in directly. The reason being I wanted to stream Spotify over said sound system, but didn’t want to go to the hassle of plugging in a keyboard and mouse, and installing a GUI and plugging my server in to a monitor, just so I can occasionally listen to music through not-so-bad speakers. Then I wondered, you can do just about anything with SSH, why not try to stream audio over it. Here’s how I do it (there are many other ways).

Requirements

The server (the computer hooked up to the sound system) needs mplayer installed so it’ll have something to play the audio with.

The audio source system (my laptop in this case) needs alsa-utils installed, specifically for the arecord application.

Obviously both the server and the audio source system need ssh installed (and the daemon running on the server).

Command

Not too much to say here.

arecord -c 1 -r 32000 | ssh <user>@<server> 'mplayer -demuxer rawaudio -rawaudio channels=1:rate=32000:samplesize=1 -nocache -'

So what that command does is…​

arecord

Is a command line program for recording from audio devices. If no output file is specified (like in this case), it writes what it records to stdout. For our purposes, we pipe stdout to ssh in the next command.

ssh…​mplayer

Here we send stdout from the previous command (hence the pipe) straight to the server over ssh. Mplayer on the server plays what it receives from stdin (the final - ). The rest of the mplayer flags are just for audio quality control (same for the flags on arecord). The -nocache reduces delay a bit, but in some cases can cause skipping, so you might want to remove that switch.

There is one caveat to this. While it works fine for streaming internet radio or any other audio you want really, streaming audio for a video source doesn’t work nearly as well. On my setup, there is about a .75 second delay, so YouTube videos don’t sync up. Otherwise though this works swimmingly.

Category:Linux Category:SSH