How to Stream to AirPlay Speakers from Linux

Jarret B

Well-Known Member
Staff member
Joined
May 22, 2017
Messages
458
Reaction score
520
Credits
19,806
Airplay is a proprietary protocol that allows access from Apple devices to TVs, printers, speakers and other compatible devices. It allows for connecting devices through Wi-Fi to allow for streaming from device to device.

For instance, you can stream a video from your iPhone or iPad to your Airplay enabled TV through the Wi-Fi without a direct physical connection. In the case of multiple Airplay speakers, the devices can all communicate with one another to allow for simultaneous streaming and not have one speaker off by a millisecond from the other speakers, this is usually termed 'broadcasting'.

Streaming music or video is done to prevent any lag in the stream, so there should be no stutter in the music or pausing in a video.

Setup

To start, you need a Wi-Fi capable device, such as a speaker. In my case, I have an 'onn Small Wi-Fi Speaker', which is referred to as 'onn Wi-Fi- Speaker S'. The 'S' is for 'small'. There also exists two others, the 'M' (Medium) and 'L' (Large). The speaker can connect with either Bluetooth or Wi-Fi.

Once you start the speaker, it defaults to Bluetooth since the Wi-Fi has not been configured. Using the instructions, there is a button to change the connection type to Wi-Fi.

Here is the issue, to connect the speaker to a Wi-Fi network, you need an Apple system. You will need an iPad, iPhone or Mac.

For an iPad or iPhone, you go to 'Settings' and then go into the Wi-Fi options, as seen in Figure 1. Here, there should be a listing of your connected and available Networks. Under these, should be another option to appear when the device determines the speaker is available and not configured yet. The new section should be labeled as 'Setup New Airplay Speaker…'. In my case, it was listed as 'onn Wi-Fi Speaker S'. Click on the speaker in the section and you should then see a screen similar to Figure 2.

Figure 1.jpg

FIGURE 1


Figure 2.jpg

FIGURE 2

The default network to connect the speaker to is the one the iPad/iPhone is connected. You can click on 'Show Other Networks…' if you wish to connect it to a different network. If you want, you can change the name of the speaker, for ease of a shorter name, I will change it to 'onn'. Also, there is an option to add a password, and verify it, to the speaker so it can add a layer of security to access the speaker. Once you have made all the necessary settings, click on 'Next' in the top right corner. You should now see a screen, similar to Figure 3, that shows the speaker is being configured with your settings.

Figure 3.jpg

FIGURE 3

Once the configuration has been made, the speaker announces "Airplay ready".

Airplay Connection

We are now ready to install the necessary program to play to the speaker over the Wi-Fi from a Linux system.

You can test whether this work very quickly by running:

Code:
sudo apt update
sudo apt search libplist-dev

It should check the repository list and return the version number that is available. If the version number is not 2.2 or higher, such as on an ARM system, then this will not work.

On Ubuntu, if the before mentioned version number is okay, open a terminal and run the commands:

Code:
sudo apt update
sudo apt install build-essential git autoconf automake libtool libdaemon-dev libasound2-dev libpopt-dev libconfig-dev avahi-daemon libavahi-client-dev libssl-dev libsoxr-dev libplist-dev libsodium-dev libavutil-dev libavcodec-dev libavformat-dev alsa alsa-base alsa-tools libgcrypt-dev pulseaudio-utils uuid-dev xxd

You do not need to have Pulseaudio installed. It starts a connection to the device and creates an audio device named for the connection, in my case it would be 'onn'. From within any program that supports audio output, such as VLC, I can change the Audio Output Device to the newly created connection.

Download and Install Main Files

Now we need to download two sets of files from the Git repository, so perform the following two commands from a terminal, be sure to be in your home folder:

Code:
 git clone http://github.com/mikebrady/nqptp.git
git clone http://github.com/mikebrady/shairport-sync.git

If you should get an SSL certificate error, then run the command:

Code:
git config --global http.sslverify false

This should fix the certificate error, if it exists, and then you rerun the two commands to clone the files to your system.

The files should be downloaded into their respective folders named 'nqptp' and 'shairport-sync'.

NQPTP

This stands for 'Not Quite Precision Time Protocol (PTP)'. This is a protocol used for setting up timing features for streaming audio or video.

PTP is useful for synchronizing time between systems, especially on a whole Local Area Network (LAN).

We need NQPTP for syncing audio between the devices on AirPlay. In this way, we do not have two devices that are off from each other by a second.

To get this installed, we need to go into the cloned folder by 'cd nqptp'.

NOTE: If you have already installed 'NQPTP' before, then you need to stop the service, uninstall it and reboot.

Once in the folder, you need to generate the necessary files for building the service. So, you need to run:

Code:
autoreconf -fi

The command generates the needed 'configure' file.

Once done, you can perform the following commands to build and install the service:

Code:
./configure --with-systemd-startup
make
sudo make install

After the build is complete, you need to enable the service to start at boot and start the service now:

Code:
sudo systemctl enable nqptp
sudo systemctl start nqptp

The NQPTP service is now running, so we need to get onto the 'Shairport-Sync' service for AirPlay.

Shairport-Sync

The Shairport-sync service is used to stream the audio or video over the Wi-Fi network.

We've already downloaded the files, so we need to build the service:

Code:
cd ..
cd shairport-sync
autoreconf -fi
./configure --with-alsa --with-avahi --with-ssl=openssl --with-systemd --with-airplay-2
make
sudo make install

NOTE: If you do not want AirPlay 2 compatability, then remove the parameter '--with-airplay-2'. If you want to change it later, you must remove the 'shairport-sync.service' file after you stop and disable the service. The perform the building of the service again with or without the parameter.

Now you need to enable and start Shairport-sync like you did 'NQPTP'.

Code:
sudo systemctl enable shairport-sync
sudo systemctl start shairport-sync

Firewall

If you are running a firewall, then you need to open two UDP Ports for use by NQPTP.

Open UDP Port 319 and 320 for timing capabilities.

Make the Connection Automatic

Use your favorite editor to create a file that is used to start the RAOP connection to the Airplay device, I will use 'raop.sh'. In the file, place the command:

Code:
pactl load-module module-raop-discover

This will allow AirPlay devices to discover your system and connect. Save and close the file. Now you need to make it executable with the command:

Code:
chmod a+x raop.sh

I just created the file in my Home folder.

In the menu for Ubuntu, select 'Startup Applications'. Once the program starts, click on 'Add'. For the 'Name' field, enter 'RAOP' or whatever you wish. For the 'Command' field, use 'Browse' to find your script. Enter a comment in the 'Comment' field, if you want. Click on 'Add' once you are finished. Close the 'Startup Applications' and then your device should be found when the system starts, if the device is powered on.

NOTE: This is only one way of auto-starting a script. You can use any method you wish that works best for you.

If the device is not powered on, then the connection is not made and you'll need to run the script after you power on the speaker.

Synchronization

I used VLC to play some MP3 files. I kept getting a lot of popping and crackling. To me, it sounded like a timing issue.

At this point, I felt that VLC was not working well with timing. So, in VLC, under Tools you need to select 'Track Synchronization'. This brings up a window for setting sync values. For the 'Audio Track Synchronization', you need to change it from 0 seconds to 1 second.

The audio sounded perfect after this.

Conclusion

With AirPlay, you are not limited to 30 feet as with Bluetooth. The limitation is your Wi-Fi network.

If you have AirPlay compatible speakers or earbuds, give this a try.
 
Last edited by a moderator:


Follow Linux.org

Members online

No members online now.

Top