Recently I came across a problem where various Media topologies in Windows were represented in different ways. For example, IMFMediaSession topology is represented using IMFTopology (http://msdn.microsoft.com/en-us/library/windows/desktop/ms705488(v=vs.85).aspx) whereas IMFSoureReader and IMFSinkWriter based topologies are represented as a chain of transforms.
I was part of a team that developed a generic library to represent all Media topologies - Topology Builder. The Topology builder library essentially does the following:
a. Helps transform any given Media topology into a Directed Graph.
b. Helps serialize this graph into a JSON string
c. Helps de-serialize the JSON back into a Directed Graph
Building the graph
The Directed Graph (with cycles) implementation was such that it was a collection of vertices. Vertex creation and deletions were managed by a memory pool. Each vertex had:
- a unique identifier
- list of neighbors
- a name attribute
- list of properties associated with the vertex. Each property in the vertex was a key-value pair of GUID and PROPVARIANT.
The public interface for building the directed graph exposed the following methods:
- ability to add a vertex
- ability to associate a set of properties to a vertex
- ability to add an edge from one vertex to another
- ability to serialize the graph into a JSON string (explained below)
The serialized graph would be represented as a JSON string. We picked JSON to be the format for serialization since JSON is a well know format and there are standard libraries available for JSON writers and readers. We picked a C++ implementation for JSON writer four purpose. For an example of a sample topology representation in JSON, please look at the attached file in this blog.
We used JsonObject present under Windows.Data.Json (http://msdn.microsoft.com/en-us/library/windows/apps/windows.data.json.jsonobject.parse.aspx) namespace in .NET for de-serializing the JSON into:
a. a class
b. a DGML representation for debugging (http://en.wikipedia.org/wiki/DGML)
c. a Database representation for statistical analysis
Here's an example view of a Topology JSON de-serialized into DGML:
The entire implementation was a combination of C++ and C# modules.
- Topology builder graph was a C++ implementation
- JSON serialization was a C++ implementation
- JSON de-serialization was a C# implementation
The was highly performing solution with respect to memory and execution time. The space and time complexity of building a Windows Capture Topology could vary between 6 KB (~1 ms) - 17 KB (~3 ms) for building the graph and serializing it.