openairplay / airplay2-receiver

AirPlay 2 Receiver - Python implementation
2.12k stars 131 forks source link
airplay airplay-receiver airplay2

Experimental

Somewhat comprehensive python implementation of AP2 receiver using some multi-room features. For now it implements:

For now it does not implement:

This code is experimental, yet fully functional. It can act as a real receiver but does not implement all airplay protocols and related pairing/authentication methods.

Next steps:

Multiple Connections

Since multithreading is now enabled, this allows multiple concurrent connections. There are no safeguards built to prevent you playing multiple streams. Python multiprocessing makes this "DJ" mode a possibility but makes stream management and session management (global state data) nigh impossible. So threading is the right approach in the receiver.

HomeKit and other AP senders can now connect concurrently to the receiver and perform operations. This opens the path to Remote Control functionality.

mDNS/ZeroConf

If you encounter strange errors like NonUniqueNameException, or Address already in use, and you run on macOS, you may have noticed that macOS and this app both try to send updates. Here is a possible workaround.

Raspberry Pi 4

Install docker and then build the image:

docker build -f docker/Dockerfile -t ap2-receiver .

To run the receiver:

docker run -it --rm --device /dev/snd --net host --volume `pwd`/pairings/:/airplay2/pairings/ ap2-receiver

Default network device is wlan0, you can change this with AP2IFACE env variable:

docker run -it --rm --device /dev/snd --env AP2IFACE=eth0 --net host ap2-receiver

Docker Compose

Example Docker Compose

docker-compose -f docker/docker-compose.yaml up

Debian

sudo apt install -y libavformat-dev libavcodec-dev libavdevice-dev libavutil-dev libswscale-dev libswresample-dev libavfilter-dev portaudio19-dev python3 python3-pip python3-pyaudio build-essential pkg-config git alsa-utils
git clone https://github.com/openairplay/airplay2-receiver.git
cd airplay2-receiver/
pip3 install virtualenv
virtualenv airplay2-receiver
cd airplay2-receiver/
pip3 install -r requirements.txt
pip3 install pyaudio

macOS Catalina

To run the receiver please use Python 3 and do the following:

brew install python3
brew install portaudio
virtualenv -p /usr/local/bin/python3 proto
source proto/bin/activate
pip install -r requirements.txt
pip install --global-option=build_ext --global-option="-I/usr/local/Cellar/portaudio/19.6.0/include" --global-option="-L/usr/local/Cellar/portaudio/19.6.0/lib" pyaudio

python ap2-receiver.py -m myap2 --netiface=en0

Note: in recent macOS versions (e.g. Ventura), you must disable AirPlay Receiver: System Settings -> AirDrop & Handoff -> AirPlay Receiver: disable.

Windows

To run the receiver please use Python 3 and do the following:

cd [WHERE_YOU_CLONED_AIRPLAY2_RECEIVER]
virtualenv ap2env
.\ap2env\Scripts\activate
pip install -r requirements.txt
pip install pipwin pycaw
pipwin install pyaudio

python ap2-receiver.py -m myap2 -n [YOUR_INTERFACE_GUID] (looks like this for instance {02681AC0-AD52-4E15-9BD6-8C6A08C4F836} )

Tested on Python 3.7.5 / macOS 10.15.2 with iPhone X 13.3 and Raspberry Pi 4

Protocol notes

https://emanuelecozzi.net/docs/airplay2