1 /*****************************************************************************
2 * filter.c : DirectShow access module for vlc
3 *****************************************************************************
4 * Copyright (C) 2002 VideoLAN
5 * $Id: filter.cpp,v 1.2 2003/08/25 21:45:04 gbazin Exp $
7 * Author: Gildas Bazin <gbazin@netcourrier.com>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
32 #include <vlc/input.h>
44 # define _I64_MAX LONG_LONG_MAX
45 # define LONGLONG long long
54 /*****************************************************************************
56 * Easier to define them hear as mingw doesn't provide them all.
57 *****************************************************************************/
58 const GUID CLSID_SystemDeviceEnum = {0x62be5d10, 0x60eb, 0x11d0, {0xbd, 0x3b, 0x00, 0xa0, 0xc9, 0x11, 0xce, 0x86}};
59 const GUID CLSID_VideoInputDeviceCategory = {0x860BB310,0x5D01,0x11d0,{0xBD,0x3B,0x00,0xA0,0xC9,0x11,0xCE,0x86}};
60 const GUID CLSID_AudioInputDeviceCategory = {0x33d9a762, 0x90c8, 0x11d0, {0xbd, 0x43, 0x00, 0xa0, 0xc9, 0x11, 0xce, 0x86}};
61 const GUID IID_IPropertyBag = {0x55272A00, 0x42CB, 0x11CE, {0x81, 0x35, 0x00, 0xAA, 0x00, 0x4B, 0xB8, 0x51}};
62 const GUID IID_ICreateDevEnum = {0x29840822, 0x5b84, 0x11d0, {0xbd, 0x3b, 0x00, 0xa0, 0xc9, 0x11, 0xce, 0x86}};
63 const GUID IID_IFilterGraph = {0x56a8689f, 0x0ad4, 0x11ce, {0xb0, 0x3a, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70}};
64 const GUID IID_IMediaControl = {0x56a868b1, 0x0ad4, 0x11ce, {0xb0, 0x3a, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70}};
65 const GUID CLSID_FilterGraph = {0xe436ebb3, 0x524f, 0x11ce, {0x9f, 0x53, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70}};
67 const GUID IID_IUnknown = {0x00000000, 0x0000, 0x0000, {0xc0,0x00, 0x00,0x00,0x00,0x00,0x00,0x46}};
68 const GUID IID_IPersist = {0x0000010c, 0x0000, 0x0000, {0xc0,0x00, 0x00,0x00,0x00,0x00,0x00,0x46}};
69 const GUID IID_IMediaFilter = {0x56a86899, 0x0ad4, 0x11ce, {0xb0,0x3a, 0x00,0x20,0xaf,0x0b,0xa7,0x70}};
70 const GUID IID_IBaseFilter = {0x56a86895, 0x0ad4, 0x11ce, {0xb0, 0x3a, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70}};
71 const GUID IID_IPin = {0x56a86891, 0x0ad4, 0x11ce, {0xb0,0x3a, 0x00,0x20,0xaf,0x0b,0xa7,0x70}};
72 const GUID IID_IMemInputPin = {0x56a8689d, 0x0ad4, 0x11ce, {0xb0,0x3a, 0x00,0x20,0xaf,0x0b,0xa7,0x70}};
74 const GUID IID_IEnumPins = {0x56a86892, 0x0ad4, 0x11ce, {0xb0,0x3a, 0x00,0x20,0xaf,0x0b,0xa7,0x70}};
75 const GUID IID_IEnumMediaTypes = {0x89c31040, 0x846b, 0x11ce, {0x97,0xd3, 0x00,0xaa,0x00,0x55,0x59,0x5a}};
78 * MEDIATYPEs and MEDIASUBTYPEs
80 const GUID MEDIATYPE_Video = {0x73646976, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
82 const GUID MEDIASUBTYPE_RGB8 = {0xe436eb7a, 0x524f, 0x11ce, {0x9f, 0x53, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70}};
83 const GUID MEDIASUBTYPE_RGB565 = {0xe436eb7b, 0x524f, 0x11ce, {0x9f, 0x53, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70}};
84 const GUID MEDIASUBTYPE_RGB555 = {0xe436eb7c, 0x524f, 0x11ce, {0x9f, 0x53, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70}};
85 const GUID MEDIASUBTYPE_RGB24 = {0xe436eb7d, 0x524f, 0x11ce, {0x9f, 0x53, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70}};
86 const GUID MEDIASUBTYPE_RGB32 = {0xe436eb7e, 0x524f, 0x11ce, {0x9f, 0x53, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70}};
87 const GUID MEDIASUBTYPE_ARGB32 = {0x773c9ac0, 0x3274, 0x11d0, {0xb7, 0x24, 0x0, 0xaa, 0x0, 0x6c, 0x1a, 0x1}};
89 const GUID MEDIASUBTYPE_YUYV = {0x56595559, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
90 const GUID MEDIASUBTYPE_Y411 = {0x31313459, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
91 const GUID MEDIASUBTYPE_Y41P = {0x50313459, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
92 const GUID MEDIASUBTYPE_YUY2 = {0x32595559, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
93 const GUID MEDIASUBTYPE_YVYU = {0x55595659, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
94 const GUID MEDIASUBTYPE_UYVY = {0x59565955, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
95 const GUID MEDIASUBTYPE_Y211 = {0x31313259, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
96 const GUID MEDIASUBTYPE_YV12 = {0x32315659, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
98 const GUID MEDIATYPE_Audio = {0x73647561, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
99 const GUID FORMAT_WaveFormatEx = {0x05589f81, 0xc356, 0x11ce, {0xbf, 0x01, 0x00, 0xaa, 0x00, 0x55, 0x59, 0x5a}};
100 const GUID MEDIASUBTYPE_PCM = {0x00000001, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
102 void WINAPI FreeMediaType( AM_MEDIA_TYPE& mt )
104 if( mt.cbFormat != 0 )
106 CoTaskMemFree( (PVOID)mt.pbFormat );
110 if( mt.pUnk != NULL )
117 HRESULT WINAPI CopyMediaType( AM_MEDIA_TYPE *pmtTarget,
118 const AM_MEDIA_TYPE *pmtSource )
120 *pmtTarget = *pmtSource;
121 if( pmtSource->cbFormat != 0 )
123 pmtTarget->pbFormat = (PBYTE)CoTaskMemAlloc( pmtSource->cbFormat );
124 if( pmtTarget->pbFormat == NULL )
126 pmtTarget->cbFormat = 0;
127 return E_OUTOFMEMORY;
131 CopyMemory( (PVOID)pmtTarget->pbFormat, (PVOID)pmtSource->pbFormat,
132 pmtTarget->cbFormat );
135 if( pmtTarget->pUnk != NULL )
137 pmtTarget->pUnk->AddRef();
143 /****************************************************************************
144 * Implementation of our dummy directshow filter pin class
145 ****************************************************************************/
147 CapturePin::CapturePin( input_thread_t * _p_input, CaptureFilter *_p_filter )
148 : p_input( _p_input ), p_filter( _p_filter ), p_connected_pin( NULL ),
153 CapturePin::~CapturePin()
157 HRESULT CapturePin::CustomGetSample( VLCMediaSample *vlc_sample )
159 if( samples_queue.size() )
161 *vlc_sample = samples_queue.back();
162 samples_queue.pop_back();
168 AM_MEDIA_TYPE CapturePin::CustomGetMediaType()
173 /* IUnknown methods */
174 STDMETHODIMP CapturePin::QueryInterface(REFIID riid, void **ppv)
177 msg_Dbg( p_input, "CapturePin::QueryInterface" );
180 if( riid == IID_IUnknown ||
186 if( riid == IID_IMemInputPin )
188 *ppv = (IMemInputPin *)this;
194 return E_NOINTERFACE;
197 STDMETHODIMP_(ULONG) CapturePin::AddRef()
200 msg_Dbg( p_input, "CapturePin::AddRef" );
206 STDMETHODIMP_(ULONG) CapturePin::Release()
209 msg_Dbg( p_input, "CapturePin::Release" );
213 if( !i_ref ) delete this;
219 STDMETHODIMP CapturePin::Connect( IPin * pReceivePin,
220 const AM_MEDIA_TYPE *pmt )
223 msg_Dbg( p_input, "CapturePin::Connect" );
227 STDMETHODIMP CapturePin::ReceiveConnection( IPin * pConnector,
228 const AM_MEDIA_TYPE *pmt )
231 msg_Dbg( p_input, "CapturePin::ReceiveConnection" );
234 p_connected_pin = pConnector;
235 p_connected_pin->AddRef();
236 return CopyMediaType( &media_type, pmt );
238 STDMETHODIMP CapturePin::Disconnect()
241 msg_Dbg( p_input, "CapturePin::Disconnect" );
244 p_connected_pin->Release();
245 p_connected_pin = NULL;
246 FreeMediaType( media_type );
249 STDMETHODIMP CapturePin::ConnectedTo( IPin **pPin )
252 msg_Dbg( p_input, "CapturePin::ConnectedTo" );
255 p_connected_pin->AddRef();
256 *pPin = p_connected_pin;
260 STDMETHODIMP CapturePin::ConnectionMediaType( AM_MEDIA_TYPE *pmt )
263 msg_Dbg( p_input, "CapturePin::ConnectionMediaType" );
266 return CopyMediaType( pmt, &media_type );
268 STDMETHODIMP CapturePin::QueryPinInfo( PIN_INFO * pInfo )
271 msg_Dbg( p_input, "CapturePin::QueryPinInfo" );
274 pInfo->pFilter = p_filter;
275 if( p_filter ) p_filter->AddRef();
277 pInfo->achName[0] = L'\0';
279 pInfo->dir = PINDIR_INPUT;
283 STDMETHODIMP CapturePin::QueryDirection( PIN_DIRECTION * pPinDir )
286 msg_Dbg( p_input, "CapturePin::QueryDirection" );
289 *pPinDir = PINDIR_INPUT;
292 STDMETHODIMP CapturePin::QueryId( LPWSTR * Id )
295 msg_Dbg( p_input, "CapturePin::QueryId" );
299 STDMETHODIMP CapturePin::QueryAccept( const AM_MEDIA_TYPE *pmt )
302 msg_Dbg( p_input, "CapturePin::QueryAccept" );
306 STDMETHODIMP CapturePin::EnumMediaTypes( IEnumMediaTypes **ppEnum )
309 msg_Dbg( p_input, "CapturePin::EnumMediaTypes" );
312 *ppEnum = new CaptureEnumMediaTypes( p_input, this, NULL );
314 if( *ppEnum == NULL ) return E_OUTOFMEMORY;
318 STDMETHODIMP CapturePin::QueryInternalConnections( IPin* *apPin, ULONG *nPin )
321 msg_Dbg( p_input, "CapturePin::QueryInternalConnections" );
325 STDMETHODIMP CapturePin::EndOfStream( void )
328 msg_Dbg( p_input, "CapturePin::EndOfStream" );
332 STDMETHODIMP CapturePin::BeginFlush( void )
335 msg_Dbg( p_input, "CapturePin::BeginFlush" );
339 STDMETHODIMP CapturePin::EndFlush( void )
342 msg_Dbg( p_input, "CapturePin::EndFlush" );
346 STDMETHODIMP CapturePin::NewSegment( REFERENCE_TIME tStart,
347 REFERENCE_TIME tStop,
351 msg_Dbg( p_input, "CapturePin::NewSegment" );
356 /* IMemInputPin methods */
357 STDMETHODIMP CapturePin::GetAllocator( IMemAllocator **ppAllocator )
360 msg_Dbg( p_input, "CapturePin::GetAllocator" );
363 return VFW_E_NO_ALLOCATOR;
365 STDMETHODIMP CapturePin::NotifyAllocator( IMemAllocator *pAllocator,
369 msg_Dbg( p_input, "CapturePin::NotifyAllocator" );
374 STDMETHODIMP CapturePin::GetAllocatorRequirements( ALLOCATOR_PROPERTIES *pProps )
377 msg_Dbg( p_input, "CapturePin::GetAllocatorRequirements" );
381 STDMETHODIMP CapturePin::Receive( IMediaSample *pSample )
384 //msg_Dbg( p_input, "CapturePin::Receive" );
388 mtime_t i_timestamp = mdate() * 10;
389 VLCMediaSample vlc_sample = {pSample, i_timestamp};
390 samples_queue.push_front( vlc_sample );
392 /* Make sure we don't cache too many samples */
393 if( samples_queue.size() > 10 )
395 vlc_sample = samples_queue.back();
396 samples_queue.pop_back();
397 msg_Dbg( p_input, "CapturePin::Receive trashing late input sample" );
398 // vlc_sample.p_sample->Release();
403 STDMETHODIMP CapturePin::ReceiveMultiple( IMediaSample **pSamples,
405 long *nSamplesProcessed )
408 msg_Dbg( p_input, "CapturePin::ReceiveMultiple" );
413 *nSamplesProcessed = 0;
414 while( nSamples-- > 0 )
416 hr = Receive( pSamples[*nSamplesProcessed] );
417 if( hr != S_OK ) break;
418 (*nSamplesProcessed)++;
422 STDMETHODIMP CapturePin::ReceiveCanBlock( void )
425 msg_Dbg( p_input, "CapturePin::ReceiveCanBlock" );
428 return S_FALSE; /* Thou shalt not block */
431 /****************************************************************************
432 * Implementation of our dummy directshow filter class
433 ****************************************************************************/
435 CaptureFilter::CaptureFilter( input_thread_t * _p_input )
436 : p_input( _p_input ), p_pin( new CapturePin( _p_input, this ) ),
441 CaptureFilter::~CaptureFilter()
446 /* IUnknown methods */
447 STDMETHODIMP CaptureFilter::QueryInterface( REFIID riid, void **ppv )
450 msg_Dbg( p_input, "CaptureFilter::QueryInterface" );
453 if( riid == IID_IUnknown )
455 *ppv = (IUnknown *)this;
458 if( riid == IID_IPersist )
460 *ppv = (IPersist *)this;
463 if( riid == IID_IMediaFilter )
465 *ppv = (IMediaFilter *)this;
468 if( riid == IID_IBaseFilter )
470 *ppv = (IBaseFilter *)this;
476 return E_NOINTERFACE;
479 STDMETHODIMP_(ULONG) CaptureFilter::AddRef()
482 msg_Dbg( p_input, "CaptureFilter::AddRef" );
488 STDMETHODIMP_(ULONG) CaptureFilter::Release()
491 msg_Dbg( p_input, "CaptureFilter::Release" );
495 if( !i_ref ) delete this;
500 /* IPersist method */
501 STDMETHODIMP CaptureFilter::GetClassID(CLSID *pClsID)
504 msg_Dbg( p_input, "CaptureFilter::GetClassID" );
509 /* IMediaFilter methods */
510 STDMETHODIMP CaptureFilter::GetState(DWORD dwMSecs, FILTER_STATE *State)
513 msg_Dbg( p_input, "CaptureFilter::GetStat" );
517 STDMETHODIMP CaptureFilter::SetSyncSource(IReferenceClock *pClock)
520 msg_Dbg( p_input, "CaptureFilter::SetSyncSource" );
525 STDMETHODIMP CaptureFilter::GetSyncSource(IReferenceClock **pClock)
528 msg_Dbg( p_input, "CaptureFilter::GetSyncSource" );
532 STDMETHODIMP CaptureFilter::Stop()
535 msg_Dbg( p_input, "CaptureFilter::Stop" );
539 STDMETHODIMP CaptureFilter::Pause()
542 msg_Dbg( p_input, "CaptureFilter::Pause" );
546 STDMETHODIMP CaptureFilter::Run(REFERENCE_TIME tStart)
549 msg_Dbg( p_input, "CaptureFilter::Run" );
554 /* IBaseFilter methods */
555 STDMETHODIMP CaptureFilter::EnumPins( IEnumPins ** ppEnum )
558 msg_Dbg( p_input, "CaptureFilter::EnumPins" );
561 /* Create a new ref counted enumerator */
562 *ppEnum = new CaptureEnumPins( p_input, this, NULL );
563 return *ppEnum == NULL ? E_OUTOFMEMORY : NOERROR;
565 STDMETHODIMP CaptureFilter::FindPin( LPCWSTR Id, IPin ** ppPin )
568 msg_Dbg( p_input, "CaptureFilter::FindPin" );
572 STDMETHODIMP CaptureFilter::QueryFilterInfo( FILTER_INFO * pInfo )
575 msg_Dbg( p_input, "CaptureFilter::QueryFilterInfo" );
578 pInfo->achName[0] = L'\0';
580 pInfo->pGraph = p_graph;
581 if( p_graph ) p_graph->AddRef();
585 STDMETHODIMP CaptureFilter::JoinFilterGraph( IFilterGraph * pGraph,
589 msg_Dbg( p_input, "CaptureFilter::JoinFilterGraph" );
596 STDMETHODIMP CaptureFilter::QueryVendorInfo( LPWSTR* pVendorInfo )
599 msg_Dbg( p_input, "CaptureFilter::QueryVendorInfo" );
605 CapturePin *CaptureFilter::CustomGetPin()
610 /****************************************************************************
611 * Implementation of our dummy directshow enumpins class
612 ****************************************************************************/
614 CaptureEnumPins::CaptureEnumPins( input_thread_t * _p_input,
615 CaptureFilter *_p_filter,
616 CaptureEnumPins *pEnumPins )
617 : p_input( _p_input ), p_filter( _p_filter ), i_ref( 1 )
619 /* Hold a reference count on our filter */
622 /* Are we creating a new enumerator */
624 if( pEnumPins == NULL )
630 i_position = pEnumPins->i_position;
634 CaptureEnumPins::~CaptureEnumPins()
639 /* IUnknown methods */
640 STDMETHODIMP CaptureEnumPins::QueryInterface( REFIID riid, void **ppv )
643 msg_Dbg( p_input, "CaptureEnumPins::QueryInterface" );
646 if( riid == IID_IUnknown ||
647 riid == IID_IEnumPins )
649 *ppv = (IEnumPins *)this;
655 return E_NOINTERFACE;
658 STDMETHODIMP_(ULONG) CaptureEnumPins::AddRef()
661 msg_Dbg( p_input, "CaptureEnumPins::AddRef" );
667 STDMETHODIMP_(ULONG) CaptureEnumPins::Release()
670 msg_Dbg( p_input, "CaptureEnumPins::Release" );
674 if( !i_ref ) delete this;
680 STDMETHODIMP CaptureEnumPins::Next( ULONG cPins, IPin ** ppPins,
684 msg_Dbg( p_input, "CaptureEnumPins::Next" );
689 if( i_position < 1 && cPins > 0 )
691 IPin *pPin = p_filter->CustomGetPin();
701 STDMETHODIMP CaptureEnumPins::Skip( ULONG cPins )
704 msg_Dbg( p_input, "CaptureEnumPins::Skip" );
715 STDMETHODIMP CaptureEnumPins::Reset()
718 msg_Dbg( p_input, "CaptureEnumPins::Reset" );
724 STDMETHODIMP CaptureEnumPins::Clone( IEnumPins **ppEnum )
727 msg_Dbg( p_input, "CaptureEnumPins::Clone" );
730 *ppEnum = new CaptureEnumPins( p_input, p_filter, this );
731 if( *ppEnum == NULL ) return E_OUTOFMEMORY;
736 /****************************************************************************
737 * Implementation of our dummy directshow enummediatypes class
738 ****************************************************************************/
740 CaptureEnumMediaTypes::CaptureEnumMediaTypes( input_thread_t * _p_input,
742 CaptureEnumMediaTypes *pEnumMediaTypes )
743 : p_input( _p_input ), p_pin( _p_pin ), i_ref( 1 )
745 /* Hold a reference count on our filter */
748 /* Are we creating a new enumerator */
750 if( pEnumMediaTypes == NULL )
756 i_position = pEnumMediaTypes->i_position;
760 CaptureEnumMediaTypes::~CaptureEnumMediaTypes()
765 /* IUnknown methods */
766 STDMETHODIMP CaptureEnumMediaTypes::QueryInterface( REFIID riid, void **ppv )
769 msg_Dbg( p_input, "CaptureEnumMediaTypes::QueryInterface" );
772 if( riid == IID_IUnknown ||
773 riid == IID_IEnumMediaTypes )
775 *ppv = (IEnumMediaTypes *)this;
781 return E_NOINTERFACE;
784 STDMETHODIMP_(ULONG) CaptureEnumMediaTypes::AddRef()
787 msg_Dbg( p_input, "CaptureEnumMediaTypes::AddRef" );
793 STDMETHODIMP_(ULONG) CaptureEnumMediaTypes::Release()
796 msg_Dbg( p_input, "CaptureEnumMediaTypes::Release" );
800 if( !i_ref ) delete this;
805 /* IEnumMediaTypes */
806 STDMETHODIMP CaptureEnumMediaTypes::Next( ULONG cMediaTypes,
807 AM_MEDIA_TYPE ** ppMediaTypes,
811 msg_Dbg( p_input, "CaptureEnumMediaTypes::Next" );
817 if( i_position < 1 && cMediaTypes > 0 )
819 IPin *pPin = p_pin->CustomGetPin();
820 *ppMediaTypes = pPin;
830 STDMETHODIMP CaptureEnumMediaTypes::Skip( ULONG cMediaTypes )
833 msg_Dbg( p_input, "CaptureEnumMediaTypes::Skip" );
836 if( cMediaTypes > 1 )
841 i_position += cMediaTypes;
844 STDMETHODIMP CaptureEnumMediaTypes::Reset()
847 msg_Dbg( p_input, "CaptureEnumMediaTypes::Reset" );
853 STDMETHODIMP CaptureEnumMediaTypes::Clone( IEnumMediaTypes **ppEnum )
856 msg_Dbg( p_input, "CaptureEnumMediaTypes::Clone" );
859 *ppEnum = new CaptureEnumMediaTypes( p_input, p_pin, this );
860 if( *ppEnum == NULL ) return E_OUTOFMEMORY;