A few days ago I discovered that some prototype DirectShow-based code I had was suddenly taking a lot longer to open files. By a lot longer, I mean up to a minute -- at full CPU. As you might imagine, this was pretty irritating, especially since not only was it running at full CPU, but it was doing something that made the entire system performance especially suck during that time. Great.
A bit of digging with the mighty F12 profiler -- actually, I guess it was Ctrl+Break, since I was using CDB -- revealed it to be the DirectShow filter graph "intelligent connect" code. Specifically, it was taking an abnormally long time to connect the audio sample grabber. "Intelligent connect" in DirectShow refers to the way in which the filter graph manager will automatically find a sequence of intermediate filters to connect two filters together whenever a direct connection isn't possible. For instance, trying to connect a renderer that wants uncompressed video to a compressed video source will result in a video decoder being stuck in between. As you might imagine, this is both handy and hazardous, the latter coming into play when the filter graph comes up with some horror like MJPEG Compressor + MJPEG Decompressor to do a color conversion. I had suspected that at first, but inspection of the resulting filter graph via GraphEdit's remote connect function didn't show anything unusual.
Some more investigation with the debugger revealed that a lot of time was being spent in creating and destroying DirectDraw surfaces, which some filter was using as part of its media type check -- not a great idea, considering how expensive it is and how often media type queries happen. For a moment, I had thought maybe some application I had installed recently had added a ton of slow or broken filters, which I'd have to hunt down and then uninstall. The situation was pretty bad too, because the filter graph manager was recursing a lot and trying some pretty deep chains of filters. Then it dawned on me... why was the filter graph manager trying so many video filters to connect an audio filter? Shouldn't it know that it already had an audio stream, and that only audio filters should be checked? Unless....
I checked the connection code again, and it turned out that I wasn't trying to connect an audio pin, but rather a source type pin. That meant that the intelligent connect code had to figure out both the demultiplex and decoder filters for the intermediate connections. Then, after checking the sample grabber code, I had a light bulb moment. It turns out that I hadn't reimplemented the EnumMediaTypes() code on the sample grabber's input pin, so it was returning no media type structures. That meant that the filter graph manager was trying to establish a connection with the following media type information:
- Output filter (source): Provides raw AVI/MPEG/whatever data, with both audio and video.
- Input filter (sample grabber): shrug
The sample grabber did check the media type in the query function, so it only accepted audio connections. However, the filter graph manager had no way to know this since EnumMediaTypes() returned nothing, so the only way it found a connection was to do a brute force search through all possible combinations of filters that would make Dijkstra proud. And when you have M filters that can be combined up to a chain N long, the result unsurprisingly is a whole lot of CPU time spent trying connections. So I reimplemented EnumMediaTypes() to return a single entry with the media type set properly, and suddenly load time dropped to sub-second range.
Moral of the story? Make sure your filter isn't being too ambiguous with its reported connection requirements.