Project: Apollo Pre-Alpha Update

I just posted a refresh of the Project: Apollo pre-alpha technical demo. No promises about the reliability of different browsers at this point, but observe:

http://pre-alpha.project-apollo.appspot.com/

Key Features to Note

  1. Portions of the UI have been built and are functional
  2. You can’t see it or manipulate it, but there is a playlist engine running with two tracks programmed in
  3. Abstracted audio rendering engine is working; the system will choose the best way to play audio based on your browser and the media you want to play*
  4. Keyboard shortcuts! Try [space] to toggle play/pause, [,] to skip back and [.] to skip forward; [v] toggles a visualizer, but it’s currently disabled

Of the sample audio, the first track is in Ogg Vorbis format and relies on HTML5 <audio> tags to render, which will likely only work if you are in Firefox 3.5+, or Chrome 6+. It works in Safari also if you are on a Mac and have the XiphQT components installed for Ogg media. The second track is in MP3 which should work in all browsers since it currently defers to the Flash implementation which most browsers can handle.

Please don’t be shy to leave your comments below!

* This was one of the hardest things to build. There is an enormously complicated permutation set of browsers and the media formats they support. The current options for playing audio are limited to Flash-based engines such as the one provided by Longtail Video, Silverlight by Microsoft, or native HTML5 audio tags. I’ve implemented all but the Silverlight option up to now. I haven’t setup any rules telling the engine when to use what implementation yet but will soon.

What’s Next in Apollo?

I’ve deployed the first pre-alpha build of Project: Apollo and you can check it out. So what’s next?

Technology Sidetrack

For a little while, I’m going to sidetrack from new features in Apollo to work on the audio/video rendering system. While I’ve got JW Player working decently now, it adds a lot of overhead and if Apollo is going to run on iOS devices, it will need to have an HTML5 implementation. So the next phase of development will be focused on improving the support for multiple rendering engines.

Visual Playlist

Right now the playlist is just a hard-coded demo. Logically, I’ll need to expose it to the user so you have some idea of what’s next in the list. Simple enough.

The Library

Probably the biggest function area is the library. This is where you can store and browse all the media in your library and make playlists and so forth. Details are vague but it will be cool. I promise.

Platform Decisions

I keep tinkering with different ways of presenting my blog, but I can’t ever seem to get quite the right “look” for functionality that I want. I probably have my only myself to blame for a) not clearly defining what it is I want and b) not taking the time to develop my own theme. I guess that’s why they say “If you want something done right, do it yourself”.

Pre-Alpha Preview of Project: Apollo

Last night I put together and deployed a pre-alpha build of Project: Apollo to demonstrate the progress I’ve made thus far. The label “pre-alpha” should pretty well indicate the state of things.

There are some browser-specific quirks to address that pertain to the deployment strategy I am currently using, but those are of low concern at this [pre-alpha] stage.

Since most of what is working is under the hood, I’d like to highlight a few of the accomplishments in this release.

Fully Abstracted Player Framework

The underlying mechanism for rendering audio is completely abstracted and decoupled from the application itself. I am currently using JW Flash Player to do the heavy lifting but as you can see it’s pretty well hidden from view. The next step is to start working on alternative rendering engines to increase compatibility. More on that later.

100% HTML/CSS/JavaScript UI

All of the interactive UI elements are HTML/CSS/JavaScript, including the play/pause butons, the time display and the scrub bar.

Growable Application Structure

I spent a lot of time tooling and retooling the basic foundations of this project to ensure I was comfortable with the architecture. It was very important to me to build a good base upon which to build the rest of the application.

Building an application completely in client-side JavaScript is a very different beast than traditional MVC web apps. Project: Apollo represents (even at this early state) a milestone for me in terms of the way an application of this size and scope is constructed.

I am particularly pleased with the implementation of certain design patterns in the code. For example, state changes are conveyed using the observable pattern via an event bus and the JW Flash Player is cleverly integrated through the use of a Façade and Bridge. I am equally pleased with the separation of concerns in the GUI layer; building entirely new runtime-swappable skins for the application should be a breeze later on thanks in part to GWT’s Deferred Binding mechanisms.

