RSS

Search Engine

Saturday, May 22, 2010

Amarok 1.4 remote in Android using DCOP+Python

We’ve all seen the Apple app that lets you control iTunes remotely, and there totally needs to be something like this for Android. I’m an avid fan of Amarok, so I’ve whipped together a simple remote control that runs on Android. It only took about 3 hours tonight, and I’m releasing everything GPLv3 here. First some details on the architecture:

Amarok can easily be controlled via DCOP, including fetching currently playing information and album art. DCOP is a local, safe IPC architecture that usually comes already enabled with KDE applications. Just a reminder that DCOP is being phased out in KDE 4 with D-Bus taking its place. Speaking of the D-Bus future, there is an awesome standard called Media Player Remote Interface Specification (MPRIS) that is being put together by the folks at XMMS, VLC, Amarok, and others. It doesn’t seem to be in stable releases yet, but will be soon. Back to the present, I’m going to just focus on getting Amarok 1.4 working with older DCOP calls.

First, I built a Python bridge that offers a simple HTTP REST-like API that will help us relay pre-approved DCOP commands from Android to Amarok. The Python is pretty simple:

  1. from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
  2. import pydcop, re
  3. port = 8484
  4. allowed = ["status", "trackCurrentTime", "trackTotalTime", "album", "artist",
  5. "title", "next", "playPause", "prev", "volumeDown", "volumeUp",
  6. "coverImage", "seek"]
  7. resafe = re.compile("[^A-Za-z0-9/]")
  8. class AmarokHandler(BaseHTTPRequestHandler):
  9. def do_GET(self):
  10. # pull out action and simple variable
  11. safe = resafe.sub('', self.path)
  12. blank, action, var = tuple(safe.split('/'))
  13. # skip if action has not been approved
  14. if not action in allowed: return
  15. # check if image request
  16. if action == "coverImage":
  17. self.send_response(200)
  18. self.send_header('Content-type', 'image/jpeg')
  19. self.end_headers()
  20. cover = open((pydcop.DCOPMethod("amarok", "player", "coverImage"))())
  21. self.wfile.write(cover.read())
  22. cover.close()
  23. return
  24. # make dcop call over to amarok
  25. if len(var) > 0: reply = (pydcop.DCOPMethod("amarok", "player", action))(int(var))
  26. else: reply = (pydcop.DCOPMethod("amarok", "player", action))()
  27. # write back any dcop response
  28. self.send_response(200)
  29. self.send_header('Content-type', 'text/plain')
  30. self.end_headers()
  31. self.wfile.write(reply)
  32. return
  33. try:
  34. server = HTTPServer(('', port), AmarokHandler)
  35. print 'started amarokremote server on port %s' % (port)
  36. server.serve_forever()
  37. except KeyboardInterrupt:
  38. server.socket.close()

Essentially we are using a URL of the form http://ipaddress:port/command/variable. For example, in the Android emulator you could call http://10.0.2.2:8282/volumeUp/ to increase the volume.

Carefully note that we are screening the commands against an “allowed” list before running off to Amarok with them. We’re also scrubbing the incoming calls to prevent any buffer overflows. Usually we are just calling the Amarok DCOP method and returning any result. One exception is for coverImage requests, because Amarok just returns a local path. We help that image over the Python bridge by sending the local JPEG as the response.

On the Android side of things, we have a simple screen layout and are hooking up the various API calls to buttons. There’s also a background thread that keeps Android in-sync with what Amarok is currently playing. Finally, we’re using some simple preferences to store the server string and update interval. (Tap the menu button to change these settings.)

Here’s a tarball of the Eclipse project, along with the APK that’s ready to run on the new 0.9 SDK. Start the Python server above on your the computer running Amarok, change the IP address if needed, and you should be ready to go.

Also, a reminder that 10.0.2.2 is magic on the Android emulator because it points to the loopback adapter of the parent computer. So, if you’re running the emulator and Amarok on the same computer, this IP address will work perfectly.

0 comments:

Post a Comment