Menu

Taking still shots while recording video

2009-10-09
2012-10-29
  • mephistodan

    mephistodan - 2009-10-09

    I hope someone can help me with this, is there an example where I can record
    video and at the same time take snapshots from the camera?
    I have tried to combine elements from two examples (one for video and one for
    still shots) but i failed, I'm not really good with direct show so if there's
    an exisint sample someone is aware of I'll greatly appreciate.
    If not, could someone describe the basic steps I need to do to achieve this?

    Many thanks

     
  • mephistodan

    mephistodan - 2009-10-15

    Here's the code where the graphs are created copied from different samples
    this is working fine for recording video but how can I add a still capture?

    > Blockquote
    >

    Guid cat;
    Guid med;
    int hr;

    if (theDevice == null)
    return;

    // Make a new filter graph
    graphBuilder = (IGraphBuilder)new FilterGraph();

    //Create the Capture Graph Builder
    ICaptureGraphBuilder2 captureGraphBuilder = null;
    captureGraphBuilder = (ICaptureGraphBuilder2)new CaptureGraphBuilder2();

    // Link the CaptureGraphBuilder to the filter graph
    hr = captureGraphBuilder.SetFiltergraph(graphBuilder);
    if (hr < 0) Marshal.ThrowExceptionForHR(hr);

    //Add the Video input device to the graph
    hr = graphBuilder.AddFilter(theDevice, "source filter");
    DsError.ThrowExceptionForHR(hr);

    if (theAudioDevice != null)
    {
    ////Add the audio device filter to the graph
    hr = graphBuilder.AddFilter(theAudioDevice, "audio source filter");
    DsError.ThrowExceptionForHR(hr);
    }

    if (theCompressor != null)
    {
    ////Add the Video compressor filter to the graph
    hr = graphBuilder.AddFilter(theCompressor, "compressor filter");
    DsError.ThrowExceptionForHR(hr);
    }

    if (theAudioCompressor != null)
    {
    ////Add the audio device filter to the graph
    hr = graphBuilder.AddFilter(theAudioCompressor, "audio source
    compressor");
    DsError.ThrowExceptionForHR(hr);
    }

    // Try looking for an interleaved media type
    object o;
    cat = PinCategory.Capture;
    med = MediaType.Interleaved;
    Guid iid = typeof(IAMStreamConfig).GUID;
    hr = captureGraphBuilder.FindInterface(
    cat, med, theDevice, iid, out o);

    if (hr != 0)
    {
    // If not found, try looking for a video media type
    med = MediaType.Video;
    hr = captureGraphBuilder.FindInterface(
    cat, med, theDevice, iid, out o);

    if (hr != 0)
    o = null;
    }
    //videoStreamConfig = o as IAMStreamConfig;

    // Retrieve the stream control interface for the audio device
    o = null;
    cat = PinCategory.Capture;
    med = MediaType.Audio;
    iid = typeof(IAMStreamConfig).GUID;
    hr = captureGraphBuilder.FindInterface(
    cat, med, theAudioDevice, iid, out o);
    if (hr != 0)
    o = null;
    //audioStreamConfig = o as IAMStreamConfig;

    // Retreive the media control interface (for starting/stopping graph)
    mediaControl = (IMediaControl)graphBuilder;

    Guid mediaSubType = MediaSubType.Avi;
    if (isRecordingToFile)
    {
    // Render the file writer portion of graph (mux -> file)
    hr = captureGraphBuilder.SetOutputFileName(mediaSubType, FileName, out
    muxFilter, out fileWriterFilter);
    if (hr < 0) Marshal.ThrowExceptionForHR(hr);
    }
    // Render video (video -> mux)
    if (theDevice != null)
    {
    // Try interleaved first, because if the device supports it,
    // it's the only way to get audio as well as video
    cat = PinCategory.Capture;
    med = MediaType.Interleaved;
    hr = captureGraphBuilder.RenderStream(cat, med, theDevice, theCompressor,
    muxFilter);
    if (hr < 0)
    {
    med = MediaType.Video;
    hr = captureGraphBuilder.RenderStream(cat, med, theDevice, theCompressor,
    muxFilter);
    if (hr == -2147220969) throw new Exception("Video device");
    if (hr < 0) Marshal.ThrowExceptionForHR(hr);
    }
    }

    // Render audio (audio -> mux)
    if (theAudioDevice != null)
    {
    cat = PinCategory.Capture;
    med = MediaType.Audio;
    hr = captureGraphBuilder.RenderStream(cat, med, theAudioDevice,
    theAudioCompressor, muxFilter);
    if (hr < 0) Marshal.ThrowExceptionForHR(hr);
    }

    // Render preview (video -> renderer)
    cat = PinCategory.Preview;
    med = MediaType.Video;
    hr = captureGraphBuilder.RenderStream( cat, med, theDevice, null, null);
    if (hr < 0) Marshal.ThrowExceptionForHR(hr);

    IVideoWindow videoWindow = null;

    // Get the IVideoWindow interface
    videoWindow = (IVideoWindow)graphBuilder;

    // Set the video window to be a child of the main window
    hr = videoWindow.put_Owner(this.Handle);
    if (hr < 0) Marshal.ThrowExceptionForHR(hr);

    // Set video window style
    hr = videoWindow.put_WindowStyle(WindowStyle.Child |
    WindowStyle.ClipChildren);
    if (hr < 0) Marshal.ThrowExceptionForHR(hr);

    // Position video window in client rect of main application window
    hr = videoWindow.SetWindowPosition(0, 0, this.Width, this.Height);
    DsError.ThrowExceptionForHR(hr);

    // Make the video window visible
    hr = videoWindow.put_Visible(OABool.True);
    DsError.ThrowExceptionForHR(hr);

    hr = videoWindow.put_MessageDrain(this.Handle);
    DsError.ThrowExceptionForHR(hr);

    if (muxFilter != null)
    Marshal.ReleaseComObject(muxFilter);

    //if (sink != null)
    // Marshal.ReleaseComObject(sink);

    Marshal.ReleaseComObject(captureGraphBuilder);

    }

    protected override void WndProc(ref Message m)
    {
    switch (m.Msg)
    {
    case 0x0201:
    OnClick(EventArgs.Empty);
    break;
    }
    base.WndProc(ref m);

    > Blockquote

     
  • snarfle

    snarfle - 2009-10-16

    Have you taken a look at any of the samples that use the SampleGrabber filter?

     
  • mephistodan

    mephistodan - 2009-10-16

    Yes I have looked but the way the capturer is defined and filters created is
    different in these examples than the video example, I have tried to combine
    them but keep gettting exceptions, I thought it's just as simple as adding a
    still pin ? or am i missing something? can you point me to something that can
    help?

    Many thanks

     
  • Peter_B

    Peter_B - 2009-10-16

    If you use the sample grabber, you can save a bitmap in the BufferCB function.
    Something like this: Bitmap.Save(Path, Drawing.Imaging.ImageFormat.Bmp)

     

Log in to post a comment.