Warning: mysql_real_escape_string(): No such file or directory in /home/jcarlson/methodknowledgy.com/wp-content/plugins/easy-google-syntax-highlighter/easy-google-syntax-highlighter.php on line 50

Warning: mysql_real_escape_string(): A link to the server could not be established in /home/jcarlson/methodknowledgy.com/wp-content/plugins/easy-google-syntax-highlighter/easy-google-syntax-highlighter.php on line 50MKLabs | MethodKnowledgy

Key Features to Note

You can’t see it or manipulate it, but there is a playlist engine running with two tracks programmed in

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*

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.

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.

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.

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.

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.

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.

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.

I had originally planned to use HTML5’s new <audio> tag to actually render the audio in Project: Apollo. That went south, however, when I discovered a few critical problems with current implementations that disqualify the <audio> tag from contention.

Google Web Toolkit

Unfortunately, there isn’t much support (yet) in Google Web Toolkit for HTML5, but it is coming. This makes it difficult to take advantage of HTML5 right now since my GWT is the main framework at play in Apollo. Specifically, I’m not sure how to go about listening for DOM events that are specific to HTML5, such as the ones fired by the <audio> element. This isn’t a huge hurdle, because I can always bridge to GWT with native JavaScript (ick). But it does add another hurdle to overcome.

The Solution, Sadly, is Flash

I have used Longtail’s JW Player for Flash in the past on other projects. Despite my reluctance to do so*, I have decided to implement JW Player 5 for Flash as the audio rendering engine. JW Player 5 has the best support of formats I called for in my key features, and it is relatively simple to use.

* Okay, I just have some minor gripes with JW. Namely, they documentation is always a sore spot as it’s often conflicting or missing. I also feel like I’m being nickel and dimed to death by their licensing terms.

For a number of reasons, I find my collection of music to be a generally complicated topic.

It’s Enormous

First of all, I have a ton of it. Gigs of it. Thousands of tracks. I own a 60GB 5th-gen iPod. It’s full. Needless to say, I seem to fancy myself a collector of audio. I refuse to let go of old tracks I would never even admit to owning. Managing such a large library is tough and most would-be music libraries out there I have tried tend to choke on large databases.

It’s Amorphous

My music collection isn’t just large, it’s also fragmented by duplicate tracks, multiple formats, and duplicate tracks in different formats.

It’s On My PC, At Home

Up until recently, I used my MacBook Pro at work and a Gateway desktop at home. All of my music resided on my Gateway with its 1TB internal disk drive. Then I decided to retire the desktop. It’s not old or broken (Intel quad-core 2.4GHz with 6GB RAM and 1TB RAID disk setup). I just grew tired of switching platforms night and day. It’s like getting into your car every morning to drive to work and finding that the seats and mirrors are never where you left them.

My desktop doesn’t travel with me, either. And since I have more music than my iPod can store, much of my music stays locked up at home (yes, I realize that millions of people have dealt with this problem since the dawn of recorded audio).

It’s Not All Physical Audio, or Even Virtual Audio

Not all the music I listen to exists in a physical incarnation. Some of it came from CD’s, some of it I bought on iTunes. Some of it exists only through streaming radio stations such as Pandora.

So What to Do About It?

Unfortunately, I can’t just throw everything onto my MacBook and be off. I don’t have the disk space for it. And I have no interest in carrying a USB drive everywhere I go to store all my music.

I tinkered with a Network Attached Storage device, but many of the same problems remain. Namely, the device is still constrained to my home and to get decent performance out of it would require a total upgrade of my home network to Gigabit.

Helium Music Manager

I did try and thoroughly enjoyed using Helium Music Manager. It’s a fantastic audio database and has some pretty nifty features for audiophiles. However, it is missing one critical, deal-breaker feature for me: it’s Windows-only.

Enter the MKLabs

Since I’m a developer, I think it ought to be possible to build a solution to suit my needs and wants. So that’s what I’m going to do. Part of MethodKnowledgy is to explore and learn about code, so this makes perfect sense. I think I’ll call my little “code shed” of tinkering MKLabs.

I won’t go into all the details of features here and now, but basically I intend to build a web-based audio database application and leverage some of the coolest new platforms and technology out there to make it happen.

I’ll use cloud-based hosting systems like Google AppEngine to host my application and give it a scalable, high performance back end. The AppEngine DataStore also looks like a good candidate for storing massive amounts of data on my music.

And I’ll use new features like HTML5 Audio tags to render the audio, removing the need for special plugins or browsers (albeit I make a rather large assumption about the capabilities of average Joe’s browser).

I’m not trying to reinvent the wheel, though. Products like iTunes, Helium, and Grooveshark have proved what features do and don’t work. Now I just want something that works the way I want it to.

I aim to make regular updates here about the progress of my new music jukebox, codename Apollo. I’ll discuss major successes or failures along the way and try to document things that work well or don’t work well, and how to overcome certain challenges.