Jul 28

if you have an iPhone, you may already play iTunes Remote which is a tool released by Apple to remote control the Apple TV and iTunes from the iPhone/iPod Touch directly.  If you didn’t tried that, I suggest you go to AppStore to download one, it is free and very easy to use.

remote_icon20080711The remote control has no limit on distance, in theory, even at Office, you can directly remote control your home iTunes in real time. How it implemented? Is that a magic? Let’s look into the actual packets.

My iPhone got a LAN address at 172.16.0.102, the iTunes PC got 172.16.0.106.  When the iPhone remote starts, it began to send  a Multicast-DNS packet to 224.0.0.251. This is a multicast which means the devices and computers in the same group will hear this packet.

The computer with iTunes installed will reply packets if it received the multicast packet. The reply is a multicast-DNS response too which provides the information about the iTunes version and capacities.

One of the replies indicates the service port of the iTunes, by default , it uses TCP port 3689. Then, the iPhone will happily connect to iTunes talking through a protocol similar to Digital Audio Access Protocol. More information about RAOP can be seen http://en.wikipedia.org/wiki/Digital_Audio_Access_Protocol

iPhone also send some discardable broadcast packet in the meanwhile which seems not make any sense.

image

Here, we are not going to dig into the protocol specification, but just go through the packets payload roughly, we can see the protocol is actually encapsulated by HTTP.

the login sequence is as below

iPhone   –>     iTunes       :             GET /server-info HTTP/1.1
iTunes   –>      iPhone      :                         HTTP/1.1 200 OK
iPhone   –>     iTunes       :             GET /login?pairing-guid=* HTTP/1.1
iTunes   –>      iPhone      :                         HTTP/1.1 200 OK

After the login, the iTunes and iPhone will exchange some library information. if you play a song remotely on iTunes, many calls related to  /ctrl-int are involved.

The implementation is compact and efficient. The multicast implementation is quite good and adaptive even to a routed complex network.