devxlogo

Teach Your TiVO New Tricks with the HME SDK

Teach Your TiVO New Tricks with the HME SDK

f you aren’t already a DVR addict, someday you likely will be! Being able to turn on your TV, push a button, and get a menu of shows that you want to watch?without surfing through hundreds of channels of junk?is a very pleasant experience. It really is TV on demand. TiVO, pioneers and ongoing innovators of this technology have recently released an open source SDK that lets you take the next step in innovating on their platform?the Home Media Engine (HME).

The HME is an extensibility platform for the TiVO unit that allows you to build applications to enhance it. However, at present it doesn’t support the full DVR platform; unfortunately, the main missing part is the DVR functionality?meaning your applications can’t interact with live video?so no program guide or TV show access for your applications yet. However, even with that lacking, there’s still a lot left to explore.

This article is a guide to get you started in building HTM applications to run on a TiVO. This is the perfect excuse to go out and buy the hardware! Alternatively, if you don’t want to buy the hardware, or already own a competing platform, get the SDK anyway, because it comes with a complete emulator so you can try your inventions out without investing any of your hard earned cash!

Getting Started
You can download the HME from tivohme.sourceforge.net. When you do that, you get a ZIP file that you simply extract to a folder on your hard drive and unzip it. Figure 1 shows the HME directory structure created when you unzip the file.

The doc directory contains (surprise, surprise) the documentation for the SDK, which consists of a couple of PDFs containing a developers guide and a protocol guide for streaming information to and from your PC (more on this later) as well as a zip file of the javadocs for HME applications.

For this article, start in the samples directory, which contains several cool samples that will give you an idea for the kind of things that you can do with the SDK.

One caveat is that the simulator only appears to work if you are connected to a network. It doesn’t bind happily to 127.0.0.1 if you are disconnected. To try out the sample applications, first make sure that you have a Java SDK installed (I used 1.4.2_07), and double click the simulator.jar file to execute the emulator. After you have done this, and the emulator is running, it will appear as shown in Figure 2.

In the ‘Samples‘ directory, you’ll see a runsamples.bat (or runsamples.sh for Linux/Unix users), which launch the sample applications. These applications will give you a good feel for the kind of things you can do with the SDK, including slide shows, music and even an excellent Connect-4 style game (seen in Figure 2).


Figure 1. The Directory Structure of the HME SDK: When you download and unzip the HME SDK, it creates this directory structure on your hard drive.
 
Figure 2. Running the Emulator: The figure shows the TiVO emulator running one of the sample applications, a Connect-4 game.

How HME Applications Work
HME applications actually run on your PC, sending commands to your TiVO box to get it to lay out and paint objects on the screen based on how you develop the application. In turn, the TiVO triggers events, such as a response to a remote control command, and sends event notifications back across the network to your application, which handles them, and generally issues commands back to the TiVO in response.

This isn’t as constraining as it might sound?and in many ways it is quite flexible, because it abstracts the TiVO box (an embedded system which is difficult to update and maintain) through the network protocol, using the PC as workhorse (a non-embedded system which is easy to update and maintain), so that you don’t have to reflash or otherwise update the embedded systems in your TiVO box every time the SDK changes. Abstracting the process through the network protocol, means that even if you do update the SDK platform, the TiVO will still work as long as you code to the same protocol spec.

Figure 3. Architecture of an HME SDK Application: Your application sends commands through the network to the TiVO, which responds to events and sends notifications back, which your application can handle.

Building Your First Application
For the rest of this article you’ll step through building a simple application that uses SOAP to communicate to the xmethods.net delayed stock quote sample site. For the sake of deployment simplicity the sample application doesn’t use any XML or SOAP libraries, and it creates the SOAP documents itself in hard code. I did this by generating a SOAP client using Oracle JDeveloper, used that to call the Web service, and then ‘sniffed’ the HTTP line to get the document format. Finally, I hard coded the result into the Java code in the sample application. So, if you want to consume a different Web service, and run it on your TiVO, you can either follow the same methodology, or if you prefer, use a SOAP client and deploy its dependencies to your TiVO. This SOAP client is adapted from the one at IBM developerWorks.