Room for Improvement

There are certainly areas of the application which I would still like to improve. One area is on resource injection. In the past, I have used tools like Gin to perform dependency resolution and injection similar to what Spring provides. So far in Project: Apollo I have opted to forego any DI tools and do so myself. Where appropriate, I’ve used constructor arguments or setters to facilitate testing and singleton patterns in other places to allow access to global resources such as the event bus.

The primary reason I chose to pass on DI mechanisms is that my application simply isn’t complex enough to need them, yet. I’m sure at some point down the line, managing the creation of all my dependencies in the module entry point will prove too cumbersome, but for now, it’s simple enough to take care of those chores manually.

Lessons Learned

The biggest take-away I’ve gotten out of Project: Apollo thus far is a sense of value for certain programming paradigms. As I mentioned above, I opted not to use Gin in this project, but only after some internal debate. Initially I had included it in the project but I removed it when I realized the only reason I included it to begin with was because I had been trained to do so. I hadn’t thought it through for myself!

Using a design pattern does not mean using a library or framework! There’s more than one way to implement a pattern, it turns out.

I’ve also taken a bigger appreciation for the use of design patterns and when to use them. In previous projects, I’ve wasted tons of time trying to shoehorn every aspect of a project into a uniform pattern. For example, I have worked and reworked my own implementation of an MVP framework but ultimately I @deprecated the MVP interfaces in Project: Apollo. Simply put, not everything can be classified as a model, a view, or a presenter, and it’s important to recognize that fact.

(That isn’t to say that I don’t intelligently separate business logic from design and view logic. I do. But only enough to facilitate good structure and programming practices; no more, no less.)

Without further adieu…

I’m glad you’re still with me. As a reward for so patiently reading my entire rant, you may now proceed to demo the pre-alpha release of Project: Apollo! Disclaimer: I am aware of many of the bugs already and this being a pre-alpha release, I’m not too concerned about them at this time.

http://pre-alpha.project-apollo.appspot.com/

Rendering Media in Project: Apollo

Just wanted to document a few nuances that I ran into while working on some of the UI features in Project: Apollo.

Flash movies must be visible

I was hoping to use CSS to set the <embed> element for a Flash movie to something like:

embed {
  display: none;
}

As it turns out, a Flash movie that isn’t displayed isn’t rendered at all; the plugin control is killed off it becomes invisible. This may not be the case in all browsers, but it is the case in FireFox. As an aside, you can set the visibility to “hidden”, but the Flash object will generally still participate in DOM layout.

This was a problem because for audio visualization or video playback as I wanted to have the player hidden until the visuals were requested by the user. Unfortunately, in this case, playback does not occur.

The solution to this problem was to use absolute or fixed positioning to station the visuals where I wanted them. When not wanted, I positioned the element somewhere off-screen.

embed {
  position: absolute;
  left: -100px;
  top: 0px;
  width: 0px;
  height: 0px;
}

There may be some fudgery necessary to keep the window from unnecessarily adding scroll bars, but I am using GWT’s LayoutPanel so this was not an issue (or at least, accounted for automatically).

This also had the side effect of complicating my thoughts around a skinnable UI. Because of this visibility bug, I can’t depend on the skin to manage the container for the MediaControl. To do so would expose too much risk of a skin setting the Widget invisible or otherwise running amuck.

Instead, I relied on a less-pretty-but-more-reliable approach of providing limited access to a managed container in the DOM. The skin can never remove this element from the DOM and so the risk of runamuckery is greatly reduced. My managed container exposes just the functions of LayoutPanel I want exposed to the skin. If the need arises, I may switch this to a singleton interface, but for now, static methods suffice.

As another aside, a fully functional and fleshed out technical demo of the ControlBar is coming soon. Just have a few more items to wrap up.

To Couple or Not to Couple; That is the Question

I’ve reached another point of contemplation in my efforts to build the greatest media player ever. This is an architectural matter about application development.

The puzzle in question is the use of loosely coupled application architecture versus event-driven application architecture.

