Detecting the AirPlay latency

17,457

Solution 1

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.

Solution 2

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! :)

Solution 3

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
Share:
17,457
Jason Crystal
Author by

Jason Crystal

Updated on June 25, 2022

Comments

  • Jason Crystal
    Jason Crystal about 2 years

    While I realize that AirPlay has inherent lag/latency, I'm wondering if there's a way for a (currently hypothetical) iPhone app to detect what that latency is. If so, how precise can that latency value be? I'm more curious in whether an app can "know" its own AirPlay latency, rather than simply minimize it.

  • amergin
    amergin over 11 years
    Thank you for the links Laurent. I've been struggling with this latency issue and your 2 second comment sorts a lot out. It appears that AVPlayer over Airplay does not have this latency but has other limitations.
  • Dev Kanchen
    Dev Kanchen over 11 years
    Fantastic references! Clears some of the unnecessary mystery around a feature that could be SO much more awesome with more control.