views:

55

answers:

1

I'm working on writing a small application using the WPF MediaKit, and I would like to record some video - my plan is, to store the last n seconds as raw audio/video frames, then write them out as an AVI when the user requests it.

Is there an easy way to actually pipe the data I've gathered from the Webcam into an AVI file? The trick is, I have to use buffered data, I can't directly just write a new DirectShow scene graph to write out the AVI file - for my app, the camera is running all the time, and the user signals the end of the capture (and we take the last few seconds or so).

The AVI doesn't have to be compressed in any way, I'm going to run it through Expression Encoder to do that. Any ideas?

A: 

If you want to buffer the data you could do the following:

  1. Create a capture graph using the SampleGrabber Filter (see Using the Sample Grabber), and copy data out of each incoming IMediaSample into an in memory buffer (a circular buffer of frames would help you limit the size in memory).
  2. After the users hits end, create a second graph for writing the file, feeding in the data from your in-memory buffer into the sample grabber.

A couple of gotchas:

  1. Don't hold on to IMediaSample pointers you get from the SampleGrabber callback. Each DirectShow filtergraph uses a memory pool of IMediaSamples, and if you take them all you will starve the graph.
  2. Be sure to use IMediaSample::GetSize and NOT IMediaSample::GetActualDataLength when copying out of the Media Sample. Some media samples are padded on DWORD (4-byte) boundaries and you want to preserve that for writing into the new AVI file.
Nick Haddad