Loosely Coupled Applications

In a loosely coupled application, interfaces define contracts for various components of the application. Dependent code is then given (preferably injected with) a reference to an instance of that interface. The implementation may or may not be a singleton, but it does not matter to the dependent code. It has a handle on an instance of the interface it requires to do its job and can execute accordingly.

Event-Driven Applications

At the other end of the spectrum, an event-based application would have one component broadcasting a message while another component listens for that message. The two components know nothing about each other and neither have any direct interaction. In this case, it is up to a messaging bus to deliver messages from one component to another. The responsibility of the sender ends when it sends the message, but the sender may also be a receiver, listening for a response message of some kind.

Comparison

The biggest difference in the two schools of thought here is that the event-driven architecture is asynchronous. When a sender broadcasts a message, there’s no guarantee that the response will come anytime soon. This works well for environments like JavaScript where services often cross the client/server boundary in order to fulfill a request, such as in AJAX.

A second advantage of event-driven architecture is the ease with which additional behaviors can be added to an ecosystem. Adding behaviors in response to a message is as simple as adding a new listener for that message; neither the existing receiver(s) nor the sender need to be modified.

Although the asynchronous nature of event-driven applications sounds promising, in actuality, it can create a bit of burdon on the developer. You no longer have any coupling whatsoever (not even loose) between senders and the receivers. This can make it difficult to read and understand code; there are no contextual clues as to who might be listening for a message. This can make it difficult to refactor code if you aren’t able to track down all of the listeners of a particular type of message.

An Example

Since this is in reference to Project: Apollo, I’ll use it as a reference.

The underlying media player in Project: Apollo supports some obvious controls: play, pause, next, and so on. To capture this, I created an interface:

public interface TransportControl {
    void next();
    void pause();
    void play();
    void previous();
    void seek(double percent);
    void stop();
}

There are several components throughout Project: Apollo that need to be able to control playback. The GUI, for example, has buttons that correspond to these functions. The playlist also invokes these controls to advance the player to the next track.

In a loosely-coupled application, an instance of TransportControl would be injected into the GUI as a collaborator. Then, the GUI could respond to a user click by directly invoking the next() method on the TransportControl. This keeps the code clear and easy to read, but it actually creates another problem. The playlist still needs to be informed when the GUI has invoked a “next” command. Some mechanism must exist to instruct the underlying player what to play next.

In the event-driven architecture, the GUI would simply broadcast a message requesting that the next item be played. In this case, the TransportControl would receive the “next” message and know that it should stop playing the current item. The PlaylistManager would also receive this message and instruct the TransportControl to load a new item. Further, a debug logger can log out the entire process, providing detailed messages about what is happening.

Conclusion and Comments

At this point, I am unsure of the best solution. Clearly there is more than one way to solve this problem and some folks I know would recommend I simply make something work and move on; it can always be refactored later. Good advice for a project on a timeline for budget, but this is a learning experience where I can really explore these types of questions. My instinct tells me the right answer is some manner of balance between the loosely-coupled option and the event-driven option.

So does it make sense to inject the GUI with an instance of the TransportControl? Or does it make more sense for components wishing to control playback to broadcast a message according to their intent? Thoughts?

UPDATE: After reading in more detail on the Wikipedia entries I linked to above, I think the best solution for my example case is a complementary application of both event-based and service-based architectures. Specifically, changes in state throughout the application should be expressed as events, but components that need to invoke certain behavior should do so on a service object or proxy, even if that proxy merely relays an event under the hood.

My Unwavering Support of Apple

Anyone who knows me will attest to my love affair with Apple products. While I wouldn’t go so far as to call myself a fan boy, I will say that I sincerely appreciate a well engineered product; I rarely let price alone influence my buying decisions. Quality matters! After all, I frequently recommend the products I have used and enjoy to friends and family. More often than not, it is Apple’s products that meet my requirements. Does that make me an Apple evangelist? I don’t think so.

