AppleTV control via network (using iPhone remote protocol?)

Posted on
Fri May 08, 2009 10:46 am
matlock offline
Posts: 41
Joined: Apr 17, 2009
Location: Atlanta Georgia

AppleTV control via network (using iPhone remote protocol?)

Okay; I really hope this hasn't been asked before, because I don't seem to be very good at using the search system on this forum for some reason.. :(

Does anyone know if anyone has figured out the protocol that the iPhone Remote uses to talk to and control the AppleTV? I'm betting Apple hasn't published it (and never will), and I suspect Apple has it encrypted somehow, but that hasn't stopped people from figuring this stuff out in the past.

Basically what I want is for Indigo to be able to tell the AppleTV to pull up playlists and play them. I can send IR commands to it (via a GC-100), but the menu system and remote are pretty ambiguous - there's no way to know if you're really at the "main" menu or not without looking at the screen.

I'm hoping for a more "robust" communications; even better would be the ability for me to poll for "what are you doing/playing right now?" like I can with iTunes.

And now let me explain why I want the AppleTV to play the music and not iTunes via streaming.. iTunes is too unpredictable. Most of the time it will launch fine and connect to the AppleTV, but sometimes it won't. It will pop up some random dialog that I can't dismiss with AppleScript. Or sometimes it will come up with Remote Speakers deactivated. Or sometimes it just won't come up at all. I'm at the point now where I actually have an Applescript execute an "do shell script 'killall iTunes'" if it stops responding (I think my library size has something to do with it; over 3TB of movies/television shows -- I've been slowly ripping my DVD collection over to the AppleTV format; 3 400 disc DVD changers take up too much room ) 8)

[It's a shame Apple won't release some sort of "AppleTV Daemon" that can run in the background on my Mac, that can accept Applescript and serve out data to the AppleTV]
Last edited by matlock on Fri May 08, 2009 11:49 am, edited 1 time in total.

Posted on
Fri May 08, 2009 10:58 am
jamus offline
Posts: 179
Joined: Dec 01, 2007

(No subject)

I was just looking into this last night...

http://dacp.jsharkey.org/

I was going to poke around and see if this could be made applescriptable.

I also wouldn't admit in a public forum that you're selling DVDs after you rip them...

Posted on
Fri May 08, 2009 11:50 am
matlock offline
Posts: 41
Joined: Apr 17, 2009
Location: Atlanta Georgia

(No subject)

jamus wrote:
I was just looking into this last night...

http://dacp.jsharkey.org/

I was going to poke around and see if this could be made applescriptable.

I also wouldn't admit in a public forum that you're selling DVDs after you rip them...


Awesome... thanks for the link! And yeah, I didn't really think about it, but I guess that does violate the copyright laws, doesn't it? Ah well... I doubt I'd make more than $1-2 each on them anyway at this point. I'll just box them up and throw them in storage as backups (which I know is 100% legal). :)

Posted on
Fri May 08, 2009 11:57 am
matlock offline
Posts: 41
Joined: Apr 17, 2009
Location: Atlanta Georgia

(No subject)

jamus wrote:
I was just looking into this last night...

http://dacp.jsharkey.org/

I was going to poke around and see if this could be made applescriptable.


Ah man.. another python program. LOL

I already have a very-hacked-together python daemon that runs constantly and receives information from the GC-100's serial port (for my Denon amp), and sends out IR commands to it. It can inject Applescript back into Indigo via python's appscript, but I haven't had luck sending Applescript back into it -- the problem is the ae-events libraries all have their own loops, and they end up messing up the network loops. I guess I'd need to multithread it, but I just started playing around with python (from a Java background), so ... I'm not there yet.

I ended up having to have Applescript run "do shell" commands to echo a command to a named pipe (because Applescript can't write to named pipes), which the python daemon reads... it's a really ugly solution, but it (mostly) works. Ideally, it would accept XMLRPC from AppleScript, since AS can do that natively without do-shells, but I'm too lazy to go that far with it.

But..... this looks similar... I'd need a daemon "listening" for stuff coming back from the AppleTV to update Indigo's variables -- then Indigo can trigger on variable changes if necessary. Hmm..

If I come up with something, I'll post it here. :)

Posted on
Fri May 08, 2009 2:59 pm
matlock offline
Posts: 41
Joined: Apr 17, 2009
Location: Atlanta Georgia

Paired up...

OK.. I was able to "pair" up to the AppleTV and can send it commands and get info... now it's just a matter of writing something nice and Indigo friendly to use it.

Remember - my python skills SUCK... and this could easily be automated...

first you need a python helper script to pair up... copy and paste to a file. Edit the script and change "devicename" to something memorable (this will show up in your AppleTV and iTunes windows -- I used Indigo when I did it):

Code: Select all
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
import struct

class PairingHandler(BaseHTTPRequestHandler):
   def do_GET(self):
      # any incoming requests are just pairing code related
      # return our guid regardless of input
      
      values = {
         'cmpg': '\x00\x00\x00\x00\x00\x00\x00\x01',
         'cmnm': 'devicename',
         'cmty': 'ipod',
         }
      
      encoded = ''
      for key, value in values.iteritems():
         encoded += '%s%s%s' % (key, struct.pack('>i', len(value)), value)
      header = 'cmpa%s' % (struct.pack('>i', len(encoded)))
      encoded = '%s%s' % (header, encoded)
      
      self.send_response(200)
      self.end_headers()
      self.wfile.write(encoded)

      return

try:
   port = 1024
   server = HTTPServer(('', port), PairingHandler)
   print 'started server on port %s' % (port)
   server.serve_forever()
   
except KeyboardInterrupt:
   server.socket.close()


Open up a terminal, and run the script; it will tell you it's listening on a certain port -- likely 1024

In a second window, execute this command, swapping out the port and DvNm) (this, of course, will only work on a Mac):

Code: Select all
mDNS -R myPython _touch-remote._tcp . <port> DvNm="Indigo" RevV=10000 DvTy=iPod txtVers=1 Pair=0000000000000001


This sends out a Bonjour broadcast that tells the AppleTV/iTunes that something wants to talk to them. You should now see your device name show up on the AppleTV Remotes Settings page. Select it, and enter any number for the PIN code. Hit Done -- and your python script will show a response line from the AppleTV that looks sort of like this (and you get your AppleTV's IP address):

Code: Select all
10.0.75.100 - - [08/May/2009 16:31:46] "GET /pair?pairingcode=1DF5CF004F9D7AEDD088FD875D02&servicename=3BAB04A31784FE4F HTTP/1.1" 200 -


You don't need anything from this. You can Control-C out of the python script now. Your new "Remote" will show up in the linked remotes list on AppleTV.

You're paired up.

Now, to actually talk to the AppleTV, you have to have something decoding what comes back. Normally, you'd have whatever is making your requests do this, but in our case, we're going to use this simple script: http://dacp.jsharkey.org/decode.txt ... save it to disk.

Now to actually do something, you have to get a session ID from the AppleTV. You do this by sending it a login HTTP request; your GUID is your device ID; if you don't want it to be ...0001, you'd have to set it in the above responder python script (cmpg). Unless you're going to have two computers running this, I wouldn't worry about it.

Code: Select all
curl -vvv -H "" "http://10.0.75.100:3689/login?pairing-guid=0x0000000000000001" | python decode.txt


If all goes right, you'll get a response back with your session ID.

Code: Select all
 mlog  --+
   mstt   4      000000c8 == 200
   mlid   4      5d923ba8 == 1569864616


The mlid is your session ID; you need the decimal value (second one), not hex.

Now you can make the AppleTV do stuff:

Code: Select all
curl -vvv -H "" "http://10.0.75.100:3659/ctrl-int/1/playstatusupdate?revision-number=1&session-id=1569864616" | python decode.txt


and you get back...

Code: Select all
 cmst  --+
   mstt   4      000000c8 == 200
   cmsr   4      0000004c == 76
   caps   1      03 == 3
   cash   1      00 == 0
   carp   1      00 == 0
   cavc   1      01 == 1
   caas   4      00000001 == 1
   caar   4      00000001 == 1
   cann   16     Very Busy People
   cana   14     The Limousines
   canl   25     Very Busy People - Single
   cant   4      0003bd08 == 245000
   cast   4      0003c0f0 == 246000


from here, the website I grabbed this from is correct: http://dacp.jsharkey.org/ -- jump down to "Protocol and common tasks" -- it tells you the protocol commands and what the different values mean when they come back.

So this is the backbone of it. Now I have to write something to receive commands from Indigo, get a session ID (I have no idea how long a session ID lasts)... the AppleTV even has a "push" mode so you can have something just camp out and listen for changes.

Posted on
Wed Aug 05, 2009 5:58 pm
chuckui offline
Posts: 1
Joined: Aug 05, 2009

PIN passcode incorrect

Have the python script running but when putting in the passcode for the remote it stats passcode incorrect for any number. Any thoughts?

------
update
I was able to solve this but am running into an issue of 403 forbidden when I try to access anything post pairing and geting a session id

any thought? did apple change permissions on the files or structure of directories?

Posted on
Wed Feb 17, 2010 1:11 pm
remeolb offline
Posts: 1
Joined: Feb 17, 2010

Re: AppleTV control via network (using iPhone remote protocol?)

Code: Select all
mDNS -R myPython _touch-remote._tcp . <port> DvNm="Indigo" RevV=10000 DvTy=iPod txtVers=1 Pair=0000000000000001


This code gives me a syntax error in Python. I am very new to using Python so there are any number of things I could be doing wrong. My server runs and listens on port 1024 just fine. Any ideas what I might be doing wrong?

Posted on
Wed Feb 17, 2010 1:49 pm
kalisphoenix offline
User avatar
Posts: 50
Joined: Feb 01, 2010

Re: AppleTV control via network (using iPhone remote protocol?)

remeolb wrote:
Code: Select all
mDNS -R myPython _touch-remote._tcp . <port> DvNm="Indigo" RevV=10000 DvTy=iPod txtVers=1 Pair=0000000000000001


This code gives me a syntax error in Python. I am very new to using Python so there are any number of things I could be doing wrong. My server runs and listens on port 1024 just fine. Any ideas what I might be doing wrong?


This may be a dumb question, but you did replace <port> with 1024, right?

(I don't have an Apple TV and I'm loathe to mess with the working system I have, so I can't really help past that)

Posted on
Fri Feb 19, 2010 4:05 pm
mak02 offline
Posts: 1
Joined: Feb 19, 2010

Re: AppleTV control via network (using iPhone remote protocol?)

Thanks for the python script and step-by-step instructions.

My device comes up in the AppleTV/iTunes window but I can't enter a passcode that is accepted. Any ideas why and how I can solve this?

I also am not getting anything back from the terminal window running the script.

Posted on
Mon Oct 22, 2012 1:41 am
freshwuzhere offline
Posts: 105
Joined: Apr 05, 2012

Re: AppleTV control via network (using iPhone remote protoco

Further to this - I have happened upon an Apple TV network control solution. But first a few of my findings:-
- DACP as used by Apple is encrypted now with RSA keys and unlikely to be cracked in the near future.
- iTunes can handle unencrypted requests (legacy I guess) though I couldn't get the functionality to work well in this way
- Firecore have recently (02 May 2012) released Air Control (Beta) that offers complete network control to Apple TV's
- Apple TV's have to be jail broken - which currently means ATV2's only (which explains why they cost more on eBay than a a new one from Apple).
- You have to install Firecore's aTV Flash ($29.95). (and then Air Control (Beta)) which is a pretty good functional addition.
- Then Perry the Cynic's wonderful plugin Cynical Network and use it to control the devices.
- Air Control can retrieve a raft of information about what's playing.
- This is how Control 4 & Philips Pronto work now.
- I will keep you posted on my progress.

Posted on
Mon Oct 22, 2012 10:51 am
jay (support) offline
Site Admin
User avatar
Posts: 18199
Joined: Mar 19, 2008
Location: Austin, Texas

Re: AppleTV control via network (using iPhone remote protoco

Thanks for the info - I hadn't really researched it recently. I had a suspicion that the encryption was going to be a problem. In fact, I'm a bit concerned that iTunes 11 will add the same encryption and that our iTunes plugin won't work with it either.

I don't think we'll be adding the jailbroken functionality to our iTunes plugin. Jailbreaking may be OK for some but I don't want to give anyone the impression that we condone it by adding functionality to one of our plugins that requires it. ;)

OTOH, a 3rd party plugin that would do it would be interesting. Our iTunes plugin code isn't really pretty (especially since I used some 3rd party code as the starting point - probably a bad idea) but feel free to use that as a basis for your own plugin if you like.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 6 guests