Freitag, 23. November 2012

Using PulseAudio as Network SoundServer

Hey again,

In the previous post PulseAudio was used to give the ability to use the usb soundcard simultaneously by more than 1 process.
PulseAudio has a special feature though (more than one, I know :P) - Acting as a network soundserver for other pulseaudio clients!

So to stick with the previous attempt, now I wanted to play music from my laptop on the NAS - It just took a few steps ;)
 #Following has to be done on the NAS
 #Install additional package for TCP Streaming/Esound
 apt-get install pulseaudio-esound-compat

 #Testing loading the modules 
 pactl load-module module-esound-protocol-tcp auth-anonymous=1
 pactl load-module module-native-protocol-tcp auth-anonymous=1

 #Both commands should have returned a number, nothin else
 #If that's the case - Let's hardcode those modules into the pulse configuration

 echo "load-module module-esound-protocol-tcp auth-anonymous=1" >> /etc/pulse/system.pa
 echo "load-module module-native-protocol-tcp auth-anonymous=1" >> /etc/pulse/system.pa

That should be all that's neccessary on the server-side :)

Now we look at the client side. I am assuming you already got pulseaudio daemon installed and setup properly for normal usage - click here if you don't.

 #Following has to be done on the client side

 #Connect to the tunnel - 1.2.3.4 is the address of your PulseServer here!
 pacmd load-module module-tunnel-sink server=1.2.3.4

 #The previous command should enable you to redirect all your sound to the NAS already!
 #I prefered to combine internal and networked-audio though

 #Find the pulse-sink name of your soundcard
 pacmd list-sinks |grep name:

 #Should return something like: name: <alsa_output.pci-0000_00_1b.0.analog-stereo>
 #Note it somewhere
 
 #Create combined pulse-sink 
 # * 1.2.3.4 is your server's ip
 # * alsa_output.pci-0000_00_1b.0.analog-stereo the name of your soundcard pulse-sink

 pacmd load-module module-combine sink_name=combined slaves="tunnel.1.2.3.4,alsa_output.pci-0000_00_1b.0.analog-stereo"

 #Now we set it as default sink!
 pacmd set-default-sink combined

 #If everything works, we write it to the pulseconfig on the client(s)
 echo "load-module module-tunnel-sink server=1.2.3.4" >> /etc/pulse/default.pa
 echo "load-module module-combine sink_name=combined slaves=\"tunnel.1.2.3.4,alsa_output.pci-0000_00_1b.0.analog-stereo\"" >> /etc/pulse/default.pa
 echo "pacmd set-default-sink combined" >> /etc/pulse/default.pa

 #Have Phun!

NOTE:
It was necessary to set ICEWEASEL_DSP="none"  to "padsp" in /etc/iceweasel/iceweaselrc and restart Iceweasel - to get sound playing from the browser / flash.


Links:
 http://wiki.openwrt.org/doc/howto/pulseaudio
 http://blog.the-jedi.co.uk/2011/06/26/pulseaudio-on-debian-7/
 

Dienstag, 13. November 2012

Setup a Media Renderer on a headless NAS w/ USB Soundcard

Hey everybody!

Got this NAS (GoFlex Net) for quite a long time, serving all my needs - till now!

Recently I aquired a new Android device, a very powerful one with LTE, which gave me the idea how great it would be to be able to send YouTube musicstreams from the android device over to the NAS, playing the sound on my crappy soundsystem with heavy bass :D

Before this moment, the NAS only used MPD + Alsa to talk to the attached USB soundcard.

What am I even looking for?

Converting Youtube videos on the mobile device, only pulling the audiostream from it .. sending it over to a networkshare and re-reading MPD database?
NAAAH... way too much work and very time intense.

What about pulseaudio-server on the NAS and pulseaudio-client on the android device? NAAAH... there is no pulseaudio-client on Android (well there is, with a lot hacking and ditching AudioFlinger involved afaik) - so, option dismissed!

Well... there is still uPnP, right? It's very doubtful I could run xbmc on this little 1,2GhZ ARM processor and still use the other services (samba, nfs, sabnzbd, mpd, pyload) on the NAS properly... also, there is no gpu or hardware-renderer so let's dismiss this option too.

