Two problems with DirectShow capturing programs

Source: Internet
Author: User

Two problems with DirectShow capturing programs

(14:41:09)

There have been two problems in the previously written program. When the program is running, click to open the video and close the video. Click the video to open the dialog box "can't build graph ". Second, the size of the collected image cannot be changed, which is always fixed at 320*240. Note that this does not mean the size of the display window.

Combined with some information, we finally solved the problem.

Problem 1: The pvw is rarely released in the capturevidieo class destructor. Dizzy. All classes found on the Internet are leaked and released.

Question 2:

Solve the problem by combining the following information:

1

(1) obtain the iamstreamconfig Interface
HR = GCAP. pbuilder-> findinterface (& pin_category_capture,
& Mediatype_interleaved,
GCAP. pvcap, iid_iamstreamconfig, (void **) & GCAP. pvsc );
(2) Control
If (GCAP. pvsc) // & GCAP. fuseframerate)
{
HR = GCAP. pvsc-> getformat (& PMT );
// DV capture does not use a videoinfoheader
If (hR = noerror)
{
If (PMT-> formattype = format_videoinfo)
{
Videoinfoheader * PVI = (videoinfoheader *) PMT-> pbformat;
PVI-> bmiheader. biwidth = 320;
PVI-> bmiheader. biheight = 240;
PVI-> bmiheader. bisizeimage = 320*240*3;
HR = GCAP. pvsc-> setformat (PMT );
}
Deletemediatype (PMT );
}
}
2

Adjust the video output format
We know that video streams can have multiple output formats. One device supports 16-bit RGB, 32-bit RGB, and yuyv. In each format, the device can also adjust the video watermark size.
On a WDM driver, the iamstreamconfig interface is used to report the format of the output video of the device. For a VFW device, you can set it in the dialog box. See the preceding content.
The capture pin and preview pin of the filter both support the iamstreamconfig interface. You can use icapturegraphbuilder2: findinterface to obtain the iamstreamconfig interface.

Iamstreamconfig * pconfig = NULL;
HR = pbuild-> findinterface (
& Pin_category_preview, // preview pin.
0, // any media type.
Pcap, // pointer to the capture filter.
Iid_iamstreamconfig, (void **) & pconfig );

The device also supports a series of media types. For each media type, the device must support a series of attributes, such as the size of the volume, how the image is scaled, and the coverage rate range.
Use iamstreamconfig: getnumberofcapabilities to obtain the number of media types supported by the device. This method returns two values, one being the number of media types and the other being the size of the structure required by the attribute.
The size of this structure is very important, because this method is used for video and audio. The video adopts the video_stream_config_caps structure, and the audio uses the audio_stream_config_caps structure.
The iamstreamconfig: getstreamcaps function is used to enumerate media types. To pass a sequence number as a parameter to this function, the function returns the media type and the corresponding attribute struct. View the code

