Writing Multimedia Applications with Vala
Jim Nelson describes using Vala to write multimedia applications for the Linux desktop.
A user recently notified me about a build problem he encountered with a multimedia application I’ve been developing in Vala, a new object-oriented language designed for GNOME.
When I suggested he upgrade to the latest version of the Vala compiler, his response was “Why on earth don’t you use a standard language?”
This is my attempt to answer his question.
The story of Vala begins with Jürg Billeter. He noted that writing and refactoring GObject-based code in C required a lot of extra work that more modern languages automated. He also noted that these modern languages do not produce libraries that can be easily used in other environments. He wanted a full-featured object-oriented language that could produce C-style interfaces automatically. (See the Gee collection class for a perfect example of Vala solving this problem.) He mentioned his idea to fellow student Raffaele Sandrini, who jumped in to help build the early compiler.
In May 2006, valac became completely self-hosted. Work continues on it today.
Building a Better Multimedia App
Vala first came to my attention in January 2009, when I joined the Yorba Foundation. Yorba was founded that same month by Adam Dingle. His goal was to make high-quality open-source multimedia applications for GNOME. His whiteboard list of applications to develop included a multitrack audio recorder, a video editor, and a photo organizer. Yorba’s challenge is to craft open-source multimedia applications as fluid and friendly as the best-of-breed in the commercial world.
When I joined, our first task was to select a programming language. Adam and I both had plenty of experience with C, C++, and Java. Adam had worked with C# as well. In considering what language would best fit Yorba’s needs, we devised these criteria:
- A modern object-oriented language. We both admired Java and C#’s interpretation of the object-oriented paradigm. We were less enthusiastic about developing desktop multimedia applications in C++ due to its quirks, many due to its C legacy.
- Strict performance and memory-usage characteristics. Thinking about real-time applications like audio recording and video editing, we had concerns about background garbage collection pauses and virtual machine/script interpreter performance. With an application like a photo organizer, which might require tens of thousands of object instances to represent a large collection, we wanted fine-tuned control over heap traffic and allocation sizes.
- Language support for GObject and GType. Since we planned on targeting the GNOME desktop, we wanted full access to all of Gtk’s features, including signals, exceptions, threads, and reference counting. If you look at any Gtk project written in C, it’s apparent the price for these features is a lot of boilerplate and bookkeeping. We hoped to minimize this work or avoid it entirely.
- Strong type checking. Adam and I agreed at the outset: We’re not fans of weakly-typed languages. Too often we’d been burned on code errors that could have been caught by strict parsing. Forgiving languages have their place in the world, but rock-solid desktop applications may not be one of them.
- Broad and mature class libraries. In addition to the sort of libraries most modern languages provide—collections, threads, asynchronous I/O and streams—any multimedia desktop application will rely on the hard work of many other projects in the open-source community, such as GStreamer, gPhoto, SQLite, Pango, Cairo, and more. We wanted that code to be transparently available, either through bindings or native support.
We found that Vala meets all these requirements, and more.
Vala is described on its home page as a “compiler for the GObject type system”. This description is accurate but unappetizing. To put it another way: Imagine a language that offers GObject’s inheritance and signaling, GType’s run-time type checking, and GLib’s exceptions and thread support, and makes them available as native language features. That language is Vala.
It’s beyond the scope of this article to detail Vala in its entirety. The executive summary is, if you know C# or Mono you know 95% of Vala’s syntax; if you know Java, you’re 90% there.
Build and run-time environment
Unlike Java/C#/Mono, Vala programs build to a native executable. There is no virtual machine or bytecode interpreter. The current Vala compiler does this by generating C code and running it through the machine’s C compiler (typically gcc). In addition to translating the application’s logic to C, valac produces the GObject boilerplate a GNOME C programmer would have to crank out. It also automates the reference-count juggling normally done by hand.
However, Vala is not merely a fancy C preprocessor. In the future, valac may become a direct compiler or a gcc back-end. (This is similar to the historical development path of C++.)
As mentioned, we had concerns about garbage collection in our applications. GObject’s solution is to use reference-counting. The deallocation happens as soon as the last reference is dropped; there is no background thread interrupting the application. This means we have been able to characterize our code’s performance knowing it operates in a deterministic way.
Reference counting does come with a price, however. Most notable is the problem of circular references (where two objects hold references to each other and their reference counts never drop to zero). Vala data structures should be designed to avoid this situation. If such references are inevitable, Vala offers the weak keyword as a way to break this loop.
Vala is distributed with nearly one hundred bindings for various libraries, including Cairo, GStreamer, HAL, libglade, WebKit, and of course Gtk and GLib. Other bindings are always under development.
Bindings are located in VAPI files, which are Vala code with embedded directives that enable valac to generate proper C code for any library out there. (Writing one is much like writing an interface.) Additionally, if the library is based on GObject (such as gstreamer), a tool distributed with Vala called vapigen can use GObject introspection to discover the library’s interface and automatically produce a VAPI file.
Benefits & Pitfalls
Yorba started Shotwell, a digital photo organizer, in March of 2009. We’ve made great progress on Lombard, a video editor, and Fillmore, an audio recorder. We’ve also built a gedit plug-in, Valencia, which offers Vala code completion, method signature lookup, and jump to symbol. All of these applications are coded in 100% Vala.
Our experience with Vala has been largely positive. As the syntax is so familiar, new programmers can jump in and make contributions quickly. Our summer intern committed his first substantial patch his second week—in a language he’d never heard of before arriving. Standard development tools (such as gdb and sysprof) work without modification. We also coded up bindings for libgphoto2 and libexif in a short amount of time. We plan on submitting these to the Vala group for inclusion in the distribution.
The pitfalls we’ve faced all deal with the newness of the Vala project. The compiler is still in development and there are some bugs we’ve had to work around. There have also been some slight shifts in the language syntax, but the dust is settling as the language matures. There are also problems with the generated C code causing gcc warnings. (These usually deal with const issues in C.) Jürg is aware of these issues and is working to correct them.
Another problem has been the bindings themselves—when they’re incorrect, valac will produce incorrect C code, causing bugs or compilation failures. These issues are being fixed as they’re discovered, but they do crop up.
Still, the productivity gains we’ve realized overshadow these shortcomings. Development and debugging speed is akin to other modern languages.
Vala has proven to be a great choice for building GNOME multimedia applications.
Getting Started with Vala
To check out Vala, your first stop should be the Vala home page at gnome.org. Check out the tutorial and sample code to get you started. The FAQ and syntax pages are important reads to learn some of Vala’s fine details. Valadoc is a vital Web-based reference guide to bindings distributed with Vala. And, of course, subscribe to the mailing list to keep current.
About the Author
Jim Nelson works at Yorba developing multimedia applications for the Linux desktop.