Listen to your iTunes library from anywhere
October 8, 2017
Continuing my theme of ‘things to do with a Raspberry Pi involving music’, I have turned my Pi and external HDD into a little NAS. My iTunes library now sits on the HDD and is shared over Samba to my laptop. The cool thing about this is that it makes it possible to access my music library from anywhere over the internet. In the age of Spotify and other streaming services this is a lot less cool than it would have been, but it’s still pretty useful if you value having control over your music files. I have tried two approaches, each with different pros and cons.
One, forked-daapd, basically imitates iTunes’ home sharing functionality and makes your library appear as a shared library. Unfortunately this comes with some limitations imposed by Apple. For example, you can’t create or edit playlists on the library.
The second option is to mount the external HDD as a volume on your local machine by connecting over the internet to the Pi and forwarding the relevant ports over SSH. This is a bit more fiddly but gives you complete control over the library on any device.
Setting up forked-daapd on your Pi is super easy. Just follow the instructions here. I created a new ‘daap’ user (with no sudo privileges), made sure they had access to the relevant directories and away we go. My library appeared on my home laptop as a shared library. Pairing my phone with the library took a few seconds using the web interface (after enabling the Pi 3’s bluetooth functionality).
If you want to access your shared library outside of your home network you’re going to have to do a little bit extra. First of all make sure that your Pi’s SSH port is open on your router (using something other than 22 is a good idea). You can double-check this from your local network by visiting canyouseeme.org.
From outside your local network SSH in to your Pi and forward port 3689 (the daap port). Your command will look something like:
ssh firstname.lastname@example.org -L 3690:localhost:3689
This forwards your laptop’s port 3690 to port 3689 on the Pi. ‘localhost’ here is from the point of view of the remote device, the Pi, and not your laptop (this tripped me up a few times). You can forward it to a different port on your laptop, if you like.
You can use a free DNS service like FreeDNS to get a static domain name if you don’t want to memorise your home IP address (my IP address seems to change very rarely, if ever).
So we now have forked-daapd accessible on our laptop on port 3690 but if you open iTunes you won’t see your library. This is because iTunes doesn’t know the library is there. forked-daapd is broadcasting a DNS announcement on our local network but it’s not reaching the laptop out on the internet.
To get around this all we have to do is mimic the broadcast. On your laptop run:
dns-sd -b -P iTunesServer _daap._tcp local 3690 localhost.local 127.0.0.1 ffid=xxx &
iTunes requires an
ffid value but it doesn’t actually care what it is. Note the ampersand at the end, which backgrounds the process.
Your Pi’s music library should now appear as a shared library called ‘iTunesServer’ in iTunes. Rejoice!
forked-daapd is a joy to use, particularly with Remote apps, but I really wanted to be able to make and edit playlists. The solution I found was to mount my remote drive as a local volume and tell iTunes to load the iTunes library stored on that drive. The library will load and work just like a library sitting on your computer’s HDD.
To set this up it is ideal to use a laptop that doesn’t already have an iTunes library, just to avoid confusion. I imagine this is likely to be the case anyway if you’re on, say, a work laptop and trying to get access to your home library.
First, go to the ‘Advanced’ tab in iTunes preferences and untick the options to keep the iTunes folder organised and to copy files when adding to library. You don’t want your laptop’s iTunes to try and copy every single track from your Pi to your laptop!
I’m assuming you already have Samba set up and running on your Pi. If not, look at some instructions here.
SSH into your Pi again, but this time forward ports 445 and 139 to their counterparts on your laptop:
ssh email@example.com -L 445:localhost:445 -L 139:localhost:139
Now we need to mount the volume. If I want to mount the drive
samba from my Pi to
/Volumes/pi on my laptop I do the following:
sudo mkdir /Volumes/pi sudo chown username /Volumes/pi mount -t smbfs //piusername:firstname.lastname@example.org/samba /Volumes/pi
Note that permissions are important here. The
/Volumes/pi directory is initially owned by root, because I have to use
sudo to create anything in
/Volumes. If I don’t
pi directory to my laptop user and mount the remote drive with
sudo then the mounted drive is owned by root and iTunes, Finder, etc can’t see it. For some reason
chown doesn’t seem to work after the drive’s been mounted so I do things in this order. Occasionally it takes one or two goes for things to work properly. Just close the SSH connection, remove the
/Volumes/pi directory and try again. I am working on a script that makes this all a bit more automated.
But anyway, once you can successfully see the remote drive in Finder you’re nearly there. Just open iTunes and immediately press the Option key, bringing up a ‘Choose iTunes library’ dialog box. Choose the iTunes library sitting on your remote volume and that’s it!
For my library of about 7000 songs it took aaages for the library to set itself up but this appears to have been a one-off cost. Happily, I now have access to my iTunes library from anywhere I want!