]> git.sesse.net Git - vlc/blob - modules/access/dshow/filter.cpp
* modules/access/dshow/filter.cpp: work-around a bug in w32api-2.5.
[vlc] / modules / access / dshow / filter.cpp
1 /*****************************************************************************
2  * filter.c : DirectShow access module for vlc
3  *****************************************************************************
4  * Copyright (C) 2002 VideoLAN
5  * $Id: filter.cpp,v 1.13 2004/02/16 13:33:10 gbazin Exp $
6  *
7  * Author: Gildas Bazin <gbazin@netcourrier.com>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <string.h>
30
31 #include <vlc/vlc.h>
32 #include <vlc/input.h>
33 #include <vlc/vout.h>
34
35 #ifndef _MSC_VER
36     /* Work-around a bug in w32api-2.5 */
37 #   define QACONTAINERFLAGS QACONTAINERFLAGS_SOMETHINGELSE
38 #endif
39
40 #include "filter.h"
41
42 #define DEBUG_DSHOW 1
43
44 struct access_sys_t
45 {
46     vlc_mutex_t lock;
47     vlc_cond_t  wait;
48 };
49
50 /*****************************************************************************
51  * DirectShow GUIDs.
52  * Easier to define them hear as mingw doesn't provide them all.
53  *****************************************************************************/
54 const GUID CLSID_SystemDeviceEnum = {0x62be5d10, 0x60eb, 0x11d0, {0xbd, 0x3b, 0x00, 0xa0, 0xc9, 0x11, 0xce, 0x86}};
55 const GUID CLSID_VideoInputDeviceCategory = {0x860BB310,0x5D01,0x11d0,{0xBD,0x3B,0x00,0xA0,0xC9,0x11,0xCE,0x86}};
56 const GUID CLSID_AudioInputDeviceCategory = {0x33d9a762, 0x90c8, 0x11d0, {0xbd, 0x43, 0x00, 0xa0, 0xc9, 0x11, 0xce, 0x86}};
57 const GUID IID_IPropertyBag = {0x55272A00, 0x42CB, 0x11CE, {0x81, 0x35, 0x00, 0xAA, 0x00, 0x4B, 0xB8, 0x51}};
58 const GUID IID_ICreateDevEnum = {0x29840822, 0x5b84, 0x11d0, {0xbd, 0x3b, 0x00, 0xa0, 0xc9, 0x11, 0xce, 0x86}};
59 const GUID IID_IFilterGraph = {0x56a8689f, 0x0ad4, 0x11ce, {0xb0, 0x3a, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70}};
60 const GUID IID_IMediaControl = {0x56a868b1, 0x0ad4, 0x11ce, {0xb0, 0x3a, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70}};
61 const GUID CLSID_FilterGraph = {0xe436ebb3, 0x524f, 0x11ce, {0x9f, 0x53, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70}};
62
63 const GUID IID_IUnknown = {0x00000000, 0x0000, 0x0000, {0xc0,0x00, 0x00,0x00,0x00,0x00,0x00,0x46}};
64 const GUID IID_IPersist = {0x0000010c, 0x0000, 0x0000, {0xc0,0x00, 0x00,0x00,0x00,0x00,0x00,0x46}};
65 const GUID IID_IMediaFilter = {0x56a86899, 0x0ad4, 0x11ce, {0xb0,0x3a, 0x00,0x20,0xaf,0x0b,0xa7,0x70}};
66 const GUID IID_IBaseFilter = {0x56a86895, 0x0ad4, 0x11ce, {0xb0, 0x3a, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70}};
67 const GUID IID_IPin = {0x56a86891, 0x0ad4, 0x11ce, {0xb0,0x3a, 0x00,0x20,0xaf,0x0b,0xa7,0x70}};
68 const GUID IID_IMemInputPin = {0x56a8689d, 0x0ad4, 0x11ce, {0xb0,0x3a, 0x00,0x20,0xaf,0x0b,0xa7,0x70}};
69
70 const GUID IID_IEnumPins = {0x56a86892, 0x0ad4, 0x11ce, {0xb0,0x3a, 0x00,0x20,0xaf,0x0b,0xa7,0x70}};
71 const GUID IID_IEnumMediaTypes = {0x89c31040, 0x846b, 0x11ce, {0x97,0xd3, 0x00,0xaa,0x00,0x55,0x59,0x5a}};
72
73 const GUID IID_IAMBufferNegotiation = {0x56ed71a0, 0xaf5f, 0x11d0, {0xb3, 0xf0, 0x00, 0xaa, 0x00, 0x37, 0x61, 0xc5}};
74
75 const GUID IID_ISpecifyPropertyPages = {0xb196b28b, 0xbab4, 0x101a, {0xb6, 0x9c, 0x00, 0xaa, 0x00, 0x34, 0x1d, 0x07}};
76
77 /*
78  * MEDIATYPEs and MEDIASUBTYPEs
79  */
80 const GUID MEDIATYPE_Video = {0x73646976, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
81 const GUID MEDIASUBTYPE_PREVIEW_VIDEO = {0x2859e1da, 0xb81f, 0x4fbd, {0x94, 0x3b, 0xe2, 0x37, 0x24, 0xa1, 0xab, 0xb3}};
82 const GUID MEDIATYPE_Stream = {0xe436eb83, 0x524f, 0x11ce, {0x9f, 0x53, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70}};
83
84 /* Packed RGB formats */
85 const GUID MEDIASUBTYPE_RGB1 = {0xe436eb78, 0x524f, 0x11ce, {0x9f, 0x53, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70}};
86 const GUID MEDIASUBTYPE_RGB4 = {0xe436eb79, 0x524f, 0x11ce, {0x9f, 0x53, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70}};
87 const GUID MEDIASUBTYPE_RGB8 = {0xe436eb7a, 0x524f, 0x11ce, {0x9f, 0x53, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70}};
88 const GUID MEDIASUBTYPE_RGB565 = {0xe436eb7b, 0x524f, 0x11ce, {0x9f, 0x53, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70}};
89 const GUID MEDIASUBTYPE_RGB555 = {0xe436eb7c, 0x524f, 0x11ce, {0x9f, 0x53, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70}};
90 const GUID MEDIASUBTYPE_RGB24 = {0xe436eb7d, 0x524f, 0x11ce, {0x9f, 0x53, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70}};
91 const GUID MEDIASUBTYPE_RGB32 = {0xe436eb7e, 0x524f, 0x11ce, {0x9f, 0x53, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70}};
92 const GUID MEDIASUBTYPE_ARGB32 = {0x773c9ac0, 0x3274, 0x11d0, {0xb7, 0x24, 0x0, 0xaa, 0x0, 0x6c, 0x1a, 0x1}};
93
94 /* Packed YUV formats */
95 const GUID MEDIASUBTYPE_YUYV = {0x56595559, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
96 const GUID MEDIASUBTYPE_Y411 = {0x31313459, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
97 const GUID MEDIASUBTYPE_Y211 = {0x31313259, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
98 const GUID MEDIASUBTYPE_YUY2 = {0x32595559, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
99 const GUID MEDIASUBTYPE_YVYU = {0x55595659, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
100 const GUID MEDIASUBTYPE_UYVY = {0x59565955, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
101
102 /* Planar YUV formats */
103 const GUID MEDIASUBTYPE_YVU9 = {0x39555659, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
104 const GUID MEDIASUBTYPE_YV12 = {0x32315659, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
105 const GUID MEDIASUBTYPE_IYUV = {0x56555949, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}}; /* identical to YV12 */
106 const GUID MEDIASUBTYPE_Y41P = {0x50313459, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
107 const GUID MEDIASUBTYPE_I420 = {0x30323449, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
108
109 const GUID MEDIATYPE_Audio = {0x73647561, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
110 const GUID FORMAT_WaveFormatEx = {0x05589f81, 0xc356, 0x11ce, {0xbf, 0x01, 0x00, 0xaa, 0x00, 0x55, 0x59, 0x5a}};
111 const GUID MEDIASUBTYPE_PCM = {0x00000001, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
112 const GUID MEDIASUBTYPE_IEEE_FLOAT = {0x00000003, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
113
114 /* DV formats */
115 const GUID MEDIASUBTYPE_dvsd = {0x64737664, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
116 const GUID MEDIASUBTYPE_dvhd = {0x64687664, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
117 const GUID MEDIASUBTYPE_dvsl = {0x6c737664, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
118
119 /* MPEG2 formats */
120 const GUID MEDIASUBTYPE_MPEG2_VIDEO = {0xe06d8026, 0xdb46, 0x11cf, {0xb4, 0xd1, 0x00, 0x80, 0x5f, 0x6c, 0xbb, 0xea}};
121 const GUID MEDIASUBTYPE_MPEG2_PROGRAM = {0xe06d8022, 0xdb46, 0x11cf, {0xb4, 0xd1, 0x00, 0x80, 0x5f, 0x6c, 0xbb, 0xea}};
122 const GUID MEDIASUBTYPE_MPEG2_TRANSPORT = {0xe06d8023, 0xdb46, 0x11cf, {0xb4, 0xd1, 0x00, 0x80, 0x5f, 0x6c, 0xbb, 0xea}};
123 const GUID FORMAT_MPEG2Video = {0xe06d80e3, 0xdb46, 0x11cf, {0xb4, 0xd1, 0x00, 0x80, 0x5f, 0x6c, 0xbb, 0xea}};
124
125 const GUID GUID_NULL = {0x0000, 0x0000, 0x0000, {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}};
126
127 void WINAPI FreeMediaType( AM_MEDIA_TYPE& mt )
128 {
129     if( mt.cbFormat != 0 )
130     {
131         CoTaskMemFree( (PVOID)mt.pbFormat );
132         mt.cbFormat = 0;
133         mt.pbFormat = NULL;
134     }
135     if( mt.pUnk != NULL )
136     {
137         mt.pUnk->Release();
138         mt.pUnk = NULL;
139     }
140 }
141
142 HRESULT WINAPI CopyMediaType( AM_MEDIA_TYPE *pmtTarget,
143                               const AM_MEDIA_TYPE *pmtSource )
144 {
145     *pmtTarget = *pmtSource;
146     if( pmtSource->cbFormat != 0 )
147     {
148         pmtTarget->pbFormat = (PBYTE)CoTaskMemAlloc( pmtSource->cbFormat );
149         if( pmtTarget->pbFormat == NULL )
150         {
151             pmtTarget->cbFormat = 0;
152             return E_OUTOFMEMORY;
153         }
154         else
155         {
156             CopyMemory( (PVOID)pmtTarget->pbFormat, (PVOID)pmtSource->pbFormat,
157                         pmtTarget->cbFormat );
158         }
159     }
160     if( pmtTarget->pUnk != NULL )
161     {
162         pmtTarget->pUnk->AddRef();
163     }
164
165     return S_OK;
166 }
167
168 /****************************************************************************
169  * Implementation of our dummy directshow filter pin class
170  ****************************************************************************/
171
172 CapturePin::CapturePin( input_thread_t * _p_input, CaptureFilter *_p_filter,
173                         AM_MEDIA_TYPE mt )
174   : p_input( _p_input ), p_filter( _p_filter ), p_connected_pin( NULL ),
175     media_type( mt ), i_ref( 1 )
176 {
177 }
178
179 CapturePin::~CapturePin()
180 {
181 #ifdef DEBUG_DSHOW
182     msg_Dbg( p_input, "CapturePin::~CapturePin" );
183 #endif
184 }
185
186 HRESULT CapturePin::CustomGetSample( VLCMediaSample *vlc_sample )
187 {
188 #if 0 //def DEBUG_DSHOW
189     msg_Dbg( p_input, "CapturePin::CustomGetSample" );
190 #endif
191
192     access_sys_t *p_sys = p_input->p_access_data;
193     vlc_mutex_lock( &p_sys->lock );
194     if( samples_queue.size() )
195     {
196         *vlc_sample = samples_queue.back();
197         samples_queue.pop_back();
198         vlc_mutex_unlock( &p_sys->lock );
199         return S_OK;
200     }
201     vlc_mutex_unlock( &p_sys->lock );
202     return S_FALSE;
203 }
204
205 AM_MEDIA_TYPE CapturePin::CustomGetMediaType()
206 {
207     return media_type;
208 }
209
210 /* IUnknown methods */
211 STDMETHODIMP CapturePin::QueryInterface(REFIID riid, void **ppv)
212 {
213 #ifdef DEBUG_DSHOW
214     msg_Dbg( p_input, "CapturePin::QueryInterface" );
215 #endif
216
217     if( riid == IID_IUnknown ||
218         riid == IID_IPin )
219     {
220         AddRef();
221         *ppv = (IPin *)this;
222         return NOERROR;
223     }
224     if( riid == IID_IMemInputPin )
225     {
226         AddRef();
227         *ppv = (IMemInputPin *)this;
228         return NOERROR;
229     }
230     else
231     {
232         *ppv = NULL;
233         return E_NOINTERFACE;
234     }
235 }
236 STDMETHODIMP_(ULONG) CapturePin::AddRef()
237 {
238 #ifdef DEBUG_DSHOW
239     msg_Dbg( p_input, "CapturePin::AddRef (ref: %i)", i_ref );
240 #endif
241
242     return i_ref++;
243 };
244 STDMETHODIMP_(ULONG) CapturePin::Release()
245 {
246 #ifdef DEBUG_DSHOW
247     msg_Dbg( p_input, "CapturePin::Release (ref: %i)", i_ref );
248 #endif
249
250     if( !InterlockedDecrement(&i_ref) ) delete this;
251
252     return 0;
253 };
254
255 /* IPin methods */
256 STDMETHODIMP CapturePin::Connect( IPin * pReceivePin,
257                                   const AM_MEDIA_TYPE *pmt )
258 {
259 #ifdef DEBUG_DSHOW
260     msg_Dbg( p_input, "CapturePin::Connect" );
261 #endif
262     return E_NOTIMPL;
263 }
264 STDMETHODIMP CapturePin::ReceiveConnection( IPin * pConnector,
265                                             const AM_MEDIA_TYPE *pmt )
266 {
267 #ifdef DEBUG_DSHOW
268     msg_Dbg( p_input, "CapturePin::ReceiveConnection" );
269 #endif
270
271     if( pmt->majortype == MEDIATYPE_Video )
272     {
273         if( media_type.subtype != GUID_NULL &&
274             media_type.subtype != pmt->subtype )
275             return VFW_E_TYPE_NOT_ACCEPTED;
276
277         if( media_type.pbFormat &&
278             ((VIDEOINFOHEADER *)media_type.pbFormat)->bmiHeader.biHeight &&
279             ((VIDEOINFOHEADER *)media_type.pbFormat)->bmiHeader.biHeight !=
280             ((VIDEOINFOHEADER *)pmt->pbFormat)->bmiHeader.biHeight )
281             return VFW_E_TYPE_NOT_ACCEPTED;
282
283         if( media_type.pbFormat &&
284             ((VIDEOINFOHEADER *)media_type.pbFormat)->bmiHeader.biWidth &&
285             ((VIDEOINFOHEADER *)media_type.pbFormat)->bmiHeader.biWidth !=
286             ((VIDEOINFOHEADER *)pmt->pbFormat)->bmiHeader.biWidth )
287             return VFW_E_TYPE_NOT_ACCEPTED;
288     }
289
290     p_connected_pin = pConnector;
291     p_connected_pin->AddRef();
292
293     FreeMediaType( media_type );
294     return CopyMediaType( &media_type, pmt );
295 }
296 STDMETHODIMP CapturePin::Disconnect()
297 {
298 #ifdef DEBUG_DSHOW
299     msg_Dbg( p_input, "CapturePin::Disconnect" );
300 #endif
301
302 #if 0 // FIXME: This does seem to create crashes sometimes
303     VLCMediaSample vlc_sample;
304     access_sys_t *p_sys = p_input->p_access_data;
305
306     vlc_mutex_lock( &p_sys->lock );
307     while( samples_queue.size() )
308     {
309         vlc_sample = samples_queue.back();
310         samples_queue.pop_back();
311         vlc_sample.p_sample->Release();
312     }
313     vlc_mutex_unlock( &p_sys->lock );
314 #endif
315
316     if( p_connected_pin ) p_connected_pin->Release();
317     p_connected_pin = NULL;
318     FreeMediaType( media_type );
319     return S_OK;
320 }
321 STDMETHODIMP CapturePin::ConnectedTo( IPin **pPin )
322 {
323 #ifdef DEBUG_DSHOW
324     msg_Dbg( p_input, "CapturePin::ConnectedTo" );
325 #endif
326
327     if( !p_connected_pin ) return VFW_E_NOT_CONNECTED;
328
329     p_connected_pin->AddRef();
330     *pPin = p_connected_pin;
331
332     return S_OK;
333 }
334 STDMETHODIMP CapturePin::ConnectionMediaType( AM_MEDIA_TYPE *pmt )
335 {
336 #ifdef DEBUG_DSHOW
337     msg_Dbg( p_input, "CapturePin::ConnectionMediaType" );
338 #endif
339
340     return CopyMediaType( pmt, &media_type );
341 }
342 STDMETHODIMP CapturePin::QueryPinInfo( PIN_INFO * pInfo )
343 {
344 #ifdef DEBUG_DSHOW
345     msg_Dbg( p_input, "CapturePin::QueryPinInfo" );
346 #endif
347
348     pInfo->pFilter = p_filter;
349     if( p_filter ) p_filter->AddRef();
350
351     pInfo->achName[0] = L'\0';
352
353     pInfo->dir = PINDIR_INPUT;
354
355     return NOERROR;
356 }
357 STDMETHODIMP CapturePin::QueryDirection( PIN_DIRECTION * pPinDir )
358 {
359 #ifdef DEBUG_DSHOW
360     msg_Dbg( p_input, "CapturePin::QueryDirection" );
361 #endif
362
363     *pPinDir = PINDIR_INPUT;
364     return NOERROR;
365 }
366 STDMETHODIMP CapturePin::QueryId( LPWSTR * Id )
367 {
368 #ifdef DEBUG_DSHOW
369     msg_Dbg( p_input, "CapturePin::QueryId" );
370 #endif
371     return E_NOTIMPL;
372 }
373 STDMETHODIMP CapturePin::QueryAccept( const AM_MEDIA_TYPE *pmt )
374 {
375 #ifdef DEBUG_DSHOW
376     msg_Dbg( p_input, "CapturePin::QueryAccept" );
377 #endif
378     return E_NOTIMPL;
379 }
380 STDMETHODIMP CapturePin::EnumMediaTypes( IEnumMediaTypes **ppEnum )
381 {
382 #ifdef DEBUG_DSHOW
383     msg_Dbg( p_input, "CapturePin::EnumMediaTypes" );
384 #endif
385
386     *ppEnum = new CaptureEnumMediaTypes( p_input, this, NULL );
387
388     if( *ppEnum == NULL ) return E_OUTOFMEMORY;
389
390     return NOERROR;
391 }
392 STDMETHODIMP CapturePin::QueryInternalConnections( IPin* *apPin, ULONG *nPin )
393 {
394 #ifdef DEBUG_DSHOW
395     msg_Dbg( p_input, "CapturePin::QueryInternalConnections" );
396 #endif
397     return E_NOTIMPL;
398 }
399 STDMETHODIMP CapturePin::EndOfStream( void )
400 {
401 #ifdef DEBUG_DSHOW
402     msg_Dbg( p_input, "CapturePin::EndOfStream" );
403 #endif
404     return S_OK;
405 }
406 STDMETHODIMP CapturePin::BeginFlush( void )
407 {
408 #ifdef DEBUG_DSHOW
409     msg_Dbg( p_input, "CapturePin::BeginFlush" );
410 #endif
411     return S_OK;
412 }
413 STDMETHODIMP CapturePin::EndFlush( void )
414 {
415 #ifdef DEBUG_DSHOW
416     msg_Dbg( p_input, "CapturePin::EndFlush" );
417 #endif
418     return S_OK;
419 }
420 STDMETHODIMP CapturePin::NewSegment( REFERENCE_TIME tStart,
421                                      REFERENCE_TIME tStop,
422                                      double dRate )
423 {
424 #ifdef DEBUG_DSHOW
425     msg_Dbg( p_input, "CapturePin::NewSegment" );
426 #endif
427     return S_OK;
428 }
429
430 /* IMemInputPin methods */
431 STDMETHODIMP CapturePin::GetAllocator( IMemAllocator **ppAllocator )
432 {
433 #ifdef DEBUG_DSHOW
434     msg_Dbg( p_input, "CapturePin::GetAllocator" );
435 #endif
436
437     return VFW_E_NO_ALLOCATOR;
438 }
439 STDMETHODIMP CapturePin::NotifyAllocator( IMemAllocator *pAllocator,
440                                           BOOL bReadOnly )
441 {
442 #ifdef DEBUG_DSHOW
443     msg_Dbg( p_input, "CapturePin::NotifyAllocator" );
444 #endif
445
446     return S_OK;
447 }
448 STDMETHODIMP CapturePin::GetAllocatorRequirements( ALLOCATOR_PROPERTIES *pProps )
449 {
450 #ifdef DEBUG_DSHOW
451     msg_Dbg( p_input, "CapturePin::GetAllocatorRequirements" );
452 #endif
453
454     return E_NOTIMPL;
455 }
456 STDMETHODIMP CapturePin::Receive( IMediaSample *pSample )
457 {
458 #if 0 //def DEBUG_DSHOW
459     msg_Dbg( p_input, "CapturePin::Receive" );
460 #endif
461
462     pSample->AddRef();
463     mtime_t i_timestamp = mdate() * 10;
464     VLCMediaSample vlc_sample = {pSample, i_timestamp};
465
466     access_sys_t *p_sys = p_input->p_access_data;
467     vlc_mutex_lock( &p_sys->lock );
468     samples_queue.push_front( vlc_sample );
469
470     /* Make sure we don't cache too many samples */
471     if( samples_queue.size() > 10 )
472     {
473         vlc_sample = samples_queue.back();
474         samples_queue.pop_back();
475         msg_Dbg( p_input, "CapturePin::Receive trashing late input sample" );
476         vlc_sample.p_sample->Release();
477     }
478
479     vlc_cond_signal( &p_sys->wait );
480     vlc_mutex_unlock( &p_sys->lock );
481
482     return S_OK;
483 }
484 STDMETHODIMP CapturePin::ReceiveMultiple( IMediaSample **pSamples,
485                                           long nSamples,
486                                           long *nSamplesProcessed )
487 {
488 #ifdef DEBUG_DSHOW
489     msg_Dbg( p_input, "CapturePin::ReceiveMultiple" );
490 #endif
491
492     HRESULT hr = S_OK;
493
494     *nSamplesProcessed = 0;
495     while( nSamples-- > 0 )
496     {
497          hr = Receive( pSamples[*nSamplesProcessed] );
498          if( hr != S_OK ) break;
499          (*nSamplesProcessed)++;
500     }
501     return hr;
502 }
503 STDMETHODIMP CapturePin::ReceiveCanBlock( void )
504 {
505 #ifdef DEBUG_DSHOW
506     msg_Dbg( p_input, "CapturePin::ReceiveCanBlock" );
507 #endif
508
509     return S_FALSE; /* Thou shalt not block */
510 }
511
512 /****************************************************************************
513  * Implementation of our dummy directshow filter class
514  ****************************************************************************/
515
516 CaptureFilter::CaptureFilter( input_thread_t * _p_input, AM_MEDIA_TYPE mt )
517   : p_input( _p_input ), p_pin( new CapturePin( _p_input, this, mt ) ),
518     media_type( mt ), state( State_Stopped ), i_ref( 1 )
519 {
520 }
521
522 CaptureFilter::~CaptureFilter()
523 {
524 #ifdef DEBUG_DSHOW
525     msg_Dbg( p_input, "CaptureFilter::~CaptureFilter" );
526 #endif
527     p_pin->Release();
528 }
529
530 /* IUnknown methods */
531 STDMETHODIMP CaptureFilter::QueryInterface( REFIID riid, void **ppv )
532 {
533 #ifdef DEBUG_DSHOW
534     msg_Dbg( p_input, "CaptureFilter::QueryInterface" );
535 #endif
536
537     if( riid == IID_IUnknown )
538     {
539         AddRef();
540         *ppv = (IUnknown *)this;
541         return NOERROR;
542     }
543     if( riid == IID_IPersist )
544     {
545         AddRef();
546         *ppv = (IPersist *)this;
547         return NOERROR;
548     }
549     if( riid == IID_IMediaFilter )
550     {
551         AddRef();
552         *ppv = (IMediaFilter *)this;
553         return NOERROR;
554     }
555     if( riid == IID_IBaseFilter )
556     {
557         AddRef();
558         *ppv = (IBaseFilter *)this;
559         return NOERROR;
560     }
561     else
562     {
563         *ppv = NULL;
564         return E_NOINTERFACE;
565     }
566 };
567 STDMETHODIMP_(ULONG) CaptureFilter::AddRef()
568 {
569 #ifdef DEBUG_DSHOW
570     msg_Dbg( p_input, "CaptureFilter::AddRef (ref: %i)", i_ref );
571 #endif
572
573     return i_ref++;
574 };
575 STDMETHODIMP_(ULONG) CaptureFilter::Release()
576 {
577 #ifdef DEBUG_DSHOW
578     msg_Dbg( p_input, "CaptureFilter::Release (ref: %i)", i_ref );
579 #endif
580
581     if( !InterlockedDecrement(&i_ref) ) delete this;
582
583     return 0;
584 };
585
586 /* IPersist method */
587 STDMETHODIMP CaptureFilter::GetClassID(CLSID *pClsID)
588 {
589 #ifdef DEBUG_DSHOW
590     msg_Dbg( p_input, "CaptureFilter::GetClassID" );
591 #endif
592     return E_NOTIMPL;
593 };
594
595 /* IMediaFilter methods */
596 STDMETHODIMP CaptureFilter::GetState(DWORD dwMSecs, FILTER_STATE *State)
597 {
598 #ifdef DEBUG_DSHOW
599     msg_Dbg( p_input, "CaptureFilter::GetState %i", state );
600 #endif
601
602     *State = state;
603     return S_OK;
604 };
605 STDMETHODIMP CaptureFilter::SetSyncSource(IReferenceClock *pClock)
606 {
607 #ifdef DEBUG_DSHOW
608     msg_Dbg( p_input, "CaptureFilter::SetSyncSource" );
609 #endif
610
611     return S_OK;
612 };
613 STDMETHODIMP CaptureFilter::GetSyncSource(IReferenceClock **pClock)
614 {
615 #ifdef DEBUG_DSHOW
616     msg_Dbg( p_input, "CaptureFilter::GetSyncSource" );
617 #endif
618
619     *pClock = NULL;
620     return NOERROR;
621 };
622 STDMETHODIMP CaptureFilter::Stop()
623 {
624 #ifdef DEBUG_DSHOW
625     msg_Dbg( p_input, "CaptureFilter::Stop" );
626 #endif
627
628     state = State_Stopped;
629     return S_OK;
630 };
631 STDMETHODIMP CaptureFilter::Pause()
632 {
633 #ifdef DEBUG_DSHOW
634     msg_Dbg( p_input, "CaptureFilter::Pause" );
635 #endif
636
637     state = State_Paused;
638     return S_OK;
639 };
640 STDMETHODIMP CaptureFilter::Run(REFERENCE_TIME tStart)
641 {
642 #ifdef DEBUG_DSHOW
643     msg_Dbg( p_input, "CaptureFilter::Run" );
644 #endif
645
646     state = State_Running;
647     return S_OK;
648 };
649
650 /* IBaseFilter methods */
651 STDMETHODIMP CaptureFilter::EnumPins( IEnumPins ** ppEnum )
652 {
653 #ifdef DEBUG_DSHOW
654     msg_Dbg( p_input, "CaptureFilter::EnumPins" );
655 #endif
656
657     /* Create a new ref counted enumerator */
658     *ppEnum = new CaptureEnumPins( p_input, this, NULL );
659     return *ppEnum == NULL ? E_OUTOFMEMORY : NOERROR;
660 };
661 STDMETHODIMP CaptureFilter::FindPin( LPCWSTR Id, IPin ** ppPin )
662 {
663 #ifdef DEBUG_DSHOW
664     msg_Dbg( p_input, "CaptureFilter::FindPin" );
665 #endif
666     return E_NOTIMPL;
667 };
668 STDMETHODIMP CaptureFilter::QueryFilterInfo( FILTER_INFO * pInfo )
669 {
670 #ifdef DEBUG_DSHOW
671     msg_Dbg( p_input, "CaptureFilter::QueryFilterInfo" );
672 #endif
673
674     pInfo->achName[0] = L'\0';
675
676     pInfo->pGraph = p_graph;
677     if( p_graph ) p_graph->AddRef();
678
679     return NOERROR;
680 };
681 STDMETHODIMP CaptureFilter::JoinFilterGraph( IFilterGraph * pGraph,
682                                              LPCWSTR pName )
683 {
684 #ifdef DEBUG_DSHOW
685     msg_Dbg( p_input, "CaptureFilter::JoinFilterGraph" );
686 #endif
687
688     p_graph = pGraph;
689
690     return NOERROR;
691 };
692 STDMETHODIMP CaptureFilter::QueryVendorInfo( LPWSTR* pVendorInfo )
693 {
694 #ifdef DEBUG_DSHOW
695     msg_Dbg( p_input, "CaptureFilter::QueryVendorInfo" );
696 #endif
697     return E_NOTIMPL;
698 };
699
700 /* Custom methods */
701 CapturePin *CaptureFilter::CustomGetPin()
702 {
703     return p_pin;
704 }
705
706 /****************************************************************************
707  * Implementation of our dummy directshow enumpins class
708  ****************************************************************************/
709
710 CaptureEnumPins::CaptureEnumPins( input_thread_t * _p_input,
711                                   CaptureFilter *_p_filter,
712                                   CaptureEnumPins *pEnumPins )
713   : p_input( _p_input ), p_filter( _p_filter ), i_ref( 1 )
714 {
715     /* Hold a reference count on our filter */
716     p_filter->AddRef();
717
718     /* Are we creating a new enumerator */
719
720     if( pEnumPins == NULL )
721     {
722         i_position = 0;
723     }
724     else
725     {
726         i_position = pEnumPins->i_position;
727     }
728 }
729
730 CaptureEnumPins::~CaptureEnumPins()
731 {
732 #ifdef DEBUG_DSHOW
733     msg_Dbg( p_input, "CaptureEnumPins::~CaptureEnumPins" );
734 #endif
735     p_filter->Release();
736 }
737
738 /* IUnknown methods */
739 STDMETHODIMP CaptureEnumPins::QueryInterface( REFIID riid, void **ppv )
740 {
741 #ifdef DEBUG_DSHOW
742     msg_Dbg( p_input, "CaptureEnumPins::QueryInterface" );
743 #endif
744
745     if( riid == IID_IUnknown ||
746         riid == IID_IEnumPins )
747     {
748         AddRef();
749         *ppv = (IEnumPins *)this;
750         return NOERROR;
751     }
752     else
753     {
754         *ppv = NULL;
755         return E_NOINTERFACE;
756     }
757 };
758 STDMETHODIMP_(ULONG) CaptureEnumPins::AddRef()
759 {
760 #ifdef DEBUG_DSHOW
761     msg_Dbg( p_input, "CaptureEnumPins::AddRef (ref: %i)", i_ref );
762 #endif
763
764     return i_ref++;
765 };
766 STDMETHODIMP_(ULONG) CaptureEnumPins::Release()
767 {
768 #ifdef DEBUG_DSHOW
769     msg_Dbg( p_input, "CaptureEnumPins::Release (ref: %i)", i_ref );
770 #endif
771
772     if( !InterlockedDecrement(&i_ref) ) delete this;
773
774     return 0;
775 };
776
777 /* IEnumPins */
778 STDMETHODIMP CaptureEnumPins::Next( ULONG cPins, IPin ** ppPins,
779                                     ULONG * pcFetched )
780 {
781 #ifdef DEBUG_DSHOW
782     msg_Dbg( p_input, "CaptureEnumPins::Next" );
783 #endif
784
785     unsigned int i_fetched = 0;
786
787     if( i_position < 1 && cPins > 0 )
788     {
789         IPin *pPin = p_filter->CustomGetPin();
790         *ppPins = pPin;
791         pPin->AddRef();
792         i_fetched = 1;
793         i_position++;
794     }
795
796     if( pcFetched ) *pcFetched = i_fetched;
797
798     return (i_fetched == cPins) ? S_OK : S_FALSE;
799 };
800 STDMETHODIMP CaptureEnumPins::Skip( ULONG cPins )
801 {
802 #ifdef DEBUG_DSHOW
803     msg_Dbg( p_input, "CaptureEnumPins::Skip" );
804 #endif
805
806     i_position += cPins;
807
808     if( i_position > 1 )
809     {
810         return S_FALSE;
811     }
812
813     return S_OK;
814 };
815 STDMETHODIMP CaptureEnumPins::Reset()
816 {
817 #ifdef DEBUG_DSHOW
818     msg_Dbg( p_input, "CaptureEnumPins::Reset" );
819 #endif
820
821     i_position = 0;
822     return S_OK;
823 };
824 STDMETHODIMP CaptureEnumPins::Clone( IEnumPins **ppEnum )
825 {
826 #ifdef DEBUG_DSHOW
827     msg_Dbg( p_input, "CaptureEnumPins::Clone" );
828 #endif
829
830     *ppEnum = new CaptureEnumPins( p_input, p_filter, this );
831     if( *ppEnum == NULL ) return E_OUTOFMEMORY;
832
833     return NOERROR;
834 };
835
836 /****************************************************************************
837  * Implementation of our dummy directshow enummediatypes class
838  ****************************************************************************/
839
840 CaptureEnumMediaTypes::CaptureEnumMediaTypes( input_thread_t * _p_input,
841                                   CapturePin *_p_pin,
842                                   CaptureEnumMediaTypes *pEnumMediaTypes )
843   : p_input( _p_input ), p_pin( _p_pin ), i_ref( 1 )
844 {
845     /* Hold a reference count on our filter */
846     p_pin->AddRef();
847
848     /* Are we creating a new enumerator */
849     if( pEnumMediaTypes == NULL )
850     {
851         i_position = 0;
852     }
853     else
854     {
855         i_position = pEnumMediaTypes->i_position;
856     }
857 }
858
859 CaptureEnumMediaTypes::~CaptureEnumMediaTypes()
860 {
861 #ifdef DEBUG_DSHOW
862     msg_Dbg( p_input, "CaptureEnumMediaTypes::~CaptureEnumMediaTypes" );
863 #endif
864     p_pin->Release();
865 }
866
867 /* IUnknown methods */
868 STDMETHODIMP CaptureEnumMediaTypes::QueryInterface( REFIID riid, void **ppv )
869 {
870 #ifdef DEBUG_DSHOW
871     msg_Dbg( p_input, "CaptureEnumMediaTypes::QueryInterface" );
872 #endif
873
874     if( riid == IID_IUnknown ||
875         riid == IID_IEnumMediaTypes )
876     {
877         AddRef();
878         *ppv = (IEnumMediaTypes *)this;
879         return NOERROR;
880     }
881     else
882     {
883         *ppv = NULL;
884         return E_NOINTERFACE;
885     }
886 };
887 STDMETHODIMP_(ULONG) CaptureEnumMediaTypes::AddRef()
888 {
889 #ifdef DEBUG_DSHOW
890     msg_Dbg( p_input, "CaptureEnumMediaTypes::AddRef (ref: %i)", i_ref );
891 #endif
892
893     return i_ref++;
894 };
895 STDMETHODIMP_(ULONG) CaptureEnumMediaTypes::Release()
896 {
897 #ifdef DEBUG_DSHOW
898     msg_Dbg( p_input, "CaptureEnumMediaTypes::Release (ref: %i)", i_ref );
899 #endif
900
901     if( !InterlockedDecrement(&i_ref) ) delete this;
902
903     return 0;
904 };
905
906 /* IEnumMediaTypes */
907 STDMETHODIMP CaptureEnumMediaTypes::Next( ULONG cMediaTypes,
908                                           AM_MEDIA_TYPE ** ppMediaTypes,
909                                           ULONG * pcFetched )
910 {
911 #ifdef DEBUG_DSHOW
912     msg_Dbg( p_input, "CaptureEnumMediaTypes::Next" );
913 #endif
914
915     if( pcFetched ) *pcFetched = 0;
916     return S_FALSE;
917 };
918 STDMETHODIMP CaptureEnumMediaTypes::Skip( ULONG cMediaTypes )
919 {
920 #ifdef DEBUG_DSHOW
921     msg_Dbg( p_input, "CaptureEnumMediaTypes::Skip" );
922 #endif
923
924     return S_FALSE;
925 };
926 STDMETHODIMP CaptureEnumMediaTypes::Reset()
927 {
928 #ifdef DEBUG_DSHOW
929     msg_Dbg( p_input, "CaptureEnumMediaTypes::Reset" );
930 #endif
931
932     i_position = 0;
933     return S_OK;
934 };
935 STDMETHODIMP CaptureEnumMediaTypes::Clone( IEnumMediaTypes **ppEnum )
936 {
937 #ifdef DEBUG_DSHOW
938     msg_Dbg( p_input, "CaptureEnumMediaTypes::Clone" );
939 #endif
940
941     *ppEnum = new CaptureEnumMediaTypes( p_input, p_pin, this );
942     if( *ppEnum == NULL ) return E_OUTOFMEMORY;
943
944     return NOERROR;
945 };