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