Several methods of realizing grasping graph in dshow

Source: Internet
Author: User
1. Add Sample Grabber Filter

When we add sample grabber filter, we can call its interface (interface) Isamplegrabber directly. This interface can obtain a separate media Samples that pass through the filter. For more information, see DXSDK. 1.1 Derivation of their own sample grabber

Derive your own class from the ISAMPLEGRABBERCB, and then implement its virtual functions, see the sample programs in the SDK (DXSDK root/samples/c++/directshow/editing/grabbitmaps) for details. 1.2 Direct call to sample Grabber filter interface

If we add filter dynamically in the playback process, the operation and efficiency are not optimistic. So I use the following method:

The method passes the time, not dynamically adding filter and screenshots while playing, but opening the source file to operate separately.

A). Declare the following interfaces:

#001 Igraphbuilder *pgraph = NULL; For graph builder

#002 IMediaControl *pcontrol = NULL; Media Control

#003 imediaseeking *pseeking = NULL; Media seeking

#004 Imediaeventex *pevent = NULL; Media envent

#005 Ibasefilter *pnullfilter =null;//for holding the Sample grabber Filter

B). Initialize interface:

#001 Jif (CoCreateInstance (Clsid_filtergraph, NULL, Clsctx_inproc,

#002 Iid_igraphbuilder, (void * * *) &pgraph));

#003

#004 Jif (CoCreateInstance (Clsid_nullrenderer, NULL, Clsctx_inproc,

#005 Iid_ibasefilter, (void * * *) &pnullfilter));

#006 Jif (Pgraph->queryinterface (Iid_imediacontrol, (void * * *) &pcontrol));

#007 Jif (Pgraph->queryinterface (iid_imediaseeking, (void * * *) &pseeking));

#008 Jif (Pgraph->queryinterface (iid_imediaevent, (void * * *) &pevent));

C). Create a sample Grabber

#001//Create the Sample grabber.

#002 ibasefilter *pgrabberf = NULL;

#003 Jif (CoCreateInstance (Clsid_samplegrabber,null, Clsctx_inproc_server,

#004 Iid_ibasefilter, (void**) &pgrabberf));

#005

#006 Jif (Pgraph->addfilter (Pgrabberf, L "Sample grabber"));

#007 Jif (Pgraph->addfilter (Pnullfilter, L "Null Render Filter"));

#008

#009 Isamplegrabber *pgrabber;

#010 Jif (Pgrabberf->queryinterface (Iid_isamplegrabber, (void**) &pgrabber));

Set the media format for sample grabber: Call Setmediatype, which accepts a am_media_type structure, mainly setting the Majortype in the structure, and the subtype domain.

D). Add Source Filter:

#001 Ibasefilter *psrc;

#002 Jif (Pgraph->addsourcefilter (t2w (m_szfile), L "Source", &psrc));

E). Connect Grabber and Nullrender two filter:

#001 Ipin *poutpin;

#002 hr = Getpin (Pgrabberf, Pindir_output, &poutpin);

#003

#004 Ipin *pinpin;

#005 hr = Getpin (Pnullfilter, Pindir_input, &pinpin);

#006

#007 Pgraph->connect (Poutpin, Pinpin);

F). Get the type of media you are currently connected to

#001 Am_media_type MT;

#002 hr = Pgrabber->getconnectedmediatype (&MT);

#003//Examine the format block.

#004 Videoinfoheader *PVIH;

#005 if ((Mt.formattype = format_videoinfo) &&

#006 (Mt.cbformat >= sizeof (videoinfoheader)) &&

#007 (Mt.pbformat!= NULL))

#008 {

#009 PVIH = (videoinfoheader*) Mt.pbformat;

#010}

#011 Else

#012 {

#013//Wrong format. Free the "format block" and "return" error.

#014 Freemediatype (MT);

#015 return vfw_e_invalidmediatype;

#016}

#017

#018//Do buffer, the samples as they pass through

#019//

#020 hr = Pgrabber->setbuffersamples (TRUE);

#021

#022//Only grab one in a time, stop stream after

#023//grabbing one sample

#024//

#025 hr = Pgrabber->setoneshot (TRUE);

G). Seeking file so that it reaches the time frame to be screenshots

#001 pseeking->setpositions (Pcurrentpos,

#002 am_seeking_absolutepositioning,

#003 NULL, am_seeking_nopositioning);

#004

#005 Pcontrol->run ();

#006

#007 long evcode = 0;

#008

#009 hr = pevent->waitforcompletion (INFINITE, &evcode);

H). Get the current buffer data

#001//Find the required buffer size.

#002 long cbbuffer = 0;

#003 hr = Pgrabber->getcurrentbuffer (&cbbuffer, NULL);

#004 Longlong Currentpos;

#005 pseeking->getcurrentposition (&currentpos);

#006 BYTE *pbuffer = new Byte[cbbuffer];

#007 if (!pbuffer)

#008 {

#009//out of memory. Return an error code.

#010 MSG ("Out of Memory");

#011}

#012 hr = Pgrabber->getcurrentbuffer (&cbbuffer, (long*) pbuffer);

I). Write to File

#001//Create a file to hold the bitmap

#002 HANDLE HF = CreateFile (szFileName, Generic_write, File_share_read,

#003 null, create_always, NULL, NULL);

#004

#005 if (HF = INVALID_HANDLE_VALUE)

#006 {

#007//Failed to create file

#008 return 0;

#009}

#010

#011//Write out the file header

#012//

#013 Bitmapfileheader BFH;

#014 memset (&BFH, 0, sizeof (BFH));

#015 bfh.bftype = ' MB ';

#016 bfh.bfsize = sizeof (BFH) + cbbuffer + sizeof (bitmapinfoheader);

#017 bfh.bfoffbits = sizeof (Bitmapinfoheader) +sizeof (Bitmapfileheader);

#018

#019 DWORD written = 0;

#020 WriteFile (HF, &BFH, sizeof (BFH), &written, NULL);

#021

#022//Write the bitmap format

#023//

#024 Bitmapinfoheader BiH;

#025 memset (&bih, 0, sizeof (BIH));

#026 bih.bisize = sizeof (BIH);

#027 bih.biwidth = pvih->bmiheader.biwidth;

#028 bih.biheight = pvih->bmiheader.biheight;

#029 Bih.biplanes = pvih->bmiheader.biplanes;

#030 Bih.bibitcount = pvih->bmiheader.bibitcount;

#031

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.