Archive for June, 2009

Published by Arto Jarvinen on 27 Jun 2009

How do you wrap your brain around 1 MLOC?

The short answer is: you don’t! When stumbling upon 1 million lines of code it is more important to understand what you don’t need to understand than to understand what you need to understand.

My mission is to add my synchronizing renderer filter as an option to MPC-HC. To do this, I need to understand where to insert my code. And to understand that, I need to know how the existing version of MPC-HC is wired, what its architecture is. How can I do that when there is no documentation of the architecture whatsoever and comments are as rare as …?

Since I do software development just for fun, I seldom have the time to penetrate a technology area in a way that would be possible if I worked with it 8+ hours a day. My interests also span from hacking a bit of php to get my photo site to behave to developing multi-threaded and time-critical applications with DirectX and DirectShow. Sometimes working for a living prevents me from working on my own projects for weeks or even months which means that I forget a lot (and this doesn’t exactly get better with age). These circumstances have on the other hand forced me to get better at getting under the hood of a new application (and rapidly identifying what parts to remain blissfully ignorant about). These are some of my tactics and tricks to do that:

Sequence diagram
A picture and a thousand words.
  • First of all, I read up on the basics of any frameworks used that I don’t already know (or have forgotten about). Even if the application documentation is non-existent, the documentation of the commercial frameworks (in this case at least MFC, ATL, and DirectShow) is usually good. Since these are all Microsoft frameworks, there is a plethora of books to read too if you are of the book-reading persuasion. I mostly use Google. The frameworks often determine the taxonomy and the architecture of the application to a large degree.
  • When learning a new framework such as DirectShow, I almost always find it much easier to generalize from examples (samples in this case) than to specialize from general descriptions; I found it for instance much easier to learn UML from annotated examples than from a meta-model. The meta-model became useful much later. I compiled and played around with the excellent samples that came with the DirectShow library. Another excellent source of samples that I consult quite often is the Code Project site
  • I usually draw simple UML class diagrams of the basic classes of the application together with the (seemingly) most interesting instance variables and operations. Sometimes I also draw the inheritance diagram to remember where to look for variables and operations when I can’t find them in the class itself. Visual Studio has a pretty good class browser too where it is easy to see inheritance hierarchies.
  • Once the main classes are mapped, in a media application a good starting point is to try to understand the “play file” use case or similar. Here I usually draw a simplified UML sequence diagram mapping the use case realizations of the interesting use cases (see image). Reading code is useful for documenting the use case realizations but sometimes I get lazy or find it hard to follow the execution flow of some other reason. In these cases I add traces to the beginnings and the ends of the operations that I believe take part of the use case realization and then run the application in debug mode. This is admittedly a rather dumb method but I make progress even when I’m tired or when the code is a real eyesore. And sometimes grunt work can be quite relaxing.

DirectShow is a modular architecture built on filters and media formats and the filter graph that connects the filters together into a media application. Since the media formats are standardized, it is relatively easy to replace a filter with an other filter in the application (filter graph) as long as it can handle the same media formats as the replaced filter. I can thus put my renderer filter in the place of any other renderer filter as long as it handles the same input formats (there are no “output formats” as the renderer is typically the last filter in the filter graph producing video on the screen). Because of the same reason I can safely ignore the source code for all the other built-in filters such as “source filters”, “splitters” and “decoders” as long as I see to it that I can handle the media formats that they output. That probably eliminates more then 900 KLOC of code to understand, including a few thousand lines of assembler that I can really live without understanding.

Ok, so where does that new code go?

Links

[1] UML tutorial from Sparx Systems.

[2] Introduction to DirectShow.

Published by Arto Jarvinen on 21 Jun 2009

BOOL CMPlayerCApp::InitInstance()

When I did my “comeback” to programming, I started with C# and .NET which was a breeze in Visual Studio. To my disappointment I then learned that DirectShow was still entirely written in C++ and based on COM so I had to regroup and learn the intricacies of COM (Don Box’s treatise on COM is second to no mathematics textbook in rigor) and a little ATL to simplify some windowing. For the simple stuff i did, I didn’t feel I needed anything like MFC. I now realize that MPC-HC is based on MFC so I guess I finally need to accept it as a fact of life too that won’t go away anytime soon. (See for instance this video for support of that last statement.)

Anyway, a friendly person pointed me to the “WinMain” of MPC-HC in a comment on the previous post. It wasn’t an “ATL WinMain”. It was an “MFC WinMain”. So I guess I need to replace that ATL textbook in the previous post with an MFC textbook.

Published by Arto Jarvinen on 20 Jun 2009

Where is WinMain?

ATL Book
Back to basics

Not being a professional programmer, I will always struggle with some basic stuff while navigating quite easily in some advanced areas where I’ve gained experience such as particular aspects of the DirectShow media API. According to the ohloh open source tracking site the Media Player Classic Home Cinema project contains about one million lines of code (including the odd comment line). A lot of those lines are for various filters that are integrated into the player in a “hard” way and thus of little concern for my mission. But even disregarding all that code there are so many trees that I haven’t yet seen the forest; I’m still looking for that WinMain (I realize I will probably not find exactly that but perhaps some corresponding ATL function or similar.) There is no “getting started” documentation for developers or architecture description of the software. Admittedly I didn’t expect to find anything like that either. Back to reading code and that old ATL book most of which I have forgotten…

