redirect sound (microphone) via ssh, how to telephone via ssh?

OK, I've just found it, and it still works! Really funny. You don’t need any fancy applications, instant messengers or the like. With this command you send your audio to the remote host.

arecord -f cd -t raw | oggenc - -r | ssh <user>@<remotehost> mplayer -

Or if you like ffmpeg better

ffmpeg -f alsa -ac 1 -i hw:3 -f ogg - \
    | ssh <user>@<remotehost> mplayer - -idle -demuxer ogg

Source: http://shmerl.blogspot.de/2011/06/some-fun-with-audio-forwarding.html


If you want a real telephone:

The command above was only for one direction. For the other direction you have to start another ssh session. So, to receive what the other user says to you, use

ssh <user>@<remotehost> 'arecord -f cd -t raw | oggenc - -r' | mplayer -

Or if you like ffmpeg better

ssh <user>@<remotehost> ffmpeg -f alsa -ac 1 -i hw:3 -f ogg - \
    | mplayer - -idle -demuxer ogg

where hw:3 is the alsadevice you want to record (find it with arecord -l; you can also use a device name, find this with arecord -L; in many cases you can just use the device listed with the following command: arecord -L | grep sysdefault).

Update

In 2018 on my Fedora Linux systems ffmpeg does not have alsa support included (it seems to be the same on RaspberryPi systems with Raspbian). But there is a simple solution without recompiling. Just pipe the output of arecord (the alsarecorder) to ffmpeg:

ssh <user>@<remotehost> 'arecord -f cd -D plughw:2 | ffmpeg -ac 1 -i - -f ogg -' \
    | mplayer - -idle -demuxer ogg

You get the input device plughw:2 by finding your device in the output of the following command:

arecord -l

In my case I see card0 and card2 (my webcam which has a microphone). So I wrote plughw:2 for card2.

Update 2 (without mplayer)

If you don’t have or like mplayer but ffplay (which is part of ffmpeg) you can use:

  • uncompressed wave-audio (high bandwidth, very low cpu usage on the recording side)

    ssh <user>@<remotehost> "arecord -f cd -D plughw:2" | ffplay -nodisp -
    
  • compressed with flac (low bandwidth, low cpu usage on the recording side)

    ssh <user>@<remotehost> "arecord -f cd -D plughw:2 | flac - -o -" | ffplay -nodisp -
    
  • compressed with ogg (very low bandwidth, high cpu usage on the recording side)

    ssh <user>@<remotehost> "arecord -f cd -D plughw:2 | oggenc -" | ffplay -nodisp -
    

Erik's answer works well, but using it how it is, using flac or oggenc, there's significant lag.
A solution is to add -cache 256 to the mplayer command, so it would look as follows:

ssh <user>@<remotehost> "arecord -f cd -D plughw:2 | oggenc -" | mplayer -cache 256 -

Another thing to mention is that nowadays pretty much every modern distribution uses PulseAudio, which takes control over the hardware device, therefore using plughw:2 will fail if PulseAudio is running.
Generally, default should work fine and use PulseAudio, where you can use pavucontrol or similar to select your desired input source using PulseAudio.

That would make the command as follows:

ssh <user>@<remotehost> "arecord -f cd | oggenc -" | mplayer -cache 256 -

BUT: This introduces a lag. In my case, it is around eight seconds. That's terrible.


The solution is much simpler than any other command:

ssh <user>@<host> "arecord -f S16_LE -r 36000" | aplay

That's it. This is using barely any CPU (0.1% according to htop). It's using ~60KB/s of bandwidth for me.

If you want stereo sound: ssh <user>@<host> "arecord -f cd" | aplay
Only useful if you have a stereo microphone, increases bandwidth to ~150KB/s for me. (Since it also raises the sampling rate to 44100Hz).

More information on sampling rate (-r) and bit rate (affected by -f) can be found here.
Basically: The lower you go, the worse the quality but the lower the bandwidth needed.

This solution is fully compatible with PulseAudio, you can control output device, volume and more using pactl or the pavucontrol GUI application:

pavucontrol