DirectShow basic programming source filter csource csourcestream

Source: Internet
Author: User

DirectShow is a set of interfaces defined by Microsoft. It consists of different interfaces, such as ibasefilter and ipin. The relationship between these interfaces forms the DirectShow architecture.

The DirectX SDK provides a C ++ baseclasses interface. These implementations are mature and complete, so we don't need to re-develop a class library to implement the dirctshow interface, just use it directly.

DirectShow programming is filter programming. Different filters are implemented based on functions. The source filter in the push mode is generally derived from csource, which implements special functions in the derived class, while the csource class uses the pin derived from csourcestream. Then, write the source filter in the push mode to implement these two classes.

 

Class csource: Public cbasefilter <br/>{< br/> Public: <br/> csource (tchar * pname, lpunknown lpunk, CLSID, hresult * phr ); <br/> csource (tchar * pname, lpunknown lpunk, CLSID); <br/> ~ Csource (); <br/> int getpincount (void); <br/> cbasepin * getpin (int n); <br/> ccritsec * pstatelock (void) {return & m_cstatelock ;}< br/> hresult addpin (csourcestream *); <br/> hresult removepin (csourcestream *); <br/> stdmethodimp findpin (lpcwstr ID, ipin ** pppin); <br/> int findpinnumber (ipin * ipin); <br/> protected: <br/> int m_ipins; <br/> csourcestream ** m_pastreams; <br/> ccritsec m_cstatelock; <br/> };

The csource class is derived from the cbasefilter class:

Member:

Csourcestream ** m_pastreams; the source filter can support multiple output pins. Therefore, a dynamic array is used to record pin pointers.

Int m_ipins; number of pins.

Method:

Hresult addpin (csourcestream *); add a pin.

Hresult removepin (csourcestream *); remove the pin.

Stdmethodimp findpin (lpcwstr ID, ipin ** pppin); find the pin.

Int getpincount (void); returns the number of pins.

It can be seen that the csource class is the management of the pin. More functions are implemented by cbasefilter. To implement your own csource class, you only need to do two things: Provide the instantiation interface and instantiate the output pin.

Instantiation interface:

Cunknown * winapi cmysource: createinstance (lpunknown lpunk, hresult * phr) <br/>{< br/> cunknown * punk = new cmysource (lpunk, PHR ); <br/> return punk; <br/>}

This interface is similar to this fixed format.

Instantiate the output pin:

Cmysource: cmysource (lpunknown lpunk, hresult * phr) <br/>: csource (..., lpunk, CLSID _...) <br/>{</P> <p> cmyoutputpin * poutpin = new cmyoutputpin (phr, this); <br/> If (failed (* phr )) <br/>{< br/> removepin (poutpin); <br/> poutpin-> release (); <br/>}< br/>}

Instantiate the output pin in the constructor. The constructor of the output pin automatically adds the pin to cmysource.

 

 

Class csourcestream: Public camthread, public cbaseoutputpin <br/>{< br/> Public: <br/> csourcestream (tchar * pobjectname, hresult * phr, csource * PMS, lpcwstr pname); <br/> virtual ~ Csourcestream (void); <br/> protected: <br/> csource * m_pfilter; <br/> virtual hresult fillbuffer (imediasample * psamp) pure; <br/> protected: <br/> virtual hresult checkmediatype (const cmediatype * pmediatype); <br/> virtual hresult getmediatype (INT iposition, cmediatype * pmediatype); <br/> };

The output pin is derived from cbaseoutputpin. The csource class must use the derived class of csourcestream as the output pin.

Member:

Csource * m_pfilter; record the pointer of the filter and assign values through the constructor.

Method:

Virtual hresult fillbuffer (imediasample * psamp) pure; fill data, pure is defined as = 0, csourcestream is an abstract class and cannot be instantiated. The derived class must implement this function. What needs to be done in the implementation of the function is to fill in the data like the imediasample passed in by the parameter, which will be passed to the downstream filter.

Virtual hresult getmediatype (INT iposition, cmediatype * pmediatype); although this function is not a pure virtual function, we must repeat it to obtain the custom media type.

Virtual hresult checkmediatype (const cmediatype * pmediatype); the two pins connected match the media type. In this function implementation, you can compare necessary data such as the primary child type and so on, if this parameter is set to true, a success is returned. Otherwise, a failure is returned. If the matching fails, the connection fails. Therefore, the connection process is actually the process of matching the media type. checkmediatype is eventually used in cbasepin: connect.

Virtual hresult decidebuffersize (imemallocator * palloc, allocator_properties * ppropinputrequest) pure; declare this pure virtual function in cbaseoutputpin. Therefore, this function must be implemented in the derived class, this function determines the data size during bucket negotiation.

 

In general, a source filter only needs to implement the content mentioned above, and all others have a fixed format. For fixed formats, refer to the example of ball In SDK.

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.