Published by Arto Jarvinen on 19 Jun 2009

Successful build!

One of the great things with FLOSS projects is that when you post a question in a forum chances are that you get an answer pretty quickly. This time it took about four minutes! After some encouragement and a download and build of ATL_Server_Source_and_Headers_9_0_70425_Alpha.zip the last build errors went away. A bunch of warnings are still issued when building but the player plays a BlueRay movie with subtitles so I must have done something right!

========== Build: 62 succeeded, 0 failed, 0 up-to-date, 1 skipped ==========!

Now some serious code review to see where my stuff goes.

Published by Arto Jarvinen on 19 Jun 2009

A mug of coffee

“Firing up the compiler” wasn’t entirely accurate as several compilers (and an assembler to boot) are required to compile all files. Installing the MinGW as suggested in the instructions for compiling MPC-HC for C++ was easy enough.

There are no further instructions though as to how the build process uses the compiler so I first added it the PATH. I still got an error message telling me that I hadn’t defined the environment variable MINGW32. Checking the bat-file that uses the compiler revealed that I needed such an environment variable that then in turn got added to the path in the bat-file.

Coffee
A mug of ambition

I learned from another error message that I also needed to define the environment variable MINGW32_GCCLIB to point at the directory with the libgcc.a library.

I needed to downgrade my DirectX SDK as the one that I was using didn’t include the DirectX7 stuff needed for MPC-HC. Fortunately several versions of this library can happily coexist on the harddrive.

The instructions for compiling MPC-HC don’t say anything about the yasm assembler that is required for building for instance the flac library. I downloaded the win32 version of yasm (I used Google to find it), renamed it to yasm.exe and copied it to my C:\Program Files\Microsoft Visual Studio 9.0\VC\bin directory according to these instructions. Now VS reports a successful build of libflac but I still get a bunch of warnings. We’ll see in due time if they indicate real problems.

With that cup of coffee I managed to build most of the solution except the player itself. Some ATL server related files only exist in the Visual Studio 2005 folder on my computer and I doubt that I should really refer to files there from a project built in Visual Studio 2008. I have posted a question on the MPC-HC thread and wait for some enlightenment.

Published by Arto Jarvinen on 18 Jun 2009

Firing up the compiler and linker

Having added references to the include files for DirectShow BaseClasses and DirectX and the corresponding library files to the VS 2008 options, a fair share of the MPC-HC solution actually compiles. At the end of the day I have "Build: 1 succeeded, 7 failed, 54 up-to-date, 1 skipped". I seem to have a problem with the path to the MINGW compiler or libraries that are used to build some of the components. Time to call it a day and tackle the rest with a fresh brain and a mug of strong breakfast coffee.

Published by Arto Jarvinen on 16 Jun 2009

The first checkout of Media Player Classic

Having announced my GothPlayer a few months ago on this thread at doom9′s forum, I was kindly invited and encouraged to merge my renderer features to the Media Player Classic Home Cinema (MPC-HC), an open source media player project. The advantage with tagging along a larger project is of course that it already has a lot of bells and whistles, one example being subtitles for BluRay which isn’t entirely trivial to implement.

It took me a while to get my act together and to persuade my employer to finance a necessary upgrade to my Visual Studio but now I’m set to go! This is my first open source project and I’m not a programmer really. I am professionally interested in the phenomenon of open source though from a a software development process and anthropological point of view so I am going to take this opportunity to write the occasional blog post about my wins and woes relating to this project.

TortoiseSVN

Before my first check-out I hadn’t used the Subversion configuration management system at all. It has fast replaced CVS as the preferred CM solution for open source projects. I therefore spent some time installing and playing with a Subversion server on my home web server running Ubuntu following the instructions in the Ubuntu Community Documentation. I configured the WebDAV protocol to access Subversion as it was the first one introduced in the how-to guide and I already had Apache2 running. Good instructions for setting up SSL with Apache2 can be found here. The textbook on Subversion can be found for free here. I installed the TortoiseSVN client on my Windows computer.

While the open source Subversion stuff was very easy to find and download, Microsoft Visual Studio 2008 was an entirely different ball-game where I live. There was no way to purchase a download copy on-line in Sweden. The Swedish Microsoft site referred to retailers that sell shrink-wrapped boxes! One retailer I checked promised a 7 day delivery time on their web site. I purchased my first software on-line about 17 years ago so I thought we had come a bit further! I finally ended up buying the integrated development environment from the UK Microsoft site that had a download option and didn’t require a State code like most US sites do. I do like Visual Studio and a lot of the Microsoft media technology but having to buy a shrink-wrapped cardboard box today is ridiculous, not to mention environmentally unsound.

The MPC-HC thread on doom9′s contains the URL of the Subversion repository which I typed into the TortoiseSVN client’s checkout dialog and see and behold: I now have the latest version of the trunk (main line of development) of MPC-HC on my C-drive. So far so good. The next milestone is to get everything to compile and link. That’ll be interesting…