Detecting the AirPlay latency

Jason Crystal picture Jason Crystal · Apr 3, 2012 · Viewed 16.7k times · Source

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.

Answer

Laurent Perrin picture Laurent Perrin · Aug 7, 2012

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.