Detecting the AirPlay latency Detecting the AirPlay latency ios ios

Detecting the AirPlay latency


The latency does not come from network jitter, but rather is decided by the source device (your iPhone).

Long story short:

  • It's always precisely 2s (down to the millisecond) with Apple devices.
  • There is no way to tweak it with public APIs.

Audio latency needs to be very accurate so that multiple outputs can play in sync.

Some explanations about AirPlay's implementation:

  • The protocol starts with several RTSP commands. During this handshake, the source transmits rtpTime, the time at which the playback starts, which is also your latency. The typical value is 88200 = 2s x 44100 Hz.
  • AirPlay devices can sync their clock with the source's with NTP to mitigate the network latency.
  • During playback, the source periodically sends a SYNC packet to adjust the audio latency and make sure that all devices are still in sync.

It's possible to change the latency if you use a custom implementation, but Apple usually rejects them.

Check this writeup for more information. You can also read the unofficial protocol documentation.


The short answer is: no, Apple does not provide a way to do this. Assuming you need your app to be approved in the App Store, you're sort of out of luck. If you can run your app on a jailbroken device you can search around for undocumented APIs that will let you do more.

If you need your app to be available in Apple's App Store, most things you can do network-wise are outlined in the "Reachability" sample app.

The only way I can think of to get a good guess would be to use Bonjour to identify the host (see sample code here https://developer.apple.com/library/ios/#samplecode/BonjourWeb/Introduction/Intro.html) and then ping the host.

However:

  • If there is more than 1 Airplay station you will need to guess or ask which the user is connected to, or maybe take an average.
  • The device may not respond to a ping at all (Apple TV and Airport Express both respond to ping, not sure about 3rd party devices.)
  • The ping may not reflect the actual latency of the audio

Instead of spending too much time on this, you should follow Apple's guidelines for preparing your audio for AirPlay and enriching your app for AirPlay: http://developer.apple.com/library/ios/#documentation/AudioVideo/Conceptual/AirPlayGuide/PreparingYourMediaforAirPlay/PreparingYourMediaforAirPlay.html#//apple_ref/doc/uid/TP40011045-CH4-SW1

Hope this helps! :)


You can query iOS's current hardware audio latency by -[AVAudioSession outputLatency],

According to the document for outputLatency:

Using an AirPlay enabled device for your audio content can result in a 2-second delay. Check for this delay in game content.

And according to my experience, this value changes when switching output device, eg:

  • Speaker: ~10ms
  • Bluetooth: ~100+ms
  • AirPlay: 2s