Dr. Dobb's Journal March, 2005
TiVo is a digital video recording (DVR) device and service that, among other things, lets you store up to 140 hours of television programming, pause/rewind/fast-forward live TV shows, find and record shows with your favorite actor, and record one program while watching another. It's no surprise that TiVo's success has made it an attractive target for hobbyists and hackers, as witnessed by the Internet sites, books, and hardware extensions available to extend and improve the user experience. Though customizing your digital video recorder is not encouraged or endorsed by TiVo, we nonetheless recognize there is a desire to extend the experience.
In this article, we introduce a new technology for doing just that. Currently available as a developer preview on standalone TiVo devices, the Home Media Engine (HME) lets you build applications that integrate seamlessly with the TiVo user experience. The HME technology originated at Strangeberry, a Silicon Valley startup we founded, which was acquired by TiVo in early 2004.
HME enables a wide variety of new application types and lays a solid foundation for integrating third-party functionality into the TiVo user interface. Dust off your compilers, because with HME, you can finally legally hack your TiVo DVR!
When we started Strangeberry in 2002, we had just completed lengthy cubicle stints writing enterprise software and were eager to turn our attention to something near and dear to our heartsconsumer electronics. Since the Strangeberry founders were TiVo enthusiasts, we investigated developing the first third-party aftermarket application for TiVo DVRs. We built a concept application in PowerPoint and pitched it to TiVo representatives. They loved the idea, but it quickly became clear that it would never see the light of day. Back then, integrating with TiVo DVRs from the outside turned out to be technically challenging with limited business opportunities.
Frustrated, we decided that the market needed an open DVR and set out to build one from scratch. Within a couple of months, we created a reasonably functional system based entirely on open-source software and featuring powerful user interface (UI) primitives.
Our experiments generated clouds of speculative smoke, but we couldn't quite find the fire. We tried to imagine how future components would be connected together. Today's devices lack connective intelligence and require too many remote controls. What kind of connector will you find on the back of your television in 10 years? NTSC? Coax? Ethernet? Wireless?
We investigated emerging Standards such as Universal Plug-and-Play (UPnP), which allows access to network-based media resources. However, it is inflexible and hard to extend in meaningful ways. Besides, we don't want our media to show up in someone else's user interface. Instead, we want our media in our user interface on someone else's device! (We want iTunes, not Windows Explorer.)
It quickly became clear that the architecture we created solved many of these problems. We decided to focus on building a Digital Media Receiver based on this technology, which we dubbed the "Home Media Engine" (HME).
The Strangeberry Digital Media Receiver was able to display rich multimedia UIs for network-based or embedded applications on television screens. Developers could create attractive applications, with alpha-blended graphics and smooth animations. We had built a truly open technology that let developers control the user experience and integrate diverse applications on a single television screen.
HME is currently available to developers for building applications for the TiVo platform. Figure 1 describes the TiVo platform. The initial release of HME is a developer preview that makes it possible to build PC-based applications that add interesting new functionality to the TiVo box. HME developers have already built personal media applications for music and photos, RSS readers, games, weather viewers, news tickers, search engines, and much more.
For techies like us, it is easiest to think of HME as X/11 for television. Instead of the tired old X primitives, HME supports image compositing, transparency, animation, antialiased text, streaming media, and remote-control input.
HME applications advertise themselves using the Rendezvous service discovery protocol. TiVo devices on the local network will discover these applications and add them to the Music & Photos screen in the TiVo UI (see Figure 2). When users select an HME application, the TiVo device connects to it and the application can display its UI on the television screen. Remote-control input is routed back to the application, giving it full control over the user experience.
The display protocol is simple but effective. The application builds a hierarchical tree of views, each of which can contain a resource, such as a color, image, or text string. The device renders the tree and combines text, graphics, and video into a UI. When users press a key on the remote, an event is delivered to the application, which can then adjust the UI accordingly.
HME has features that let applications create a rich user experience. It contains animation primitives that allow for smooth transitions and fades. Assets can be loaded directly from the application or from an external location specified by a URL.
The hardware inside the TiVo DVR superficially resembles a modern PC, but with reduced cost comes reduced horsepower. Most TiVo DVRs are built around a 180MHz MIPS CPU with 32MB of memory and limited memory bandwidth. The system is continuously recording and playing back video, which consumes a sizable chunk of the system's limited resources.
Luckily, each TiVo device contains a graphics chip that provides a reasonably powerful 2D compositing engine. The chip is a fascinating component with interesting potential and equally interesting restrictions. To work around these restrictions, the HME team built a powerful tiling engine that breaks the HME view hierarchy into a list of HASH(0x86ff44) that can be rendered using the graphics hardware.
The tiling engine, informally known as the "supercollider," detects situations that would violate the constraints imposed by the graphics chip. When the graphics chip becomes overloaded, portions of the user interface are rendered by the main CPU. As the load increases, the rendering engine continues to paint the screen correctly, but performance gradually degrades, similar to a modern rendering pipeline. The result is a smooth and interactive user experience with an intuitive API for developers.
Because the HME is currently only available as a developer preview release, users need to enter a secret backdoor key sequence to turn on support for the HME rendering engine. In future releases, the backdoor will be removed and HME turned on by default.
The HME developer release is freely available and includes a Java SDK, HME simulator, sample applications, documentation, and lots of source code (http://www.tivo.com/developer/). The SDK is available under the CPL license (similar to LGPL) and can be included in commercial products without requiring a separate license.
HME is part of the 7.1 release of the TiVo System software and available on all TiVo Series2 standalone units. Standalone units require a USB Ethernet or wireless network adapter to connect to the local network.
What is the secret backdoor key sequence? Go to the System Information screen and enter clear-clear-0-0 using the remote. You will notice that the Music & Photos screen is renamed to Music, Photos & More. HME is now enabled on the TiVo box.
To get started with HME, we present in Listing One TiVo's version of the ubiquitous HelloWorld application. HelloWorld creates a text resource by specifying the font, color, and a text string. The text resource is placed into the application's root view and appears centered on the television screen.
Since HelloWorld is easy to understand but a bit dull, we'll explore another sample application that is part of the SDKTicTacToe. The TicTacToe (see Figure 3) example covers simple screen layout, animation, remote control events, and sound effects.
The init method is the main entry point for each application, and one of its primary tasks is to layout the screen. Listing Two is TicTacToe's init method.
"Views" are the basic building blocks of HME applications and provide clipping, a coordinate system, and transparency, but are invisible unless they contain resources. "Resources" are concrete elements that are drawn inside views. The layout of the screen is controlled using the view hierarchy. Each view can contain many child views but it can contain only a single resource.
Each application has a root view. The root view can be thought of as the television's "desktop." The size of the root view is 640×480 pixels. However, application developers should be careful to use only the "title-safe" area of the screen. Older televisions have a black border of paint around the edge that can obscure almost 32 pixels on each side!
TicTacToe's init method creates two views as children of the root view. The piecesView is initially empty and will eventually contain the X and O pieces used throughout the game. Later, we will add children to piecesView, one for each move made in the game. The grid view paints the TicTacToe grid that serves as the playing field. It is placed in a specific location on screen and populated with the grid.png image contained in the application.
Because piecesView is created before grid, piecesView and all of its children will paint before grid. On screen, this has the effect of making the pieces appear "underneath" the grid. This is important because when the game ends, we want to highlight and animate the pieces in various ways and the effects are improved if the pieces are underneath the grid. In fact, this is the only reason that piecesView exists at allto provide layering in a specific way that makes the special effects look more dramatic.
The images array contains the X and O text resources that are used to paint the pieces. A single resource can be used in many different views, and we only need a single X and O for the entire lifetime of the application.
Now that we've put a grid on screen, we need to respond to remote-control events to actually play the game when the user presses buttons. Listing Three is TicTacToe's handleKeyPress method. TicTacToe uses the numeric pad on the TiVo remote control. The one button (KEY_NUM1) represents the upper-left corner of the grid, and the nine button (KEY_NUM9) is the lower right. If users hit a numeric key on the remote, we convert it to x-/y-coordinates and make a move. If a user hits any other key, we play the bonk.snd error sound. Nearly all of our standard set of TiVo keys and sound effects are available for use by HME applications.
Finally, TicTacToe takes advantage of HME's powerful animation primitives to provide feedback when the game is over. By default, changes made to HME views using accessor methods, such as setBounds, setTransparency, and setScale, occur instantly. But these same view methods accept an optional animation argument. By specifying an animation, the change can be performed incrementally over some period of time. Listing Four is a snippet from the game-over code.
When a player wins, the X and O pieces appear to explode away from the board over a period of one second. The pieces gradually fade away at the same time. If the game is a draw, the pieces fade without the satisfying explosion, leaving deadlocked players to stare at the stark lines of the game grid.
HME was designed from the ground up to produce an NFL-quality experience, not an HTML experience. Even a simple game like TicTacToe benefits greatly from the pizzazz offered by HME.
TiVo is excited about opening up the platform to developers. We can't wait to see what applications you'll write. By open sourcing the SDK, we hope to tap into a vibrant community and help developers build on our position at the center of the modern living room. This is your official invitation to join the party!
To encourage your creativity, we're hosting the TiVo Developer Challenge, where you can compete for fame and fortune by writing the most creative and interesting HME applications. The contest has great prizes and a jury consisting of well-known industry leaders. We hope it will inspire many of you to develop the next killer app for the TiVo DVR. For competition details, see http://www.tivo.com/developer/.
This initial release is just the tip of the iceberg for the TiVo platform. In upcoming releases, HME will be enabled by default so that all TiVo service users can enjoy the benefits of an open platform. We plan to introduce additional deployment models that don't require a PC, and in some cases, don't even require a home network. The HME API will provide access to more DVR functionality, including scheduling, recording, and playback. Stay tunedyou don't want to miss the show.
DDJ
import com.tivo.hme.sdk.*;
public class HelloWorld extends Application {
protected void init(Context context) {
root.setResource(createText("default-36-bold.font",
"0xffffff", "Hello, world!"));
}
}
Back to articlepublic class TicTacToe extends Application
{
View piecesView;
Resource images[] = new Resource[2];
int gridX, gridY;
...
protected void init(Context context)
{
piecesView = new View(root, 0, 0, width, height);
gridX = (width - 300) / 2;
gridY = (height - 300) / 2;
View grid = new View(root, gridX, gridY, 300, 300);
grid.setResource("grid.png");
images[0] = createText("default-72-bold.font", "0xffffff", "X");
images[1] = createText("default-72-bold.font", "0xffffff", "O");
}
...
Back to articlepublic boolean handleKeyPress(int code, long rawcode)
{
if (code >= KEY_NUM1 && code <= KEY_NUM9) {
int pos = code - KEY_NUM1;
makeMove(pos % 3, pos / 3);
return true;
}
play("bonk.snd");
return false;
}
Back to articleResource animation = getResource("*1000");
for (int x = 0; x < 3; ++x) {
for (int y = 0; y < 3; ++y) {
View v = pieces[x][y];
if (v != null) {
if (victory) {
v.setLocation(v.x + (x - 1) * 400, v.y + (y - 1) * 300, animation);
}
v.setTransparency(1, animation);
...
}
}
}
Back to article