I don’t claim that Apple products are problem-proof or that the extra expense of an Apple product implies a problem-free product lifetime. In fact, I make no claims whatsoever as to the hardware quality control Apple institutes. What makes me believe in Apple so strongly is how they act when a problem does arise. And yes, I maintain my faith even after I have had problems with my Apple products. Here’s why.

Today my MacBook Pro had the misfortune of requiring a visit to the Genius Bar at my local Apple retail store. After 18 months of [ab]use, I finally decided to have a long-standing issue looked at by the geniuses in blue.

For as far back as I can remember, I have had miscellaneous intermittent problems with my Mac. When it was new, the display periodically went haywire on my Dell 30″ monitor at the office. It was completely random and extremely frustrating, though I eventually traced the problem to the dual-link DVI adapter and managed to swap* it out with a newer model which had a newer firmware version and that seems to have mostly resolved that issue.

Recently, though, my machine has picked up a habit of going zombie and devouring any works-in-progress left open on my desktop. After the display went to sleep, or sometimes when the machine itself was asleep, it would become unresponsive. The fans inside would speed up to maximum and the machine would get very warm. Possibly some runaway process?

Digging through the Console logs, I saw a lot of this:

kernel	NVDA(OpenGL): Channel timeout!
kernel	NVRM-RC: 0 4e565f504649464f5f494e54525f303a307831303031
kernel	NVRM-RC: 0 4e565f504649464f5f4341434845315f444d415f53544154453a307838303030
kernel	NVRM-RC: 1 363232300a
kernel	NVDA(OpenGL): Channel exception! exception type = 0x6 = Fifo: Parse Error
kernel	00000069
kernel	NVRM-RC: 0 4e565f504649464f5f494e54525f303a307831303031
kernel	NVRM-RC: 0 4e565f504649464f5f4341434845315f444d415f53544154453a307838303030
kernel	NVRM-RC: 1 363232300a

That did not look good. The genius didn’t think highly of it either and quickly concluded that the graphics card, nay the entire logic board in my poor laptop was in need of replacement. Oh dear. The cost of the parts and labor were estimated between $350-700 (essentially, buy a logic board for $700 or option a retroactive Apple Care plan for $350).

This is where the transaction got better. The rep looked through his computer for a moment and I suggested that the problem had been going on for months and that it had taken me this long to compile a reasonable amount of clues. At this point, the technician spoke to his supervisor and offered to waive the entire cost of repair. Apple was going to completely replace the logic board in my laptop 220 days out of warranty absolutely free.

So yes, I will buy any iThingy Apple slaps their logo on. I will consider Apple’s products first and foremost when I am in the market for an iGadget. And I will continue to recommend, nay, endorse the products Apple makes because I know first-hand how highly Apple values the relationship it has with its customers.

Still not convinced? Call the manufacturer of your favorite gadget and see if you can even talk to a human being today. If you can, see if you can convince them to replace your gadget for free, even while under warranty. Good luck.

* When I say “swap”, I mean “purchase a new one, put the old one in the new box and return it.”

Using GWT’s Application Framework Judiciously

I am very excited to see the inclusion of a proper application framework in the upcoming Google Web Toolkit 2.1 release. The addition of this framework is a clear signal that GWT is a serious library for enterprise scale applications.

As a seasoned developer, I am very familiar with the traditional Model-View-Controller approach to building web applications. The MVC approach works great for server-based frameworks like Spring MVC or Struts. However, ever since I started working with GWT way back at version 1.4, I’ve had a hard time mapping MVC into a truly client-side application where the entire GUI is built in GWT.

The MVC approach is tried and true in the desktop software world. But on the web, there are a few extra quirks that make traditional MVC a little tricky. Here are some of the challenges I’ve run into.

History Management and Support

Because it is a web application running in a browser, there is an implied expectation that the forward and back buttons work, regardless of whether your web application is mostly client- or server-side. This presents a unique challenge to MVC because most desktop applications don’t have forward or back buttons.

Bookmark Support

Another important feature to any good web application is the ability to bookmark or share the link you’re currently viewing. It should be possible to bookmark a page or paste the current URL into an email, for example, and restore the state of the application based solely on that URL.

Composite View Structure