The initial application is very straightforward. When building TiVO applications, your Java should extend Application, and have an init() function. In your init function you place the following:

protected void init(Context context){   String strVal = "IBM Stock is trading at " +       getQuote("IBM");   root.setResource(createText("default-36-bold.font",      Color.white,strVal));}

The init() method calls the getQuote helper function to get the stock quote for (in this case) IBM. It then sets an HME resource with the text “IBM Stock is trading at xxx” where xxx is the price returned from the xmethods.net delayed stock quote service.

The getQuote helper function calls the getSOAPQuote helper function that does all the dirty work of creating the SOAP envelope, calling the Web service, and reading the reply. It then takes the SOAP document that is returned from this Web service and slices out the return value, putting it into a string and returning it to the caller (in this case init). Here’s the getQuote method.

public String getQuote(String strTicker){   String strReturn = "";   String strSOAPPacket = getSOAPQuote(strTicker);   //strReturn = strSOAPPacket;   //String strTest = cli.getQuote("IBM");   int nStart = strSOAPPacket.indexOf(      "") + 29;   int nEnd = strSOAPPacket.indexOf("");   strReturn = strSOAPPacket.substring(nStart,nEnd);   return strReturn;}

Listing 1 shows the getSOAPQuote method.

Figure 4. Running Your First Application: The figure shows the sample stock quote application showing a delayed quote obtained via the Web service displayed on the TiVO simulator.

To run this application on the Emulator, you have to jump through a couple of hoops. First, you need to deploy it to a .jar file. I used the package com.devx.tivo.SoapClient, and called the class StockQuote. You’ll see all this in the download. Next, you need to create a batch file, similar to the one used to run the samples. This batch file will contain a single line:

java -cp ..hme.jar;SoapClient.jar    com.tivo.hme.sdk.Factory -launcher stock.txt

If you want to see debug messages, you can use the –d option too, like this:

java -cp ..hme.jar;SoapClient.jar    com.tivo.hme.sdk.Factory –d -launcher stock.txt

The SoapClient.jar reference in the preceding code is the .jar file mentioned in the last paragraph, and stock.txt is a set of instructions that the launcher needs. For this sample, create a stock.txt file containing the following:

com.devx.tivo.SoapClient.StockQuote

To explore and understand more of these launcher-instruction files, check out the ones that the samples use (runsamples.bat and launcher.txt respectively).

When you run this application you’ll get a basic TIVO screen containing a delayed quote for IBM (see Figure 4).

Enhancing the Application Using the HME
The sample application makes a good start, but it is a little basic. But you can take the code you’ve already seen as a foundation and use it to generate a portfolio grid of many stocks on the screen, updating them every few seconds. You can see the finished version (available in the downloadable code) in Figure 5.

Figure 5. Multiple Stock Quotes: The figure shows the multiple stock quote application running in the TiVO simulator listing five stocks that update every five seconds.

To do this, the first thing you need to do is to make your class runnable, by having its declaration specify implements Runnable like this:

public class StockQuote extends Application    implements Runnable

Next, you will need to use HME “views” to specify the areas of the screen where you want to put the information. Earlier you used root.SetResource to write to the screen, but that method just writes to the root screen object. If you have a number of objects that you want to write to, you define a content view that overlays the root, and then have panels that divide the screen up?in this case into the five colored areas that you see in Figure 5. This application is hard coded for five stocks, but changing that number or making it dynamic should not be a big deal. Finally, the code adds views to these panels that will hold the text. The code from the init() function shown below handles all this.

View content = new View(root, SAFE_ACTION_H,    SAFE_ACTION_V, root.width - SAFE_ACTION_H * 2,   root.height - SAFE_ACTION_V * 2);int panelHeight = content.height / views.length;for(int lp=0;lp<5;++lp){   View panel = new View(content, 0, lp * panelHeight,      content.width, panelHeight);   views[lp] = new View(panel, 0, 0, 200, 70);}new Thread(this).start();

The constants that you see (SAFE_ACTION_H, etc.) are part of the SDK. Check the documentation for more details. The last line of the preceding code starts a new thread, which runs the application, causing the run() function to be called. Run() is the workhorse of the app; it uses the SOAP code you saw earlier to get a quote for each of the five stock tickers hard-coded into this sample app. It then updates each of the views with the appropriate text, as shown in the code fragment below.

while (context != null) {   //update(views[1],"waiting...");   flush();   try   {      for(int lp=0;lp<5;lp++)      {         fQuotes[lp] = getQuote(strTickers[lp]);         update(views[lp],strTickers[lp] + ":" +             fQuotes[lp]);      }   }   catch(Exception e)   {      e.printStackTrace();      update(views[1],"Error : " + e);      flush();   }   flush();   // Wait 5 seconds and do it again   synchronized (this)    {      try       {         wait(5 * 1000);      }       catch (InterruptedException e)       {         return;      }   }}

The flush() function triggers a redraw of the TiVO screen. The run() function code is pretty straightforward, as you can see. Every time it runs, it loops through the tickers and gets the quote using getQuote(strTickers[lp]) where strTickers is the array of pre-defined ticker strings. It passes the returned value to the update function, which you'll see in just a moment. After it's done with this, it calls flush() again to make sure that the screen is updated with the new values before going into a five-second (5,000 millisecond) wait cycle. At the end of this cycle, it runs again.

The update() function is a helper function that takes the string value and the reference to the view (the five views are stored in an array), and sets its text to the string value. It also randomly changes the background of the panel on which the view resides, causing a color shifting effect in the banded colors. See Figure 6 and Figure 7 to see the different colors. The function sets the view's text using view.setResource in much the same way as you called root.setResource in the earlier example. Here's the code:

void update(View view, String strValue){   // Tweak the parent's background color, just for    // fun. This lets the user know that the RSS feed    // was updated.   int red   = random.nextInt(0x80);   int green = random.nextInt(0x80);   int blue  = random.nextInt(0x80);           view.parent.setResource(new Color(      red, green, blue));   // Now update the view with the new text.   view.setResource(createText("default-36.font",       Color.white, strValue));}


Figure 6 One Set of Colors: The update() function updates the views, using white text and a randomly chosen background color.
 
Figure 7. Colors Changed by the Update() Function: As the sample application calls update(), the background colors change.

The TiVO HME SDK is a very cool piece of software. Just imagine, you can write custom Java applications that run on your computer, communicate with an embedded device, and render on your TV! Not only that, but you can connect your TiVO to the Internet and use it to consume Web services as discussed in this article. This is really the beginning of a massive convergence of information and multimedia.

This article showed you how to get your TiVO to connect to a Web service using manually generated and parsed SOAP packets, and then to display the information on your TV showing the delayed values of some stocks with a color changing background, which you could run as a useful screensaver! With the emergence of Web services, and with outlets such as Amazon and eBay opening their data centers via Web services, the stage is set for a whole new way to access information?through your TV set. The promise has been around for some time (remember WebTV?), but now, TiVO appears to be much closer to delivering on that promise?bringing the information highway into the living room. What you've seen here isn't all the SDK can do; it also lets you build applications for home media such as MP3s and your digital pictures. Finally, when TiVO opens the SDK up so it can access the program guide and interact with digital recordings on your TiVO?or live TV?a whole new wealth of application development opportunities will arise. And the best part is that you don't even need a TiVO to begin playing with them. Be forewarned though?after you start building to the emulator, you'll probably want to try it out on the real thing!

devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist