shdocvw: Added InternetExplorer object tests.
[wine] / dlls / quartz / parser.c
1 /*
2  * Parser (Base for parsers and splitters)
3  *
4  * Copyright 2003 Robert Shearman
5  * Copyright 2004-2005 Christian Costa
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  */
21
22 #include "quartz_private.h"
23 #include "pin.h"
24
25 #include "vfwmsgs.h"
26 #include "amvideo.h"
27
28 #include "wine/unicode.h"
29 #include "wine/debug.h"
30
31 #include <math.h>
32 #include <assert.h>
33
34 #include "parser.h"
35
36 WINE_DEFAULT_DEBUG_CHANNEL(quartz);
37
38 static const WCHAR wcsInputPinName[] = {'i','n','p','u','t',' ','p','i','n',0};
39 static const IMediaSeekingVtbl Parser_Seeking_Vtbl;
40 static const IPinVtbl Parser_OutputPin_Vtbl;
41 static const IPinVtbl Parser_InputPin_Vtbl;
42
43 static HRESULT WINAPI Parser_ChangeStart(IMediaSeeking *iface);
44 static HRESULT WINAPI Parser_ChangeStop(IMediaSeeking *iface);
45 static HRESULT WINAPI Parser_ChangeRate(IMediaSeeking *iface);
46 static HRESULT WINAPI Parser_OutputPin_DecideBufferSize(BaseOutputPin *iface, IMemAllocator *pAlloc, ALLOCATOR_PROPERTIES *ppropInputRequest);
47 static HRESULT WINAPI Parser_OutputPin_GetMediaType(BasePin *iface, int iPosition, AM_MEDIA_TYPE *pmt);
48 static HRESULT WINAPI Parser_OutputPin_DecideAllocator(BaseOutputPin *This, IMemInputPin *pPin, IMemAllocator **pAlloc);
49 static HRESULT WINAPI Parser_OutputPin_BreakConnect(BaseOutputPin *This);
50
51 static inline ParserImpl *impl_from_IMediaSeeking( IMediaSeeking *iface )
52 {
53     return (ParserImpl *)((char*)iface - FIELD_OFFSET(ParserImpl, sourceSeeking.lpVtbl));
54 }
55
56 /* FIXME: WRONG */
57 static IPin* WINAPI Parser_GetPin(BaseFilter *iface, int pos)
58 {
59     ParserImpl *This = (ParserImpl *)iface;
60
61     TRACE("Asking for pos %x\n", pos);
62
63     /* Input pin also has a pin, hence the > and not >= */
64     if (pos > This->cStreams || pos < 0)
65         return NULL;
66
67     IPin_AddRef(This->ppPins[pos]);
68     return This->ppPins[pos];
69 }
70
71 static LONG WINAPI Parser_GetPinCount(BaseFilter *iface)
72 {
73     ParserImpl *This = (ParserImpl *)iface;
74
75     return This->cStreams;
76 }
77
78 static const BaseFilterFuncTable BaseFuncTable = {
79     Parser_GetPin,
80     Parser_GetPinCount
81 };
82
83 HRESULT Parser_Create(ParserImpl* pParser, const IBaseFilterVtbl *Parser_Vtbl, const CLSID* pClsid, PFN_PROCESS_SAMPLE fnProcessSample, PFN_QUERY_ACCEPT fnQueryAccept, PFN_PRE_CONNECT fnPreConnect, PFN_CLEANUP fnCleanup, PFN_DISCONNECT fnDisconnect, REQUESTPROC fnRequest, STOPPROCESSPROC fnDone, SourceSeeking_ChangeStop stop, SourceSeeking_ChangeStart start, SourceSeeking_ChangeRate rate)
84 {
85     HRESULT hr;
86     PIN_INFO piInput;
87
88     /* pTransformFilter is already allocated */
89     BaseFilter_Init(&pParser->filter, Parser_Vtbl, pClsid, (DWORD_PTR)(__FILE__ ": ParserImpl.csFilter"), &BaseFuncTable);
90
91     pParser->fnDisconnect = fnDisconnect;
92
93     pParser->cStreams = 0;
94     pParser->ppPins = CoTaskMemAlloc(1 * sizeof(IPin *));
95
96     /* construct input pin */
97     piInput.dir = PINDIR_INPUT;
98     piInput.pFilter = (IBaseFilter *)pParser;
99     lstrcpynW(piInput.achName, wcsInputPinName, sizeof(piInput.achName) / sizeof(piInput.achName[0]));
100
101     if (!start)
102         start = Parser_ChangeStart;
103
104     if (!stop)
105         stop = Parser_ChangeStop;
106
107     if (!rate)
108         rate = Parser_ChangeRate;
109
110     SourceSeeking_Init(&pParser->sourceSeeking, &Parser_Seeking_Vtbl, stop, start, rate,  &pParser->filter.csFilter);
111
112     hr = PullPin_Construct(&Parser_InputPin_Vtbl, &piInput, fnProcessSample, (LPVOID)pParser, fnQueryAccept, fnCleanup, fnRequest, fnDone, &pParser->filter.csFilter, (IPin **)&pParser->pInputPin);
113
114     if (SUCCEEDED(hr))
115     {
116         pParser->ppPins[0] = (IPin *)pParser->pInputPin;
117         pParser->pInputPin->fnPreConnect = fnPreConnect;
118     }
119     else
120     {
121         CoTaskMemFree(pParser->ppPins);
122         BaseFilterImpl_Release((IBaseFilter*)pParser);
123         CoTaskMemFree(pParser);
124     }
125
126     return hr;
127 }
128
129 HRESULT WINAPI Parser_QueryInterface(IBaseFilter * iface, REFIID riid, LPVOID * ppv)
130 {
131     ParserImpl *This = (ParserImpl *)iface;
132     TRACE("(%s, %p)\n", qzdebugstr_guid(riid), ppv);
133
134     *ppv = NULL;
135
136     if ( IsEqualIID(riid, &IID_IUnknown)
137       || IsEqualIID(riid, &IID_IPersist)
138       || IsEqualIID(riid, &IID_IMediaFilter)
139       || IsEqualIID(riid, &IID_IBaseFilter) )
140         *ppv = This;
141
142     if (*ppv)
143     {
144         IUnknown_AddRef((IUnknown *)(*ppv));
145         return S_OK;
146     }
147
148     if (!IsEqualIID(riid, &IID_IPin) && !IsEqualIID(riid, &IID_IVideoWindow))
149         FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
150
151     return E_NOINTERFACE;
152 }
153
154 ULONG WINAPI Parser_AddRef(IBaseFilter * iface)
155 {
156     return BaseFilterImpl_AddRef(iface);
157 }
158
159 void Parser_Destroy(ParserImpl *This)
160 {
161     IPin *connected = NULL;
162     ULONG pinref;
163
164     assert(!This->filter.refCount);
165     PullPin_WaitForStateChange(This->pInputPin, INFINITE);
166
167     /* Don't need to clean up output pins, freeing input pin will do that */
168     IPin_ConnectedTo((IPin *)This->pInputPin, &connected);
169     if (connected)
170     {
171         assert(IPin_Disconnect(connected) == S_OK);
172         IPin_Release(connected);
173         assert(IPin_Disconnect((IPin *)This->pInputPin) == S_OK);
174     }
175     pinref = IPin_Release((IPin *)This->pInputPin);
176     if (pinref)
177     {
178         /* Valgrind could find this, if I kill it here */
179         ERR("pinref should be null, is %u, destroying anyway\n", pinref);
180         assert((LONG)pinref > 0);
181
182         while (pinref)
183             pinref = IPin_Release((IPin *)This->pInputPin);
184     }
185
186     CoTaskMemFree(This->ppPins);
187
188     TRACE("Destroying parser\n");
189     CoTaskMemFree(This);
190 }
191
192 ULONG WINAPI Parser_Release(IBaseFilter * iface)
193 {
194     ParserImpl *This = (ParserImpl *)iface;
195     ULONG refCount = BaseFilterImpl_Release(iface);
196
197     TRACE("(%p)->() Release from %d\n", This, refCount + 1);
198
199     if (!refCount)
200         Parser_Destroy(This);
201
202     return refCount;
203 }
204
205 /** IPersist methods **/
206
207 HRESULT WINAPI Parser_GetClassID(IBaseFilter * iface, CLSID * pClsid)
208 {
209     ParserImpl *This = (ParserImpl *)iface;
210
211     TRACE("(%p)\n", pClsid);
212
213     *pClsid = This->filter.clsid;
214
215     return S_OK;
216 }
217
218 /** IMediaFilter methods **/
219
220 HRESULT WINAPI Parser_Stop(IBaseFilter * iface)
221 {
222     ParserImpl *This = (ParserImpl *)iface;
223     PullPin *pin = (PullPin *)This->ppPins[0];
224     ULONG i;
225
226     TRACE("()\n");
227
228     EnterCriticalSection(&pin->thread_lock);
229
230     IAsyncReader_BeginFlush(This->pInputPin->pReader);
231     EnterCriticalSection(&This->filter.csFilter);
232
233     if (This->filter.state == State_Stopped)
234     {
235         LeaveCriticalSection(&This->filter.csFilter);
236         IAsyncReader_EndFlush(This->pInputPin->pReader);
237         LeaveCriticalSection(&pin->thread_lock);
238         return S_OK;
239     }
240
241     This->filter.state = State_Stopped;
242
243     for (i = 1; i < (This->cStreams + 1); i++)
244     {
245         BaseOutputPinImpl_Inactive((BaseOutputPin *)This->ppPins[i]);
246     }
247
248     LeaveCriticalSection(&This->filter.csFilter);
249
250     PullPin_PauseProcessing(This->pInputPin);
251     PullPin_WaitForStateChange(This->pInputPin, INFINITE);
252     IAsyncReader_EndFlush(This->pInputPin->pReader);
253
254     LeaveCriticalSection(&pin->thread_lock);
255     return S_OK;
256 }
257
258 HRESULT WINAPI Parser_Pause(IBaseFilter * iface)
259 {
260     HRESULT hr = S_OK;
261     ParserImpl *This = (ParserImpl *)iface;
262     PullPin *pin = (PullPin *)This->ppPins[0];
263
264     TRACE("()\n");
265
266     EnterCriticalSection(&pin->thread_lock);
267     EnterCriticalSection(&This->filter.csFilter);
268
269     if (This->filter.state == State_Paused)
270     {
271         LeaveCriticalSection(&This->filter.csFilter);
272         LeaveCriticalSection(&pin->thread_lock);
273         return S_OK;
274     }
275
276     if (This->filter.state == State_Stopped)
277     {
278         LeaveCriticalSection(&This->filter.csFilter);
279         hr = IBaseFilter_Run(iface, -1);
280         EnterCriticalSection(&This->filter.csFilter);
281     }
282
283     if (SUCCEEDED(hr))
284         This->filter.state = State_Paused;
285
286     LeaveCriticalSection(&This->filter.csFilter);
287     LeaveCriticalSection(&pin->thread_lock);
288
289     return hr;
290 }
291
292 HRESULT WINAPI Parser_Run(IBaseFilter * iface, REFERENCE_TIME tStart)
293 {
294     HRESULT hr = S_OK;
295     ParserImpl *This = (ParserImpl *)iface;
296     PullPin *pin = (PullPin *)This->ppPins[0];
297
298     ULONG i;
299
300     TRACE("(%s)\n", wine_dbgstr_longlong(tStart));
301
302     EnterCriticalSection(&pin->thread_lock);
303     EnterCriticalSection(&This->filter.csFilter);
304     {
305         HRESULT hr_any = VFW_E_NOT_CONNECTED;
306
307         This->filter.rtStreamStart = tStart;
308         if (This->filter.state == State_Running || This->filter.state == State_Paused)
309         {
310             This->filter.state = State_Running;
311             LeaveCriticalSection(&This->filter.csFilter);
312             LeaveCriticalSection(&pin->thread_lock);
313             return S_OK;
314         }
315
316         for (i = 1; i < (This->cStreams + 1); i++)
317         {
318             hr = BaseOutputPinImpl_Active((BaseOutputPin *)This->ppPins[i]);
319             if (SUCCEEDED(hr))
320                 hr_any = hr;
321         }
322
323         hr = hr_any;
324         if (SUCCEEDED(hr))
325         {
326             LeaveCriticalSection(&This->filter.csFilter);
327             hr = PullPin_StartProcessing(This->pInputPin);
328             EnterCriticalSection(&This->filter.csFilter);
329         }
330
331         if (SUCCEEDED(hr))
332             This->filter.state = State_Running;
333     }
334     LeaveCriticalSection(&This->filter.csFilter);
335     LeaveCriticalSection(&pin->thread_lock);
336
337     return hr;
338 }
339
340 HRESULT WINAPI Parser_GetState(IBaseFilter * iface, DWORD dwMilliSecsTimeout, FILTER_STATE *pState)
341 {
342     ParserImpl *This = (ParserImpl *)iface;
343     PullPin *pin = (PullPin *)This->ppPins[0];
344     HRESULT hr = S_OK;
345
346     TRACE("(%d, %p)\n", dwMilliSecsTimeout, pState);
347
348     EnterCriticalSection(&pin->thread_lock);
349     EnterCriticalSection(&This->filter.csFilter);
350     {
351         *pState = This->filter.state;
352     }
353     LeaveCriticalSection(&This->filter.csFilter);
354
355     if (This->pInputPin && (PullPin_WaitForStateChange(This->pInputPin, dwMilliSecsTimeout) == S_FALSE))
356         hr = VFW_S_STATE_INTERMEDIATE;
357     LeaveCriticalSection(&pin->thread_lock);
358
359     return hr;
360 }
361
362 HRESULT WINAPI Parser_SetSyncSource(IBaseFilter * iface, IReferenceClock *pClock)
363 {
364     ParserImpl *This = (ParserImpl *)iface;
365     PullPin *pin = (PullPin *)This->ppPins[0];
366
367     TRACE("(%p)\n", pClock);
368
369     EnterCriticalSection(&pin->thread_lock);
370     BaseFilterImpl_SetSyncSource(iface,pClock);
371     LeaveCriticalSection(&pin->thread_lock);
372
373     return S_OK;
374 }
375
376 HRESULT WINAPI Parser_GetSyncSource(IBaseFilter * iface, IReferenceClock **ppClock)
377 {
378     return BaseFilterImpl_GetSyncSource(iface, ppClock);
379 }
380
381 /** IBaseFilter implementation **/
382
383 HRESULT WINAPI Parser_EnumPins(IBaseFilter * iface, IEnumPins **ppEnum)
384 {
385     return BaseFilterImpl_EnumPins(iface,ppEnum);
386 }
387
388 HRESULT WINAPI Parser_FindPin(IBaseFilter * iface, LPCWSTR Id, IPin **ppPin)
389 {
390     FIXME("(%p)->(%s,%p)\n", iface, debugstr_w(Id), ppPin);
391
392     /* FIXME: critical section */
393
394     return E_NOTIMPL;
395 }
396
397 HRESULT WINAPI Parser_QueryFilterInfo(IBaseFilter * iface, FILTER_INFO *pInfo)
398 {
399     return BaseFilterImpl_QueryFilterInfo(iface, pInfo);
400 }
401
402 HRESULT WINAPI Parser_JoinFilterGraph(IBaseFilter * iface, IFilterGraph *pGraph, LPCWSTR pName)
403 {
404     return BaseFilterImpl_JoinFilterGraph(iface, pGraph, pName);
405 }
406
407 HRESULT WINAPI Parser_QueryVendorInfo(IBaseFilter * iface, LPWSTR *pVendorInfo)
408 {
409     return BaseFilterImpl_QueryVendorInfo(iface, pVendorInfo);
410 }
411
412 static const  BasePinFuncTable output_BaseFuncTable = {
413     NULL,
414     BaseOutputPinImpl_AttemptConnection,
415     BasePinImpl_GetMediaTypeVersion,
416     Parser_OutputPin_GetMediaType
417 };
418
419 static const BaseOutputPinFuncTable output_BaseOutputFuncTable = {
420     Parser_OutputPin_DecideBufferSize,
421     Parser_OutputPin_DecideAllocator,
422     Parser_OutputPin_BreakConnect
423 };
424
425 HRESULT Parser_AddPin(ParserImpl * This, const PIN_INFO * piOutput, ALLOCATOR_PROPERTIES * props, const AM_MEDIA_TYPE * amt)
426 {
427     IPin ** ppOldPins;
428     HRESULT hr;
429
430     ppOldPins = This->ppPins;
431
432     This->ppPins = CoTaskMemAlloc((This->cStreams + 2) * sizeof(IPin *));
433     memcpy(This->ppPins, ppOldPins, (This->cStreams + 1) * sizeof(IPin *));
434
435     hr = BaseOutputPin_Construct(&Parser_OutputPin_Vtbl, sizeof(Parser_OutputPin), piOutput, &output_BaseFuncTable, &output_BaseOutputFuncTable, &This->filter.csFilter, This->ppPins + (This->cStreams + 1));
436
437     if (SUCCEEDED(hr))
438     {
439         IPin *pPin = This->ppPins[This->cStreams + 1];
440         Parser_OutputPin *pin = (Parser_OutputPin *)pPin;
441         pin->pmt = CoTaskMemAlloc(sizeof(AM_MEDIA_TYPE));
442         CopyMediaType(pin->pmt, amt);
443         pin->dwSamplesProcessed = 0;
444
445         pin->pin.pin.pinInfo.pFilter = (LPVOID)This;
446         pin->allocProps = *props;
447         This->cStreams++;
448         BaseFilterImpl_IncrementPinVersion((BaseFilter*)This);
449         CoTaskMemFree(ppOldPins);
450     }
451     else
452     {
453         CoTaskMemFree(This->ppPins);
454         This->ppPins = ppOldPins;
455         ERR("Failed with error %x\n", hr);
456     }
457
458     return hr;
459 }
460
461 static HRESULT Parser_RemoveOutputPins(ParserImpl * This)
462 {
463     /* NOTE: should be in critical section when calling this function */
464     HRESULT hr;
465     ULONG i;
466     IPin ** ppOldPins = This->ppPins;
467
468     TRACE("(%p)\n", This);
469
470     /* reduce the pin array down to 1 (just our input pin) */
471     This->ppPins = CoTaskMemAlloc(sizeof(IPin *) * 1);
472     memcpy(This->ppPins, ppOldPins, sizeof(IPin *) * 1);
473
474     for (i = 0; i < This->cStreams; i++)
475     {
476         hr = ((BaseOutputPin *)ppOldPins[i + 1])->pFuncsTable->pfnBreakConnect((BaseOutputPin *)ppOldPins[i + 1]);
477         TRACE("Disconnect: %08x\n", hr);
478         IPin_Release(ppOldPins[i + 1]);
479     }
480
481     BaseFilterImpl_IncrementPinVersion((BaseFilter*)This);
482     This->cStreams = 0;
483     CoTaskMemFree(ppOldPins);
484
485     return S_OK;
486 }
487
488 static HRESULT WINAPI Parser_ChangeStart(IMediaSeeking *iface)
489 {
490     FIXME("(%p) filter hasn't implemented start position change!\n", iface);
491     return S_OK;
492 }
493
494 static HRESULT WINAPI Parser_ChangeStop(IMediaSeeking *iface)
495 {
496     FIXME("(%p) filter hasn't implemented stop position change!\n", iface);
497     return S_OK;
498 }
499
500 static HRESULT WINAPI Parser_ChangeRate(IMediaSeeking *iface)
501 {
502     FIXME("(%p) filter hasn't implemented rate change!\n", iface);
503     return S_OK;
504 }
505
506
507 static HRESULT WINAPI Parser_Seeking_QueryInterface(IMediaSeeking * iface, REFIID riid, LPVOID * ppv)
508 {
509     ParserImpl *This = impl_from_IMediaSeeking(iface);
510
511     return IUnknown_QueryInterface((IUnknown *)This, riid, ppv);
512 }
513
514 static ULONG WINAPI Parser_Seeking_AddRef(IMediaSeeking * iface)
515 {
516     ParserImpl *This = impl_from_IMediaSeeking(iface);
517
518     return IUnknown_AddRef((IUnknown *)This);
519 }
520
521 static ULONG WINAPI Parser_Seeking_Release(IMediaSeeking * iface)
522 {
523     ParserImpl *This = impl_from_IMediaSeeking(iface);
524
525     return IUnknown_Release((IUnknown *)This);
526 }
527
528 static const IMediaSeekingVtbl Parser_Seeking_Vtbl =
529 {
530     Parser_Seeking_QueryInterface,
531     Parser_Seeking_AddRef,
532     Parser_Seeking_Release,
533     SourceSeekingImpl_GetCapabilities,
534     SourceSeekingImpl_CheckCapabilities,
535     SourceSeekingImpl_IsFormatSupported,
536     SourceSeekingImpl_QueryPreferredFormat,
537     SourceSeekingImpl_GetTimeFormat,
538     SourceSeekingImpl_IsUsingTimeFormat,
539     SourceSeekingImpl_SetTimeFormat,
540     SourceSeekingImpl_GetDuration,
541     SourceSeekingImpl_GetStopPosition,
542     SourceSeekingImpl_GetCurrentPosition,
543     SourceSeekingImpl_ConvertTimeFormat,
544     SourceSeekingImpl_SetPositions,
545     SourceSeekingImpl_GetPositions,
546     SourceSeekingImpl_GetAvailable,
547     SourceSeekingImpl_SetRate,
548     SourceSeekingImpl_GetRate,
549     SourceSeekingImpl_GetPreroll
550 };
551
552 static HRESULT WINAPI Parser_OutputPin_DecideBufferSize(BaseOutputPin *iface, IMemAllocator *pAlloc, ALLOCATOR_PROPERTIES *ppropInputRequest)
553 {
554     Parser_OutputPin *This = (Parser_OutputPin *)iface;
555     ALLOCATOR_PROPERTIES actual;
556
557     if (ppropInputRequest->cbAlign && ppropInputRequest->cbAlign != This->allocProps.cbAlign)
558         FIXME("Requested Buffer cbAlign mismatch %i,%i\n",This->allocProps.cbAlign, ppropInputRequest->cbAlign);
559     if (ppropInputRequest->cbPrefix)
560         FIXME("Requested Buffer cbPrefix mismatch %i,%i\n",This->allocProps.cbPrefix, ppropInputRequest->cbPrefix);
561     if (ppropInputRequest->cbBuffer)
562         FIXME("Requested Buffer cbBuffer mismatch %i,%i\n",This->allocProps.cbBuffer, ppropInputRequest->cbBuffer);
563     if (ppropInputRequest->cBuffers)
564         FIXME("Requested Buffer cBuffers mismatch %i,%i\n",This->allocProps.cBuffers, ppropInputRequest->cBuffers);
565
566     return IMemAllocator_SetProperties(pAlloc, &This->allocProps, &actual);
567 }
568
569 static HRESULT WINAPI Parser_OutputPin_GetMediaType(BasePin *iface, int iPosition, AM_MEDIA_TYPE *pmt)
570 {
571     Parser_OutputPin *This = (Parser_OutputPin *)iface;
572     if (iPosition < 0)
573         return E_INVALIDARG;
574     if (iPosition > 0)
575         return VFW_S_NO_MORE_ITEMS;
576     CopyMediaType(pmt, This->pmt);
577     return S_OK;
578 }
579
580 static HRESULT WINAPI Parser_OutputPin_DecideAllocator(BaseOutputPin *iface, IMemInputPin *pPin, IMemAllocator **pAlloc)
581 {
582     Parser_OutputPin *This = (Parser_OutputPin *)iface;
583     HRESULT hr;
584
585     pAlloc = NULL;
586
587     if (This->alloc)
588         hr = IMemInputPin_NotifyAllocator(pPin, This->alloc, This->readonly);
589     else
590         hr = VFW_E_NO_ALLOCATOR;
591
592     return hr;
593 }
594
595 static HRESULT WINAPI Parser_OutputPin_BreakConnect(BaseOutputPin *This)
596 {
597     HRESULT hr;
598
599     TRACE("(%p)->()\n", This);
600
601     EnterCriticalSection(This->pin.pCritSec);
602     if (!This->pin.pConnectedTo || !This->pMemInputPin)
603         hr = VFW_E_NOT_CONNECTED;
604     else
605     {
606         hr = IPin_Disconnect(This->pin.pConnectedTo);
607         IPin_Disconnect((IPin *)This);
608     }
609     LeaveCriticalSection(This->pin.pCritSec);
610
611     return hr;
612 }
613
614
615 static HRESULT WINAPI Parser_OutputPin_QueryInterface(IPin * iface, REFIID riid, LPVOID * ppv)
616 {
617     Parser_OutputPin *This = (Parser_OutputPin *)iface;
618
619     TRACE("(%s, %p)\n", qzdebugstr_guid(riid), ppv);
620
621     *ppv = NULL;
622
623     if (IsEqualIID(riid, &IID_IUnknown))
624         *ppv = iface;
625     else if (IsEqualIID(riid, &IID_IPin))
626         *ppv = iface;
627     else if (IsEqualIID(riid, &IID_IMediaSeeking))
628     {
629         return IBaseFilter_QueryInterface(This->pin.pin.pinInfo.pFilter, &IID_IMediaSeeking, ppv);
630     }
631
632     if (*ppv)
633     {
634         IUnknown_AddRef((IUnknown *)(*ppv));
635         return S_OK;
636     }
637
638     FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
639
640     return E_NOINTERFACE;
641 }
642
643 static ULONG WINAPI Parser_OutputPin_Release(IPin * iface)
644 {
645     Parser_OutputPin *This = (Parser_OutputPin *)iface;
646     ULONG refCount = InterlockedDecrement(&This->pin.pin.refCount);
647     
648     TRACE("(%p)->() Release from %d\n", iface, refCount + 1);
649
650     if (!refCount)
651     {
652         FreeMediaType(This->pmt);
653         CoTaskMemFree(This->pmt);
654         FreeMediaType(&This->pin.pin.mtCurrent);
655         CoTaskMemFree(This);
656         return 0;
657     }
658     return refCount;
659 }
660
661 static HRESULT WINAPI Parser_OutputPin_Connect(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
662 {
663     Parser_OutputPin *This = (Parser_OutputPin *)iface;
664     ParserImpl *parser = (ParserImpl *)This->pin.pin.pinInfo.pFilter;
665
666     /* Set the allocator to our input pin's */
667     EnterCriticalSection(This->pin.pin.pCritSec);
668     This->alloc = parser->pInputPin->pAlloc;
669     LeaveCriticalSection(This->pin.pin.pCritSec);
670
671     return BaseOutputPinImpl_Connect(iface, pReceivePin, pmt);
672 }
673
674 static HRESULT WINAPI Parser_OutputPin_QueryAccept(IPin *iface, const AM_MEDIA_TYPE * pmt)
675 {
676     Parser_OutputPin *This = (Parser_OutputPin *)iface;
677
678     TRACE("()\n");
679     dump_AM_MEDIA_TYPE(pmt);
680
681     return (memcmp(This->pmt, pmt, sizeof(AM_MEDIA_TYPE)) == 0);
682 }
683
684 static const IPinVtbl Parser_OutputPin_Vtbl = 
685 {
686     Parser_OutputPin_QueryInterface,
687     BasePinImpl_AddRef,
688     Parser_OutputPin_Release,
689     Parser_OutputPin_Connect,
690     BaseOutputPinImpl_ReceiveConnection,
691     BaseOutputPinImpl_Disconnect,
692     BasePinImpl_ConnectedTo,
693     BasePinImpl_ConnectionMediaType,
694     BasePinImpl_QueryPinInfo,
695     BasePinImpl_QueryDirection,
696     BasePinImpl_QueryId,
697     Parser_OutputPin_QueryAccept,
698     BasePinImpl_EnumMediaTypes,
699     BasePinImpl_QueryInternalConnections,
700     BaseOutputPinImpl_EndOfStream,
701     BaseOutputPinImpl_BeginFlush,
702     BaseOutputPinImpl_EndFlush,
703     BasePinImpl_NewSegment
704 };
705
706 static HRESULT WINAPI Parser_PullPin_Disconnect(IPin * iface)
707 {
708     HRESULT hr;
709     PullPin *This = (PullPin *)iface;
710
711     TRACE("()\n");
712
713     EnterCriticalSection(&This->thread_lock);
714     EnterCriticalSection(This->pin.pCritSec);
715     {
716         if (This->pin.pConnectedTo)
717         {
718             FILTER_STATE state;
719             ParserImpl *Parser = (ParserImpl *)This->pin.pinInfo.pFilter;
720
721             LeaveCriticalSection(This->pin.pCritSec);
722             hr = IBaseFilter_GetState(This->pin.pinInfo.pFilter, INFINITE, &state);
723             EnterCriticalSection(This->pin.pCritSec);
724
725             if (SUCCEEDED(hr) && (state == State_Stopped) && SUCCEEDED(Parser->fnDisconnect(Parser)))
726             {
727                 LeaveCriticalSection(This->pin.pCritSec);
728                 PullPin_Disconnect(iface);
729                 EnterCriticalSection(This->pin.pCritSec);
730                 hr = Parser_RemoveOutputPins((ParserImpl *)This->pin.pinInfo.pFilter);
731             }
732             else
733                 hr = VFW_E_NOT_STOPPED;
734         }
735         else
736             hr = S_FALSE;
737     }
738     LeaveCriticalSection(This->pin.pCritSec);
739     LeaveCriticalSection(&This->thread_lock);
740
741     return hr;
742 }
743
744 static HRESULT WINAPI Parser_PullPin_ReceiveConnection(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
745 {
746     HRESULT hr;
747
748     TRACE("()\n");
749
750     hr = PullPin_ReceiveConnection(iface, pReceivePin, pmt);
751     if (FAILED(hr))
752     {
753         BasePin *This = (BasePin *)iface;
754
755         EnterCriticalSection(This->pCritSec);
756         Parser_RemoveOutputPins((ParserImpl *)This->pinInfo.pFilter);
757         LeaveCriticalSection(This->pCritSec);
758     }
759
760     return hr;
761 }
762
763 static HRESULT WINAPI Parser_PullPin_EnumMediaTypes(IPin *iface, IEnumMediaTypes **ppEnum)
764 {
765     BasePin *This = (BasePin *)iface;
766
767     TRACE("(%p/%p)->(%p)\n", This, iface, ppEnum);
768
769     return EnumMediaTypes_Construct(This, BasePinImpl_GetMediaType, BasePinImpl_GetMediaTypeVersion, ppEnum);
770 }
771
772 static const IPinVtbl Parser_InputPin_Vtbl =
773 {
774     PullPin_QueryInterface,
775     BasePinImpl_AddRef,
776     PullPin_Release,
777     BaseInputPinImpl_Connect,
778     Parser_PullPin_ReceiveConnection,
779     Parser_PullPin_Disconnect,
780     BasePinImpl_ConnectedTo,
781     BasePinImpl_ConnectionMediaType,
782     BasePinImpl_QueryPinInfo,
783     BasePinImpl_QueryDirection,
784     BasePinImpl_QueryId,
785     PullPin_QueryAccept,
786     Parser_PullPin_EnumMediaTypes,
787     BasePinImpl_QueryInternalConnections,
788     PullPin_EndOfStream,
789     PullPin_BeginFlush,
790     PullPin_EndFlush,
791     PullPin_NewSegment
792 };