After making some research for my options I came across a nice project called GMediaRenderer - It uses Gstreamer to catch files via uPnP and playback those locally. The problem is, the original version of it is not in a very functional state BUT somebody, hzeller "Henner Zeller", picked it up and got it to a working condition - he calls it >gmrender-resurrect< :)

At first I had to install pulseaudio and run it in system-wide mode so MPD and GmediaRenderer can both use it (normally MPD autospawns it's own pulseaudio session, we don't want that in this case). Please note that I am working as root the whole time, I know it's unsafe, but meeh.. it's just a little embedded device.

 # Install pulseaudio via package-manager  
 apt-get install pulseaudio pulseaudio-utils  
   
 nano /etc/default/pulseaudio  
 # Change PULSEAUDIO_SYSTEM_START from 0 to 1  
 # Change DISALLOW_MODULE_LOADING from 1 to 0  
 # Save the file  

Now we would like to adjust MPD to use pulseaudio instead of alsa.
 # Add mpd user to pulse-access group  
 usermod -aG pulse-access mpd  
   
 # Change mpd from alsa to pulse  
 nano /etc/mpd.conf  
   
 # Comment out all alsa references like this:  
   
 #audio_output {  
 #  type    "alsa"  
 #  name    "My ALSA Device"  
 #  device    "hw:0,0"  # optional  
 #  format    "44100:16:2"  # optional  
 #  mixer_device  "default"  # optional  
 #  mixer_control  "PCM"    # optional  
 #  mixer_index  "0"    # optional  
 #}  
   
 # Now make the pulse-section look like this  
 audio_output {  
 type    "pulse"  
 name    "MPD PulseAudio Stream"  
 }  
   
 # Save the file  

Reboot your machine and verify that MPD is still working! If everything is correct, continue with the following steps to install GMediaRenderer.
 #Install dependencies
 apt-get install alsa-base alsa-tools alsa-utils gstreamer0.10-alsa \
 gstreamer0.10-ffmpeg gstreamer0.10-fluendo-mp3 gstreamer0.10-plugins-base \
 libgstreamer-plugins-base0.10-0 libgstreamer0.10-0 libgstreamer0.10-dev \
 gstreamer0.10-plugins-good gstreamer0.10-tools libupnp-dev automake git

 #Clone the repository
 git clone git://github.com/hzeller/gmrender-resurrect.git
 cd gmrender-resurrect

 #Build the application
 ./autogen.sh
 ./configure
 make
 make install

 #You need to add our user to the "pulse-access" group
 usermod -aG pulse-access root

Now that the application *should* be built, we can run it.
 gmediarender -f "NAS MediaRenderer"
 #You don't have to care about the dbus / X11 errors

Now you should be able to push Videos from your Android device to the MediaRenderer and hear the sound playing. Personally, I am using "BubbleUPnP" on Android. If everything works, you can make it autostart on boot.
 cat > /etc/init.d/gmediarender << EOF
### BEGIN INIT INFO
# Provides: gmediarender
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start GMediaRender at boot time
# Description: Start GMediaRender at boot time.
### END INIT INFO
#! /bin/sh
# /etc/init.d/gmediarender
USER=root
HOME=/root
export USER HOME
case "$1" in
 start)
 echo "Starting GMediaRender"
 /usr/local/bin/gmediarender -d -f "NAS MediaRenderer"
 ;;
stop)
 echo "Stopping GMediaRender"
 killall gmediarender
 ;;
*)
 echo "Usage: /etc/init.d/gmediarender {start|stop}"
 exit 1
 ;;
esac
exit 0
EOF

 #Make it executable
 chmod 755 /etc/init.d/gmediarender

 #Configure the system to run it on startup:
 update-rc.d gmediarender defaults
Reboot the system and you should be good to go :)

UPDATE  1: In the init.d script I forgot to start gmediarender as DAEMON, FIXED!


Links which helped me during this process:
http://mpd.wikia.com/wiki/PulseAudio
http://chrisbaume.wordpress.com/2012/06/24/raspberry-pi-upnp-media-player/
https://github.com/hzeller/gmrender-resurrect
http://www.ubuntugeek.com/how-to-setup-mpd-with-pulseaudio-independent-on-x.html