Int icount = 0, isize = 0;
HR = pconfig-> getnumberofcapabilities (& icount, & isize );
// Check the size to make sure we pass in the correct structure.
If (isize = sizeof (video_stream_config_caps)
{
// Use the video capabilities structure.
For (INT iformat = 0; iformat <icount; iformat ++)
{
Video_stream_config_caps SCC;
Am_media_type * pmtconfig;
HR = pconfig-> getstreamcaps (iformat, & pmtconfig, (byte *) & SCC );
If (succeeded (HR ))
{
// Delete the media type when you are done.
HR = pconfig-> setformat (pmtconfig); // reset the video format
Deletemediatype (pmtconfig );
}
}

You can call iamstreamconfig: setformat to set the new media type.
HR = pconfig-> setformat (pmtconfig );
If the PIN is not connected, it tries to use a new format when it is connected. If the PIN is already connected, it will be reconnected in a new media format. In any case, the downstream filter may reject the new media format.
Before setformat, you can modify the video_stream_config_caps structure to reset the media type.
For example:
If getstreamcaps returns the 24-bit RGB format and the size of the queue is 320x240 pixels, you can check the major type, subtpye, and format values of the media type.

If (pmtconfig. majortype = mediatype_video )&&
(Pmtconfig. Subtype = mediasubtype_rgb24 )&&
(Pmtconfig. formattype = format_videoinfo )&&
(Pmtconfig. cbformat> = sizeof (videoinfoheader ))&&
(Pmtconfig. pbformat! = NULL ))
{
Videoinfoheader * pvih = (videoinfoheader *) pmtconfig. pbformat;
// Pvih contains the detailed format information.
Long lwidth = pvih-> bmiheader. biwidth;
Long lheight = pvih-> bmiheader. biheight;
}

The video_stream_config_caps structure contains the maximum and minimum values of the video length and width of the media type, and the incremental amplitude value, that is, the amplitude of each adjustment of the video size. For example, the device may return the following values:
? Minoutputsize: 160x120
? Maxoutputsize: 320x240
? Outputgranularityx: 8 pixels (horizontal step size)
? Outputgranularityy: 8 pixels (vertical step size)
In this way, you can set the width in the range of (160,168,176,... 304,312,320), and set the height value in (120,128,136,... 104,112,120,

Figure 6
To set a new value, directly modify the value returned by the getstreamcaps function,

Pvih-> bmiheader. biwidth = 160;
Pvih-> bmiheader. biheight = 120;
Pvih-> bmiheader. bisizeimage = dibsize (pvih-> bmiheader );

Then pass the media type to the setformat function to modify the video format.

The three modified programs are as follows:

CPP

Ccapturevideo ::~ Ccapturevideo ()
{
// Stop media playback
If (bisvideoopen)
{If (m_pmc) m_pmc-> stop ();
If (m_pvw)
{
M_pvw-> put_visible (oafalse );
M_pvw-> put_owner (null );
}
Srelease (m_pbf );
}
Srelease (m_pcapture );
Srelease (m_pmc );
Srelease (m_pgb );
Srelease (m_pvw); // you can add this sentence and dizzy. Otherwise, you cannot open the video twice in a program.

Couninitialize ();

}
Hresult ccapturevideo: Init (INT ideviceid, hwnd)
{
Hresult hr;
HR = initcapturegraphbuilder ();
If (failed (HR ))
{
Afxmessagebox ("failed to get video interfaces! ");
Return hr;
}
// Bind device filter. We know the device because the ID was passed in
If (! Bindfilter (ideviceid, & m_pbf ))
{
Afxmessagebox ("no USB camera found! \ N check the device and try again! ");
// Postquitmessage (0 );
Return s_false;
}
HR = m_pgb-> addfilter (m_pbf, l "capture filter ");

// HR = m_pcapture-> renderstream (& pin_category_preview, & mediatype_video,
// M_pbf, null, null );

// Create a sample Grabber
HR = m_pgrabber.cocreateinstance (clsid_samplegrabber );
If (! M_pgrabber)
{
Afxmessagebox ("fail to create samplegrabber, maybe qedit. dll is not registered? ");
Return hr;
}
Ccomqiptr <ibasefilter, & iid_ibasefilter> pgrabbase (m_pgrabber );

// Set the video format
Iamstreamconfig * pconfig = NULL;
HR = m_pcapture-> findinterface (& pin_category_capture, 0, // any media type.
M_pbf, // pointer to the capture filter.
Iid_iamstreamconfig, (void **) & pconfig );
Am_media_type * PMT;
HR = pconfig-> getformat (& PMT );
// Zeromemory (PMT, sizeof (am_media_type ));
PMT-> majortype = mediatype_video;
PMT-> subtype = mediasubtype_rgb24;
PMT-> formattype = format_videoinfo;
Videoinfoheader * PVI = (videoinfoheader *) PMT-> pbformat;
PVI-> bmiheader. biwidth = 640;
PVI-> bmiheader. biheight = 480;
PVI-> bmiheader. bisizeimage = 640*480*3; // set the frame size, instead of simply changing the preview window size.
HR = pconfig-> setformat (PMT );
Freemediatype (* PMT );
Am_media_type MT;
// Zeromemory (& mt, sizeof (am_media_type ));
// Mt. majortype = mediatype_video;
// Mt. Subtype = mediasubtype_rgb24;
// Mt. formattype = format_videoinfo;
// Videoinfoheader * VIH = (videoinfoheader *) Mt. pbformat; // image file information cannot be set here
//
// HR = m_pgrabber-> setmediatype (& mt );

If (failed (HR ))
{
Afxmessagebox ("fail to set media type! ");
Return hr;
}

HR = m_pgb-> addfilter (pgrabbase, l "Grabber ");

If (failed (HR ))
{
Afxmessagebox ("fail to put sample grabber in graph ");
Return hr;
}

// Try to render preview/capture pin
HR = m_pcapture-> renderstream (& pin_category_preview, & mediatype_video, m_pbf, pgrabbase, null );
If (failed (HR ))
HR = m_pcapture-> renderstream (& pin_category_capture, & mediatype_video, m_pbf, pgrabbase, null );
If (failed (HR ))
{
Afxmessagebox ("can't build the graph ");
Return hr;
}

HR = m_pgrabber-> getconnectedmediatype (& mt );

If (failed (HR ))
{
Afxmessagebox ("failt to read the connected media type ");
Return hr;
}
Videoinfoheader * vihh = (videoinfoheader *) Mt. pbformat;
// Vihh-> bmiheader. biwidth = 320;
// Vihh-> bmiheader. biheight = 240;
// Vihh-> bmiheader. bisizeimage = 320*240*3;
// HR = m_pgrabber-> setmediatype (& mt );
McB. lwidth = vihh-> bmiheader. biwidth;
McB. lheight = vihh-> bmiheader. biheight;
Freemediatype (MT );

HR = m_pgrabber-> setbuffersamples (false );
HR = m_pgrabber-> setoneshot (false );
HR = m_pgrabber-> setcallback (& McB, 1); // affects callback

// Set the video capture window
M_hwnd = hwnd;
Setupvideowindow ();
HR = m_pmc-> Run (); // start video capture
If (failed (HR ))
{
Afxmessagebox ("couldn't run the graph! ");
Return hr;
}

Bisvideoopen = true;

Return s_ OK;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.