Mike Taulty's Blog
Bits and Bytes from Microsoft UK
Silverlight 4 – Yet More on Capturing Video from Webcams

Blogs

Mike Taulty's Blog

Elsewhere

Archives

In this previous post, I wrote some code that took a particular approach to saving uncompressed AVI files (i.e. just frames consisting of device-independent-bitmaps) from the webcam in Silverlight 4 by writing an application that;

  1. Saved video from the webcam to isolated storage in a “raw” file format
  2. Offered a process to take these isolated storage files in “raw” file format and convert it to an AVI file stored outside of isolated storage

I went this route because I’d originally intended to try and compress the video frames ( which would take a tonne of time and needed to be divorced from the capturing of the video ) but I never got there. Consequently, it all got a bit more complex than it perhaps needed to be and it also involved the application requiring elevated trust to do step (2) above.

Over the Christmas break, I got to rewrite that code ( on a very rainy day ) and I’ve another version of the application. It’s embedded in the page below, doesn’t require full-trust and should run in the browser with the Silverlight 4 beta ( if you’ve got a webcam that is, I don’t do much in the way of checking around that :-) ).

Note – you might want to right mouse and install this out-of-browser as I notice that the sizing isn’t “quite perfect” when it’s in such a small window as the one below.

 
This code takes a different approach in that it works something along the lines of;
 
  1. On start recording, use the file save dialog to grab a file stream from the user securely.
  2. Synchronously write out the RIFF AVI file headers once video capture starts.
  3. As video capture progresses, add each video frame to a queue.
  4. Drain the queue using asynchronous writes from the queue to the file selected by the user.
  5. When video capture ends, patch up a few header entries in the RIFF AVI file headers that can’t be written until capture has ended.

The basic idea being two-fold

  • To avoid doing large synchronous writes as the video frames come in as that might slow the capture process too much
  • To try and drain the queue in steps (3,4) above reasonably quickly with async IO to avoid it getting too big and becoming too much of a memory drain.

It seems to work out reasonably well – naturally, the file being written is still uncompressed video so it is still extremely large. I also chose to write out 32bpp rather than 24bpp so it’s even bigger than last time around although that’d be easy enough to change.

The application runs something like this – you click to switch on the webcam;

image

you get the persmissions dialog;

image

that enables the record button ( yes, it’s got the icon for a “play” button, sorry );

image

clicking that button raises the file dialog;

image

and then it records with a basic progress indicator;

image 

until you click the stop button.

There’s still at least a couple of outstanding problems with this, especially;

  • I can only get the outputted AVI file to play in Quicktime. I’m not sure what’s wrong with the file I’m writing that makes it unplayable in Media Player but Media Player doesn’t like it right now.

but, once again, if the code’s of use to you then I’ve put the solution here for downloading. It’s not quite-as-shoddy code as the last variant on this theme but it’s quite a way from “production level”.

Enjoy!


Posted Mon, Dec 28 2009 6:11 AM by mtaulty
Filed under: ,

Comments

András Velvárt wrote re: Silverlight 4 – Yet More on Capturing Video from Webcams
on Mon, Dec 28 2009 9:19 AM

Mike, this looks great - the only problem I found is that the resulting video plays too fast. Also, the file seems to have some problems az VLC says it would like to repair it - as if the avi was not closed properly or so.

Is the resulting AVI compressed, or is it just a raw file? a few seconds took 36 megabytes for me....

Denny wrote re: Silverlight 4 – Yet More on Capturing Video from Webcams
on Mon, Dec 28 2009 12:24 PM

nice work.

really it seems to me like MSFT needs to just pony-up and add some support for saving and for streaming as it seems like everyone keeps asking how to make a video chat app.

or other forms of send my stream to some other place / user/ target.

Denny wrote re: Silverlight 4 – Yet More on Capturing Video from Webcams
on Mon, Dec 28 2009 12:25 PM

PS:  please make the page stop asking me to install SL 4 beta on every page load!

Billy K wrote re: Silverlight 4 – Yet More on Capturing Video from Webcams
on Mon, Dec 28 2009 5:50 PM

I love webcams!  Next to the computer, this is the greatest invention!

Mike Taulty's Blog wrote Silverlight 4 – WebCams.OnceMoreWithAudio()
on Wed, Dec 30 2009 5:24 AM

Ok, ok – this will be the last post about webcams and capturing video. I’ve got to stop :-) BUT…I thought

mtaulty wrote re: Silverlight 4 – Yet More on Capturing Video from Webcams
on Wed, Dec 30 2009 5:32 AM

Andras,

The file is not compressed - it will be HUGE. It's raw bitmap frames.

Also, the file definitely has some "issues" and Media Player doesn't like it so if anyone has a tool that analyses RIFF/AVI files and tells you what's wrong with them then let me know. BTW - I have done a later post where the file has changed slightly but it still has "issues" :-)

( BTW - I've tried RiffPad and one or two other RIFF tools ).

Thanks,

Mike.

Dave Smits wrote re: Silverlight 4 – Yet More on Capturing Video from Webcams
on Thu, Jan 14 2010 3:08 AM

here is an example of recording and playback the webcam stream www.familie-smits.com/SilverlightWebcamRecording.aspx

dave smits wrote re: Silverlight 4 – Yet More on Capturing Video from Webcams
on Thu, Jan 14 2010 3:59 AM

Hi Mike,

Great example. I did another aproach and now possible to record and playback the webcam source. Here an example: www.familie-smits.com/SilverlightWebcamRecording.aspx

hasan wrote re: Silverlight 4 – Yet More on Capturing Video from Webcams
on Fri, Jan 29 2010 4:05 AM

I can run the the sample code but whn i want to save the recorded .avi file it sends an error.

  void OnBufferQueueShutdown(object sender, EventArgs e)

   {

       try

       {

           frameCountBookmark.Value = (uint)frameCount;

           frameCountBookmark.ReplaceValues();

           sampleCountBookmark.Value = (uint)sampleCount;

           sampleCountBookmark.ReplaceValues();

           listMovi.Close();

           riffChunk.Close();

           this.stream.Flush();

           this.stream.Close();

           this.stream.Dispose();

       }

       catch(Exception Exp)

       {

           string err = Exp.Message.ToString();

       }

   }

listMovi.Close(); i m getting an error here Object reference not set to an instance of an object ' plz can u tell the solution?