Posted 11/18/2008 12:54:00 AM. 13 comments
In my first post in this series I introduced the MediaPlayer class that we’ve added to the Silverlight 2 player templates in Expression Encoder 2 SP1 and highlighted some of its internals. In this post I will explore the topic of how to debug Expression Encoder MediaPlayer projects, specifically the case where you are encoding to the IIS Smooth Streaming format. I focus on this case because it is the most involved template debugging scenario as will become apparent.
But before we dive in I’d like to touch on IIS Smooth Streaming and also explain how Expression Encoder previews its output.
IIS Smooth Streaming falls into a class of delivery known generically as Adaptive Streaming. With this method, the video asset is encoded at multiple bitrates in a GOP aligned fashion which allows client-side Silverlight managed code running inside of a custom MediaStreamSource class to make decisions at runtime based on network and local conditions (e.g. dropped frames) and switch to lower or higher steams as needed.
Over the wire, streams are delivered in chunks using progressive HTTP, each chunk containing a couple of seconds worth of video samples. In classical adaptive streaming, these chunks are typically laid down on disk as many discrete files. One of the nice features of IIS Smooth Streaming is that, on disk, the chunks are all stored in a single file per stream rather than many hundreds or thousands of fragments. The IIS Smooth Streaming Module installed on the server then translates chunk requests from the wire into file offsets and serves the chunks; effectively virtualizing the concept of a chunk.
This is a huge management win. However, from an Expression Encoder preview point of view it complicates matters since it would suggest that you would need an IIS server running locally in order to preview the output of an encoding job.
We solved this problem by adding support for IIS Smooth Streaming to the private web server that is use to host the web preview output.
When you encode an IIS Smooth Streaming job and enable Preview in browser, we first generate the file set (which, for IIS Smooth Streaming, looks something like this):
And then start an in-process instance of our webserver which handles the serving up the files over HTTP and as well as the logical to physical chunk generation process where necessary. This works great for previewing your job locally. For deployment in the real world, both IIS7 and the IIS Smooth Streaming module are required.
Now that we understand more about IIS Smooth Streaming, we can move onto the topic of debugging Silverlight 2 Templates. We have encoded our content into a template including the IIS Smooth Streaming client-side MSS code and we want to observe the execution as video plays back. For example, maybe we want to debug into the heuristics module and look at the network requests for chunk retrieval.
In order to debug the client-side player code including what’s going on inside of the MediaStreamSource, we have to manually attach the debugger to the instance of IE that gets launched by Expression Encoder to ensure that the files are getting served up by our webserver.
The easiest way to illustrate this is with a worked example:
1) Import Bear.wmv
2) Pick the Adaptive Streaming template
3) Pick the default Silverlight 2 Player skin
4) Edit a copy in Visual Studio
5) Switch to VS and compile the project; open the MediaPlayer project and then open the MediaPlayer.cs source file; set a breakpoint in the constructor
6) Switch back to Expression Encoder and encode the job; wait until the preview launches in IE.
watch closely and see how, initially, the lowest bitrate plays and looks a little blury then after a few seconds playback seamlessly switches to a higher bitrate. This is adaptive streaming at work.
7) Switch back into Visual Studio and select Attach to Process from the Debug menu (or use ctrl-alt P). You now need to attach to the correct instance of IE.. note that if you are running IE8, this may not be the instance you expect. The one you want to connect to is the one with Silverlight in the type column. Sometimes it is best to close all instances of IE before you attempt to debug to make it easier to find the right one.
8) Switch back to Internet Explorer and hit F5 to make the browser refresh.. at this point you should hit the breakpoint you set in the constructor of MediaPlayer.
It is worth mentioning that, in Visual Studio, you are working with the source code of the template but are attaching the debugger to the published output of the job.
This is a subtle distinction; what it means, in practice, is that if you make changes to the source code of the template in VS, you need to republish the job by clicking encode before you’ll see the changes take effect. This will thankfully be really fast due to the Partial Rebuild feature that only re-encodes the video if settings have been changed. It also means that you cannot press F5 in VS and have the project launch with your encoded project.
If you are interested in a second debugging technique that enables the debugging of published output, please leave a message in the comments and I’ll do a follow up post on that topic.
In my next post I’ll focus on the internals of the AdaptiveStreaming code, talk about our default Heuristics implementation and show how you can replace it with your own.