Archive for January, 2010

Speaking Calendar: January 26-February 25

I’m going to be assisting the Microsoft office by doing some presentations of the PDC Roadshow.

January 26, 6 PM: Rockford .NET Users Group

February 3rd, 6 PM: Madison .NET Users Group

February 25, 6:30 PM: Lake County .NET Users Group

 

If you have a user group in the Wisconsin/Illinois/Indiana area and want to see this content, let me know. I can either come out to your group or help find a presenter who can deliver the content.

Leave a comment

Incorporating Video into a Silverlight 4 Application

html, body {
height: 100%;
overflow: auto;
}
body {
padding: 0;
margin: 0;
}
#silverlightcontrolhost {
height: 100%;
text-align:center;
}

Over the past week, I spent some time with Silverlight 4. I’m really impressed by how easy it is to incorporate video into an application. This post shows how to capture video from the camera and display the video on the screen. The post also handles grabbing single frames of video. You might use this type of arrangement to allow users to upload images, hold impromptu web casts, and to do video conferencing.

I’ve uploaded a barebones application that turns on video capture and displays the video:

 

To understand how this works, you need to be familiar with the following objects:

CaptureSource

CaptureDeviceConfiguration

VideoBrush

The CaptureSource type encapsulates the methods needed to interact with audio and video devices.

CaptureDeviceConfiguration acts as gatekeeper to the webcam and microphone. CaptureDeviceConfiguration knows how to ask the user for permission to use the webcam or microphone. It also remembers what the user said about using the webcam or microphone during the current session.

VideoBrush knows how to paint using a video as the source.

In order to interact with the camera, you need to know whether the user gave you permission and if not, you have to ask for permission. Once you have permission, you just need to capture video from the default camera, create the brush, and start capturing the video.

private CaptureSource _captureSource = null;
private void btnStartCapture_Click(object sender, RoutedEventArgs e)
{
    if (_captureSource != null && 
        _captureSource.State == CaptureState.Started)
    {
        return;
    }
    if(!CaptureDeviceConfiguration.AllowedDeviceAccess)
    {
        if(!CaptureDeviceConfiguration.RequestDeviceAccess())
        {
            return;
        }
    }

    _captureSource = new CaptureSource {
         VideoCaptureDevice = 
 CaptureDeviceConfiguration.GetDefaultVideoCaptureDevice()
       };
    var brush = new VideoBrush();
    brush.SetSource(_captureSource);
    _captureSource.Start();
    rectVideo.Fill = brush;
}

Later on, when the user wants to capture the current video as an image, you just do something like this to put the current frame into an image control:

 

private void btnGrabImage_Click(object sender, RoutedEventArgs e)
{
    if(_captureSource == null || _captureSource.State != CaptureState.Started)
    {
        return;
    }
    _captureSource.AsyncCaptureImage(
        image => imgCapture.Source = image
        );
}

As someone who spent way too much time in the C++/MFC era writing code to capture video from devices, I am incredibly impressed with the brevity of the code. I think the SL4 team did an awesome job here. Way to go!

 

You can grab the sample project here.

Leave a comment

F# is Changing My Style

Tonight, I was writing some code that made use of transactional WCF bindings. I wanted to do some experimentation with all the bindings available to see which ones support flowing transactions. The pre-built bindings that do this will create a TransactionFlowBindingElement when you ask them to call CreateBindingElements(). The types I’m interested are concrete (no abstract methods) and have a zero-argument constructor. A few years ago, I would have done a bunch of looping constructs to look at each element. However, I’ve been doing a lot more work with F#. While doing this experiment in C# for a project, I wound up writing the following instead:

 

static void Main(string[] args)
{
    var bindings = from bindingType in typeof (MessageContractAttribute).
                       Assembly.GetTypes()
                   where typeof (Binding).IsAssignableFrom(bindingType) &&
                   !bindingType.IsAbstract &&
                   (from constructorInfo in bindingType.GetConstructors()
                           where constructorInfo.GetParameters().Length == 0
                           select constructorInfo).Count() > 0
                   select bindingType;

    var txBindings = from bindingType in bindings
                     where
                         (from bindingElement in
                              ((Binding) Activator.CreateInstance(
                                bindingType)).CreateBindingElements()
                          where bindingElement.GetType() ==
                            typeof (TransactionFlowBindingElement)
                          select bindingElement).Count() > 0
                     select bindingType;
    foreach (var txBinding in txBindings)
        Console.WriteLine(txBinding.FullName);
}

I would’ve loved a Seq.iter for the last 2 lines of the function. I think this is a good reason to learn F# or another functional language. It’ll change how you write code elsewhere. I’m not saying that this code is more readable, but it is interesting.

So, what does the code write out as the transaction supporting bindings?

System.ServiceModel.NetTcpBinding

System.ServiceModel.NetTcpContextBinding

System.ServiceModel.WSHttpBinding

System.ServiceModel.WSHttpContextBinding

System.ServiceModel.NetNamedPipeBinding

System.ServiceModel.WSFederationHttpBinding

System.ServiceModel.WS2007FederationHttpBinding

System.ServiceModel.WS2007HttpBinding

System.ServiceModel.WSDualHttpBinding

Leave a comment

Follow

Get every new post delivered to your Inbox.