When working with traditional MVC frameworks like Spring MVC, the process is pretty straight forward. For any given URL, only a single Controller responds to the request. The Controller prepares a single Model and that Model is handed off to a single View. Common elements of the UI can be rendered with include statements, but the View generally has a very direct relationship with the URL.

Conversely, with a more desktop-like application built in GWT, the UI is frequently built as a Composite. There are several components in play at any given time and there isn’t necessarily a straight forward relationship between the current URL and the current set of components viewable to the user. This makes intra-application messaging and navigation a bit more challenging; you can’t just rely on a DispatcherServlet, for example, to coordinate user interaction.

Enter the GWT Application Framework

Clearly, the new App module in GWT is designed to address many of these concerns. It’s great to see History Management and the notion modular composite structure being supported as first class features.

The application framework in GWT is all new to me, though, and it isn’t really MVC. Instead, it’s more of a Model-View-Activity (or -Presenter) approach. In this case, the Activity takes the place of the Controller and is responsible for only the slice of the UI represented by its View. This MVA approach handles application navigation very well, assuming your application is, like a traditional web application, a series of screens or “activities” through which the user navigates linearly.

So what happens when the UI dictates that two or more of these activities be present at once? Well GWT can handle that as well by using multiple ActivityManagers. The ActivityManager is responsible for coordinating all of the Activities for a designated Display – the area of UI over which control can be delegated to an Activity.

This is all well and good, but Project: Apollo presented me with a unique challenge in this new MVA architecture. What do you do with UI components that are always present? Case in point: Apollo has a control bar that is always present in the UI, similar to the button on the front face of a DVD player.

The control bar allows the user to control playback and displays pertinent playback information such as the elapsed time and name of the current track. The control bar, by nature of its omnipresence, does not participate in the navigational functions of the ActivityManager, nor is there any other Activity that would fill its slot.

I’ve also gleaned through GWT code in trunk that the usual way of handling Activities is through an ActivityMapper; the ActivityMapper is responsible for resolving and creating instances of Activities and their corresponding Views. However, control bar isn’t managed as such. I have a self-imposed rule in development of Project: Apollo that UI components should be UiBinder compatible; I should be able to include the control bar in my UI declaratively through the constructs afforded by UiBinder. By contrast, Activities depend on the current state and history of the application, so their lifecycle would not be appropriate to manage through UiBinder, hence the Display slot concept.

Bring Back the Controller

The Java docs for Activity state:

Implemented by objects that control a piece of user interface, with a life cycle managed by an ActivityManager, in response to PlaceChangeEvent events as the user navigates through the app.

It’s easy to see that my control bar doesn’t strictly qualify as an Activity. In this case, MVA isn’t a good fit; I need a different approach. One option, for simpler components, is to let the UiBinder template be the “view” and the backing Widget code be the “controller”. For more complex components, though, I still want to break out application logic to a separate class and leave UI-centric logic in the View-backing code.

Taking a hint from previous iterations of GWT’s application framework, I am choosing to call my pseudo-controllers “delegates”. The Delegate represents a code structure to which the actual View will delegate user interaction for handling. Delegates contain familiar Activity-esque functions to facilitate lifecycle events where necessary.

public interface Delegate<W extends IsWidget> {
    void shutdown();
    void startup(W widget, EventBus eventBus);
}

For my control bar, then, I have broken the code into a ControlBarWidget (the View), a ControlBarDelegate (the Delegate), and a ControlBarModel (the Model).

To keep with my requirements that the control bar be UiBinder compatible, I am allowing the View to handle creation of a default implementation of the Delegate. I’m using GWT.create() to instantiate it so the actual implementation can always be easily swapped out later via deferred binding, but for now, the need to swap out implementations is low to non-existent.

The Judicious Part

The lesson I am driving at here is that the new application framework in GWT may be great for some portions of an application, but it probably isn’t appropriate for everything. Parts of the application that do not participate in history or navigation (such as global menus or controls) are not good candidates for Activities.

I think GWT and the new application framework represent a fantastic toolset for building user interfaces and entire applications. You just have to be smart about when and how you use them.